Module:Titlelib: Difference between revisions

From Wikimedia Foundation Governance Wiki
Content deleted Content added
Pols12 (talk | contribs)
m oops
Pols12 (talk | contribs)
handle interlang prefixes such as they should ever come after interwiki prefixes (and both should be unique)
Line 5: Line 5:


I.e. wether the prefix is registered in mw.site.interwikiMap()
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
@return true if str is a registered interwiki prefix, false else
]]
]]
function p.isInterwiki(str)
function p.isInterwiki(str, onlyInterlang)
if not onlyInterlang then onlyInterlang = false end
str = mw.ustring.lower(str) --prefixes are stored as lower case
str = mw.ustring.lower(str) --prefixes are stored as lower case
for prefix, details in pairs(mw.site.interwikiMap()) do
for prefix, details in pairs(mw.site.interwikiMap()) do
if str == prefix then
if str == prefix then
return not onlyInterlang or mw.language.isKnownLanguageTag(str)
return true
end
end
end
end
return false
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
end


Line 42: Line 59:


--[[ Split given title into two parts: interwiki prefixes and page title
--[[ Split given title into two parts: interwiki prefixes and page title
@Returns table containing 2 strings:
@Return table containing 2 strings:
* first one is all prefixes which are recognized as interwikis, joined
* first one is all prefixes which are recognized as interwikis, joined
with colon
with colon
Line 58: Line 75:
local hasGotLastInterwikiPrefix = false
local hasGotLastInterwikiPrefix = false
local hasInterwikiPrefix = false
for i, linkPart in ipairs(linkTable) do
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
if linkPart == '' then
elseif hasGotLastInterwikiPrefix or p.isNamespace(linkPart) then
elseif hasGotLastInterwikiPrefix or p.isNamespace(linkPart) then
hasGotLastInterwikiPrefix = true
hasGotLastInterwikiPrefix = true
inwikiPart = inwikiPart .. ':' .. linkPart
inwikiPart = inwikiPart .. ':' .. linkPart
elseif p.isInterwiki(linkPart) then
elseif p.isInterlangPrefix(linkPart) then
outwikiPart = outwikiPart .. ':' .. linkPart
hasGotLastInterwikiPrefix = true
elseif not hasInterwikiPrefix and p.isInterwiki(linkPart) then
outwikiPart = outwikiPart .. ':' .. linkPart
outwikiPart = outwikiPart .. ':' .. linkPart
hasInterwikiPrefix = true
else
else
hasGotLastInterwikiPrefix = true
hasGotLastInterwikiPrefix = true

Revision as of 01:47, 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