00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_CatalogIndex_Model_Indexer extends Mage_Core_Model_Abstract
00036 {
00037 const REINDEX_TYPE_ALL = 0;
00038 const REINDEX_TYPE_PRICE = 1;
00039 const REINDEX_TYPE_ATTRIBUTE = 2;
00040
00041 const STEP_SIZE = 1000;
00042
00043
00044
00045
00046
00047
00048
00049 protected $_indexers = array();
00050
00051
00052
00053
00054
00055
00056 protected $_priceIndexers = array('price', 'tier_price', 'minimal_price');
00057
00058
00059
00060
00061
00062
00063
00064 protected $_attributeIndexers = array('eav');
00065
00066
00067
00068
00069
00070
00071 protected $_productTypePriority = null;
00072
00073
00074
00075
00076
00077 protected function _construct()
00078 {
00079 $this->_loadIndexers();
00080 $this->_init('catalogindex/indexer');
00081 }
00082
00083
00084
00085
00086
00087
00088 protected function _loadIndexers()
00089 {
00090 foreach ($this->_getRegisteredIndexers() as $name=>$class) {
00091 $this->_indexers[$name] = Mage::getSingleton($class);
00092 }
00093 return $this;
00094 }
00095
00096
00097
00098
00099
00100
00101 protected function _getRegisteredIndexers()
00102 {
00103 $result = array();
00104 $indexerRegistry = Mage::getConfig()->getNode('global/catalogindex/indexer');
00105
00106 foreach ($indexerRegistry->children() as $node) {
00107 $result[$node->getName()] = (string) $node->class;
00108 }
00109 return $result;
00110 }
00111
00112
00113
00114
00115
00116
00117
00118 protected function _getIndexableAttributeCodes()
00119 {
00120 $result = array();
00121 foreach ($this->_indexers as $indexer) {
00122 $codes = $indexer->getIndexableAttributeCodes();
00123
00124 if (is_array($codes))
00125 $result = array_merge($result, $codes);
00126 }
00127 return $result;
00128 }
00129
00130
00131
00132
00133
00134
00135 protected function _getStores()
00136 {
00137 $stores = $this->getData('_stores');
00138 if (is_null($stores)) {
00139 $stores = Mage::app()->getStores();
00140 $this->setData('_stores', $stores);
00141 }
00142 return $stores;
00143 }
00144
00145
00146
00147
00148
00149
00150 protected function _getWebsites()
00151 {
00152 $websites = $this->getData('_websites');
00153 if (is_null($websites)) {
00154 $websites = Mage::getModel('core/website')->getCollection()->load();
00155
00156
00157 $this->setData('_websites', $websites);
00158 }
00159 return $websites;
00160 }
00161
00162
00163
00164
00165
00166
00167
00168 public function cleanup($product) {
00169 $this->_getResource()->clear(true, true, true, true, true, $product);
00170 return $this;
00171 }
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 public function plainReindex($products = null, $attributes = null, $stores = null)
00182 {
00183
00184
00185
00186 $flag = Mage::getModel('catalogindex/catalog_index_flag')->loadSelf();
00187 if ($flag->getState() == Mage_CatalogIndex_Model_Catalog_Index_Flag::STATE_RUNNING) {
00188 return $this;
00189 }
00190
00191 else {
00192 $flag->setState(Mage_CatalogIndex_Model_Catalog_Index_Flag::STATE_RUNNING)->save();
00193 }
00194
00195 try {
00196
00197
00198
00199 $websites = array();
00200 $attributeCodes = $priceAttributeCodes = array();
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211 if (is_null($stores)) {
00212 $stores = $this->_getStores();
00213 $websites = $this->_getWebsites();
00214 }
00215 elseif ($stores instanceof Mage_Core_Model_Store) {
00216 $websites[] = $stores->getWebsiteId();
00217 $stores = array($stores);
00218 }
00219 elseif (is_array($stores)) {
00220 foreach ($stores as $one) {
00221 $websites[] = Mage::app()->getStore($one)->getWebsiteId();
00222 }
00223 }
00224 elseif (!is_array($stores)) {
00225 Mage::throwException('Invalid stores supplied for indexing');
00226 }
00227
00228
00229
00230
00231 if (is_null($attributes)) {
00232 $priceAttributeCodes = $this->_indexers['price']->getIndexableAttributeCodes();
00233 $attributeCodes = $this->_indexers['eav']->getIndexableAttributeCodes();
00234 }
00235 elseif ($attributes instanceof Mage_Eav_Model_Entity_Attribute_Abstract) {
00236 if ($this->_indexers['eav']->isAttributeIndexable($attributes)) {
00237 $attributeCodes[] = $attributes->getAttributeId();
00238 }
00239 if ($this->_indexers['price']->isAttributeIndexable($attributes)) {
00240 $priceAttributeCodes[] = $attributes->getAttributeId();
00241 }
00242 }
00243 elseif ($attributes == self::REINDEX_TYPE_PRICE) {
00244 $priceAttributeCodes = $this->_indexers['price']->getIndexableAttributeCodes();
00245 }
00246 elseif ($attributes == self::REINDEX_TYPE_ATTRIBUTE) {
00247 $attributeCodes = $this->_indexers['eav']->getIndexableAttributeCodes();
00248 }
00249 else {
00250 Mage::throwException('Invalid attributes supplied for indexing');
00251 }
00252
00253
00254
00255
00256 $this->_getResource()->clear(
00257 $attributeCodes,
00258 $priceAttributeCodes,
00259 count($priceAttributeCodes)>0,
00260 count($priceAttributeCodes)>0,
00261 count($priceAttributeCodes)>0,
00262 $products,
00263 $stores
00264 );
00265
00266
00267
00268
00269
00270 foreach ($websites as $website) {
00271 $ws = Mage::app()->getWebsite($website);
00272 if (!$ws) {
00273 continue;
00274 }
00275
00276 $group = $ws->getDefaultGroup();
00277 if (!$group) {
00278 continue;
00279 }
00280
00281 $store = $group->getDefaultStore();
00282
00283
00284
00285
00286 if (!$store) {
00287 continue;
00288 }
00289
00290 foreach ($this->_getPriorifiedProductTypes() as $type) {
00291 $collection = $this->_getProductCollection($store, $products);
00292 $collection->addAttributeToFilter(
00293 'status',
00294 array('in'=>Mage::getModel('catalog/product_status')->getSaleableStatusIds())
00295 );
00296 $collection->addFieldToFilter('type_id', $type);
00297 $this->_walkCollection($collection, $store, array(), $priceAttributeCodes);
00298 if (!is_null($products) && !$this->getRetreiver($type)->getTypeInstance()->isComposite()) {
00299 $this->_walkCollectionRelation($collection, $ws, array(), $priceAttributeCodes);
00300 }
00301 }
00302 }
00303
00304
00305
00306
00307 foreach ($stores as $store) {
00308 foreach ($this->_getPriorifiedProductTypes() as $type) {
00309 $collection = $this->_getProductCollection($store, $products);
00310 Mage::getSingleton('catalog/product_visibility')->addVisibleInSiteFilterToCollection($collection);
00311 $collection->addFieldToFilter('type_id', $type);
00312
00313 $this->_walkCollection($collection, $store, $attributeCodes);
00314 if (!is_null($products) && !$this->getRetreiver($type)->getTypeInstance()->isComposite()) {
00315 $this->_walkCollectionRelation($collection, $store, $attributeCodes);
00316 }
00317 }
00318 }
00319
00320 $this->_afterPlainReindex($stores, $products);
00321
00322
00323
00324
00325 if (Mage::helper('catalog/product_flat')->isBuilt()) {
00326 foreach ($stores as $store) {
00327 $this->updateCatalogProductFlat($store, $products);
00328 }
00329 }
00330
00331 } catch (Exception $e) {
00332 $flag->delete();
00333 throw $e;
00334 }
00335
00336 if ($flag->getState() == Mage_CatalogIndex_Model_Catalog_Index_Flag::STATE_RUNNING) {
00337 $flag->delete();
00338 }
00339
00340 return $this;
00341 }
00342
00343
00344
00345
00346
00347
00348
00349
00350 protected function _afterPlainReindex($store, $products = null)
00351 {
00352 Mage::dispatchEvent('catalogindex_plain_reindex_after', array(
00353 'products' => $products
00354 ));
00355
00356
00357
00358
00359 if (Mage::helper('catalog/product_flat')->isBuilt()) {
00360 if ($store instanceof Mage_Core_Model_Website) {
00361 foreach ($store->getStores() as $storeObject) {
00362 $this->_afterPlainReindex($storeObject->getId(), $products);
00363 }
00364 return $this;
00365 }
00366 elseif ($store instanceof Mage_Core_Model_Store) {
00367 $store = $store->getId();
00368 }
00369
00370 elseif (is_array($store)) {
00371 foreach ($store as $storeObject) {
00372 $this->_afterPlainReindex($storeObject->getId(), $products);
00373 }
00374 return $this;
00375 }
00376
00377 $this->updateCatalogProductFlat($store, $products);
00378 }
00379
00380 return $this;
00381 }
00382
00383
00384
00385
00386
00387
00388
00389
00390 protected function _getProductCollection($store, $products)
00391 {
00392 $collection = Mage::getModel('catalog/product')
00393 ->getCollection()
00394 ->setStoreId($store)
00395 ->addStoreFilter($store);
00396 if ($products instanceof Mage_Catalog_Model_Product) {
00397 $collection->addIdFilter($products->getId());
00398 } else if (is_array($products) || is_numeric($products)) {
00399 $collection->addIdFilter($products);
00400 } elseif ($products instanceof Mage_Catalog_Model_Product_Condition_Interface) {
00401 $products->applyToCollection($collection);
00402 }
00403
00404 return $collection;
00405 }
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416 public function _walkCollectionRelation($collection, $store, $attributes = array(), $prices = array())
00417 {
00418 if ($store instanceof Mage_Core_Model_Website) {
00419 $storeObject = $store->getDefaultStore();
00420 }
00421 elseif ($store instanceof Mage_Core_Model_Store) {
00422 $storeObject = $store;
00423 }
00424
00425 $statusCond = array(
00426 'in' => Mage::getSingleton('catalog/product_status')->getSaleableStatusIds()
00427 );
00428
00429 $productCount = $collection->getSize();
00430 $iterateCount = ($productCount / self::STEP_SIZE);
00431 for ($i = 0; $i < $iterateCount; $i++) {
00432 $stepData = $collection
00433 ->getAllIds(self::STEP_SIZE, $i * self::STEP_SIZE);
00434 foreach ($this->_getPriorifiedProductTypes() as $type) {
00435 $retriever = $this->getRetreiver($type);
00436 if (!$retriever->getTypeInstance()->isComposite()) {
00437 continue;
00438 }
00439
00440 $parentIds = $retriever->getTypeInstance()
00441 ->getParentIdsByChild($stepData);
00442 if ($parentIds) {
00443 $parentCollection = $this->_getProductCollection($storeObject, $parentIds);
00444 $parentCollection->addAttributeToFilter('status', $statusCond);
00445 $parentCollection->addFieldToFilter('type_id', $type);
00446 $this->_walkCollection($parentCollection, $storeObject, $attributes, $prices);
00447
00448 $this->_afterPlainReindex($store, $parentIds);
00449 }
00450 }
00451 }
00452
00453 return $this;
00454 }
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465 protected function _walkCollection($collection, $store, $attributes = array(), $prices = array())
00466 {
00467 $productCount = $collection->getSize();
00468 if (!$productCount) {
00469 return $this;
00470 }
00471
00472 for ($i=0;$i<$productCount/self::STEP_SIZE;$i++) {
00473 $this->_getResource()->beginTransaction();
00474
00475 $stepData = $collection->getAllIds(self::STEP_SIZE, $i*self::STEP_SIZE);
00476
00477
00478
00479
00480 if (count($attributes)) {
00481 $this->_getResource()->reindexAttributes($stepData, $attributes, $store);
00482 }
00483
00484
00485
00486
00487 if (count($prices)) {
00488 $this->_getResource()->reindexPrices($stepData, $prices, $store);
00489 $this->_getResource()->reindexTiers($stepData, $store);
00490 $this->_getResource()->reindexMinimalPrices($stepData, $store);
00491 $this->_getResource()->reindexFinalPrices($stepData, $store);
00492 }
00493
00494 Mage::getResourceSingleton('catalog/product')->refreshEnabledIndex($store, $stepData);
00495
00496 $kill = Mage::getModel('catalogindex/catalog_index_kill_flag')->loadSelf();
00497 if ($kill->checkIsThisProcess()) {
00498 $this->_getResource()->rollBack();
00499 $kill->delete();
00500 } else {
00501 $this->_getResource()->commit();
00502 }
00503 }
00504 return $this;
00505 }
00506
00507
00508
00509
00510
00511
00512
00513 public function getRetreiver($type)
00514 {
00515 return Mage::getSingleton('catalogindex/retreiver')->getRetreiver($type);
00516 }
00517
00518
00519
00520
00521
00522
00523 public function queueIndexing()
00524 {
00525 Mage::getModel('catalogindex/catalog_index_flag')
00526 ->loadSelf()
00527 ->setState(Mage_CatalogIndex_Model_Catalog_Index_Flag::STATE_QUEUED)
00528 ->save();
00529
00530 return $this;
00531 }
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541 protected function _getPriorifiedProductTypes()
00542 {
00543 if (is_null($this->_productTypePriority)) {
00544 $this->_productTypePriority = array();
00545 $config = Mage::getConfig()->getNode('global/catalog/product/type');
00546
00547 foreach ($config->children() as $type) {
00548 $typeName = $type->getName();
00549 $typePriority = (string) $type->index_priority;
00550 $this->_productTypePriority[$typePriority] = $typeName;
00551 }
00552 ksort($this->_productTypePriority);
00553 }
00554 return $this->_productTypePriority;
00555 }
00556
00557
00558
00559
00560
00561
00562
00563 protected function _getBaseToSpecifiedCurrencyRate($code)
00564 {
00565 return Mage::app()->getStore()->getBaseCurrency()->getRate($code);
00566 }
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577 public function buildEntityPriceFilter($attributes, $values, &$filteredAttributes, $productCollection)
00578 {
00579 $additionalCalculations = array();
00580 $filter = array();
00581 $store = Mage::app()->getStore()->getId();
00582 $website = Mage::app()->getStore()->getWebsiteId();
00583
00584 $currentStoreCurrency = Mage::app()->getStore()->getCurrentCurrencyCode();
00585
00586 foreach ($attributes as $attribute) {
00587 $code = $attribute->getAttributeCode();
00588 if (isset($values[$code])) {
00589 foreach ($this->_priceIndexers as $indexerName) {
00590 $indexer = $this->_indexers[$indexerName];
00591
00592 if ($indexer->isAttributeIndexable($attribute)) {
00593 if ($values[$code]) {
00594 if (isset($values[$code]['from']) && isset($values[$code]['to'])
00595 && (strlen($values[$code]['from']) == 0 && strlen($values[$code]['to']) == 0)) {
00596 continue;
00597 }
00598 $table = $indexer->getResource()->getMainTable();
00599 if (!isset($filter[$code])) {
00600 $filter[$code] = $this->_getSelect();
00601 $filter[$code]->from($table, array('entity_id'));
00602 $filter[$code]->distinct(true);
00603
00604 $response = new Varien_Object();
00605 $response->setAdditionalCalculations(array());
00606 $args = array(
00607 'select'=>$filter[$code],
00608 'table'=>$table,
00609 'store_id'=>$store,
00610 'response_object'=>$response,
00611 );
00612 Mage::dispatchEvent('catalogindex_prepare_price_select', $args);
00613 $additionalCalculations[$code] = $response->getAdditionalCalculations();
00614
00615 if ($indexer->isAttributeIdUsed()) {
00616 $filter[$code]->where("$table.attribute_id = ?", $attribute->getId());
00617 }
00618 }
00619 if (is_array($values[$code])) {
00620 $rateConversion = 1;
00621 $filter[$code]->distinct(true);
00622
00623 if (isset($values[$code]['from']) && isset($values[$code]['to'])) {
00624 if (isset($values[$code]['currency'])) {
00625 $rateConversion = $this->_getBaseToSpecifiedCurrencyRate($values[$code]['currency']);
00626 } else {
00627 $rateConversion = $this->_getBaseToSpecifiedCurrencyRate($currentStoreCurrency);
00628 }
00629
00630 if (strlen($values[$code]['from'])>0) {
00631 $filter[$code]->where(
00632 "($table.value".implode('', $additionalCalculations[$code]).")*{$rateConversion} >= ?",
00633 $values[$code]['from']
00634 );
00635 }
00636
00637 if (strlen($values[$code]['to'])>0) {
00638 $filter[$code]->where(
00639 "($table.value".implode('', $additionalCalculations[$code]).")*{$rateConversion} <= ?",
00640 $values[$code]['to']
00641 );
00642 }
00643 }
00644 }
00645 $filter[$code]->where("$table.website_id = ?", $website);
00646
00647 if ($code == 'price') {
00648 $filter[$code]->where(
00649 $table . '.customer_group_id = ?',
00650 Mage::getSingleton('customer/session')->getCustomerGroupId()
00651 );
00652 }
00653
00654 $filteredAttributes[]=$code;
00655 }
00656 }
00657 }
00658 }
00659 }
00660 return $filter;
00661 }
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672 public function buildEntityFilter($attributes, $values, &$filteredAttributes, $productCollection)
00673 {
00674 $filter = array();
00675 $store = Mage::app()->getStore()->getId();
00676
00677 foreach ($attributes as $attribute) {
00678 $code = $attribute->getAttributeCode();
00679 if (isset($values[$code])) {
00680 foreach ($this->_attributeIndexers as $indexerName) {
00681 $indexer = $this->_indexers[$indexerName];
00682
00683 if ($indexer->isAttributeIndexable($attribute)) {
00684 if ($values[$code]) {
00685 if (isset($values[$code]['from']) && isset($values[$code]['to'])
00686 && (!$values[$code]['from'] && !$values[$code]['to'])) {
00687 continue;
00688 }
00689
00690 $table = $indexer->getResource()->getMainTable();
00691 if (!isset($filter[$code])) {
00692 $filter[$code] = $this->_getSelect();
00693 $filter[$code]->from($table, array('entity_id'));
00694 }
00695 if ($indexer->isAttributeIdUsed()) {
00696 $filter[$code]->where('attribute_id = ?', $attribute->getId());
00697 }
00698 if (is_array($values[$code])) {
00699 if (isset($values[$code]['from']) && isset($values[$code]['to'])) {
00700
00701 if ($values[$code]['from']) {
00702 if (!is_numeric($values[$code]['from'])) {
00703 $values[$code]['from'] = date("Y-m-d H:i:s", strtotime($values[$code]['from']));
00704 }
00705
00706 $filter[$code]->where("value >= ?", $values[$code]['from']);
00707 }
00708
00709
00710 if ($values[$code]['to']) {
00711 if (!is_numeric($values[$code]['to'])) {
00712 $values[$code]['to'] = date("Y-m-d H:i:s", strtotime($values[$code]['to']));
00713 }
00714 $filter[$code]->where("value <= ?", $values[$code]['to']);
00715 }
00716 } else {
00717 $filter[$code]->where('value in (?)', $values[$code]);
00718 }
00719 } else {
00720 $filter[$code]->where('value = ?', $values[$code]);
00721 }
00722 $filter[$code]->where('store_id = ?', $store);
00723 $filteredAttributes[]=$code;
00724 }
00725 }
00726 }
00727 }
00728 }
00729 return $filter;
00730 }
00731
00732
00733
00734
00735
00736
00737 protected function _getSelect()
00738 {
00739 return $this->_getResource()->getReadConnection()->select();
00740 }
00741
00742
00743
00744
00745
00746
00747
00748
00749 protected function _addFilterableAttributesToCollection($collection)
00750 {
00751 $attributeCodes = $this->_getIndexableAttributeCodes();
00752 foreach ($attributeCodes as $code) {
00753 $collection->addAttributeToSelect($code);
00754 }
00755
00756 return $this;
00757 }
00758
00759
00760
00761
00762
00763
00764
00765 public function prepareCatalogProductFlatColumns(Varien_Object $object)
00766 {
00767 $this->_getResource()->prepareCatalogProductFlatColumns($object);
00768
00769 return $this;
00770 }
00771
00772
00773
00774
00775
00776
00777
00778 public function prepareCatalogProductFlatIndexes(Varien_Object $object)
00779 {
00780 $this->_getResource()->prepareCatalogProductFlatIndexes($object);
00781
00782 return $this;
00783 }
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793 public function updateCatalogProductFlat($store, $products = null, $resourceTable = null)
00794 {
00795 if ($store instanceof Mage_Core_Model_Store) {
00796 $store = $store->getId();
00797 }
00798 if ($products instanceof Mage_Catalog_Model_Product) {
00799 $products = $products->getId();
00800 }
00801 $this->_getResource()->updateCatalogProductFlat($store, $products, $resourceTable);
00802
00803 return $this;
00804 }
00805 }