User:ShakataGaNai/tools/Category Counter

From Wikinews, the free news source you can write!
Jump to navigation Jump to search

Copy and save the code as SOMETHING.php.

CautionDo not use unless you know what you are doing!



<?php

$handle = fopen("master.csv", "w");

$start_date = strtotime("2004-11-09");
$end_date = strtotime("2009-09-12");

$a = 0;
while($start_date <= $end_date){
        $a++;
        $start_date = strtotime("+1 day", $start_date);
        $cat = date("F_j,_Y", $start_date);
        $short = date("Y-m-d", $start_date);

        $url = 'http://en.wikinews.org/w/api.php?action=query&prop=categoryinfo&format=xml&titles=Category:' . $cat;
        $page = curlIt($url);
        if(preg_match("/<categoryinfo size=\"(.*)\" pages=\"(.*)\" files=\"(.*)\" subcats=\"(.*)\" \/>/", $page, $ray)){
                $it = $ray[2];
        }else{
                $it = 0;
        }
        print "$short,$it\n";
        fwrite($handle, "$short,$it\n");
        sleep(1);
}

fclose($handle);
print "DONE - $a\n\n";

function curlIt($url, $unpwd="", $post=""){
        $UA = "Mozilla/5.0 (compatible; WikinewsBot)";
        $ch = curl_init($url);
        //curl_setopt($ch, CURLOPT_VERBOSE, 1); //--Debug only
        if($unpwd != ""){
                curl_setopt($ch, CURLOPT_USERPWD, $unpwd);
        }
        if($post != ""){
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        }
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_USERAGENT, $UA);
        ob_start();
        curl_exec($ch);
        $retHTML = ob_get_contents();
        ob_end_clean();

        return($retHTML);
        curl_close($ch);
}