Module:UserLinks/sandbox: Difference between revisions

Content deleted Content added
use maybeLoadModule to simplify some code and to make some of the dependent modules optional
others
 
(51 intermediate revisions by 14 users not shown)
Line 1:
--------------------------------------------------------------------------------
-- UserLinks --
-- 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. --
--------------------------------------------------------------------------------
 
-- Require necessary modules
local yesno = require('Module:Yesno')
Line 42 ⟶ 48:
-- data snippets. New link functions should be added below the existing
-- functions.
--
-- For documentation on how to add new link functions, please see
-- [[Module:UserLinks#Adding new links]].
----------------------------------------------------------------------------
 
Line 55 ⟶ 58:
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
 
Line 72 ⟶ 86:
snippets.interwiki,
-1,
'ContributionsContribs/' .. snippets.username,
message('display-contributions')
)
end
function linkFunctions.c64(snippets)
-- Contributions
local first64 = snippets.username:match('^%x+:%x+:%x+:%x+:')
or snippets.username:match('^%x+:%x+:%x+:')
or snippets.username:match('^%x+:%x+:')
or snippets.username:match('^%x+:')
return first64 and makeWikilink(
snippets.interwiki,
-1,
'Contribs/' .. first64 .. ':/64',
'(/64)'
) or ''
end
 
function linkFunctions.ct(snippets)
-- Edit count
return makeUrlLinkmakeWikilink(
{'xtools',
0,
host = 'tools.wmflabs.org',
'ec/' .. snippets.toolLang .. '.' .. snippets.projectLong .. '.org/' .. snippets.username,
path = '/supercount/index.php',
query = {
user = snippets.username,
project = snippets.toolLang .. '.' .. snippets.projectLong
}
},
message('display-count')
)
Line 109 ⟶ 132:
'Log/' .. snippets.username,
message('display-logs')
)
end
 
function linkFunctions.ae(snippets)
-- Automated edits (and non-automated contributions).
return makeWikilink(
'xtools',
0,
'autoedits/' .. snippets.toolLang .. '.' .. snippets.projectLong .. '.org/' .. snippets.username,
message('display-autoedits')
)
end
Line 175 ⟶ 208:
function linkFunctions.es(snippets)
-- Edit summaries
return makeUrlLinkmakeWikilink(
{'xtools',
0,
host = 'tools.wmflabs.org',
'editsummary/' .. snippets.toolLang .. '.' .. snippets.projectLong .. '.org/' .. snippets.username,
path = '/xtools/editsummary/index.php',
query = {
name = snippets.username,
lang = snippets.toolLang,
wiki = snippets.projectLong
}
},
message('display-editsummaries')
)
Line 205 ⟶ 232:
-1,
'ListUsers',
{limit = 1, userusername = snippets.username},
message('display-listuser')
)
Line 291 ⟶ 318:
action = 'query',
list = 'users',
usprop = 'groups|editcount',
ususers = snippets.username
}
Line 306 ⟶ 333:
'ListFiles/' .. snippets.username,
message('display-uploads')
)
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/him',
'she/her',
'they/them'
)
end
Line 586 ⟶ 636:
function snippetFunctions.fullDomain()
-- The full ___domain name of the site, e.g. www.mediawiki.org,
-- en.wikpediawikipedia.org, or ja.wikibooks.org.
local fullDomain
local lang = getSnippet('toolLang')
Line 609 ⟶ 659:
 
return snippets
end
 
function p.validateProjectCode(s)
Line 617 ⟶ 667:
-- returns nil for both.
interwikiTable = interwikiTable or mw.loadData('Module:InterwikiTable')
for key, t in pairs(interwikiTable) do
for i, prefix in ipairs(t.iw_prefix) do
if s == prefix then
return s, key
end
end
end
end
return nil, nil
end
 
Line 667 ⟶ 717:
local options = {}
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.sup = yesno(args.sup, true)
Line 692 ⟶ 743:
function p.export(codes, links, options)
-- Make the user link.
local userLink = options.noPing and links.np or links.u
 
-- If we weren't passed any link codes, just return the user link.
Line 711 ⟶ 762:
local toolbar = mToolbar.main(toolbarArgs)
 
-- Apply the sup option and return the result.
if options.sup then
toolbar = '<sup>' .. toolbar .. '</sup>'
end
return userLink .. '&nbsp;' .. toolbar
-- If we are transcluding, add a non-breaking space, but if we are substing
-- just use a normal space
local space = mw.isSubsting() and ' ' or '&nbsp;'
return userLink .. space .. toolbar
end
 
Line 750 ⟶ 806:
return links[code]
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