Jump to content

User:SQL/mcount.php

From Meta, a Wikimedia project coordination wiki
<?php
$version = "0.0.9";
$urlbase1='http://';
$urlbase2='.wikipedia.org/w/api.php?';
$aurlq='action=query&list=allusers&augroup=sysop&aulimit=500';

$query = $urlbase1 . $argv[1] . $urlbase2 . $aurlq;

echo "Meta Countadmin $version\r\n";
echo "Getting list of stewards from meta.\r\n";
$stew_arr = file('http://meta.wikimedia.org/w/api.php?action=query&list=allusers&augroup=steward&aulimit=500&format=php');
$stewards_raw = unserialize($stew_arr[0]);
$stewards_lessraw = $stewards_raw[query][allusers];
$stewards = array();
foreach($stewards_lessraw as $steward) {
        array_push($stewards, $steward[name]);
}
$ladmin = file($query);
$na = 0;
function getit ($query) {
        $ladmin = array();
        $input = fopen($query, "r");
            while (!feof($input)) {
                $line = fgets($input, 4096);
                $line = urldecode($line);
                $line = htmlspecialchars_decode($line);
        array_push($ladmin, $line);
    }
    fclose($input);
        return($ladmin);
}
$g = 0;
echo "Processing list of admins on $argv[1].wikipedia.org\r\n";

while ($alldone != 1) {
        if($doneonce = 1) {
                $query2 = $query . '&' . $addon;
                $ladmin = getit($query2);
                echo "G$g ";
                $g++;
        } else {
                $ladmin = getit($query);
                $g++;
        }
        $matches = preg_grep('/\<span style\=\"color\:blue\;\"\>\<allusers aufrom\=\"(.*)\" \/\>\<\/span\>/i', $ladmin);
        preg_match('/\<span style\=\"color\:blue\;\"\>\<allusers (aufrom\=\"(.*)\") \/\>\<\/span\>/i', $matches[17], $match2);
        $addon = $match2[1];
        $addon = preg_replace('/\"/', "", $addon);
        if ($match2[1] == ""){
                $alldone = 1;
        }
        foreach ($ladmin as $line_num => $line) {
        if(preg_match('/name/i', $line)) {
                $na++;
        }
        foreach($stewards as $steward) {
                $sname = '/' . $steward . '/' . "i";
                if(preg_match($sname, $line)) {
                        $na = $na - 1;
                }
        }
        }
        $doneonce = 1;
}
//Get # of crats
$aurlq='action=query&list=allusers&augroup=bureaucrat&aulimit=500';

$query = $urlbase1 . $argv[1] . $urlbase2 . $aurlq;

$g = 0;
echo "\r\nProcessing list of bureaucrats on $argv[1].wikipedia.org\r\n";
$alldone = 0;
$doneonce = 0;

while ($alldone != 1) {
        if($doneonce = 1) {
                $query2 = $query . '&' . $addon;
                $ladmin = getit($query2);
                echo "G$g ";
                $g++;
        } else {
                $ladmin = getit($query);
                $g++;
        }
        $matches = preg_grep('/\<span style\=\"color\:blue\;\"\>\<allusers aufrom\=\"(.*)\" \/\>\<\/span\>/i', $ladmin);
        preg_match('/\<span style\=\"color\:blue\;\"\>\<allusers (aufrom\=\"(.*)\") \/\>\<\/span\>/i', $matches[17], $match2);
        $addon = $match2[1];
        $addon = preg_replace('/\"/', "", $addon);
        if ($match2[1] == ""){
                $alldone = 1;
        }
        foreach ($ladmin as $line_num => $line) {
        if(preg_match('/name/i', $line)) {
                $nc++;
        }
        }
        $doneonce = 1;
}
//Get # of articles
echo "\r\nGetting number of articles on $argv[1].wikipedia.org\r\n";
echo "Getting number of users on $argv[1].wikipedia.org\r\n";
$urlstart= 'http://';
$urlend = '.wikipedia.org/wiki/Special:Statistics?action=raw';
$urlname = $argv[1];

$url = $urlstart . $urlname . $urlend;

$input = file($url);

foreach ($input as $line_num => $line) {
preg_match('/good\=(.*)\;views/', $line, $matches);
$articles = $matches[1];
preg_match('/users\=(.*)\;admins/', $line, $umatches);
$users = $umatches[1];
}

echo "\r\nNumber of Admins: $na\r\n";
echo "\r\nNumber of Crats: $nc\r\n";
echo "\r\nNumber of Users: $users\r\n";
echo "\r\nNumber of Articles: $articles\r\n";
?>