Permanently protected module

Module:Redirect: Difference between revisions

From HIBIKIFORUM
Jump to navigation Jump to search
(Created page with "require('strict') local p = {} -- key is beginning of arg name. value is table with namespace number and link -- alternatively, a function taking the namespace number and returning a validity -- can be used local namespaceCategories = { all = { function() return true end }, main = { 0, 'main' }, help = { 12, 'help' }, portal = { 100, 'portal' }, talk = { function(n) return n > 0 and n%2 == 1 end, '[[Help:Talk pa...")
 
m (Changed protection settings for "Module:Redirect" ([Edit=Require template editor access] (indefinite) [Move=Require template editor access] (indefinite)))
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
require('strict')
-- This module provides functions for getting the target of a redirect page.


local p = {}
local p = {}


-- key is beginning of arg name. value is table with namespace number and link
-- Gets a mw.title object, using pcall to avoid generating script errors if we
-- alternatively, a function taking the namespace number and returning a validity
-- are over the expensive function count limit (among other possible causes).
-- can be used
local function getTitle(...)
local namespaceCategories = {
local success, titleObj = pcall(mw.title.new, ...)
all = { function() return true end },
if success then
main = { 0, '[[wp:mainspace|main]]' },
return titleObj
help = { 12, '[[wp:help namespace|help]]' },
else
portal = { 100, '[[wp:portal|portal]]' },
return nil
talk = { function(n) return n > 0 and n%2 == 1 end, '[[Help:Talk pages|talk]]' },
end
template = { 10, '[[wp:template namespace|template]]' },
end
wikipedia = { 4, '[[wp:project namespace|Wikipedia project]]' },
category = { 14, '[[wp:categorization|category]]' },
user = { 2, '[[wp:user pages|user]]' },
}


-- remove whitespaces from beginning and end of args
-- Gets the name of a page that a redirect leads to, or nil if it isn't a
local function valueFunc(key, val)
-- redirect.
if type(val) == 'string' then
function p.getTargetFromText(text)
val = val:match('^%s*(.-)%s*$')
local target = string.match(
if val == '' then
text,
return nil
"^%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
 
-- Gets the target of a redirect. If the page specified is not a redirect,
-- returns nil.
function p.getTarget(page, fulltext)
-- Get the title object. Both page names and title objects are allowed
-- as input.
local titleObj
if type(page) == 'string' or type(page) == 'number' then
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
end
else
return nil
end
end
return val
end
end


local function getPrettyName(args)
--[[
for k in pairs(namespaceCategories) do
-- Given a single page name determines what page it redirects to and returns the
if args[k .. ' category'] then
-- target page name, or the passed page name when not a redirect. The passed
return string.format("'''[[:Category:%s|%s]]''': ", args[k .. ' category'], args.name)
-- 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
end
return bracket:format(ret)
else
return nil
end
end
return string.format("'''%s''': ", args.name)
end
end


-- Provides access to the luaMain function from wikitext.
function p.main(frame)
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {wrappers = 'Template:Redirect template', valueFunc = valueFunc})
local args = require('Module:Arguments').getArgs(frame, {frameOnly = true})
local namespace = mw.title.getCurrentTitle().namespace
return p.luaMain(args[1], args.bracket, args.fulltext) or ''
end


--- XXX: this is a HORRIBLE HACK. kill it with fire as soon as https://bugzilla.wikimedia.org/show_bug.cgi?id=12974 is fixed
-- Returns true if the specified page is a redirect, and false otherwise.
local beCompatibleWithBug12974 = args.info and (args.info:find('^[:;#*]', 1) == 1 or args.info:find('{|', 1, true) == 1) and '\n' or ' '
function p.luaIsRedirect(page)
local titleObj = getTitle(page)
local content = string.format('\n<div class="rcat %s">\n*%sThis is a redirect%s%s.%s%s\n</div>',
if not titleObj then
args.id and ('rcat-' .. string.gsub(args.id, ' ', '_')) or '',
return false
args.name and getPrettyName(args) or '',
end
args.from and (' from ' .. args.from) or '',
if titleObj.isRedirect then
args.to and (' to ' .. args.to) or '',
return true
args.info and beCompatibleWithBug12974 or '',
else
args.info or ''
return false
)
for k,v in pairs(namespaceCategories) do
if args[k .. ' category'] then
if type(v[1]) == 'function' and v[1](namespace) or v[1] == namespace then
if args.sortkey then
content = content .. string.format('[[Category:%s|%s]]', args[k .. ' category'], args.sortkey)
else
content = content .. string.format('[[Category:%s]]', args[k .. ' category'])
end
elseif args['other category'] then
if args.sortkey then
content = content .. string.format('[[Category:%s|%s]]', args['other category'], args.sortkey)
else
content = content .. string.format('[[Category:%s]]', args['other category'])
end
else
content = content .. frame:expandTemplate{title = 'Incorrect redirect template', args = {v[2]}}
end
end
end
end
end


if namespace == 0 then
-- Provides access to the luaIsRedirect function from wikitext, returning 'yes'
local yesno = require('Module:Yesno')
-- if the specified page is a redirect, and the blank string otherwise.
if yesno(args.printworthy) == true then
function p.isRedirect(frame)
return content .. '[[Category:Printworthy redirects]]'
local args = require('Module:Arguments').getArgs(frame, {frameOnly = true})
elseif yesno(args.printworthy) == false then
if p.luaIsRedirect(args[1]) then
return content .. '[[Category:Unprintworthy redirects]]'
return 'yes'
end
else
return ''
end
end
return content
end
end


return p
return p

Latest revision as of 05:36, 19 June 2023


-- This module provides functions for getting the target of a redirect page.

local p = {}

-- Gets a mw.title object, using pcall to avoid generating script errors if we
-- are over the expensive function count limit (among other possible causes).
local function getTitle(...)
	local success, titleObj = pcall(mw.title.new, ...)
	if success then
		return titleObj
	else
		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

-- Gets the target of a redirect. If the page specified is not a redirect,
-- returns nil.
function p.getTarget(page, fulltext)
	-- Get the title object. Both page names and title objects are allowed
	-- as input.
	local titleObj
	if type(page) == 'string' or type(page) == 'number' then
		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.
function p.luaIsRedirect(page)
	local titleObj = getTitle(page)
	if not titleObj then
		return false
	end
	if titleObj.isRedirect then
		return true
	else
		return false
	end
end

-- Provides access to the luaIsRedirect function from wikitext, returning 'yes'
-- if the specified page is a redirect, and the blank string otherwise.
function p.isRedirect(frame)
	local args = require('Module:Arguments').getArgs(frame, {frameOnly = true})
	if p.luaIsRedirect(args[1]) then
		return 'yes'
	else
		return ''
	end
end

return p