PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/modules/zendsearch/vendors/Zend/Search/Lucene/Storage/Directory/Filesystem.php

https://gitlab.com/RonLab1987/YupePlusClear
PHP | 354 lines | 189 code | 33 blank | 132 comment | 20 complexity | eb00a0975e1384db486ced4b56f97faa 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-2012 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 24593 2012-01-05 20:35:02Z matthew $
  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-2012 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 = [];
  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 = [];
  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 = [];
  137. $dirContent = opendir($this->_dirPath);
  138. while (($file = readdir($dirContent)) !== false) {
  139. if (($file == '..') || ($file == '.')) {
  140. continue;
  141. }
  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. require_once 'Zend/Search/Lucene/Storage/File/Filesystem.php';
  163. $this->_fileHandlers[$filename] = new Zend_Search_Lucene_Storage_File_Filesystem($this->_dirPath . '/' . $filename, 'w+b');
  164. // Set file permissions, but don't care about any possible failures, since file may be already
  165. // created by anther user which has to care about right permissions
  166. @chmod($this->_dirPath . '/' . $filename, self::$_defaultFilePermissions);
  167. return $this->_fileHandlers[$filename];
  168. }
  169. /**
  170. * Removes an existing $filename in the directory.
  171. *
  172. * @param string $filename
  173. * @return void
  174. * @throws Zend_Search_Lucene_Exception
  175. */
  176. public function deleteFile($filename)
  177. {
  178. if (isset($this->_fileHandlers[$filename])) {
  179. $this->_fileHandlers[$filename]->close();
  180. }
  181. unset($this->_fileHandlers[$filename]);
  182. global $php_errormsg;
  183. $trackErrors = ini_get('track_errors');
  184. ini_set('track_errors', '1');
  185. if (!@unlink($this->_dirPath . '/' . $filename)) {
  186. ini_set('track_errors', $trackErrors);
  187. require_once 'Zend/Search/Lucene/Exception.php';
  188. throw new Zend_Search_Lucene_Exception('Can\'t delete file: ' . $php_errormsg);
  189. }
  190. ini_set('track_errors', $trackErrors);
  191. }
  192. /**
  193. * Purge file if it's cached by directory object
  194. *
  195. * Method is used to prevent 'too many open files' error
  196. *
  197. * @param string $filename
  198. * @return void
  199. */
  200. public function purgeFile($filename)
  201. {
  202. if (isset($this->_fileHandlers[$filename])) {
  203. $this->_fileHandlers[$filename]->close();
  204. }
  205. unset($this->_fileHandlers[$filename]);
  206. }
  207. /**
  208. * Returns true if a file with the given $filename exists.
  209. *
  210. * @param string $filename
  211. * @return boolean
  212. */
  213. public function fileExists($filename)
  214. {
  215. return isset($this->_fileHandlers[$filename]) ||
  216. file_exists($this->_dirPath . '/' . $filename);
  217. }
  218. /**
  219. * Returns the length of a $filename in the directory.
  220. *
  221. * @param string $filename
  222. * @return integer
  223. */
  224. public function fileLength($filename)
  225. {
  226. if (isset($this->_fileHandlers[$filename])) {
  227. return $this->_fileHandlers[$filename]->size();
  228. }
  229. return filesize($this->_dirPath . '/' . $filename);
  230. }
  231. /**
  232. * Returns the UNIX timestamp $filename was last modified.
  233. *
  234. * @param string $filename
  235. * @return integer
  236. */
  237. public function fileModified($filename)
  238. {
  239. return filemtime($this->_dirPath . '/' . $filename);
  240. }
  241. /**
  242. * Renames an existing file in the directory.
  243. *
  244. * @param string $from
  245. * @param string $to
  246. * @return void
  247. * @throws Zend_Search_Lucene_Exception
  248. */
  249. public function renameFile($from, $to)
  250. {
  251. global $php_errormsg;
  252. if (isset($this->_fileHandlers[$from])) {
  253. $this->_fileHandlers[$from]->close();
  254. }
  255. unset($this->_fileHandlers[$from]);
  256. if (isset($this->_fileHandlers[$to])) {
  257. $this->_fileHandlers[$to]->close();
  258. }
  259. unset($this->_fileHandlers[$to]);
  260. if (file_exists($this->_dirPath . '/' . $to)) {
  261. if (!unlink($this->_dirPath . '/' . $to)) {
  262. require_once 'Zend/Search/Lucene/Exception.php';
  263. throw new Zend_Search_Lucene_Exception('Delete operation failed');
  264. }
  265. }
  266. $trackErrors = ini_get('track_errors');
  267. ini_set('track_errors', '1');
  268. $success = @rename($this->_dirPath . '/' . $from, $this->_dirPath . '/' . $to);
  269. if (!$success) {
  270. ini_set('track_errors', $trackErrors);
  271. require_once 'Zend/Search/Lucene/Exception.php';
  272. throw new Zend_Search_Lucene_Exception($php_errormsg);
  273. }
  274. ini_set('track_errors', $trackErrors);
  275. return $success;
  276. }
  277. /**
  278. * Sets the modified time of $filename to now.
  279. *
  280. * @param string $filename
  281. * @return void
  282. */
  283. public function touchFile($filename)
  284. {
  285. return touch($this->_dirPath . '/' . $filename);
  286. }
  287. /**
  288. * Returns a Zend_Search_Lucene_Storage_File object for a given $filename in the directory.
  289. *
  290. * If $shareHandler option is true, then file handler can be shared between File Object
  291. * requests. It speed-ups performance, but makes problems with file position.
  292. * Shared handler are good for short atomic requests.
  293. * Non-shared handlers are useful for stream file reading (especial for compound files).
  294. *
  295. * @param string $filename
  296. * @param boolean $shareHandler
  297. * @return Zend_Search_Lucene_Storage_File
  298. */
  299. public function getFileObject($filename, $shareHandler = true)
  300. {
  301. $fullFilename = $this->_dirPath . '/' . $filename;
  302. require_once 'Zend/Search/Lucene/Storage/File/Filesystem.php';
  303. if (!$shareHandler) {
  304. return new Zend_Search_Lucene_Storage_File_Filesystem($fullFilename);
  305. }
  306. if (isset($this->_fileHandlers[$filename])) {
  307. $this->_fileHandlers[$filename]->seek(0);
  308. return $this->_fileHandlers[$filename];
  309. }
  310. $this->_fileHandlers[$filename] = new Zend_Search_Lucene_Storage_File_Filesystem($fullFilename);
  311. return $this->_fileHandlers[$filename];
  312. }
  313. }