Jump to content

Module:Pagelist: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m Changed protection level of Module:Pagelist: High-risk Lua module: allow template editors ([Edit=Allow only template editors and admins] (indefinite) [Move=Allow only template editors and admins] (indefinite))
remove the getPageObject as it is no longer expensive, use Module:Arguments, and fix indenting and whitespace
Line 1: Line 1:
local p = {}
local p = {}


local separators = {
-- Get the page object. This will return the page object for the page
dot = true,
-- specified, or nil if there are errors in the title, if the
pipe = true,
-- expensive function count has been exceeded, or if the page was not
comma = true,
-- specified.
['tpt-languages'] = true
function getPageObject( page )
}
if not page then
return nil
end
-- Get the page object, passing the function through pcall
-- in case we are over the expensive function count limit.
local noError, pageObject = pcall(mw.title.new, page)
if not noError then
return nil
else
return pageObject
end
end


local function getSeparator(sep)
-- Process the separator parameter.
if type(sep) ~= 'string' then
local function getSeparator( sep )
return nil
if sep and type(sep) == 'string' then
end
if sep == 'dot'
if separators[sep] then
or sep =='pipe'
return mw.message.new(sep .. '-separator'):plain()
or sep == 'comma'
else
or sep == 'tpt-languages' then
return sep
return mw.message.new( sep .. '-separator' ):plain()
end
else
return sep
end
else
return nil
end
end
end


local function generateLink( page, nspace, delim, edelim )
local function generateLink(page, nspace, delim, endDelim)
if not page then
if not page then
return nil
return nil
end
end
local pagename = getPageObject( page )
local pagename = mw.title.new(page)
if not pagename then
if not pagename then
-- Default to the args we were passed if our page
-- Default to the args we were passed if our page
-- object was nil.
-- object was nil.
pagename = page
pagename = page
else
else
pagename = pagename.text
pagename = pagename.text
end
end
delim = delim or ''
delim = delim or ''
edelim = edelim or delim
endDelim = endDelim or delim
nspace = nspace or mw.title.getCurrentTitle().nsText
nspace = nspace or mw.title.getCurrentTitle().nsText
return mw.ustring.format(
return string.format(
'%s[[:%s:%s|%s]]%s',
'%s[[:%s:%s|%s]]%s',
delim, nspace, pagename, page, edelim
delim, nspace, pagename, page, endDelim
)
)
end
end


local function _main( args )
function p._main(args)
local t = {}
local t = {}
local separator = getSeparator( args.separator )
local separator = getSeparator(args.separator)
local conjunction = getSeparator( args.conjunction )
local conjunction = getSeparator(args.conjunction)
for i, v in ipairs( args ) do
for i, v in ipairs(args) do
table.insert( t, generateLink(
table.insert(t, generateLink(
v, args.nspace, args.delim, args.edelim
v, args.nspace, args.delim, args.edelim
))
) )
end
end
return mw.text.listToText( t, separator, conjunction )
return mw.text.listToText(t, separator, conjunction)
end
end


function p.main( frame )
function p.main(frame)
local origArgs = require('Module:Arguments').getArgs(frame, {
local origArgs
trim = false,
if frame == mw.getCurrentFrame() then
removeBlanks = false,
-- We're being called via #invoke. If the invoking template passed any arguments,
wrappers = 'Template:Pagelist'
-- use them. Otherwise, use the arguments that were passed into the template.
})
origArgs = frame:getParent().args

for k, v in pairs( frame.args ) do
-- Process integer args. Allow for explicit positional arguments that are
origArgs = frame.args
-- specified out of order, e.g. {{br separated entries|3=entry3}}.
break
-- After processing, the args can be accessed accurately from ipairs.
end
local args = {}
else
for k, v in pairs(origArgs) do
-- We're being called from another module or from the debug console, so assume
if type(k) == 'number' and
-- the arguments are passed in directly.
k >= 1 and
origArgs = frame
math.floor(k) == k and
end
string.match(v, '%S') then -- Remove blank or whitespace values.
table.insert(args, k)
-- Process integer args. Allow for explicit positional arguments that are
end
-- specified out of order, e.g. {{br separated entries|3=entry3}}.
end
-- After processing, the args can be accessed accurately from ipairs.
table.sort(args)
local args = {}
for k, v in pairs( origArgs ) do
for i, v in ipairs(args) do
args[i] = origArgs[v]
if type( k ) == 'number' and
-- Trim whitespace.
k >= 1 and
if type(args[i]) == 'string' then
math.floor( k ) == k and
args[i] = mw.text.trim(args[i])
mw.ustring.match( v, '%S' ) then -- Remove blank or whitespace values.
end
table.insert( args, k )
end
end

end
-- Get old named args. We don't need to remove blank values
table.sort( args )
-- as for the nspace and edelim parameters the behaviour is different
for i, v in ipairs( args ) do
-- depending on whether the parameters are blank or absent, and for
args[ i ] = origArgs[ v ]
-- the delim parameter the default should be the blank string anyway.
-- Trim whitespace.
args.delim = origArgs.delim
if type( args[ i ] ) == 'string' then
args.edelim = origArgs.edelim
args[ i ] = mw.text.trim( args[ i ] )
args.nspace = origArgs.nspace
end

end
-- Get new named args, "separator" and "conjunction", and strip blank values.
if origArgs.separator and origArgs.separator ~= '' then
-- Get old named args. We don't need to remove blank values
args.separator = origArgs.separator
-- as for the nspace and edelim parameters the behaviour is different
end
-- depending on whether the parameters are blank or absent, and for
if origArgs.conjunction and origArgs.conjunction ~= '' then
-- the delim parameter the default should be the blank string anyway.
args.delim = origArgs.delim
args.conjunction = origArgs.conjunction
end
args.edelim = origArgs.edelim

args.nspace = origArgs.nspace
return p._main(args)
-- Get new named args, "separator" and "conjunction", and strip blank values.
if origArgs.separator and origArgs.separator ~= '' then
args.separator = origArgs.separator
end
if origArgs.conjunction and origArgs.conjunction ~= '' then
args.conjunction = origArgs.conjunction
end
return _main( args )
end
end

return p
return p

Revision as of 04:32, 12 January 2015

local p = {}

local separators = {
	dot = true,
	pipe = true,
	comma = true,
	['tpt-languages'] = true
}

local function getSeparator(sep)
	if type(sep) ~= 'string' then
		return nil
	end
	if separators[sep] then
		return mw.message.new(sep .. '-separator'):plain()
	else
		return sep
	end
end

local function generateLink(page, nspace, delim, endDelim)
	if not page then
		return nil
	end
	local pagename = mw.title.new(page)
	if not pagename then
		-- Default to the args we were passed if our page
		-- object was nil.
		pagename = page
	else
		pagename = pagename.text
	end
	delim = delim or ''
	endDelim = endDelim or delim
	nspace = nspace or mw.title.getCurrentTitle().nsText
	return string.format( 
		'%s[[:%s:%s|%s]]%s',
		delim, nspace, pagename, page, endDelim
	)
end

function p._main(args)
	local t = {}
	local separator = getSeparator(args.separator)
	local conjunction = getSeparator(args.conjunction)
	for i, v in ipairs(args) do
		table.insert(t, generateLink(
			v, args.nspace, args.delim, args.edelim
		))
	end
	return mw.text.listToText(t, separator, conjunction)
end

function p.main(frame)
	local origArgs = require('Module:Arguments').getArgs(frame, {
		trim = false,
		removeBlanks = false,
		wrappers = 'Template:Pagelist'
	})

	-- Process integer args. Allow for explicit positional arguments that are
	-- specified out of order, e.g. {{br separated entries|3=entry3}}.
	-- After processing, the args can be accessed accurately from ipairs.
	local args = {}
	for k, v in pairs(origArgs) do
		if type(k) == 'number' and 
			k >= 1 and
			math.floor(k) == k and
			string.match(v, '%S') then -- Remove blank or whitespace values.
			table.insert(args, k)
		end
	end
	table.sort(args)
	for i, v in ipairs(args) do
		args[i] = origArgs[v]
		-- Trim whitespace.
		if type(args[i]) == 'string' then
			args[i] = mw.text.trim(args[i])
		end
	end

	-- Get old named args. We don't need to remove blank values
	-- as for the nspace and edelim parameters the behaviour is different
	-- depending on whether the parameters are blank or absent, and for
	-- the delim parameter the default should be the blank string anyway.
	args.delim = origArgs.delim
	args.edelim = origArgs.edelim
	args.nspace = origArgs.nspace

	-- Get new named args, "separator" and "conjunction", and strip blank values.
	if origArgs.separator and origArgs.separator ~= '' then
		args.separator = origArgs.separator
	end
	if origArgs.conjunction and origArgs.conjunction ~= '' then
		args.conjunction = origArgs.conjunction
	end

	return p._main(args)
end

return p