local rfx = require( 'Module:Rfx' )
local colours = mw.loadData( 'Module:RFX report/colour' )
local p = {}
local function getRfxes()
-- Get the title object for [[Wikipedia:Requests for adminship]].
local performed, rfa = pcall( mw.title.new, 'Wikipedia:Requests for adminship' )
if not performed or ( performed and not rfa ) then
return nil
end
local rfaText = rfa:getContent()
if not rfaText then
return nil
end
-- Return a table with a list of pages transcluded from
-- [[Wikipedia:Requests for adminship]], minus the exceptions
-- which are always transcluded there.
local t = {}
local exceptions = { 'Front matter', 'Header', 'bureaucratship' }
for rfxPage, rfxSubpage in mw.ustring.gmatch(
rfaText, '{{[ _]*([wW]ikipedia:[rR]equests for %w+/([^{}]-))[ _]*}}'
) do
local isException = false
for _, v in ipairs( exceptions ) do
if rfxSubpage == v then
isException = true
end
end
if not isException then
table.insert( t, rfxPage )
end
end
return t
end
local function makeRow( rfxObject )
if not ( type( rfxObject ) == 'table' and rfxObject.getTitleObject and rfxObject.getSupportUsers ) then
return mw.html.create()
end
local status = rfxObject:getStatus()
local is_pending_close = false
if status == 'pending closure' then
is_pending_close = true
end
local user = rfxObject.user or rfxObject:getTitleObject().subpageText
local page = rfxObject:getTitleObject().prefixedText
local row = mw.html.create('tr')
row:addClass(is_pending_close and 'rfx-report-pending' or nil)
row:tag('td')
:wikitext('[[' .. page .. '|' .. user .. ']]')
local supports = rfxObject.supports
local opposes = rfxObject.opposes
local neutrals = rfxObject.neutrals
local percent = rfxObject.percent
local error_class = 'rfx-report-error'
if supports and opposes and neutrals and percent then
row:tag('td')
:addClass('rfx-error-sonp')
:wikitext('[[' .. page .. '#Support|' .. tostring(supports) .. ']]')
:done()
:tag('td')
:addClass('rfx-error-sonp')
:wikitext('[[' .. page .. '#Support|' .. tostring(opposes) .. ']]')
:done()
:tag('td')
:addClass('rfx-error-sonp')
:wikitext('[[' .. page .. '#Support|' .. tostring(neutrals) .. ']]')
:done()
:tag('td')
:addClass('rfx-error-sonp')
:css('background-color', percent and ('#' .. colours[ rfxObject.type ][ percent ]) or nil)
:wikitext(tostring(percent))
else
row:tag('td')
:attr('colspan', '4')
:addClass(error_class)
:wikitext('Error parsing votes')
end
if status then
status = mw.language.getContentLanguage():ucfirst( status )
if is_pending_close then
status = 'Pending closure...'
end
row:tag('td')
:wikitext(status)
else
row:tag('td')
:addClass(error_class)
:wikitext('Error getting status')
end
local endTime = rfxObject.endTime
local secondsLeft = rfxObject:getSecondsLeft()
local timeLeft = rfxObject:getTimeLeft()
if endTime and timeLeft then
row:tag('td')
:wikitext(endTime)
:done()
:tag('td')
:wikitext(timeLeft)
else
row:tag('td')
:attr('colspan', '2')
:addClass(error_class)
:wikitext('Error parsing end time')
end
local dupes = rfxObject:dupesExist()
if dupes then
dupes = "'''yes'''"
elseif dupes == false then
dupes = 'no'
else
dupes = '--'
end
row:tag('td')
:addClass('rfx-report-dupes')
:wikitext(dupes)
local report = rfxObject:getReport()
if report then
row:tag('td')
:wikitext('[' .. tostring(report) .. ' report]')
else
row:tag('td')
:addClass(error_class)
:wikitext('Report not found')
end
return row
end
local function makeHeading( rfxType )
local rfxCaps
if rfxType == 'rfa' then
rfxCaps = 'RfA'
elseif rfxType == 'rfb' then
rfxCaps = 'RfB'
else
return mw.html.create()
end
local row = mw.html.create('tr')
row:tag('th'):wikitext(rfxCaps .. ' candidate'):done()
:tag('th')
:tag('abbr'):attr('title', 'Support'):wikitext('S'):done()
:done()
:tag('th')
:tag('abbr'):attr('title', 'Oppose'):wikitext('O'):done()
:done()
:tag('th')
:tag('abbr'):attr('title', 'Neutral'):wikitext('N'):done()
:done()
:tag('th')
:tag('abbr'):attr('title', '% Support'):wikitext('S %'):done()
:done()
:tag('th'):wikitext('Status'):done()
:tag('th'):wikitext('Ending (UTC)'):done()
:tag('th'):wikitext('Time left'):done()
:tag('th')
:tag('abbr'):attr('title', 'Has duplicate votes?'):wikitext('Dups?'):done()
:done()
:tag('th'):wikitext('Report')
return row
end
local function makeReportRows()
local rfxes = getRfxes()
local ret = {}
if not rfxes then
local row = mw.html.create('tr')
row:tag('td')
:attr('colspan', '10')
:wikitext('No current discussions. ')
:tag('small')
:wikitext('Recent RfAs: ([[Wikipedia:Successful requests for adminship|successful]], [[Wikipedia:Unsuccessful adminship candidacies (Chronological)|unsuccessful]]) Recent RfBs: ([[Wikipedia:Successful bureaucratship candidacies|successful]], [[Wikipedia:Unsuccessful bureaucratship candidacies|unsuccessful]])')
table.insert(ret, row)
return ret
end
-- Get RfX objects and separate RfAs and RfBs.
local rfas = {}
local rfbs = {}
for _, rfxPage in ipairs( rfxes ) do
local rfxObject = rfx.new( rfxPage )
if rfxObject then
if rfxObject.type == 'rfa' then
table.insert( rfas, rfxObject )
elseif rfxObject.type == 'rfb' then
table.insert( rfbs, rfxObject )
end
end
end
if #rfas > 0 then
table.insert( ret, makeHeading( 'rfa' ) )
for _, rfaObject in ipairs( rfas ) do
table.insert( ret, makeRow( rfaObject ) )
end
end
if #rfbs > 0 then
table.insert( ret, makeHeading( 'rfb' ) )
for _, rfbObject in ipairs( rfbs ) do
table.insert( ret, makeRow( rfbObject ) )
end
end
return ret
end
local function makeReport( args )
local report = mw.html.create('table')
report:addClass('wikitable')
:addClass('rfx-report')
if not args.style then
report:addClass('rfx-report-styled')
:css('float', args.float or args.align or nil)
:css('clear', args.clear or nil)
end
report:tag('caption')
:wikitext('Requests for [[Wikipedia:Requests for adminship|adminship]] and [[Wikipedia:Requests for bureaucratship|bureaucratship]]')
:tag('span')
:addClass('rfx-report-purge')
:addClass('plainlinks')
:wikitext(mw.ustring.format(
'[%s update]', mw.title.getCurrentTitle():fullUrl( 'action=purge' )
))
for _, row in ipairs(makeReportRows()) do
report:node(row)
end
return mw.getCurrentFrame():extensionTag{
name = 'templatestyles', args = { src = 'Module:RFX report/styles.css' }
} .. tostring(report)
end
function p.main( frame )
-- Process the arguments.
local args
if frame == mw.getCurrentFrame() then
args = frame:getParent().args
for k, v in pairs( frame.args ) do
args = frame.args
break
end
else
args = frame
end
return makeReport( args )
end
return p