vendor/zf1/zend-search-lucene/library/Zend/Search/Lucene/Storage/Directory/Filesystem.php line 190

Open in your IDE?
  1. <?php
  2. /**
  3.  * Zend Framework
  4.  *
  5.  * LICENSE
  6.  *
  7.  * This source file is subject to the new BSD license that is bundled
  8.  * with this package in the file LICENSE.txt.
  9.  * It is also available through the world-wide-web at this URL:
  10.  * http://framework.zend.com/license/new-bsd
  11.  * If you did not receive a copy of the license and are unable to
  12.  * obtain it through the world-wide-web, please send an email
  13.  * to license@zend.com so we can send you a copy immediately.
  14.  *
  15.  * @category   Zend
  16.  * @package    Zend_Search_Lucene
  17.  * @subpackage Storage
  18.  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19.  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  20.  * @version    $Id$
  21.  */
  22. /** Zend_Search_Lucene_Storage_Directory */
  23. // require_once 'Zend/Search/Lucene/Storage/Directory.php';
  24. /**
  25.  * FileSystem implementation of Directory abstraction.
  26.  *
  27.  * @category   Zend
  28.  * @package    Zend_Search_Lucene
  29.  * @subpackage Storage
  30.  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31.  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  32.  */
  33. class Zend_Search_Lucene_Storage_Directory_Filesystem extends Zend_Search_Lucene_Storage_Directory
  34. {
  35.     /**
  36.      * Filesystem path to the directory
  37.      *
  38.      * @var string
  39.      */
  40.     protected $_dirPath null;
  41.     /**
  42.      * Cache for Zend_Search_Lucene_Storage_File_Filesystem objects
  43.      * Array: filename => Zend_Search_Lucene_Storage_File object
  44.      *
  45.      * @var array
  46.      * @throws Zend_Search_Lucene_Exception
  47.      */
  48.     protected $_fileHandlers;
  49.     /**
  50.      * Default file permissions
  51.      *
  52.      * @var integer
  53.      */
  54.     protected static $_defaultFilePermissions 0666;
  55.     /**
  56.      * Get default file permissions
  57.      *
  58.      * @return integer
  59.      */
  60.     public static function getDefaultFilePermissions()
  61.     {
  62.         return self::$_defaultFilePermissions;
  63.     }
  64.     /**
  65.      * Set default file permissions
  66.      *
  67.      * @param integer $mode
  68.      */
  69.     public static function setDefaultFilePermissions($mode)
  70.     {
  71.         self::$_defaultFilePermissions $mode;
  72.     }
  73.     /**
  74.      * Utility function to recursive directory creation
  75.      *
  76.      * @param string $dir
  77.      * @param integer $mode
  78.      * @param boolean $recursive
  79.      * @return boolean
  80.      */
  81.     public static function mkdirs($dir$mode 0775$recursive true)
  82.     {
  83.         $mode $mode & ~0002;
  84.         if (($dir === null) || $dir === '') {
  85.             return false;
  86.         }
  87.         if (is_dir($dir) || $dir === '/') {
  88.             return true;
  89.         }
  90.         if (self::mkdirs(dirname($dir), $mode$recursive)) {
  91.             return mkdir($dir$mode);
  92.         }
  93.         return false;
  94.     }
  95.     /**
  96.      * Object constructor
  97.      * Checks if $path is a directory or tries to create it.
  98.      *
  99.      * @param string $path
  100.      * @throws Zend_Search_Lucene_Exception
  101.      */
  102.     public function __construct($path)
  103.     {
  104.         if (!is_dir($path)) {
  105.             if (file_exists($path)) {
  106.                 // require_once 'Zend/Search/Lucene/Exception.php';
  107.                 throw new Zend_Search_Lucene_Exception('Path exists, but it\'s not a directory');
  108.             } else {
  109.                 if (!self::mkdirs($path)) {
  110.                     // require_once 'Zend/Search/Lucene/Exception.php';
  111.                     throw new Zend_Search_Lucene_Exception("Can't create directory '$path'.");
  112.                 }
  113.             }
  114.         }
  115.         $this->_dirPath $path;
  116.         $this->_fileHandlers = array();
  117.     }
  118.     /**
  119.      * Closes the store.
  120.      *
  121.      * @return void
  122.      */
  123.     public function close()
  124.     {
  125.         foreach ($this->_fileHandlers as $fileObject) {
  126.             $fileObject->close();
  127.         }
  128.         $this->_fileHandlers = array();
  129.     }
  130.     /**
  131.      * Returns an array of strings, one for each file in the directory.
  132.      *
  133.      * @return array
  134.      */
  135.     public function fileList()
  136.     {
  137.         $result = array();
  138.         $dirContent opendir$this->_dirPath );
  139.         while (($file readdir($dirContent)) !== false) {
  140.             if (($file == '..')||($file == '.'))   continue;
  141.             if( !is_dir($this->_dirPath '/' $file) ) {
  142.                 $result[] = $file;
  143.             }
  144.         }
  145.         closedir($dirContent);
  146.         return $result;
  147.     }
  148.     /**
  149.      * Creates a new, empty file in the directory with the given $filename.
  150.      *
  151.      * @param string $filename
  152.      * @return Zend_Search_Lucene_Storage_File
  153.      * @throws Zend_Search_Lucene_Exception
  154.      */
  155.     public function createFile($filename)
  156.     {
  157.         if (isset($this->_fileHandlers[$filename])) {
  158.             $this->_fileHandlers[$filename]->close();
  159.         }
  160.         unset($this->_fileHandlers[$filename]);
  161.         // require_once 'Zend/Search/Lucene/Storage/File/Filesystem.php';
  162.         $this->_fileHandlers[$filename] = new Zend_Search_Lucene_Storage_File_Filesystem($this->_dirPath '/' $filename'w+b');
  163.         // Set file permissions, but don't care about any possible failures, since file may be already
  164.         // created by anther user which has to care about right permissions
  165.         @chmod($this->_dirPath '/' $filenameself::$_defaultFilePermissions);
  166.         return $this->_fileHandlers[$filename];
  167.     }
  168.     /**
  169.      * Removes an existing $filename in the directory.
  170.      *
  171.      * @param string $filename
  172.      * @return void
  173.      * @throws Zend_Search_Lucene_Exception
  174.      */
  175.     public function deleteFile($filename)
  176.     {
  177.         if (isset($this->_fileHandlers[$filename])) {
  178.             $this->_fileHandlers[$filename]->close();
  179.         }
  180.         unset($this->_fileHandlers[$filename]);
  181.         global $php_errormsg;
  182.         $trackErrors ini_get('track_errors');
  183.         ini_set('track_errors''1');
  184.         if (!@unlink($this->_dirPath '/' $filename)) {
  185.             ini_set('track_errors'$trackErrors);
  186.             // require_once 'Zend/Search/Lucene/Exception.php';
  187.             throw new Zend_Search_Lucene_Exception('Can\'t delete file: ' $php_errormsg);
  188.         }
  189.         ini_set('track_errors'$trackErrors);
  190.     }
  191.     /**
  192.      * Purge file if it's cached by directory object
  193.      *
  194.      * Method is used to prevent 'too many open files' error
  195.      *
  196.      * @param string $filename
  197.      * @return void
  198.      */
  199.     public function purgeFile($filename)
  200.     {
  201.         if (isset($this->_fileHandlers[$filename])) {
  202.             $this->_fileHandlers[$filename]->close();
  203.         }
  204.         unset($this->_fileHandlers[$filename]);
  205.     }
  206.     /**
  207.      * Returns true if a file with the given $filename exists.
  208.      *
  209.      * @param string $filename
  210.      * @return boolean
  211.      */
  212.     public function fileExists($filename)
  213.     {
  214.         return isset($this->_fileHandlers[$filename]) ||
  215.                file_exists($this->_dirPath '/' $filename);
  216.     }
  217.     /**
  218.      * Returns the length of a $filename in the directory.
  219.      *
  220.      * @param string $filename
  221.      * @return integer
  222.      */
  223.     public function fileLength($filename)
  224.     {
  225.         if (isset( $this->_fileHandlers[$filename] )) {
  226.             return $this->_fileHandlers[$filename]->size();
  227.         }
  228.         return filesize($this->_dirPath .'/'$filename);
  229.     }
  230.     /**
  231.      * Returns the UNIX timestamp $filename was last modified.
  232.      *
  233.      * @param string $filename
  234.      * @return integer
  235.      */
  236.     public function fileModified($filename)
  237.     {
  238.         return filemtime($this->_dirPath .'/'$filename);
  239.     }
  240.     /**
  241.      * Renames an existing file in the directory.
  242.      *
  243.      * @param string $from
  244.      * @param string $to
  245.      * @return void
  246.      * @throws Zend_Search_Lucene_Exception
  247.      */
  248.     public function renameFile($from$to)
  249.     {
  250.         global $php_errormsg;
  251.         if (isset($this->_fileHandlers[$from])) {
  252.             $this->_fileHandlers[$from]->close();
  253.         }
  254.         unset($this->_fileHandlers[$from]);
  255.         if (isset($this->_fileHandlers[$to])) {
  256.             $this->_fileHandlers[$to]->close();
  257.         }
  258.         unset($this->_fileHandlers[$to]);
  259.         if (file_exists($this->_dirPath '/' $to)) {
  260.             if (!unlink($this->_dirPath '/' $to)) {
  261.                 // require_once 'Zend/Search/Lucene/Exception.php';
  262.                 throw new Zend_Search_Lucene_Exception('Delete operation failed');
  263.             }
  264.         }
  265.         $trackErrors ini_get('track_errors');
  266.         ini_set('track_errors''1');
  267.         $success = @rename($this->_dirPath '/' $from$this->_dirPath '/' $to);
  268.         if (!$success) {
  269.             ini_set('track_errors'$trackErrors);
  270.             // require_once 'Zend/Search/Lucene/Exception.php';
  271.             throw new Zend_Search_Lucene_Exception($php_errormsg);
  272.         }
  273.         ini_set('track_errors'$trackErrors);
  274.         return $success;
  275.     }
  276.     /**
  277.      * Sets the modified time of $filename to now.
  278.      *
  279.      * @param string $filename
  280.      * @return void
  281.      */
  282.     public function touchFile($filename)
  283.     {
  284.         return touch($this->_dirPath .'/'$filename);
  285.     }
  286.     /**
  287.      * Returns a Zend_Search_Lucene_Storage_File object for a given $filename in the directory.
  288.      *
  289.      * If $shareHandler option is true, then file handler can be shared between File Object
  290.      * requests. It speed-ups performance, but makes problems with file position.
  291.      * Shared handler are good for short atomic requests.
  292.      * Non-shared handlers are useful for stream file reading (especial for compound files).
  293.      *
  294.      * @param string $filename
  295.      * @param boolean $shareHandler
  296.      * @return Zend_Search_Lucene_Storage_File
  297.      */
  298.     public function getFileObject($filename$shareHandler true)
  299.     {
  300.         $fullFilename $this->_dirPath '/' $filename;
  301.         // require_once 'Zend/Search/Lucene/Storage/File/Filesystem.php';
  302.         if (!$shareHandler) {
  303.             return new Zend_Search_Lucene_Storage_File_Filesystem($fullFilename);
  304.         }
  305.         if (isset( $this->_fileHandlers[$filename] )) {
  306.             $this->_fileHandlers[$filename]->seek(0);
  307.             return $this->_fileHandlers[$filename];
  308.         }
  309.         $this->_fileHandlers[$filename] = new Zend_Search_Lucene_Storage_File_Filesystem($fullFilename);
  310.         return $this->_fileHandlers[$filename];
  311.     }
  312. }