Module:UserLinks/sandbox: Difference between revisions

Content deleted Content added
add missing error check to p.single
convert p.getLinkFunctions to p.getLinks, so that we don't load the /extra module every time; __pairs function still buggy
Line 3:
 
-- Lazily initialise modules that we might or might not need
local mExtra -- [[Module:UserLinks/extra]]
local mArguments
local mArguments -- [[Module:Arguments]]
local mToolbar
local mToolbar -- [[Module:Toolbar]]
local mCategoryHandler
local mCategoryHandler -- [[Module:Category handler]]
local mTableTools
local interwikiTablemTableTools -- [[Module:InterwikiTable, loaded with mw.loadDataTableTools]]
local interwikiTable -- [[Module:InterwikiTable]], loaded with mw.loadData
 
-- Load shared helper functions
Line 22 ⟶ 23:
--------------------------------------------------------------------------------
 
function p.getLinkFunctionsgetLinks(snippets)
local links, linkFunctions = {}, {}
local extraLinkFunctions
 
-- Get extra linkDefine functions first,for andshared allowcode the built-in link functionsthe tometatable.
local function validateCode(code)
-- overwrite them. Extra link functions are stored in /extra. We need to
if type(code) == 'string' and code ~= '' then
-- give the built-in functions priority, or a vandal could change high-risk
return code
-- link functions from the /extra module.
else
local success, mExtra = pcall(require, 'Module:UserLinks/extra')
return nil
if success then
end
local extraLinkFunctions = type(mExtra) == 'table' and mExtra.linkFunctions
end
if type(extraLinkFunctions) == 'table' then
 
for code, func in pairs(extraLinkFunctions) do
local function loadExtra()
if type(code) == 'string'
andif codemExtra ~== nil ''then
local success
and type(func) == 'function'
success, module = pcall(require, 'Module:UserLinks/extra')
then
if success then
linkFunctions[code] = func
endmExtra = module
else
mExtra = false
end
end
end
 
local function getExtraLinkFunctions()
-- Built-in link functions
if extraLinkFunctions ~= nil then
return extraLinkFunctions
end
loadExtra()
if type(mExtra) == 'table' and type(mExtra.linkFunctions) == 'table' then
extraLinkFunctions = mExtra.linkFunctions
else
extraLinkFunctions = false
end
return extraLinkFunctions
end
 
-- Define the metatable.
setmetatable(links, {
__index = function (t, key)
local code = validateCode(key)
if not code then
raiseError('invalid link code detected|Invalid link code')
end
local linkFunction = linkFunctions[code]
local link
if linkFunction then
link = linkFunction(snippets)
links[code] = link
else
extraLinkFunctions = getExtraLinkFunctions()
if extraLinkFunctions then
local extraLinkFunction = extraLinkFunctions[code]
if type(extraLinkFunction) == 'function' then
link = extraLinkFunction(snippets)
links[code] = link
end
end
end
return link
end,
__pairs = function ()
return function (t, key)
local nextBuiltInKey, linkFunction = next(linkFunctions, key)
if linkFunction then
local link = linkFunction(snippets)
links[nextBuiltInKey] = link
return nextBuiltInKey, link
else
extraLinkFunctions = getExtraLinkFunctions()
if extraLinkFunctions then
local nextExtraKey, extraLinkFunction = next(extraLinkFunctions, key)
if validateCode(nextKey) and type(extraLinkFunction) == 'function' then
local link = extraLinkFunction(snippets)
links[nextExtraKey] = link
return nextExtraKey, link
end
end
end
return nil, nil
end
end
})
 
-- Define the built-in link functions.
function linkFunctions.u(snippets)
-- User page
Line 174 ⟶ 238:
end
 
return linkFunctionslinks
end
 
Line 352 ⟶ 416:
local snippets = p.getSnippets(args)
local codes = p.getCodes(args)
local linkFunctionslinks = p.getLinkFunctionsgetLinks(snippets)
local success, result = pcall(p.export, codes, snippets, linkFunctionslinks, options)
if success then
return result
Line 377 ⟶ 441:
end
 
function p.export(codes, snippets, linkFunctionslinks, options)
-- Make the user link.
local userLink = linkFunctionslinks.u(snippets)
 
-- If we weren't passed any link codes, just return the user link.
Line 390 ⟶ 454:
local toolbarArgs = {}
for i, code in ipairs(codes) do
local linkFunctionlink = linkFunctionslinks[code]
if not linkFunction then
raiseError('"' .. code .. '" is not a valid link code', 'Not a valid link code')
end
local link = linkFunction(snippets)
toolbarArgs[#toolbarArgs + 1] = link
end
Line 425 ⟶ 485:
end
local snippets = p.getSnippets(args)
local linkFunctionslinks = p.getLinkFunctionsgetLinks()
 
local linkFunction = linkFunctions[code]
-- Define a function so we can get the link with pcall, as indexing the
if not linkFunction then
-- links table might produce an error because of how the metatable is set up.
return makeWikitextError(
local function getLink(code)
'"' .. code .. '" is not a valid link code|Not a valid link code',
return links[code]
options.isDemo
)
end
 
local success, result = pcall(linkFunction, snippets)
local success, link = pcall(getLink, code)
if success then
return resultlink
else
return makeWikitextError(resultlink, options.isDemo)
end
end