cvs: pear /File_Archive Archive.php /File_Archive/Archive/Predicate Index.php /File_Archive/Archive/Writer UniqueAppender.php

From: Date: Mon, 30 May 2005 19:44:54 +0000
Subject: cvs: pear /File_Archive Archive.php /File_Archive/Archive/Predicate Index.php /File_Archive/Archive/Writer UniqueAppender.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-32254@lists.php.net to get a copy of this message
vincentlascaux Mon May 30 15:44:54 2005 EDT Added files: /pear/File_Archive/Archive/Predicate Index.php /pear/File_Archive/Archive/Writer UniqueAppender.php Modified files: /pear/File_Archive Archive.php Log: + Add an predicate to select files by their position in the archive + Add a writer wrapper called UniqueAppender that appends to a reader and delete the duplicates it created at the end + File_Archive::append is now called appender and takes a parameter to indicate whether you want to delete the files that has the same name in the existing archive to prevent duplication of files

http://cvs.php.net/diff.php/pear/File_Archive/Archive.php?r1=1.57&r2=1.58&ty=u Index: pear/File_Archive/Archive.php diff -u pear/File_Archive/Archive.php:1.57 pear/File_Archive/Archive.php:1.58 --- pear/File_Archive/Archive.php:1.57 Mon May 30 11:25:14 2005 +++ pear/File_Archive/Archive.php Mon May 30 15:44:52 2005 @@ -27,7 +27,7 @@ * @author Vincent Lascaux <vincentlascaux@php.net> * @copyright 1997-2005 The PHP Group * @license http://www.gnu.org/copyleft/lesser.html LGPL - * @version CVS: $Id: Archive.php,v 1.57 2005/05/30 15:25:14 vincentlascaux Exp $ + * @version CVS: $Id: Archive.php,v 1.58 2005/05/30 19:44:52 vincentlascaux Exp $ * @link http://pear.php.net/package/File_Archive */ @@ -912,14 +912,19 @@ * * @param File_Archive_Reader $source A reader where some files will be appended * @param string $URL URL to reach the archive in the source. - * if $URL is null, a writer to append files to the $source reader will be returned + * if $URL is null, a writer to append files to the $source reader will + * be returned + * @param bool $unique If true, the duplicate files will be deleted on close + * Default is false (and setting it to true may have some performance + * consequences) * @param string $type Extension of the archive (or null to use the one in the URL) - * @param array $stat Used only if archive is created, array of stat as returned by PHP stat function - * or Reader getStat function: stats of the archive) + * @param array $stat Used only if archive is created, array of stat as returned + * by PHP stat function or Reader getStat function: stats of the archive) * Time (index 9) will be overwritten to current time * @return File_Archive_Writer a writer that you can use to append files to the reader */ - function appendToSource(&$source, $URL = null, $type = null, $stat = array()) + function appenderFromSource(&$source, $URL = null, $unique = false, + $type = null, $stat = array()) { if (PEAR::isError($source)) { return $source; @@ -944,7 +949,12 @@ PEAR::popErrorHandling(); if (!PEAR::isError($result)) { - return $result->makeAppendWriter(); + if ($unique) { + require_once "File/Archive/Writer/UniqueAppender.php"; + return new File_Archive_Writer_UniqueAppender($result); + } else { + return $result->makeAppendWriter(); + } } //The source can't be found and has to be created @@ -967,10 +977,10 @@ * @param File_Archive_Reader $source A reader where some files will be appended * @return File_Archive_Writer a writer that you can use to append files to the reader */ - function append($URL, $type = null, $stat = array()) + function appender($URL, $unique = false, $type = null, $stat = array()) { $source = null; - return File_Archive::appendToSource($source, $URL, $type, $stat); + return File_Archive::appenderFromSource($source, $URL, $unique, $type, $stat); } /** @@ -1034,8 +1044,10 @@ } require_once "File/Archive/Predicate/Duplicate.php"; + $pred = new File_Archive_Predicate_Duplicate($source); + $source->close(); return File_Archive::removeFromSource( - new File_Archive_Predicate_Duplicate($source), + $pred, $source, null ); http://cvs.php.net/co.php/pear/File_Archive/Archive/Predicate/Index.php?r=1.1&p=1 Index: pear/File_Archive/Archive/Predicate/Index.php +++ pear/File_Archive/Archive/Predicate/Index.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * Evaluates to true if the index is in a given array of indexes * The array has the indexes in key (so you may want to call * array_flip if your array has indexes as value) * * PHP versions 4 and 5 * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA * * @category File Formats * @package File_Archive * @author Vincent Lascaux <vincentlascaux@php.net> * @copyright 1997-2005 The PHP Group * @license http://www.gnu.org/copyleft/lesser.html LGPL * @version CVS: $Id: Index.php,v 1.1 2005/05/30 19:44:53 vincentlascaux Exp $ * @link http://pear.php.net/package/File_Archive */ require_once "File/Archive/Predicate.php"; /** * Evaluates to true if the index is in a given array of indexes * The array has the indexes in key (so you may want to call * array_flip if your array has indexes as value) */ class File_Archive_Predicate_Index extends File_Archive_Predicate { var $indexes; var $pos = 0; /** * @param $extensions array or comma separated string of allowed extensions */ function File_Archive_Predicate_Index($indexes) { $this->indexes = $indexes; } /** * @see File_Archive_Predicate::isTrue() */ function isTrue(&$source) { return isset($this->indexes[$this->pos++]); } } ?> http://cvs.php.net/co.php/pear/File_Archive/Archive/Writer/UniqueAppender.php?r=1.1&p=1 Index: pear/File_Archive/Archive/Writer/UniqueAppender.php +++ pear/File_Archive/Archive/Writer/UniqueAppender.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * A writer wrapper that will remove the files the eventual duplicate * files from the reader to keep only the new ones * When calling newFile, the file with the highest index in the reader * and the same filename will be removed * Note that it ensure that the archive won't have duplicates only if * it didn't have duplicates before, and if no two calls to newFile with * the same filename is done * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA * * @category File Formats * @package File_Archive * @author Vincent Lascaux <vincentlascaux@php.net> * @copyright 1997-2005 The PHP Group * @license http://www.gnu.org/copyleft/lesser.html LGPL * @version CVS: $Id: UniqueAppender.php,v 1.1 2005/05/30 19:44:53 vincentlascaux Exp $ * @link http://pear.php.net/package/File_Archive */ require_once "File/Archive/Writer.php"; require_once "File/Archive/Reader.php"; require_once "File/Archive/Predicate/Index.php"; /** * A writer wrapper that will remove the files the eventual duplicate * files from the reader to keep only the new ones * If there were already some duplications in the provided reader, not * all duplication will be removed * If you use newFile with the same filename several file, only the latest * write will be kept (no time comparision is done) */ class File_Archive_Writer_UniqueAppender extends File_Archive_Writer { var $reader; var $writer; var $fileList = array(); var $toDelete = array(); /** * Construct a unique writer that will write to the specified writer * and remove duplicate files from the reader on close */ function File_Archive_Writer_UniqueAppender(&$reader) { $reader->close(); $pos = 0; while ($reader->next()) { $this->fileList[$reader->getFilename()] = $pos++; } $this->reader =& $reader; $this->writer = $reader->makeAppendWriter(); } /** * @see File_Archive_Writer::newFile() */ function newFile($filename, $stat = array(), $mime = "application/octet-stream") { if (isset($this->fileList[$filename])) { $this->toDelete[$this->fileList[$filename]] = true; } return $this->writer->newFile($filename, $stat, $mime); } /** * @see File_Archive_Writer::newFromTempFile() */ function newFromTempFile($tmpfile, $filename, $stat = array(), $mime = "application/octet-stream") { if (isset($this->fileList[$filename])) { $this->toDelete[$this->fileList[$filename]] = true; } return $this->writer->newFromTempFile($tmpfile, $filename, $stat, $mime); } /** * @see File_Archive_Writer::newFileNeedsMIME() */ function newFileNeedsMIME() { return $this->writer->newFileNeedsMIME(); } /** * @see File_Archive_Writer::writeData() */ function writeData($data) { return $this->writer->writeData($data); } /** * @see File_Archive_Writer::writeFile() */ function writeFile($filename) { return $this->writer->writeFile($filename); } /** * Close the writer, eventually flush the data, write the footer... * This function must be called before the end of the script */ function close() { $error = $this->writer->close(); if (PEAR::isError($error)) { return $error; } if (!empty($this->toDelete)) { $tmp = $this->reader->makeWriterRemoveFiles( new File_Archive_Predicate_Index($this->toDelete) ); if (PEAR::isError($tmp)) { return $tmp; } return $tmp->close(); } } } ?>
« previous php.pear.cvs (#32254) next »