Module:NUMBEROFSECTIONS: Difference between revisions

Content deleted Content added
replace p.numberof for Template:NUMBEROF with new code that loads data once per page from Module:NUMBEROF/data, see Template talk:NUMBEROF#Overhead
use magic variables if want local site; if want commas, use local digits and formatting
Line 1:
local p = {}
 
-- Credit: http://lua-users.org/wiki/FormattingNumbers
local function commaValue(amount)
local formatted = amount
while true do
local k
formatted, k = string.gsub(formatted, '^(-?%d+)(%d%d%d)', '%1,%2')
if k == 0 then
break
end
end
return formatted
end
 
local function trimArg(arg, i)
Line 23 ⟶ 10:
end
return mw.ustring.lower(arg)
end
 
local function commaValuegetIfLocal(amountsite, action)
-- If wanted site is the local site where module is running,
-- return numberof result for given action, or nil.
-- This is faster than reading the cached table, and gives the current value.
local localSite = string.match(mw.site.server, '.*//(.*)%.org$') -- examples: 'en.wikipedia', 'commons.wikimedia'
if site == localSite then
if action == 'activeusers' then
action = 'activeUsers'
end
return mw.site.stats[action]
end
end
 
function p.numberof(frame)
local args = frame:getParent().args
local action = trimArg(args[1], 1) -- ACTIVEUSERSactiveusers, ADMINSadmins, ARTICLESarticles, EDITSedits, FILESfiles, PAGESpages, USERSusers
local site = trimArg(args[2], 2) -- "en" or "en.wikipedia" or "en.wikiquote" etc.
if not site:find('.', 1, true) then
site = site .. '.wikipedia'
end
local data = mw.loadData('Module:NUMBEROF/data')
local map = data.map
data = data.data
local wantComma = trimArg(args[3]) -- nil for no commas in output; "N" or anything nonblank inserts commas
local result = data[getIfLocal(site], action)
if not result then
local data = mw.loadData('Module:NUMBEROF/data')
result = result[map[action]]
local map = data.map
data = data.data
result = data[site]
if k == 0result then
result = result[map[action]]
end
end
if result then
result = tostring(result)
if wantComma then
result = commaValuemw.getContentLanguage():formatNum(result)
end
return result -- number or formatted string
end
return '-1'