Module:UserLinks
![]() | This Lua module is used in system messages, and on approximately 1,010,000 pages, or roughly 2% of all pages. Changes to it can cause immediate changes to the Wikipedia user interface. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Please discuss changes on the talk page before implementing them. |
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This module depends on the following other modules: |
This module generates links about a given user. It is used to generate templates such as {{user}}, {{user5}}, and {{admin}}, usually through its wrapper template {{user-multi}}.
Functions
Main
The main
function implements the {{user-multi}} template. It generates a list of links about a given user. Please see the template page for documentation.
Single
The single
function generates a single link about a given user. See {{user-multi/link}} for documentation.
Porting to other wikis
If you want to use this module on another wiki, there are a few modules that you must also copy across, and some that can be used but are not essential.
Required modules:
- Module:UserLinks
- Module:UserLinks/shared
- Module:UserLinks/config
- Module:Arguments
- Module:Yesno
- Module:Toolbar
- Module:InterwikiTable
- Module:TableTools (optional in Module:UserLinks, but required by Module:Toolbar)
Optional modules:
- Module:UserLinks/extra - used for testing new link functions before they are moved to the main module.
- Module:Category handler - if an error occurs, and this module is present, pages are not categorised if they match the module's blacklist.
After you have copied over the necessary modules, you should adjust the configuration settings in Module:UserLinks/config for your language and for your wiki's setup.
local ToolbarBuilder = require('Module:Toolbar')
local u = {} -- Table for user-data helper strings.
local demo
-- Define a custom error message for this module.
local function err(msg, cat)
if cat and not demo then
cat = '[[Category:' .. cat .. ']]'
else
cat = ''
end
return '<span class="error">[[Module:UserLinks]] error: ' .. msg .. '.</span>' .. cat
end
--------------------------------------------------------------------------------------------------
-- Below are the helper strings available for writing user link functions. --
-- If the project name has not been specified, all the project strings will be --
-- the blank string, ''. If the username is not specified, the module outputs an error. --
-- Either way, there is no need to worry about escaping nil values when using the --
-- helper strings. --
-- --
-- u.username The plain username --
-- u.usernameHtml The username html-encoded. Spaces are encoded with plus signs. --
-- --
-- u.project The plain project name. --
-- u.projectColon The project name with two colons surrounding it, i.e. ":project:" --
-- --
-- If you want more strings, you can define them in the generateUserDataStrings --
-- function below. --
--------------------------------------------------------------------------------------------------
local function makeUserLink()
return '[[' .. u.projectColon .. 'User:' .. u.username .. '|' .. u.username .. ']]'
end
local function makeTalkLink()
return '[[' .. u.projectColon .. 'User talk:' .. u.username .. '|talk]]'
end
local function makeContribsLink()
return '[[' .. u.projectColon .. 'Special:Contributions/' .. u.username .. '|contribs]]'
end
local function makeCountLink()
return '[//tools.wmflabs.org/xtools/pcount/index.php?name=' .. u.usernameHtml .. '&lang=en&wiki=wikipedia count]'
end
local function makeMovesLink()
return '[[' .. u.projectColon .. 'Special:Log/move/' .. u.username .. '|page moves]]'
end
local function makeLogsLink()
return '[[' .. u.projectColon .. 'Special:Log/' .. u.username .. '|logs]]'
end
local function makeBlockLogLink()
local url = mw.uri.fullUrl('Special:Log/block', 'page=User:' .. u.usernameHtml)
return '[' .. tostring(url) .. ' block log]'
end
local function makeBlocksLink()
return '[[' .. u.projectColon .. 'Special:Log/block/' .. u.username .. '|blocks]]'
end
local function makeBlockUserLink()
return '[[' .. u.projectColon .. 'Special:Block/' .. u.username .. '|block user]]'
end
local function makeCentralAuthLink()
return '[[' .. u.projectColon .. 'Special:CentralAuth/' .. u.username .. '|central auth]]'
end
local function makeDeletedContribsLink()
return '[[' .. u.projectColon .. 'Special:DeletedContributions/' .. u.username .. '|deleted contribs]]'
end
local function makeEmailLink()
return '[[' .. u.projectColon .. 'Special:Emailuser/' .. u.username .. '|email]]'
end
local function makeEditSummariesLink()
return '[http://toolserver.org/~tparis/editsummary/index.php?name=' .. u.usernameHtml .. '&lang=en&wiki=wikipedia edit summaries]'
end
local function makeDeletionsLink()
return '[[' .. u.projectColon .. 'Special:Log/delete/' .. u.username .. '|deletions]]'
end
local function makeListUserLink()
local url = mw.uri.fullUrl('Special:ListUsers', 'limit=1&username=' .. u.usernameHtml)
return '[' .. tostring(url) .. ' list user]'
end
local function makeSulLink()
return '[[sulutil:' .. u.username .. '|global contribs]]'
end
local function makeTargetLogsLink()
local url = mw.uri.fullUrl('Special:Log', 'page=User:' .. u.usernameHtml)
return '[' .. tostring(url) .. ' target logs]'
end
local function makeEditFilterLogLink()
local url = mw.uri.fullUrl('Special:AbuseLog', 'wpSearchUser=' .. u.usernameHtml)
return '[' .. tostring(url) .. ' edit filter log]'
end
local function makeProtectionsLink()
return '[[' .. u.projectColon .. 'Special:Log/protect/' .. u.username .. '|protections]]'
end
local function makeRightsLink()
return '[[' .. u.projectColon .. 'Special:Log/rights/' .. u.username .. '|rights]]'
end
local function makeRenamesLink()
return '[[' .. u.projectColon .. 'Special:Log/renameuser/' .. u.username .. '|renames]]'
end
local function makeRfaLink()
return '[[Special:PrefixIndex/Wikipedia:Requests for adminship/' .. u.username .. '|RfA]]'
end
local function getLink(linktype)
local linktypes = {
t = makeTalkLink,
c = makeContribsLink,
ct = makeCountLink,
m = makeMovesLink,
l = makeLogsLink,
bl = makeBlockLogLink,
bls = makeBlocksLink,
bu = makeBlockUserLink,
ca = makeCentralAuthLink,
dc = makeDeletedContribsLink,
e = makeEmailLink,
es = makeEditSummariesLink,
del = makeDeletionsLink,
lu = makeListUserLink,
sul = makeSulLink,
tl = makeTargetLogsLink,
efl = makeEditFilterLogLink,
pr = makeProtectionsLink,
rl = makeRightsLink,
ren = makeRenamesLink,
rfa = makeRfaLink
}
if not linktypes[linktype] then
return err('"' .. linktype .. '" is not a valid link code', 'UserLinks transclusions with invalid link types')
end
return linktypes[linktype]()
end
local function makeToolbar(args)
local targs = {}
local numArgsExist = false
for k, v in pairs(args) do
if type(k) == 'number' then
numArgsExist = true
targs[k] = getLink(v)
end
end
targs.style = args.small and 'font-size: 90%;'
targs.separator = args.separator or 'dot'
if numArgsExist == false then
return -- Don't return a toolbar if no numeric arguments exist.
else
return ToolbarBuilder.main(targs)
end
end
local function generateUserDataStrings(args)
-- Username helper strings.
u.username = args.user or args.User
u.usernameHtml = mw.uri.encode(u.username) -- Html-encoded username. Spaces are encoded as pluses.
-- Project helper strings.
u.project = args.Project or args.project
u.projectColon = (u.project and (':' .. u.project .. ':')) or '' -- Project value plus colons, for escaping interwiki links.
u.project = u.project or '' -- Control for nil values of args.Project.
end
local function generateTrackingCategories(args)
local ret = ''
if (args.Project or args.project) and not demo then
ret = ret .. '[[Category:UserLinks transclusions with project parameters]]'
end
return ret
end
local function getSingleLink(args)
local linktype = args[1]
if not linktype then
return err('no link type specified', 'UserLinks transclusions with invalid link types')
end
local result = getLink(linktype)
result = result .. generateTrackingCategories(args)
return result
end
local function getLinks(args)
-- Build the template output.
local result = makeToolbar(args) -- Get the toolbar contents.
if result then
if args.sup then
result = '<sup>' .. result .. '</sup>'
end
result = ' ' .. result
else
result = '' -- If there are no links specified, don't return the toolbar at all.
end
result = '<span>' .. makeUserLink() .. result .. '</span>'
result = result .. generateTrackingCategories(args)
return result
end
local function makeWrapper(func)
return function (frame)
-- If called via #invoke, use the args passed into the invoking template.
-- Otherwise, for testing purposes, assume args are being passed directly in.
local origArgs
if frame == mw.getCurrentFrame() then
origArgs = frame:getParent().args
for k, v in pairs(frame.args) do
origArgs = frame.args
break
end
else
origArgs = frame
end
-- Strip whitespace, and treat blank arguments as nil.
-- 'user', 'User', and 'separator' have different behaviour depending on
-- whether they are blank or nil, so keep them as they are.
local args = {}
for k, v in pairs(origArgs) do
v = mw.text.trim(v)
if v ~= '' or k == 'user' or k == 'User' or k == 'separator' then
args[k] = v
end
end
demo = args.demo -- Set the demo variable.
-- If the username is absent or blank, return an error and a tracking category.
if args.user == '' or (not args.user and (not args.User or args.User == '')) then
return err('no username detected', 'UserLinks transclusions without usernames')
end
-- Generate the user data strings.
generateUserDataStrings(args)
return func(args)
end
end
return {
main = makeWrapper(getLinks),
single = makeWrapper(getSingleLink)
}