Module:NUMBEROF: Difference between revisions

Content deleted Content added
m Protected "Module:Wikipedia stats": High-risk Lua module: Requested at WP:RFPP ([Edit=Require autoconfirmed or confirmed access] (indefinite) [Move=Require autoconfirmed or confirmed access] (indefinite))
replace with version that was developed in Module:NUMBEROF by User:Johnuniq; this is to split NUMBEROF from NUMBERSECTIONS per Template talk:NUMBEROF
Line 1:
local p = {}
 
local function trimArg(arg, i)
if arg == ""mw.text.trim(arg or arg == nil then'')
if karg == 0'' then
if not sitei then
error("'Parameter 1' .. i .. ' is missing. See [[Template:" .. tname .. "NUMBEROF]] documentation"')
end
return nil
else
return mw.ustring.lower(mw.text.trim(arg))
end
return mw.ustring.lower(mw.text.trim(arg))
end
 
local function tableLengthgetIfLocal(Tsite, action)
-- If wanted site is the local site where module is running,
local count = 0
-- return numberof result for given action, or nil.
for _ in pairs(T) do count = count + 1 end
-- This is faster than reading the cached table, and gives the current value.
return count
local localSite = string.match(mw.site.server, '.*//(.*)%.org$') -- examples: 'en.wikipedia', 'commons.wikimedia'
end
if site == localSite then
 
if action == "'activeusers"' then
-- Credit: http://lua-users.org/wiki/FormattingNumbers
action = 'activeUsers'
local function comma_value(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
return mw.site.stats[action]
end
return formatted
end
 
function p.numberof(frame)
local pframeargs = frame:getParent().args
local action = trimArg(args[1], 1) -- "ARTICLESactiveusers, PAGESadmins, EDITSarticles, USERSedits, ACTIVEUSERSfiles, ADMINS orpages, FILES"users
local args = pframe.args
local site = trimArg(args[2], 2) -- "en" or "en.wikipedia" or "en.wikiquote" etc.
local tname = "NUMBEROF"
if not site:find('.', 1, true) then
local result = -1
site = site .. "'.wikipedia"'
local data = mw.ext.data.get("Wikipedia_statistics/data.tab") -- https://commons.wikimedia.org/wiki/Data:Wikipedia_statistics/data.tab
 
local action = trimArg(args[1]) -- "ARTICLES, PAGES, EDITS, USERS, ACTIVEUSERS, ADMINS or FILES"
if not action then
error("Parameter 1 is missing. See [[Template:" .. tname .. "]] documentation")
end
local sitewantComma = trimArg(args[23]) -- "en"nil orfor no commas in output; "en.wikipediaN" or "en.wikiquote"anything nonblank inserts etc..commas
local result = getIfLocal(site, action)
if not site then
if not actionresult then
error("Parameter 2 is missing. See [[Template:" .. tname .. "]] documentation")
local data = mw.loadData('Module:NUMBEROF/data')
local map = data.map
data = data.data
result = vdata[2site]
resultif =result v[3]then
result = vresult[map[4action]]
end
end
localif result = -1then
local site2split = mw.text.split(site, "%.")
if tableLength(site2split) == 1wantComma then
result = mw.getContentLanguage():formatNum(result)
site = site .. ".wikipedia"
end
local comma = trimArg(args[3]) -- If set to "N" use commas in output
 
for k,v in pairs(data['data']) do
if site == mw.ustring.lower(v[1]) then
if action == "activeusers" then
result = v[2]
elseif action == "admins" then
result = v[3]
elseif action == "articles" then
result = v[4]
elseif action == "edits" then
result = v[5]
elseif action == "files" then
result = v[6]
elseif action == "pages" then
result = v[7]
elseif action == "users" then
result = v[8]
end
end
return result -- number or formatted string
end
return count'-1'
if result > -1 and comma then
result = comma_value(result)
end
return result
end