? php5.diff.txt ? phpcs.diff.txt Index: SearchReplace.php =================================================================== RCS file: /repository/pear/File_SearchReplace/SearchReplace.php,v retrieving revision 1.16 diff -u -r1.16 SearchReplace.php --- SearchReplace.php 27 Oct 2008 15:48:58 -0000 1.16 +++ SearchReplace.php 29 Oct 2008 15:57:41 -0000 @@ -1,53 +1,53 @@ | -// +-----------------------------------------------------------------------+ -// -// $Id: SearchReplace.php,v 1.16 2008/10/27 15:48:58 clockwerx Exp $ -// -// Search and Replace Utility -// +/** + * Copyright (c) 2002-2005, Richard Heyes + * All rights reserved. + * + * PHP version 4, 5 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * o Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * o Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * o The names of the authors may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * @category File + * @package File_SearchReplace + * @author Richard Heyes + * @version CVS: $Id: SearchReplace.php,v 1.16 2008/10/27 15:48:58 clockwerx Exp $ + * @link http://pear.php.net/File_SearchReplace + */ /** * Search and Replace Utility * - * - * @author Richard Heyes - * @version 1.0 - * @package File + * @category File + * @package File_SearchReplace + * @author Richard Heyes + * @link http://pear.php.net/File_SearchReplace */ class File_SearchReplace { - + // {{{ Properties (All private) var $find; @@ -68,28 +68,32 @@ /** * Sets up the object * + * @param string $find The string/regex to find. + * @param string $replace The string/regex to replace $find with. + * @param array $files The file(s) to perform this operation on. + * @param array $directories The directories to perform this operation on. + * @param bool $include_subdir If performing on directories, whether to + * traverse subdirectories. + * @param array $ignore_lines Ignore lines beginning with any of the strings + * in this array. This + * feature only works with the "normal" search. + * * @access public - * @param string $find The string/regex to find. - * @param string $replace The string/regex to replace $find with. - * @param array $files The file(s) to perform this operation on. - * @param array $directories (optional) The directories to perform this operation on. - * @param bool $include_subdir If performing on directories, whether to traverse subdirectories. - * @param array $ignore_lines Ignore lines beginning with any of the strings in this array. This - * feature only works with the "normal" search. - */ - function File_SearchReplace($find, $replace, $files, $directories = '', $include_subdir = TRUE, $ignore_lines = array()) - { - - $this->find = $find; - $this->replace = $replace; - $this->files = $files; - $this->directories = $directories; - $this->include_subdir = $include_subdir; - $this->ignore_lines = (array) $ignore_lines; + */ + function File_SearchReplace($find, $replace, $files, $directories = '', + $include_subdir = true, $ignore_lines = array()) + { + + $this->find = $find; + $this->replace = $replace; + $this->files = $files; + $this->directories = $directories; + $this->include_subdir = $include_subdir; + $this->ignore_lines = (array) $ignore_lines; $this->occurences = 0; $this->search_function = 'search'; - $this->php5 = (substr(PHP_VERSION, 0, 1) == 5) ? TRUE : FALSE; + $this->php5 = substr(PHP_VERSION, 0, 1) == 5; $this->last_error = ''; } @@ -128,8 +132,10 @@ /** * Accessor for setting find variable. * - * @access public * @param string $find The string/regex to find. + * + * @access public + * @return void */ function setFind($find) { @@ -142,8 +148,11 @@ /** * Accessor for setting replace variable. * + * @param string $replace The string/regex to replace the find + * string/regex with. + * * @access public - * @param string $replace The string/regex to replace the find string/regex with. + * @return void */ function setReplace($replace) { @@ -156,8 +165,10 @@ /** * Accessor for setting files variable. * - * @access public * @param array $files The file(s) to perform this operation on. + * + * @access public + * @return void */ function setFiles($files) { @@ -170,8 +181,10 @@ /** * Accessor for setting directories variable. * - * @access public * @param array $directories The directories to perform this operation on. + * + * @access public + * @return void */ function setDirectories($directories) { @@ -184,8 +197,10 @@ /** * Accessor for setting include_subdir variable. * - * @access public * @param bool $include_subdir Whether to traverse subdirectories or not. + * + * @access public + * @return void */ function setIncludeSubdir($include_subdir) { @@ -198,9 +213,12 @@ /** * Accessor for setting ignore_lines variable. * - * @access public - * @param array $ignore_lines Ignore lines beginning with any of the strings in this array. This + * @param array $ignore_lines Ignore lines beginning with any of the + * strings in this array. This * feature only works with the "normal" search. + * + * @access public + * @return void */ function setIgnoreLines($ignore_lines) { @@ -213,34 +231,45 @@ /** * Function to determine which search function is used. * + * Can be any one of: + * normal - Default search. Goes line by line. Ignore lines feature + * only works with this type. + * quick - Uses str_replace for straight replacement throughout + * file. Quickest of the lot. + * preg - Uses preg_replace(), so any valid regex + * ereg - Uses ereg_replace(), so any valid regex + * + * @param string $search_function The search function that should be used. + * * @access public - * @param string The search function that should be used. Can be any one of: - * normal - Default search. Goes line by line. Ignore lines feature only works with this type. - * quick - Uses str_replace for straight replacement throughout file. Quickest of the lot. - * preg - Uses preg_replace(), so any regex valid with this function is valid here. - * ereg - Uses ereg_replace(), so any regex valid with this function is valid here. + * @return void */ function setSearchFunction($search_function) { switch($search_function) { - case 'normal': $this->search_function = 'search'; - return TRUE; + case 'normal': + $this->search_function = 'search'; + return true; break; - case 'quick' : $this->search_function = 'quickSearch'; - return TRUE; + case 'quick' : + $this->search_function = 'quickSearch'; + return true; break; - case 'preg' : $this->search_function = 'pregSearch'; - return TRUE; + case 'preg' : + $this->search_function = 'pregSearch'; + return true; break; - case 'ereg' : $this->search_function = 'eregSearch'; - return TRUE; + case 'ereg' : + $this->search_function = 'eregSearch'; + return true; break; - default : $this->last_error = 'Invalid search function specified'; - return FALSE; + default : + $this->last_error = 'Invalid search function specified'; + return false; break; } } @@ -251,10 +280,12 @@ /** * Default ("normal") search routine. * - * @access private * @param string $filename The filename to search and replace upon. - * @return array Will return an array containing the new file contents and the number of occurences. - * Will return FALSE if there are no occurences. + * + * @access private + * @return array Will return an array containing the new file contents + * and the number of occurences. + * Will return false if there are no occurences. */ function search($filename) { @@ -262,11 +293,12 @@ $file_array = file($filename); // just for the sake of catching occurences - $local_find = array_values((array) $this->find); - $local_replace = (is_array($this->replace)) ? array_values($this->replace) : $this->replace; + $local_find = $this->_getFind(); + $local_replace = $this->_getReplace(); if (empty($this->ignore_lines) && $this->php5) { // PHP5 acceleration - $file_array = str_replace($local_find, $local_replace, $file_array, $occurences); + $file_array = str_replace($local_find, $local_replace, + $file_array, $occurences); } else { // str_replace() doesn't return number of occurences in PHP4 // so we need to count them manually and/or filter strings @@ -274,34 +306,45 @@ - for ($i=0; $i < count($file_array); $i++) { + foreach ($file_array as $i => $file) { if ($ignore_lines_num > 0) { - for ($j=0; $j < $ignore_lines_num; $j++) { - if (substr($file_array[$i],0,strlen($this->ignore_lines[$j])) == $this->ignore_lines[$j]) continue 2; + for ($j = 0; $j < $ignore_lines_num; $j++) { + $text = substr($file, 0, strlen($this->ignore_lines[$j])); + if ($text == $this->ignore_lines[$j]) { + continue 2; + } } } if ($this->php5) { - $file_array[$i] = str_replace($this->find, $this->replace, $file_array[$i], $counted); + $file_array[$i] = str_replace($local_find, $local_replace, + $file, $counted); + $occurences += $counted; } else { foreach ($local_find as $fk => $ff) { - $occurences += substr_count($file_array[$i], $ff); + $occurences += substr_count($file, $ff); if (!is_array($local_replace)) { $fr = $local_replace; } else { - $fr = (isset($local_replace[$fk])) ? $local_replace[$fk] : ""; + $fr = ""; + if (isset($local_replace[$fk])) { + $fr = $local_replace[$fk]; + } } - $file_array[$i] = str_replace($ff, $fr, $file_array[$i]); + $file_array[$i] = str_replace($ff, $fr, $file); } } } } - if ($occurences > 0) $return = array($occurences, implode('', $file_array)); else $return = FALSE; - return $return; + if ($occurences > 0) { + return array($occurences, implode('', $file_array)); + } + + return false; } // }}} @@ -310,21 +353,24 @@ /** * Quick search routine. * - * @access private * @param string $filename The filename to search and replace upon. - * @return array Will return an array containing the new file contents and the number of occurences. - * Will return FALSE if there are no occurences. + * + * @access private + * @return array Will return an array containing the new file contents + * and the number of occurences. + * Will return false if there are no occurences. */ function quickSearch($filename) { clearstatcache(); - $file = fread($fp = fopen($filename, 'r'), max(1, filesize($filename))); fclose($fp); - $local_find = array_values((array) $this->find); - $local_replace = (is_array($this->replace)) ? array_values($this->replace) : $this->replace; + $file = file_get_contents($filename); + + $local_find = $this->_getFind(); + $local_replace = $this->_getReplace(); - $occurences = 0; + $occurences = 0; // logic is the same as in str_replace function with one exception: // if is a string and is an array - substitution @@ -335,6 +381,7 @@ if ($this->php5) { $file = str_replace($this->find, $this->replace, $file, $counted); + $occurences += $counted; } else { foreach ($local_find as $fk => $ff) { @@ -342,14 +389,17 @@ if (!is_array($local_replace)) { $fr = $local_replace; } else { - $fr = (isset($local_replace[$fk])) ? $local_replace[$fk] : ""; + $fr = isset($local_replace[$fk]) ? $local_replace[$fk] : ""; } $file = str_replace($ff, $fr, $file); } } - if ($occurences > 0) $return = array($occurences, $file); else $return = FALSE; - return $return; + if ($occurences > 0) { + return array($occurences, $file); + } + + return false; } @@ -359,34 +409,40 @@ /** * Preg search routine. * - * @access private * @param string $filename The filename to search and replace upon. - * @return array Will return an array containing the new file contents and the number of occurences. - * Will return FALSE if there are no occurences. + * + * @access private + * @return array Will return an array containing the new file contents + * and the number of occurences. + * Will return false if there are no occurences. */ function pregSearch($filename) { clearstatcache(); - $file = fread($fp = fopen($filename, 'r'), max(1, filesize($filename))); fclose($fp); - $local_find = array_values((array) $this->find); - $local_replace = (is_array($this->replace)) ? array_values($this->replace) : $this->replace; + $file = file_get_contents($filename); + + $local_find = $this->_getFind(); + $local_replace = $this->_getReplace(); $occurences = 0; - foreach($local_find as $fk => $ff) { + foreach ($local_find as $fk => $ff) { $occurences += preg_match_all($ff, $file, $matches); if (!is_array($local_replace)) { $fr = $local_replace; } else { - $fr = (isset($local_replace[$fk])) ? $local_replace[$fk] : ""; + $fr = isset($local_replace[$fk]) ? $local_replace[$fk] : ""; } $file = preg_replace($ff, $fr, $file); } - if ($occurences > 0) $return = array($occurences, $file); else $return = FALSE; - return $return; + if ($occurences > 0) { + return array($occurences, $file); + } + + return false; } @@ -396,54 +452,61 @@ /** * Ereg search routine. * - * @access private * @param string $filename The filename to search and replace upon. - * @return array Will return an array containing the new file contents and the number of occurences. - * Will return FALSE if there are no occurences. + * + * @access private + * @return array Will return an array containing the new file contents + * and the number of occurences. + * Will return false if there are no occurences. */ function eregSearch($filename) { clearstatcache(); - $file = fread($fp = fopen($filename, 'r'), max(1, filesize($filename))); fclose($fp); - $local_find = array_values((array) $this->find); - $local_replace = (is_array($this->replace)) ? array_values($this->replace) : $this->replace; + $file = file_get_contents($filename); + + $local_find = $this->_getFind(); + $local_replace = $this->_getReplace(); $occurences = 0; - foreach($local_find as $fk => $ff) { + foreach ($local_find as $fk => $ff) { $occurences += count(split($ff, $file)) - 1; if (!is_array($local_replace)) { $fr = $local_replace; } else { - $fr = (isset($local_replace[$fk])) ? $local_replace[$fk] : ""; + $fr = isset($local_replace[$fk]) ? $local_replace[$fk] : ""; } $file = ereg_replace($ff, $fr, $file); } - if ($occurences > 0) $return = array($occurences, $file); else $return = FALSE; - return $return; + if ($occurences > 0) { + return array($occurences, $file); + } + return false; } // }}} // {{{ writeout() - + /** * Function to writeout the file contents. * - * @access private * @param string $filename The filename of the file to write. * @param string $contents The contents to write to the file. + * + * @access private + * @return void */ function writeout($filename, $contents) { if ($fp = @fopen($filename, 'w')) { - flock($fp,2); + flock($fp, 2); fwrite($fp, $contents); - flock($fp,3); + flock($fp, 3); fclose($fp); } else { $this->last_error = 'Could not open file: '.$filename; @@ -457,18 +520,29 @@ /** * Function called by doSearch() to go through any files that need searching. * - * @access private * @param string $ser_func The search function to use. + * + * @access private + * @return void */ function doFiles($ser_func) { - if (!is_array($this->files)) $this->files = explode(',', $this->files); - for ($i=0; $ifiles); $i++) { - if ($this->files[$i] == '.' OR $this->files[$i] == '..') continue; - if (is_dir($this->files[$i]) == TRUE) continue; - $newfile = $this->$ser_func($this->files[$i]); - if (is_array($newfile) == TRUE){ - $this->writeout($this->files[$i], $newfile[1]); + if (!is_array($this->files)) { + $this->files = explode(',', $this->files); + } + + foreach ($this->files as $file) { + if ($file == '.' OR $file == '..') { + continue; + } + + if (is_dir($file)) { + continue; + } + + $newfile = $this->$ser_func($file); + if (is_array($newfile)) { + $this->writeout($file, $newfile[1]); $this->occurences += $newfile[0]; } } @@ -478,31 +552,39 @@ // {{{ doDirectories() /** - * Function called by doSearch() to go through any directories that need searching. + * Function called by doSearch() to go through any directories that + * need searching. * - * @access private * @param string $ser_func The search function to use. + * + * @access private + * @return void */ function doDirectories($ser_func) { - if (!is_array($this->directories)) $this->directories = explode(',', $this->directories); - for ($i=0; $idirectories); $i++) { - $dh = opendir($this->directories[$i]); + if (!is_array($this->directories)) { + $this->directories = explode(',', $this->directories); + } + + foreach ($this->directories as $directory) { + $dh = opendir($directory); while ($file = readdir($dh)) { - if ($file == '.' OR $file == '..') continue; + if ($file == '.' OR $file == '..') { + continue; + } - if (is_dir($this->directories[$i].$file) == TRUE) { - if ($this->include_subdir == TRUE) { - $this->directories[] = $this->directories[$i].$file.'/'; + if (is_dir($directory.$file) == true) { + if ($this->include_subdir == true) { + $this->directories[] = $directory.$file.'/'; continue; } else { continue; } } - $newfile = $this->$ser_func($this->directories[$i].$file); - if (is_array($newfile) == TRUE) { - $this->writeout($this->directories[$i].$file, $newfile[1]); + $newfile = $this->$ser_func($directory.$file); + if (is_array($newfile) == true) { + $this->writeout($directory.$file, $newfile[1]); $this->occurences += $newfile[0]; } } @@ -511,39 +593,72 @@ // }}} // {{{ doSearch() - + /** * This starts the search/replace off. The behavior of this function will likely * to be changed in future versions to work in read only mode. If you want to do - * actual replace with writing files - use doReplace method instead. + * actual replace with writing files - use doReplace method instead. * * @access public + * @return void */ function doSearch() { $this->doReplace(); } - + // }}} // {{{ doReplace() - + /** * This starts the search/replace off. Call this to do the replace. * First do whatever files are specified, and/or if directories are specified, * do those too. * * @access public + * @return void */ function doReplace() { $this->occurences = 0; - if ($this->find != '') { - if ((is_array($this->files) AND count($this->files) > 0) OR $this->files != '') $this->doFiles($this->search_function); - if ($this->directories != '') $this->doDirectories($this->search_function); + if (!empty($this->find)) { + if (!empty($this->files)) { + $this->doFiles($this->search_function); + } + + if (!empty($this->directories)) { + $this->doDirectories($this->search_function); + } } } - + // }}} + + /** + * Helper method to ensure we always have an array of things to find. + * + * @access private + * @return array + */ + function _getFind() + { + return array_values((array) $this->find); + } + + /** + * Helper method to fetch replace + * + * @access private + * @return mixed + */ + function _getReplace() + { + if (is_array($this->replace)) { + return array_values($this->replace); + } + + return $this->replace; + } } ?>