Skip to content

Instantly share code, notes, and snippets.

@jacob-g
Created September 16, 2013 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacob-g/6575431 to your computer and use it in GitHub Desktop.
Save jacob-g/6575431 to your computer and use it in GitHub Desktop.
Improved Scratch Wiki Vote Counter (PHP) - algorithm based on blob8108's, but implemented in PHP
<?php
$data = file_get_contents('http://wiki.scratch.mit.edu/w/api.php?action=query&titles=Scratch%20Wiki:Votes%20For%20Admin%202013&prop=revisions&rvprop=content&format=xml&salt=' . md5(time()));
$xml = new SimpleXMLElement($data);
$votes = (string) ($xml->query->pages->page->revisions->rev);
$lines = explode("\n", $votes);
$vote_lines = array();
foreach ($lines as $val) {
if (preg_match('%^\| *[A-z]%msi', $val)) {
$vote_lines[] = $val;
}
}
$votes = array();
foreach ($vote_lines as $val) {
preg_match_all('%\| *([^ |]+)%s', $val, $columns);
$voter = $columns[1][0];
unset($columns[1][0]);
$i = sizeof($columns[1]);
foreach ($columns[1] as $vote) {
if (!strstr($vote, 'N/A') && strpos($vote, '<') !== 0) {
if (isset($votes[strtolower($vote)])) {
$votes[strtolower($vote)] += $i;
} else {
$votes[strtolower($vote)] = $i;
}
$i--;
}
}
}
natsort($votes);
$votes = array_reverse($votes);
echo 'Scratch Wiki Vote Calculator - COPYRIGHT (C)2013 Jacob G.' . "\n";
echo 'There are currently ' . sizeof($vote_lines) . ' votes, and your results are...' . "\n";
$i = 0;
foreach ($votes as $key => $val) {
$i++;
printf('%2d:%15s with %5d points', $i, $key, $val);
echo "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment