Module:RFX report: Difference between revisions

Content deleted Content added
don't match higher-level headings
use Module:Rfx, clean up my spaghetti code, add a new header, and add a message to display when there are no current RfXes
Line 1:
-- This module is a replacement for the RfX report bot.
 
local rfx = require( 'Module:Rfx' )
--[==[
local colours = mw.loadData( 'Module:RFX report/colour' )
TODO:
* Add a purge link (but where?)
* Show a message if there are no active RfXs (again, where?)
* Get the last active RfX date if there are none open?
This could be screen-scraped from
[[Wikipedia:Successful requests for adminship/2013]] etc.
* Comment the code properly
--]==]
 
local colours = mw.loadData('Module:RFX report/colour')
 
local p = {}
local r = {
rfa = {},
rfb = {}
}
 
local function getLongRfxNamegetRfxes(rfxType)
-- Get the title object for [[Wikipedia:Requests for adminship]].
if rfxType == 'rfa' then
local noError, rfa = returnpcall( mw.title.new, 'Wikipedia:Requests for adminship' )
elseifif rfxTypenot ==noError 'rfb'or ( noError and not rfa ) then
return 'bureaucratship'nil
else
error('invalid rfx code detected - must be either "rfa" or "rfb"', 2)
end
local rfaText = rfa:getContent()
end
if not rfaText then
 
return nil
local function matchRfx(text, rfxType)
end
return mw.ustring.gmatch( text, '{{[wW]ikipedia:[rR]equests for ' .. getLongRfxName(rfxType) .. '/([^{}]-)}}' )
end
-- Return a table with a list of pages transcluded from
 
-- [[Wikipedia:Requests for adminship]], minus the exceptions
local function addRfxToTable(text, rfxType, exceptions)
-- which are always transcluded there.
for rfxPage in matchRfx(text, rfxType) do
local t = {}
local exceptions = { 'Front matter', 'bureaucratship' }
for rfxPage, rfxSubpage in mw.ustring.gmatch( rfaText, '{{[ _]*([wW]ikipedia:[rR]equests for %w+/([^{}]-))[ _]*}}' ) do
local isException = false
iffor _, v in ipairs( exceptions then) do
forif _,rfxSubpage == v in ipairs(exceptions) dothen
if rfxPageisException == v thentrue
isException = true
end
end
end
if not isException then
table.insert( r[rfxType]t, { page = 'Wikipedia:Requests for ' .. getLongRfxName(rfxType) .. '/' .. rfxPage } )
end
end
return t
end
 
local function getRfxPagesmakeRow( rfxObject )
if not ( type( rfxObject ) == 'table' and rfxObject.getTitleObject and rfxObject.getSupportUsers ) then
local title = mw.title.new( 'Wikipedia:Requests for adminship' )
return nil
local text = title:getContent()
addRfxToTable( text, 'rfa', {'Front matter', 'bureaucratship'} )
addRfxToTable( text, 'rfb' )
end
 
local function getRfxSections(page)
local pageObject = mw.title.new(page)
if not pageObject then
error(tostring(page) .. ' is an invalid title', 2)
end
local pageTextstyle = pageObject:getContent()''
local styleInline = ''
if not pageText then
local status = rfxObject:getStatus()
error('could not get page content from page ' .. pageObject.title, 2)
if status == 'pending closure' then
style = ' style="background: #f8cdc6;" |'
styleInline = ' background: #f8cdc6;'
end
local intro, s, o, npage = mwrfxObject:getTitleObject().ustring.match(prefixedText
local user = rfxObject.user or rfxObject:getTitleObject().subpageText
pageText,
local supports = rfxObject.supports
'^(.-)\n====[^=\n][^\n]-====.-'
local opposes = rfxObject.opposes
.. '\n=====%s*[sS]upport%s*=====(.-)'
local neutrals = rfxObject.neutrals
.. '\n=====%s*[oO]ppose%s*=====(.-)'
local percent = rfxObject.percent
.. '\n=====%s*[nN]eutral%s*=====(.-)$'
local colour
 
)if percent then
colour = colours[ rfxObject.type ][ percent ]
return intro, s, o, n
end
 
-- Returns a table of usernames that voted in a particular section.
local function getVoteTable(section)
section = mw.ustring.match(section, '^(.-\n#.-)\n[^#]') or section -- Discard subsequent numbered lists.
local t = {}
for vote in mw.ustring.gmatch(section, '\n#[^#*;:][^\n]*') do
local username = false
for link in mw.ustring.gmatch(vote, '%[%[([^%[%]]-)%]%]') do
-- If there is a pipe, get the text before it.
if mw.ustring.match(link, '|') then
link = mw.ustring.match(link, '^(.-)|')
end
-- If there is a slash, get the text before that.
if mw.ustring.match(link, '/') then
link = mw.ustring.match(link, '^(.-)/')
end
-- Decode html entities and percent encodings.
link = mw.text.decode(link, true)
link = mw.uri.decode(link, 'WIKI')
-- If there is a hash, get the text before that.
if mw.ustring.match(link, '#') then
link = mw.ustring.match(link, '^(.-)#')
end
local userLink = mw.ustring.match(link, '^[%s_]*[uU][sS][eE][rR][%s_]*:[%s_]*(.-)[%s_]*$')
local userTalkLink = mw.ustring.match(link, '^[%s_]*[uU][sS][eE][rR][%s_]*[tT][aA][lL][kK][%s_]*:[%s_]*(.-)[%s_]*$')
if userLink then
username = userLink
elseif userTalkLink then
username = userTalkLink
end
end
if not username then
username = mw.ustring.format( 'Error processing vote no. %d. (Random number: %.8f.)', #t, math.random() )
end
table.insert(t, username)
end
colour = colour or ''
return t
local votes
end
if supports and opposes and neutrals and percent then
 
votes = mw.ustring.format( [==[
local function checkDups(...)
local t = {}
| style="text-align: right;%s" | [[%s#Support|%d]]
local tables = {...}
| style="text-align: right;%s" | [[%s#Oppose|%d]]
for i, v in ipairs(tables) do
| style="text-align: right;%s" | [[%s#Neutral|%d]]
for j, name in ipairs(v) do
| style="text-align: right; background: #%s;" | %d]==],
if t[name] then
styleInline, page, return truesupports,
elsestyleInline, page, opposes,
styleInline, page, t[name] = trueneutrals,
endcolour, 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', 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\n| %s', endTime, timeLeft )
else
time = '\n| colspan="2" style="background: #f8cdc6;" | Error parsing end time'
end
local dupes = rfxObject:dupesExist()
return false
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%s\n| style="text-align: center;%s" | %s%s',
style, page, user, votes, status, time, styleInline, dupes, report
)
end
 
local function getEndTimemakeHeading(intro rfxType )
local rfxCaps
local ret = mw.ustring.match(intro, '(%d%d:%d%d, %d+ %w+ %d+) %(UTC%)')
if rfxType == 'rfa' then
return ret
rfxCaps = 'RfA'
end
elseif rfxType == 'rfb' then
 
rfxCaps = 'RfB'
local function getUser(intro)
local ret = mw.ustring.match(intro, '===%s*%[%[%s*[wW]ikipedia%s*:%s*[rR]equests for %w+/.-|%s*(.-)%s*%]%]%s*===')
return ret
end
 
local function getTimeLeft(endTime)
local lang = mw.getContentLanguage()
local now = tonumber( lang:formatDate("U") )
endTime = tonumber( lang:formatDate("U", endTime) )
local secondsLeft = endTime - now
local daysLeft
if secondsLeft <= 0 then
daysLeft = 'Pending closure...'
else
return nil
daysLeft = mw.ustring.gsub( lang:formatDuration(secondsLeft, {'days', 'hours'}), ' and', ',')
end
return daysLeftmw.ustring.format(
'\n|-\n! %s candidate !! S !! O !! N !! S%% !! Status !! Ending (UTC) !! Time left !! Dupes? !! Report',
rfxCaps
)
end
 
local function makeRfxTablesmakeReportRows(rfxType)
forlocal i,rfxes rfx= in ipairsgetRfxes(r[rfxType]) do
if not rfxes then
local intro, s, o, n = getRfxSections(rfx.page)
sreturn = getVoteTable(s)nil
o = getVoteTable(o)
n = getVoteTable(n)
r[rfxType][i].user = getUser(intro)
r[rfxType][i].support = #s
r[rfxType][i].oppose = #o
r[rfxType][i].neutral = #n
r[rfxType][i].percent = math.floor( (r[rfxType][i].support / (r[rfxType][i].support + r[rfxType][i].oppose) * 100) + 0.5 )
r[rfxType][i].dups = checkDups(s, o, n)
r[rfxType][i].endTime = getEndTime(intro)
r[rfxType][i].timeLeft = getTimeLeft( r[rfxType][i].endTime )
r[rfxType][i].report = '[//tools.wmflabs.org/xtools/rfa/?p=' .. mw.uri.encode( r[rfxType][i].page ) .. ' report]'
end
-- Get RfX objects and separate RfAs and RfBs.
end
local rfas = {}
 
local rfbs = {}
local function makeReportRows(rfxType)
for i, rfxPage in ipairs( rfxes ) do
local ret = ''
local rfxObject = rfx.new( rfxPage )
for i, rfx in ipairs(r[rfxType]) do
localif dupsrfxObject then
if r[rfxType][i]rfxObject.dupstype == 'rfa' then
dups = "'''yes'''" table.insert( rfas, rfxObject )
elseif rfxObject.type == 'rfb' then
else
dups = 'no' table.insert( rfbs, rfxObject )
end
end
end
local styleret = ''{}
if #rfas > 0 local styleInline = ''then
table.insert( ret, makeHeading( 'rfa' ) )
if r[rfxType][i].timeLeft == 'Pending closure...' then
for i, rfaObject in styleipairs( =rfas ') style="background: #f8cdc6;" |'do
styleInlinetable.insert( =ret, 'makeRow( background:rfaObject #f8cdc6;') )
end
local page = r[rfxType][i].page
local colour = colours[rfxType][ r[rfxType][i].percent ]
ret = ret .. mw.ustring.format( [==[
 
|-
|%s [[%s|%s]]
| style="text-align: right;%s" | [[%s#Support|%d]]
| style="text-align: right;%s" | [[%s#Oppose|%d]]
| style="text-align: right;%s" | [[%s#Neutral|%d]]
| style="text-align: right; background: #%s;" | %d
|%s %s
|%s %s
| style="text-align: center;%s" | %s
|%s %s]==],
style, page, r[rfxType][i].user,
styleInline, page, r[rfxType][i].support,
styleInline, page, r[rfxType][i].oppose,
styleInline, page, r[rfxType][i].neutral,
colour, r[rfxType][i].percent,
style, r[rfxType][i].endTime,
style, r[rfxType][i].timeLeft,
styleInline, dups,
style, r[rfxType][i].report
)
end
if #rfbs > 0 then
return ret
table.insert( ret, makeHeading( 'rfb' ) )
for i, rfbObject in ipairs( rfbs ) do
table.insert( ret, makeRow( rfbObject ) )
end
end
return table.concat( ret )
end
 
local function makeReport( args )
local purgeLink = mw.title.getCurrentTitle():fullUrl( 'action=purge' )
getRfxPages()
local header = mw.ustring.format(
makeRfxTables('rfa')
'\n|-\n! colspan="10" style="text-align: center;" | Requests for [[Wikipedia:Requests for adminship|adminship]] and [[Wikipedia:Requests for bureaucratship|bureaucratship]]<span class="plainlinks" style="float: right;"><small>[%s update]</small></span>',
makeRfxTables('rfb')
purgeLink
)
local float = args.float or args.align or 'right'
local clearrows = args.clearmakeReportRows() or 'left'
if rows == '' then
rows = '\n|-\n| colspan="10" | No current discussions. <small>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]])</small>'
local ret = '{| class="wikitable" '
if args.style then
ret = ret .. args.style
else
ret = ret .. 'style="white-space:wrap; clear: ' .. clear .. '; '
.. 'margin-top: 0em; margin-bottom: .5em; float: ' .. float .. '; '
.. 'padding: .5em 0em 0em 1.4em; background: #ffffff; '
.. 'border-collapse: collapse; border-spacing: 0;"'
end
iflocal #r.rfastyle >= 0 thenargs.style
if not style then
ret = ret .. '\n|-\n! RfA candidate !! S !! O !! N !! S% !! Ending (UTC) !! Time left !! Dups? !! Report'
retlocal float = retargs.float or args..align or makeReportRows('rfaright')
local clear = args.clear or 'left'
end
style = mw.ustring.format(
if #r.rfb > 0 then
'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;"',
ret = ret .. '\n|-\n! RfB candidate !! S !! O !! N !! S% !! Ending (UTC) !! Time left !! Dups? !! Report'
ret = ret .. makeReportRows('rfb')clear, float
)
end
return mw.ustring.format( '\n{| class="wikitable" %s%s%s\n|-\n|}', style, header, rows )
ret = ret .. '\n|-\n|}'
return ret
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
Line 255 ⟶ 195:
args = frame
end
return makeReport( args )
end