Module:Transclusion count
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Transclusion count/doc
1 local p = {}
2
3 function p.fetch(frame)
4 local template = nil
5 local return_value = nil
6
7 -- Use demo parameter if it exists, otherswise use current template name
8 local namespace = mw.title.getCurrentTitle().namespace
9 if frame.args["demo"] and frame.args["demo"] ~= "" then
10 template = frame.args["demo"]
11 elseif namespace == 10 then -- Template namespace
12 template = mw.title.getCurrentTitle().text
13 elseif namespace == 828 then -- Module namespace
14 template = (mw.site.namespaces[828].name .. ":" .. mw.title.getCurrentTitle().text)
15 end
16
17 -- If in template or module namespace, look up count in /data
18 if template ~= nil then
19 namespace = mw.title.new(template, "Template").namespace
20 if namespace == 10 or namespace == 828 then
21 template = mw.ustring.gsub(template, "/doc$", "") -- strip /doc from end
22 local index = mw.ustring.sub(mw.title.new(template).text,1,1)
23 local status, data = pcall(function ()
24 return(mw.loadData('Module:Transclusion_count/data/' .. (mw.ustring.find(index, "%a") and index or "other")))
25 end)
26 if status then
27 return_value = tonumber(data[mw.ustring.gsub(template, " ", "_")])
28 end
29 end
30 end
31
32 -- If database value doesn't exist, use value passed to template
33 if return_value == nil and frame.args[1] ~= nil then
34 local arg1=mw.ustring.match(frame.args[1], '[%d,]+')
35 if arg1 and arg1 ~= '' then
36 return_value = tonumber(frame:callParserFunction('formatnum', arg1, 'R'))
37 end
38 end
39
40 return return_value
41 end
42
43 -- Tabulate this data for [[Wikipedia:Database reports/Templates transcluded on the most pages]]
44 function p.tabulate(frame)
45 local list = {}
46 for i = 65, 91 do
47 local data = mw.loadData('Module:Transclusion count/data/' .. ((i == 91) and 'other' or string.char(i)))
48 for name, count in pairs(data) do
49 table.insert(list, {mw.title.new(name, "Template").fullText, count})
50 end
51 end
52 table.sort(list, function(a, b)
53 return (a[2] == b[2]) and (a[1] < b[1]) or (a[2] > b[2])
54 end)
55 local lang = mw.getContentLanguage();
56 for i = 1, #list do
57 list[i] = ('|-\n| %d || [[%s]] || %s\n'):format(i, list[i][1]:gsub('_', ' '), lang:formatNum(list[i][2]))
58 end
59 return table.concat(list)
60 end
61
62 return p