Jump to content

Module:Signpost poll

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 15:10, 7 March 2015 (add more colours, default values, and make Poll.new look respectable). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- This module implments polls used in articles of the Signpost.

local CONFIG_MODULE = 'Module:Signpost poll/config'

local mStringCount = require('Module:String count')

local cfg = {

colors = {
	'#006699', -- Foundation blue
	'#339966', -- Foundation red
	'#990000', -- Foundation green

	-- From http://stackoverflow.com/questions/470690/how-to-automatically-generate-n-distinct-colors
	-- with some colors similar to Foundation colors removed.
	'#FFB300',
	'#803E75',
	'#FF6800',
	'#A6BDD7',
	'#CEA262',
	'#817066',
	'#F6768E',
	'#FF7A5C',
	'#53377A',
	'#FF8E00',
	'#B32851',
	'#F4C800',
	'#93AA00',
	'#593315',
	'#F13A13',
	'#232C16',
},

msg = {

-- The default vote preload text.
-- $1 is the option number.
['vote-default'] = 'Voting for option $1.',

['not-enough-votes'] = "Need '''$1''' more votes to display results—" ..
	"if you haven't already, consider voting!",

['preload-default'] = 'Wikipedia:Wikipedia Signpost/Templates/Voter/Vote preload',

['icon-default'] = 'WikipediaSignpostIcon.svg',

['overlay-default'] = 'Foundation Logo Transparent.svg',

['minimum-default'] = 10,

['expiry-default'] = 14,

},

}

--[[
We need:

preload - use a standard preload and pass the option text in by parameter.
icon
question
votepage

answer1
answer1vote - the vote text to use for answer one - defaults to answer1
answer1color
answer2
answer2vote
answer2color
...

minimum - the minimum number of votes before results are displayed
break - if 'all', breaks on all options; if a number, breaks after that number option
overlay
expiry
]]

-------------------------------------------------------------------------------
-- Poll class
-------------------------------------------------------------------------------

local Poll = {}
Poll.__index = Poll

function Poll.new(args, cfg)
	local self = setmetatable({}, Poll)
	self.cfg = cfg

	-- Set required fields
	self.question = assert(args.question, 'please specify a question')
	self.votepage = assert(args.votepage, 'please specify a vote page')

	-- Set options
	self.preload = args.preload or self:message('preload-default')
	self.icon = args.icon or self:message('icon-default')
	self.overlay = args.overlay or self:message('overlay-default')
	self.minimum = args.minimum or self:message('minimum-default')
	self.expiry = args.expiry or self:message('expiry-default')
	self.lineBreak = args['break']

	-- Set answers
	self.answers = {}
	do
		local i = 1
		while true do
			local key = 'answer' .. tostring(i)
			local answer = args[key]
			if not answer then
				break
			end
			table.insert(self.answers, {
				answer = answer,
				vote = args[key .. 'vote'],
				color = args[key .. 'color'],
			})
			i = i + 1
		end
		if #self.answers < 2 then
			error('polls must have at least two answers')
		end
	end

	return self
end

function Poll:message(key, ...)
	local msg = self.cfg.msg[key]
	if select('#', ...) > 0 then
		return mw.message.newRawMessage(msg, ...):plain()
	else
		return msg
	end
end

function Poll:getColor(n)
	-- Get the color for option n
	local colors = self.cfg.colors
	-- colors[#colors] is necessary as Lua arrays are indexed starting at 1,
	-- and n % #colors might sometimes equal 0.
	return colors[n] or colors[n % #colors] or colors[#colors]
end

-------------------------------------------------------------------------------
-- Exports
-------------------------------------------------------------------------------

local p = {}

function p._main(args, cfg)
	cfg = cfg or mw.loadData(CONFIG_MODULE)
	return tostring(Poll.new(args, cfg))
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = 'Wikipedia:Wikipedia Signpost/Templates/Voter'
	})
	return p._main(args)
end

return p