Module:UserLinks
From the Croc Wiki, the Croc encyclopedia
Documentation for this module may be created at Module:UserLinks/doc
local p = {}
local ToolbarBuilder = require('Module:Toolbar')
local u = {} -- Table for user-data helper strings.
--------------------------------------------------------------------------------------------------
-- 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.usernameHtmlWiki The username html-encoded. Spaces are encoded with underscores. --
-- --
-- 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 '[http://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.usernameHtmlWiki)
return '[' .. tostring(url) .. ' block log]'
end
local function getLink(linktype)
local linktypes = {
t = makeTalkLink,
c = makeContribsLink,
ct = makeCountLink,
m = makeMovesLink,
l = makeLogsLink,
}
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 --
-------------------------------------------------
-- Get the username value. If it is absent or blank, return an html error and a tracking category.
if args.user == '' or (not args.user and (not args.User or args.User == '')) then
return '<span class="error">Error: No username detected by [[Module:UserLinks]].</span>[[Category:UserLinks transclusions without usernames]]'
else
u.username = args.user or args.User
end
u.usernameHtml = mw.uri.encode(u.username) -- Html-encoded username. Spaces are encoded as pluses.
u.usernameHtmlWiki = mw.uri.encode(u.username, 'WIKI') -- Html-encodeed username. Spaces are encoded as underscores.
-------------------------------------------------
-- Project helper strings --
-------------------------------------------------
u.project = args.Project -- Get the project value.
u.projectColon = (u.project and (':' .. args.Project .. ':')) or '' -- Project value plus colons, for escaping interwiki links.
end
local function getLinks(args)
generateUserDataStrings(args)
local result = makeToolbar(args)
if result then
if args.sup then
result = '<sup>' .. result .. '</sup>'
end
result = ' ' .. result
else
result = ''
end
result = '<span>' .. makeUserLink() .. result .. '</span>'
return result
end
function p.main(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
return getLinks(args)
end
return p