Jump to content

Module:RFX report/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Izno (talk | contribs) at 21:20, 5 September 2021 (put that where it should go). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- This module is a replacement for the RfX report bot.

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 pending_close_color = '#f8cdc6'
    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:tag('td')
    	:css('background-color', is_pending_close and pending_close_color or nil)
    	:wikitext('[[' .. page .. '|' .. user .. ']]')
    	:done()
    
    local supports = rfxObject.supports
    local opposes = rfxObject.opposes
    local neutrals = rfxObject.neutrals
    local percent = rfxObject.percent
    local error_color = '#f8cdc6'
	
    if supports and opposes and neutrals and percent then
    	row:tag('td')
    		:css('text-align', 'right')
    		:css('background-color', is_pending_close and pending_close_color or nil)
    		:wikitext('[[' .. page .. '#Support|' .. tostring(supports) .. ']]')
    		:done()
    	:tag('td')
    		:css('text-align', 'right')
    		:css('background-color', is_pending_close and pending_close_color or nil)
    		:wikitext('[[' .. page .. '#Support|' .. tostring(opposes) .. ']]')
    		:done()
    	:tag('td')
    		:css('text-align', 'right')
    		:css('background-color', is_pending_close and pending_close_color or nil)
    		:wikitext('[[' .. page .. '#Support|' .. tostring(neutrals) .. ']]')
    		:done()
    	:tag('td')
    		:css('text-align', 'right')
    		:css('background-color', percent and colours[ rfxObject.type ][ percent ] or nil)
    		:wikitext(tostring(percent))
    		:done()
    else
        row:tag('td')
        	:attr('colspan', '4')
        	:css('background-color', error_color)
        	:wikitext('Error parsing votes')
        	:done()
    end
    row:tag('td')
    if status then
        status = mw.language.getContentLanguage():ucfirst( status )
        if is_pending_close then
            status = 'Pending closure...'
        end
        row:css('background-color', is_pending_close and pending_close_color or nil)
    		:wikitext(status)
    else
    	row:css('background-color', error_color)
    		:wikitext('Error getting status')
    end
    row:done()
    
    local endTime = rfxObject.endTime
    local secondsLeft = rfxObject:getSecondsLeft()
    local timeLeft = rfxObject:getTimeLeft()
    if endTime and timeLeft then
    	row:tag('td')
    		:css('background-color', is_pending_close and pending_close_color or nil)
    		:wikitext(endTime)
    		:done()
    	:tag('td')
    		:css('background-color', is_pending_close and pending_close_color or nil)
    		:wikitext(timeLeft)
    		:done()
    else
    	row:tag('td')
    		:attr('colspan', '2')
    		:css('background-color', error_color)
    		:wikitext('Error parsing end time')
    		:done()
    end
    local dupes = rfxObject:dupesExist()
    if dupes then
        dupes = "'''yes'''"
    elseif dupes == false then
        dupes = 'no'
    else
        dupes = '--'
    end
    row:tag('tr')
    	:css('text-align', 'center')
    	:css('background-color', is_pending_close and pending_close_color or nil)
    	:wikitext(dupes)
    	:done()
    local report = rfxObject:getReport()
    
	row:tag('tr')
    if report then
    	row:css('background-color', is_pending_close and pending_close_color or nil)
    		:wikitext('[' .. tostring(report) .. ' report]')
    else
    	row:css('background-color', error_color)
    		:wikitext('Report not found')
    end
    row:allDone()
    
    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'):allDone()
    	
    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]])')
    			:allDone() -- tr
    	table.insert(ret, row)
        return ret
    end
    -- Get RfX objects and separate RfAs and RfBs.
    local rfas = {}
    local rfbs = {}
    for i, 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 i, rfaObject in ipairs( rfas ) do
            table.insert( ret, makeRow( rfaObject ) )
        end
    end
    if #rfbs > 0 then
        table.insert( ret, makeHeading( 'rfb' ) )
        for i, 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' )
			))
			:done()
		:done() -- caption
	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