Jump to content

Module:Random portal component/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 05:08, 4 December 2014 (organise both functions by header, body, and footer, rather than by text snippet). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- This module implements [[Template:Random portal component]]

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 makeExpandArgFunc(args)
	-- Make a function to 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.
	return function (key)
		local val = args[key]
		if val then
			return val
		else
			return string.format('{{{%s}}}', key)
		end
	end
end

function p._main(args, frame)
	frame = frame or mw.getCurrentFrame()
	local expandArg = makeExpandArgFunc(args)

	-- Find the different pages
	local rootpage = args.rootpage or currentTitle.prefixedText
	local subpage = rootpage .. '/' .. expandArg('subpage')
	local randomSubpage = subpage .. '/' .. getRandomNumber(args.max)

	local ret = {}

	-- Header
	do
		local header = args.header or 'subpage'
		ret[#ret + 1] = frame:expandTemplate{
			title = rootpage .. '/box-header',
			args = {header, randomSubpage}
		}
	end
	
	-- Random subpage content
	ret[#ret + 1] = frame:expandTemplate{title = randomSubpage}

	-- Footer
	if not args.footer or not args.footer:find('%S') then
		ret[#ret + 1] = '<div style="clear:both;"></div></div>'
	else
		local footerArg = string.format(
			'[[%s|%s]]',
			subpage,
			expandArg('footer')
		)
		ret[#ret + 1] = frame:expandTemplate{
			title = rootpage .. '/box-footer',
			args = {footerArg}
		}
	end

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

function p._nominate(args, frame)
	frame = frame or mw.getCurrentFrame()
	local expandArg = makeExpandArgFunc(args)

	-- Gather together the text snippets used in the template.
	local rootpage = currentTitle.prefixedText
	local subpage = rootpage .. '/' .. expandArg('subpage')
	local randomSubpage = subpage .. '/' .. getRandomNumber(args.max)
	
	local ret = {}

	-- Header
	ret[#ret + 1] = frame:expandTemplate{
		title = rootpage .. '/box-header',
		args = {expandArg('header'), randomSubpage}
	}

	-- Random subpage content
	ret[#ret + 1] = frame:expandTemplate{title = randomSubpage}

	-- Footer
	do
		local footerArg = string.format(
			'[[/Nominate/%s|Suggest]] • [[%s|%s]] ',
			expandArg('subpage'),
			subpage,
			args.footer or 'Archive'
		)
		ret[#ret + 1] = frame:expandTemplate{
			title = rootpage .. '/box-footer',
			args = {footerArg}
		}
	end

	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'
		})
		return func(args, frame)
	end
end

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

return p