Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / search / Zend / Search / Lucene / Storage / Directory / Filesystem.php
blob7ac76810ab6a5d1d095f0becad20169c2cf15521
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
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
23 /** Zend_Search_Lucene_Storage_Directory */
24 require_once $CFG->dirroot.'/search/Zend/Search/Lucene/Storage/Directory.php';
26 /** Zend_Search_Lucene_Storage_File_Filesystem */
27 require_once $CFG->dirroot.'/search/Zend/Search/Lucene/Storage/File/Filesystem.php';
30 /**
31 * FileSystem implementation of Directory abstraction.
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
39 class Zend_Search_Lucene_Storage_Directory_Filesystem extends Zend_Search_Lucene_Storage_Directory
41 /**
42 * Filesystem path to the directory
44 * @var string
46 private $_dirPath = null;
48 /**
49 * Cache for Zend_Search_Lucene_Storage_File_Filesystem objects
50 * Array: filename => Zend_Search_Lucene_Storage_File object
52 * @var array
53 * @throws Zend_Search_Lucene_Exception
55 private $_fileHandlers;
58 /**
59 * Utility function to recursive directory creation
61 * @param string $dir
62 * @param integer $mode
63 * @param boolean $recursive
64 * @return boolean
67 public static function mkdirs($dir, $mode = 0777, $recursive = true)
69 if (is_null($dir) || $dir === '') {
70 return false;
72 if (is_dir($dir) || $dir === '/') {
73 return true;
75 if (self::mkdirs(dirname($dir), $mode, $recursive)) {
76 return mkdir($dir, $mode);
78 return false;
82 /**
83 * Object constructor
84 * Checks if $path is a directory or tries to create it.
86 * @param string $path
87 * @throws Zend_Search_Lucene_Exception
89 public function __construct($path)
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'.");
100 $this->_dirPath = $path;
101 $this->_fileHandlers = array();
106 * Closes the store.
108 * @return void
110 public function close()
112 foreach ($this->_fileHandlers as $fileObject) {
113 $fileObject->close();
116 $this->_fileHandlers = array();
121 * Returns an array of strings, one for each file in the directory.
123 * @return array
125 public function fileList()
127 $result = array();
129 $dirContent = opendir( $this->_dirPath );
130 while (($file = readdir($dirContent)) !== false) {
131 if (($file == '..')||($file == '.')) continue;
133 if( !is_dir($this->_dirPath . '/' . $file) ) {
134 $result[] = $file;
137 closedir($dirContent);
139 return $result;
143 * Creates a new, empty file in the directory with the given $filename.
145 * @param string $filename
146 * @return Zend_Search_Lucene_Storage_File
148 public function createFile($filename)
150 if (isset($this->_fileHandlers[$filename])) {
151 $this->_fileHandlers[$filename]->close();
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];
160 * Removes an existing $filename in the directory.
162 * @param string $filename
163 * @return void
165 public function deleteFile($filename)
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).
173 if (isset($this->_fileHandlers[$filename])) {
174 $this->_fileHandlers[$filename]->close();
176 unset($this->_fileHandlers[$filename]);
177 unlink($this->_dirPath . '/' . $filename);
182 * Returns true if a file with the given $filename exists.
184 * @param string $filename
185 * @return boolean
187 public function fileExists($filename)
189 return isset($this->_fileHandlers[$filename]) ||
190 file_exists($this->_dirPath . '/' . $filename);
195 * Returns the length of a $filename in the directory.
197 * @param string $filename
198 * @return integer
200 public function fileLength($filename)
202 if (isset( $this->_fileHandlers[$filename] )) {
203 return $this->_fileHandlers[$filename]->size();
205 return filesize($this->_dirPath .'/'. $filename);
210 * Returns the UNIX timestamp $filename was last modified.
212 * @param string $filename
213 * @return integer
215 public function fileModified($filename)
217 return filemtime($this->_dirPath .'/'. $filename);
222 * Renames an existing file in the directory.
224 * @param string $from
225 * @param string $to
226 * @return void
227 * @throws Zend_Search_Lucene_Exception
229 public function renameFile($from, $to)
231 global $php_errormsg;
233 if (isset($this->_fileHandlers[$from])) {
234 $this->_fileHandlers[$from]->close();
236 unset($this->_fileHandlers[$from]);
238 if (isset($this->_fileHandlers[$to])) {
239 $this->_fileHandlers[$to]->close();
241 unset($this->_fileHandlers[$to]);
243 if (file_exists($this->_dirPath . '/' . $to)) {
244 if (!unlink($this->_dirPath . '/' . $to)) {
245 throw new Zend_Search_Lucene_Exception('Delete operation failed');
249 $trackErrors = ini_get('track_errors');
250 ini_set('track_errors', '1');
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);
258 ini_set('track_errors', $trackErrors);
260 return $success;
265 * Sets the modified time of $filename to now.
267 * @param string $filename
268 * @return void
270 public function touchFile($filename)
272 return touch($this->_dirPath .'/'. $filename);
277 * Returns a Zend_Search_Lucene_Storage_File object for a given $filename in the directory.
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).
284 * @param string $filename
285 * @param boolean $shareHandler
286 * @return Zend_Search_Lucene_Storage_File
288 public function getFileObject($filename, $shareHandler = true)
290 $fullFilename = $this->_dirPath . '/' . $filename;
292 if (!$shareHandler) {
293 return new Zend_Search_Lucene_Storage_File_Filesystem($fullFilename);
296 if (isset( $this->_fileHandlers[$filename] )) {
297 $this->_fileHandlers[$filename]->seek(0);
298 return $this->_fileHandlers[$filename];
301 $this->_fileHandlers[$filename] = new Zend_Search_Lucene_Storage_File_Filesystem($fullFilename);
302 return $this->_fileHandlers[$filename];