Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once 'abstract.php';
- class ISAAC_Category_Load_Tester extends Mage_Shell_Abstract
- {
- public function run()
- {
- $loadInBulk = false;
- $useFlatTable = false;
- $restrictedAttributes = false;
- $addUrlRewrite = false;
- if (!$this->getArg('category')) {
- echo 'category is required ' . PHP_EOL;
- $this->usageHelp();
- return;
- }
- $categoryId = $this->getArg('category');
- $category = Mage::getModel('catalog/category')->load($categoryId);
- if (!$category->getId()) {
- echo sprintf('error: could not find category with id %s', $categoryId) . PHP_EOL;
- return;
- }
- if ($this->getArg('load-in-bulk')) {
- $loadInBulk = true;
- }
- if ($this->getArg('use-flat-table')) {
- $useFlatTable = true;
- }
- if ($this->getArg('restricted-attributes')) {
- $restrictedAttributes = true;
- }
- if ($this->getArg('add-url-rewrite')) {
- $addUrlRewrite = true;
- }
- Mage::app()->setCurrentStore(1);
- $time = microtime(true);
- $categoryIds = $category->getResource()->getChildren($category);
- if ($loadInBulk) {
- $this->printCategories(
- $this->getCategoriesByIds($categoryIds, $useFlatTable, $restrictedAttributes, $addUrlRewrite)
- );
- } else {
- foreach ($categoryIds as $categoryId) {
- $this->printCategories(
- $this->getCategoriesByIds($categoryId, $useFlatTable, $restrictedAttributes, $addUrlRewrite)
- );
- }
- }
- echo sprintf(
- 'Loading categories %s with %s attributes took %s seconds',
- $loadInBulk ? 'in bulk' : 'one by one',
- $restrictedAttributes ? 'restricted' : 'all',
- microtime(true) - $time
- ) . PHP_EOL;
- return;
- }
- protected function getCategoriesByIds($categoryIds, $useFlatTable, $restrictedAttributes, $addUrlRewrite)
- {
- if ($useFlatTable) {
- $categoryCollection = Mage::getModel('catalog/category')->getCollection();
- } else {
- $categoryCollection = Mage::getResourceModel('catalog/category_collection');
- }
- if ($restrictedAttributes) {
- $categoryCollection->addAttributeToSelect('url_key');
- $categoryCollection->addAttributeToSelect('include_in_menu');
- } else {
- $categoryCollection->addAttributeToSelect('*');
- }
- $categoryCollection->addIdFilter($categoryIds);
- if ($addUrlRewrite) {
- $categoryCollection->addUrlRewriteToResult();
- }
- return $categoryCollection->getItems();
- }
- protected function printCategories($categories)
- {
- foreach ($categories as $category) {
- echo sprintf(
- 'category %d (%s, %s): %s',
- $category->getId(),
- $category->getUrlKey(),
- ($category->getIncludeInMenu() ? 'included in menu' : 'not included in menu'),
- $category->getUrl()
- ) . PHP_EOL;
- }
- }
- public function usageHelp()
- {
- global $argv;
- echo $argv[0] . ' --category CATEGORY_ID [--load-in-bulk] [--use-flat-table] [--restricted-attributes] [--add-url-rewrite]' . PHP_EOL;
- echo PHP_EOL;
- echo 'Tests category load for the children of CATEGORY_ID analyzing the performance of the Emico_Tweakwise_Helper_Data::getFilterCategory method.' . PHP_EOL;
- echo PHP_EOL;
- }
- }
- $shell = new ISAAC_Category_Load_Tester();
- $shell->run();
Add Comment
Please, Sign In to add comment