Module:UserLinks: Difference between revisions
From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Content added Content deleted
(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: | Line 1: | ||
local ToolbarBuilder = require('Module:Toolbar') |
local ToolbarBuilder = require('Module:Toolbar') |
||
local interwikiTable = mw.loadData("Module:InterwikiTable") |
|||
local u = {} -- Table for user-data helper strings. |
local u = {} -- Table for user-data helper strings. |
||
local demo |
local demo |
||
-- Define a custom error message for this module. |
-- Define a custom error message for this module. |
||
local function err(msg |
local function err(msg) |
||
⚫ | |||
⚫ | |||
cat = '[[Category:' .. cat .. ']]' |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end |
end |
||
---------------------------------------------------------------------------------------------- |
|||
-- Below are the helper strings available for writing user link functions. |
-- Below are the helper strings available for writing user link functions. -- |
||
-- |
-- -- |
||
-- |
-- u.username The plain username. If the username is not present then the -- |
||
-- |
-- module returns an error. -- |
||
⚫ | |||
⚫ | |||
-- |
-- u.project The project name. Nil if not specified. -- |
||
-- u. |
-- u.lang The language code. Nil if not specified. -- |
||
-- u. |
-- u.interwiki The interwiki prefix, consisting of the project and language -- |
||
-- |
-- values, separated by colons, e.g. ":wikt:es:". If no project -- |
||
-- |
-- or language values are found, this is the blank string, "". -- |
||
⚫ | |||
-- 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 -- |
||
-- |
-- function below. -- |
||
⚫ | |||
-- function below. -- |
|||
⚫ | |||
local function makeUserLink() |
local function makeUserLink() |
||
return '[[' .. u. |
return '[[' .. u.interwiki .. 'User:' .. u.username .. '|' .. u.username .. ']]' |
||
end |
end |
||
local function makeTalkLink() |
local function makeTalkLink() |
||
return '[[' .. u. |
return '[[' .. u.interwiki .. 'User talk:' .. u.username .. '|talk]]' |
||
end |
end |
||
local function makeContribsLink() |
local function makeContribsLink() |
||
return '[[' .. u. |
return '[[' .. u.interwiki .. 'Special:Contributions/' .. u.username .. '|contribs]]' |
||
end |
end |
||
Line 47: | Line 43: | ||
local function makeMovesLink() |
local function makeMovesLink() |
||
return '[[' .. u. |
return '[[' .. u.interwiki .. 'Special:Log/move/' .. u.username .. '|page moves]]' |
||
end |
end |
||
local function makeLogsLink() |
local function makeLogsLink() |
||
return '[[' .. u. |
return '[[' .. u.interwiki .. 'Special:Log/' .. u.username .. '|logs]]' |
||
end |
end |
||
Line 60: | Line 56: | ||
local function makeBlocksLink() |
local function makeBlocksLink() |
||
return '[[' .. u. |
return '[[' .. u.interwiki .. 'Special:Log/block/' .. u.username .. '|blocks]]' |
||
end |
end |
||
local function makeBlockUserLink() |
local function makeBlockUserLink() |
||
return '[[' .. u. |
return '[[' .. u.interwiki .. 'Special:Block/' .. u.username .. '|block user]]' |
||
end |
end |
||
local function makeCentralAuthLink() |
local function makeCentralAuthLink() |
||
return '[[' .. u. |
return '[[' .. u.interwiki .. 'Special:CentralAuth/' .. u.username .. '|central auth]]' |
||
end |
end |
||
local function makeDeletedContribsLink() |
local function makeDeletedContribsLink() |
||
return '[[' .. u. |
return '[[' .. u.interwiki .. 'Special:DeletedContributions/' .. u.username .. '|deleted contribs]]' |
||
end |
end |
||
local function makeEmailLink() |
local function makeEmailLink() |
||
return '[[' .. u. |
return '[[' .. u.interwiki .. 'Special:Emailuser/' .. u.username .. '|email]]' |
||
end |
end |
||
Line 84: | Line 80: | ||
local function makeDeletionsLink() |
local function makeDeletionsLink() |
||
return '[[' .. u. |
return '[[' .. u.interwiki .. 'Special:Log/delete/' .. u.username .. '|deletions]]' |
||
end |
end |
||
Line 107: | Line 103: | ||
local function makeProtectionsLink() |
local function makeProtectionsLink() |
||
return '[[' .. u. |
return '[[' .. u.interwiki .. 'Special:Log/protect/' .. u.username .. '|protections]]' |
||
end |
end |
||
local function makeRightsLink() |
local function makeRightsLink() |
||
return '[[' .. u. |
return '[[' .. u.interwiki .. 'Special:Log/rights/' .. u.username .. '|rights]]' |
||
end |
end |
||
local function makeRenamesLink() |
local function makeRenamesLink() |
||
return '[[' .. u. |
return '[[' .. u.interwiki .. 'Special:Log/renameuser/' .. u.username .. '|renames]]' |
||
end |
end |
||
Line 147: | Line 143: | ||
} |
} |
||
if not linktypes[linktype] then |
if not linktypes[linktype] then |
||
return err('"' .. linktype .. '" is not a valid link code |
return err('"' .. linktype .. '" is not a valid link code') |
||
end |
end |
||
return linktypes[linktype]() |
return linktypes[linktype]() |
||
Line 169: | Line 165: | ||
return ToolbarBuilder.main(targs) |
return ToolbarBuilder.main(targs) |
||
end |
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 |
|||
⚫ | |||
return false |
|||
end |
end |
||
local function generateUserDataStrings(args) |
local function generateUserDataStrings(args) |
||
-- If the username is absent or blank, return an error and a tracking category. |
|||
⚫ | |||
if args.user == '' or (not args.user and (not args.User or args.User == '')) then |
|||
⚫ | |||
⚫ | |||
⚫ | |||
-- Project helper strings. |
|||
u.username = args.user or args.User |
|||
end |
|||
⚫ | |||
u.project = args.Project or args.project |
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 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 |
end |
||
Line 192: | Line 248: | ||
local linktype = args[1] |
local linktype = args[1] |
||
if not linktype then |
if not linktype then |
||
return err('no link type specified |
return err('no link type specified') |
||
end |
end |
||
local result = getLink(linktype) |
local result = getLink(linktype) |
||
Line 241: | Line 297: | ||
end |
end |
||
end |
end |
||
demo = args.demo -- Set the demo variable. |
demo = args.demo -- Set the demo variable. |
||
-- |
-- Generate the user data strings and return any errors. |
||
⚫ | |||
if args.user == '' or (not args.user and (not args.User or args.User == '')) then |
|||
if dataStringError then |
|||
⚫ | |||
return dataStringError |
|||
end |
end |
||
⚫ | |||
⚫ | |||
return func(args) |
return func(args) |
||
end |
end |