Module:Userlinks: Difference between revisions

From Test Wiki
m (Saint moved page Module:UserLinks to Module:Userlinks without leaving a redirect)
Line 1: Line 1:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--                                UserLinks                                 --
--                                Userlinks                                 --
-- This module creates a list of links about a given user. It can be used on  --
-- This module creates a list of links about a given user. It can be used on  --
-- its own or from a template. See the /doc page for more documentation.      --
-- its own or from a template. See the /doc page for more documentation.      --
Line 9: Line 9:


-- Lazily initialise modules that we might or might not need
-- Lazily initialise modules that we might or might not need
local mExtra -- [[Module:UserLinks/extra]]
local mExtra -- [[Module:Userlinks/extra]]
local mArguments -- [[Module:Arguments]]
local mArguments -- [[Module:Arguments]]
local mToolbar -- [[Module:Toolbar]]
local mToolbar -- [[Module:Toolbar]]
Line 17: Line 17:


-- Load shared helper functions
-- Load shared helper functions
local mShared = require('Module:UserLinks/shared')
local mShared = require('Module:Userlinks/shared')
local raiseError = mShared.raiseError
local raiseError = mShared.raiseError
local maybeLoadModule = mShared.maybeLoadModule
local maybeLoadModule = mShared.maybeLoadModule
Line 38: Line 38:
-- indexed. This is made possible by the metatable and by the various link
-- indexed. This is made possible by the metatable and by the various link
-- functions, some of which are defined here, and some of which are defined
-- functions, some of which are defined here, and some of which are defined
-- at [[Module:UserLinks/extra]].
-- at [[Module:Userlinks/extra]].
--]=]
--]=]
local links, linkFunctions = {}, {}
local links, linkFunctions = {}, {}
Line 58: Line 58:
snippets.username
snippets.username
)
)
end
function linkFunctions.np(snippets)
-- User page (no ping)
return '<span class="plainlinks">' .. makeFullUrlLink(
snippets.interwiki,
2,
snippets.username,
'',
snippets.username
) .. '</span>'
end
end


Line 340: Line 351:
end
end
function linkFunctions.nuke(snippets)
-- Mass delete/Special:Nuke
return makeWikilink(
snippets.interwiki,
-1,
'Nuke/' .. snippets.username,
message('display-nuke')
)
end
function linkFunctions.gender(snippets)
-- Gender
return mw.getCurrentFrame():callParserFunction(
'GENDER',
snippets.username,
'he',
'she',
'they'
)
end
----------------------------------------------------------------------------
----------------------------------------------------------------------------
-- End of link functions
-- End of link functions
Line 345: Line 377:


-- Define the metatable that memoizes the link functions, and fetches link
-- Define the metatable that memoizes the link functions, and fetches link
-- functions from [[Module:UserLinks/extra]] if necessary.
-- functions from [[Module:Userlinks/extra]] if necessary.


-- Lazily initialise the extraLinkFunctions table. We only want to load
-- Lazily initialise the extraLinkFunctions table. We only want to load
-- [[Module:UserLinks/extra]] as necessary, so it has a low transclusion
-- [[Module:Userlinks/extra]] as necessary, so it has a low transclusion
-- count.
-- count.
local extraLinkFunctions
local extraLinkFunctions
Line 376: Line 408:
-- Here we use the distinction between false and nil to record
-- Here we use the distinction between false and nil to record
-- whether we have already tried to load the /extra module.
-- whether we have already tried to load the /extra module.
mExtra = maybeLoadModule('Module:UserLinks/extra')
mExtra = maybeLoadModule('Module:Userlinks/extra')
end
end
if type(mExtra) == 'table'
if type(mExtra) == 'table'
Line 471: Line 503:
--
--
-- If you add a new snippet, please document it at
-- If you add a new snippet, please document it at
-- [[Module:UserLinks#Adding new links]].
-- [[Module:Userlinks#Adding new links]].
--]=]
--]=]
local snippets, snippetFunctions = {}, {}
local snippets, snippetFunctions = {}, {}
Line 637: Line 669:


-- End snippet functions. If you add a new snippet function, please
-- End snippet functions. If you add a new snippet function, please
-- document it at [[Module:UserLinks#Adding new links]].
-- document it at [[Module:Userlinks#Adding new links]].


return snippets
return snippets
Line 698: Line 730:
local options = {}
local options = {}
options.isDemo = yesno(args.demo) or false
options.isDemo = yesno(args.demo) or false
options.noPing = yesno(args.noPing) or yesno(args.noping) or yesno(args.np) or false
options.toolbarStyle = yesno(args.small) and 'font-size: 90%;' or nil
options.toolbarStyle = yesno(args.small) and 'font-size: 90%;' or nil
options.sup = yesno(args.sup, true)
options.sup = yesno(args.sup, true)
Line 723: Line 756:
function p.export(codes, links, options)
function p.export(codes, links, options)
-- Make the user link.
-- Make the user link.
local userLink = links.u
local userLink = options.noPing and links.np or links.u


-- If we weren't passed any link codes, just return the user link.
-- If we weren't passed any link codes, just return the user link.
Line 786: Line 819:
return links[code]
return links[code]
end
end
--------------------------------------------------------------------------------
-- Link table
--------------------------------------------------------------------------------
function p.linktable()
-- Returns a wikitext table of link codes, with an example link for each
-- one. This function doesn't take any arguments, so it can be accessed
-- directly from wiki pages without using makeInvokeFunction.
local args = {user = 'Example'}
local snippets = p.getSnippets(args)
local links = p.getLinks(snippets)
-- Assemble the codes and links in order
local firstCodes = {'u', 't', 'c'}
local firstLinks, firstCodesKeys = {}, {}
for i, code in ipairs(firstCodes) do
firstCodesKeys[code] = true
firstLinks[#firstLinks + 1] = {code, links[code]}
end
local secondLinks = {}
for code, link in pairs(links) do
if not firstCodesKeys[code] then
secondLinks[#secondLinks + 1] = {code, link}
end
end
table.sort(secondLinks, function(t1, t2)
return t1[1] < t2[1]
end)
local links = {}
for i, t in ipairs(firstLinks) do
links[#links + 1] = t
end
for i, t in ipairs(secondLinks) do
links[#links + 1] = t
end
-- Output the code table in table format
local ret = {}
ret[#ret + 1] = '{| class="wikitable plainlinks sortable"'
ret[#ret + 1] = '|-'
ret[#ret + 1] = '! ' .. message('linktable-codeheader')
ret[#ret + 1] = '! ' .. message('linktable-previewheader')
for i, t in ipairs(links) do
local code = t[1]
local link = t[2]
ret[#ret + 1] = '|-'
ret[#ret + 1] = "| '''" .. code .. "'''"
ret[#ret + 1] = '| ' .. link
end
ret[#ret + 1] = '|}'
return table.concat(ret, '\n')
end


return p
return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.