Module:Redirect: Difference between revisions
More actions
No edit summary |
dedup |
||
| Line 100: | Line 100: | ||
return ret and bracket:format(ret) | return ret and bracket:format(ret) | ||
end | |||
local function _main(frame, ck, alt) | |||
local args = require('Module:Arguments').getArgs(frame, {frameOnly = true}) | |||
--ck = ck or args.noredlinks | |||
--alt = alt or args[2] | |||
return p.luaMain(args[1], args.bracket, args.fulltext, ck) or alt or '' | |||
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. | -- albeit page titles that do not exist in the wiki. ck and alt are optional | ||
function p.main(frame) | function p.main(frame) | ||
return _main(frame) | |||
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 | ||
function p.mainE(frame) | function p.mainE(frame) | ||
return _main(frame, true) | |||
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 | ||
function p.mainA(frame) | function p.mainA(frame) | ||
return _main(frame, true, args[2]) | |||
end | end | ||
Revision as of 11:05, 10 January 2020
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
- {{Category redirect|target|optional reason}}
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
For the full list of derived templates (specifying an embedded reason parameter passed to the main template), see
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
local function _main(frame, ck, alt)
local args = require('Module:Arguments').getArgs(frame, {frameOnly = true})
--ck = ck or args.noredlinks
--alt = alt or args[2]
return p.luaMain(args[1], args.bracket, args.fulltext, ck) or alt or ''
end
-- 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
function p.main(frame)
return _main(frame)
end
-- main variant, returns empty string if a redlink would be returned otherwise
function p.mainE(frame)
return _main(frame, true)
end
-- main variant, returns args[2] if a redlink would be returned otherwise
function p.mainA(frame)
return _main(frame, true, 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