Modul:Redirect hatnote: Unterschied zwischen den Versionen

detect more types of invalid and missing redirects
K (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)
Zeile 8: Zeile 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 = {}
Zeile 112: Zeile 111:
end
end


function p._redirect(redirect, data, options, titleObj)
function p._redirect(redirect, data, options, currentTitle, redirectTitle, targetTitle)
-- Validate the input. Don't bother checking titleObj as it is only used
-- Validate the input. Don't bother checking currentTitle, redirectTitle or
-- for testing purposes.
-- targetTitle, as they are only used in testing.
checkType('_redirect', 1, redirect, 'string')
checkType('_redirect', 1, redirect, 'string')
checkType('_redirect', 2, data, 'table', true)
checkType('_redirect', 2, data, 'table', true)
Zeile 120: Zeile 119:
data = data or {}
data = data or {}
options = options or {}
options = options or {}
currentTitle = currentTitle or mw.title.getCurrentTitle()


-- Generate the text.
-- Generate the text.
Zeile 132: Zeile 132:
text = table.concat(text, ' ')
text = table.concat(text, ' ')
-- Generate the options to pass to [[Module:Hatnote]].
-- Generate the tracking category.
local mhOptions = {}
-- We don't need a tracking category if the template invocation has been
mhOptions.selfref = options.selfref
-- 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' then
if not redirect:find('^REDIRECT%d*$') and redirect ~= 'TERM' --
titleObj = titleObj or mw.title.getCurrentTitle()
and currentTitle.namespace == 0
if titleObj.namespace == 0 then
then
local redirectTitle = getTitle(redirect)
redirectTitle = redirectTitle or getTitle(redirect)
if redirectTitle then
if not redirectTitle or not redirectTitle.exists then
if not redirectTitle.exists then
category = 'Missing redirects'
category = '[[Category:Missing redirects]]'
elseif not redirectTitle.isRedirect then
else
category = 'Invalid redirects'
mRedirect = require('Module:Redirect')
else
local target = mRedirect.getTarget(redirectTitle)
local mRedirect = require('Module:Redirect')
local targetTitle = target and getTitle(target)
local target = mRedirect.getTarget(redirectTitle)
if targetTitle and not mw.title.equals(targetTitle, titleObj) then
targetTitle = targetTitle or target and getTitle(target)
category = '[[Category:Invalid redirects]]'
if targetTitle and targetTitle ~= currentTitle then
end
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]].
local mhOptions = {}
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
mhOptions.selfref = options.selfref
end


return mHatnote._hatnote(text, mhOptions) .. category
return mHatnote._hatnote(text, mhOptions) .. category
Anonymer Benutzer