Skip to content

Instantly share code, notes, and snippets.

@mklooss
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mklooss/9785275 to your computer and use it in GitHub Desktop.
Save mklooss/9785275 to your computer and use it in GitHub Desktop.
Magento Reindex All, some parts from AvS_Fastsimpleimport :)
<?php
if(php_sapi_name() != 'cli')
{
echo "only for cli!";
exit;
}
require 'app/Mage.php';
Mage::app('admin');
$resource = Mage::getSingleton('core/resource');
/* @var $resource Mage_Core_Model_Resource */
$conn = $resource->getConnection('core_write');
/* @var $conn Varien_Db_Adapter_Interface */
echo "Start Reindex\n";
$entityIds = Mage::getModel('catalog/product')->getCollection()->getAllIds();
echo count($entityIds)."\n";
$event = Mage::getModel('index/event');
$event->setNewData(array(
'reindex_price_product_ids' => &$entityIds, // for product_indexer_price
'reindex_stock_product_ids' => &$entityIds, // for indexer_stock
'product_ids' => &$entityIds, // for category_indexer_product
'reindex_eav_product_ids' => &$entityIds // for product_indexer_eav
));
echo "stock index\n";
$conn->update($resource->getTableName('index_process'), array('status' => 'working', 'started_at' => date('Y-m-d H:i:s')), 'indexer_code=\'cataloginventory_stock\'');
Mage::getResourceSingleton('cataloginventory/indexer_stock')->catalogProductMassAction($event);
$conn->update($resource->getTableName('index_process'), array('status' => 'pending', 'ended_at' => date('Y-m-d H:i:s')), 'indexer_code=\'cataloginventory_stock\'');
echo "price index\n";
$conn->update($resource->getTableName('index_process'), array('status' => 'working', 'started_at' => date('Y-m-d H:i:s')), 'indexer_code=\'catalog_product_price\'');
Mage::getResourceSingleton('catalog/product_indexer_price')->catalogProductMassAction($event);
$conn->update($resource->getTableName('index_process'), array('status' => 'pending', 'ended_at' => date('Y-m-d H:i:s')), 'indexer_code=\'catalog_product_price\'');
echo "category index\n";
$conn->update($resource->getTableName('index_process'), array('status' => 'working', 'started_at' => date('Y-m-d H:i:s')), 'indexer_code=\'catalog_category_product\'');
Mage::getResourceSingleton('catalog/category_indexer_product')->catalogProductMassAction($event);
$conn->update($resource->getTableName('index_process'), array('status' => 'pending', 'ended_at' => date('Y-m-d H:i:s')), 'indexer_code=\'catalog_category_product\'');
echo "eav index\n";
$conn->update($resource->getTableName('index_process'), array('status' => 'working', 'started_at' => date('Y-m-d H:i:s')), 'indexer_code=\'catalog_product_attribute\'');
Mage::getResourceSingleton('catalog/product_indexer_eav')->catalogProductMassAction($event);
$conn->update($resource->getTableName('index_process'), array('status' => 'pending', 'ended_at' => date('Y-m-d H:i:s')), 'indexer_code=\'catalog_product_attribute\'');
echo "fulltext index\n";
$conn->update($resource->getTableName('index_process'), array('status' => 'working', 'started_at' => date('Y-m-d H:i:s')), 'indexer_code=\'catalogsearch_fulltext\'');
Mage::getResourceSingleton('catalogsearch/fulltext')->rebuildIndex(null, $entityIds);
$conn->update($resource->getTableName('index_process'), array('status' => 'pending', 'ended_at' => date('Y-m-d H:i:s')), 'indexer_code=\'catalogsearch_fulltext\'');
echo "flat index\n";
$conn->update($resource->getTableName('index_process'), array('status' => 'working', 'started_at' => date('Y-m-d H:i:s')), 'indexer_code=\'catalog_product_flat\'');
Mage::getSingleton('catalog/product_flat_indexer')->saveProduct($entityIds);
$conn->update($resource->getTableName('index_process'), array('status' => 'pending', 'ended_at' => date('Y-m-d H:i:s')), 'indexer_code=\'catalog_product_flat\'');
echo "category index\n";
$conn->update($resource->getTableName('index_process'), array('status' => 'working', 'started_at' => date('Y-m-d H:i:s')), 'indexer_code=\'catalog_category_flat\'');
Mage::getSingleton('index/indexer')->getProcessByCode("catalog_category_flat")->reindexAll();
$conn->update($resource->getTableName('index_process'), array('status' => 'pending', 'ended_at' => date('Y-m-d H:i:s')), 'indexer_code=\'catalog_category_flat\'');
$conn->update($resource->getTableName('index_process'), array('status' => 'working', 'started_at' => date('Y-m-d H:i:s')), 'indexer_code=\'catalog_url\'');
echo "url index\n";
if (Mage::getResourceModel('ecomdev_urlrewrite/indexer')) {
Mage::getResourceSingleton('ecomdev_urlrewrite/indexer')->updateProductRewrites($entityIds);
} else {
$urlModel = Mage::getSingleton('catalog/url');
$urlModel->refreshCategoryRewrite(Mage::app()->getWebsite(true)->getDefaultStore()->getRootCategoryId(), null, false);
$urlModel->clearStoreInvalidRewrites(); // Maybe some products were moved or removed from website
foreach ($entityIds as $productId) {
$urlModel->refreshProductRewrite($productId);
}
}
$conn->update($resource->getTableName('index_process'), array('status' => 'pending', 'ended_at' => date('Y-m-d H:i:s')), 'indexer_code=\'catalog_url\'');
echo "Reindex Done\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment