Module:Titlelib: Difference between revisions
handle interlang prefixes such as they should ever come after interwiki prefixes (and both should be unique) |
MusikBot II (talk | contribs) m Protected "Module:Titlelib": High-risk template or module (more info) ([Edit=Allow only autoconfirmed users] (indefinite)) |
||
(No difference)
|
Revision as of 18:04, 23 February 2020
Documentation for this module may be created at Module:Titlelib/doc
-- == Titlelib.lua == --
local p = {}
--[[ Checks wether the given String is a valid interwiki prefix.
I.e. wether the prefix is registered in mw.site.interwikiMap()
@param onlyInterlang bool Wether to look only for interlang wiki prefix.
False by default.
@return true if str is a registered interwiki prefix, false else
]]
function p.isInterwiki(str, onlyInterlang)
if not onlyInterlang then onlyInterlang = false end
str = mw.ustring.lower(str) --prefixes are stored as lower case
for prefix, details in pairs(mw.site.interwikiMap()) do
if str == prefix then
return not onlyInterlang or mw.language.isKnownLanguageTag(str)
end
end
return false
end
--[[ Checks wether the given String is a valid interlang wiki prefix.
I.e. wether the prefix is registered in mw.site.interwikiMap() and is a
known language code.
@param str String the prefix to check for.
@return true if str is a registered interlang prefix, false else.
]]
function p.isInterlangPrefix(str)
return p.isInterwiki(str, true)
end
--[[ Checks wether the given String is a valid namespace prefix on Meta.
I.e. wether the prefix is registered in mw.site.namespaces
@return true if str is a registered interwiki prefix, false else
]]
function p.isNamespace(str)
str = mw.ustring.lower(str)
for i, details in pairs(mw.site.namespaces) do
if str == mw.ustring.lower(details.name)
or str == mw.ustring.lower(details.canonicalName)
or (details.aliases[1] and str == mw.ustring.lower(details.aliases[1]))
--on Meta there is no more than 1 alias; else we should use
--table.contains(aliases, str)
then
return true
end
end
return false
end
--[[ Split given title into two parts: interwiki prefixes and page title
@Return table containing 2 strings:
* first one is all prefixes which are recognized as interwikis, joined
with colon
* second one is the rest of the given string
]]
function p.splitPrefixedTitle(linkStr)
if not mw.ustring.find(linkStr, ':') then --if there is no colon in title
return ':', linkStr --that means there is no prefix
end
local linkTable = mw.text.split(linkStr, ':', true)
local outwikiPart = '' --interwiki prefixes and language code
local inwikiPart = '' --page name, including namespace prefix
local hasGotLastInterwikiPrefix = false
local hasInterwikiPrefix = false
for i, linkPart in ipairs(linkTable) do
if linkPart == '' then
elseif hasGotLastInterwikiPrefix or p.isNamespace(linkPart) then
hasGotLastInterwikiPrefix = true
inwikiPart = inwikiPart .. ':' .. linkPart
elseif p.isInterlangPrefix(linkPart) then
outwikiPart = outwikiPart .. ':' .. linkPart
hasGotLastInterwikiPrefix = true
elseif not hasInterwikiPrefix and p.isInterwiki(linkPart) then
outwikiPart = outwikiPart .. ':' .. linkPart
hasInterwikiPrefix = true
else
hasGotLastInterwikiPrefix = true
inwikiPart = linkPart
end
end
return outwikiPart .. ':', inwikiPart
end
--[[ Prefixes page name with Special:MyLanguage.
@param pageLink String Page name, eventually prefixed with
interwiki prefixes and namespace.
@return String Name of page which links to given page link
in user language.
]]
function p.myLangLink(pageLink)
local outPrefix, pageName = p.splitPrefixedTitle(pageLink)
return outPrefix .. 'Special:MyLanguage/' .. pageName
end
return p