-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathRegenerateUrlAliasesCommand.php
More file actions
339 lines (301 loc) · 11.6 KB
/
Copy pathRegenerateUrlAliasesCommand.php
File metadata and controls
339 lines (301 loc) · 11.6 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace Ibexa\Bundle\Core\Command;
use Exception;
use Ibexa\Contracts\Core\Repository\Repository;
use Ibexa\Contracts\Core\Repository\Values\Content\Language;
use Ibexa\Contracts\Core\Repository\Values\Content\Location;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
/**
* The ezplatform:urls:regenerate-aliases Symfony command implementation.
* Recreates system URL aliases for all existing Locations and cleanups corrupted URL alias nodes.
*/
class RegenerateUrlAliasesCommand extends Command implements BackwardCompatibleCommand
{
public const DEFAULT_ITERATION_COUNT = 1000;
public const BEFORE_RUNNING_HINTS = <<<EOT
<error>Before you continue:</error>
- Make sure to back up your database.
- If you are regenerating URL aliases for all Locations, take the installation offline. The database should not be modified while the script is being executed.
- Run this command without memory limit, because processing large numbers of Locations (e.g. 300k) can take up to 1 GB of RAM.
- Run this command in production environment using <info>--env=prod</info>
- Manually clear HTTP cache after running this command.
EOT;
/** @var \Ibexa\Contracts\Core\Repository\Repository */
private $repository;
/** @var \Psr\Log\LoggerInterface */
private $logger;
/**
* @param \Ibexa\Contracts\Core\Repository\Repository $repository
* @param \Psr\Log\LoggerInterface $logger
*/
public function __construct(Repository $repository, ?LoggerInterface $logger = null)
{
parent::__construct();
$this->repository = $repository;
$this->logger = null !== $logger ? $logger : new NullLogger();
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$beforeRunningHints = self::BEFORE_RUNNING_HINTS;
$this
->setName('ibexa:urls:regenerate-aliases')
->setAliases($this->getDeprecatedAliases())
->setDescription(
'Regenerates Location URL aliases (autogenerated) and cleans up custom Location ' .
'and global URL aliases stored in the Legacy Storage Engine'
)
->addOption(
'iteration-count',
'c',
InputOption::VALUE_OPTIONAL,
'Number of Locations fetched into memory and processed at once',
self::DEFAULT_ITERATION_COUNT
)->addOption(
'location-id',
null,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'Only Locations with provided ID\'s will have URL aliases regenerated',
[]
)->addOption(
'force',
'f',
InputOption::VALUE_NONE,
'Prevents confirmation dialog when used with --no-interaction. Please use it carefully.'
)->setHelp(
<<<EOT
{$beforeRunningHints}
The command <info>%command.name%</info> regenerates URL aliases for Locations and cleans up
corrupted URL aliases (pointing to non-existent Locations).
Existing aliases are archived (will redirect to the new ones).
Note: This script can potentially run for a very long time.
Due to performance issues the command does not send any Events.
<comment>You need to clear HTTP cache manually after executing this command.</comment>
EOT
);
}
/**
* Regenerate URL aliases.
*
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$iterationCount = (int)$input->getOption('iteration-count');
$locationIds = $input->getOption('location-id');
if (!empty($locationIds)) {
$locationIds = $this->getFilteredLocationList($locationIds);
$locationsCount = count($locationIds);
} else {
$locationsCount = $this->repository->sudo(
static function (Repository $repository) {
return $repository->getLocationService()->getAllLocationsCount();
}
);
}
if ($locationsCount === 0) {
$output->writeln('<info>No location was found. Exiting.</info>');
return 0;
}
if (!$input->getOption('no-interaction')) {
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion(
sprintf(
"<info>Found %d Locations.</info>\n%s\n<info>Do you want to proceed? [y/N] </info>",
$locationsCount,
self::BEFORE_RUNNING_HINTS
),
false
);
if (!$helper->ask($input, $output, $question)) {
return 0;
}
} elseif (!$input->getOption('force')) {
return 1;
}
$this->regenerateSystemUrlAliases($output, $locationsCount, $locationIds, $iterationCount);
$output->writeln('<info>Cleaning up corrupted URL aliases...</info>');
$corruptedAliasesCount = $this->repository->sudo(
static function (Repository $repository) {
return $repository->getURLAliasService()->deleteCorruptedUrlAliases();
}
);
$output->writeln("<info>Done. Deleted {$corruptedAliasesCount} entries.</info>");
$output->writeln('<comment>Make sure to clear HTTP cache.</comment>');
return 0;
}
/**
* Return configured progress bar helper.
*
* @param int $maxSteps
* @param \Symfony\Component\Console\Output\OutputInterface $output
*
* @return \Symfony\Component\Console\Helper\ProgressBar
*/
protected function getProgressBar($maxSteps, OutputInterface $output)
{
$progressBar = new ProgressBar($output, $maxSteps);
$progressBar->setFormat(
' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%'
);
return $progressBar;
}
/**
* Process single results page of fetched Locations.
*
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Location[] $locations
* @param \Symfony\Component\Console\Helper\ProgressBar $progressBar
*/
private function processLocations(array $locations, ProgressBar $progressBar)
{
$contentList = $this->repository->sudo(
static function (Repository $repository) use ($locations) {
$contentInfoList = array_map(
static function (Location $location) {
return $location->contentInfo;
},
$locations
);
// load Content list in all languages
return $repository->getContentService()->loadContentListByContentInfo(
$contentInfoList,
Language::ALL,
false
);
}
);
foreach ($locations as $location) {
try {
// ignore missing Content items
if (!isset($contentList[$location->contentId])) {
continue;
}
$this->repository->sudo(
static function (Repository $repository) use ($location) {
$repository->getURLAliasService()->refreshSystemUrlAliasesForLocation(
$location
);
}
);
} catch (Exception $e) {
$contentInfo = $location->getContentInfo();
$msg = sprintf(
'Failed processing location %d - [%d] %s (%s: %s)',
$location->id,
$contentInfo->id,
$contentInfo->name,
get_class($e),
$e->getMessage()
);
$this->logger->warning($msg);
// in debug mode log full exception with a trace
$this->logger->debug($e);
} finally {
$progressBar->advance(1);
}
}
}
/**
* @param int $offset
* @param int $iterationCount
*
* @return \Ibexa\Contracts\Core\Repository\Values\Content\Location[]
*
* @throws \Exception
*/
private function loadAllLocations(int $offset, int $iterationCount): array
{
return $this->repository->sudo(
static function (Repository $repository) use ($offset, $iterationCount) {
return $repository->getLocationService()->loadAllLocations($offset, $iterationCount);
}
);
}
/**
* @param int[] $locationIds
* @param int $offset
* @param int $iterationCount
*
* @return \Ibexa\Contracts\Core\Repository\Values\Content\Location[]
*
* @throws \Exception
*/
private function loadSpecificLocations(array $locationIds, int $offset, int $iterationCount): array
{
$locationIds = array_slice($locationIds, $offset, $iterationCount);
return $this->repository->sudo(
static function (Repository $repository) use ($locationIds) {
return $repository->getLocationService()->loadLocationList($locationIds);
}
);
}
/**
* @param int[] $locationIds
*
* @return int[]
*
* @throws \Exception
*/
private function getFilteredLocationList(array $locationIds): array
{
$locations = $this->repository->sudo(
static function (Repository $repository) use ($locationIds) {
$locationService = $repository->getLocationService();
return $locationService->loadLocationList($locationIds);
}
);
return array_map(
static function (Location $location) {
return $location->id;
},
$locations
);
}
/**
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @param int $locationsCount
* @param int[] $locationIds
* @param int $iterationCount
*/
private function regenerateSystemUrlAliases(
OutputInterface $output,
int $locationsCount,
array $locationIds,
int $iterationCount
): void {
$output->writeln('Regenerating System URL aliases...');
$progressBar = $this->getProgressBar($locationsCount, $output);
$progressBar->start();
for ($offset = 0; $offset <= $locationsCount; $offset += $iterationCount) {
gc_disable();
if (!empty($locationIds)) {
$locations = $this->loadSpecificLocations($locationIds, $offset, $iterationCount);
} else {
$locations = $this->loadAllLocations($offset, $iterationCount);
}
$this->processLocations($locations, $progressBar);
gc_enable();
}
$progressBar->finish();
$output->writeln('');
$output->writeln('<info>Done.</info>');
}
public function getDeprecatedAliases(): array
{
return ['ezplatform:urls:regenerate-aliases'];
}
}
class_alias(RegenerateUrlAliasesCommand::class, 'eZ\Bundle\EzPublishCoreBundle\Command\RegenerateUrlAliasesCommand');