[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/search/Zend/Search/Lucene/Storage/Directory/ -> Filesystem.php (source)

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


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7