|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Algolia\AlgoliaSearch\Service\Product; |
| 4 | + |
| 5 | +use Algolia\AlgoliaSearch\Helper\Logger; |
| 6 | +use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection; |
| 7 | +use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; |
| 8 | +use Magento\Framework\App\ResourceConnection; |
| 9 | +use Magento\Framework\DB\Select; |
| 10 | +use Magento\Framework\Indexer\IndexerInterface; |
| 11 | +use Magento\Framework\Indexer\IndexerRegistry; |
| 12 | +use Magento\Framework\Indexer\StateInterface; |
| 13 | +use Zend_Db_Select; |
| 14 | + |
| 15 | +class MissingPriceIndexHandler |
| 16 | +{ |
| 17 | + public const PRICE_INDEX_TABLE = 'catalog_product_index_price'; |
| 18 | + public const PRICE_INDEX_TABLE_ALIAS = 'price_index'; |
| 19 | + public const MAIN_TABLE_ALIAS = 'e'; |
| 20 | + |
| 21 | + protected array $_indexedProducts = []; |
| 22 | + |
| 23 | + protected IndexerInterface $indexer; |
| 24 | + public function __construct( |
| 25 | + protected CollectionFactory $productCollectionFactory, |
| 26 | + protected ResourceConnection $resourceConnection, |
| 27 | + protected Logger $logger, |
| 28 | + IndexerRegistry $indexerRegistry |
| 29 | + ) |
| 30 | + { |
| 31 | + $this->indexer = $indexerRegistry->get('catalog_product_price'); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @param string[]|ProductCollection $products |
| 36 | + * @return string[] Array of product IDs that were reindexed by this repair operation |
| 37 | + */ |
| 38 | + public function refreshPriceIndex(array|ProductCollection $products): array |
| 39 | + { |
| 40 | + $reindexIds = $this->getProductIdsToReindex($products); |
| 41 | + if (empty($reindexIds)) { |
| 42 | + return []; |
| 43 | + } |
| 44 | + |
| 45 | + $this->logger->log(__("Pricing records missing or invalid for %1 product(s)", count($reindexIds))); |
| 46 | + $this->logger->log(__("Reindexing product ID(s): %1", implode(', ', $reindexIds))); |
| 47 | + |
| 48 | + $this->indexer->reindexList($reindexIds); |
| 49 | + |
| 50 | + return $reindexIds; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Analyzes a product collection and determines which (if any) records should have their prices reindexed |
| 55 | + * @param string[]|ProductCollection $products - either an explicit list of product ids or a product collection |
| 56 | + * @return string[] IDs of products that require price reindexing (will be empty if no indexing is required) |
| 57 | + */ |
| 58 | + protected function getProductIdsToReindex(array|ProductCollection $products): array |
| 59 | + { |
| 60 | + $productIds = $products instanceof ProductCollection |
| 61 | + ? $this->getProductIdsFromCollection($products) |
| 62 | + : $products; |
| 63 | + |
| 64 | + if (empty($productIds)) { |
| 65 | + return []; |
| 66 | + } |
| 67 | + |
| 68 | + $state = $this->indexer->getState()->getStatus(); |
| 69 | + if ($state === StateInterface::STATUS_INVALID) { |
| 70 | + return $this->filterProductIdsNotYetProcessed($productIds); |
| 71 | + } |
| 72 | + |
| 73 | + $productIds = $this->filterProductIdsMissingPricing($productIds); |
| 74 | + if (empty($productIds)) { |
| 75 | + return []; |
| 76 | + } |
| 77 | + |
| 78 | + return $this->filterProductIdsNotYetProcessed($productIds); |
| 79 | + } |
| 80 | + |
| 81 | + protected function filterProductIdsMissingPricing(array $productIds): array |
| 82 | + { |
| 83 | + $collection = $this->productCollectionFactory->create(); |
| 84 | + |
| 85 | + $collection->addAttributeToSelect(['name', 'price']); |
| 86 | + |
| 87 | + $collection->getSelect()->joinLeft( |
| 88 | + [self::PRICE_INDEX_TABLE_ALIAS => self::PRICE_INDEX_TABLE], |
| 89 | + self::MAIN_TABLE_ALIAS . '.entity_id = ' . self::PRICE_INDEX_TABLE_ALIAS . '.entity_id', |
| 90 | + [] |
| 91 | + ); |
| 92 | + |
| 93 | + $collection->getSelect() |
| 94 | + ->where(self::PRICE_INDEX_TABLE_ALIAS . '.entity_id IS NULL') |
| 95 | + ->where(self::MAIN_TABLE_ALIAS . '.entity_id IN (?)', $productIds); |
| 96 | + |
| 97 | + return $collection->getAllIds(); |
| 98 | + } |
| 99 | + |
| 100 | + protected function filterProductIdsNotYetProcessed(array $productIds): array { |
| 101 | + $pendingProcessing = array_fill_keys($productIds, true); |
| 102 | + |
| 103 | + $notProcessed = array_diff_key($pendingProcessing, $this->_indexedProducts); |
| 104 | + |
| 105 | + if (empty($notProcessed)) { |
| 106 | + return []; |
| 107 | + } |
| 108 | + |
| 109 | + $this->_indexedProducts += $notProcessed; |
| 110 | + |
| 111 | + return array_keys($notProcessed); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Expand the query for product ids from the collection regardless of price index status |
| 116 | + * @return string[] An array of indices to be evaluated - array will be empty if no price index join found |
| 117 | + */ |
| 118 | + protected function getProductIdsFromCollection(ProductCollection $collection): array |
| 119 | + { |
| 120 | + |
| 121 | + $select = clone $collection->getSelect(); |
| 122 | + try { |
| 123 | + $joins = $select->getPart(Zend_Db_Select::FROM); |
| 124 | + } catch (\Zend_Db_Select_Exception $e) { |
| 125 | + $this->logger->error("Unable to build query for missing product prices: " . $e->getMessage()); |
| 126 | + return []; |
| 127 | + } |
| 128 | + |
| 129 | + $priceIndexJoin = $this->getPriceIndexJoinAlias($joins); |
| 130 | + |
| 131 | + if (!$priceIndexJoin) { |
| 132 | + // no price index on query - keep calm and carry on |
| 133 | + return []; |
| 134 | + } |
| 135 | + |
| 136 | + $this->expandPricingJoin($joins, $priceIndexJoin); |
| 137 | + $this->rebuildJoins($select, $joins); |
| 138 | + |
| 139 | + return $this->resourceConnection->getConnection()->fetchCol($select); |
| 140 | + } |
| 141 | + |
| 142 | + protected function expandPricingJoin(array &$joins, string $priceIndexJoin): void |
| 143 | + { |
| 144 | + $modifyJoin = &$joins[$priceIndexJoin]; |
| 145 | + $modifyJoin['joinType'] = Zend_Db_Select::LEFT_JOIN; |
| 146 | + } |
| 147 | + |
| 148 | + protected function rebuildJoins(Select $select, array $joins): void |
| 149 | + { |
| 150 | + $select->reset(Zend_Db_Select::COLUMNS); |
| 151 | + $select->reset(Zend_Db_Select::FROM); |
| 152 | + foreach ($joins as $alias => $joinData) { |
| 153 | + if ($joinData['joinType'] === Zend_Db_Select::FROM) { |
| 154 | + $select->from( |
| 155 | + [$alias => $joinData['tableName']], |
| 156 | + 'entity_id' |
| 157 | + ); |
| 158 | + } elseif ($joinData['joinType'] === Zend_Db_Select::LEFT_JOIN) { |
| 159 | + $select->joinLeft( |
| 160 | + [$alias => $joinData['tableName']], |
| 161 | + $joinData['joinCondition'], |
| 162 | + [], |
| 163 | + $joinData['schema'] |
| 164 | + ); |
| 165 | + } else { |
| 166 | + $select->join( |
| 167 | + [$alias => $joinData['tableName']], |
| 168 | + $joinData['joinCondition'], |
| 169 | + [], |
| 170 | + $joinData['schema'] |
| 171 | + ); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * @param array<string, array> $joins |
| 178 | + * @return string |
| 179 | + */ |
| 180 | + protected function getPriceIndexJoinAlias(array $joins): string |
| 181 | + { |
| 182 | + if (isset($joins[self::PRICE_INDEX_TABLE_ALIAS])) { |
| 183 | + return self::PRICE_INDEX_TABLE_ALIAS; |
| 184 | + } |
| 185 | + else { |
| 186 | + foreach ($joins as $alias => $joinData) { |
| 187 | + if ($joinData['tableName'] === self::PRICE_INDEX_TABLE) { |
| 188 | + return $alias; |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + return ""; |
| 194 | + } |
| 195 | +} |
0 commit comments