PageRenderTime 27ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

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