Misplaced Pages

Module:Random portal component: Difference between revisions

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Browse history interactively← Previous editNext edit →Content deleted Content added
Revision as of 23:34, 4 December 2014 view sourceMr. Stradivarius (talk | contribs)Edit filter managers, Administrators59,191 edits avoid putting portals with nonexistent subpages in Category:Pages with script errors, and output an error message and put them in Category:Portals needing attention instead← Previous edit Revision as of 00:17, 5 December 2014 view source Jackmcbarn (talk | contribs)31,380 edits do the same for the other 2 things that expand subpagesNext edit →
Line 31: Line 31:
end end


local function getHeader(frame, pages, header) local function tryExpandTemplate(frame, title, args)
return frame:expandTemplate{ local success, result = pcall(frame.expandTemplate, frame, {title = title, args = args})
if success then
title = pages.root .. '/box-header',
return result
args = {header, pages.random}
}
end

local function getRandomSubpageContent(frame, pages)
local success, content = pcall(
frame.expandTemplate, frame,
{title = pages.random}
)
if success and content then
return content
else else
return string.format( return string.format(
'<strong class="error">Error: ] does not exist.</strong>' .. '<strong class="error">Error: ] does not exist.</strong>' ..
']', ']',
title
pages.random
) )
end end
end

local function getHeader(frame, pages, header)
return tryExpandTemplate(
frame,
pages.root .. '/box-header',
{header, pages.random}
)
end

local function getRandomSubpageContent(frame, pages)
return tryExpandTemplate(
frame,
pages.random
)
end end


local function getFooter(frame, pages, link) local function getFooter(frame, pages, link)
return frame:expandTemplate{ return tryExpandTemplate(
frame,
title = pages.root .. '/box-footer', pages.root .. '/box-footer',
args = {link} {link}
} )
end end



Revision as of 00:17, 5 December 2014

Module documentation[view] [edit] [history] [purge]
BetaThis module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected.
This module depends on the following other modules:

This module implements {{random portal component}}. Please see the template page for documentation.

See also

Portal templates
Visual overview of template usage
Layout and formatting
Content transclusion
Content slideshows
Content randomisation
General
Linking templates
Talk pages / Maintenance
Images
Modules
Usage
For more information, see: Category:Template-Class Portal pages and Category:Misplaced Pages Portal templates
The above documentation is transcluded from Module:Random portal component/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.

-- This module implements ]

local p = {}

local mRandom = require('Module:Random')
local currentTitle = mw.title.getCurrentTitle()

local function getRandomNumber(max)
	-- gets a random integer between 1 and max; max defaults to 1
	return mRandom.number{max or 1}
end

local function expandArg(args, key)
	-- Emulate how unspecified template parameters appear in wikitext. If the
	-- specified argument exists, its value is returned, and if not the argument
	-- name is returned inside triple curly braces.
	local val = args
	if val then
		return val
	else
		return string.format('{{{%s}}}', key)
	end
end

local function getPages(args)
	local pages = {}
	pages.root = args.rootpage or currentTitle.prefixedText
	pages.subpage = pages.root .. '/' .. expandArg(args, 'subpage')
	pages.random = pages.subpage .. '/' .. getRandomNumber(args.max)
	return pages
end

local function tryExpandTemplate(frame, title, args)
	local success, result = pcall(frame.expandTemplate, frame, {title = title, args = args})
	if success then
		return result
	else
		return string.format(
			'<strong class="error">Error: ] does not exist.</strong>' ..
			']',
			title
		)
	end
end

local function getHeader(frame, pages, header)
	return tryExpandTemplate(
		frame,
		pages.root .. '/box-header',
		{header, pages.random}
	)
end

local function getRandomSubpageContent(frame, pages)
	return tryExpandTemplate(
		frame,
		pages.random
	)
end

local function getFooter(frame, pages, link)
	return tryExpandTemplate(
		frame,
		pages.root .. '/box-footer',
		{link}
	)
end

function p._main(args, frame)
	frame = frame or mw.getCurrentFrame()
	local pages = getPages(args)

	local ret = {}
	ret = getHeader(frame, pages, args.header or 'subpage')
	ret = getRandomSubpageContent(frame, pages)
	if not args.footer or not args.footer:find('%S') then
		ret = '<div style="clear:both;"></div></div>'
	else
		ret = getFooter(frame, pages, string.format(
			']',
			pages.root,
			expandArg(args, 'footer')
		))
	end

	return table.concat(ret, '\n')
end

function p._nominate(args, frame)
	frame = frame or mw.getCurrentFrame()
	local pages = getPages(args)
	
	local ret = {}
	ret = getHeader(frame, pages, expandArg(args, 'header'))
	ret = getRandomSubpageContent(frame, pages)
	ret = getFooter(frame, pages, string.format(
		'] • ] ',
		expandArg(args, 'subpage'),
		pages.root,
		args.footer or 'Archive'
	))

	return table.concat(ret, '\n')
end

local function makeInvokeFunction(func)
	return function (frame)
		local args = require('Module:Arguments').getArgs(frame, {
			trim = false,
			removeBlanks = false,
			wrappers = {
				'Template:Random portal component',
				'Template:Random portal component with nominate'
			}
		})
		return func(args, frame)
	end
end

p.main = makeInvokeFunction(p._main)
p.nominate = makeInvokeFunction(p._nominate)

return p
Category: