Guest User

Untitled

a guest
May 20th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. <?php
  2. require_once 'abstract.php';
  3.  
  4. class ISAAC_Category_Load_Tester extends Mage_Shell_Abstract
  5. {
  6. public function run()
  7. {
  8. $loadInBulk = false;
  9. $useFlatTable = false;
  10. $restrictedAttributes = false;
  11. $addUrlRewrite = false;
  12. if (!$this->getArg('category')) {
  13. echo 'category is required ' . PHP_EOL;
  14. $this->usageHelp();
  15. return;
  16. }
  17. $categoryId = $this->getArg('category');
  18. $category = Mage::getModel('catalog/category')->load($categoryId);
  19. if (!$category->getId()) {
  20. echo sprintf('error: could not find category with id %s', $categoryId) . PHP_EOL;
  21. return;
  22. }
  23. if ($this->getArg('load-in-bulk')) {
  24. $loadInBulk = true;
  25. }
  26. if ($this->getArg('use-flat-table')) {
  27. $useFlatTable = true;
  28. }
  29. if ($this->getArg('restricted-attributes')) {
  30. $restrictedAttributes = true;
  31. }
  32. if ($this->getArg('add-url-rewrite')) {
  33. $addUrlRewrite = true;
  34. }
  35. Mage::app()->setCurrentStore(1);
  36. $time = microtime(true);
  37. $categoryIds = $category->getResource()->getChildren($category);
  38. if ($loadInBulk) {
  39. $this->printCategories(
  40. $this->getCategoriesByIds($categoryIds, $useFlatTable, $restrictedAttributes, $addUrlRewrite)
  41. );
  42. } else {
  43. foreach ($categoryIds as $categoryId) {
  44. $this->printCategories(
  45. $this->getCategoriesByIds($categoryId, $useFlatTable, $restrictedAttributes, $addUrlRewrite)
  46. );
  47. }
  48. }
  49. echo sprintf(
  50. 'Loading categories %s with %s attributes took %s seconds',
  51. $loadInBulk ? 'in bulk' : 'one by one',
  52. $restrictedAttributes ? 'restricted' : 'all',
  53. microtime(true) - $time
  54. ) . PHP_EOL;
  55. return;
  56. }
  57.  
  58. protected function getCategoriesByIds($categoryIds, $useFlatTable, $restrictedAttributes, $addUrlRewrite)
  59. {
  60. if ($useFlatTable) {
  61. $categoryCollection = Mage::getModel('catalog/category')->getCollection();
  62. } else {
  63. $categoryCollection = Mage::getResourceModel('catalog/category_collection');
  64. }
  65. if ($restrictedAttributes) {
  66. $categoryCollection->addAttributeToSelect('url_key');
  67. $categoryCollection->addAttributeToSelect('include_in_menu');
  68. } else {
  69. $categoryCollection->addAttributeToSelect('*');
  70. }
  71. $categoryCollection->addIdFilter($categoryIds);
  72. if ($addUrlRewrite) {
  73. $categoryCollection->addUrlRewriteToResult();
  74. }
  75. return $categoryCollection->getItems();
  76. }
  77.  
  78. protected function printCategories($categories)
  79. {
  80. foreach ($categories as $category) {
  81. echo sprintf(
  82. 'category %d (%s, %s): %s',
  83. $category->getId(),
  84. $category->getUrlKey(),
  85. ($category->getIncludeInMenu() ? 'included in menu' : 'not included in menu'),
  86. $category->getUrl()
  87. ) . PHP_EOL;
  88. }
  89. }
  90.  
  91. public function usageHelp()
  92. {
  93. global $argv;
  94. echo $argv[0] . ' --category CATEGORY_ID [--load-in-bulk] [--use-flat-table] [--restricted-attributes] [--add-url-rewrite]' . PHP_EOL;
  95. echo PHP_EOL;
  96. echo 'Tests category load for the children of CATEGORY_ID analyzing the performance of the Emico_Tweakwise_Helper_Data::getFilterCategory method.' . PHP_EOL;
  97. echo PHP_EOL;
  98. }
  99. }
  100.  
  101. $shell = new ISAAC_Category_Load_Tester();
  102. $shell->run();
Add Comment
Please, Sign In to add comment