Repository navigation
Expand file tree
/
Copy pathFullJournalImportExportDeployment.php
More file actions
458 lines (412 loc) · 16.7 KB
/
Copy pathFullJournalImportExportDeployment.php
File metadata and controls
458 lines (412 loc) · 16.7 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<?php
declare(strict_types=1);
namespace APP\plugins\importexport\fullJournalTransfer;
use APP\core\Application;
use APP\facades\Repo;
use APP\file\PublicFileManager;
use APP\plugins\importexport\fullJournalTransfer\filter\nativeData\NativeXmlNativeDataFilter;
use APP\plugins\importexport\fullJournalTransfer\package\ArchiveManager;
use APP\plugins\importexport\fullJournalTransfer\persistence\WorkflowUserFinder;
use APP\plugins\importexport\fullJournalTransfer\transfer\ImportedResourceJournal;
use APP\plugins\importexport\fullJournalTransfer\transfer\TransferState;
use APP\plugins\importexport\native\NativeImportExportDeployment;
use DOMDocument;
use DOMElement;
use InvalidArgumentException;
use PKP\plugins\importexport\PKPImportExportFilter;
use PKP\site\Site;
use PKP\submissionFile\SubmissionFile;
use PKP\user\Collector;
use Throwable;
class FullJournalImportExportDeployment extends NativeImportExportDeployment
{
private ?ImportedResourceJournal $resourceJournal = null;
private ?TransferState $transferState = null;
private ?DOMElement $historicalDatesNode = null;
public function getStageNameStageIdMapping()
{
return array_merge(parent::getStageNameStageIdMapping(), [
'internal_review_file' => SubmissionFile::SUBMISSION_FILE_INTERNAL_REVIEW_FILE,
'internal_review_revision' => SubmissionFile::SUBMISSION_FILE_INTERNAL_REVIEW_REVISION,
]);
}
public function importPackage(
string $archivePath,
string $rootFilter,
?ArchiveManager $archiveManager = null,
?callable $progress = null
): bool {
$archiveManager = $archiveManager ?? new ArchiveManager();
return $archiveManager->withExtractedPackage(
$archivePath,
function (string $stagingPath) use ($rootFilter, $progress): bool {
$journalXml = file_get_contents($stagingPath . DIRECTORY_SEPARATOR . 'journal.xml');
if ($journalXml === false) {
throw new \RuntimeException(__('plugins.importexport.fullJournal.error.journalXmlReadFailed'));
}
$this->setImportPath($stagingPath);
try {
$this->warnAboutUnavailableLocales($journalXml, $progress);
if ($progress) {
$progress(__('plugins.importexport.fullJournal.progress.importingJournalData'));
}
$this->import($rootFilter, $journalXml);
return !$this->isProcessFailed();
} finally {
$this->setImportPath('');
}
},
$progress
);
}
private function warnAboutUnavailableLocales(string $journalXml, ?callable $progress): void
{
if (!$progress) {
return;
}
$document = new DOMDocument('1.0', 'UTF-8');
if (!$document->loadXML($journalXml, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING)) {
return;
}
$root = $document->documentElement;
if (!$root instanceof DOMElement) {
return;
}
$sourceLocales = [];
foreach ($root->childNodes as $child) {
if (!$child instanceof DOMElement || $child->localName !== 'locales') {
continue;
}
foreach ($child->childNodes as $localeNode) {
if ($localeNode instanceof DOMElement && $localeNode->localName === 'locale') {
$sourceLocales[] = trim($localeNode->getAttribute('code'));
}
}
}
$site = $this->getSite();
if (!$site instanceof Site) {
return;
}
$unavailableLocales = array_values(array_diff(array_unique($sourceLocales), $site->getSupportedLocales()));
if ($unavailableLocales === []) {
return;
}
$progress(
__(
'plugins.importexport.fullJournal.warning.unavailableLocales',
[
'unavailableLocales' => implode(', ', $unavailableLocales),
]
)
);
}
public function import($rootFilter, $importXml)
{
$this->resourceJournal()->reset();
$this->historicalDatesNode = null;
try {
$this->runNativeImport($rootFilter, $importXml);
} catch (Throwable $exception) {
$this->compensateCreatedResources();
throw $exception;
}
if ($this->isProcessFailed()) {
$this->compensateCreatedResources();
} else {
$this->resourceJournal()->reset();
}
}
public function recordCreatedFile(string $path): void
{
$this->resourceJournal()->recordFile($path);
}
public function recordCreatedDirectory(string $path): void
{
$this->resourceJournal()->recordDirectory($path);
}
public function exportContextData(): DOMDocument
{
$filter = PKPImportExportFilter::getFilter('journal=>full-journal-xml', $this);
$context = $this->getContext();
return $filter->execute($context);
}
public function importContextData(DOMElement $root): void
{
$filter = PKPImportExportFilter::getFilter('full-journal-xml=>journal', $this);
$filter->hydrate($root, $this->getContext());
}
public function createContextData(DOMElement $root): \APP\journal\Journal
{
$filter = PKPImportExportFilter::getFilter('full-journal-xml=>journal', $this);
$context = $filter->handleElement($root);
$this->setContext($context);
return $context;
}
public function exportUsers(): DOMDocument
{
$filter = PKPImportExportFilter::getFilter('user=>full-journal-user-xml', $this);
$usersById = [];
$contextUsers = Repo::user()->getCollector()
->filterByContextIds([$this->getContext()->getId()])
->getMany();
foreach ($contextUsers as $user) {
$usersById[(int) $user->getId()] = $user;
}
$workflowUserIds = (new WorkflowUserFinder())->findIds((int) $this->getContext()->getId());
if ($workflowUserIds !== []) {
$workflowUsers = Repo::user()->getCollector()
->filterByUserIds($workflowUserIds)
->filterByStatus(Collector::STATUS_ALL)
->getMany();
foreach ($workflowUsers as $user) {
$usersById[(int) $user->getId()] = $user;
}
}
$missingUserIds = array_diff($workflowUserIds, array_keys($usersById));
if ($missingUserIds !== []) {
throw new InvalidArgumentException(__(
'plugins.importexport.fullJournal.error.workflowReferencesMissingUser',
[
'userId' => reset($missingUserIds),
]
));
}
ksort($usersById, SORT_NUMERIC);
$users = array_values($usersById);
$nativeDocument = $filter->execute($users);
$document = new DOMDocument('1.0', 'UTF-8');
$root = $document->createElementNS($this->getNamespace(), 'users');
$document->appendChild($root);
foreach ($nativeDocument->documentElement->childNodes as $child) {
if ($child instanceof DOMElement) {
$root->appendChild($document->importNode($child, true));
}
}
return $document;
}
public function importUsers(DOMElement $usersNode): array
{
$userGroupsNode = $this->getRequiredChild($usersNode, 'user_groups');
$userListNode = $this->getRequiredChild($usersNode, 'users');
$this->resetReferenceMap('user_group');
$this->resetReferenceMap('user');
$this->transferState()->resetUserConflicts();
$userGroupFilter = PKPImportExportFilter::getFilter('full-journal-user-xml=>user-group', $this);
$userGroupsDocument = $this->documentFor($userGroupsNode);
$userGroupFilter->execute($userGroupsDocument);
$userFilter = PKPImportExportFilter::getFilter('full-journal-user-xml=>user', $this);
$usersDocument = $this->documentFor($userListNode);
$userFilter->execute($usersDocument);
return [
'user_id_map' => $this->getReferenceMap('user'),
'user_group_id_map' => $this->getReferenceMap('user_group'),
'conflicts' => $this->getUserConflicts(),
];
}
public function exportReferenceData(): DOMDocument
{
$filter = PKPImportExportFilter::getFilter('reference-data=>full-journal-xml', $this);
$context = $this->getContext();
return $filter->execute($context);
}
public function importReferenceData(DOMElement $referenceDataNode): array
{
foreach (['review_form', 'review_form_element', 'genre', 'section'] as $entity) {
$this->resetReferenceMap($entity);
}
$filter = PKPImportExportFilter::getFilter('full-journal-xml=>reference-data', $this);
$document = $this->documentFor($referenceDataNode);
$filter->execute($document);
return [
'review_form_id_map' => $this->getReferenceMap('review_form'),
'review_form_element_id_map' => $this->getReferenceMap('review_form_element'),
'genre_id_map' => $this->getReferenceMap('genre'),
'section_id_map' => $this->getReferenceMap('section'),
];
}
public function exportNativeData(): DOMDocument
{
$filter = PKPImportExportFilter::getFilter('native-data=>full-journal-xml', $this);
$context = $this->getContext();
return $filter->execute($context);
}
public function importNativeData(DOMElement $nativeDataNode): array
{
$this->historicalDatesNode = null;
$filter = PKPImportExportFilter::getFilter('full-journal-xml=>native-data', $this);
$document = $this->documentFor($nativeDataNode);
$filter->execute($document);
foreach ($document->documentElement->childNodes as $child) {
if ($child instanceof DOMElement && $child->localName === 'historical_dates') {
$this->historicalDatesNode = $child;
break;
}
}
return [
'issue_id_map' => $this->getReferenceMap('issue'),
'submission_id_map' => $this->getReferenceMap('submission'),
'publication_id_map' => $this->getReferenceMap('publication'),
'author_id_map' => $this->getReferenceMap('author'),
'issue_galley_id_map' => $this->getReferenceMap('issue_galley'),
'article_galley_id_map' => $this->getReferenceMap('article_galley'),
'submission_file_id_map' => $this->getReferenceMap('submission_file'),
'file_id_map' => $this->getReferenceMap('file'),
];
}
public function exportWorkflow(): DOMDocument
{
$filter = PKPImportExportFilter::getFilter('workflow=>full-journal-xml', $this);
$context = $this->getContext();
return $filter->execute($context);
}
public function importWorkflow(DOMElement $workflowNode): array
{
$filter = PKPImportExportFilter::getFilter('full-journal-xml=>workflow', $this);
$document = $this->documentFor($workflowNode);
$filter->execute($document);
$this->restoreHistoricalDates();
return [
'stage_assignment_id_map' => $this->getReferenceMap('stage_assignment'),
'review_round_id_map' => $this->getReferenceMap('review_round'),
'review_assignment_id_map' => $this->getReferenceMap('review_assignment'),
'discussion_id_map' => $this->getReferenceMap('discussion'),
'discussion_note_id_map' => $this->getReferenceMap('discussion_note'),
'discussion_attachment_id_map' => $this->getReferenceMap('discussion_attachment'),
'editorial_decision_id_map' => $this->getReferenceMap('editorial_decision'),
];
}
private function restoreHistoricalDates(): void
{
if (!$this->historicalDatesNode) {
return;
}
$filter = PKPImportExportFilter::getFilter('full-journal-xml=>native-data', $this);
if (!$filter instanceof NativeXmlNativeDataFilter) {
throw new InvalidArgumentException(__('plugins.importexport.fullJournal.error.nativeDataFilterCannotRestoreHistoricalDates'));
}
$filter->restoreHistoricalDates($this->historicalDatesNode);
}
public function exportMetrics(): DOMDocument
{
$filter = PKPImportExportFilter::getFilter('metrics=>full-journal-xml', $this);
$context = $this->getContext();
return $filter->execute($context);
}
public function importMetrics(DOMElement $metricsNode): void
{
$this->transferState()->resetMetricRejections();
$filter = PKPImportExportFilter::getFilter('full-journal-xml=>metrics', $this);
$document = $this->documentFor($metricsNode);
$filter->execute($document);
}
public function addMetricRejection(array $rejection): void
{
$this->transferState()->addMetricRejection($rejection);
}
public function getMetricRejections(): array
{
return $this->transferState()->getMetricRejections();
}
public function mapReference(string $entity, string $sourceReference, int $destinationId): void
{
$this->transferState()->mapReference($entity, $sourceReference, $destinationId);
}
public function getReferenceMap(string $entity): array
{
return $this->transferState()->getReferenceMap($entity);
}
public function resetReferenceMap(string $entity): void
{
$this->transferState()->resetReferenceMap($entity);
}
public function requireReference(string $entity, string $sourceReference): int
{
return $this->transferState()->requireReference($entity, $sourceReference);
}
public function addUserConflict(array $conflict): void
{
$this->transferState()->addUserConflict($conflict);
}
public function getUserConflicts(): array
{
return $this->transferState()->getUserConflicts();
}
public function getSite()
{
return Application::get()->getRequest()->getSite();
}
public function setCurrentReviewFormId(?int $reviewFormId): void
{
$this->transferState()->setCurrentReviewFormId($reviewFormId);
}
public function getCurrentReviewFormId(): ?int
{
return $this->transferState()->getCurrentReviewFormId();
}
public function setSubmissionsByIssue(array $submissionsByIssue): void
{
$this->transferState()->setSubmissionsByIssue($submissionsByIssue);
}
public function getSubmissionsForIssue(int $issueId): array
{
return $this->transferState()->getSubmissionsForIssue($issueId);
}
private function transferState(): TransferState
{
if (!$this->transferState) {
$this->transferState = new TransferState();
}
return $this->transferState;
}
protected function runNativeImport($rootFilter, $importXml): void
{
if (is_string($importXml)) {
$document = new DOMDocument('1.0', 'UTF-8');
libxml_use_internal_errors(true);
$document->loadXML($importXml);
$importXml = $document;
}
parent::import($rootFilter, $importXml);
}
private function compensateCreatedResources(): void
{
$publicFileManager = new PublicFileManager();
$errors = $this->resourceJournal()->compensate(function (string $path) use ($publicFileManager): bool {
return $publicFileManager->rmtree($path);
});
foreach ($errors as $error) {
$this->addError(\PKP\core\PKPApplication::ASSOC_TYPE_NONE, 0, $error);
}
}
private function resourceJournal(): ImportedResourceJournal
{
if (!$this->resourceJournal) {
$this->resourceJournal = new ImportedResourceJournal();
}
return $this->resourceJournal;
}
private function getRequiredChild(DOMElement $parent, string $name): DOMElement
{
$matches = [];
foreach ($parent->childNodes as $child) {
if ($child instanceof DOMElement && $child->localName === $name) {
$matches[] = $child;
}
}
if (count($matches) !== 1) {
throw new InvalidArgumentException(__(
'plugins.importexport.fullJournal.error.expectedSingleElement',
[
'name' => $name,
]
));
}
return $matches[0];
}
private function documentFor(DOMElement $element): DOMDocument
{
$document = new DOMDocument('1.0', 'UTF-8');
$document->appendChild($document->importNode($element, true));
return $document;
}
}