Jump to content

Module:STikiLeaderboard

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius on tour (talk | contribs) at 07:00, 11 June 2013 (better error check for the leaderboard content). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- Get the raw leaderboard content.
local lb = mw.title.new('Wikipedia:STiki/leaderboard') or {}
lbtext = lb:getContent() or error('Could not find the leaderboard text')

-- Get a specific data value for a given username.
local function findLeaderboardData(dfield, username)
    local r = {}
    r.rank, r.uses, r.vand, r.agf, r.queue, r.first, r.last, r.avg, r.yest, r.last30 = mw.ustring.match(
        lbtext,
        '\n| align=center | (%d+) || align=left | %[%[User:' .. username .. '|' .. username
        .. '%]%] %(%[%[User_Talk:' .. username .. '|talk%]%] | %[%[Special:Contributions/'
        .. username .. '|contribs%]%]%) || align=right | (%d+) || align=right | ([%d%.]+)%% '
        .. '|| align=right | ([%d%.]+)%% || align=center | (%S+) || align=right '
        .. '| {{ntsh|%d+}} (%d+) days ago || align=right | {{ntsh|%d+}} (%d+) days ago || align=right '
        .. '| {{ntsh|[%d%.]+}} ([%d%.]+) edits || align=right | (%d+) || align=right | (%d+)'
    )
    return r[dfield]
end

-- Get the username from #invoke and return the rank using findLeaderboardVals().
local function makeWrapper(dfield)
    return function (frame)
        -- Get username, filter bad input, trim whitespace, and capitalise first letter.
        local username = frame.args[1] or ''
        username = mw.getContentLanguage():ucfirst(mw.text.trim(username))
        if username == '' then
            error('No username specified')
        end
        
        return findLeaderboardData(dfield, username)
    end
end

return {
    rank   = makeWrapper('rank'),
    uses   = makeWrapper('uses'),
    vand   = makeWrapper('vand'),
    agf    = makeWrapper('agf'),
    queue  = makeWrapper('queue'),
    first  = makeWrapper('first'),
    last   = makeWrapper('last'),
    avg    = makeWrapper('avg'),
    yest   = makeWrapper('yest'),
    last30 = makeWrapper('last30'),
}