PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/vendor/Zend/Search/Lucene/Storage/Directory/Filesystem.php

https://bitbucket.org/anycode/sfluceneplugin
PHP | 362 lines | 188 code | 42 blank | 132 comment | 20 complexity | dbaa9c2d06215bcf6792799b892ab5cb MD5 | raw file
Possible License(s): BSD-3-Clause
  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Filesystem.php 20096 2010-01-06 02:05:09Z bkarwin $
  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-2010 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 = 0777, $recursive = true)
  82. {
  83. if (($dir === null) || $dir === '') {
  84. return false;
  85. }
  86. if (is_dir($dir) || $dir === '/') {
  87. return true;
  88. }
  89. if (self::mkdirs(dirname($dir), $mode, $recursive)) {
  90. return mkdir($dir, $mode);
  91. }
  92. return false;
  93. }
  94. /**
  95. * Object constructor
  96. * Checks if $path is a directory or tries to create it.
  97. *
  98. * @param string $path
  99. * @throws Zend_Search_Lucene_Exception
  100. */
  101. public function __construct($path)
  102. {
  103. if (!is_dir($path)) {
  104. if (file_exists($path)) {
  105. require_once 'Zend/Search/Lucene/Exception.php';
  106. throw new Zend_Search_Lucene_Exception('Path exists, but it\'s not a directory');
  107. } else {
  108. if (!self::mkdirs($path)) {
  109. require_once 'Zend/Search/Lucene/Exception.php';
  110. throw new Zend_Search_Lucene_Exception("Can't create directory '$path'.");
  111. }
  112. }
  113. }
  114. $this->_dirPath = $path;
  115. $this->_fileHandlers = array();
  116. }
  117. /**
  118. * Closes the store.
  119. *
  120. * @return void
  121. */
  122. public function close()
  123. {
  124. foreach ($this->_fileHandlers as $fileObject) {
  125. $fileObject->close();
  126. }
  127. $this->_fileHandlers = array();
  128. }
  129. /**
  130. * Returns an array of strings, one for each file in the directory.
  131. *
  132. * @return array
  133. */
  134. public function fileList()
  135. {
  136. $result = array();
  137. $dirContent = opendir( $this->_dirPath );
  138. while (($file = readdir($dirContent)) !== false) {
  139. if (($file == '..')||($file == '.')) continue;
  140. if( !is_dir($this->_dirPath . '/' . $file) ) {
  141. $result[] = $file;
  142. }
  143. }
  144. closedir($dirContent);
  145. return $result;
  146. }
  147. /**
  148. * Creates a new, empty file in the directory with the given $filename.
  149. *
  150. * @param string $filename
  151. * @return Zend_Search_Lucene_Storage_File
  152. * @throws Zend_Search_Lucene_Exception
  153. */
  154. public function createFile($filename)
  155. {
  156. if (isset($this->_fileHandlers[$filename])) {
  157. $this->_fileHandlers[$filename]->close();
  158. }
  159. unset($this->_fileHandlers[$filename]);
  160. require_once 'Zend/Search/Lucene/Storage/File/Filesystem.php';
  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. require_once 'Zend/Search/Lucene/Storage/File/Filesystem.php';
  300. if (!$shareHandler) {
  301. return new Zend_Search_Lucene_Storage_File_Filesystem($fullFilename);
  302. }
  303. if (isset( $this->_fileHandlers[$filename] )) {
  304. $this->_fileHandlers[$filename]->seek(0);
  305. return $this->_fileHandlers[$filename];
  306. }
  307. $this->_fileHandlers[$filename] = new Zend_Search_Lucene_Storage_File_Filesystem($fullFilename);
  308. return $this->_fileHandlers[$filename];
  309. }
  310. }