Module:Titlelib
Documentation for this module may be created at Module:Titlelib/doc
-- == Titlelib.lua == --
--[[ Checks wether the given String is a valid interwiki prefix.
I.e. wether the prefix is registered in mw.site.interwikiMap()
@return true if str is a registered interwiki prefix, false else
]]
function p.isInterwiki(str)
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 true
end
end
return false
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
@Returns 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
for i, linkPart in ipairs(linkTable) do
--s’arrêter si c'est le dernier morceau
--if i == #linkTable then
-- break
--end
if linkPart == '' then
elseif hasGotLastInterwikiPrefix or p.isNamespace(linkPart) then
hasGotLastInterwikiPrefix = true
inwikiPart = inwikiPart .. ':' .. linkPart
elseif p.isInterwiki(linkPart) then
outwikiPart = outwikiPart .. ':' .. linkPart
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