https://testwiki.wiki/api.php?action=feedcontributions&user=Joaquinito2017&feedformat=atomTest Wiki - User contributions [en]2024-03-29T00:02:07ZUser contributionsMediaWiki 1.41.0https://testwiki.wiki/index.php?title=Module:Userlinks/shared&diff=3373Module:Userlinks/shared2017-10-15T14:16:33Z<p>Joaquinito2017: 1 revision imported</p>
<hr />
<div>-- This module stores functions that are shared between [[Module:UserLinks]]<br />
-- and [[Module:UserLinks/extra]].<br />
<br />
-- Load data and define often-used variables<br />
local cfg = mw.loadData('Module:UserLinks/config')<br />
local namespaces = mw.site.namespaces<br />
<br />
-- Lazily initialise modules that we may or may not need<br />
local mCategoryHandler<br />
<br />
-- Define namespaces for which links need to be escaped with the colon trick.<br />
-- See [[w:en:Help:Colon trick]].<br />
local colonNamespaces = {<br />
[6] = true, -- File<br />
[14] = true, -- Category<br />
}<br />
<br />
local p = {}<br />
<br />
function p.maybeLoadModule(s)<br />
-- Attempts to load the module s. If it succeeds, returns the module;<br />
-- otherwise, returns false.<br />
local success, mdl = pcall(require, s)<br />
if success then<br />
return mdl<br />
else<br />
return false<br />
end<br />
end<br />
<br />
function p.raiseError(message, section, level)<br />
-- Raises an error using the Lua error function. The error message is<br />
-- designed to be caught with pcall and then passed to p.makeWikitextError.<br />
-- The section, if specified, is the section name on a help page that gives<br />
-- help to users about that particular error.<br />
if section then<br />
message = message .. '|' .. section<br />
end<br />
if not level or level == 0 then<br />
level = 0<br />
else<br />
level = level + 1<br />
end<br />
error(message, level)<br />
end<br />
<br />
local localBlacklist = {<br />
'/[sS]andbox$', -- Don't categorise sandboxes<br />
'/[tT]est ?cases$', -- Don't categorise test case pages<br />
}<br />
<br />
local function currentTitleMatchesLocalBlacklist()<br />
-- Return true if the current title matches any of the patterns in the<br />
-- local blacklist table. Otherwise return false.<br />
local title = mw.title.getCurrentTitle().prefixedText<br />
for i, pattern in ipairs(localBlacklist) do<br />
if title:find(pattern) then<br />
return true<br />
end<br />
end<br />
return false<br />
end<br />
<br />
function p.makeWikitextError(encodedMessage, demo)<br />
local errorMessage, section = mw.ustring.match(encodedMessage, '^(.-)|(.*)$')<br />
errorMessage = errorMessage or encodedMessage<br />
<br />
-- If not a demo, get the error category link and pass it through<br />
-- [[Module:Category handler]]'s blacklist.<br />
local category<br />
if not demo then<br />
category = string.format(<br />
'[[%s:%s]]',<br />
namespaces[14].name,<br />
p.message('error-config-category')<br />
)<br />
mCategoryHandler = p.maybeLoadModule('Module:Category handler')<br />
if mCategoryHandler then<br />
-- Categorise all namespaces, but not blacklisted pages.<br />
category = mCategoryHandler.main{all = category}<br />
end<br />
if category and currentTitleMatchesLocalBlacklist() then<br />
category = nil<br />
end<br />
end<br />
category = category or ''<br />
<br />
-- Format the error message and the section link.<br />
local formattedError<br />
if section then<br />
formattedError = p.message(<br />
'error-config-message-help',<br />
errorMessage,<br />
section<br />
)<br />
else<br />
formattedError = p.message(<br />
'error-config-message-nohelp',<br />
errorMessage<br />
)<br />
end<br />
<br />
-- Return the error message and the category inside html error tags.<br />
return string.format(<br />
'<strong class="error">%s</strong>%s',<br />
formattedError,<br />
category<br />
)<br />
end<br />
<br />
local function formatPage(interwiki, namespace, page)<br />
-- Formats an interwiki, a namespace and a page into a wikilink-ready<br />
-- string. The interwiki and namespace are optional. If a namespace is<br />
-- specified, it should be a valid key to mw.site.namespaces. The page<br />
-- parameter is required.<br />
local ret = {}<br />
interwiki = interwiki or ''<br />
if interwiki ~= '' or colonNamespaces[namespace] then<br />
ret[#ret + 1] = ':'<br />
end<br />
ret[#ret + 1] = interwiki<br />
if interwiki ~= '' then<br />
ret[#ret + 1] = ':'<br />
end<br />
if namespace then<br />
local nsTable = namespaces[namespace]<br />
if not nsTable then<br />
error('"' .. tostring(namespace) .. '" is not a valid namespace key', 2)<br />
end<br />
ret[#ret + 1] = nsTable.name<br />
if namespace ~= 0 then<br />
ret[#ret + 1] = ':'<br />
end<br />
end<br />
ret[#ret + 1] = page<br />
return table.concat(ret)<br />
end<br />
<br />
local function formatDisplay(s)<br />
-- Replaces spaces in a string with "&nbsp;" to make sure they don't wrap.<br />
-- Don't replace anything if we are substing, as we generally don't want<br />
-- to use "&nbsp;" in that case.<br />
if mw.isSubsting() then<br />
return s<br />
else<br />
return s:gsub(' ', '&nbsp;')<br />
end<br />
end<br />
<br />
function p.makeWikilink(interwiki, namespace, page, display)<br />
-- Creates a wikilink. The interwiki, namespace and display parameters are<br />
-- optional. If a namespace parameter is specified it must be a valid key<br />
-- to mw.site.namespaces.<br />
local formattedPage = formatPage(interwiki, namespace, page)<br />
if display then<br />
display = formatDisplay(display)<br />
return string.format('[[%s|%s]]', formattedPage, display)<br />
else<br />
return string.format('[[%s]]', formattedPage)<br />
end<br />
end<br />
<br />
local function formatUrlLink(url, display)<br />
-- Formats a URL link with an optional display parameter.<br />
if display then<br />
display = formatDisplay(display)<br />
return string.format('[%s %s]', url, display)<br />
else<br />
return string.format('[%s]', url)<br />
end<br />
end<br />
<br />
function p.makeUrlLink(s, display)<br />
-- Makes a URL link with an optional display parameter. The first input<br />
-- may be any valid input to mw.uri.new.<br />
local url = mw.uri.new(s)<br />
url = tostring(url)<br />
return formatUrlLink(url, display)<br />
end<br />
<br />
function p.makeFullUrlLink(interwiki, namespace, page, query, display)<br />
-- Makes a link to the full URL of a page. The interwiki, namespace, query<br />
-- and display parameters are optional. If a namespace parameter is<br />
-- specified it must be a valid key to mw.site.namespaces. The query<br />
-- parameter can be a string or a table as specified in the mw.uri library.<br />
local formattedPage = formatPage(interwiki, namespace, page)<br />
local url = mw.uri.fullUrl(formattedPage, query)<br />
url = tostring(url)<br />
return formatUrlLink(url, display)<br />
end<br />
<br />
function p.message(key, ...)<br />
-- Returns the message with the given key from [[Module:UserLinks/config]].<br />
-- Extra parameters are substituted in the message for keys $1, $2, $3, etc.<br />
local msg = cfg[key]<br />
if not msg then<br />
p.raiseError(<br />
'No message found with key "' .. tostring(key) .. '"',<br />
'No message found',<br />
2<br />
)<br />
end<br />
local noArgs = select('#', ...)<br />
if noArgs < 1 then<br />
return msg<br />
else<br />
local msg = mw.message.newRawMessage(msg, ...)<br />
return msg:plain()<br />
end<br />
end<br />
<br />
return p</div>Joaquinito2017https://testwiki.wiki/index.php?title=Module:Userlinks&diff=3369Module:Userlinks2017-10-15T14:16:33Z<p>Joaquinito2017: 1 revision imported</p>
<hr />
<div>--------------------------------------------------------------------------------<br />
-- UserLinks --<br />
-- This module creates a list of links about a given user. It can be used on --<br />
-- its own or from a template. See the /doc page for more documentation. --<br />
--------------------------------------------------------------------------------<br />
<br />
-- Require necessary modules<br />
local yesno = require('Module:Yesno')<br />
<br />
-- Lazily initialise modules that we might or might not need<br />
local mExtra -- [[Module:UserLinks/extra]]<br />
local mArguments -- [[Module:Arguments]]<br />
local mToolbar -- [[Module:Toolbar]]<br />
local mCategoryHandler -- [[Module:Category handler]]<br />
local mTableTools -- [[Module:TableTools]]<br />
local interwikiTable -- [[Module:InterwikiTable]], loaded with mw.loadData<br />
<br />
-- Load shared helper functions<br />
local mShared = require('Module:UserLinks/shared')<br />
local raiseError = mShared.raiseError<br />
local maybeLoadModule = mShared.maybeLoadModule<br />
local makeWikitextError = mShared.makeWikitextError<br />
local makeWikilink = mShared.makeWikilink<br />
local makeUrlLink = mShared.makeUrlLink<br />
local makeFullUrlLink = mShared.makeFullUrlLink<br />
local message = mShared.message<br />
<br />
local p = {}<br />
<br />
--------------------------------------------------------------------------------<br />
-- Link table<br />
--------------------------------------------------------------------------------<br />
<br />
function p.getLinks(snippets)<br />
--[=[<br />
-- Get a table of links that can be indexed with link codes. The table<br />
-- returned is blank, but links are added to it on demand when it is<br />
-- indexed. This is made possible by the metatable and by the various link<br />
-- functions, some of which are defined here, and some of which are defined<br />
-- at [[Module:UserLinks/extra]].<br />
--]=]<br />
local links, linkFunctions = {}, {}<br />
<br />
----------------------------------------------------------------------------<br />
-- Link functions<br />
--<br />
-- The following functions make the links from the link codes and the user<br />
-- data snippets. New link functions should be added below the existing<br />
-- functions.<br />
--<br />
-- For documentation on how to add new link functions, please see<br />
-- [[Module:UserLinks#Adding new links]].<br />
----------------------------------------------------------------------------<br />
<br />
function linkFunctions.u(snippets)<br />
-- User page<br />
return makeWikilink(<br />
snippets.interwiki,<br />
2,<br />
snippets.username,<br />
snippets.username<br />
)<br />
end<br />
<br />
function linkFunctions.t(snippets)<br />
-- User talk page<br />
return makeWikilink(<br />
snippets.interwiki,<br />
3,<br />
snippets.username,<br />
message('display-talk')<br />
)<br />
end<br />
<br />
function linkFunctions.c(snippets)<br />
-- Contributions<br />
return makeWikilink(<br />
snippets.interwiki,<br />
-1,<br />
'Contribs/' .. snippets.username,<br />
message('display-contributions')<br />
)<br />
end<br />
<br />
function linkFunctions.ct(snippets)<br />
-- Edit count<br />
return makeUrlLink(<br />
{<br />
host = 'tools.wmflabs.org',<br />
path = '/xtools-ec/',<br />
query = {<br />
user = snippets.username,<br />
project = snippets.toolLang .. '.' .. snippets.projectLong .. '.org'<br />
}<br />
},<br />
message('display-count')<br />
)<br />
end<br />
<br />
function linkFunctions.m(snippets)<br />
-- Page moves<br />
return makeWikilink(<br />
snippets.interwiki,<br />
-1,<br />
'Log/move/' .. snippets.username,<br />
message('display-moves')<br />
)<br />
end<br />
<br />
function linkFunctions.l(snippets)<br />
-- Logs<br />
return makeWikilink(<br />
snippets.interwiki,<br />
-1,<br />
'Log/' .. snippets.username,<br />
message('display-logs')<br />
)<br />
end<br />
<br />
function linkFunctions.bl(snippets)<br />
-- Block log<br />
return makeFullUrlLink(<br />
snippets.interwiki,<br />
-1,<br />
'Log/block',<br />
{page = 'User:' .. snippets.username},<br />
message('display-blocklog')<br />
)<br />
end<br />
<br />
function linkFunctions.bls(snippets)<br />
-- Blocks<br />
return makeWikilink(<br />
snippets.interwiki,<br />
-1,<br />
'Log/block/' .. snippets.username,<br />
message('display-blocks')<br />
)<br />
end<br />
<br />
function linkFunctions.bu(snippets)<br />
-- Block user<br />
return makeWikilink(<br />
snippets.interwiki,<br />
-1,<br />
'Block/' .. snippets.username,<br />
message('display-blockuser')<br />
)<br />
end<br />
<br />
function linkFunctions.ca(snippets)<br />
-- Central auth<br />
return makeWikilink(<br />
snippets.interwiki,<br />
-1,<br />
'CentralAuth/' .. snippets.username,<br />
message('display-centralauth')<br />
)<br />
end<br />
<br />
function linkFunctions.dc(snippets)<br />
-- Deleted contribs<br />
return makeWikilink(<br />
snippets.interwiki,<br />
-1,<br />
'DeletedContributions/' .. snippets.username,<br />
message('display-deletedcontributions')<br />
)<br />
end<br />
<br />
function linkFunctions.e(snippets)<br />
-- Email<br />
return makeWikilink(<br />
snippets.interwiki,<br />
-1,<br />
'EmailUser/' .. snippets.username,<br />
message('display-email')<br />
)<br />
end<br />
<br />
function linkFunctions.es(snippets)<br />
-- Edit summaries<br />
return makeUrlLink(<br />
{<br />
host = 'tools.wmflabs.org',<br />
path = '/xtools/editsummary/index.php',<br />
query = {<br />
name = snippets.username,<br />
lang = snippets.toolLang,<br />
wiki = snippets.projectLong<br />
}<br />
},<br />
message('display-editsummaries')<br />
)<br />
end<br />
<br />
function linkFunctions.del(snippets)<br />
-- Deletions<br />
return makeWikilink(<br />
snippets.interwiki,<br />
-1,<br />
'Log/delete/' .. snippets.username,<br />
message('display-deletions')<br />
)<br />
end<br />
<br />
function linkFunctions.lu(snippets)<br />
-- List user<br />
return makeFullUrlLink(<br />
snippets.interwiki,<br />
-1,<br />
'ListUsers',<br />
{limit = 1, username = snippets.username},<br />
message('display-listuser')<br />
)<br />
end<br />
<br />
function linkFunctions.sul(snippets)<br />
-- SUL<br />
return makeWikilink(<br />
nil,<br />
nil,<br />
'sulutil:' .. snippets.username,<br />
message('display-sul')<br />
)<br />
end<br />
<br />
function linkFunctions.tl(snippets)<br />
-- Target logs<br />
return makeFullUrlLink(<br />
snippets.interwiki,<br />
-1,<br />
'Log',<br />
{page = mw.site.namespaces[2].name .. ':' .. snippets.username},<br />
message('display-targetlogs')<br />
)<br />
end<br />
<br />
function linkFunctions.efl(snippets)<br />
-- Edit filter log<br />
return makeFullUrlLink(<br />
snippets.interwiki,<br />
-1,<br />
'AbuseLog',<br />
{wpSearchUser = snippets.username},<br />
message('display-abuselog')<br />
)<br />
end<br />
<br />
function linkFunctions.pr(snippets)<br />
-- Protections<br />
return makeWikilink(<br />
snippets.interwiki,<br />
-1,<br />
'Log/protect/' .. snippets.username,<br />
message('display-protections')<br />
)<br />
end<br />
<br />
function linkFunctions.rl(snippets)<br />
-- User rights<br />
return makeWikilink(<br />
snippets.interwiki,<br />
-1,<br />
'Log/rights/' .. snippets.username,<br />
message('display-rights')<br />
)<br />
end<br />
<br />
function linkFunctions.ren(snippets)<br />
-- Renames<br />
return makeWikilink(<br />
snippets.interwiki,<br />
-1,<br />
'Log/renameuser/' .. snippets.username,<br />
message('display-renames')<br />
)<br />
end<br />
<br />
function linkFunctions.rfa(snippets)<br />
-- Requests for adminship<br />
return makeWikilink(<br />
nil,<br />
-1,<br />
'PrefixIndex/' .. message('page-rfa') .. '/' .. snippets.username,<br />
message('display-rfa')<br />
)<br />
end<br />
<br />
function linkFunctions.api(snippets)<br />
-- API user data<br />
return makeUrlLink(<br />
{<br />
host = snippets.fullDomain,<br />
path = '/w/api.php',<br />
query = {<br />
action = 'query',<br />
list = 'users',<br />
usprop = 'groups|editcount',<br />
ususers = snippets.username<br />
}<br />
},<br />
message('display-api')<br />
)<br />
end<br />
<br />
function linkFunctions.up(snippets)<br />
-- Uploads<br />
return makeWikilink(<br />
snippets.interwiki,<br />
-1,<br />
'ListFiles/' .. snippets.username,<br />
message('display-uploads')<br />
)<br />
end<br />
<br />
----------------------------------------------------------------------------<br />
-- End of link functions<br />
----------------------------------------------------------------------------<br />
<br />
-- Define the metatable that memoizes the link functions, and fetches link<br />
-- functions from [[Module:UserLinks/extra]] if necessary.<br />
<br />
-- Lazily initialise the extraLinkFunctions table. We only want to load<br />
-- [[Module:UserLinks/extra]] as necessary, so it has a low transclusion<br />
-- count.<br />
local extraLinkFunctions<br />
<br />
-- Define functions for shared code in the metatable.<br />
local function validateCode(code)<br />
-- Checks whether code is a valid link code - i.e. checks that it is a<br />
-- string and that it is not the blank string. Returns the code if<br />
-- the check passes, and nil if not.<br />
if type(code) == 'string' and code ~= '' then<br />
return code<br />
else<br />
return nil<br />
end<br />
end<br />
<br />
local function getExtraLinkFunctions()<br />
-- Loads the table of extra link functions from the /extra module.<br />
-- If there is a problem with loading it, return false. We use the<br />
-- distinction between false and nil to record whether we have already<br />
-- tried to load it.<br />
if extraLinkFunctions ~= nil then<br />
return extraLinkFunctions<br />
end<br />
if mExtra == nil then<br />
-- If loading the module fails, maybeLoadModule returns false.<br />
-- Here we use the distinction between false and nil to record<br />
-- whether we have already tried to load the /extra module.<br />
mExtra = maybeLoadModule('Module:UserLinks/extra')<br />
end<br />
if type(mExtra) == 'table'<br />
and type(mExtra.linkFunctions) == 'table'<br />
then<br />
extraLinkFunctions = mExtra.linkFunctions<br />
else<br />
extraLinkFunctions = false<br />
end<br />
return extraLinkFunctions<br />
end<br />
<br />
local function memoizeExtraLink(code, func)<br />
local success, link = pcall(func, snippets)<br />
if success and type(link) == 'string' then<br />
links[code] = link<br />
return link<br />
end<br />
return nil<br />
end<br />
<br />
-- Define the metatable.<br />
setmetatable(links, {<br />
__index = function (t, key)<br />
local code = validateCode(key)<br />
if not code then<br />
raiseError(<br />
message('error-malformedlinkcode'),<br />
message('error-malformedlinkcode-section')<br />
)<br />
end<br />
local linkFunction = linkFunctions[code]<br />
local link<br />
if linkFunction then<br />
link = linkFunction(snippets)<br />
links[code] = link<br />
else<br />
extraLinkFunctions = getExtraLinkFunctions()<br />
if extraLinkFunctions then<br />
local extraLinkFunction = extraLinkFunctions[code]<br />
if type(extraLinkFunction) == 'function' then<br />
link = memoizeExtraLink(code, extraLinkFunction)<br />
end<br />
end<br />
end<br />
if link then<br />
return link<br />
else<br />
raiseError(<br />
message('error-invalidlinkcode', code),<br />
message('error-invalidlinkcode-section')<br />
)<br />
end<br />
end,<br />
__pairs = function ()<br />
extraLinkFunctions = getExtraLinkFunctions()<br />
if extraLinkFunctions then<br />
for code, func in pairs(extraLinkFunctions) do<br />
if validateCode(code) and type(func) == 'function' then<br />
memoizeExtraLink(code, func)<br />
end<br />
end<br />
end<br />
-- Allow built-in functions to overwrite extra functions.<br />
for code, func in pairs(linkFunctions) do<br />
local link = func(snippets)<br />
links[code] = link<br />
end<br />
return function (t, key)<br />
return next(links, key)<br />
end<br />
end<br />
})<br />
return links<br />
end<br />
<br />
--------------------------------------------------------------------------------<br />
-- User data snippets<br />
--------------------------------------------------------------------------------<br />
<br />
function p.getSnippets(args)<br />
--[=[<br />
-- This function gets user data snippets from the arguments, and from<br />
-- [[Module:InterwikiTable]]. The data is loaded as necessary and memoized<br />
-- in the snippets table for performance. <br />
--<br />
-- Snippets default to the blank string, '', so they can be used in<br />
-- concatenation operations without coders having to worry about raising<br />
-- errors. Because of this, the local functions snippetExists and<br />
-- getSnippet have been written to aid people writing new snippets. These<br />
-- functions treat the blank string as false. It is not necessary to return<br />
-- the blank string from a snippet function, as nil and false values are<br />
-- automatically converted into the blank string by the metatable.<br />
--<br />
-- If you add a new snippet, please document it at<br />
-- [[Module:UserLinks#Adding new links]].<br />
--]=]<br />
local snippets, snippetFunctions = {}, {}<br />
setmetatable(snippets, {<br />
__index = function (t, key)<br />
local snippetFunction = snippetFunctions[key]<br />
if snippetFunction then<br />
snippets[key] = snippetFunction() or ''<br />
return snippets[key]<br />
else<br />
raiseError(<br />
message('error-nosnippet', key),<br />
message('error-nosnippet-section')<br />
)<br />
end<br />
end<br />
})<br />
<br />
-- Define helper functions for writting the snippet functions.<br />
local function snippetExists(key)<br />
-- We have set the metatable up to make snippets default to '', so we<br />
-- don't have to test for false or nil.<br />
return snippets[key] ~= ''<br />
end<br />
<br />
local function getSnippet(key)<br />
local ret = snippets[key]<br />
if ret == '' then<br />
return nil<br />
else<br />
return ret<br />
end<br />
end<br />
<br />
-- Start snippet functions.<br />
<br />
function snippetFunctions.username()<br />
-- The username.<br />
local username = args.user or args.User<br />
return username or raiseError(<br />
message('error-nousername'),<br />
message('error-nousername-section')<br />
)<br />
end<br />
<br />
function snippetFunctions.usernameHtml()<br />
-- The username html-encoded. Spaces are encoded as pluses.<br />
return mw.uri.encode(snippets.username)<br />
end<br />
<br />
function snippetFunctions.project()<br />
-- The project name.<br />
-- Also does the work for snippetFunctions.interwikiTableKey, and adds<br />
-- the project value to snippets.lang if it is a valid language code.<br />
local project = args.Project or args.project<br />
if not project then<br />
return nil<br />
end<br />
local projectValidated, interwikiTableKey = p.validateProjectCode(project)<br />
if not projectValidated then<br />
if mw.language.isKnownLanguageTag(project) then<br />
if not snippetExists('lang') then<br />
snippets.lang = project<br />
end<br />
else<br />
raiseError(<br />
message('error-invalidproject', project),<br />
message('error-invalidproject-section')<br />
)<br />
end<br />
end<br />
snippets.interwikiTableKey = interwikiTableKey<br />
return project<br />
end<br />
<br />
function snippetFunctions.interwikiTableKey()<br />
-- The key for the project in Module:InterwikiTable.<br />
-- Relies on snippetFunctions.project to do the real work.<br />
local temp = snippets.project -- required; puts key in snippets table<br />
return rawget(snippets, 'interwikiTableKey')<br />
end<br />
<br />
function snippetFunctions.toolProject()<br />
-- The short project code for use with toolserver or labs. It is always<br />
-- present, even if the "project" argument is absent. The default value<br />
-- is the "snippet-project-default" message.<br />
local project = getSnippet('project')<br />
if not project then<br />
return message('snippet-project-default')<br />
else<br />
return project<br />
end<br />
end<br />
<br />
function snippetFunctions.projectLong()<br />
-- The long form of the project name, e.g. "wikipedia" or "wikibooks".<br />
local key = getSnippet('interwikiTableKey')<br />
if not key then<br />
return message('snippet-projectlong-default')<br />
end<br />
interwikiTable = interwikiTable or mw.loadData('Module:InterwikiTable')<br />
local prefixes = interwikiTable[key].iw_prefix<br />
-- Using prefixes[2] is a bit of a hack, but should find the long name<br />
-- most of the time.<br />
return prefixes[2] or prefixes[1] <br />
end<br />
<br />
function snippetFunctions.lang()<br />
-- The language code.<br />
local lang = args.lang or args.Lang<br />
if not lang then<br />
return nil<br />
end<br />
if mw.language.isKnownLanguageTag(lang) then<br />
return lang<br />
else<br />
raiseError(<br />
message('error-invalidlanguage', lang),<br />
message('error-invalidlanguage-section')<br />
)<br />
end<br />
end<br />
<br />
function snippetFunctions.toolLang()<br />
-- The language code for use with toolserver or labs tools. It is always<br />
-- present, even if the "lang" argument is absent. The default value is<br />
-- the "snippet-lang-default" message. <br />
return getSnippet('lang') or message('snippet-lang-default')<br />
end<br />
<br />
function snippetFunctions.interwiki()<br />
-- The interwiki prefix, consisting of the project and language values,<br />
-- separated by colons, e.g. ":wikt:es:".<br />
local project = getSnippet('project')<br />
local lang = getSnippet('lang')<br />
if not project and not lang then<br />
return nil<br />
end<br />
local ret = {}<br />
ret[#ret + 1] = project<br />
ret[#ret + 1] = lang<br />
return table.concat(ret, ':')<br />
end<br />
<br />
function snippetFunctions.fullDomain()<br />
-- The full domain name of the site, e.g. www.mediawiki.org,<br />
-- en.wikpedia.org, or ja.wikibooks.org.<br />
local fullDomain<br />
local lang = getSnippet('toolLang')<br />
local key = getSnippet('interwikiTableKey')<br />
if key then<br />
interwikiTable = interwikiTable or mw.loadData('Module:InterwikiTable')<br />
local domain = interwikiTable[key].domain<br />
local takesLangPrefix = interwikiTable[key].takes_lang_prefix<br />
if takesLangPrefix then<br />
fullDomain = lang .. '.' .. domain<br />
else<br />
fullDomain = domain<br />
end<br />
else<br />
fullDomain = lang .. '.wikipedia.org'<br />
end<br />
return fullDomain<br />
end<br />
<br />
-- End snippet functions. If you add a new snippet function, please<br />
-- document it at [[Module:UserLinks#Adding new links]].<br />
<br />
return snippets<br />
end <br />
<br />
function p.validateProjectCode(s)<br />
-- Validates a project code, by seeing whether it is present in<br />
-- [[Module:InterwikiTable]]. If it is present, returns the code and the<br />
-- InterwikiTable key for the corresponding site. If not present,<br />
-- returns nil for both.<br />
interwikiTable = interwikiTable or mw.loadData('Module:InterwikiTable')<br />
for key, t in pairs(interwikiTable) do<br />
for i, prefix in ipairs(t.iw_prefix) do<br />
if s == prefix then<br />
return s, key<br />
end<br />
end<br />
end<br />
return nil, nil<br />
end<br />
<br />
--------------------------------------------------------------------------------<br />
-- Main functions<br />
--------------------------------------------------------------------------------<br />
<br />
local function makeInvokeFunction(funcName)<br />
-- Makes a function that can be accessed from #invoke. This is only required<br />
-- for functions that need to access arguments.<br />
return function (frame)<br />
mArguments = require('Module:Arguments')<br />
local args = mArguments.getArgs(frame)<br />
return p[funcName](args)<br />
end<br />
end<br />
<br />
p.main = makeInvokeFunction('_main')<br />
<br />
function p._main(args)<br />
-- The main function. This is the one called from [[Template:User-multi]],<br />
-- via p.main.<br />
local options = p.getOptions(args)<br />
local snippets = p.getSnippets(args)<br />
local codes = p.getCodes(args)<br />
local links = p.getLinks(snippets)<br />
-- Overload the built-in Lua error function to generate wikitext errors<br />
-- meant for end users to see. This makes things harder to debug when<br />
-- real errors occur, but it is the only realistic way to show wikitext<br />
-- errors and and still have sane code when using metatables, etc.<br />
local success, result = pcall(p.export, codes, links, options)<br />
if success then<br />
return result<br />
else<br />
return makeWikitextError(result, options.isDemo)<br />
end<br />
end<br />
<br />
function p.getOptions(args)<br />
-- Gets the options from the args table, so that we don't have to pass<br />
-- around the whole args table all the time.<br />
local options = {}<br />
options.isDemo = yesno(args.demo) or false<br />
options.toolbarStyle = yesno(args.small) and 'font-size: 90%;' or nil<br />
options.sup = yesno(args.sup, true)<br />
options.separator = args.separator<br />
options.span = args.span<br />
return options<br />
end<br />
<br />
function p.getCodes(args)<br />
-- Gets the link codes from the arguments. The codes aren't validated<br />
-- at this point.<br />
mTableTools = maybeLoadModule('Module:TableTools')<br />
local codes<br />
if mTableTools then<br />
codes = mTableTools.compressSparseArray(args)<br />
else<br />
codes = {}<br />
for i, code in ipairs(args) do<br />
codes[i] = code<br />
end<br />
end<br />
return codes<br />
end<br />
<br />
function p.export(codes, links, options)<br />
-- Make the user link.<br />
local userLink = links.u<br />
<br />
-- If we weren't passed any link codes, just return the user link.<br />
if #codes < 1 then<br />
return userLink<br />
end<br />
<br />
-- Make the toolbar.<br />
mToolbar = require('Module:Toolbar')<br />
local toolbarArgs = {}<br />
for i, code in ipairs(codes) do<br />
local link = links[code]<br />
toolbarArgs[#toolbarArgs + 1] = link<br />
end<br />
toolbarArgs.style = options.toolbarStyle<br />
toolbarArgs.separator = options.separator or 'dot'<br />
toolbarArgs.span = options.span<br />
local toolbar = mToolbar.main(toolbarArgs)<br />
<br />
-- Apply the sup option.<br />
if options.sup then<br />
toolbar = '<sup>' .. toolbar .. '</sup>'<br />
end<br />
<br />
-- If we are transcluding, add a non-breaking space, but if we are substing<br />
-- just use a normal space<br />
local space = mw.isSubsting() and ' ' or '&nbsp;'<br />
<br />
return userLink .. space .. toolbar<br />
end<br />
<br />
--------------------------------------------------------------------------------<br />
-- Single link function<br />
--------------------------------------------------------------------------------<br />
<br />
p.single = makeInvokeFunction('_single')<br />
<br />
function p._single(args)<br />
-- Fetches a single link from the link table.<br />
local options = p.getOptions(args)<br />
local snippets = p.getSnippets(args)<br />
local links = p.getLinks(snippets)<br />
local code = args[1]<br />
local success, link = pcall(p.exportSingle, links, code)<br />
if success then<br />
return link<br />
else<br />
return makeWikitextError(link, options.isDemo)<br />
end<br />
end<br />
<br />
function p.exportSingle(links, code)<br />
-- If any errors occur, they will probably occur here. This function<br />
-- exists purely so that all the errors that will occur in p._single can<br />
-- be handled using a single pcall.<br />
if not code then<br />
raiseError(<br />
message('error-nolinkcode'),<br />
message('error-nolinkcode-section')<br />
)<br />
end<br />
return links[code]<br />
end<br />
<br />
--------------------------------------------------------------------------------<br />
-- Link table<br />
--------------------------------------------------------------------------------<br />
<br />
function p.linktable()<br />
-- Returns a wikitext table of link codes, with an example link for each<br />
-- one. This function doesn't take any arguments, so it can be accessed<br />
-- directly from wiki pages without using makeInvokeFunction.<br />
local args = {user = 'Example'}<br />
local snippets = p.getSnippets(args)<br />
local links = p.getLinks(snippets)<br />
<br />
-- Assemble the codes and links in order<br />
local firstCodes = {'u', 't', 'c'}<br />
local firstLinks, firstCodesKeys = {}, {}<br />
for i, code in ipairs(firstCodes) do<br />
firstCodesKeys[code] = true<br />
firstLinks[#firstLinks + 1] = {code, links[code]}<br />
end<br />
local secondLinks = {}<br />
for code, link in pairs(links) do<br />
if not firstCodesKeys[code] then<br />
secondLinks[#secondLinks + 1] = {code, link}<br />
end<br />
end<br />
table.sort(secondLinks, function(t1, t2)<br />
return t1[1] < t2[1]<br />
end)<br />
local links = {}<br />
for i, t in ipairs(firstLinks) do<br />
links[#links + 1] = t<br />
end<br />
for i, t in ipairs(secondLinks) do<br />
links[#links + 1] = t<br />
end<br />
<br />
-- Output the code table in table format<br />
local ret = {}<br />
ret[#ret + 1] = '{| class="wikitable plainlinks sortable"'<br />
ret[#ret + 1] = '|-'<br />
ret[#ret + 1] = '! ' .. message('linktable-codeheader')<br />
ret[#ret + 1] = '! ' .. message('linktable-previewheader')<br />
for i, t in ipairs(links) do<br />
local code = t[1]<br />
local link = t[2]<br />
ret[#ret + 1] = '|-'<br />
ret[#ret + 1] = "| '''" .. code .. "'''" <br />
ret[#ret + 1] = '| ' .. link<br />
end<br />
ret[#ret + 1] = '|}'<br />
return table.concat(ret, '\n')<br />
end <br />
<br />
return p</div>Joaquinito2017https://testwiki.wiki/index.php?title=Template:User&diff=3335Template:User2017-10-15T14:16:30Z<p>Joaquinito2017: 1 revision imported</p>
<hr />
<div><includeonly>{{safesubst:#invoke:user|main}}</includeonly><noinclude><br />
<!-- <br />
-- Use the automatic documentation from [[Template:User-multi/template]].<br />
-- Some parameters must be blank, but not absent, to work correctly.<br />
--<br />
-->{{User-multi/template <br />
| User = <br />
| Project = <br />
| Lang = <br />
| separator = <br />
| 1 = t<br />
| 2 = c<br />
| demo = <br />
| span = no<br />
| doc = yes<br />
}}<br />
<!-- Categories go on the /doc subpage, and interwikis go on Wikidata. --><br />
</noinclude></div>Joaquinito2017https://testwiki.wiki/index.php?title=Test_Wiki:Request_for_permissions&diff=2366Test Wiki:Request for permissions2017-10-15T13:50:57Z<p>Joaquinito2017: </p>
<hr />
<div></div>Joaquinito2017https://testwiki.wiki/index.php?title=Test_Wiki:Request_for_permissions&diff=2355Test Wiki:Request for permissions2017-10-15T09:18:04Z<p>Joaquinito2017: </p>
<hr />
<div></div>Joaquinito2017