root > WikiSense-trunk > util > tickerLogGrab.php

tickerLogGrab.php

application/x-php, 1117 bytes (load raw)
<?php


function urlencodeTitle($title) {
    return str_replace(
        array( '%3A', '%2F', '+' ),
        array( ':', '/', '_' ),
        urlencode( $title )
    );
}

$from = NULL;
$suffix = "";
$f = "php://stdin";
if (sizeof($argv)>1) $f = $argv[1];
if (sizeof($argv)>2) $from = $argv[2];
if (sizeof($argv)>3) $suffix = $argv[3];

$h = fopen($f, 'r');
if (!$h) die("failed to open $f!\n");

$i = 0;

while (true) {
        $s = fgets($h);
        if ($s===false || strlen($s)===0) break;

        $i++;

        $s = trim($s);
        if (strlen($s)===0) continue;

        if ($from) {
                if (preg_match($from, $s)) {
                        $from = NULL;
                        print " - start tag found in line $i\n";
                }
                continue;
        }

        if (preg_match('!^== .* ==$!', $s, $m)) {
                $domain = NULL;
                print " - separator found in line $i\n";
        }
        else if (preg_match('!^analyzing for ([-.\w\d]+),!', $s, $m)) {
                $domain = $m[1];
                print " - domain found in line $i: $domain\n";
        }
        else if ($domain && preg_match('!^- data posted to \[\[(.*)\]\]!', $s, $m)) {
                $title = $m[1];
                $u = "http://" . $domain . "/wiki/" . urlencodeTitle($title) . $suffix;
                print $u . "\n";
        }
}

fclose($h);

?>