|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the eZ Publish Kernel package. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace eZ\Bundle\EzPublishCoreBundle\Command; |
|
10
|
|
|
|
|
11
|
|
|
use eZ\Publish\Core\Search\Common\IterativelyIndexer; |
|
12
|
|
|
use eZ\Publish\SPI\Persistence\Content\ContentInfo; |
|
13
|
|
|
use eZ\Publish\Core\Search\Common\Indexer; |
|
14
|
|
|
use Doctrine\DBAL\Driver\Statement; |
|
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
19
|
|
|
use Symfony\Component\Process\Process; |
|
20
|
|
|
use Symfony\Component\Process\PhpExecutableFinder; |
|
21
|
|
|
use RuntimeException; |
|
22
|
|
|
use DateTime; |
|
23
|
|
|
use PDO; |
|
24
|
|
|
|
|
25
|
|
|
class ReindexCommand extends ContainerAwareCommand |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var \eZ\Publish\Core\Search\Common\Indexer |
|
29
|
|
|
*/ |
|
30
|
|
|
private $searchIndexer; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Initialize objects required by {@see execute()}. |
|
34
|
|
|
* |
|
35
|
|
|
* @param InputInterface $input |
|
36
|
|
|
* @param OutputInterface $output |
|
37
|
|
|
*/ |
|
38
|
|
|
public function initialize(InputInterface $input, OutputInterface $output) |
|
39
|
|
|
{ |
|
40
|
|
|
parent::initialize($input, $output); |
|
41
|
|
|
$this->searchIndexer = $this->getContainer()->get('ezpublish.spi.search.indexer'); |
|
42
|
|
|
if (!$this->searchIndexer instanceof Indexer) { |
|
43
|
|
|
throw new RuntimeException( |
|
44
|
|
|
sprintf('Expected to find Search Engine Indexer but found "%s" instead', get_parent_class($this->searchIndexer)) |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function configure() |
|
53
|
|
|
{ |
|
54
|
|
|
$this |
|
55
|
|
|
->setName('ezplatform:reindex') |
|
56
|
|
|
->setDescription('Recreate or Refresh search engine index') |
|
57
|
|
|
->addOption('iteration-count', 'c', InputOption::VALUE_OPTIONAL, 'Number of objects to be indexed in a single iteration, for avoiding using to much memory', 20) |
|
58
|
|
|
->addOption('no-commit', null, InputOption::VALUE_NONE, 'Do not commit after each iteration') |
|
59
|
|
|
->addOption('no-purge', null, InputOption::VALUE_NONE, 'Do not purge before indexing. BC NOTE: Should this be default as of 2.0?') |
|
60
|
|
|
->addOption('since', null, InputOption::VALUE_NONE, 'Index changes since a given time, any format understood by DateTime. Implies "no-purge", can not be combined with "content-ids".') |
|
61
|
|
|
->addOption('content-ids', null, InputOption::VALUE_NONE, 'Comma separated list of content id\'s to refresh (deleted or updated/added). Implies "no-purge", can not be combined with "since".') |
|
62
|
|
|
->addOption('processes', null, InputOption::VALUE_NONE, 'Number of sub processes to spawn in parallel, by default: (number of cpu cores)-1, disable by setting to "0"', $this->getNumberOfCPUCores() -1) |
|
63
|
|
|
->setHelp( |
|
64
|
|
|
<<<EOT |
|
65
|
|
|
The command <info>%command.name%</info> indexes current configured database in configured search engine index. |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
TODO: ADD EXAMPLES OF ADVANCE USAGE! |
|
69
|
|
|
|
|
70
|
|
|
EOT |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
78
|
|
|
{ |
|
79
|
|
|
$noCommit = $input->getOption('no-commit'); |
|
80
|
|
|
$iterationCount = $input->getOption('iteration-count'); |
|
81
|
|
|
if (!is_numeric($iterationCount) || (int) $iterationCount < 1) { |
|
82
|
|
|
throw new RuntimeException("'--iteration-count' option should be > 0, got '{$iterationCount}'"); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
if (!$this->searchIndexer instanceof IterativelyIndexer) { |
|
87
|
|
|
$output->writeln( <<<EOT |
|
88
|
|
|
DEPRECATED: |
|
89
|
|
|
Running indexing against an Indexer that has not been updated to use IterativelyIndexer abstract. |
|
90
|
|
|
|
|
91
|
|
|
Options that won't be taken into account: |
|
92
|
|
|
- since |
|
93
|
|
|
- content-ids |
|
94
|
|
|
- processes |
|
95
|
|
|
- no-purge |
|
96
|
|
|
EOT |
|
97
|
|
|
); |
|
98
|
|
|
$this->searchIndexer->createSearchIndex($output, (int) $iterationCount, empty($noCommit)); |
|
99
|
|
|
|
|
100
|
|
|
return 0; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// If content-ids are specified we assume we are in a sub process or that it's not needed |
|
104
|
|
|
if ($contentIds = $input->getOption('content-ids')) { |
|
105
|
|
|
$this->searchIndexer->updateSearchIndex($contentIds); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $this->executeParallel($input, $output, (int) $iterationCount, (bool) $noCommit); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
private function executeParallel(InputInterface $input, OutputInterface $output, $iterationCount, $noCommit) |
|
|
|
|
|
|
113
|
|
|
{ |
|
114
|
|
|
$since = $input->getOption('since'); |
|
115
|
|
|
$processes = $input->getOption('processes'); |
|
|
|
|
|
|
116
|
|
|
$noPurge = $input->getOption('no-purge'); |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
if ($since) { |
|
119
|
|
|
$stmt = $this->getStatementContentSince(new DateTime($since)); |
|
|
|
|
|
|
120
|
|
|
} else { |
|
121
|
|
|
$stmt = $this->getStatementContentAll(); |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param DateTime $since |
|
129
|
|
|
* |
|
130
|
|
|
* @return \Doctrine\DBAL\Driver\Statement |
|
131
|
|
|
*/ |
|
132
|
|
|
private function getStatementContentSince(DateTime $since) |
|
133
|
|
|
{ |
|
134
|
|
|
/** |
|
135
|
|
|
* @var \Doctrine\DBAL\Connection $connection |
|
136
|
|
|
*/ |
|
137
|
|
|
$connection = $this->getContainer()->get('ezpublish.api.storage_engine.legacy.connection'); |
|
138
|
|
|
$q = $connection->createQueryBuilder() |
|
139
|
|
|
->select('c.id') |
|
140
|
|
|
->from('ezcontentobject', 'c') |
|
141
|
|
|
->where('c.status = :status')->andWhere('c.modified >= :since') |
|
142
|
|
|
->orderBy('c.modified', true) |
|
143
|
|
|
->setParameter('status', ContentInfo::STATUS_PUBLISHED, PDO::PARAM_INT) |
|
144
|
|
|
->setParameter('since', $since->getTimestamp(), PDO::PARAM_INT); |
|
145
|
|
|
|
|
146
|
|
|
return $q->execute(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return \Doctrine\DBAL\Driver\Statement |
|
151
|
|
|
*/ |
|
152
|
|
|
private function getStatementContentAll() |
|
153
|
|
|
{ |
|
154
|
|
|
/** |
|
155
|
|
|
* @var \Doctrine\DBAL\Connection $connection |
|
156
|
|
|
*/ |
|
157
|
|
|
$connection = $this->getContainer()->get('ezpublish.api.storage_engine.legacy.connection'); |
|
158
|
|
|
$q = $connection->createQueryBuilder() |
|
159
|
|
|
->select('c.id') |
|
160
|
|
|
->from('ezcontentobject', 'c') |
|
161
|
|
|
->where('c.status = :status') |
|
162
|
|
|
->setParameter('status', ContentInfo::STATUS_PUBLISHED, PDO::PARAM_INT); |
|
163
|
|
|
|
|
164
|
|
|
return $q->execute(); |
|
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param \Doctrine\DBAL\Driver\Statement $stmt |
|
169
|
|
|
* @param int $iterationCount |
|
170
|
|
|
* |
|
171
|
|
|
* @return int[][] Return an array of arrays, each array contains content id's of $iterationCount. |
|
172
|
|
|
*/ |
|
173
|
|
|
private function fetchIteration(Statement $stmt, $iterationCount) |
|
174
|
|
|
{ |
|
175
|
|
|
do { |
|
176
|
|
|
$contentIds = []; |
|
177
|
|
View Code Duplication |
for ($i = 0; $i < $iterationCount; ++$i) { |
|
|
|
|
|
|
178
|
|
|
if ($contentId = $stmt->fetch(PDO::FETCH_COLUMN)) { |
|
179
|
|
|
$contentIds[] = $contentId; |
|
180
|
|
|
} else { |
|
181
|
|
|
break; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
yield $contentIds; |
|
186
|
|
|
} while ($contentId); |
|
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param string $consoleDir |
|
191
|
|
|
* |
|
192
|
|
|
* @return Process |
|
193
|
|
|
*/ |
|
194
|
|
|
private static function getPhpProcess($consoleDir = 'app') |
|
|
|
|
|
|
195
|
|
|
{ |
|
196
|
|
|
$phpFinder = new PhpExecutableFinder(); |
|
197
|
|
|
if (!$phpPath = $phpFinder->find()) { |
|
198
|
|
|
throw new \RuntimeException('The php executable could not be found, add it to your PATH environment variable and try again'); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
$cmd = 'ezplatform:reindex'; |
|
202
|
|
|
$php = escapeshellarg($phpFinder); |
|
203
|
|
|
$console = escapeshellarg($consoleDir.'/console'); |
|
204
|
|
|
|
|
205
|
|
|
return new Process($php.' '.$console.' '.$cmd, null, null, null, null); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @return int |
|
210
|
|
|
*/ |
|
211
|
|
|
private function getNumberOfCPUCores() |
|
212
|
|
|
{ |
|
213
|
|
|
$cores = 1; |
|
214
|
|
|
if (is_file('/proc/cpuinfo')) { |
|
215
|
|
|
// Linux (and potentially Windows with linux sub systems) |
|
216
|
|
|
$cpuinfo = file_get_contents('/proc/cpuinfo'); |
|
217
|
|
|
preg_match_all('/^processor/m', $cpuinfo, $matches); |
|
218
|
|
|
$cores = count($matches[0]); |
|
219
|
|
|
} else if (DIRECTORY_SEPARATOR === '\\') { |
|
220
|
|
|
// Windows |
|
221
|
|
|
if (($process = @popen('wmic cpu get NumberOfCores', 'rb')) !== false) { |
|
222
|
|
|
fgets($process); |
|
223
|
|
|
$cores = (int) fgets($process); |
|
224
|
|
|
pclose($process); |
|
225
|
|
|
} |
|
226
|
|
|
} else { |
|
227
|
|
|
// *nix (Linux, BSD and Mac) |
|
228
|
|
|
if (($process = @popen('sysctl -a', 'rb')) !== false) { |
|
229
|
|
|
$output = stream_get_contents($process); |
|
230
|
|
|
if (preg_match('/hw.ncpu: (\d+)/', $output, $matches)) { |
|
231
|
|
|
$cores = (int) $matches[1][0]; |
|
232
|
|
|
} |
|
233
|
|
|
pclose($process); |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
return $cores; |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.