Module:Namespace detect/data

From KitwarePublic
Revision as of 23:19, 12 July 2016 by WikiSysop (talk | contribs) (1 revision)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Documentation for this module may be created at Module:Namespace detect/data/doc

 1 --------------------------------------------------------------------------------
 2 --                          Namespace detect data                             --
 3 -- This module holds data for [[Module:Namespace detect]] to be loaded per    --
 4 -- page, rather than per #invoke, for performance reasons.                    --
 5 --------------------------------------------------------------------------------
 6 
 7 local cfg = require('Module:Namespace detect/config')
 8 
 9 local function addKey(t, key, defaultKey)
10 	if key ~= defaultKey then
11 		t[#t + 1] = key
12 	end
13 end
14 
15 -- Get a table of parameters to query for each default parameter name.
16 -- This allows wikis to customise parameter names in the cfg table while
17 -- ensuring that default parameter names will always work. The cfg table
18 -- values can be added as a string, or as an array of strings.
19 
20 local defaultKeys = {
21 	'main',
22 	'talk',
23 	'other',
24 	'subjectns',
25 	'demospace',
26 	'demopage'
27 }
28 
29 local argKeys = {}
30 for i, defaultKey in ipairs(defaultKeys) do
31 	argKeys[defaultKey] = {defaultKey}
32 end
33 
34 for defaultKey, t in pairs(argKeys) do
35 	local cfgValue = cfg[defaultKey]
36 	local cfgValueType = type(cfgValue)
37 	if cfgValueType == 'string' then
38 		addKey(t, cfgValue, defaultKey)
39 	elseif cfgValueType == 'table' then
40 		for i, key in ipairs(cfgValue) do
41 			addKey(t, key, defaultKey)
42 		end
43 	end
44 	cfg[defaultKey] = nil -- Free the cfg value as we don't need it any more.
45 end
46 
47 local function getParamMappings()
48 	--[[
49 	-- Returns a table of how parameter names map to namespace names. The keys
50 	-- are the actual namespace names, in lower case, and the values are the
51 	-- possible parameter names for that namespace, also in lower case. The
52 	-- table entries are structured like this:
53 	-- {
54 	--   [''] = {'main'},
55 	--   ['wikipedia'] = {'wikipedia', 'project', 'wp'},
56 	--   ...
57 	-- }
58 	--]]
59 	local mappings = {}
60 	local mainNsName = mw.site.subjectNamespaces[0].name
61 	mainNsName = mw.ustring.lower(mainNsName)
62 	mappings[mainNsName] = mw.clone(argKeys.main)
63 	mappings['talk'] = mw.clone(argKeys.talk)
64 	for nsid, ns in pairs(mw.site.subjectNamespaces) do
65 		if nsid ~= 0 then -- Exclude main namespace.
66 			local nsname = mw.ustring.lower(ns.name)
67 			local canonicalName = mw.ustring.lower(ns.canonicalName)
68 			mappings[nsname] = {nsname}
69 			if canonicalName ~= nsname then
70 				table.insert(mappings[nsname], canonicalName)
71 			end
72 			for _, alias in ipairs(ns.aliases) do
73 				table.insert(mappings[nsname], mw.ustring.lower(alias))
74 			end
75 		end
76 	end
77 	return mappings
78 end
79 
80 return {
81 	argKeys = argKeys,
82 	cfg = cfg,
83 	mappings = getParamMappings()
84 }