Jump to content

Module:Archive URL counts

From Wikipedia, the free encyclopedia

local lang_obj = mw.language.getContentLanguage();								-- for use with <wantComma> and <last_update>

local aliases = {
    commons = 'commons.wikimedia',
    donate = 'donate.wikimedia',
    foundation = 'foundation.wikimedia',
    incubator = 'incubator.wikimedia',
    meta = 'meta.wikimedia',
    species = 'species.wikimedia',
    wikidata = 'www.wikidata',
    wikifunctions = 'www.wikifunctions',
    wikimania = 'wikimania.wikimedia',
    wikitech = 'wikitech.wikimedia',
    wikisource = 'www.wikisource',
}

local function trimArg(arg, i)
    arg = mw.text.trim(arg or '')
    if arg == '' then
        if i then
            error('Parameter ' .. i .. ' is missing. See template documentation')
        end
        return nil
    end
    return mw.ustring.lower(arg)
end

local function main(frame)
    local args = frame:getParent().args
    local action = trimArg(args[1], 1)
    
    -- Clean up action if someone uses legacy formatting
    if action:sub(1, 8) == 'numberof' then 
        action = trimArg(action:sub(9), 1)
    end
    
    local site = trimArg(args[2], 2)
    site = aliases[site] or site
    
    -- If no project is specified (e.g. "fr"), default to ".wikipedia"
    -- This also handles the "total" aliases gracefully
    if not site:find('.', 1, true) then
        site = site .. '.wikipedia'
    end
    
    local wantComma = trimArg(args[3]) -- "N" or anything non-blank inserts commas
    local result

    -- Load the cached data table
    local dataObj = mw.loadData('Module:Archive URL counts/data')
    local map = dataObj.map
    local data = dataObj.data

	if 'last_update' == action then												-- if all we want is the date of the last update
		return lang_obj:formatDate ('j F Y', dataObj.last_update);				-- make the date all pretty-like (dmy), return it, and done
	end

    local siteData = data[site]
    
    if siteData and map[action] then
        result = siteData[map[action]]
    end

    if result then
        if wantComma then
            result = lang_obj:formatNum(result)
        end
        return result
    end
    
    -- Return -1 if the site or statistic doesn't exist in the JSON
    return -1
end

return {
    main = main,
}