PageRenderTime 29ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/web/concrete/libraries/3rdparty/Zend/Search/Lucene/Storage/Directory/Filesystem.php

https://github.com/sidealice/concrete5
PHP | 363 lines | 189 code | 42 blank | 132 comment | 20 complexity | 9acefb2a591a2b4d564a984ac94936ac 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-2011 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 23964 2011-05-03 14:20:58Z adamlundrigan $
  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-2011 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');
  182. 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. require_once 'Zend/Search/Lucene/Storage/File/Filesystem.php';
  301. if (!$shareHandler) {
  302. return new Zend_Search_Lucene_Storage_File_Filesystem($fullFilename);
  303. }
  304. if (isset( $this->_fileHandlers[$filename] )) {
  305. $this->_fileHandlers[$filename]->seek(0);
  306. return $this->_fileHandlers[$filename];
  307. }
  308. $this->_fileHandlers[$filename] = new Zend_Search_Lucene_Storage_File_Filesystem($fullFilename);
  309. return $this->_fileHandlers[$filename];
  310. }
  311. }