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

/app/code/core/Mage/Core/Model/File/Storage/Database.php

https://gitlab.com/blingbang2016/shop
PHP | 333 lines | 147 code | 40 blank | 146 comment | 11 complexity | c8cd146d38f94cb1a34aa7ba4807e9d5 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  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@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * File storage database model class
  28. *
  29. * @category Mage
  30. * @package Mage_Core
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Core_Model_File_Storage_Database extends Mage_Core_Model_File_Storage_Database_Abstract
  34. {
  35. /**
  36. * Prefix of model events names
  37. *
  38. * @var string
  39. */
  40. protected $_eventPrefix = 'core_file_storage_database';
  41. /**
  42. * Directory singleton
  43. *
  44. * @var Mage_Core_Model_File_Storage_Directory_Database
  45. */
  46. protected $_directoryModel = null;
  47. /**
  48. * Collect errors during sync process
  49. *
  50. * @var array
  51. */
  52. protected $_errors = array();
  53. /**
  54. * Class construct
  55. *
  56. * @param string $connectionName
  57. */
  58. public function __construct($connectionName = null)
  59. {
  60. $this->_init('core/file_storage_database');
  61. parent::__construct($connectionName);
  62. }
  63. /**
  64. * Retrieve directory model
  65. *
  66. * @return Mage_Core_Model_File_Storage_Directory_Database
  67. */
  68. public function getDirectoryModel()
  69. {
  70. if (is_null($this->_directoryModel)) {
  71. $this->_directoryModel = Mage::getModel(
  72. 'core/file_storage_directory_database',
  73. array('connection' => $this->getConnectionName()));
  74. }
  75. return $this->_directoryModel;
  76. }
  77. /**
  78. * Create tables for file and directory storages
  79. *
  80. * @return Mage_Core_Model_File_Storage_Database
  81. */
  82. public function init()
  83. {
  84. $this->getDirectoryModel()->prepareStorage();
  85. $this->prepareStorage();
  86. return $this;
  87. }
  88. /**
  89. * Return storage name
  90. *
  91. * @return string
  92. */
  93. public function getStorageName()
  94. {
  95. return Mage::helper('core')->__('database "%s"', $this->getConnectionName());
  96. }
  97. /**
  98. * Load object data by filename
  99. *
  100. * @param string $filePath
  101. * @return Mage_Core_Model_File_Storage_Database
  102. */
  103. public function loadByFilename($filePath)
  104. {
  105. $filename = basename($filePath);
  106. $path = dirname($filePath);
  107. $this->_getResource()->loadByFilename($this, $filename, $path);
  108. return $this;
  109. }
  110. /**
  111. * Check if there was errors during sync process
  112. *
  113. * @return bool
  114. */
  115. public function hasErrors()
  116. {
  117. return (!empty($this->_errors) || $this->getDirectoryModel()->hasErrors());
  118. }
  119. /**
  120. * Clear files and directories in storage
  121. *
  122. * @return Mage_Core_Model_File_Storage_Database
  123. */
  124. public function clear()
  125. {
  126. $this->getDirectoryModel()->clearDirectories();
  127. $this->_getResource()->clearFiles();
  128. return $this;
  129. }
  130. /**
  131. * Export directories from storage
  132. *
  133. * @param int $offset
  134. * @param int $count
  135. * @return bool|array
  136. */
  137. public function exportDirectories($offset = 0, $count = 100) {
  138. return $this->getDirectoryModel()->exportDirectories($offset, $count);
  139. }
  140. /**
  141. * Import directories to storage
  142. *
  143. * @param array $dirs
  144. * @return Mage_Core_Model_File_Storage_Directory_Database
  145. */
  146. public function importDirectories($dirs) {
  147. return $this->getDirectoryModel()->importDirectories($dirs);
  148. }
  149. /**
  150. * Export files list in defined range
  151. *
  152. * @param int $offset
  153. * @param int $count
  154. * @return array|bool
  155. */
  156. public function exportFiles($offset = 0, $count = 100)
  157. {
  158. $offset = ((int) $offset >= 0) ? (int) $offset : 0;
  159. $count = ((int) $count >= 1) ? (int) $count : 1;
  160. $result = $this->_getResource()->getFiles($offset, $count);
  161. if (empty($result)) {
  162. return false;
  163. }
  164. return $result;
  165. }
  166. /**
  167. * Import files list
  168. *
  169. * @param array $files
  170. * @return Mage_Core_Model_File_Storage_Database
  171. */
  172. public function importFiles($files)
  173. {
  174. if (!is_array($files)) {
  175. return $this;
  176. }
  177. $dateSingleton = Mage::getSingleton('core/date');
  178. foreach ($files as $file) {
  179. if (!isset($file['filename']) || !strlen($file['filename']) || !isset($file['content'])) {
  180. continue;
  181. }
  182. try {
  183. $file['update_time'] = $dateSingleton->date();
  184. $file['directory_id'] = (isset($file['directory']) && strlen($file['directory']))
  185. ? Mage::getModel(
  186. 'core/file_storage_directory_database',
  187. array('connection' => $this->getConnectionName()))
  188. ->loadByPath($file['directory'])->getId()
  189. : null;
  190. $this->_getResource()->saveFile($file);
  191. } catch (Exception $e) {
  192. $this->_errors[] = $e->getMessage();
  193. Mage::logException($e);
  194. }
  195. }
  196. return $this;
  197. }
  198. /**
  199. * Store file into database
  200. *
  201. * @param string $filename
  202. * @return Mage_Core_Model_File_Storage_Database
  203. */
  204. public function saveFile($filename)
  205. {
  206. $fileInfo = $this->collectFileInfo($filename);
  207. $filePath = $fileInfo['directory'];
  208. $directory = Mage::getModel('core/file_storage_directory_database')->loadByPath($filePath);
  209. if (!$directory->getId()) {
  210. $directory = $this->getDirectoryModel()->createRecursive($filePath);
  211. }
  212. $fileInfo['directory_id'] = $directory->getId();
  213. $this->_getResource()->saveFile($fileInfo);
  214. return $this;
  215. }
  216. /**
  217. * Check whether file exists in DB
  218. *
  219. * @param string $filePath
  220. * @return bool
  221. */
  222. public function fileExists($filePath)
  223. {
  224. return $this->_getResource()->fileExists(basename($filePath), dirname($filePath));
  225. }
  226. /**
  227. * Copy files
  228. *
  229. * @param string $oldFilePath
  230. * @param string $newFilePath
  231. * @return Mage_Core_Model_File_Storage_Database
  232. */
  233. public function copyFile($oldFilePath, $newFilePath)
  234. {
  235. $this->_getResource()->copyFile(
  236. basename($oldFilePath),
  237. dirname($oldFilePath),
  238. basename($newFilePath),
  239. dirname($newFilePath)
  240. );
  241. return $this;
  242. }
  243. /**
  244. * Rename files in database
  245. *
  246. * @param string $oldFilePath
  247. * @param string $newFilePath
  248. * @return Mage_Core_Model_File_Storage_Database
  249. */
  250. public function renameFile($oldFilePath, $newFilePath)
  251. {
  252. $this->_getResource()->renameFile(
  253. basename($oldFilePath),
  254. dirname($oldFilePath),
  255. basename($newFilePath),
  256. dirname($newFilePath)
  257. );
  258. $newPath = dirname($newFilePath);
  259. $directory = Mage::getModel('core/file_storage_directory_database')->loadByPath($newPath);
  260. if (!$directory->getId()) {
  261. $directory = $this->getDirectoryModel()->createRecursive($newPath);
  262. }
  263. $this->loadByFilename($newFilePath);
  264. if ($this->getId()) {
  265. $this->setDirectoryId($directory->getId())->save();
  266. }
  267. return $this;
  268. }
  269. /**
  270. * Return directory listing
  271. *
  272. * @param string $directory
  273. * @return mixed
  274. */
  275. public function getDirectoryFiles($directory)
  276. {
  277. $directory = Mage::helper('core/file_storage_database')->getMediaRelativePath($directory);
  278. return $this->_getResource()->getDirectoryFiles($directory);
  279. }
  280. /**
  281. * Delete file from database
  282. *
  283. * @param string $path
  284. * @return Mage_Core_Model_File_Storage_Database
  285. */
  286. public function deleteFile($path)
  287. {
  288. $filename = basename($path);
  289. $directory = dirname($path);
  290. $this->_getResource()->deleteFile($filename, $directory);
  291. return $this;
  292. }
  293. }