1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Core_Model_File_Storage extends Mage_Core_Model_Abstract
36: {
37: 38: 39:
40: const STORAGE_MEDIA_FILE_SYSTEM = 0;
41: const STORAGE_MEDIA_DATABASE = 1;
42:
43: 44: 45:
46: const XML_PATH_STORAGE_MEDIA = 'default/system/media_storage_configuration/media_storage';
47: const XML_PATH_STORAGE_MEDIA_DATABASE = 'default/system/media_storage_configuration/media_database';
48: const XML_PATH_MEDIA_RESOURCE_WHITELIST = 'default/system/media_storage_configuration/allowed_resources';
49: const XML_PATH_MEDIA_UPDATE_TIME = 'system/media_storage_configuration/configuration_update_time';
50:
51:
52: 53: 54: 55: 56:
57: protected $_eventPrefix = 'core_file_storage';
58:
59: 60: 61: 62: 63: 64: 65:
66: protected function _synchronizeHasErrors(Mage_Core_Model_Abstract $sourceModel,
67: Mage_Core_Model_Abstract $destinationModel
68: ) {
69: if (!$sourceModel || !$destinationModel) {
70: return true;
71: }
72:
73: return $sourceModel->hasErrors() || $destinationModel->hasErrors();
74: }
75:
76: 77: 78: 79: 80:
81: public function getSyncFlag()
82: {
83: return Mage::getSingleton('core/file_storage_flag')->loadSelf();
84: }
85:
86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98:
99: public function getStorageModel($storage = null, $params = array())
100: {
101: if (is_null($storage)) {
102: $storage = Mage::helper('core/file_storage')->getCurrentStorageCode();
103: }
104:
105: switch ($storage) {
106: case self::STORAGE_MEDIA_FILE_SYSTEM:
107: $model = Mage::getModel('core/file_storage_file');
108: break;
109: case self::STORAGE_MEDIA_DATABASE:
110: $connection = (isset($params['connection'])) ? $params['connection'] : null;
111: $model = Mage::getModel('core/file_storage_database', array('connection' => $connection));
112: break;
113: default:
114: return false;
115: }
116:
117: if (isset($params['init']) && $params['init']) {
118: $model->init();
119: }
120:
121: return $model;
122: }
123:
124: 125: 126: 127: 128: 129: 130: 131: 132: 133:
134: public function synchronize($storage)
135: {
136: if (is_array($storage) && isset($storage['type'])) {
137: $storageDest = (int) $storage['type'];
138: $connection = (isset($storage['connection'])) ? $storage['connection'] : null;
139: $helper = Mage::helper('core/file_storage');
140:
141:
142: if ($storageDest == $helper->getCurrentStorageCode() && $helper->isInternalStorage()) {
143: return $this;
144: }
145:
146: $sourceModel = $this->getStorageModel();
147: $destinationModel = $this->getStorageModel(
148: $storageDest,
149: array(
150: 'connection' => $connection,
151: 'init' => true
152: )
153: );
154:
155: if (!$sourceModel || !$destinationModel) {
156: return $this;
157: }
158:
159: $hasErrors = false;
160: $flag = $this->getSyncFlag();
161: $flagData = array(
162: 'source' => $sourceModel->getStorageName(),
163: 'destination' => $destinationModel->getStorageName(),
164: 'destination_storage_type' => $storageDest,
165: 'destination_connection_name' => (string) $destinationModel->getConfigConnectionName(),
166: 'has_errors' => false,
167: 'timeout_reached' => false
168: );
169: $flag->setFlagData($flagData);
170:
171: $destinationModel->clear();
172:
173: $offset = 0;
174: while (($dirs = $sourceModel->exportDirectories($offset)) !== false) {
175: $flagData['timeout_reached'] = false;
176: if (!$hasErrors) {
177: $hasErrors = $this->_synchronizeHasErrors($sourceModel, $destinationModel);
178: if ($hasErrors) {
179: $flagData['has_errors'] = true;
180: }
181: }
182:
183: $flag->setFlagData($flagData)
184: ->save();
185:
186: $destinationModel->importDirectories($dirs);
187: $offset += count($dirs);
188: }
189: unset($dirs);
190:
191: $offset = 0;
192: while (($files = $sourceModel->exportFiles($offset, 1)) !== false) {
193: $flagData['timeout_reached'] = false;
194: if (!$hasErrors) {
195: $hasErrors = $this->_synchronizeHasErrors($sourceModel, $destinationModel);
196: if ($hasErrors) {
197: $flagData['has_errors'] = true;
198: }
199: }
200:
201: $flag->setFlagData($flagData)
202: ->save();
203:
204: $destinationModel->importFiles($files);
205: $offset += count($files);
206: }
207: unset($files);
208: }
209:
210: return $this;
211: }
212:
213: 214: 215: 216: 217:
218: public static function getScriptConfig()
219: {
220: $config = array();
221: $config['media_directory'] = Mage::getBaseDir('media');
222:
223: $allowedResources = (array) Mage::app()->getConfig()->getNode(self::XML_PATH_MEDIA_RESOURCE_WHITELIST);
224: foreach ($allowedResources as $key => $allowedResource) {
225: $config['allowed_resources'][] = $allowedResource;
226: }
227:
228: $config['update_time'] = Mage::getStoreConfig(self::XML_PATH_MEDIA_UPDATE_TIME);
229:
230: return $config;
231: }
232: }
233: