Module:Redirect/sandbox: Difference between revisions

Content deleted Content added
temporarily debug
Trimmed redundancy
 
(75 intermediate revisions by 8 users not shown)
Line 1:
-- This module provides functions for getting the target of a redirect page.
-- 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.
 
-- Thus these are roughly the same:
-- [[{{#invoke:redirect|main|redirect-page-name}}]] and {{#invoke:redirect|main|redirect-page-name|bracket=yes}}
 
local p = {}
 
-- Gets a mw.title object, using pcall to avoid generating script errors if we
function getArgs(frame)
-- are over the expensive function count limit (among other possible causes).
-- If called via #invoke, use the args passed into the invoking
local function getTitle(...)
-- template, or the args passed to #invoke if any exist. Otherwise
local success, titleObj = pcall(mw.title.new, ...)
-- assume args are being passed directly in from the debug console
if success then
-- or from another Lua module.
return titleObj
local origArgs
else
if frame == mw.getCurrentFrame() then
return nil
origArgs = frame:getParent().args
end
for k, v in pairs( frame.args ) do
origArgs = frame.args
break
end
else
origArgs = frame
end
-- Trim whitespace and remove blank arguments.
local args = {}
for k, v in pairs( origArgs ) do
v = mw.text.trim( v )
if v ~= '' then
args[k] = v
end
end
return args
end
 
-- Gets the name of a page that a redirect leads to, or nil if it isn't a
function getRedirect(rname)
-- redirect.
-- Get the title object, passing the function through pcall
function p.getTargetFromText(text)
-- in case we are over the expensive function count limit.
local target = string.match(
-- returning nil indicates the redirect did not parse, but nonexistent or non-redirect pages are not nil.
text,
local noError, rpage = pcall(mw.title.new, rname)
"^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]|]-)%]%]"
if not noError or noError and not rpage or not rpage.isRedirect then
) or string.match(
-- mw.title.new failed, or the page is not a redirect, so use the passed page name.
text,
return mw.ustring.format(bracket, rname) -- it's an error, but NOT fatal in previous version.
"^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]|]-)|[^%[%]]-%]%]"
end
)
debuglog = tostring(rname)
return target and mw.uri.decode(target, 'PATH')
end
 
-- Gets the target of a redirect. If the page specified is not a redirect,
local redirect = mw.ustring.match(rpage:getContent() or "", "^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]]-)%]%]" )
-- returns nil.
debuglog = debuglog .. tostring(redirect)
function p.getTarget(page, fulltext)
if redirect then
-- Get the title object. Both page names and title objects are allowed
-- Decode html entities and percent encodings.
-- as input.
redirect = mw.text.decode(redirect, true)
local titleObj
redirect = mw.uri.decode(redirect, 'WIKI')
if type(page) == 'string' or type(page) == 'number' then
end
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 not titleObj then
return nil
end
local targetTitle = titleObj.redirectTarget
if targetTitle then
if fulltext then
return targetTitle.fullText
else
return targetTitle.prefixedText
end
else
return nil
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)
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 target = p.getTarget(rname, fulltext)
local ret = target or rname
ret = getTitle(ret)
if ret then
if fulltext then
ret = ret.fullText
else
ret = ret.prefixedText
end
return bracket:format(ret)
else
return nil
end
end
 
-- Provides access to the luaMain function from wikitext.
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {frameOnly = true})
return p.luaMain(args[1], args.bracket, args.fulltext) or ''
end
 
-- Returns true if the specified page is a redirect, and false otherwise.
local args = getArgs(frame)
function p.luaIsRedirect(page)
local rname, bracket = args[1], args.bracket
local titleObj = getTitle(page)
if type(rname) ~= "string" or not mw.ustring.match(rname, "%S") then return end
if not titleObj then
bracket = bracket and "[[%s]]" or "%s"
return false
rname = mw.ustring.match(rname, "%[%[(.+)%]%]") or rname
end
 
return titleObj.isRedirect
local redirect = getRedirect(rname)
end
 
-- Provides access to the luaIsRedirect function from wikitext, returning 'yes'
if redirect then
-- if the specified page is a redirect, and the blank string otherwise.
return mw.ustring.format(bracket, redirect)
function p.isRedirect(frame)
else
local args = require('Module:Arguments').getArgs(frame, {frameOnly = true})
return mw.ustring.format('debuglog .. <span class="error">[[Module:redirect]] error: could not parse redirect - [[%s]]</span>', rname)
if p.luaIsRedirect(args[1]) then
end
return 'yes'
else
return ''
end
end