Module:UserLinks: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
automatically detect project and language values from the project argument input
(add function for generating tracking categories, enable lower-case project parameter name)
(automatically detect project and language values from the project argument input)
Line 1:
local ToolbarBuilder = require('Module:Toolbar')
local interwikiTable = mw.loadData("Module:InterwikiTable")
 
local u = {} -- Table for user-data helper strings.
local demo
 
-- Define a custom error message for this module.
local function err(msg, cat)
return '<span class="error">[[Module:UserLinks]] error: ' .. msg .. '.</span>'[[Category:UserLinks ..transclusions catwith errors]]'
if cat and not demo then
cat = '[[Category:' .. cat .. ']]'
else
cat = ''
end
return '<span class="error">[[Module:UserLinks]] error: ' .. msg .. '.</span>' .. cat
end
 
--------------------------------------------------------------------------------------------------
-- Below are the helper strings available for writing user link functions. --
-- If the project name has not been specified, all the project strings will be --
-- theu.username blank string, '' The plain username. If the username is not specified,present then the module outputs an error. --
-- Either way, there is no need to worry about escaping nil values when using the module returns an error. --
-- u.usernameHtml = mw.uri.encode(u.username) -- Html-encoded The username html-encoded. Spaces are encoded aswith plusesplus signs. --
-- helper strings. --
-- u.project The project name. Nil if not specified. --
-- u.username The plain username lang The language code. Nil if not specified. --
-- u.usernameHtmlinterwiki The username html-encoded. SpacesThe interwiki areprefix, encodedconsisting withof plusthe signs.project and language --
-- values, separated by colons, e.g. ":wikt:es:". If no project --
-- u.project The plain project name. or language values are found, this is the blank string, "". --
-- helper strings. --
-- u.projectColon The project name with two colons surrounding it, i.e. ":project:" --
-- If you want more strings, you can define them in the generateUserDataStrings --
-- Iffunction below. you want more strings, you can define them in the generateUserDataStrings --
--------------------------------------------------------------------------------------------------
-- function below. --
--------------------------------------------------------------------------------------------------
 
local function makeUserLink()
return '[[' .. u.projectColoninterwiki .. 'User:' .. u.username .. '|' .. u.username .. ']]'
end
 
local function makeTalkLink()
return '[[' .. u.projectColoninterwiki .. 'User talk:' .. u.username .. '|talk]]'
end
 
local function makeContribsLink()
return '[[' .. u.projectColoninterwiki .. 'Special:Contributions/' .. u.username .. '|contribs]]'
end
 
Line 47 ⟶ 43:
 
local function makeMovesLink()
return '[[' .. u.projectColoninterwiki .. 'Special:Log/move/' .. u.username .. '|page&nbsp;moves]]'
end
 
local function makeLogsLink()
return '[[' .. u.projectColoninterwiki .. 'Special:Log/' .. u.username .. '|logs]]'
end
 
Line 60 ⟶ 56:
 
local function makeBlocksLink()
return '[[' .. u.projectColoninterwiki .. 'Special:Log/block/' .. u.username .. '|blocks]]'
end
 
local function makeBlockUserLink()
return '[[' .. u.projectColoninterwiki .. 'Special:Block/' .. u.username .. '|block&nbsp;user]]'
end
 
local function makeCentralAuthLink()
return '[[' .. u.projectColoninterwiki .. 'Special:CentralAuth/' .. u.username .. '|central&nbsp;auth]]'
end
 
local function makeDeletedContribsLink()
return '[[' .. u.projectColoninterwiki .. 'Special:DeletedContributions/' .. u.username .. '|deleted&nbsp;contribs]]'
end
 
local function makeEmailLink()
return '[[' .. u.projectColoninterwiki .. 'Special:Emailuser/' .. u.username .. '|email]]'
end
 
Line 84 ⟶ 80:
 
local function makeDeletionsLink()
return '[[' .. u.projectColoninterwiki .. 'Special:Log/delete/' .. u.username .. '|deletions]]'
end
 
Line 107 ⟶ 103:
 
local function makeProtectionsLink()
return '[[' .. u.projectColoninterwiki .. 'Special:Log/protect/' .. u.username .. '|protections]]'
end
 
local function makeRightsLink()
return '[[' .. u.projectColoninterwiki .. 'Special:Log/rights/' .. u.username .. '|rights]]'
end
 
local function makeRenamesLink()
return '[[' .. u.projectColoninterwiki .. 'Special:Log/renameuser/' .. u.username .. '|renames]]'
end
 
Line 147 ⟶ 143:
}
if not linktypes[linktype] then
return err('"' .. linktype .. '" is not a valid link code', 'UserLinks transclusions with invalid link types')
end
return linktypes[linktype]()
Line 169 ⟶ 165:
return ToolbarBuilder.main(targs)
end
end
 
local function isKnownProject(prefix)
for projectKey, projectVal in pairs(interwikiTable) do
for _, iwCode in ipairs(projectVal.iw_prefix) do
if iwCode == prefix then
return true
end
end
end
return false
end
 
local function generateUserDataStrings(args)
-- If the username is absent or blank, return an error and a tracking category.
-- Username helper strings.
uif args.usernameuser == '' or (not args.user and (not args.User or args.User == '')) then
return err('no username detected', 'UserLinks transclusions without usernames')
u.usernameHtml = mw.uri.encode(u.username) -- Html-encoded username. Spaces are encoded as pluses.
else
-- Project helper strings.
u.username = args.user or args.User
end
-- GenerateGet other thebasic user data strings.
u.project = args.Project or args.project
u.lang = args.lang or args.Lang
u.projectColon = (u.project and (':' .. u.project .. ':')) or '' -- Project value plus colons, for escaping interwiki links.
u.project = u.project or '' -- Control for nil values of args.Project.
-- Process the project value if it is present.
if cat and not demou.project then
-- If u.project is a known project, we don't need to do anything. If the project isn't known, first
-- check whether it is a valid language code, and if not then see if it's an interwiki code
-- separated by colons, e.g. "wikt:es".
if not isKnownProject(u.project) then
if mw.language.isKnownLanguageTag(u.project) then
u.lang = u.project
u.project = nil
else
-- Guess
local pref1, pref2 = mw.ustring.match( u.project, '(%w+):(%w+)' )
if pref1 and pref2 then
if isKnownProject(pref1) and mw.language.isKnownLanguageTag(pref2) then
u.project = pref1
u.lang = pref2
elseif isKnownProject(pref2) and mw.language.isKnownLanguageTag(pref1) then
u.project = pref2
u.lang = pref1
else
return err('"' .. u.project .. '" is not a valid interwiki prefix')
end
else
return err('"' .. u.project .. '" is not a valid interwiki prefix')
end
end
end
end
-- Generate the interwiki prefix. This includes colons.
if u.project or u.lang then
catu.interwiki = ''
if u.project then
u.interwiki = u.interwiki .. ':' .. u.project
end
if u.lang then
u.interwiki = u.interwiki .. ':' .. u.lang
end
u.interwiki = u.interwiki .. ':'
else
u.interwiki = ''
end
 
-- UsernameGenerate the other helper strings.
u.usernameHtml = mw.uri.encode(u.username) -- Html-encoded username. Spaces are encoded as pluses.
end
 
Line 192 ⟶ 248:
local linktype = args[1]
if not linktype then
return err('no link type specified', 'UserLinks transclusions with invalid link types')
end
local result = getLink(linktype)
Line 241 ⟶ 297:
end
end
demo = args.demo -- Set the demo variable.
-- IfGenerate the usernameuser isdata absentstrings or blank,and return an error and a trackingany categoryerrors.
local dataStringError = generateUserDataStrings(args)
if args.user == '' or (not args.user and (not args.User or args.User == '')) then
if dataStringError then
return err('no username detected', 'UserLinks transclusions without usernames')
return dataStringError
end
-- Generate the user data strings.
generateUserDataStrings(args)
return func(args)
end
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu