Anonymous user
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
return '<span class="error">[[Module:UserLinks]] error: ' .. msg .. '.</span>
if cat and not demo then▼
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.
--
--
--
-- u.usernameHtml
-- helper strings. --▼
-- u.project The project name. Nil if not
-- u.
-- u.
-- values, separated by colons, e.g. ":wikt:es:". If no
--
-- If you want more strings, you can define them in the
--
▲--------------------------------------------------------------------------------------------------
local function makeUserLink()
return '[[' .. u.
end
local function makeTalkLink()
return '[[' .. u.
end
local function makeContribsLink()
return '[[' .. u.
end
Line 47 ⟶ 43:
local function makeMovesLink()
return '[[' .. u.
end
local function makeLogsLink()
return '[[' .. u.
end
Line 60 ⟶ 56:
local function makeBlocksLink()
return '[[' .. u.
end
local function makeBlockUserLink()
return '[[' .. u.
end
local function makeCentralAuthLink()
return '[[' .. u.
end
local function makeDeletedContribsLink()
return '[[' .. u.
end
local function makeEmailLink()
return '[[' .. u.
end
Line 84 ⟶ 80:
local function makeDeletionsLink()
return '[[' .. u.
end
Line 107 ⟶ 103:
local function makeProtectionsLink()
return '[[' .. u.
end
local function makeRightsLink()
return '[[' .. u.
end
local function makeRenamesLink()
return '[[' .. u.
end
Line 147 ⟶ 143:
}
if not linktypes[linktype] then
return err('"' .. linktype .. '" is not a valid link code
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.▼
▲ u.usernameHtml = mw.uri.encode(u.username) -- Html-encoded username. Spaces are encoded as pluses.
▲ else
u.username = args.user or args.User
end
u.project = args.Project or args.project
u.lang = args.lang or args.Lang
-- Process the project value if it is present.
-- 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
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
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
end
local result = getLink(linktype)
Line 241 ⟶ 297:
end
end
demo = args.demo -- Set the demo variable.
--
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
|