Module:Redirect: Difference between revisions

use mw.title.new, code by Galobtter and Rummskartoffel
m (1 revision imported)
(use mw.title.new, code by Galobtter and Rummskartoffel)
Line 12:
return nil
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)
local target = string.match(
text,
"^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]|]-)%]%]"
) or string.match(
text,
"^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]|]-)|[^%[%]]-%]%]"
)
return target and mw.uri.decode(target, 'PATH')
end
 
Line 44 ⟶ 31:
), 2)
end
if not titleObj or not titleObj.isRedirect then
return nil
end
local targetTitle = getTitle(target)titleObj.redirectTarget
if targetTitle then
-- Find the target by using string matching on the page content.
if fulltext then
local target = p.getTargetFromText(titleObj:getContent() or "")
return targetTitle.fullText
if target then
local targetTitle = getTitle(target)
if targetTitle then
if fulltext then
return targetTitle.fullText
else
return targetTitle.prefixedText
end
else
return niltargetTitle.prefixedText
end
else
return nil
-- 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
Anonymous user