-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMigrateEzImageCommand.php
More file actions
342 lines (275 loc) · 13.1 KB
/
Copy pathMigrateEzImageCommand.php
File metadata and controls
342 lines (275 loc) · 13.1 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
340
341
342
<?php
namespace Netgen\Bundle\RemoteMediaBundle\Command;
use eZ\Publish\API\Repository\Repository;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use Netgen\Bundle\RemoteMediaBundle\RemoteMedia\UploadFile;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
class MigrateEzImageCommand extends ContainerAwareCommand
{
protected $rootLocationId;
protected $webPath;
/** @var \Symfony\Component\Console\Input\InputInterface */
protected $input;
/** @var \Symfony\Component\Console\Output\OutputInterface */
protected $output;
/** @var \eZ\Publish\API\Repository\ContentTypeService */
protected $contentTypeService;
/** @var \eZ\Publish\API\Repository\ContentService */
protected $contentService;
/** @var \eZ\Publish\API\Repository\LocationService */
protected $locationService;
/** @var \eZ\Publish\API\Repository\SearchService */
protected $searchService;
/** @var \Netgen\Bundle\RemoteMediaBundle\RemoteMedia\RemoteMediaProvider */
protected $remoteMediaProvider;
/** @var \eZ\Publish\Core\Helper\FieldHelper */
protected $fieldHelper;
/** @var \eZ\Publish\Core\Helper\TranslationHelper */
protected $translationHelper;
/** @var \Sensio\Bundle\GeneratorBundle\Command\Helper\QuestionHelper */
protected $questionHelper;
protected function configure()
{
$this
->setName('netgen:ngremotemedia:migrate:ezimage')
->setDescription('This command will migrate ezimage field to ngremotemedia field.')
->addArgument(
'migrate-field',
InputArgument::IS_ARRAY,
"Migrate one field: content type identifier, image field type identifier, remote media type identifier"
)
->addOption(
'dry-run',
'd',
InputOption::VALUE_NONE
)
->addOption(
'continue-on-error',
'c',
InputOption::VALUE_NONE
)
;
}
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;
$this->contentTypeService = $this->getContainer()->get('ezpublish.api.service.content_type');
$this->searchService = $this->getContainer()->get('ezpublish.api.service.search');
$this->contentService = $this->getContainer()->get('ezpublish.api.service.content');
$this->locationService = $this->getContainer()->get('ezpublish.api.service.location');
$this->remoteMediaProvider = $this->getContainer()->get('netgen_remote_media.provider');
$this->fieldHelper = $this->getContainer()->get('ezpublish.field_helper');
$this->translationHelper = $this->getContainer()->get('ezpublish.translation_helper');
$this->questionHelper = $this->getHelper('question');
$this->rootLocationId = $this->getContainer()->get('ezpublish.config.resolver')->getParameter('content.tree_root.location_id');
$this->webPath = $this->getContainer()->getParameter('kernel.root_dir').'/../web';
}
protected function listFields()
{
$this->output->writeln('');
$this->output->writeln('List of all ez image and remotemedia fields');
$this->output->writeln('');
$contentTypes = [];
$contentTypeGroups = $this->contentTypeService->loadContentTypeGroups();
foreach ($contentTypeGroups as $contentTypeGroup) {
$contentTypesResult = $this->contentTypeService->loadContentTypes($contentTypeGroup);
foreach ($contentTypesResult as $contentType) {
$fields = $contentType->getFieldDefinitions();
foreach ($fields as $field) {
if ('ezimage' === $field->fieldTypeIdentifier || 'ngremotemedia' === $field->fieldTypeIdentifier) {
$contentTypes[$field->id] = [
$contentType->getName($contentType->mainLanguageCode),
$contentType->identifier,
$field->fieldTypeIdentifier,
$field->getName($contentType->mainLanguageCode),
$field->identifier,
];
}
}
}
}
$table = new Table($this->output);
$table
->setHeaders(['Content type', 'Content type identifier', 'Field type', 'Field name', 'Field identifier'])
->setRows($contentTypes);
$table->render();
}
protected function getContentType()
{
$question = new Question('Please select content type identifier to migrate: ', '');
$contentTypeIdentifier = $this->questionHelper->ask($this->input, $this->output, $question);
return $this->contentTypeService->loadContentTypeByIdentifier($contentTypeIdentifier);
}
protected function getFieldIdentifier(ContentType $contentType, $type)
{
$questionMessage = [
'source' => 'Please select identifier of source field (ezimage): ',
'destination' => 'Please select identifier of the destination field (ngremotemedia): ',
];
$question = new Question($questionMessage[$type], '');
$fieldIdentifier = $this->questionHelper->ask($this->input, $this->output, $question);
$field = $contentType->getFieldDefinition($fieldIdentifier);
if (!$field) {
throw new \InvalidArgumentException('Could not find the field with the selected identifier.');
}
return $fieldIdentifier;
}
protected function getOverwrite()
{
$question = new Question('Overwrite existing data on ngremotemedia field if exists? (y/n): ', 'n');
$overwrite = $this->questionHelper->ask($this->input, $this->output, $question);
return 'y' === $overwrite;
}
protected function getContinue()
{
$question = new Question('Continue? (y/n): ', 'y');
$continue = $this->questionHelper->ask($this->input, $this->output, $question);
return 'y' === $continue;
}
protected function searchLocations(ContentType $contentType, $offset = 0, $limit = 100)
{
$rootLocation = $this->locationService->loadLocation($this->rootLocationId);
$query = new LocationQuery();
$query->filter = new Query\Criterion\LogicalAnd(
[
new Query\Criterion\Subtree($rootLocation->pathString),
new Query\Criterion\ContentTypeIdentifier($contentType->identifier),
]
);
$query->limit = $limit;
$query->offset = $offset;
return $this->searchService->findLocations($query, [], false);
}
protected function migrateField(Location $location, $ezimageFieldIdentifier, $remoteMediaFieldIdentifier, $forceUpdate = false, $dryRun = false)
{
$content = $this->contentService->loadContentByContentInfo(
$location->getContentInfo()
);
if ($this->fieldHelper->isFieldEmpty($content, $ezimageFieldIdentifier)) {
$this->output->writeln("<error>Field '{$ezimageFieldIdentifier}' for the content {$content->id} is empty. Skipping...</error>");
return false;
}
if (!$forceUpdate) {
if (!$this->fieldHelper->isFieldEmpty($content, $remoteMediaFieldIdentifier)) {
$this->output->writeln("<error>RemoteMedia field is not empty, and 'overwrite' was not selected. Skipping...</error>");
return false;
}
}
// @todo: handle translations
$ezImageValue = $this->translationHelper->getTranslatedField($content, $ezimageFieldIdentifier)->value;
$uploadFile = UploadFile::fromEzImageValue($ezImageValue, $this->webPath);
if ($dryRun) {
$this->output->writeln('<info>Would migrate image: '.$uploadFile->uri().'</info>');
return true;
}
$repository = $this->getContainer()->get('ezpublish.api.repository');
$repository->beginTransaction();
try {
$contentDraft = $repository->sudo(
function (Repository $repository) use ($content) {
return $repository->getContentService()->createContentDraft($content->contentInfo);
}
);
$repository->commit();
} catch (\Exception $e) {
$repository->rollback();
throw $e;
}
$value = $this->remoteMediaProvider->upload($uploadFile);
$contentUpdateStruct = $this->contentService->newContentUpdateStruct();
$contentUpdateStruct->setField($remoteMediaFieldIdentifier, $value);
$repository->beginTransaction();
try {
$repository->sudo(
function (Repository $repository) use ($contentDraft, $contentUpdateStruct) {
$contentDraft = $repository->getContentService()->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
return $repository->getContentService()->publishVersion($contentDraft->versionInfo);
}
);
$repository->commit();
} catch (\Exception $e) {
$repository->rollback();
throw $e;
}
return true;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$dryRun = $input->getOption('dry-run');
$continueOnError = $input->getOption('continue-on-error');
$migrateField = $input->getArgument('migrate-field');
if (count($migrateField) == 3) {
$contentType = $this->contentTypeService->loadContentTypeByIdentifier($migrateField[0]);
$ezimageFieldIdentifier = $migrateField[1];
if (!$contentType->getFieldDefinition($ezimageFieldIdentifier)) {
throw new \InvalidArgumentException('Could not find the field with the identifier:'. $ezimageFieldIdentifier);
}
$remoteMediaFieldIdentifier = $migrateField[2];
if (!$contentType->getFieldDefinition($remoteMediaFieldIdentifier)) {
throw new \InvalidArgumentException('Could not find the field with the identifier:'. $remoteMediaFieldIdentifier);
}
} else {
$this->listFields();
$output->writeln('');
$contentType = $this->getContentType();
$ezimageFieldIdentifier = $this->getFieldIdentifier($contentType, 'source');
$remoteMediaFieldIdentifier = $this->getFieldIdentifier($contentType, 'destination');
}
$overwrite = $this->getOverwrite();
$siteaccess = $this->getContainer()->get('ezpublish.siteaccess');
$updated = 0;
$offset = 0;
$limit = 5;
do {
$result = $this->searchLocations($contentType, $offset, $limit);
if (0 === $offset) {
$this->output->writeln("<info>Found {$result->totalCount} objects for siteaccess '{$siteaccess->name}'...</info>");
$continue = $this->getContinue();
if (!$continue) {
return;
}
}
foreach ($result->searchHits as $searchHit) {
try {
/** @var Location $location */
$location = $searchHit->valueObject;
$this->output->writeln('Migrating: '.$location->getContentInfo()->id.' - "'.$location->getContentInfo()->name.'"');
$migrateResult = $this->migrateField(
$location,
$ezimageFieldIdentifier,
$remoteMediaFieldIdentifier,
$overwrite,
$dryRun
);
if ($migrateResult) {
$this->output->writeln('<info>Success</info>');
$this->output->writeln('-----------------------------');
++$updated;
} else {
$this->output->writeln('<error>No change</error>');
$this->output->writeln('-----------------------------');
}
} catch (\Exception $e) {
if ($continueOnError) {
$this->output->writeln("<error>ContentId: {$searchHit->valueObject->contentInfo->id}: ".$e->getMessage().'</error>');
} else {
throw $e;
}
}
}
$offset += $limit;
} while (count($result->searchHits) > 0);
$this->output->writeln("<info>Updated {$updated} objects.</info>");
}
}