Module:User/sandbox
| This is the module sandbox page for Module:User (diff). |
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}}→ Example (talk · contribs) - {{user2}}:
{{#invoke:User|2|Example}}→ Example (talk · contribs · count) - {{user3}}:
{{#invoke:User|3|Example}}→ Example (talk · contribs · logs) - {{user4}}:
{{#invoke:User|4|Example}}→ Example (talk · contribs · email) - {{user5}}:
{{#invoke:User|5|Example}}→ Example (talk · contribs · deleted contribs · page moves · block user · block log) - {{user6}}:
{{#invoke:User|6|Example}}→ Example (talk · contribs · count · logs · page moves · block log) - {{user7}}:
{{#invoke:User|7|Example}}→ Example (talk · contribs · count · logs · email) - {{user8}}:
{{#invoke:User|8|Example}}→ Example (talk · contribs · central auth · count · email) - {{admin}}:
{{#invoke:User|admin|Example}}→ Example (talk · contribs · blocks · protections · deletions · page moves · rights · RfA) - {{former admin}}:
{{#invoke:User|former admin|Example}}→ Example (talk · contribs · former admin: · blocks · protections · deletions · rights · meta · local rights)
--[=[
-- This module implements {{user}}. {{user}} is a high-use template, sometimes
-- with thousands of transclusions on a page. This module optimises the
-- template's performance by reducing the number of parameters called from
-- wikitext, while still allowing all the features provided by
-- [[Module:UserLinks]]. It is about twice as fast as the version of {{user}}
-- that called the {{user-multi}} template from wikitext.
--]=]
local mUserLinks = require('Module:UserLinks')
local mShared = require('Module:UserLinks/shared')
local yesno = require('Module:Yesno')
local p = {}
local function validateArg(arg)
-- Validates one argument. Whitespace is stripped, and blank arguments
-- are treated as nil.
if not arg then
return nil
end
arg = arg:match('^%s*(.-)%s*$')
if arg ~= '' then
return arg
else
return nil
end
end
function p.main(frame, opts)
-- Grab the user, project and lang args from wikitext.
local argKeys = {
user = {
1,
'User',
'user'
},
project = {
2,
'Project',
'project'
},
lang = {
3,
'Lang',
'lang'
}
}
local origArgs = require('Module:Arguments').getArgs(frame)
local args = {}
for argKey, t in pairs(argKeys) do
for i, origArgKey in ipairs(t) do
local value = origArgs[origArgKey]
value = validateArg(value)
if value then
args[argKey] = value
-- If we have found a value, break the loop. For the average
-- invocation this saves two argument lookups.
break
end
end
end
-- Generate options. Some of these need wikitext args also.
local options = {
span = false,
separator = validateArg(origArgs.separator) or 'dot',
isDemo = yesno(validateArg(origArgs.demo))
}
-- Input the codes directly. This saves two argument lookups for each
-- invocation.
local codes = type(opts) and opts or {'t', 'c'}
-- Generate mop icon for formeradmin
local mop = (origArgs.mop and yesno(origArgs.mop)) and require('Module:Icon')._main({'mop'}) or ''
-- Normalize username
args.user = mw.ustring.gsub(mw.text.trim(args.user), "^[Uu]ser:", "")
-- Plug the data into [[Module:UserLinks]].
local snippets = mUserLinks.getSnippets(args)
local links = mUserLinks.getLinks(snippets)
local success, result = pcall(mUserLinks.export, codes, links, options)
if success then
return mop .. result
else
return mShared.makeWikitextError(result, options.isDemo)
end
end
function p.admin(frame)
return p.main(frame, {'t','c','bls','pr','del','m','rl','rfa'})
end
function p.eight(frame)
return p.main(frame, {'t', 'c', 'ca', 'ct', 'e'})
end
function p.five(frame)
return p.main(frame, {'t', 'c', 'dc', 'm', 'bu', 'bl'})
end
function p.four(frame)
return p.main(frame, {'t', 'c', 'e'})
end
function p.formeradmin(frame)
return p.main(frame, {'t','c','fa','bls','pr','del','rl','mr','lr'})
end
function p.seven(frame)
return p.main(frame, {'t', 'c', 'ct', 'l', 'e'})
end
function p.six(frame)
return p.main(frame, {'t', 'c', 'ct', 'l', 'm', 'bl'})
end
function p.three(frame)
return p.main(frame, {'t', 'c', 'l'})
end
function p.two(frame)
return p.main(frame, {'t', 'c', 'ct'})
end
p[''] = p.main
p['2'] = p.two
p['3'] = p.three
p['4'] = p.four
p['5'] = p.five
p['6'] = p.six
p['7'] = p.seven
p['8'] = p.eight
p['former admin'] = p.formeradmin
return p
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.