Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_Index_Adminhtml_ProcessController
  • Mage_Index_Block_Adminhtml_Notifications
  • Mage_Index_Block_Adminhtml_Process
  • Mage_Index_Block_Adminhtml_Process_Edit
  • Mage_Index_Block_Adminhtml_Process_Edit_Form
  • Mage_Index_Block_Adminhtml_Process_Edit_Tab_Main
  • Mage_Index_Block_Adminhtml_Process_Edit_Tabs
  • Mage_Index_Block_Adminhtml_Process_Grid
  • Mage_Index_Block_Adminhtml_Process_Grid_Massaction
  • Mage_Index_Helper_Data
  • Mage_Index_Model_Event
  • Mage_Index_Model_Indexer
  • Mage_Index_Model_Indexer_Abstract
  • Mage_Index_Model_Mysql4_Abstract
  • Mage_Index_Model_Mysql4_Event
  • Mage_Index_Model_Mysql4_Event_Collection
  • Mage_Index_Model_Mysql4_Process
  • Mage_Index_Model_Mysql4_Process_Collection
  • Mage_Index_Model_Mysql4_Setup
  • Mage_Index_Model_Observer
  • Mage_Index_Model_Process
  • Mage_Index_Model_Resource_Abstract
  • Mage_Index_Model_Resource_Event
  • Mage_Index_Model_Resource_Event_Collection
  • Mage_Index_Model_Resource_Helper_Mysql4
  • Mage_Index_Model_Resource_Process
  • Mage_Index_Model_Resource_Process_Collection
  • Mage_Index_Model_Resource_Setup
  • Overview
  • Package
  • Class
  • Tree
  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@magentocommerce.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.magentocommerce.com for more information.
 20:  *
 21:  * @category    Mage
 22:  * @package     Mage_Index
 23:  * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 24:  * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 25:  */
 26: 
 27: /**
 28:  * Indexer strategy
 29:  */
 30: class Mage_Index_Model_Indexer
 31: {
 32:     /**
 33:      * Collection of available processes
 34:      *
 35:      * @var Mage_Index_Model_Resource_Process_Collection
 36:      */
 37:     protected $_processesCollection;
 38: 
 39:     /**
 40:      * Indexer processes lock flag
 41:      *
 42:      * @deprecated after 1.6.1.0
 43:      * @var bool
 44:      */
 45:     protected $_lockFlag = false;
 46: 
 47:     /**
 48:      * Whether table changes are allowed
 49:      *
 50:      * @var bool
 51:      */
 52:     protected $_allowTableChanges = true;
 53: 
 54:     /**
 55:      * Current processing event(s)
 56:      * In array case it should be array(Entity type, Event type)
 57:      *
 58:      * @var null|Mage_Index_Model_Event|array
 59:      */
 60:     protected $_currentEvent = null;
 61: 
 62:     /**
 63:      * Class constructor. Initialize index processes based on configuration
 64:      */
 65:     public function __construct()
 66:     {
 67:         $this->_processesCollection = Mage::getResourceModel('index/process_collection');
 68:     }
 69: 
 70:     /**
 71:      * Get collection of all available processes
 72:      *
 73:      * @return Mage_Index_Model_Resource_Process_Collection
 74:      */
 75:     public function getProcessesCollection()
 76:     {
 77:         return $this->_processesCollection;
 78:     }
 79: 
 80:     /**
 81:      * Get index process by specific id
 82:      *
 83:      * @param int $processId
 84:      * @return Mage_Index_Model_Process | false
 85:      */
 86:     public function getProcessById($processId)
 87:     {
 88:         foreach ($this->_processesCollection as $process) {
 89:             if ($process->getId() == $processId) {
 90:                 return $process;
 91:             }
 92:         }
 93:         return false;
 94:     }
 95: 
 96:     /**
 97:      * Get index process by specific code
 98:      *
 99:      * @param string $code
100:      * @return Mage_Index_Model_Process | false
101:      */
102:     public function getProcessByCode($code)
103:     {
104:         foreach ($this->_processesCollection as $process) {
105:             if ($process->getIndexerCode() == $code) {
106:                 return $process;
107:             }
108:         }
109:         return false;
110:     }
111: 
112:     /**
113:      * Lock indexer actions
114:      * @deprecated after 1.6.1.0
115:      *
116:      * @return Mage_Index_Model_Indexer
117:      */
118:     public function lockIndexer()
119:     {
120:         $this->_lockFlag = true;
121:         return $this;
122:     }
123: 
124:     /**
125:      * Unlock indexer actions
126:      * @deprecated after 1.6.1.0
127:      *
128:      * @return Mage_Index_Model_Indexer
129:      */
130:     public function unlockIndexer()
131:     {
132:         $this->_lockFlag = false;
133:         return $this;
134:     }
135: 
136:     /**
137:      * Check if onject actions are locked
138:      *
139:      * @deprecated after 1.6.1.0
140:      * @return bool
141:      */
142:     public function isLocked()
143:     {
144:         return $this->_lockFlag;
145:     }
146: 
147:     /**
148:      * Indexing all pending events.
149:      * Events set can be limited by event entity and type
150:      *
151:      * @param   null | string $entity
152:      * @param   null | string $type
153:      * @return  Mage_Index_Model_Indexer
154:      */
155:     public function indexEvents($entity=null, $type=null)
156:     {
157:         Mage::dispatchEvent('start_index_events' . $this->_getEventTypeName($entity, $type));
158: 
159:         /** @var $resourceModel Mage_Index_Model_Resource_Process */
160:         $resourceModel = Mage::getResourceSingleton('index/process');
161: 
162:         $allowTableChanges = $this->_allowTableChanges && !$resourceModel->isInTransaction();
163:         if ($allowTableChanges) {
164:             $this->_currentEvent = array($entity, $type);
165:             $this->_changeKeyStatus(false);
166:         }
167: 
168:         $resourceModel->beginTransaction();
169:         $this->_allowTableChanges = false;
170:         try {
171:             $this->_runAll('indexEvents', array($entity, $type));
172:             $resourceModel->commit();
173:         } catch (Exception $e) {
174:             $resourceModel->rollBack();
175:             throw $e;
176:         }
177:         if ($allowTableChanges) {
178:             $this->_allowTableChanges = true;
179:             $this->_changeKeyStatus(true);
180:             $this->_currentEvent = null;
181:         }
182:         Mage::dispatchEvent('end_index_events' . $this->_getEventTypeName($entity, $type));
183:         return $this;
184:     }
185: 
186:     /**
187:      * Index one event by all processes
188:      *
189:      * @param   Mage_Index_Model_Event $event
190:      * @return  Mage_Index_Model_Indexer
191:      */
192:     public function indexEvent(Mage_Index_Model_Event $event)
193:     {
194:         $this->_runAll('safeProcessEvent', array($event));
195:         return $this;
196:     }
197: 
198:     /**
199:      * Register event in each indexing process process
200:      *
201:      * @param Mage_Index_Model_Event $event
202:      */
203:     public function registerEvent(Mage_Index_Model_Event $event)
204:     {
205:         $this->_runAll('register', array($event));
206:         return $this;
207:     }
208: 
209:     /**
210:      * Create new event log and register event in all processes
211:      *
212:      * @param   Varien_Object $entity
213:      * @param   string $entityType
214:      * @param   string $eventType
215:      * @param   bool $doSave
216:      * @return  Mage_Index_Model_Event
217:      */
218:     public function logEvent(Varien_Object $entity, $entityType, $eventType, $doSave=true)
219:     {
220:         $event = Mage::getModel('index/event')
221:             ->setEntity($entityType)
222:             ->setType($eventType)
223:             ->setDataObject($entity)
224:             ->setEntityPk($entity->getId());
225: 
226:         $this->registerEvent($event);
227:         if ($doSave) {
228:             $event->save();
229:         }
230:         return $event;
231:     }
232: 
233:     /**
234:      * Create new event log and register event in all processes.
235:      * Initiate events indexing procedure.
236:      *
237:      * @param   Varien_Object $entity
238:      * @param   string $entityType
239:      * @param   string $eventType
240:      * @return  Mage_Index_Model_Indexer
241:      */
242:     public function processEntityAction(Varien_Object $entity, $entityType, $eventType)
243:     {
244:         $event = $this->logEvent($entity, $entityType, $eventType, false);
245:         /**
246:          * Index and save event just in case if some process matched it
247:          */
248:         if ($event->getProcessIds()) {
249:             Mage::dispatchEvent('start_process_event' . $this->_getEventTypeName($entityType, $eventType));
250: 
251:             /** @var $resourceModel Mage_Index_Model_Resource_Process */
252:             $resourceModel = Mage::getResourceSingleton('index/process');
253: 
254:             $allowTableChanges = $this->_allowTableChanges && !$resourceModel->isInTransaction();
255:             if ($allowTableChanges) {
256:                 $this->_currentEvent = $event;
257:                 $this->_changeKeyStatus(false);
258:             }
259: 
260:             $resourceModel->beginTransaction();
261:             $this->_allowTableChanges = false;
262:             try {
263:                 $this->indexEvent($event);
264:                 $resourceModel->commit();
265:             } catch (Exception $e) {
266:                 $resourceModel->rollBack();
267:                 if ($allowTableChanges) {
268:                     $this->_allowTableChanges = true;
269:                     $this->_changeKeyStatus(true);
270:                     $this->_currentEvent = null;
271:                 }
272:                 throw $e;
273:             }
274:             if ($allowTableChanges) {
275:                 $this->_allowTableChanges = true;
276:                 $this->_changeKeyStatus(true);
277:                 $this->_currentEvent = null;
278:             }
279:             $event->save();
280:             Mage::dispatchEvent('end_process_event' . $this->_getEventTypeName($entityType, $eventType));
281:         }
282:         return $this;
283:     }
284: 
285:     /**
286:      * Run all processes method with parameters
287:      * Run by depends priority
288:      * Not recursive call is not implement
289:      *
290:      * @param string $method
291:      * @param array $args
292:      * @return Mage_Index_Model_Indexer
293:      */
294:     protected function _runAll($method, $args)
295:     {
296:         $checkLocks = $method != 'register';
297:         $processed = array();
298:         foreach ($this->_processesCollection as $process) {
299:             $code = $process->getIndexerCode();
300:             if (in_array($code, $processed)) {
301:                 continue;
302:             }
303:             $hasLocks = false;
304: 
305:             if ($process->getDepends()) {
306:                 foreach ($process->getDepends() as $processCode) {
307:                     $dependProcess = $this->getProcessByCode($processCode);
308:                     if ($dependProcess && !in_array($processCode, $processed)) {
309:                         if ($checkLocks && $dependProcess->isLocked()) {
310:                             $hasLocks = true;
311:                         } else {
312:                             call_user_func_array(array($dependProcess, $method), $args);
313:                             if ($checkLocks && $dependProcess->getMode() == Mage_Index_Model_Process::MODE_MANUAL) {
314:                                 $hasLocks = true;
315:                             } else {
316:                                 $processed[] = $processCode;
317:                             }
318:                         }
319:                     }
320:                 }
321:             }
322: 
323:             if (!$hasLocks) {
324:                 call_user_func_array(array($process, $method), $args);
325:                 $processed[] = $code;
326:             }
327:         }
328:     }
329: 
330:     /**
331:      * Enable/Disable keys in index tables
332:      *
333:      * @param bool $enable
334:      * @return Mage_Index_Model_Indexer
335:      */
336:     protected function _changeKeyStatus($enable = true)
337:     {
338:         $processed = array();
339:         foreach ($this->_processesCollection as $process) {
340:             $code = $process->getIndexerCode();
341:             if (in_array($code, $processed)) {
342:                 continue;
343:             }
344: 
345:             if ($process->getDepends()) {
346:                 foreach ($process->getDepends() as $processCode) {
347:                     $dependProcess = $this->getProcessByCode($processCode);
348:                     if ($dependProcess && !in_array($processCode, $processed)) {
349:                         if ($this->_changeProcessKeyStatus($dependProcess, $enable)) {
350:                             $processed[] = $processCode;
351:                         }
352:                     }
353:                 }
354:             }
355: 
356:             if ($this->_changeProcessKeyStatus($process, $enable)) {
357:                 $processed[] = $code;
358:             }
359:         }
360: 
361:         return $this;
362:     }
363: 
364:     /**
365:      * Check if the event will be processed and disable/enable keys in index tables
366:      *
367:      * @param mixed|Mage_Index_Model_Process $process
368:      * @param bool $enable
369:      * @return bool
370:      */
371:     protected function _changeProcessKeyStatus($process, $enable = true)
372:     {
373:         $event = $this->_currentEvent;
374:         if ($process instanceof Mage_Index_Model_Process
375:             && $process->getMode() !== Mage_Index_Model_Process::MODE_MANUAL
376:             && !$process->isLocked()
377:             && (is_null($event)
378:                 || ($event instanceof Mage_Index_Model_Event && $process->matchEvent($event))
379:                 || (is_array($event) && $process->matchEntityAndType($event[0], $event[1]))
380:         )) {
381:             if ($enable) {
382:                 $process->enableIndexerKeys();
383:             } else {
384:                 $process->disableIndexerKeys();
385:             }
386:             return true;
387:         }
388:         return false;
389:     }
390: 
391:     /**
392:      * Allow DDL operations while indexing
393:      *
394:      * @return Mage_Index_Model_Indexer
395:      */
396:     public function allowTableChanges()
397:     {
398:         $this->_allowTableChanges = true;
399:         return $this;
400:     }
401: 
402:     /**
403:      * Disallow DDL operations while indexing
404:      *
405:      * @return Mage_Index_Model_Indexer
406:      */
407:     public function disallowTableChanges()
408:     {
409:         $this->_allowTableChanges = false;
410:         return $this;
411:     }
412: 
413:     /**
414:      * Get event type name
415:      *
416:      * @param null|string $entityType
417:      * @param null|string $eventType
418:      * @return string
419:      */
420:     protected function _getEventTypeName($entityType = null, $eventType = null)
421:     {
422:         $eventName = $entityType . '_' . $eventType;
423:         $eventName = trim($eventName, '_');
424:         if (!empty($eventName)) {
425:             $eventName = '_' . $eventName;
426:         }
427:         return $eventName;
428:     }
429: }
430: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0