Module:RFX report

-- This module gives report for RFS, RFGR, RFC.

local rfx = require( 'Module:RFX' ) local colours = mw.loadData( 'Module:RFX report/colour' )

local p = {}

local function makeRow( rfxObject ) if not ( type( rfxObject ) == 'table' and rfxObject.getTitleObject and rfxObject.getSupportUsers ) then return nil end local style = '' local styleInline = '' local status = rfxObject:getStatus if status == 'pending closure' then style = ' style="background: #f8cdc6;" |' styleInline = ' background: #f8cdc6;' end local page = rfxObject:getTitleObject.prefixedText local user = rfxObject.user or rfxObject:getTitleObject.subpageText local supports = rfxObject.supports local abstains = rfxObject.abstains local opposes = rfxObject.opposes local percent = rfxObject.percent local colour if percent then colour = colours[ rfxObject.type ][ percent ] end colour = colour or '' local votes if supports and abstains and opposes and percent then votes = mw.ustring.format( [==[           styleInline, page, supports,            styleInline, page, abstains,            styleInline, page, opposes,            colour, percent        ) else votes = '\n| colspan="4" style="background: #f8cdc6;" | Error parsing votes' end if status then status = mw.language.getContentLanguage:ucfirst( status ) if status == 'Pending closure' then status = 'Pending closure...' end status = mw.ustring.format( '\n| %s %s', style, status ) else status = '\n| style="background: #f8cdc6;" | Error getting status' end local endTime = rfxObject.endTime local secondsLeft = rfxObject:getSecondsLeft local timeLeft = rfxObject:getTimeLeft local time if endTime and timeLeft then time = mw.ustring.format( '\n| %s %s\n| %s %s', style, endTime, style, timeLeft ) else time = '\n| colspan="2" style="background: #f8cdc6;" | Error parsing end time' end local dupes = rfxObject:dupesExist if dupes then dupes = "yes" elseif dupes == false then dupes = 'no' else dupes = '--' end local report = rfxObject:getReport if report then report = mw.ustring.format( '\n|%s [%s report]', style, tostring( report ) ) else report = '\n| style="background: #f8cdc6;" | Report not found' end return mw.ustring.format(       '\n|-\n|%s %s%s%s%s\n| style="text-align: center;%s" | %s%s',        style, page, user, votes, status, time, styleInline, dupes, report    ) end
 * style="text-align: right;%s" | %d
 * style="text-align: right;%s" | %d
 * style="text-align: right;%s" | %d
 * style="text-align: right; background: #%s;" | %d]==],

local function makeHeading( rfxType ) if type( rfxType ) ~= 'string' then return nil end return mw.ustring.format(       '\n|-\n! %s candidate !! S !! A !! O !! S%% !! Status !! Ending (UTC) !! Time left !! Dupes? !! Report',        rfxType    ) end

local function getRfxes( args ) if type( args ) ~= 'table' then return nil end local nums, ret = {}, {} for k, v in pairs( args ) do       if type( k ) == 'number' and k >= 1 and math.floor( k ) == k and k ~= math.huge then table.insert( nums, k ) end end table.sort( nums ) for i, v in ipairs( nums ) do       ret[ i ] = args[ v ] end return ret end

local function makeReportRows( args ) local rfxes = getRfxes( args ) if not rfxes then return nil end -- Get RfX objects and separate RfGRs, RfSs and RfCs. local rfgrs = {} local rfss = {} local rfcs = {} for i, rfxPage in ipairs( rfxes ) do       local rfxObject = rfx.new( rfxPage ) if rfxObject then if rfxObject.type == 'rfgr' then table.insert( rfgrs, rfxObject ) elseif rfxObject.type == 'rfs' then table.insert( rfss, rfxObject ) elseif rfxObject.type == 'rfc' then table.insert( rfcs, rfxObject ) end end end local ret = {} if #rfgrs > 0 then table.insert( ret, makeHeading( 'RfGR' ) ) for i, rfgrObject in ipairs( rfgrs ) do           table.insert( ret, makeRow( rfgrObject ) ) end end if #rfss > 0 then table.insert( ret, makeHeading( 'RfS' ) ) for i, rfsObject in ipairs( rfss ) do           table.insert( ret, makeRow( rfsObject ) ) end end if #rfcs > 0 then table.insert( ret, makeHeading( 'RfC' ) ) for i, rfcObject in ipairs( rfcs ) do           table.insert( ret, makeRow( rfcObject ) ) end end return table.concat( ret ) end

local function makeReport( args ) local purgeLink = mw.title.getCurrentTitle:fullUrl( 'action=purge' ) local header = mw.ustring.format(       '\n|-\n! colspan="10" style="text-align: center;" | Requests for global rights, stewardship and comment [%s update] ',        purgeLink    ) local rows = makeReportRows( args ) or '' if rows == '' then rows = '\n|-\n| colspan="10" | No current discussions. Recent RfGRs: (global rights, unsuccessful) Recent RfSs: (successful, unsuccessful) Recent RfCs: (successful, unsuccessful '   end    local style = args.style    if not style then        local float = args.float or args.align or 'right'        local clear = args.clear or 'left'        style = mw.ustring.format( 'style="white-space:wrap; clear: %s; margin-top: 0em; margin-bottom: .5em; float: %s; padding: .5em 0em 0em 1.4em; background: #ffffff; border-collapse: collapse; border-spacing: 0;"', clear, float )   end    return mw.ustring.format( '\n{| class="wikitable" %s%s%s\n|-\n|}', style, header, rows ) end

function p.main( frame ) -- If called via #invoke, use the args passed into the invoking -- template, or the args passed to #invoke if any exist. Otherwise -- assume args are being passed directly in from the debug console -- or from another Lua module. local origArgs if frame == mw.getCurrentFrame then origArgs = frame:getParent.args for k, v in pairs( frame.args ) do           origArgs = frame.args break end else origArgs = frame end -- Trim whitespace and remove blank arguments. local args = {} for k, v in pairs( origArgs ) do       v = mw.text.trim( v ) if v ~= '' then args[k] = v       end end return makeReport( args ) end

return p