Advertisement
jvvg

Wiki Counter

Sep 30th, 2014
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.27 KB | None | 0 0
  1. <?php
  2. $data = file_get_contents('http://wiki.scratch.mit.edu/w/api.php?action=query&titles=' . rawurlencode('Scratch Wiki:Elections/October 2014') . '&prop=revisions&rvprop=content&format=xml&salt=' . md5(time()));
  3. $xml = new SimpleXMLElement($data);
  4. $votes = (string) ($xml->query->pages->page->revisions->rev);
  5. $lines = explode("\n", $votes);
  6. $vote_lines = array();
  7. foreach ($lines as $val) {
  8.     $val = trim($val);
  9.     if (strpos($val, '|') === 0 && strlen($val) > 2) {
  10.         $vote_lines[] = $val;
  11.     }
  12. }
  13. $votes = array();
  14. foreach ($vote_lines as $val) {
  15.     preg_match_all('%\| *([^ |]+)%s', $val, $columns);
  16.     $voter = $columns[1][0];
  17.     unset($columns[1][0]);
  18.     $i = sizeof($columns[1]);
  19.     foreach ($columns[1] as $vote) {
  20.         if (!strstr($vote, 'N/A') && strpos($vote, '<') !== 0) {
  21.             if (isset($votes[strtolower($vote)])) {
  22.                 $votes[strtolower($vote)] += $i;
  23.             } else {
  24.                 $votes[strtolower($vote)] = $i;
  25.             }
  26.             $i--;
  27.         }
  28.     }
  29. }
  30. natsort($votes);
  31. $votes = array_reverse($votes);
  32. echo 'Scratch Wiki Vote Calculator - COPYRIGHT (C)2014 Jacob G.' . "\n";
  33. echo 'There are currently ' . sizeof($vote_lines) . ' votes, and your results are...' . "\n";
  34. $i = 0;
  35. foreach ($votes as $key => $val) {
  36.     $i++;
  37.     printf('%2d:%15s with %5d points', $i, $key, $val);
  38.     echo "\n";
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement