Inheritance: extends Mage_Core_Helper_Abstract
 /**
  * Rebuild all index data.
  */
 public function reindexAll()
 {
     if (!$this->config->getApplicationID() || !$this->config->getAPIKey() || !$this->config->getSearchOnlyAPIKey()) {
         /** @var Mage_Adminhtml_Model_Session $session */
         $session = Mage::getSingleton('adminhtml/session');
         $session->addError('Algolia reindexing failed: You need to configure your Algolia credentials in System > Configuration > Algolia Search.');
         return;
     }
     $this->queue->runCron();
     return $this;
 }
Exemplo n.º 2
0
 public function rebuildStoreProductIndexPage($storeId, $collectionDefault, $page, $pageSize, $emulationInfo = null, $productIds = null, $useTmpIndex = false)
 {
     if ($this->config->isEnabledBackend($storeId) === false) {
         $this->logger->log('INDEXING IS DISABLED FOR ' . $this->logger->getStoreName($storeId));
         return;
     }
     $this->logger->start('rebuildStoreProductIndexPage ' . $this->logger->getStoreName($storeId) . ' page ' . $page . ' pageSize ' . $pageSize);
     $emulationInfoPage = null;
     if ($emulationInfo === null) {
         $emulationInfoPage = $this->startEmulation($storeId);
     }
     $index_prefix = Mage::getConfig()->getTablePrefix();
     $additionalAttributes = $this->config->getProductAdditionalAttributes($storeId);
     /** @var Mage_Catalog_Model_Resource_Product_Collection $collection */
     $collection = clone $collectionDefault;
     $collection->setCurPage($page)->setPageSize($pageSize);
     $collection->addCategoryIds();
     $collection->addUrlRewrite();
     if ($this->product_helper->isAttributeEnabled($additionalAttributes, 'stock_qty')) {
         $collection->joinField('stock_qty', $index_prefix . 'cataloginventory_stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left');
     }
     if ($this->product_helper->isAttributeEnabled($additionalAttributes, 'ordered_qty')) {
         $collection->getSelect()->columns('(SELECT SUM(qty_ordered) FROM ' . $index_prefix . 'sales_flat_order_item WHERE ' . $index_prefix . 'sales_flat_order_item.product_id = e.entity_id) as ordered_qty');
     }
     if ($this->product_helper->isAttributeEnabled($additionalAttributes, 'total_ordered')) {
         $collection->getSelect()->columns('(SELECT SUM(row_total) FROM ' . $index_prefix . 'sales_flat_order_item WHERE ' . $index_prefix . 'sales_flat_order_item.product_id = e.entity_id) as total_ordered');
     }
     if ($this->product_helper->isAttributeEnabled($additionalAttributes, 'rating_summary')) {
         $collection->joinField('rating_summary', $index_prefix . 'review_entity_summary', 'rating_summary', 'entity_pk_value=entity_id', '{{table}}.store_id=' . $storeId, 'left');
     }
     $this->logger->start('LOADING ' . $this->logger->getStoreName($storeId) . ' collection page ' . $page . ', pageSize ' . $pageSize);
     $collection->load();
     $this->logger->log('Loaded ' . count($collection) . ' products');
     $this->logger->stop('LOADING ' . $this->logger->getStoreName($storeId) . ' collection page ' . $page . ', pageSize ' . $pageSize);
     $index_name = $this->product_helper->getIndexName($storeId, $useTmpIndex);
     $indexData = $this->getProductsRecords($storeId, $collection, $productIds);
     if (!empty($indexData['toIndex'])) {
         $this->logger->start('ADD/UPDATE TO ALGOLIA');
         $this->algolia_helper->addObjects($indexData['toIndex'], $index_name);
         $this->logger->log('Product IDs: ' . implode(', ', array_keys($indexData['toIndex'])));
         $this->logger->stop('ADD/UPDATE TO ALGOLIA');
     }
     if (!empty($indexData['toRemove'])) {
         $this->logger->start('REMOVE FROM ALGOLIA');
         $this->algolia_helper->deleteObjects($indexData['toRemove'], $index_name);
         $this->logger->log('Product IDs: ' . implode(', ', $indexData['toRemove']));
         $this->logger->stop('REMOVE FROM ALGOLIA');
     }
     unset($indexData);
     $collection->walk('clearInstance');
     $collection->clear();
     unset($collection);
     if ($emulationInfo === null) {
         $this->stopEmulation($emulationInfoPage);
     }
     $this->logger->stop('rebuildStoreProductIndexPage ' . $this->logger->getStoreName($storeId) . ' page ' . $page . ' pageSize ' . $pageSize);
 }
Exemplo n.º 3
0
 /**
  * Only used when reindexing everything. Otherwise Model/Indexer/Algolia will take care of the rest.
  */
 public function rebuildIndex($storeId = null, $productIds = null)
 {
     if ($this->config->isEnabledBackend(Mage::app()->getStore()->getId()) === false) {
         return parent::rebuildIndex($storeId, $productIds);
     }
     if (!$this->config->getApplicationID() || !$this->config->getAPIKey() || !$this->config->getSearchOnlyAPIKey()) {
         /** @var Mage_Adminhtml_Model_Session $session */
         $session = Mage::getSingleton('adminhtml/session');
         $session->addError('Algolia reindexing failed: You need to configure your Algolia credentials in System > Configuration > Algolia Search.');
         return null;
     }
     /* Avoid Indexing twice */
     if (is_array($productIds) && $productIds > 0) {
         return $this;
     }
     $this->engine->rebuildProducts($storeId);
     return $this;
 }
Exemplo n.º 4
0
 public function run($limit)
 {
     $full_reindex = $limit === -1;
     $limit = $full_reindex ? 1 : $limit;
     $element_count = 0;
     $jobs = array();
     $offset = 0;
     $max_size = $this->config->getNumberOfElementByPage() * $limit;
     while ($element_count < $max_size) {
         $data = $this->db->query($this->db->select()->from($this->table, '*')->where('pid IS NULL')->order(array('job_id'))->limit($limit, $limit * $offset));
         $data = $data->fetchAll();
         $offset++;
         if (count($data) <= 0) {
             break;
         }
         foreach ($data as $job) {
             $job_size = (int) $job['data_size'];
             if ($element_count + $job_size <= $max_size) {
                 $jobs[] = $job;
                 $element_count += $job_size;
             } else {
                 break 2;
             }
         }
     }
     if (count($jobs) <= 0) {
         return;
     }
     $first_id = $jobs[0]['job_id'];
     $last_id = $jobs[count($jobs) - 1]['job_id'];
     $pid = getmypid();
     // Reserve all new jobs since last run
     $this->db->query("UPDATE {$this->db->quoteIdentifier($this->table, true)} SET pid = " . $pid . ' WHERE job_id >= ' . $first_id . " AND job_id <= {$last_id}");
     foreach ($jobs as &$job) {
         $job['data'] = json_decode($job['data'], true);
     }
     $jobs = $this->sortAndMergeJob($jobs);
     // Run all reserved jobs
     foreach ($jobs as $job) {
         try {
             $model = Mage::getSingleton($job['class']);
             $method = $job['method'];
             $model->{$method}(new Varien_Object($job['data']));
         } catch (Exception $e) {
             // Increment retries and log error information
             $this->logger->log("Queue processing {$job['pid']} [KO]: Mage::getSingleton({$job['class']})->{$job['method']}(" . json_encode($job['data']) . ')');
             $this->logger->log(date('c') . ' ERROR: ' . get_class($e) . ": '{$e->getMessage()}' in {$e->getFile()}:{$e->getLine()}\n" . "Stack trace:\n" . $e->getTraceAsString());
         }
     }
     // Delete only when finished to be able to debug the queue if needed
     $where = $this->db->quoteInto('pid = ?', $pid);
     $this->db->delete($this->table, $where);
     if ($full_reindex) {
         $this->run(-1);
     }
 }
 public function addObjects($objects, $index_name)
 {
     $this->handleTooBigRecords($objects, $index_name);
     $index = $this->getIndex($index_name);
     if ($this->config->isPartialUpdateEnabled()) {
         $index->partialUpdateObjects($objects);
     } else {
         $index->addObjects($objects);
     }
 }
Exemplo n.º 6
0
 /**
  * Rebuild all index data.
  */
 public function reindexAll()
 {
     if (!$this->config->getApplicationID() || !$this->config->getAPIKey() || !$this->config->getSearchOnlyAPIKey()) {
         /** @var Mage_Adminhtml_Model_Session $session */
         $session = Mage::getSingleton('adminhtml/session');
         $session->addError('Algolia reindexing failed: You need to configure your Algolia credentials in System > Configuration > Algolia Search.');
         $this->logger->log('ERROR Credentials not configured correctly');
         return;
     }
     $this->logger->start('PRODUCTS FULL REINDEX');
     $this->engine->rebuildProducts();
     $this->logger->stop('PRODUCTS FULL REINDEX');
     return $this;
 }
Exemplo n.º 7
0
 public function prepareEntityIndex($index, $separator = ' ')
 {
     if ($this->config->isEnabledBackend(Mage::app()->getStore()->getId()) === false) {
         return parent::prepareEntityIndex($index, $separator);
     }
     foreach ($index as $key => $value) {
         if (is_array($value) && !empty($value)) {
             $index[$key] = implode($separator, array_unique(array_filter($value)));
         } else {
             if (empty($index[$key])) {
                 unset($index[$key]);
             }
         }
     }
     return $index;
 }
Exemplo n.º 8
0
 /**
  * Call algoliasearch.xml To load js / css / phtml.
  */
 public function useAlgoliaSearchPopup(Varien_Event_Observer $observer)
 {
     if ($this->config->isEnabledFrontEnd()) {
         if ($this->config->getApplicationID() && $this->config->getAPIKey()) {
             if ($this->config->isPopupEnabled() || $this->config->isInstantEnabled()) {
                 $observer->getLayout()->getUpdate()->addHandle('algolia_search_handle');
                 if ($this->config->isDefaultSelector()) {
                     $observer->getLayout()->getUpdate()->addHandle('algolia_search_handle_with_topsearch');
                 } else {
                     $observer->getLayout()->getUpdate()->addHandle('algolia_search_handle_no_topsearch');
                 }
             }
         }
     }
     return $this;
 }
Exemplo n.º 9
0
 public function getBaseIndexName($storeId = null)
 {
     return (string) $this->config->getIndexPrefix($storeId) . Mage::app()->getStore($storeId)->getCode();
 }
Exemplo n.º 10
0
 public function log($message, $forceLog = false)
 {
     if ($this->config->isLoggingEnabled() || $forceLog) {
         Mage::log($message, null, 'algolia.log');
     }
 }