forked from mgtf/magento-1.9.2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexer.php
216 lines (204 loc) · 8.23 KB
/
indexer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Shell
* @copyright Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
require_once 'abstract.php';
/**
* Magento Compiler Shell Script
*
* @category Mage
* @package Mage_Shell
* @author Magento Core Team <[email protected]>
*/
class Mage_Shell_Compiler extends Mage_Shell_Abstract
{
/**
* Get Indexer instance
*
* @return Mage_Index_Model_Indexer
*/
protected function _getIndexer()
{
return $this->_factory->getSingleton($this->_factory->getIndexClassAlias());
}
/**
* Parse string with indexers and return array of indexer instances
*
* @param string $string
* @return array
*/
protected function _parseIndexerString($string)
{
$processes = array();
if ($string == 'all') {
$collection = $this->_getIndexer()->getProcessesCollection();
foreach ($collection as $process) {
if ($process->getIndexer()->isVisible() === false) {
continue;
}
$processes[] = $process;
}
} else if (!empty($string)) {
$codes = explode(',', $string);
$codes = array_map('trim', $codes);
$processes = $this->_getIndexer()->getProcessesCollectionByCodes($codes);
foreach($processes as $key => $process) {
if ($process->getIndexer()->getVisibility() === false) {
unset($processes[$key]);
}
}
if ($this->_getIndexer()->hasErrors()) {
echo implode(PHP_EOL, $this->_getIndexer()->getErrors()), PHP_EOL;
}
}
return $processes;
}
/**
* Run script
*
*/
public function run()
{
$_SESSION = array();
if ($this->getArg('info')) {
$processes = $this->_parseIndexerString('all');
foreach ($processes as $process) {
/* @var $process Mage_Index_Model_Process */
echo sprintf('%-30s', $process->getIndexerCode());
echo $process->getIndexer()->getName() . "\n";
}
} else if ($this->getArg('status') || $this->getArg('mode')) {
if ($this->getArg('status')) {
$processes = $this->_parseIndexerString($this->getArg('status'));
} else {
$processes = $this->_parseIndexerString($this->getArg('mode'));
}
foreach ($processes as $process) {
/* @var $process Mage_Index_Model_Process */
$status = 'unknown';
if ($this->getArg('status')) {
switch ($process->getStatus()) {
case Mage_Index_Model_Process::STATUS_PENDING:
$status = 'Pending';
break;
case Mage_Index_Model_Process::STATUS_REQUIRE_REINDEX:
$status = 'Require Reindex';
break;
case Mage_Index_Model_Process::STATUS_RUNNING:
$status = 'Running';
break;
default:
$status = 'Ready';
break;
}
} else {
switch ($process->getMode()) {
case Mage_Index_Model_Process::MODE_SCHEDULE:
$status = 'Update by schedule';
break;
case Mage_Index_Model_Process::MODE_REAL_TIME:
$status = 'Update on Save';
break;
case Mage_Index_Model_Process::MODE_MANUAL:
$status = 'Manual Update';
break;
}
}
echo sprintf('%-35s ', $process->getIndexer()->getName() . ':') . $status ."\n";
}
} else if ($this->getArg('mode-realtime') || $this->getArg('mode-manual')) {
if ($this->getArg('mode-realtime')) {
$mode = Mage_Index_Model_Process::MODE_REAL_TIME;
$processes = $this->_parseIndexerString($this->getArg('mode-realtime'));
} else {
$mode = Mage_Index_Model_Process::MODE_MANUAL;
$processes = $this->_parseIndexerString($this->getArg('mode-manual'));
}
foreach ($processes as $process) {
/* @var $process Mage_Index_Model_Process */
try {
$process->setMode($mode)->save();
echo $process->getIndexer()->getName() . " index was successfully changed index mode\n";
} catch (Mage_Core_Exception $e) {
echo $e->getMessage() . "\n";
} catch (Exception $e) {
echo $process->getIndexer()->getName() . " index process unknown error:\n";
echo $e . "\n";
}
}
} else if ($this->getArg('reindex') || $this->getArg('reindexall')) {
if ($this->getArg('reindex')) {
$processes = $this->_parseIndexerString($this->getArg('reindex'));
} else {
$processes = $this->_parseIndexerString('all');
}
try {
Mage::dispatchEvent('shell_reindex_init_process');
foreach ($processes as $process) {
/* @var $process Mage_Index_Model_Process */
try {
$startTime = microtime(true);
$process->reindexEverything();
$resultTime = microtime(true) - $startTime;
Mage::dispatchEvent($process->getIndexerCode() . '_shell_reindex_after');
echo $process->getIndexer()->getName()
. " index was rebuilt successfully in " . gmdate('H:i:s', $resultTime) . "\n";
} catch (Mage_Core_Exception $e) {
echo $e->getMessage() . "\n";
} catch (Exception $e) {
echo $process->getIndexer()->getName() . " index process unknown error:\n";
echo $e . "\n";
}
}
Mage::dispatchEvent('shell_reindex_finalize_process');
} catch (Exception $e) {
Mage::dispatchEvent('shell_reindex_finalize_process');
echo $e->getMessage() . "\n";
}
} else {
echo $this->usageHelp();
}
}
/**
* Retrieve Usage Help Message
*
*/
public function usageHelp()
{
return <<<USAGE
Usage: php -f indexer.php -- [options]
--status <indexer> Show Indexer(s) Status
--mode <indexer> Show Indexer(s) Index Mode
--mode-realtime <indexer> Set index mode type "Update on Save"
--mode-manual <indexer> Set index mode type "Manual Update"
--reindex <indexer> Reindex Data
info Show allowed indexers
reindexall Reindex Data by all indexers
help This help
<indexer> Comma separated indexer codes or value "all" for all indexers
USAGE;
}
}
$shell = new Mage_Shell_Compiler();
$shell->run();