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
No edit summary
Line 11: Line 11:
else
else
return nil
return nil
end
end
local function fmtTitle(page, fulltext, ck)
if page then
local t = getTitle(page)
if t and (not ck or ck and t.exists) then
return fulltext and t.fullText or t.prefixedText
end
end
end
end
end


function p.getTargetFromCatRedirect(text)
function p.getTargetFromCatRedirect(text)
local r = text:match(
local r = text:match("{{%s*[Cc]at%a*%s?redirect%s*|[%s:]*([^}|]-)[}|]") or
"[Cc][Aa][Tt]%a*%s?[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*|%s*:?([^}|]-)[}|]"
  text:match(        "{{%s*[Ss]ee%s?cat%s*|[%s:]*([^}|]-)[}|]")
)
return r and (r:match("^.ategory:") and r or "Category:"..r)
return r and "Category:" .. r:gsub("[Cc]ategory:", "")
end
end


-- Gets the name of a page that a redirect leads to, or nil if it isn't a
-- redirect.
function p.getTargetFromText(text)
function p.getTargetFromText(text)
return string.match(
return text:match("#[Rr][Ee][Dd]...[Cc][Tt][%s:]*%[%[([^]|]+)")
text,
end
"^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]|]-)%]%]"
 
) or string.match(
-- Get page name that a redirect leads to, or nil if it isn't a redirect.
text,
function p.getTargetFrom(obj)
"^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]|]-)|[^%[%]]-%]%]"
local text = obj:getContent() or ""
)
local r = p.getTargetFromText(text)
 
if not r then
if obj:inNamespace("Category") then
r = p.getTargetFromCatRedirect(text)
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
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)
function p.getTarget(page, fulltext, ck)
-- 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 50: Line 72:
), 2)
), 2)
end
end
if not titleObj or not titleObj.exists or (not titleObj.isRedirect
and not titleObj:inNamespace("Category")) then
return nil
end
-- Find the target by using string matching on the page content.
local target = p.getTargetFromText(titleObj:getContent() or "")
if not target and titleObj:inNamespace("Category") then
if titleObj and titleObj.exists and
-- try to find target of a cat redirect
(titleObj.isRedirect or titleObj:inNamespace("Category"))
target = p.getTargetFromCatRedirect(titleObj:getContent() or "")
then
if not target then
-- Find the target by using string matching on the page content.
return nil
return fmtTitle(p.getTargetFrom(titleObj), fulltext, ck)
end
end
if target then
local targetTitle = getTitle(target)
if targetTitle then
if fulltext then
return targetTitle.fullText
else
return targetTitle.prefixedText
end
else
return nil
end
else
-- The page is a redirect, but matching failed. This indicates a bug in
-- the redirect matching pattern, so throw an error.
error(string.format(
'could not parse redirect on page "%s"',
fulltext and titleObj.fullText or titleObj.prefixedText
))
end
end
end
end
Line 101: Line 95:
end
end
bracket = bracket and "[[%s]]" or "%s"
bracket = bracket and "[[%s]]" or "%s"
rname = rname:match("%[%[(.+)%]%]") or rname
rname = rname:match("%[%[(.*)%]%]") or rname
local target = p.getTarget(rname, fulltext)
local ret = p.getTarget(rname, fulltext, ensureTitleExists) or
local ret = target or rname
fmtTitle(rname, fulltext, ensureTitleExists)
ret = getTitle(ret)
if ret then
return ret and bracket:format(ret)
if ensureTitleExists and not ret.exists then
return nil
end
if fulltext then
ret = ret.fullText
else
ret = ret.prefixedText
end
return bracket:format(ret)
else
return nil
end
end
end



Revision as of 10:40, 10 January 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

local function fmtTitle(page, fulltext, ck)
	if page then
		local t = getTitle(page)
		if t and (not ck or ck and t.exists) then
			return fulltext and t.fullText or t.prefixedText
		end
	end
end

function p.getTargetFromCatRedirect(text)
	local r = text:match("{{%s*[Cc]at%a*%s?redirect%s*|[%s:]*([^}|]-)[}|]") or
			  text:match(        "{{%s*[Ss]ee%s?cat%s*|[%s:]*([^}|]-)[}|]")
	return r and (r:match("^.ategory:") and r or "Category:"..r)
end

function p.getTargetFromText(text)
	return text:match("#[Rr][Ee][Dd]...[Cc][Tt][%s:]*%[%[([^]|]+)")
end

-- Get page name that a redirect leads to, or nil if it isn't a redirect.
function p.getTargetFrom(obj)
	local text = obj:getContent() or ""
	local r = p.getTargetFromText(text)

	if not r then
		if obj:inNamespace("Category") then
			r = p.getTargetFromCatRedirect(text)
		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

-- Gets the target of a redirect. If the page specified is not a redirect,
-- returns nil.
function p.getTarget(page, fulltext, ck)
	-- 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, ck)
	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
	bracket = bracket and "[[%s]]" or "%s"
	rname = rname:match("%[%[(.*)%]%]") or rname
	local ret = p.getTarget(rname, fulltext, ensureTitleExists) or
				fmtTitle(rname, fulltext, ensureTitleExists)
	
	return ret and bracket:format(ret)
end

-- Provides access to the luaMain function from wikitext, may return redlinks,
-- albeit page titles that do not exist in the wiki.
function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {frameOnly = true})
	return p.luaMain(args[1], args.bracket, args.fulltext) or ''
end

-- main variant, returns empty string if a redlink would be returned otherwise
function p.mainE(frame)
	local args = require('Module:Arguments').getArgs(frame, {frameOnly = true})
	return p.luaMain(args[1], args.bracket, args.fulltext, true) or ''
end

-- main variant, returns args[2] if a redlink would be returned otherwise
function p.mainA(frame)
	local args = require('Module:Arguments').getArgs(frame, {frameOnly = true})
	return p.luaMain(args[1], args.bracket, args.fulltext, true) or args[2]
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.