<?php
/**
 * This file is basically used to parse the svn
 * logs saved as xml (svn log --xml > file). This gives
 * you the ability to find logs per user with the date
 * of their commits, the message of it and the user of course.
 *
 * @license   LGPL-CC
 * @copyright Agora Production 2000-2007
 * @author    David Coallier <davidc@agoraproduction.com>
 *
 * @param string $argv[0]          The name of the php execution script
 * @param string $argv[1]          The name of the xml file to parse (The log)
 * @param string $argv[2]          The username to search for
 * @param string $argv[3]          The file to store the result in.
 * @param string optional $argv[4] What to do (append or overwrite) default is append.
 */

/**
 * We have to make sure the number of variables
 * - arguments passed to the script is 3 (at least)
 */
if ($argv[1] == '--help' || count($argv) < 3) {
    die(
"Usage: php {$argv[0]} file.xml usernameToParse fileToStoreResult [append|ovewrrite]\n");
}

$file     $argv[1];
$username $argv[2];
$newFile  $argv[3];

/**
 * Default action is appending.
 */
$action FILE_APPEND;

if (isset(
$argv[4])) {
    switch (
strtolower($argv[4])) {
        case 
'append':
            
$action FILE_APPEND;
            break;
        case 
'overwrite':
            
$action false;
            break;
        default: 
            
$action FILE_APPEND;
            break;
    }
}

/**
 * Check if the file exists, if not then throw
 * an exception.
 *
 * @throw new Exception File doesn't exist.
 */
try {
    if (!
file_exists($file)) {
        throw new 
Exception("File : $file does not exist");
    }
} catch (
Exception $e) {
    die(
$e->getMessage());
}

$xml = new SimpleXMLElement(file_get_contents($file));

/**
 * Initialize array $entries
 */
$entries = array();

foreach (
$xml->logentry as $entry) {
    
$tmp = array();
    if (
$entry->author == $username) {
        
$tmp['author'] = $entry->author;
        
$tmp['date']   = $entry->date;
        
$tmp['msg']    = $entry->msg;

        
$entries[] = $tmp;
    }
}


$fullString '';
foreach (
$entries as $key => $entry) {
    
$str  "Author         : {$entry['author']}\n";
    
$str .= "Date           : {$entry['date']}\n";
    
$str .= "Commit Message : {$entry['msg']}\n";
    
$str .="\n\n";

    
$fullString .= $str;
}
file_put_contents($newFile$fullString$action);
?>