PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Search/Lucene/Storage/Directory/Filesystem.php

https://bitbucket.org/baruffaldi/cms-php-bfcms
PHP | 362 lines | 187 code | 43 blank | 132 comment | 20 complexity | 99a29c1f6a75ad6ba8919685f6aa13cc MD5 | raw file
  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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Search_Lucene_Storage_Directory */
  22. require_once 'Zend/Search/Lucene/Storage/Directory.php';
  23. /** Zend_Search_Lucene_Storage_File_Filesystem */
  24. require_once 'Zend/Search/Lucene/Storage/File/Filesystem.php';
  25. /**
  26. * FileSystem implementation of Directory abstraction.
  27. *
  28. * @category Zend
  29. * @package Zend_Search_Lucene
  30. * @subpackage Storage
  31. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Search_Lucene_Storage_Directory_Filesystem extends Zend_Search_Lucene_Storage_Directory
  35. {
  36. /**
  37. * Filesystem path to the directory
  38. *
  39. * @var string
  40. */
  41. protected $_dirPath = null;
  42. /**
  43. * Cache for Zend_Search_Lucene_Storage_File_Filesystem objects
  44. * Array: filename => Zend_Search_Lucene_Storage_File object
  45. *
  46. * @var array
  47. * @throws Zend_Search_Lucene_Exception
  48. */
  49. protected $_fileHandlers;
  50. /**
  51. * Default file permissions
  52. *
  53. * @var integer
  54. */
  55. protected static $_defaultFilePermissions = 0666;
  56. /**
  57. * Get default file permissions
  58. *
  59. * @return integer
  60. */
  61. public static function getDefaultFilePermissions()
  62. {
  63. return self::$_defaultFilePermissions;
  64. }
  65. /**
  66. * Set default file permissions
  67. *
  68. * @param integer $mode
  69. */
  70. public static function setDefaultFilePermissions($mode)
  71. {
  72. self::$_defaultFilePermissions = $mode;
  73. }
  74. /**
  75. * Utility function to recursive directory creation
  76. *
  77. * @param string $dir
  78. * @param integer $mode
  79. * @param boolean $recursive
  80. * @return boolean
  81. */
  82. public static function mkdirs($dir, $mode = 0777, $recursive = true)
  83. {
  84. if (is_null($dir) || $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. $this->_fileHandlers[$filename] = new Zend_Search_Lucene_Storage_File_Filesystem($this->_dirPath . '/' . $filename, 'w+b');
  162. // Set file permissions, but don't care about any possible failures, since file may be already
  163. // created by anther user which has to care about right permissions
  164. @chmod($this->_dirPath . '/' . $filename, self::$_defaultFilePermissions);
  165. return $this->_fileHandlers[$filename];
  166. }
  167. /**
  168. * Removes an existing $filename in the directory.
  169. *
  170. * @param string $filename
  171. * @return void
  172. * @throws Zend_Search_Lucene_Exception
  173. */
  174. public function deleteFile($filename)
  175. {
  176. if (isset($this->_fileHandlers[$filename])) {
  177. $this->_fileHandlers[$filename]->close();
  178. }
  179. unset($this->_fileHandlers[$filename]);
  180. global $php_errormsg;
  181. $trackErrors = ini_get('track_errors'); ini_set('track_errors', '1');
  182. if (!@unlink($this->_dirPath . '/' . $filename)) {
  183. ini_set('track_errors', $trackErrors);
  184. require_once 'Zend/Search/Lucene/Exception.php';
  185. throw new Zend_Search_Lucene_Exception('Can\'t delete file: ' . $php_errormsg);
  186. }
  187. ini_set('track_errors', $trackErrors);
  188. }
  189. /**
  190. * Purge file if it's cached by directory object
  191. *
  192. * Method is used to prevent 'too many open files' error
  193. *
  194. * @param string $filename
  195. * @return void
  196. */
  197. public function purgeFile($filename)
  198. {
  199. if (isset($this->_fileHandlers[$filename])) {
  200. $this->_fileHandlers[$filename]->close();
  201. }
  202. unset($this->_fileHandlers[$filename]);
  203. }
  204. /**
  205. * Returns true if a file with the given $filename exists.
  206. *
  207. * @param string $filename
  208. * @return boolean
  209. */
  210. public function fileExists($filename)
  211. {
  212. return isset($this->_fileHandlers[$filename]) ||
  213. file_exists($this->_dirPath . '/' . $filename);
  214. }
  215. /**
  216. * Returns the length of a $filename in the directory.
  217. *
  218. * @param string $filename
  219. * @return integer
  220. */
  221. public function fileLength($filename)
  222. {
  223. if (isset( $this->_fileHandlers[$filename] )) {
  224. return $this->_fileHandlers[$filename]->size();
  225. }
  226. return filesize($this->_dirPath .'/'. $filename);
  227. }
  228. /**
  229. * Returns the UNIX timestamp $filename was last modified.
  230. *
  231. * @param string $filename
  232. * @return integer
  233. */
  234. public function fileModified($filename)
  235. {
  236. return filemtime($this->_dirPath .'/'. $filename);
  237. }
  238. /**
  239. * Renames an existing file in the directory.
  240. *
  241. * @param string $from
  242. * @param string $to
  243. * @return void
  244. * @throws Zend_Search_Lucene_Exception
  245. */
  246. public function renameFile($from, $to)
  247. {
  248. global $php_errormsg;
  249. if (isset($this->_fileHandlers[$from])) {
  250. $this->_fileHandlers[$from]->close();
  251. }
  252. unset($this->_fileHandlers[$from]);
  253. if (isset($this->_fileHandlers[$to])) {
  254. $this->_fileHandlers[$to]->close();
  255. }
  256. unset($this->_fileHandlers[$to]);
  257. if (file_exists($this->_dirPath . '/' . $to)) {
  258. if (!unlink($this->_dirPath . '/' . $to)) {
  259. require_once 'Zend/Search/Lucene/Exception.php';
  260. throw new Zend_Search_Lucene_Exception('Delete operation failed');
  261. }
  262. }
  263. $trackErrors = ini_get('track_errors');
  264. ini_set('track_errors', '1');
  265. $success = @rename($this->_dirPath . '/' . $from, $this->_dirPath . '/' . $to);
  266. if (!$success) {
  267. ini_set('track_errors', $trackErrors);
  268. require_once 'Zend/Search/Lucene/Exception.php';
  269. throw new Zend_Search_Lucene_Exception($php_errormsg);
  270. }
  271. ini_set('track_errors', $trackErrors);
  272. return $success;
  273. }
  274. /**
  275. * Sets the modified time of $filename to now.
  276. *
  277. * @param string $filename
  278. * @return void
  279. */
  280. public function touchFile($filename)
  281. {
  282. return touch($this->_dirPath .'/'. $filename);
  283. }
  284. /**
  285. * Returns a Zend_Search_Lucene_Storage_File object for a given $filename in the directory.
  286. *
  287. * If $shareHandler option is true, then file handler can be shared between File Object
  288. * requests. It speed-ups performance, but makes problems with file position.
  289. * Shared handler are good for short atomic requests.
  290. * Non-shared handlers are useful for stream file reading (especial for compound files).
  291. *
  292. * @param string $filename
  293. * @param boolean $shareHandler
  294. * @return Zend_Search_Lucene_Storage_File
  295. */
  296. public function getFileObject($filename, $shareHandler = true)
  297. {
  298. $fullFilename = $this->_dirPath . '/' . $filename;
  299. if (!$shareHandler) {
  300. return new Zend_Search_Lucene_Storage_File_Filesystem($fullFilename);
  301. }
  302. if (isset( $this->_fileHandlers[$filename] )) {
  303. $this->_fileHandlers[$filename]->seek(0);
  304. return $this->_fileHandlers[$filename];
  305. }
  306. $this->_fileHandlers[$filename] = new Zend_Search_Lucene_Storage_File_Filesystem($fullFilename);
  307. return $this->_fileHandlers[$filename];
  308. }
  309. }