home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / phptriad / phptriad2-2-1.exe / php / pear / File / SearchReplace.php < prev   
PHP Script  |  2001-07-22  |  15KB  |  480 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP version 4.0                                                      |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Richard Heyes <richard.heyes@heyes-computing.net>           |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: SearchReplace.php,v 1.2 2001/07/22 14:38:15 mj Exp $
  19. //
  20. // Search and Replace Utility
  21. //
  22.  
  23. /**
  24.  * Search and Replace Utility
  25.  *
  26.  * See http://www.heyes-computing.net/scripts/ for full tar/zip
  27.  * including example file.
  28.  *
  29.  * @author  Richard Heyes <richard.heyes@heyes-computing.net>
  30.  * @version 1.0
  31.  * @package File
  32.  */
  33. class File_SearchReplace
  34. {
  35.     
  36.     // {{{ Properties (All private)
  37.  
  38.     var $find;
  39.     var $replace;
  40.     var $files;
  41.     var $directories;
  42.     var $include_subdir;
  43.     var $ignore_lines;
  44.     var $ignore_sep;
  45.     var $occurences;
  46.     var $search_function;
  47.     var $last_error;
  48.  
  49.     // }}}
  50.     // {{{ Constructor
  51.  
  52.     /**
  53.      * Sets up the object
  54.      *
  55.      * @access public
  56.      * @param string $find                      The string/regex to find.
  57.      * @param string $replace                   The string/regex to replace $find with.
  58.      * @param array  $files                     The file(s) to perform this operation on.
  59.      * @param array  $directories    (optional) The directories to perform this operation on.
  60.      * @param int    $include_subdir            If performing on directories, whether to traverse subdirectories.
  61.      * @param array  $ignore_lines              Ignore lines beginning with any of the strings in this array. This
  62.      *                                          feature only works with the "normal" search.
  63.      *
  64.      * @author Richard Heyes <richard.heyes@heyes-computing.net>
  65.      */
  66.     function File_SearchReplace($find, $replace, $files, $directories = '', $include_subdir = 1, $ignore_lines = array())
  67.     {
  68.  
  69.         $this->find            = $find;
  70.         $this->replace         = $replace;
  71.         $this->files           = $files;
  72.         $this->directories     = $directories;
  73.         $this->include_subdir  = $include_subdir;
  74.         $this->ignore_lines    = $ignore_lines;
  75.  
  76.         $this->occurences      = 0;
  77.         $this->search_function = 'search';
  78.         $this->last_error      = '';
  79.  
  80.     }
  81.  
  82.     // }}}
  83.     // {{{ getNumOccurences()
  84.  
  85.     /**
  86.      * Accessor to return the number of occurences found.
  87.      *
  88.      * @access public
  89.      * @return int Number of occurences found.
  90.      *
  91.      * @author Richard Heyes <richard.heyes@heyes-computing.net>
  92.      */
  93.     function getNumOccurences()
  94.     {
  95.         return $this->occurences;
  96.     }
  97.  
  98.     // }}}
  99.     // {{{ getLastError()
  100.  
  101.     /**
  102.      * Accessor for retrieving last error.
  103.      *
  104.      * @access public
  105.      * @return string The last error that occurred, if any.
  106.      *
  107.      * @author Richard Heyes <richard.heyes@heyes-computing.net>
  108.      */
  109.     function getLastError()
  110.     {
  111.         return $this->last_error;
  112.     }
  113.  
  114.     // }}}
  115.     // {{{ setFind()
  116.  
  117.     /**
  118.      * Accessor for setting find variable.
  119.      *
  120.      * @access public
  121.      * @param string $find The string/regex to find.
  122.      *
  123.      * @author Richard Heyes <richard.heyes@heyes-computing.net>
  124.      */
  125.     function setFind($find)
  126.     {
  127.         $this->find = $find;
  128.     }
  129.  
  130.     // }}}
  131.     // {{{ setReplace()
  132.  
  133.     /**
  134.      * Accessor for setting replace variable.
  135.      *
  136.      * @access public
  137.      * @param string $replace The string/regex to replace the find string/regex with.
  138.      *
  139.      * @author Richard Heyes <richard.heyes@heyes-computing.net>
  140.      */
  141.     function setReplace($replace)
  142.     {
  143.         $this->replace = $replace;
  144.     }
  145.  
  146.     // }}}
  147.     // {{{ setFiles()
  148.  
  149.     /**
  150.      * Accessor for setting files variable.
  151.      *
  152.      * @access public
  153.      * @param array $files The file(s) to perform this operation on.
  154.      *
  155.      * @author Richard Heyes <richard.heyes@heyes-computing.net>
  156.      */
  157.     function setFiles($files)
  158.     {
  159.         $this->files = $files;
  160.     }
  161.  
  162.     // }}}
  163.     // {{{ setDirectories()
  164.  
  165.     /**
  166.      * Accessor for setting directories variable.
  167.      *
  168.      * @access public
  169.      * @param array $directories The directories to perform this operation on.
  170.      *
  171.      * @author Richard Heyes <richard.heyes@heyes-computing.net>
  172.      */
  173.     function setDirectories($directories)
  174.     {
  175.         $this->directories = $directories;
  176.     }
  177.  
  178.     // }}}
  179.     // {{{ setIncludeSubdir
  180.  
  181.     /**
  182.      * Accessor for setting include_subdir variable.
  183.      *
  184.      * @access public
  185.      * @param int $include_subdir Whether to traverse subdirectories or not.
  186.      *
  187.      * @author Richard Heyes <richard.heyes@heyes-computing.net>
  188.      */
  189.     function setIncludeSubdir($include_subdir)
  190.     {
  191.         $this->include_subdir = $include_subdir;
  192.     }
  193.  
  194.     // }}}
  195.     // {{{ setIgnoreLines()
  196.  
  197.     /**
  198.      * Accessor for setting ignore_lines variable.
  199.      *
  200.      * @access public
  201.      * @param array $ignore_lines Ignore lines beginning with any of the strings in this array. This
  202.      *                            feature only works with the "normal" search.
  203.      *
  204.      * @author Richard Heyes <richard.heyes@heyes-computing.net>
  205.      */
  206.     function setIgnoreLines($ignore_lines)
  207.     {
  208.         $this->ignore_lines = $ignore_lines;
  209.     }
  210.  
  211.     // }}}
  212.     // {{{ setSearchFunction()
  213.  
  214.     /**
  215.      * Function to determine which search function is used.
  216.      *
  217.      * @access public
  218.      * @param string The search function that should be used. Can be any one of:
  219.      *               normal - Default search. Goes line by line. Ignore lines feature only works with this type.
  220.      *               quick  - Uses str_replace for straight replacement throughout file. Quickest of the lot.
  221.      *               preg   - Uses preg_replace(), so any regex valid with this function is valid here.
  222.      *               ereg   - Uses ereg_replace(), so any regex valid with this function is valid here.
  223.      *
  224.      * @author Richard Heyes <richard.heyes@heyes-computing.net>
  225.      */
  226.     function setSearchFunction($search_function)
  227.     {
  228.         switch($search_function) {
  229.         case 'normal': $this->search_function = 'search';
  230.             return TRUE;
  231.             break;
  232.  
  233.         case 'quick' : $this->search_function = 'quickSearch';
  234.             return TRUE;
  235.             break;
  236.  
  237.         case 'preg'  : $this->search_function = 'pregSearch';
  238.             return TRUE;
  239.             break;
  240.  
  241.         case 'ereg'  : $this->search_function = 'eregSearch';
  242.             return TRUE;
  243.             break;
  244.  
  245.         default      : $this->last_error      = 'Invalid search function specified';
  246.             return FALSE;
  247.             break;
  248.         }
  249.     }
  250.  
  251.     // }}}
  252.     // {{{ search()
  253.  
  254.     /**
  255.      * Default ("normal") search routine.
  256.      *
  257.      * @access private
  258.      * @param string $filename The filename to search and replace upon.
  259.      * @return array Will return an array containing the new file contents and the number of occurences.
  260.      *               Will return FALSE if there are no occurences.
  261.      *
  262.      * @author Richard Heyes <richard.heyes@heyes-computing.net>
  263.      */
  264.     function search($filename)
  265.     {
  266.  
  267.         $occurences = 0;
  268.         $file_array = file($filename);
  269.  
  270.         for ($i=0; $i<count($file_array); $i++) {
  271.  
  272.             if (count($this->ignore_lines) > 0) {
  273.                 for ($j=0; $j<count($this->ignore_lines); $j++) {
  274.                     if (substr($file_array[$i],0,strlen($this->ignore_lines[$j])) == $this->ignore_lines[$j]) continue 2;
  275.                 }
  276.             }
  277.  
  278.             $occurences += count(explode($this->find, $file_array[$i])) - 1;
  279.             $file_array[$i] = str_replace($this->find, $this->replace, $file_array[$i]);
  280.         }
  281.         if ($occurences > 0) $return = array($occurences, implode('', $file_array)); else $return = FALSE;
  282.         return $return;
  283.  
  284.     }
  285.  
  286.     // }}}
  287.     // {{{ quickSearch()
  288.  
  289.     /**
  290.      * Quick search routine.
  291.      *
  292.      * @access private
  293.      * @param string $filename The filename to search and replace upon.
  294.      * @return array Will return an array containing the new file contents and the number of occurences.
  295.      *               Will return FALSE if there are no occurences.
  296.      *
  297.      * @author Richard Heyes <richard.heyes@heyes-computing.net>
  298.      */
  299.     function quickSearch($filename)
  300.     {
  301.  
  302.         clearstatcache();
  303.  
  304.         $file       = fread($fp = fopen($filename, 'r'), filesize($filename)); fclose($fp);
  305.         $occurences = count(explode($this->find, $file)) - 1;
  306.         $file       = str_replace($this->find, $this->replace, $file);
  307.  
  308.         if ($occurences > 0) $return = array($occurences, $file); else $return = FALSE;
  309.         return $return;
  310.  
  311.     }
  312.  
  313.     // }}}
  314.     // {{{ pregSearch()
  315.  
  316.     /**
  317.      * Preg search routine.
  318.      *
  319.      * @access private
  320.      * @param string $filename The filename to search and replace upon.
  321.      * @return array Will return an array containing the new file contents and the number of occurences.
  322.      *               Will return FALSE if there are no occurences.
  323.      *
  324.      * @author Richard Heyes <richard.heyes@heyes-computing.net>
  325.      */
  326.     function pregSearch($filename)
  327.     {
  328.  
  329.         clearstatcache();
  330.  
  331.         $file       = fread($fp = fopen($filename, 'r'), filesize($filename)); fclose($fp);
  332.         $occurences = count($matches = preg_split($this->find, $file)) - 1;
  333.         $file       = preg_replace($this->find, $this->replace, $file);
  334.  
  335.         if ($occurences > 0) $return = array($occurences, $file); else $return = FALSE;
  336.         return $return;
  337.  
  338.     }
  339.  
  340.     // }}}
  341.     // {{{ eregSearch()
  342.  
  343.     /**
  344.      * Ereg search routine.
  345.      *
  346.      * @access private
  347.      * @param string $filename The filename to search and replace upon.
  348.      * @return array Will return an array containing the new file contents and the number of occurences.
  349.      *               Will return FALSE if there are no occurences.
  350.      *
  351.      * @author Richard Heyes <richard.heyes@heyes-computing.net>
  352.      */
  353.     function eregSearch($filename)
  354.     {
  355.  
  356.         clearstatcache();
  357.  
  358.         $file = fread($fp = fopen($filename, 'r'), filesize($filename)); fclose($fp);
  359.  
  360.         $occurences = count($matches = split($this->find, $file)) -1;
  361.         $file       = ereg_replace($this->find, $this->replace, $file);
  362.  
  363.         if ($occurences > 0) $return = array($occurences, $file); else $return = FALSE;
  364.         return $return;
  365.  
  366.     }
  367.  
  368.     // }}}
  369.     // {{{ writeout()
  370.     
  371.     /**
  372.      * Function to writeout the file contents.
  373.      *
  374.      * @access private
  375.      * @param string $filename The filename of the file to write.
  376.      * @param string $contents The contents to write to the file.
  377.      *
  378.      * @author Richard Heyes <richard.heyes@heyes-computing.net>
  379.      */
  380.     function writeout($filename, $contents)
  381.     {
  382.  
  383.         if ($fp = @fopen($filename, 'w')) {
  384.             flock($fp,2);
  385.             fwrite($fp, $contents);
  386.             flock($fp,3);
  387.             fclose($fp);
  388.         } else {
  389.             $this->last_error = 'Could not open file: '.$filename;
  390.         }
  391.  
  392.     }
  393.  
  394.     // }}}
  395.     // {{{ doFiles()
  396.  
  397.     /**
  398.      * Function called by doSearch() to go through any files that need searching.
  399.      *
  400.      * @access private
  401.      * @param string $ser_func The search function to use.
  402.      *
  403.      * @author Richard Heyes <richard.heyes@heyes-computing.net>
  404.      */
  405.     function doFiles($ser_func)
  406.     {
  407.         if (!is_array($this->files)) $this->files = explode(',', $this->files);
  408.         for ($i=0; $i<count($this->files); $i++) {
  409.             if ($this->files[$i] == '.' OR $this->files[$i] == '..') continue;
  410.             if (is_dir($this->files[$i]) == TRUE) continue;
  411.             $newfile = $this->$ser_func($this->files[$i]);
  412.             if (is_array($newfile) == TRUE){
  413.                 $this->writeout($this->files[$i], $newfile[1]);
  414.                 $this->occurences += $newfile[0];
  415.             }
  416.         }
  417.     }
  418.  
  419.     // }}}
  420.     // {{{ doDirectories()
  421.  
  422.     /**
  423.      * Function called by doSearch() to go through any directories that need searching.
  424.      *
  425.      * @access private
  426.      * @param string $ser_func The search function to use.
  427.      *
  428.      * @author Richard Heyes <richard.heyes@heyes-computing.net>
  429.      */
  430.     function doDirectories($ser_func)
  431.     {
  432.         if (!is_array($this->directories)) $this->directories = explode(',', $this->directories);
  433.         for ($i=0; $i<count($this->directories); $i++) {
  434.             $dh = opendir($this->directories[$i]);
  435.             while ($file = readdir($dh)) {
  436.                 if ($file == '.' OR $file == '..') continue;
  437.  
  438.                 if (is_dir($this->directories[$i].$file) == TRUE) {
  439.                     if ($this->include_subdir == 1) {
  440.                         $this->directories[] = $this->directories[$i].$file.'/';
  441.                         continue;
  442.                     } else {
  443.                         continue;
  444.                     }
  445.                 }
  446.  
  447.                 $newfile = $this->$ser_func($this->directories[$i].$file);
  448.                 if (is_array($newfile) == TRUE) {
  449.                     $this->writeout($this->directories[$i].$file, $newfile[1]);
  450.                     $this->occurences += $newfile[0];
  451.                 }
  452.             }
  453.         }
  454.     }
  455.  
  456.     // }}}
  457.     // {{{ doSearch()
  458.     
  459.     /**
  460.      * This starts the search/replace off. Call this to do the search.
  461.      * First do whatever files are specified, and/or if directories are specified,
  462.      * do those too.
  463.      *
  464.      * @access public
  465.      *
  466.      * @author Richard Heyes <richard.heyes@heyes-computing.net>
  467.      */
  468.     function doSearch()
  469.     {
  470.         if ($this->find != '') {
  471.             if ((is_array($this->files) AND count($this->files) > 0) OR $this->files != '') $this->doFiles($this->search_function);
  472.             if ($this->directories != '')                                                   $this->doDirectories($this->search_function);
  473.         }
  474.     }
  475.     
  476.     // }}}
  477.  
  478. }
  479. ?>
  480.