Modulo:Quorum

Versione del 5 ago 2020 alle 13:42 di Sakretsu (discussione | contributi) (creazione modulo)
(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)

Modulo che implementa le funzionalità del template:Quorum.


local p = {}

function set_error(msg)
	return string.format('<span class="error">%s</span>', msg)
end

function p.main()
	local page = 'Wikipedia:Amministratori/Sistema di voto/Quorum'
	local source = mw.title.new(page):getContent()
	local quorum = 0
	local elections, candidates, votes, hash = {}, {}, {}, {}
	for election in string.gmatch(source, '%[%[(W%a+:Amministratori/Elezioni/.-)[|%]]') do
		table.insert(elections, election)
		local candidate = election:match('Elezioni/([^/]+)')
		table.insert(candidates, string.format('[[User:%s|%s]]', candidate, candidate))
	end
	if #elections ~= 8 then
		return set_error('È stato trovato un elenco di ' .. #elections .. ' elezioni nella pagina ' .. page)
	end
	for i, election in ipairs(elections) do
		local content = mw.title.new(election):getContent()
		if not content then
			return set_error('Non è stata trovata la pagina della seguente elezione: ' .. election)
		end
		if hash[election] then
			return set_error('È stata elencata più volte la seguente elezione: ' .. election)
		end
		hash[election] = true
		content = content:gsub('\n==+ *[^\n]- *==+', '\r%0')
		for heading, section in string.gmatch(content, '\n==+ *([^\n]-) *==+(.-)\r') do
			if heading == 'Pro' or heading == 'Contro' then
				local _, count = section:gsub('\n#[^#:*\n][^\n]-%a', '')
				quorum = quorum + count
				votes[i] = (votes[i] or 0) + count
			end
		end
	end
	if not source:match('%s' .. table.concat(votes, '%s.-%s') .. '%s') then
		return set_error('I voti nella pagina ' .. page .. ' non combaciano con quelli calcolati per ciascuna elezione: ' .. table.concat(votes, ', '))
	end
	quorum = math.floor(quorum / 12)
	if not mw.title.equals(mw.title.getCurrentTitle(), mw.title.new(page)) then return quorum end
	candidates = mw.text.listToText(candidates, ', ', ' e ')
	return string.format('Pertanto il quorum per la prossima votazione, calcolato in base al numero di favorevoli e contrari nelle votazioni relative a %s, è pari a <b>%s</b>.', candidates, quorum)
end

return p