Module:User
This module implements {{user}}, {{user2}}, {{user3}}, {{user4}}, {{user5}}, {{user6}}, {{user7}}, {{user8}}, {{admin}}, and {{former admin}}. For documentation, please see the template page.
Technical details
While the {{user}} template is only a wrapper for Module:UserLinks, by implementing it as a Lua module we make significant gains in performance. It is roughly twice as fast as accessing Module:UserLinks from wikitext. For example, this module rendered a test page containing nothing but 1000 {{user}} transclusions in 3.9 seconds, whereas the version that called Module:UserLinks from wikitext rendered it in 7.7 seconds.
The performance gains are achieved by reducing the number of parameters that are looked up from wikitext. In particular, the link codes, "t" and "c", along with the "span=no" option, are passed directly to UserLinks. Also, UserLinks looks up the arguments "user", "User", "project", "Project", "lang", and "Lang". From wikitext, each parameter is checked twice – once for the #invoke text and once for the calling template – and each check is relatively resource-heavy. From Lua, each parameter is only checked once, and the individual checks take much less time. This means that on average, maybe nine argument lookups are saved in each invocation by using this module. On pages with hundreds or thousands of invocations, this adds up to a lot of argument lookups saved.
Examples
- {{user}}:
{{#invoke:User||Example}}
→ Script error: The function "" does not exist. - {{user2}}:
{{#invoke:User|2|Example}}
→ Script error: The function "2" does not exist. - {{user3}}:
{{#invoke:User|3|Example}}
→ Script error: The function "3" does not exist. - {{user4}}:
{{#invoke:User|4|Example}}
→ Script error: The function "4" does not exist. - {{user5}}:
{{#invoke:User|5|Example}}
→ Script error: The function "5" does not exist. - {{user6}}:
{{#invoke:User|6|Example}}
→ Script error: The function "6" does not exist. - {{user7}}:
{{#invoke:User|7|Example}}
→ Script error: The function "7" does not exist. - {{user8}}:
{{#invoke:User|8|Example}}
→ Script error: The function "8" does not exist. - {{admin}}:
{{#invoke:User|admin|Example}}
→ Script error: The function "admin" does not exist. - {{former admin}}:
{{#invoke:User|former admin|Example}}
→ Script error: The function "former admin" does not exist.
local mUserLinks = require('Module:UserLinks')
local mShared = require('Module:UserLinks/shared')
local yesno = require('Module:Yesno')
local p = {}
local function validateArg(arg)
arg = arg:match('^%s*(.-)%s*$')
if arg ~= '' then
return arg
end
end
function p.main(frame)
local argKeys = {
user = {
1,
'User',
'user'
},
project = {
2,
'Project',
'project'
},
lang = {
3,
'Lang',
'lang'
}
}
local origArgs = frame:getParent().args
local args = {}
for argKey, t in pairs(argKeys) do
for i, origArgKey in ipairs(t) do
local value = validateArg(origArgs[origArgKey])
if value then
args[argKey] = value
break
end
end
end
local options = {
span = false,
separator = validateArg(origArgs.separator) or 'dot',
isDemo = yesno(validateArg(origArgs.demo))
}
local codes = {'t', 'c'}
local snippets = userLinks.getSnippets(args)
local links = userLinks.getLinks(snippets)
local success, result = pcall(mUserLinks.export, codes, links, options)
if success then
return result
else
return mShared.makeWikitextError(result, options.isDemo)
end
end
return p