16 import(
'lib.pkp.classes.plugins.ImportExportPlugin');
19 define(
'EXPORT_STATUS_ANY',
'');
20 define(
'EXPORT_STATUS_NOT_DEPOSITED',
'notDeposited');
21 define(
'EXPORT_STATUS_MARKEDREGISTERED',
'markedRegistered');
22 define(
'EXPORT_STATUS_REGISTERED',
'registered');
25 define(
'EXPORT_ACTION_EXPORT',
'export');
26 define(
'EXPORT_ACTION_MARKREGISTERED',
'markRegistered');
27 define(
'EXPORT_ACTION_DEPOSIT',
'deposit');
30 define(
'EXPORT_CONFIG_ERROR_SETTINGS', 0x02);
42 if (!is_a($this->_cache,
'PubObjectCache')) {
44 import(
'classes.plugins.PubObjectCache');
53 function register($category, $path, $mainContextId =
null) {
54 if (!parent::register($category, $path, $mainContextId))
return false;
60 foreach ($this->
_getDAOs() as $dao) {
80 switch (
$request->getUserVar(
'verb')) {
82 $form->readInputData();
83 if ($form->validate()) {
85 $notificationManager->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_SUCCESS);
97 $templateMgr->assign(array(
98 'statusMessage' => htmlentities($statusMessage),
100 return new JSONMessage(
true, $templateMgr->fetch($this->getTemplateResource(
'statusMessage.tpl')));
103 return parent::manage($args,
$request);
113 switch (array_shift($args)) {
117 $configurationErrors = array();
120 foreach($form->getFormFields() as $fieldName => $fieldType) {
121 if ($form->isOptional($fieldName))
continue;
122 $pluginSetting = $this->
getSetting($context->getId(), $fieldName);
123 if (empty($pluginSetting)) {
124 $configurationErrors[] = EXPORT_CONFIG_ERROR_SETTINGS;
132 import(
'lib.pkp.classes.linkAction.request.NullAction');
133 $linkActions = array();
134 foreach ($actionNames as $action => $actionName) {
138 $templateMgr->assign(array(
140 'actionNames' => $actionNames,
141 'configurationErrors' => $configurationErrors,
144 case 'exportSubmissions':
145 case 'exportRepresentations':
146 $selectedSubmissions = (array)
$request->getUserVar(
'selectedSubmissions');
147 $selectedRepresentations = (array)
$request->getUserVar(
'selectedRepresentations');
148 $tab = (string)
$request->getUserVar(
'tab');
149 $noValidation =
$request->getUserVar(
'validation') ? false :
true;
151 if (empty($selectedSubmissions) && empty($selectedRepresentations)) {
152 fatalError(__(
'plugins.importexport.common.error.noObjectsSelected'));
154 if (!empty($selectedSubmissions)) {
157 $objectsFileNamePart =
'preprints';
158 } elseif (!empty($selectedRepresentations)) {
161 $objectsFileNamePart =
'galleys';
180 $path = array(
'plugin', $this->
getName());
182 assert($filter !=
null);
184 $exportXml = $this->
exportXML($objects, $filter, $context, $noValidation);
185 import(
'lib.pkp.classes.file.FileManager');
188 $fileManager->writeFile($exportFileName, $exportXml);
189 $fileManager->downloadByPath($exportFileName);
190 $fileManager->deleteByPath($exportFileName);
191 } elseif (
$request->getUserVar(EXPORT_ACTION_DEPOSIT)) {
192 assert($filter !=
null);
194 $exportXml = $this->
exportXML($objects, $filter, $context, $noValidation);
197 import(
'lib.pkp.classes.file.FileManager');
200 $fileManager->writeFile($exportFileName, $exportXml);
202 $result = $this->
depositXML($objects, $context, $exportFileName);
204 if ($result ===
true) {
207 $this->getDepositSuccessNotificationMessageKey(),
208 NOTIFICATION_TYPE_SUCCESS
211 if (is_array($result)) {
212 foreach($result as $error) {
213 assert(is_array($error) && count($error) >= 1);
217 NOTIFICATION_TYPE_ERROR,
218 (isset($error[1]) ? $error[1] :
null)
224 $fileManager->deleteByPath($exportFileName);
226 $request->redirect(
null,
null,
null, $path,
null, $tab);
227 } elseif (
$request->getUserVar(EXPORT_ACTION_MARKREGISTERED)) {
230 $request->redirect(
null,
null,
null, $path,
null, $tab);
232 $dispatcher =
$request->getDispatcher();
233 $dispatcher->handle404();
242 return 'plugins.importexport.common.register.success';
253 abstract function depositXML($objects, $context, $filename);
287 EXPORT_STATUS_ANY => __(
'plugins.importexport.common.status.any'),
288 EXPORT_STATUS_NOT_DEPOSITED => __(
'plugins.importexport.common.status.notDeposited'),
289 EXPORT_STATUS_MARKEDREGISTERED => __(
'plugins.importexport.common.status.markedRegistered'),
290 EXPORT_STATUS_REGISTERED => __(
'plugins.importexport.common.status.registered'),
310 $actions = array(EXPORT_ACTION_EXPORT, EXPORT_ACTION_MARKREGISTERED);
311 if ($this->
getSetting($context->getId(),
'username') && $this->getSetting($context->getId(),
'password')) {
312 array_unshift($actions, EXPORT_ACTION_DEPOSIT);
323 EXPORT_ACTION_DEPOSIT => __(
'plugins.importexport.common.action.register'),
324 EXPORT_ACTION_EXPORT => __(
'plugins.importexport.common.action.export'),
325 EXPORT_ACTION_MARKREGISTERED => __(
'plugins.importexport.common.action.markRegistered'),
343 function exportXML($objects, $filter, $context, $noValidation =
null) {
345 $exportFilters = $filterDao->getObjectsByGroup($filter);
346 assert(count($exportFilters) == 1);
347 $exportFilter = array_shift($exportFilters);
349 $exportFilter->setDeployment($exportDeployment);
350 if ($noValidation) $exportFilter->setNoValidation($noValidation);
351 libxml_use_internal_errors(
true);
352 $exportXml = $exportFilter->execute($objects,
true);
353 $xml = $exportXml->saveXml();
354 $errors = array_filter(libxml_get_errors(),
function($a) {
355 return $a->level == LIBXML_ERR_ERROR || $a->level == LIBXML_ERR_FATAL;
357 if (!empty($errors)) {
369 foreach ($objects as $object) {
385 $dao = $object->getDAO();
386 $dao->updateObject($object);
398 assert(count($args) == 2);
399 $additionalFields =& $args[1];
400 assert(is_array($additionalFields));
402 $additionalFields[] = $fieldName;
417 $schema =& $params[0];
419 $schema->properties->{$fieldName} = (object) [
421 'apiSummary' =>
true,
422 'validation' => [
'nullable'],
433 protected function _getObjectAdditionalSettings() {
434 return array($this->getDepositStatusSettingName());
440 function callbackParseCronTab($hookName, $args) {
441 $taskFilesPath =& $args[0];
442 $taskFilesPath[] = $this->getPluginPath() . DIRECTORY_SEPARATOR .
'scheduledTasks.xml';
451 function getUnregisteredArticles($context) {
454 $articles = $submissionDao->getExportable(
460 $this->getDepositStatusSettingName(),
461 EXPORT_STATUS_NOT_DEPOSITED,
464 return $articles->toArray();
471 function isTestMode($context) {
472 return ($this->getSetting($context->getId(),
'testMode') == 1);
479 function getDepositStatusSettingName() {
480 return $this->getPluginSettingsPrefix().
'::status';
488 function usage($scriptName) {
490 'plugins.importexport.' . $this->getPluginSettingsPrefix() .
'.cliUsage',
492 'scriptName' => $scriptName,
493 'pluginName' => $this->getName()
501 function executeCLI($scriptName, &$args) {
504 $command = array_shift($args);
505 if (!in_array($command, array(
'export',
'register'))) {
506 $this->usage($scriptName);
510 $outputFile = $command ==
'export' ? array_shift($args) : null;
511 $contextPath = array_shift($args);
512 $objectType = array_shift($args);
515 $context = $contextDao->getByPath($contextPath);
517 if ($contextPath !=
'') {
518 echo __(
'plugins.importexport.common.cliError') .
"\n";
519 echo __(
'plugins.importexport.common.error.unknownJournal', array(
'journalPath' => $contextPath)) .
"\n\n";
521 $this->usage($scriptName);
526 if ($this->isRelativePath($outputFile)) {
527 $outputFile = PWD .
'/' . $outputFile;
529 $outputDir = dirname($outputFile);
530 if (!is_writable($outputDir) || (file_exists($outputFile) && !is_writable($outputFile))) {
531 echo __(
'plugins.importexport.common.cliError') .
"\n";
532 echo __(
'plugins.importexport.common.export.error.outputFileNotWritable', array(
'param' => $outputFile)) .
"\n\n";
533 $this->usage($scriptName);
538 switch ($objectType) {
540 $objects = $this->getPublishedSubmissions($args, $context);
541 $filter = $this->getSubmissionFilter();
542 $objectsFileNamePart =
'preprints';
545 $objects = $this->getArticleGalleys($args);
546 $filter = $this->getRepresentationFilter();
547 $objectsFileNamePart =
'galleys';
550 $this->usage($scriptName);
554 if (empty($objects)) {
555 echo __(
'plugins.importexport.common.cliError') .
"\n";
556 echo __(
'plugins.importexport.common.error.unknownObjects') .
"\n\n";
557 $this->usage($scriptName);
561 $this->usage($scriptName);
565 $this->executeCLICommand($scriptName, $command, $context, $outputFile, $objects, $filter, $objectsFileNamePart);
579 function executeCLICommand($scriptName, $command, $context, $outputFile, $objects, $filter, $objectsFileNamePart) {
580 $exportXml = $this->exportXML($objects, $filter, $context);
581 if ($command ==
'export' && $outputFile) file_put_contents($outputFile, $exportXml);
583 if ($command ==
'register') {
584 import(
'lib.pkp.classes.file.FileManager');
586 $exportFileName = $this->getExportFileName($this->getExportPath(), $objectsFileNamePart, $context,
'.xml');
587 $fileManager->writeFile($exportFileName, $exportXml);
588 $result = $this->depositXML($objects, $context, $exportFileName);
589 if ($result ===
true) {
590 echo __(
'plugins.importexport.common.register.success') .
"\n";
592 echo __(
'plugins.importexport.common.cliError') .
"\n";
593 if (is_array($result)) {
594 foreach($result as $error) {
595 assert(is_array($error) && count($error) >= 1);
596 $errorMessage = __($error[0], array(
'param' => (isset($error[1]) ? $error[1] :
null)));
597 echo
"*** $errorMessage\n";
601 echo __(
'plugins.importexport.common.register.error.mdsError', array(
'param' =>
' - ')) .
"\n\n";
603 $this->usage($scriptName);
605 $fileManager->deleteByPath($exportFileName);
615 function getPublishedSubmissions($submissionIds, $context) {
616 $submissions = array_map(
function($submissionId) {
619 return array_filter($submissions,
function($submission) {
620 return $submission->getData(
'status') === STATUS_PUBLISHED;
629 function getArticleGalleys($galleyIds) {
632 foreach ($galleyIds as $galleyId) {
633 $articleGalley = $articleGalleyDao->getById($galleyId);
634 if ($articleGalley) $galleys[] = $articleGalley;
646 function _sendNotification($user, $message, $notificationType, $param =
null) {
647 static $notificationManager =
null;
648 if (is_null($notificationManager)) {
649 import(
'classes.notification.NotificationManager');
652 if (!is_null($param)) {
653 $params = array(
'param' => $param);
657 $notificationManager->createTrivialNotification(
660 array(
'contents' => __($message, $params))
669 function _instantiateExportDeployment($context) {
670 $exportDeploymentClassName = $this->getExportDeploymentClassName();
671 $this->
import($exportDeploymentClassName);
672 $exportDeployment =
new $exportDeploymentClassName($context, $this);
673 return $exportDeployment;
681 function _instantiateSettingsForm($context) {
682 $settingsFormClassName = $this->getSettingsFormClassName();
683 $this->
import(
'classes.form.' . $settingsFormClassName);
684 $settingsForm =
new $settingsFormClassName($this, $context->getId());
685 return $settingsForm;
692 protected function _getDAOs() {