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:
36: class Mage_Catalog_Model_Indexer_Url extends Mage_Index_Model_Indexer_Abstract
37: {
38: 39: 40:
41: const EVENT_MATCH_RESULT_KEY = 'catalog_url_match_result';
42:
43: 44: 45: 46: 47: 48:
49: protected $_matchedEntities = array(
50: Mage_Catalog_Model_Product::ENTITY => array(
51: Mage_Index_Model_Event::TYPE_SAVE
52: ),
53: Mage_Catalog_Model_Category::ENTITY => array(
54: Mage_Index_Model_Event::TYPE_SAVE
55: ),
56: Mage_Core_Model_Store::ENTITY => array(
57: Mage_Index_Model_Event::TYPE_SAVE
58: ),
59: Mage_Core_Model_Store_Group::ENTITY => array(
60: Mage_Index_Model_Event::TYPE_SAVE
61: ),
62: Mage_Core_Model_Config_Data::ENTITY => array(
63: Mage_Index_Model_Event::TYPE_SAVE
64: ),
65: Mage_Catalog_Model_Convert_Adapter_Product::ENTITY => array(
66: Mage_Index_Model_Event::TYPE_SAVE
67: )
68: );
69:
70: protected $_relatedConfigSettings = array(
71: Mage_Catalog_Helper_Category::XML_PATH_CATEGORY_URL_SUFFIX,
72: Mage_Catalog_Helper_Product::XML_PATH_PRODUCT_URL_SUFFIX,
73: Mage_Catalog_Helper_Product::XML_PATH_PRODUCT_URL_USE_CATEGORY,
74: );
75:
76: 77: 78: 79: 80:
81: public function getName()
82: {
83: return Mage::helper('catalog')->__('Catalog URL Rewrites');
84: }
85:
86: 87: 88: 89: 90:
91: public function getDescription()
92: {
93: return Mage::helper('catalog')->__('Index product and categories URL rewrites');
94: }
95:
96: 97: 98: 99: 100: 101: 102:
103: public function matchEvent(Mage_Index_Model_Event $event)
104: {
105: $data = $event->getNewData();
106: if (isset($data[self::EVENT_MATCH_RESULT_KEY])) {
107: return $data[self::EVENT_MATCH_RESULT_KEY];
108: }
109:
110: $entity = $event->getEntity();
111: if ($entity == Mage_Core_Model_Store::ENTITY) {
112: $store = $event->getDataObject();
113: if ($store && ($store->isObjectNew() || $store->dataHasChangedFor('group_id'))) {
114: $result = true;
115: } else {
116: $result = false;
117: }
118: } else if ($entity == Mage_Core_Model_Store_Group::ENTITY) {
119: $storeGroup = $event->getDataObject();
120: $hasDataChanges = $storeGroup && ($storeGroup->dataHasChangedFor('root_category_id')
121: || $storeGroup->dataHasChangedFor('website_id'));
122: if ($storeGroup && !$storeGroup->isObjectNew() && $hasDataChanges) {
123: $result = true;
124: } else {
125: $result = false;
126: }
127: } else if ($entity == Mage_Core_Model_Config_Data::ENTITY) {
128: $configData = $event->getDataObject();
129: if ($configData && in_array($configData->getPath(), $this->_relatedConfigSettings)) {
130: $result = $configData->isValueChanged();
131: } else {
132: $result = false;
133: }
134: } else {
135: $result = parent::matchEvent($event);
136: }
137:
138: $event->addNewData(self::EVENT_MATCH_RESULT_KEY, $result);
139:
140: return $result;
141: }
142:
143: 144: 145: 146: 147:
148: protected function _registerEvent(Mage_Index_Model_Event $event)
149: {
150: $event->addNewData(self::EVENT_MATCH_RESULT_KEY, true);
151: $entity = $event->getEntity();
152: switch ($entity) {
153: case Mage_Catalog_Model_Product::ENTITY:
154: $this->_registerProductEvent($event);
155: break;
156:
157: case Mage_Catalog_Model_Category::ENTITY:
158: $this->_registerCategoryEvent($event);
159: break;
160:
161: case Mage_Catalog_Model_Convert_Adapter_Product::ENTITY:
162: $event->addNewData('catalog_url_reindex_all', true);
163: break;
164:
165: case Mage_Core_Model_Store::ENTITY:
166: case Mage_Core_Model_Store_Group::ENTITY:
167: case Mage_Core_Model_Config_Data::ENTITY:
168: $process = $event->getProcess();
169: $process->changeStatus(Mage_Index_Model_Process::STATUS_REQUIRE_REINDEX);
170: break;
171: }
172: return $this;
173: }
174:
175: 176: 177: 178: 179:
180: protected function _registerProductEvent(Mage_Index_Model_Event $event)
181: {
182: $product = $event->getDataObject();
183: $dataChange = $product->dataHasChangedFor('url_key')
184: || $product->getIsChangedCategories()
185: || $product->getIsChangedWebsites();
186:
187: if (!$product->getExcludeUrlRewrite() && $dataChange) {
188: $event->addNewData('rewrite_product_ids', array($product->getId()));
189: }
190: }
191:
192: 193: 194: 195: 196:
197: protected function _registerCategoryEvent(Mage_Index_Model_Event $event)
198: {
199: $category = $event->getDataObject();
200: if (!$category->getInitialSetupFlag() && $category->getLevel() > 1) {
201: if ($category->dataHasChangedFor('url_key') || $category->getIsChangedProductList()) {
202: $event->addNewData('rewrite_category_ids', array($category->getId()));
203: }
204: 205: 206:
207: if ($category->getAffectedCategoryIds()) {
208: $event->addNewData('rewrite_category_ids', $category->getAffectedCategoryIds());
209: }
210: }
211: }
212:
213: 214: 215: 216: 217:
218: protected function _processEvent(Mage_Index_Model_Event $event)
219: {
220: $data = $event->getNewData();
221: if (!empty($data['catalog_url_reindex_all'])) {
222: $this->reindexAll();
223: }
224:
225:
226: $urlModel = Mage::getSingleton('catalog/url');
227:
228:
229: $dataObject = $event->getDataObject();
230: if ($dataObject instanceof Varien_Object && $dataObject->hasData('save_rewrites_history')) {
231: $urlModel->setShouldSaveRewritesHistory($dataObject->getData('save_rewrites_history'));
232: }
233:
234: if(isset($data['rewrite_product_ids'])) {
235: $urlModel->clearStoreInvalidRewrites();
236: foreach ($data['rewrite_product_ids'] as $productId) {
237: $urlModel->refreshProductRewrite($productId);
238: }
239: }
240: if (isset($data['rewrite_category_ids'])) {
241: $urlModel->clearStoreInvalidRewrites();
242: foreach ($data['rewrite_category_ids'] as $categoryId) {
243: $urlModel->refreshCategoryRewrite($categoryId);
244: }
245: }
246: }
247:
248: 249: 250:
251: public function reindexAll()
252: {
253:
254: $resourceModel = Mage::getResourceSingleton('catalog/url');
255: $resourceModel->beginTransaction();
256: try {
257: Mage::getSingleton('catalog/url')->refreshRewrites();
258: $resourceModel->commit();
259: } catch (Exception $e) {
260: $resourceModel->rollBack();
261: throw $e;
262: }
263: }
264: }
265: