Modul:Redirect hatnote: Unterschied zwischen den Versionen
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 p = {} | local p = {} | ||
Zeile 112: | Zeile 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. | ||
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 | -- 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. | |||
-- We | |||
-- | |||
local category | local category | ||
if not redirect:find('^REDIRECT%d*$') and redirect ~= 'TERM' | if not redirect:find('^REDIRECT%d*$') and redirect ~= 'TERM' -- | ||
and currentTitle.namespace == 0 | |||
then | |||
redirectTitle = redirectTitle or getTitle(redirect) | |||
if not redirectTitle or not redirectTitle.exists then | |||
category = 'Missing redirects' | |||
elseif not redirectTitle.isRedirect then | |||
category = 'Invalid redirects' | |||
else | |||
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 | ||
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 |