Da WikIAC.
<?php
# Tournament handler for wikIACers
# Author: Chirale <http://iac.quasipercaso.com/Utente:Chirale>
# argv: turno, gironi, sfidanti, passano
# input: voters choice (comma separated values)
# ;Voter\n [{A-Z}{1-n}, ...] ...
# output: vote list, statistics
$wgExtensionFunctions[] = "wfTournamentExtension";
function wfTournamentExtension() {
global $wgParser;
# register the extension with the WikiText parser
# the first parameter is the name of the new tag.
# In this case it defines the tag <example> ... </example>
# the second parameter is the callback function for
# processing the text between the tags
$wgParser->setHook( "torneo", "getTournamentStatistic" );
}
# The callback function for converting the input text to HTML output
function getTournamentStatistic( $input, $argv ) {
# $argv is an array containing any arguments passed to the
# extension like <example argument="foo" bar>..
# Put this on the sandbox page: (works in MediaWiki 1.5.5)
# <example argument="foo" argument2="bar">Testing text **example** in between the new tags</example>
# $output = "Text passed into example extension: <br/>$input";
# $output .= " <br/> and the value for the arg 'argument' is " . $argv["argument"];
# $output .= " <br/> and the value for the arg 'argument2' is: " . $argv["argument2"];
# return $output;
$crlfed = explode("\n", $input);
$output="";
for($i=-1, $line=0; $line<count($crlfed); $line++){
#elimino gli spazi ed i ";"
if(ereg("^;",$crlfed[$line])){
#autore
$i++;
$votelist[$i][0]=trim($crlfed[$line], "\x20\x3B");
$votelist[$i][1]=Array();
}
elseif(ereg("^\x20",$crlfed[$line])){
$votelist[$i][1] = array_merge($votelist[$i][1],explode(", ",trim($crlfed[$line])));
}
}
#punto all'elemento successivo all'ultimo
$i++;
#creazione output
#leggo gli argomenti passati tramite <torneo>
$gironi=intval($argv["gironi"]);
$sfidanti=intval($argv["sfidanti"]);
$turno=intval($argv["turno"]);
$passano=intval($argv["passano"]);
#variabili calcolate da argomenti
$candidatixgirone=intval($sfidanti/$gironi);
#inizio output
$output.="<table border=\"1\" id=\"torneodatigenerali\" class=\"wikitable\">";
$output.="<caption>".$turno."º turno</caption>";
$output.="<tr><th><a href=\"#votanti\">votanti</a></th><td>".$i." IACer</td></tr>";
$output.="<tr><th><a href=\"#Sfidanti\">sfidanti</a></th><td>".$sfidanti."</td></tr>";
$output.="<tr><th>gironi</th><td>".$gironi."</td></tr>";
$output.="<tr><th>candidati per girone</th><td>".$candidatixgirone."</td></tr>";
$output.="<tr><th>ogni girone passano</th><td>".$passano."</td></tr>";
$output.="</table>";
#comprendo anche i possibili scarti
if(($sfidanti%$gironi)>0)
$candidatixgirone++;
#inizializzo la matrice
for($a=0; $a<$gironi; $a++){
for($b=0; $b<$candidatixgirone; $b++){
$votemat[$a][$b]=0;
}
}
#output torneo voti
#(elaborato prima, segue però in stampa la matrice voti)
$otorneovoti.="<h2><a name=\"votanti\"></a>Voti individuali</h2>";
$otorneovoti.="<dl class=\"torneovoti\">";
foreach ($votelist as $voto){
$otorneovoti.="<dt>".$voto[0]."</dt><dd>";
foreach ($voto[1] as $preferenza){
$otorneovoti.=$preferenza." ";
$pref=strtoupper($preferenza);
$votemat[(ord(substr($pref,0,1))-65)][intval(substr($pref,1))-1]++;
}
#Voti in comune (intersect)
$match=Array();
foreach($votelist as $votoaltrui){
$match[] = Array(count(array_intersect($voto[1], $votoaltrui[1])), $votoaltrui[0]);
}
#ordine inverso rispetto alle corrispondenze
rsort($match);
$otorneovoti.="<table class=\"torneointersect\">";
$otorneovoti.="<caption>Voti in comune</caption>";
foreach($match as $el){
#escludo il votante corrente
#el[1] è l'autore nella matrice degli intersect mentre el[0] è il numero di corrispondenze
if(strcmp($el[1],$voto[0]))
$otorneovoti.="<tr><td>".$el[0]."</td><td>".$el[1]."</td></tr>";
}
$otorneovoti.="</table></dd>";
}
$otorneovoti.="</dl>";
#prendo i primi $passano voti massimi per ogni girone
#in ordine decrescente
#serve per stabilire chi passerÃ
for($a=0; $a<$gironi; $a++){
#$passanomat[$a] = array_unique($votemat[$a]);
$passanomat[$a] = $votemat[$a];
rsort($passanomat[$a]);
#se l'ultimo candidato che passa ha gli stessi voti del successivo
#significa che vi sono parimerito oltre il tetto di quelli che possono passare
#e vanno quindi segnalati in modo diverso
if(count($passanomat[$a])>$passano){
if($passanomat[$a][$passano]==$passanomat[$a][$passano-1])
$parimerito[$a]=true; #true (1)
else
$parimerito[$a]=false; #false (0)
$passanomat[$a] = array_slice($passanomat[$a], 0, $passano);
}
}
#stampo la matrice dei voti
$output.="<table border=\"1\" id=\"torneomat\" class=\"wikitable\">";
$output.="<caption>Voti per candidati</caption>";
#riga + casella vuota
$output.="<tr><th></th>";
#stampo intestazione (numeri)
for($a=1;$a<=$candidatixgirone;$a++){
$output.="<th>".$a."</th>";
}
$output.="</tr>";
for($a=0; $a<$gironi; $a++){
$output.="<tr>";
#stampo intestazione laterale (lettere)
if(!$parimerito[$a]){
$thopen ="<th>";
$thclose ="</th>";
}
else{
$thopen = "<th><strong><em>";
$thclose = "</em></strong></th>";
}
$output.=$thopen.chr($a+65).$thclose;
#stampo riga matrice
for($b=0; $b<$candidatixgirone; $b++){
$vmclose=$vmopen="";
if(in_array($votemat[$a][$b],$passanomat[$a])){
if(($votemat[$a][$b]==$passanomat[$a][count($passanomat[$a])-1]) && ($parimerito[$a])){
#il più basso fra quelli che passano è evidenziato in modo differente
#così quelli a pari merito sono distinguibili
$vmopen = "<strong><em>";
$vmclose = "</em></strong>";
}
else{
$vmopen = "<strong>";
$vmclose = "</strong>";
}
}
$output.="<td>".$vmopen.$votemat[$a][$b].$vmclose."</td>";
}
$output.="</tr>";
}
$output.="</table>";
$output.=$otorneovoti;
return $output;
}
?>