Jump to content
Toggle menu
  • 8 articles
  • 75 files
  • 7 users
  • 37.8K edits
Neodyland Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Redirect: Difference between revisions

From Neodyland Wiki
mNo edit summary
resolve hard redirects (pages starting by '#REDIRECT') faster and less costlier, using titleObj:redirectTarget, without loading the page content with obj:getContent and parsing it (needed for soft redirects only)
Line 14: Line 14:
end
end


local function fmtTitle(page, fulltext, ck)
--[[
if page then
Detect hard redirect in wiki page content (not used inside this module)
local t = getTitle(page)
]]
if t and (not ck or ck and t.exists) then
function p.getTargetFromText(text)
return fulltext and t.fullText or t.prefixedText
text = text or ''
end
return text:match('#[Rr][Ee][Dd]...[Cc][Tt][%s:]*%[%[([^]|]-)%s*%]%]')
end
end
end


--[[
Detect soft redirect in wiki page content of 'Category:' pages, using template
'{{Category redirect|target}}', or aliases: '{{Cat redirect|target}}',
'{{See cat|target}}', ...
The target may be prefixed by 'Category:' or not (implied).
]]
function p.getTargetFromCatRedirect(text)
function p.getTargetFromCatRedirect(text)
local r = text:match("{{%s*[Cc]at%a*%s?redirect%s*|[%s:]*([^}|]-)[}|]") or
text = text or ''
  text:match(       "{{%s*[Ss]ee%s?cat%s*|[%s:]*([^}|]-)[}|]")
local r = text:match('{{%s*[Cc]at%a*%s?redirect%s*|[%s:]*([^}|]-)%s*[}|]') or
return r and (r:match("^.ategory:") and r or "Category:"..r)
text:match('{{%s*[Ss]ee%s?cat%s*|[%s:]*([^}|]-)%s*[}|]')
end
return r and (r:match('^[Cc]ategory%s*:') and r or 'Category:' .. r)
 
function p.getTargetFromText(text)
return text:match("#[Rr][Ee][Dd]...[Cc][Tt][%s:]*%[%[([^]|]+)")
end
end


-- Get page name that a redirect leads to, or nil if it isn't a redirect.
-- Get page name that a redirect leads to, or nil if it isn't a redirect.
function p.getTargetFrom(obj)
function p.getTargetFrom(obj)
local text = obj:getContent() or ""
--[[
local r = p.getTargetFromText(text)
obj:getContent() is intensive for large pages, but obj.redirectTarget returns directly
 
the target without needing to load and parse the text content of the wiki page.
This works for wiki pages that are hard redirects (content starting by '#REDIRECT').
In this case, using p.getTargetFromText(obj:getContent()) is not needed.
]]
local r = obj.isRedirect and obj.redirectTarget
if not r then
if not r then
if obj:inNamespace("Category") then
--[[
r = p.getTargetFromCatRedirect(text)
For pages using soft redirects (like categories), we must detect the template
used in the page content itself (this may be costly in memory and slow if not
loaded in the cache of page contents).
]]
if obj:inNamespace('Category') then
r = p.getTargetFromCatRedirect(obj:getContent())
end
end
if not r and obj.isRedirect then
if not r and obj.isRedirect then
Line 51: Line 62:
end
end
end
end
return r
return r
end
local function fmtTitle(target, fulltext, ensureTitleExists)
local titleObj
if type(target) == 'string' or type(page) == 'number' then
titleObj = getTitle(target)
elseif type(target) == 'table' and type(target.getContent) == 'function' then
titleObj = target
else
error(string.format(
"bad argument #1 to 'fmtTitle'"
.. " (string, number, or title object expected, got %s)",
type(target)
), 2)
end
if titleObj then
if not ensureTitleExists or ensureTitleExists and titleObj.exists then
return fulltext and titleObj.fullText or titleObj.prefixedText
end
end
end
end


-- Gets the target of a redirect. If the page specified is not a redirect,
-- Gets the target of a redirect. If the page specified is not a redirect,
-- returns nil.
-- returns nil.
function p.getTarget(page, fulltext, ck)
function p.getTarget(page, rname, fulltext, ensureTitleExists)
-- Get the title object. Both page names and title objects are allowed
-- Get the title object. Both page names and title objects are allowed
-- as input.
-- as input.
Line 77: Line 107:
then
then
-- Find the target by using string matching on the page content.
-- Find the target by using string matching on the page content.
return fmtTitle(p.getTargetFrom(titleObj), fulltext, ck)
return fmtTitle(p.getTargetFrom(titleObj), fulltext, ensureTitleExists)
end
end
end
end
Line 94: Line 124:
return nil
return nil
end
end
bracket = bracket and "[[%s]]" or "%s"
rname = rname:match("%[%[%s*([%]|]-)%s*%]%]") or rname
rname = rname:match("%[%[(.*)%]%]") or rname
local ret = p.getTarget(rname, fulltext, ensureTitleExists) or
local ret = p.getTarget(rname, fulltext, ensureTitleExists) or
fmtTitle(rname, fulltext, ensureTitleExists)
fmtTitle(rname, fulltext, ensureTitleExists)
link = bracket and (ret == rname and "[[:%s]]" or "[[:%s|%s]]") or "%s"
return ret and bracket:format(ret)
return ret and link:format(ret, rname)
end
end


local function use(x)
-- proxy to load the module lazily, replaced by the actual loaded function
return x and #x > 0 and ("true yes 1"):find(x, 1, true) or nil
local getArgs = function(...)
getArgs = require('Module:Arguments').getArgs
return getArgs(...)
end
end


local function _main(frame, ck, alt)
local function use(x)
local args = require('Module:Arguments').getArgs(frame, {frameOnly = true})
return x and (x == '1' or x == 't' or x == 'true' or x == 'y' or x == 'yes') or nil
ck = ck or use(args.noredlinks)
alt = alt and args[2] or ''
return p.luaMain(args[1], use(args.bracket), use(args.fulltext), ck) or alt
end
end


-- Provides access to the luaMain function from wikitext, may return redlinks,
-- Provides access to the luaMain function from wikitext, may return redlinks,
-- albeit page titles that do not exist in the wiki. ck and alt are optional
-- albeit page titles that do not exist in the wiki.
-- Parameters alt, bracket, fulltext, ensureTitleExists are optional
function p.main(frame)
function p.main(frame)
return _main(frame)
local args = getArgs(frame, {frameOnly = true})
local rname = args[1]
local alt = args[2] or ''
local bracket = use(args.bracket)
local fulltext = use(args.fulltext)
local ensureTitleExists = use(args.noredlinks)
 
return p.luaMain(rname, bracket, fulltext, ensureTitleExists) or alt
end
end


-- main variant, returns empty string if a redlink would be returned otherwise
-- main variant, returns empty string if a redlink would be returned otherwise.
-- '|noredlinks=true' by default.
function p.mainE(frame)
function p.mainE(frame)
return _main(frame, true)
local args = getArgs(frame, {frameOnly = true})
local rname = args[1]
local alt = args[2] or ''
local bracket = use(args.bracket)
local fulltext = use(args.fulltext)
 
return p.luaMain(rname, bracket, fulltext, true) or alt
end
end


-- main variant, returns args[2] if a redlink would be returned otherwise
-- main variant, returns args[2] if a redlink would be returned otherwise.
-- '|bracket=true|noredlinks=true' by default
function p.mainA(frame)
function p.mainA(frame)
return _main(frame, true, true)
local args = getArgs(frame, {frameOnly = true})
local rname = args[1]
local alt = args[2] or ''
local bracket = use(args.bracket)
 
return p.luaMain(rname, bracket, true, true) or alt
end
end



Revision as of 03:07, 21 July 2020

Module documentation[ view · edit · history · purge ]
This documentation is transcluded from Module:Redirect/doc.

This module implements templates that detect and resolve redirects and soft category redirects.

Soft redirects are hard to detect and costly as they require loading and parsing the wiki page content of 'Category:' pages.

Soft redirects are category pages containing the template

or one of its known aliases on Commons (there are too many!!!): Template:Columns For the full list of aliases (simple redirections) of {{Category redirect}}, see

https://commons.wikimedia.org/w/index.php?title=Special:WhatLinksHere/Template:Category_redirect&hidetrans=1&hidelinks=1

For the full list of derived templates (specifying an embedded reason parameter passed to the main template), see

https://commons.wikimedia.org/w/index.php?title=Special:WhatLinksHere/Template:Category_redirect&namespace=10&hidelinks=1&hideredirs=1

The 'target' parameter value for these templates may be prefixed by 'Category:' or ':Category:' or not (it is implied).

There should exist only one soft-redirect or redirection per page. If you need several redirections (because the original name was ambiguous and could refer to several distinct topics), this means that this redirection page has to be converted into a disambiguation page, providing a list of links to the different possible targets (each linked target page may be possibly followed by a short descriptive comment without any link, in order to help users making the right choice).

-- This module provides functions for getting the target of a redirect page.

local p = {}

-- Gets a mw.title object, using pcall to avoid generating script errors if we
-- are over the expensive function count limit (among other possible causes).
local function getTitle(...)
	local success, titleObj = pcall(mw.title.new, ...)
	if success then
		return titleObj
	else
		return nil
	end
end

--[[
Detect hard redirect in wiki page content (not used inside this module)
]]
function p.getTargetFromText(text)
	text = text or ''
	return text:match('#[Rr][Ee][Dd]...[Cc][Tt][%s:]*%[%[([^]|]-)%s*%]%]')
end

--[[
Detect soft redirect in wiki page content of 'Category:' pages, using template
'{{Category redirect|target}}', or aliases: '{{Cat redirect|target}}',
'{{See cat|target}}', ...
The target may be prefixed by 'Category:' or not (implied).
]]
function p.getTargetFromCatRedirect(text)
	text = text or ''
	local r = text:match('{{%s*[Cc]at%a*%s?redirect%s*|[%s:]*([^}|]-)%s*[}|]') or
		text:match('{{%s*[Ss]ee%s?cat%s*|[%s:]*([^}|]-)%s*[}|]')
	return r and (r:match('^[Cc]ategory%s*:') and r or 'Category:' .. r)
end

-- Get page name that a redirect leads to, or nil if it isn't a redirect.
function p.getTargetFrom(obj)
	--[[
	obj:getContent() is intensive for large pages, but obj.redirectTarget returns directly
	the target without needing to load and parse the text content of the wiki page.
	This works for wiki pages that are hard redirects (content starting by '#REDIRECT').
	In this case, using p.getTargetFromText(obj:getContent()) is not needed.
	]]
	local r = obj.isRedirect and obj.redirectTarget
	if not r then
		--[[
		For pages using soft redirects (like categories), we must detect the template
		used in the page content itself (this may be costly in memory and slow if not
		loaded in the cache of page contents).
		]]
		if obj:inNamespace('Category') then
			r = p.getTargetFromCatRedirect(obj:getContent())
		end
		if not r and obj.isRedirect then
			-- The page is a redirect, but matching failed, which may be a bug
			-- in redirect matching pattern, so throw an error.
			error(string.format(
				'could not parse redirect on page "%s"',
				fulltext and obj.fullText or obj.prefixedText
			))
		end
	end
	return r
end

local function fmtTitle(target, fulltext, ensureTitleExists)
	local titleObj
	if type(target) == 'string' or type(page) == 'number' then
		titleObj = getTitle(target)
	elseif type(target) == 'table' and type(target.getContent) == 'function' then
		titleObj = target
	else
		error(string.format(
			"bad argument #1 to 'fmtTitle'"
				.. " (string, number, or title object expected, got %s)",
			type(target)
		), 2)
	end
	if titleObj then
		if not ensureTitleExists or ensureTitleExists and titleObj.exists then
			return fulltext and titleObj.fullText or titleObj.prefixedText
		end
	end
end

-- Gets the target of a redirect. If the page specified is not a redirect,
-- returns nil.
function p.getTarget(page, rname, fulltext, ensureTitleExists)
	-- Get the title object. Both page names and title objects are allowed
	-- as input.
	local titleObj
	if type(page) == 'string' or type(page) == 'number' then
		titleObj = getTitle(page)
	elseif type(page) == 'table' and type(page.getContent) == 'function' then
		titleObj = page
	else
		error(string.format(
			"bad argument #1 to 'getTarget'"
				.. " (string, number, or title object expected, got %s)",
			type(page)
		), 2)
	end
	
	if titleObj and titleObj.exists and
		(titleObj.isRedirect or titleObj:inNamespace("Category"))
	then
		-- Find the target by using string matching on the page content.
		return fmtTitle(p.getTargetFrom(titleObj), fulltext, ensureTitleExists)
	end
end

--[[
-- Given a single page name determines what page it redirects to and returns the
-- target page name, or the passed page name when not a redirect. The passed
-- page name can be given as plain text or as a page link.
-- 
-- Returns page name as plain text, or when the bracket parameter is given, as a
-- page link. Returns an error message when page does not exist or the redirect
-- target cannot be determined for some reason.
--]]
function p.luaMain(rname, bracket, fulltext, ensureTitleExists)
	if type(rname) ~= "string" or not rname:find("%S") then
		return nil
	end
	rname = rname:match("%[%[%s*([%]|]-)%s*%]%]") or rname
	local ret = p.getTarget(rname, fulltext, ensureTitleExists) or
		fmtTitle(rname, fulltext, ensureTitleExists)
	link = bracket and (ret == rname and "[[:%s]]" or "[[:%s|%s]]") or "%s"
	return ret and link:format(ret, rname)
end

-- proxy to load the module lazily, replaced by the actual loaded function
local getArgs = function(...)
	getArgs = require('Module:Arguments').getArgs
	return getArgs(...)
end

local function use(x)
	return x and (x == '1' or x == 't' or x == 'true' or x == 'y' or x == 'yes') or nil
end

-- Provides access to the luaMain function from wikitext, may return redlinks,
-- albeit page titles that do not exist in the wiki.
-- Parameters alt, bracket, fulltext, ensureTitleExists are optional
function p.main(frame)
	local args = getArgs(frame, {frameOnly = true})
	local rname = args[1]
	local alt = args[2] or ''
	local bracket = use(args.bracket)
	local fulltext = use(args.fulltext)
	local ensureTitleExists = use(args.noredlinks)

	return p.luaMain(rname, bracket, fulltext, ensureTitleExists) or alt
end

-- main variant, returns empty string if a redlink would be returned otherwise.
-- '|noredlinks=true' by default.
function p.mainE(frame)
	local args = getArgs(frame, {frameOnly = true})
	local rname = args[1]
	local alt = args[2] or ''
	local bracket = use(args.bracket)
	local fulltext = use(args.fulltext)

	return p.luaMain(rname, bracket, fulltext, true) or alt
end

-- main variant, returns args[2] if a redlink would be returned otherwise.
-- '|bracket=true|noredlinks=true' by default
function p.mainA(frame)
	local args = getArgs(frame, {frameOnly = true})
	local rname = args[1]
	local alt = args[2] or ''
	local bracket = use(args.bracket)

	return p.luaMain(rname, bracket, true, true) or alt
end

-- Returns true if the specified page is a redirect, and false otherwise.
function p.luaIsRedirect(page)
	local titleObj = getTitle(page)
	if not titleObj then
		return false
	end
	if titleObj.isRedirect then
		return true
	else
		return false
	end
end

-- Provides access to the luaIsRedirect function from wikitext, returning 'yes'
-- if the specified page is a redirect, and the blank string otherwise.
function p.isRedirect(frame)
	local args = require('Module:Arguments').getArgs(frame, {frameOnly = true})
	if p.luaIsRedirect(args[1]) then
		return 'yes'
	else
		return ''
	end
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.