* @copyright 1997-2005 The PHP Group * @license http://www.gnu.org/copyleft/lesser.html LGPL * @version CVS: $Id: Duplicate.php,v 1.1 2005/05/30 17:18:11 vincentlascaux Exp $ * @link http://pear.php.net/package/File_Archive */ require_once "File/Archive/Predicate.php"; /** * Evaluates to true if a for the files for which a newer version * can be found in a specified archive * Comparison is by default made on dates of the files, or position * in the archive (if two files have the same date or the date of a * file is not specified). */ class File_Archive_Predicate_Duplicate extends File_Archive_Predicate { /** * @var array Key is the filename, value is an array of date (index 0) and * position in the archive (index) 1 of the newest entry with this filename */ var $newest = array(); /** * @var int The current position of the file in the source */ var $pos = 0; /** * @param File_Archive_Reader $source The source will be inspected to find * the date of old files * The predicate should then be used on the same source to remove the * old duplicate files */ function File_Archive_Predicate_Duplicate(&$source) { //Ensure we are at the begining of the file $source->close(); $pos = 0; while ($source->next()) { $filename = $source->getFilename(); $stat = $source->getStat(); $value = isset($this->newest[$filename]) ? $this->newest[$filename] : null; if ($value === null || $this->compare($stat[9], $value[0]) >= 0 ) { $this->newest[$filename] = array($stat[9], $pos); } $pos++; } } /** * Compare the dates of two files. null is considered infinitely old * * @return int < 0 if $a can be considered older than $b * = 0 if $a and $b can be considered same age * > 0 if $a can be considered newer than $b */ function compare($a, $b) { return ($a === null ? -1 : $a) - ($b === null ? -1 : $b); } /** * @see File_Archive_Predicate::isTrue() */ function isTrue(&$source) { $filename = $source->getFilename(); $stat = $source->getStat(); $value = isset($this->newest[$filename]) ? $this->newest[$filename] : null; if ($value === null) { $delete = false; } else { $comp = $this->compare($stat[9], $value[0]); $delete = $comp < 0 || ($comp == 0 && $this->pos != $value[1]); } $this->pos++; return $delete; } } ?>