16 import(
'lib.pkp.classes.plugins.ImportExportPlugin');
23 function register($category, $path, $mainContextId =
null) {
24 $success = parent::register($category, $path, $mainContextId);
26 $this->
import(
'NativeImportExportDeployment');
36 return 'NativeImportExportPlugin';
44 return __(
'plugins.importexport.native.displayName');
52 return __(
'plugins.importexport.native.description');
71 switch (array_shift($args)) {
74 $apiUrl =
$request->getDispatcher()->url(
$request, ROUTE_API, $context->getPath(),
'submissions');
75 $submissionsListPanel = new \APP\components\listPanels\SubmissionsListPanel(
77 __(
'common.publications'),
81 'getParams' =>
new stdClass(),
85 $submissionsConfig = $submissionsListPanel->getConfig();
86 $submissionsConfig[
'addUrl'] =
'';
87 $submissionsConfig[
'filters'] = array_slice($submissionsConfig[
'filters'], 1);
88 $templateMgr->setState([
90 'submissions' => $submissionsConfig,
93 $templateMgr->assign([
94 'pageComponent' =>
'ImportExportPage',
98 case 'uploadImportXML':
100 import(
'lib.pkp.classes.file.TemporaryFileManager');
102 $temporaryFile = $temporaryFileManager->handleUpload(
'uploadedFile', $user->getId());
103 if ($temporaryFile) {
105 $json->setAdditionalAttributes(array(
106 'temporaryFileId' => $temporaryFile->getId()
109 $json =
new JSONMessage(
false, __(
'common.uploadFailed'));
111 header(
'Content-Type: application/json');
112 return $json->getString();
115 $json->setEvent(
'addTab', array(
116 'title' => __(
'plugins.importexport.native.results'),
117 'url' =>
$request->url(
null,
null,
null, array(
'plugin', $this->
getName(),
'import'), array(
'temporaryFileId' =>
$request->getUserVar(
'temporaryFileId'))),
119 header(
'Content-Type: application/json');
120 return $json->getString();
123 $temporaryFileId =
$request->getUserVar(
'temporaryFileId');
126 $temporaryFile = $temporaryFileDao->getTemporaryFile($temporaryFileId, $user->getId());
127 if (!$temporaryFile) {
128 $json =
new JSONMessage(
true, __(
'plugins.inportexport.native.uploadFile'));
129 header(
'Content-Type: application/json');
130 return $json->getString();
132 $temporaryFilePath = $temporaryFile->getFilePath();
134 $filter =
'native-xml=>issue';
136 $xmlString = file_get_contents($temporaryFilePath);
137 $document =
new DOMDocument();
138 $document->loadXml($xmlString);
139 if (in_array($document->documentElement->tagName, array(
'article',
'articles'))) {
140 $filter =
'native-xml=>article';
145 libxml_use_internal_errors(
true);
146 $content = $this->
importSubmissions(file_get_contents($temporaryFilePath), $filter, $deployment);
147 $templateMgr->assign(
'content', $content);
148 $validationErrors = array_filter(libxml_get_errors(),
function($a) {
149 return $a->level == LIBXML_ERR_ERROR || $a->level == LIBXML_ERR_FATAL;
151 $templateMgr->assign(
'validationErrors', $validationErrors);
152 libxml_clear_errors();
155 $warningTypes = array(
156 ASSOC_TYPE_ISSUE =>
'issuesWarnings',
157 ASSOC_TYPE_SUBMISSION =>
'submissionsWarnings',
158 ASSOC_TYPE_SECTION =>
'sectionWarnings',
160 foreach ($warningTypes as $assocType => $templateVar) {
161 $foundWarnings = $deployment->getProcessedObjectsWarnings($assocType);
162 if (!empty($foundWarnings)) {
163 $templateMgr->assign($templateVar, $foundWarnings);
169 ASSOC_TYPE_ISSUE =>
'issuesErrors',
170 ASSOC_TYPE_SUBMISSION =>
'submissionsErrors',
171 ASSOC_TYPE_SECTION =>
'sectionErrors',
173 $foundErrors =
false;
174 foreach ($errorTypes as $assocType => $templateVar) {
175 $currentErrors = $deployment->getProcessedObjectsErrors($assocType);
176 if (!empty($currentErrors)) {
177 $templateMgr->assign($templateVar, $currentErrors);
183 if ($foundErrors || !empty($validationErrors)) {
185 foreach (array_keys($errorTypes) as $assocType) {
186 $deployment->removeImportedObjects($assocType);
190 $json =
new JSONMessage(
true, $templateMgr->fetch($this->getTemplateResource(
'results.tpl')));
191 header(
'Content-Type: application/json');
192 return $json->getString();
193 case 'exportSubmissions':
195 (array)
$request->getUserVar(
'selectedSubmissions'),
199 import(
'lib.pkp.classes.file.FileManager');
202 $fileManager->writeFile($exportFileName, $exportXml);
203 $fileManager->downloadByPath($exportFileName);
204 $fileManager->deleteByPath($exportFileName);
208 (array)
$request->getUserVar(
'selectedIssues'),
212 import(
'lib.pkp.classes.file.FileManager');
215 $fileManager->writeFile($exportFileName, $exportXml);
216 $fileManager->downloadByPath($exportFileName);
217 $fileManager->deleteByPath($exportFileName);
220 $dispatcher =
$request->getDispatcher();
221 $dispatcher->handle404();
236 $nativeExportFilters = $filterDao->getObjectsByGroup(
'article=>native-xml');
237 assert(count($nativeExportFilters) == 1);
238 $exportFilter = array_shift($nativeExportFilters);
241 $submissions = array();
242 foreach ($submissionIds as $submissionId) {
245 $submission = $submissionService->get($submissionId);
247 if ($submission) $submissions[] = $submission;
250 libxml_use_internal_errors(
true);
251 $exportFilter->setOpts($opts);
252 $submissionXml = $exportFilter->execute($submissions,
true);
253 $xml = $submissionXml->saveXml();
255 $errors = array_filter(libxml_get_errors(),
function($a) {
256 return $a->level == LIBXML_ERR_ERROR || $a->level == LIBXML_ERR_FATAL;
259 if (!empty($errors)) {
276 $nativeExportFilters = $filterDao->getObjectsByGroup(
'issue=>native-xml');
277 assert(count($nativeExportFilters) == 1);
278 $exportFilter = array_shift($nativeExportFilters);
281 foreach ($issueIds as $issueId) {
282 $issue = $issueDao->getById($issueId, $context->getId());
283 if ($issue) $issues[] = $issue;
285 libxml_use_internal_errors(
true);
286 $exportFilter->setOpts($opts);
287 $issueXml = $exportFilter->execute($issues,
true);
288 $xml = $issueXml->saveXml();
289 $errors = array_filter(libxml_get_errors(),
function($a) {
290 return $a->level == LIBXML_ERR_ERROR || $a->level == LIBXML_ERR_FATAL;
292 if (!empty($errors)) {
307 $nativeImportFilters = $filterDao->getObjectsByGroup($filter);
308 assert(count($nativeImportFilters) == 1);
309 $importFilter = array_shift($nativeImportFilters);
310 $importFilter->setDeployment($deployment);
311 return $importFilter->execute($importXml);
318 echo __(
'plugins.importexport.native.cliUsage', array(
319 'scriptName' => $scriptName,
320 'pluginName' => $this->
getName()
328 $opts = $this->
parseOpts($args, [
'no-embed',
'use-file-urls']);
329 $command = array_shift($args);
330 $xmlFile = array_shift($args);
331 $journalPath = array_shift($args);
338 $journal = $journalDao->getByPath($journalPath);
341 if ($journalPath !=
'') {
342 echo __(
'plugins.importexport.common.cliError') .
"\n";
343 echo __(
'plugins.importexport.common.error.unknownJournal', array(
'journalPath' => $journalPath)) .
"\n\n";
345 $this->
usage($scriptName);
350 $xmlFile = PWD .
'/' . $xmlFile;
355 $userName = array_shift($args);
356 $user = $userDao->getByUsername($userName);
359 if ($userName !=
'') {
360 echo __(
'plugins.importexport.common.cliError') .
"\n";
361 echo __(
'plugins.importexport.native.error.unknownUser', array(
'userName' => $userName)) .
"\n\n";
363 $this->
usage($scriptName);
367 if (!file_exists($xmlFile)) {
368 echo __(
'plugins.importexport.common.cliError') .
"\n";
369 echo __(
'plugins.importexport.common.export.error.inputFileNotReadable', array(
'param' => $xmlFile)) .
"\n\n";
370 $this->
usage($scriptName);
374 $filter =
'native-xml=>issue';
376 $xmlString = file_get_contents($xmlFile);
377 $document =
new DOMDocument();
378 $document->loadXml($xmlString);
379 if (in_array($document->documentElement->tagName, array(
'article',
'articles'))) {
380 $filter =
'native-xml=>article';
383 $deployment->setImportPath(dirname($xmlFile));
385 $validationErrors = array_filter(libxml_get_errors(),
function($a) {
386 return $a->level == LIBXML_ERR_ERROR || $a->level == LIBXML_ERR_FATAL;
391 ASSOC_TYPE_ISSUE =>
'issue.issue',
392 ASSOC_TYPE_SUBMISSION =>
'submission.submission',
393 ASSOC_TYPE_SECTION =>
'section.section',
395 foreach ($errorTypes as $assocType => $localeKey) {
396 $foundWarnings = $deployment->getProcessedObjectsWarnings($assocType);
397 if (!empty($foundWarnings)) {
398 echo __(
'plugins.importexport.common.warningsEncountered') .
"\n";
400 foreach ($foundWarnings as $foundWarningMessages) {
401 if (count($foundWarningMessages) > 0) {
402 echo ++$i .
'.' . __($localeKey) .
"\n";
403 foreach ($foundWarningMessages as $foundWarningMessage) {
404 echo
'- ' . $foundWarningMessage .
"\n";
412 $foundErrors =
false;
413 foreach ($errorTypes as $assocType => $localeKey) {
414 $currentErrors = $deployment->getProcessedObjectsErrors($assocType);
415 if (!empty($currentErrors)) {
416 echo __(
'plugins.importexport.common.errorsOccured') .
"\n";
418 foreach ($currentErrors as $currentErrorMessages) {
419 if (count($currentErrorMessages) > 0) {
420 echo ++$i .
'.' . __($localeKey) .
"\n";
421 foreach ($currentErrorMessages as $currentErrorMessage) {
422 echo
'- ' . $currentErrorMessage .
"\n";
431 if ($foundErrors || !empty($validationErrors)) {
433 foreach (array_keys($errorTypes) as $assocType) {
434 $deployment->removeImportedObjects($assocType);
439 $outputDir = dirname($xmlFile);
440 if (!is_writable($outputDir) || (file_exists($xmlFile) && !is_writable($xmlFile))) {
441 echo __(
'plugins.importexport.common.cliError') .
"\n";
442 echo __(
'plugins.importexport.common.export.error.outputFileNotWritable', array(
'param' => $xmlFile)) .
"\n\n";
443 $this->
usage($scriptName);
446 if ($xmlFile !=
'')
switch (array_shift($args)) {
468 $this->
usage($scriptName);
482 foreach ($args as $arg) {
484 $opts[$sticky] = $arg;
488 if (!starts_with($arg,
'--')) {
492 $opt = substr($arg, 2);
493 if (in_array($opt, $optCodes)) {
497 if (in_array($opt .
":", $optCodes)) {