|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
6
|
|
|
use eZ\Publish\API\Repository\Repository; |
|
7
|
|
|
use Kaliop\eZMigrationBundle\API\Collection\MigrationDefinitionCollection; |
|
8
|
|
|
use Kaliop\eZMigrationBundle\API\LanguageAwareInterface; |
|
9
|
|
|
use Kaliop\eZMigrationBundle\API\StorageHandlerInterface; |
|
10
|
|
|
use Kaliop\eZMigrationBundle\API\LoaderInterface; |
|
11
|
|
|
use Kaliop\eZMigrationBundle\API\DefinitionParserInterface; |
|
12
|
|
|
use Kaliop\eZMigrationBundle\API\ExecutorInterface; |
|
13
|
|
|
use Kaliop\eZMigrationBundle\API\Value\Migration; |
|
14
|
|
|
use Kaliop\eZMigrationBundle\API\Value\MigrationDefinition; |
|
15
|
|
|
use Kaliop\eZMigrationBundle\API\Exception\MigrationStepExecutionException; |
|
16
|
|
|
use Kaliop\eZMigrationBundle\API\Event\BeforeStepExecutionEvent; |
|
17
|
|
|
use Kaliop\eZMigrationBundle\API\Event\StepExecutedEvent; |
|
18
|
|
|
|
|
19
|
|
|
class MigrationService |
|
20
|
|
|
{ |
|
21
|
|
|
use RepositoryUserSetterTrait; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Constant defining the default Admin user ID. |
|
25
|
|
|
* @todo inject via config parameter |
|
26
|
|
|
*/ |
|
27
|
|
|
const ADMIN_USER_ID = 14; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var LoaderInterface $loader |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $loader; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var StorageHandlerInterface $storageHandler |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $storageHandler; |
|
37
|
|
|
|
|
38
|
|
|
/** @var DefinitionParserInterface[] $DefinitionParsers */ |
|
39
|
|
|
protected $DefinitionParsers = array(); |
|
40
|
20 |
|
|
|
41
|
5 |
|
/** @var ExecutorInterface[] $executors */ |
|
42
|
20 |
|
protected $executors = array(); |
|
43
|
20 |
|
|
|
44
|
20 |
|
protected $repository; |
|
45
|
20 |
|
|
|
46
|
20 |
|
protected $dispatcher; |
|
47
|
|
|
|
|
48
|
20 |
|
public function __construct(LoaderInterface $loader, StorageHandlerInterface $storageHandler, Repository $repository, EventDispatcherInterface $eventDispatcher) |
|
49
|
|
|
{ |
|
50
|
20 |
|
$this->loader = $loader; |
|
51
|
20 |
|
$this->storageHandler = $storageHandler; |
|
52
|
|
|
$this->repository = $repository; |
|
53
|
20 |
|
$this->dispatcher = $eventDispatcher; |
|
54
|
|
|
} |
|
55
|
20 |
|
|
|
56
|
20 |
|
public function addDefinitionParser(DefinitionParserInterface $DefinitionParser) |
|
57
|
20 |
|
{ |
|
58
|
20 |
|
$this->DefinitionParsers[] = $DefinitionParser; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function addExecutor(ExecutorInterface $executor) |
|
62
|
|
|
{ |
|
63
|
|
|
foreach($executor->supportedTypes() as $type) { |
|
64
|
|
|
$this->executors[$type] = $executor; |
|
65
|
|
|
} |
|
66
|
20 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
20 |
|
* NB: returns UNPARSED definitions |
|
70
|
20 |
|
* |
|
71
|
20 |
|
* @param string[] $paths |
|
72
|
20 |
|
* @return MigrationDefinitionCollection key: migration name, value: migration definition as binary string |
|
73
|
20 |
|
*/ |
|
74
|
20 |
|
public function getMigrationsDefinitions(array $paths = array()) |
|
75
|
20 |
|
{ |
|
76
|
20 |
|
// we try to be flexible in file types we support, and the same time avoid loading all files in a directory |
|
77
|
|
|
$handledDefinitions = array(); |
|
78
|
|
|
foreach($this->loader->listAvailableDefinitions($paths) as $migrationName => $definitionPath) { |
|
79
|
20 |
|
foreach($this->DefinitionParsers as $definitionParser) { |
|
80
|
|
|
if ($definitionParser->supports($migrationName)) { |
|
81
|
|
|
$handledDefinitions[] = $definitionPath; |
|
82
|
|
|
} |
|
83
|
20 |
|
} |
|
84
|
2 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
// we can not call loadDefinitions with an empty array using the Filesystem loader, or it will start looking in bundles... |
|
87
|
|
|
if (empty($handledDefinitions) && !empty($paths)) { |
|
88
|
|
|
return new MigrationDefinitionCollection(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
20 |
|
return $this->loader->loadDefinitions($handledDefinitions); |
|
92
|
|
|
} |
|
93
|
20 |
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Returns the list of all the migrations which where executed or attempted so far |
|
96
|
|
|
* |
|
97
|
|
|
* @return \Kaliop\eZMigrationBundle\API\Collection\MigrationCollection |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getMigrations() |
|
100
|
19 |
|
{ |
|
101
|
|
|
return $this->storageHandler->loadMigrations(); |
|
102
|
19 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param string $migrationName |
|
106
|
|
|
* @return Migration|null |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getMigration($migrationName) |
|
109
|
19 |
|
{ |
|
110
|
|
|
return $this->storageHandler->loadMigration($migrationName); |
|
111
|
19 |
|
} |
|
112
|
19 |
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param MigrationDefinition $migrationDefinition |
|
115
|
|
|
* @return Migration |
|
116
|
|
|
*/ |
|
117
|
19 |
|
public function addMigration(MigrationDefinition $migrationDefinition) |
|
118
|
|
|
{ |
|
119
|
19 |
|
return $this->storageHandler->addMigration($migrationDefinition); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param Migration $migration |
|
124
|
|
|
*/ |
|
125
|
|
|
public function deleteMigration(Migration $migration) |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->storageHandler->deleteMigration($migration); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
20 |
|
/** |
|
131
|
|
|
* @param MigrationDefinition $migrationDefinition |
|
132
|
20 |
|
* @return Migration |
|
133
|
20 |
|
*/ |
|
134
|
|
|
public function skipMigration(MigrationDefinition $migrationDefinition) |
|
135
|
20 |
|
{ |
|
136
|
|
|
return $this->storageHandler->skipMigration($migrationDefinition); |
|
137
|
|
|
} |
|
138
|
20 |
|
|
|
139
|
13 |
|
/** |
|
140
|
1 |
|
* Not be called by external users for normal use cases, you should use executeMigration() instead |
|
141
|
1 |
|
* |
|
142
|
1 |
|
* @param Migration $migration |
|
143
|
1 |
|
*/ |
|
144
|
1 |
|
public function endMigration(Migration $migration) |
|
145
|
1 |
|
{ |
|
146
|
1 |
|
return $this->storageHandler->endMigration($migration); |
|
147
|
1 |
|
} |
|
148
|
|
|
|
|
149
|
20 |
|
/** |
|
150
|
|
|
* Parses a migration definition, return a parsed definition. |
|
151
|
20 |
|
* If there is a parsing error, the definition status will be updated accordingly |
|
152
|
12 |
|
* |
|
153
|
15 |
|
* @param MigrationDefinition $migrationDefinition |
|
154
|
|
|
* @return MigrationDefinition |
|
155
|
|
|
* @throws \Exception if the migrationDefinition has no suitable parser for its source format |
|
156
|
|
|
*/ |
|
157
|
|
|
public function parseMigrationDefinition(MigrationDefinition $migrationDefinition) |
|
158
|
|
|
{ |
|
159
|
|
|
foreach($this->DefinitionParsers as $definitionParser) { |
|
160
|
|
|
if ($definitionParser->supports($migrationDefinition->name)) { |
|
161
|
|
|
// parse the source file |
|
162
|
|
|
$migrationDefinition = $definitionParser->parseMigrationDefinition($migrationDefinition); |
|
163
|
|
|
|
|
164
|
|
|
// and make sure we know how to handle all steps |
|
165
|
|
|
foreach($migrationDefinition->steps as $step) { |
|
166
|
12 |
|
if (!isset($this->executors[$step->type])) { |
|
167
|
|
|
return new MigrationDefinition( |
|
168
|
12 |
|
$migrationDefinition->name, |
|
169
|
|
|
$migrationDefinition->path, |
|
170
|
|
|
$migrationDefinition->rawDefinition, |
|
171
|
|
|
MigrationDefinition::STATUS_INVALID, |
|
172
|
12 |
|
array(), |
|
173
|
12 |
|
"Can not handle migration step of type '{$step->type}'" |
|
174
|
|
|
); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
12 |
|
|
|
178
|
|
|
return $migrationDefinition; |
|
179
|
|
|
} |
|
180
|
12 |
|
} |
|
181
|
|
|
|
|
182
|
|
|
throw new \Exception("No parser available to parse migration definition '$migrationDefinition'"); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
12 |
|
* @param MigrationDefinition $migrationDefinition |
|
187
|
|
|
* @param bool $useTransaction when set to false, no repo transaction will be used to wrap the migration |
|
188
|
12 |
|
* @param string $defaultLanguageCode |
|
189
|
|
|
* @throws \Exception |
|
190
|
|
|
* |
|
191
|
|
|
* @todo add support for skipped migrations, partially executed migrations |
|
192
|
|
|
*/ |
|
193
|
12 |
|
public function executeMigration(MigrationDefinition $migrationDefinition, $useTransaction = true, $defaultLanguageCode = null) |
|
194
|
|
|
{ |
|
195
|
12 |
|
if ($migrationDefinition->status == MigrationDefinition::STATUS_TO_PARSE) { |
|
196
|
|
|
$migrationDefinition = $this->parseMigrationDefinition($migrationDefinition); |
|
197
|
12 |
|
} |
|
198
|
|
|
|
|
199
|
12 |
|
if ($migrationDefinition->status == MigrationDefinition::STATUS_INVALID) { |
|
200
|
|
|
throw new \Exception("Can not execute migration '{$migrationDefinition->name}': {$migrationDefinition->parsingError}"); |
|
201
|
12 |
|
} |
|
202
|
|
|
|
|
203
|
10 |
|
// Inject default language code in executors that support it. |
|
204
|
|
|
if ($defaultLanguageCode) { |
|
|
|
|
|
|
205
|
10 |
|
foreach ($this->executors as $executor) { |
|
206
|
10 |
|
if ($executor instanceof LanguageAwareInterface) { |
|
207
|
|
|
$executor->setDefaultLanguageCode($defaultLanguageCode); |
|
208
|
9 |
|
} |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
12 |
|
|
|
212
|
9 |
|
// set migration as begun - has to be in own db transaction |
|
213
|
9 |
|
$migration = $this->storageHandler->startMigration($migrationDefinition); |
|
214
|
9 |
|
|
|
215
|
9 |
|
if ($useTransaction) { |
|
216
|
|
|
$this->repository->beginTransaction(); |
|
217
|
9 |
|
} |
|
218
|
|
|
|
|
219
|
9 |
|
$previousUserId = null; |
|
220
|
|
|
|
|
221
|
|
|
try { |
|
222
|
|
|
|
|
223
|
|
|
$i = 1; |
|
224
|
|
|
|
|
225
|
|
|
foreach($migrationDefinition->steps as $step) { |
|
226
|
|
|
// we validated the fact that we have a good executor at parsing time |
|
227
|
|
|
$executor = $this->executors[$step->type]; |
|
228
|
|
|
|
|
229
|
12 |
|
$beforeStepExecutionEvent = new BeforeStepExecutionEvent($step, $executor); |
|
230
|
|
|
$this->dispatcher->dispatch('ez_migration.before_execution', $beforeStepExecutionEvent); |
|
231
|
12 |
|
// allow some sneaky trickery here: event listeners can manipulate 'live' the step definition and the executor |
|
232
|
|
|
$executor = $beforeStepExecutionEvent->getExecutor(); |
|
233
|
|
|
$step = $beforeStepExecutionEvent->getStep(); |
|
234
|
|
|
|
|
235
|
|
|
$result = $executor->execute($step); |
|
236
|
|
|
|
|
237
|
|
|
$this->dispatcher->dispatch('ez_migration.step_executed', new StepExecutedEvent($step, $result)); |
|
238
|
|
|
|
|
239
|
|
|
$i++; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
3 |
|
// set migration as done |
|
243
|
3 |
|
$this->storageHandler->endMigration(new Migration( |
|
244
|
3 |
|
$migration->name, |
|
245
|
3 |
|
$migration->md5, |
|
246
|
3 |
|
$migration->path, |
|
247
|
3 |
|
$migration->executionDate, |
|
248
|
3 |
|
Migration::STATUS_DONE |
|
249
|
3 |
|
)); |
|
250
|
|
|
|
|
251
|
12 |
|
if ($useTransaction) { |
|
252
|
12 |
|
// there might be workflows or other actions happening at commit time that fail if we are not admin |
|
253
|
12 |
|
$previousUserId = $this->loginUser(self::ADMIN_USER_ID); |
|
254
|
|
|
$this->repository->commit(); |
|
255
|
|
|
$this->loginUser($previousUserId); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
} catch(\Exception $e) { |
|
259
|
|
|
|
|
260
|
|
|
$errorMessage = $this->getFullExceptionMessage($e) . ' in file ' . $e->getFile() . ' line ' . $e->getLine(); |
|
261
|
|
|
$finalStatus = Migration::STATUS_FAILED; |
|
262
|
19 |
|
|
|
263
|
|
|
if ($useTransaction) { |
|
264
|
3 |
|
try { |
|
265
|
3 |
|
// cater to the case where the $this->repository->commit() call above throws an exception |
|
266
|
|
|
if ($previousUserId) { |
|
|
|
|
|
|
267
|
3 |
|
$this->loginUser($previousUserId); |
|
268
|
3 |
|
} |
|
269
|
19 |
|
|
|
270
|
|
|
// there is no need to become admin here, at least in theory |
|
271
|
|
|
$this->repository->rollBack(); |
|
272
|
|
|
|
|
273
|
|
|
} catch(\Exception $e2) { |
|
274
|
|
|
// This check is not rock-solid, but at the moment is all we can do to tell apart 2 cases of |
|
275
|
|
|
// exceptions originating above: the case where the commit was successful but a commit-queue event |
|
276
|
|
|
// failed, from the case where something failed beforehand |
|
277
|
|
|
if ($previousUserId && $e2->getMessage() == 'There is no active transaction.') { |
|
|
|
|
|
|
278
|
|
|
// since the migration succeeded and it was committed, no use to mark it as failed... |
|
279
|
|
|
$finalStatus = Migration::STATUS_DONE; |
|
280
|
|
|
$errorMessage = 'Error post migration execution: ' . $this->getFullExceptionMessage($e2) . |
|
281
|
|
|
' in file ' . $e2->getFile() . ' line ' . $e2->getLine(); |
|
282
|
|
|
} else { |
|
283
|
|
|
$errorMessage .= '. In addition, an exception was thrown while rolling back: ' . |
|
284
|
|
|
$this->getFullExceptionMessage($e2) . ' in file ' . $e2->getFile() . ' line ' . $e2->getLine(); |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
// set migration as failed |
|
290
|
|
|
// NB: we use the 'force' flag here because we might be catching an exception happened during the call to |
|
291
|
|
|
// $this->repository->commit() above, in which case the Migration might already be in the DB with a status 'done' |
|
292
|
|
|
$this->storageHandler->endMigration( |
|
293
|
|
|
new Migration( |
|
294
|
|
|
$migration->name, |
|
295
|
|
|
$migration->md5, |
|
296
|
3 |
|
$migration->path, |
|
297
|
|
|
$migration->executionDate, |
|
298
|
|
|
$finalStatus, |
|
299
|
|
|
$errorMessage |
|
300
|
|
|
), |
|
301
|
|
|
true |
|
302
|
|
|
); |
|
303
|
|
|
|
|
304
|
|
|
throw new MigrationStepExecutionException($errorMessage, $i, $e); |
|
305
|
|
|
} |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Turns eZPublish cryptic exceptions into something more palatable for random devs |
|
310
|
|
|
* @todo should this be moved to a lower layer ? |
|
311
|
|
|
* |
|
312
|
|
|
* @param \Exception $e |
|
313
|
|
|
* @return string |
|
314
|
|
|
*/ |
|
315
|
|
|
protected function getFullExceptionMessage(\Exception $e) |
|
316
|
|
|
{ |
|
317
|
|
|
$message = $e->getMessage(); |
|
318
|
|
|
if (is_a($e, '\eZ\Publish\API\Repository\Exceptions\ContentTypeFieldDefinitionValidationException') || |
|
319
|
|
|
is_a($e, '\eZ\Publish\API\Repository\Exceptions\LimitationValidationException') || |
|
320
|
|
|
is_a($e, '\eZ\Publish\Core\Base\Exceptions\ContentFieldValidationException') |
|
321
|
|
|
) { |
|
322
|
|
|
if (is_a($e, '\eZ\Publish\API\Repository\Exceptions\LimitationValidationException')) { |
|
323
|
|
|
$errorsArray = $e->getLimitationErrors(); |
|
|
|
|
|
|
324
|
|
|
if ($errorsArray == null) { |
|
325
|
|
|
return $message; |
|
326
|
|
|
} |
|
327
|
|
|
} else if (is_a($e, '\eZ\Publish\Core\Base\Exceptions\ContentFieldValidationException')) { |
|
328
|
|
|
foreach ($e->getFieldErrors() as $limitationError) { |
|
|
|
|
|
|
329
|
|
|
// we get the 1st language |
|
330
|
|
|
$errorsArray[] = reset($limitationError); |
|
|
|
|
|
|
331
|
|
|
} |
|
332
|
|
|
} else { |
|
333
|
|
|
$errorsArray = $e->getFieldErrors(); |
|
|
|
|
|
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
foreach ($errorsArray as $errors) { |
|
|
|
|
|
|
337
|
|
|
// sometimes error arrays are 2-level deep, sometimes 1... |
|
338
|
|
|
if (!is_array($errors)) { |
|
339
|
|
|
$errors = array($errors); |
|
340
|
|
|
} |
|
341
|
|
|
foreach ($errors as $error) { |
|
342
|
|
|
/// @todo find out what is the proper eZ way of getting a translated message for these errors |
|
343
|
|
|
$translatableMessage = $error->getTranslatableMessage(); |
|
344
|
|
|
if (is_a($translatableMessage, '\eZ\Publish\API\Repository\Values\Translation\Plural')) { |
|
345
|
|
|
$msgText = $translatableMessage->plural; |
|
346
|
|
|
} else { |
|
347
|
|
|
$msgText = $translatableMessage->message; |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
$message .= "\n" . $msgText . " - " . var_export($translatableMessage->values, true); |
|
351
|
|
|
} |
|
352
|
|
|
} |
|
353
|
|
|
} |
|
354
|
|
|
return $message; |
|
355
|
|
|
} |
|
356
|
|
|
} |
|
357
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: