#!/usr/bin/php
<?php
if (count($argv) == 1) {
    $limit = 20;
} elseif (count($argv) == 2) {
    $limit = $argv[0];
} else {
    echo "usage: $argv[0] [limit]\n";
    exit;
}

$wsdl = getenv('PHPWIKI_WSDL_URL');
if ($wsdl === false) {
    $wsdl = "http://phpwiki.fr/PhpWiki.wsdl";
}

try {
    $client = new SoapClient($wsdl);
} catch (SoapFault $fault) {
    die($fault->faultstring);
}

$phpwiki = getenv("HOME")."/.phpwiki";
if (!file_exists($phpwiki)) {
    $login = readline("Login: ");
    $password = readline("Password: ");
    $credentials = base64_encode($login.':'.$password);
    if ($fp = fopen($phpwiki, 'w')) {
        fprintf($fp, "%s:%s", $login, $password);
        fclose($fp);
        chmod($phpwiki, 0600);
    }
} else {
    $credentials = base64_encode(file_get_contents($phpwiki));
}

try {
    $changes = $client->getRecentChanges($limit, $credentials);
    foreach ($changes as $change) {
        echo "Pagename: ".$change['pagename']."\n";
        echo "Last modified: ".$change['lastModified']."\n";
        echo "Author: ".$change['author']."\n";
        echo "Summary: ".$change['summary']."\n";
        echo "Version: ".$change['version']."\n";
        echo "\n";
    }

} catch (SoapFault $e) {
    echo 'Error: ' .  $e->getMessage() . "\n";
}
