Module:Redirect/sandbox: Difference between revisions

Content deleted Content added
Setting nowiki to default true
Trimmed redundancy
 
(53 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 rname
"^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]|]-)|[^%[%]]-%]%]"
end
)
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.
if redirect then
function p.getTarget(page, fulltext)
-- Decode html entities and percent encodings.
-- Get the title object. Both page names and title objects are allowed
redirect = mw.text.decode(redirect, true)
-- as input.
redirect = mw.uri.decode(redirect, 'WIKI')
local titleObj
end
if type(page) == 'string' or type(page) == 'number' then
return redirect
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
 
--[[
function p.main(frame)
-- 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
local args = getArgs(frame)
-- page name can be given as plain text or as a page link.
local rname, bracket = args[1], args.bracket
--
bracket = bracket and "[[%s]]" or "%s"
-- Returns page name as plain text, or when the bracket parameter is given, as a
if type(rname) ~= "string" or not mw.ustring.match(rname, "%S") then return end
-- page link. Returns an error message when page does not exist or the redirect
rname = mw.ustring.match(rname, "%[%[(.+)%]%]") or rname
-- target cannot be determined for some reason.
 
--]]
local redirect = getRedirect(rname)
function p.luaMain(rname, bracket, fulltext)
 
if type(rname) ~= "string" or not rname:find("%S") then
if redirect then
return nil
return mw.ustring.format(bracket, redirect)
end
else
bracket = bracket and "[[%s]]" or "%s"
return mw.ustring.format('<span class="error">[[Module:redirect]] error: could not parse redirect - [[%s]]</span>', rname)
rname = rname:match("%[%[(.+)%]%]") or rname
end
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 getPage(sourceName)
function p.main(frame)
 
local args = require('Module:Arguments').getArgs(frame, {frameOnly = true})
if type(sourceName) ~= "string" or not mw.ustring.match(sourceName, "%S") then return end
return p.luaMain(args[1], args.bracket, args.fulltext) or ''
-- the following delink doesn't make as much sense for this usage but is done for consistency.
sourceName = mw.ustring.match(sourceName, "%[%[(.+)%]%]") or sourceName
 
local noError, title = pcall(mw.title.new, sourceName)
if not noError then
text = nil -- should be anyway; nil means error
else
text = title:getContent()
end
return text
end
 
-- Returns true if the specified page is a redirect, and false otherwise.
function nowikize(text, nowiki, default)
function p.luaIsRedirect(page)
if (default or nowiki) and (nowiki ~= 'no') then
local titleObj = getTitle(page)
text = frame:preprocess('<nowiki>'..text..'</nowiki>')
if not titleObj then
return false
end
return texttitleObj.isRedirect
end
 
-- Provides access to the luaIsRedirect function from wikitext, returning 'yes'
function p.block(frame)
-- if the specified page is a redirect, and the blank string otherwise.
-- this feature takes an initial page as an argument. It obtains all the links from that page
function p.isRedirect(frame)
-- starting from the first instance of some link named as start=, and replaces every link after
local args = require('Module:Arguments').getArgs(frame, {frameOnly = true})
-- that which is a redirect with the non-redirected canonical name, up until pcall throws an error
if p.luaIsRedirect(args[1]) then
-- due to expense. Finally, it returns the substituted page.
return 'yes'
 
else
local args = getArgs(frame)
return ''
local sourceName, start, bracket, text, nowiki = args[1], args[2], args.bracket, args.text, args.nowiki
end
bracket = bracket and "[[%s]]" or "%s" -- kind of an unwanted parameter in this function, but lingers
if type(text) ~= "string" then
text = getPage(sourceName)-- we're getting text = the contents of the page at args[1]
end
 
if not(text) then -- nothing to work from
return -- optional error here
end
local originalText = text -- I don't want to even think about conflicts...
local nextLink = mw.ustring.gmatch(originalText, "%[%[([^%]|]*)|?([^%]]-)%]%]")
local link = " " -- true
local display = "" -- false
while link do
if not(start) and link ~= " " then
link = mw.text.trim(link) or link
newLink = getRedirect(link)
if newLink and (newLink ~= link) then
-- still need a way to unscrew pattern recognition in link and even newLink
text = mw.ustring.gsub(text, link, newLink, 1) or text -- no sense looking past 1, we'll be back...
end
end
 
link, display = nextLink()
 
if start then
if start == link then
start = nil
else
link = " " -- discard all links before the one to start with
end
end
end
return nowikize(text, nowiki, true)
end