Modul:SGW-Zwischenstand

aus Wikipedia, der freien Enzyklopädie
local p = {}
function p.pro( title )
	return count(title,2) -- Ausgabe der Anzahl der Prostimmen
end
function p.kontra( title )
	return count(title,3) -- Ausgabe der Anzahl der Kontrastimmen
end
function p.enthaltung( title )
	return count(title,4) -- Ausgabe der Anzahl der Stimmenthaltungen
end
function p.rang( title )
	local args = title.args
	local diff = {} -- {diffTest, diffTest2, ..., diffTestN}
	local names = getnames(title) -- {Test, Test2, ..., TestN}
	local nameposition
	local rank = 1
	local i = 1
	while (i<=#names) do -- Stimmendifferenz des Referenzkandidaten ermitteln
		if (names[i]==args.kandidat) then
			nameposition = i
			local pro = count(title,2,names[nameposition])
			local kon = count(title,3,names[nameposition])
			diff[nameposition]=pro-kon
			i = 1
			break
		end
		i = i+1
	end
	if (diff[nameposition]<=0) then
		return "–"
	end
	while (i<=#names) do -- Rang in Relation zu den anderen Kandidaten ermitteln
		pro = count(title,2,names[i])
		kon = count(title,3,names[i])
		diff[i]=pro-kon
		if (diff[i]>diff[nameposition]) then
			rank = rank+1
		end
		i = i+1
	end
	return rank .. "."
end
function count( title, part, cand ) -- Stimmen zählen
	text = splittext(gettext(title,cand))
	local hits = 0
	for hit in string.gmatch(text[part],"[\n]#[^:#]") do
		hits = hits+1
	end
	return hits
end
function getnames( title ) -- Kandidatenliste ermitteln
	local names = {}
	local args = title.args
	local pg = mw.title.new("Wikipedia:Schiedsgericht/Wahl/" .. args.wahl)
	local text = pg:getContent()
	text = string.gsub(text,".-== ?[Kk]andidaten ?==","")
	text = string.gsub(text,".-== ?Kandidatinnen und Kandidaten ?==","")
	local i = 1
	for name in string.gmatch(text,"\n%* ?%[%[/?.-|?(.-)/?]]") do
		names[i] = name
		i = i+1
	end
	return names
end
function gettext( title, cand ) -- Kandidatenseiten einlesen
	local args = title.args
	args.kandidat = cand or args.kandidat
	local pg = mw.title.new("Wikipedia:Schiedsgericht/Wahl/" .. args.wahl .. "/" .. args.kandidat)
	local text = pg:getContent()
   	text = string.gsub(text,"<!%-%-.-%-%->","") -- Kommentare entfernen
	return text
end
function splittext( text ) -- Kandidatenseiten aufteilen
	local split = {}
	local function helper( text )
		table.insert(split, text)
		return ""
	end
	helper((text:gsub("(.-)==== ?[PpKkEe][RrOoNn][OoNnTtMm]", helper)))
	return split
end
return p