|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Indexer; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use Mage_Index_Model_Process; |
|
7
|
|
|
use Symfony\Component\Console\Helper\DialogHelper; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
11
|
|
|
|
|
12
|
|
|
class ReindexCommand extends AbstractIndexerCommand |
|
13
|
|
|
{ |
|
14
|
|
|
protected function configure() |
|
15
|
|
|
{ |
|
16
|
|
|
$this |
|
17
|
|
|
->setName('index:reindex') |
|
18
|
|
|
->addArgument('index_code', InputArgument::OPTIONAL, 'Code of indexer.') |
|
19
|
|
|
->setDescription('Reindex a magento index by code'); |
|
20
|
|
|
|
|
21
|
|
|
$help = <<<HELP |
|
22
|
|
|
Index by indexer code. Code is optional. If you don't specify a code you can pick a indexer from a list. |
|
23
|
|
|
|
|
24
|
|
|
$ n98-magerun.phar index:reindex [code] |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
Since 1.75.0 it's possible to run mutiple indexers by seperating code with a comma. |
|
28
|
|
|
|
|
29
|
|
|
i.e. |
|
30
|
|
|
|
|
31
|
|
|
$ n98-magerun.phar index:reindex catalog_product_attribute,tag_summary |
|
32
|
|
|
|
|
33
|
|
|
If no index is provided as argument you can select indexers from menu by "number" like "1,3" for first and third |
|
34
|
|
|
indexer. |
|
35
|
|
|
HELP; |
|
36
|
|
|
$this->setHelp($help); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param InputInterface $input |
|
41
|
|
|
* @param OutputInterface $output |
|
42
|
|
|
* |
|
43
|
|
|
* @return int|void |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->detectMagento($output, true); |
|
48
|
|
|
if (!$this->initMagento()) { |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$this->writeSection($output, 'Reindex'); |
|
53
|
|
|
$this->disableObservers(); |
|
54
|
|
|
$indexCode = $input->getArgument('index_code'); |
|
55
|
|
|
if ($indexCode === null) { |
|
56
|
|
|
$indexCodes = $this->askForIndexCodes($output); |
|
57
|
|
|
} else { |
|
58
|
|
|
// take cli argument |
|
59
|
|
|
$indexCodes = \N98\Util\BinaryString::trimExplodeEmpty(',', $indexCode); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$processes = $this->getProcessesByIndexCodes($indexCodes); |
|
63
|
|
|
$this->executeProcesses($output, $processes); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param $indexCodes |
|
68
|
|
|
* |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
|
|
private function getProcessesByIndexCodes($indexCodes) |
|
72
|
|
|
{ |
|
73
|
|
|
$processes = array(); |
|
74
|
|
|
foreach ($indexCodes as $indexCode) { |
|
75
|
|
|
/* @var $process Mage_Index_Model_Process */ |
|
76
|
|
|
$process = $this->getIndexerModel()->getProcessByCode($indexCode); |
|
77
|
|
|
if (!$process) { |
|
78
|
|
|
throw new InvalidArgumentException(sprintf('Indexer "%s" was not found!', $indexCode)); |
|
79
|
|
|
} |
|
80
|
|
|
$processes[] = $process; |
|
81
|
|
|
} |
|
82
|
|
|
return $processes; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param OutputInterface $output |
|
87
|
|
|
* |
|
88
|
|
|
* @return array |
|
|
|
|
|
|
89
|
|
|
*/ |
|
90
|
|
|
private function askForIndexCodes(OutputInterface $output) |
|
91
|
|
|
{ |
|
92
|
|
|
$indexerList = $this->getIndexerList(); |
|
93
|
|
|
$question = array(); |
|
94
|
|
|
foreach ($indexerList as $key => $indexer) { |
|
95
|
|
|
$question[] = sprintf( |
|
96
|
|
|
"<comment>%-4s</comment> %-40s <info>(last runtime: %s)</info>\n", |
|
97
|
|
|
'[' . ($key + 1) . ']', |
|
98
|
|
|
$indexer['code'], |
|
99
|
|
|
$indexer['last_runtime'] |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
$question[] = '<question>Please select a indexer:</question>'; |
|
103
|
|
|
|
|
104
|
|
|
$validator = function ($typeInput) use ($indexerList) { |
|
105
|
|
|
if (strstr($typeInput, ',')) { |
|
106
|
|
|
$typeInputs = \N98\Util\BinaryString::trimExplodeEmpty(',', $typeInput); |
|
107
|
|
|
} else { |
|
108
|
|
|
$typeInputs = array($typeInput); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$returnCodes = array(); |
|
112
|
|
|
foreach ($typeInputs as $typeInput) { |
|
113
|
|
|
if (!isset($indexerList[$typeInput - 1])) { |
|
114
|
|
|
throw new InvalidArgumentException('Invalid indexer'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$returnCodes[] = $indexerList[$typeInput - 1]['code']; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $returnCodes; |
|
121
|
|
|
}; |
|
122
|
|
|
|
|
123
|
|
|
/** @var DialogHelper $dialog */ |
|
124
|
|
|
$dialog = $this->getHelper('dialog'); |
|
125
|
|
|
$indexCodes = $dialog->askAndValidate($output, $question, $validator); |
|
126
|
|
|
|
|
127
|
|
|
return $indexCodes; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.