Module:UserLinks: Difference between revisions
From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Content added Content deleted
(re-add support for the demo parameter to the error function) |
(better tracking category handling, add check for the validity of the language prefix in project parameters, add interwiki support for more link types) |
||
Line 3: | Line 3: | ||
local u = {} -- Table for user-data helper strings. |
local u = {} -- Table for user-data helper strings. |
||
local trackingCategories = {} -- Table for storing the tracking categories. |
|||
local demo |
local demo |
||
Line 56: | Line 57: | ||
local function makeBlockLogLink() |
local function makeBlockLogLink() |
||
local url = mw.uri.fullUrl('Special:Log/block', 'page=User:' .. u.usernameHtml) |
local url = mw.uri.fullUrl(u.interwiki .. 'Special:Log/block', 'page=User:' .. u.usernameHtml) |
||
return '[' .. tostring(url) .. ' block log]' |
return '[' .. tostring(url) .. ' block log]' |
||
end |
end |
||
Line 89: | Line 90: | ||
local function makeListUserLink() |
local function makeListUserLink() |
||
local url = mw.uri.fullUrl('Special:ListUsers', 'limit=1&username=' .. u.usernameHtml) |
local url = mw.uri.fullUrl(u.interwiki .. 'Special:ListUsers', 'limit=1&username=' .. u.usernameHtml) |
||
return '[' .. tostring(url) .. ' list user]' |
return '[' .. tostring(url) .. ' list user]' |
||
end |
end |
||
Line 98: | Line 99: | ||
local function makeTargetLogsLink() |
local function makeTargetLogsLink() |
||
local url = mw.uri.fullUrl('Special:Log', 'page=User:' .. u.usernameHtml) |
local url = mw.uri.fullUrl(u.interwiki .. 'Special:Log', 'page=User:' .. u.usernameHtml) |
||
return '[' .. tostring(url) .. ' target logs]' |
return '[' .. tostring(url) .. ' target logs]' |
||
end |
end |
||
local function makeEditFilterLogLink() |
local function makeEditFilterLogLink() |
||
local url = mw.uri.fullUrl('Special:AbuseLog', 'wpSearchUser=' .. u.usernameHtml) |
local url = mw.uri.fullUrl(u.interwiki .. 'Special:AbuseLog', 'wpSearchUser=' .. u.usernameHtml) |
||
return '[' .. tostring(url) .. ' edit filter log]' |
return '[' .. tostring(url) .. ' edit filter log]' |
||
end |
end |
||
Line 120: | Line 121: | ||
local function makeRfaLink() |
local function makeRfaLink() |
||
if u.project or u.lang then |
|||
⚫ | |||
end |
|||
return '[[Special:PrefixIndex/Wikipedia:Requests for adminship/' .. u.username .. '|RfA]]' |
return '[[Special:PrefixIndex/Wikipedia:Requests for adminship/' .. u.username .. '|RfA]]' |
||
end |
end |
||
Line 166: | Line 170: | ||
if numArgsExist == false then |
if numArgsExist == false then |
||
return -- Don't return a toolbar if no numeric arguments exist. |
return nil -- Don't return a toolbar if no numeric arguments exist. |
||
else |
else |
||
return ToolbarBuilder.main(targs) |
return ToolbarBuilder.main(targs) |
||
Line 172: | Line 176: | ||
end |
end |
||
-- This function finds whether a string is a valid interwiki project prefix. |
|||
-- If the string is valid, the function outputs two values: true, and the site code |
|||
-- used in [[Module:InterwikiTable]]. If the string is valid, the function outputs |
|||
-- false and nil. |
|||
local function isKnownProject(prefix) |
local function isKnownProject(prefix) |
||
for |
for projectCode, projectVal in pairs(interwikiTable) do |
||
for _, iwCode in ipairs(projectVal.iw_prefix) do |
for _, iwCode in ipairs(projectVal.iw_prefix) do |
||
if iwCode == prefix then |
if iwCode == prefix then |
||
return true |
return true, projectCode |
||
end |
end |
||
end |
end |
||
end |
end |
||
return false |
return false, nil |
||
end |
end |
||
Line 194: | Line 202: | ||
u.project = args.Project or args.project |
u.project = args.Project or args.project |
||
u.lang = args.lang or args.Lang |
u.lang = args.lang or args.Lang |
||
⚫ | |||
return err('"' .. u.lang .. '" is not a valid language code') |
|||
end |
|||
-- Process the project value if it is present. |
-- Process the project value if it is present. |
||
if u.project then |
if u.project then |
||
table.insert( trackingCategories, '[[Category:UserLinks transclusions with project parameters]]' ) |
|||
-- If u.project is a known project, we don't need to do anything. If the project isn't known, first |
-- 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 |
-- check whether it is a valid language code, and if not then see if it's an interwiki code |
||
Line 205: | Line 218: | ||
u.project = nil |
u.project = nil |
||
else |
else |
||
local pref1, pref2 = mw.ustring.match( u.project, '^(%w+):(%w+)$' ) |
|||
local pref1, pref2 = mw.ustring.match( u.project, '(%w+):(%w+)' ) |
|||
if pref1 and pref2 then |
if pref1 and pref2 then |
||
local pref1IsKnownProject, pref1ProjectCode = isKnownProject(pref1) |
|||
local pref2IsKnownProject, pref2ProjectCode = isKnownProject(pref2) |
|||
if pref1IsKnownProject |
|||
and mw.language.isKnownLanguageTag(pref2) |
|||
and interwikiTable[pref1ProjectCode].takes_lang_prefix then |
|||
u.project = pref1 |
u.project = pref1 |
||
u.lang = pref2 |
u.lang = pref2 |
||
table.insert( trackingCategories, '[[Category:UserLinks transclusions with project parameters containing language codes]]' ) |
|||
⚫ | |||
elseif pref2IsKnownProject |
|||
and mw.language.isKnownLanguageTag(pref1) |
|||
and interwikiTable[pref2ProjectCode].takes_lang_prefix then |
|||
u.project = pref2 |
u.project = pref2 |
||
u.lang = pref1 |
u.lang = pref1 |
||
table.insert( trackingCategories, '[[Category:UserLinks transclusions with project parameters containing language codes]]' ) |
|||
else |
else |
||
return err('"' .. u.project .. '" is not a valid interwiki prefix') |
return err('"' .. u.project .. '" is not a valid interwiki prefix') |
||
Line 242: | Line 262: | ||
end |
end |
||
local function generateTrackingCategories( |
local function generateTrackingCategories() |
||
if demo then |
|||
⚫ | |||
if (args.Project or args.project) and not demo then |
|||
else |
|||
⚫ | |||
return table.concat(trackingCategories) |
|||
end |
end |
||
⚫ | |||
end |
end |
||
Line 256: | Line 276: | ||
end |
end |
||
local result = getLink(linktype) |
local result = getLink(linktype) |
||
result = result .. generateTrackingCategories( |
result = result .. generateTrackingCategories() |
||
return result |
return result |
||
end |
end |
||
Line 272: | Line 292: | ||
end |
end |
||
result = '<span>' .. makeUserLink() .. result .. '</span>' |
result = '<span>' .. makeUserLink() .. result .. '</span>' |
||
result = result .. generateTrackingCategories( |
result = result .. generateTrackingCategories() |
||
return result |
return result |