Module:Redirect hatnote: Difference between revisions
From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Content added Content deleted
m (Protected Module:Redirect hatnote: High-risk Lua module ([Edit=Allow only template editors and admins] (indefinite) [Move=Allow only template editors and admins] (indefinite))) |
(detect more types of invalid and missing redirects) |
||
Line 8: | Line 8: | ||
local libraryUtil = require('libraryUtil') |
local libraryUtil = require('libraryUtil') |
||
local checkType = libraryUtil.checkType |
local checkType = libraryUtil.checkType |
||
local mRedirect -- lazily initialise [[Module:Redirect]] |
|||
local p = {} |
local p = {} |
||
Line 112: | Line 111: | ||
end |
end |
||
function p._redirect(redirect, data, options, |
function p._redirect(redirect, data, options, currentTitle, redirectTitle, targetTitle) |
||
-- Validate the input. Don't bother checking |
-- Validate the input. Don't bother checking currentTitle, redirectTitle or |
||
-- targetTitle, as they are only used in testing. |
|||
-- for testing purposes. |
|||
checkType('_redirect', 1, redirect, 'string') |
checkType('_redirect', 1, redirect, 'string') |
||
checkType('_redirect', 2, data, 'table', true) |
checkType('_redirect', 2, data, 'table', true) |
||
Line 120: | Line 119: | ||
data = data or {} |
data = data or {} |
||
options = options or {} |
options = options or {} |
||
⚫ | |||
-- Generate the text. |
-- Generate the text. |
||
Line 132: | Line 132: | ||
text = table.concat(text, ' ') |
text = table.concat(text, ' ') |
||
-- Generate the |
-- Generate the tracking category. |
||
-- We don't need a tracking category if the template invocation has been |
|||
⚫ | |||
-- copied directly from the docs, or if we aren't in mainspace. |
|||
⚫ | |||
-- Find whether to add the tracking category. |
|||
-- We only add the category if both of the following are true: |
|||
-- a) redirect isn't any of the keywords 'REDIRECT', 'REDIRECT1', |
|||
-- 'REDIRECT2', ..., or 'TERM'. |
|||
-- b) we are in the main namespace. |
|||
-- If these are both true, then we check for existence of the redirect. If |
|||
-- it doesn't exist, then we add the missing redirect category. If it does |
|||
-- exist, but the redirect target is not the current page, we add the |
|||
-- invalid redirect category. |
|||
local category |
local category |
||
if not redirect:find('^REDIRECT%d*$') and redirect ~= 'TERM' |
if not redirect:find('^REDIRECT%d*$') and redirect ~= 'TERM' -- |
||
⚫ | |||
⚫ | |||
then |
|||
⚫ | |||
redirectTitle = redirectTitle or getTitle(redirect) |
|||
if not redirectTitle or not redirectTitle.exists then |
|||
⚫ | |||
elseif not redirectTitle.isRedirect then |
|||
⚫ | |||
⚫ | |||
else |
|||
mRedirect = require('Module:Redirect') |
|||
local mRedirect = require('Module:Redirect') |
|||
local target = mRedirect.getTarget(redirectTitle) |
|||
targetTitle = targetTitle or target and getTitle(target) |
|||
if targetTitle and targetTitle ~= currentTitle then |
|||
⚫ | |||
category = 'Invalid redirects' |
|||
end |
|||
⚫ | |||
end |
end |
||
end |
end |
||
end |
end |
||
category = category or '' |
category = category and string.format('[[Category:%s]]', category) or '' |
||
-- Generate the options to pass to [[Module:Hatnote]]. |
|||
⚫ | |||
if currentTitle.namespace == 0 |
|||
and redirectTitle and redirectTitle.namespace ~= 0 |
|||
then |
|||
-- We are on a mainspace page, and the hatnote starts with something |
|||
-- like "Wikipedia:Foo redirects here", so automatically label it as a |
|||
-- self-reference. |
|||
mhOptions.selfref = true |
|||
else |
|||
⚫ | |||
⚫ | |||
return mHatnote._hatnote(text, mhOptions) .. category |
return mHatnote._hatnote(text, mhOptions) .. category |