CVS_History

Colin Viebrock colin@easydns.com
Mon, 22 Oct 2001 09:31:22 -0400


Here's that CVS history class I was talking about.  I'm pretty sure the only
thing wrong is that CVS expects some environment variables, and when the
webserver runs the script, those variables aren't there.

<?php

/**
 * CVSLib history class.
 *
 * @author  Colin Viebrock <colin@easydns.com>
 * @version $Revision: 1.1 $
 * @since   Chora 0.1
 * @package chora
 */

class CVSLib_History {

var $CVS, $history;


function CVSLib_History(&$rep) {
    $this->CVS = &$rep;
}

function getHistory($startdate=false, $enddate=false) {
    $this->history = array();
    $cmd = $this->CVS->conf['paths']['cvs'] . ' -d ' .
      $this->CVS->cvsRoot() . ' history -c';
    if ($startdate) {
        $cmd .= ' -D "' . date('r', $startdate) . '"';
    }
    echo "$cmd<BR>\n";

    if (!($pstream = popen($cmd, 'r'))) {
        return new CVSLib_Error(CVSLIB_INTERNAL_ERROR,
          "Failed to spawn cvs to retrieve history information");
    }

    while ($line = fgets($pstream, 4096)) {

        echo "$line<br>\n";    # debugging

        if (preg_match('/No records selected/i', $line)) {
            break;
        }

        $entry = array();

        /* strip off the type */

        $entry['type'] = substr($line,0,1);
        $line = substr($line,2);

        /* get the date */

        $entry['date'] = substr($line,0,22);
        $entry['timestamp'] = strtotime($entry['date']);
        $line = substr($line,23);

        /* skip if it's past enddate */

        if ($enddate && $entry['timestamp']>$enddate) {
            continue;
        }

        /* get the rest */

        $temp = preg_split('/[\s]+/', $line);
        $entry['user'] = $temp[0];
        $entry['revision'] = $temp[1];
        $entry['file'] = $temp[2];
        list($entry['module'], $entry['path']) = split('/', $temp[3], 2);
        $entry['full'] = $temp[3] . '/' . $temp[2];

        array_push($this->history, $entry);

    }

    pclose($pstream);
    if (sizeof($this->history)) {
        usort($this->history, array($this,'sortbyDate'));
        return true;
    }

    return false;

}


function sortbyDate($a, $b) {
    if ($a['timestamp'] == $b['timestamp']) {
        return 0;
    }
    return ($a['timestamp'] < $b['timestamp']) ? -1 : 1;
}


function numRows() {
    return sizeof($this->history);
}

function fetchAll() {
    return $this->history;
}

}