Module:UserLinks: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
add capability to link to a single link, use a custom error function, and generate html errors for invalid link types
(fix bug where the username error wasn't showing up properly, add more comments)
(add capability to link to a single link, use a custom error function, and generate html errors for invalid link types)
Line 1:
local p = {}
 
local ToolbarBuilder = require('Module:Toolbar')
local u = {} -- Table for user-data helper strings.
 
-- Define a custom error message for this module.
local function err(msg, cat)
cat = (cat and ('[[Category:' .. cat .. ']]')) or ''
return '<span class="error">[[Module:UserLinks]] error: ' .. msg .. '.</span>' .. cat
end
 
--------------------------------------------------------------------------------------------------
Line 137 ⟶ 141:
rfa = makeRfaLink
}
if not linktypes[linktype] then
return err('"' .. linktype .. '" is not a valid link code', 'UserLinks transclusions with invalid link types')
end
return linktypes[linktype]()
end
Line 169 ⟶ 176:
end
 
local function getLinksgetSingleLink(args)
local linktype = args[1]
-- If the username is absent or blank, return an html error and a tracking category.
if not linktype then
if args.user == '' or (not args.user and (not args.User or args.User == '')) then
return err('<span class="error">Error: No usernamelink detectedtype byspecified', [[Module:UserLinks]].</span>[[Category:'UserLinks transclusions withoutwith invalid link usernames]]types')
end
return getLink(linktype)
-- Generate the user data strings.
end
generateUserDataStrings(args)
 
local function getLinks(args)
-- Build the template output.
local result = makeToolbar(args) -- Get the toolbar contents.
Line 192 ⟶ 200:
end
 
local function p.mainmakeWrapper(framefunc)
return function (frame)
-- If called via #invoke, use the args passed into the invoking template.
-- Otherwise,If forcalled testingvia purposes#invoke, assumeuse the args arepassed beinginto passedthe directlyinvoking intemplate.
-- Otherwise, for testing purposes, assume args are being passed directly in.
local origArgs
if frame == mw.getCurrentFrame() then
origArgsif frame == frame:getParentmw.getCurrentFrame().args then
for k, v in pairs(origArgs = frame:getParent().args) do
origArgsfor =k, v in pairs(frame.args) do
break origArgs = frame.args
break
origArgs = frame end
else
origArgs = frame
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
 
end
-- If the username 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 err('No username detected', 'UserLinks transclusions without usernames')
end
-- Generate the user data strings.
generateUserDataStrings(args)
return getLinksfunc(args)
end
end
 
return p{
main = makeWrapper(getLinks),
single = makeWrapper(getSingleLink)
}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu