Open Journal Systems  3.3.0
DataciteInfoSender.inc.php
1 <?php
2 
16 import('lib.pkp.classes.scheduledTask.ScheduledTask');
17 
18 
21  var $_plugin;
22 
27  function __construct($args) {
28  PluginRegistry::loadCategory('importexport');
29  $plugin = PluginRegistry::getPlugin('importexport', 'DataciteExportPlugin'); /* @var $plugin DataciteExportPlugin */
30  $this->_plugin = $plugin;
31 
32  if (is_a($plugin, 'DataciteExportPlugin')) {
33  $plugin->addLocaleData();
34  }
35 
36  parent::__construct($args);
37  }
38 
42  function getName() {
43  return __('plugins.importexport.datacite.senderTask.name');
44  }
45 
49  function executeActions() {
50  if (!$this->_plugin) return false;
51 
52  $plugin = $this->_plugin;
53  $journals = $this->_getJournals();
54 
55  foreach ($journals as $journal) {
56  $pubIdPlugins = PluginRegistry::loadCategory('pubIds', true, $journal->getId());
57  $doiPubIdPlugin = $pubIdPlugins['doipubidplugin'];
58 
59  if ($doiPubIdPlugin->getSetting($journal->getId(), 'enableIssueDoi')) {
60  // Get unregistered issues
61  $unregisteredIssues = $plugin->getUnregisteredIssues($journal);
62  // If there are issues to be deposited
63  if (count($unregisteredIssues)) {
64  $this->_registerObjects($unregisteredIssues, 'issue=>datacite-xml', $journal, 'issues');
65  }
66  }
67 
68  if ($doiPubIdPlugin->getSetting($journal->getId(), 'enablePublicationDoi')) {
69  // Get unregistered articles
70  $unregisteredArticles = $plugin->getUnregisteredArticles($journal);
71  // If there are articles to be deposited
72  if (count($unregisteredArticles)) {
73  $this->_registerObjects($unregisteredArticles, 'article=>datacite-xml', $journal, 'articles');
74  }
75  }
76 
77  if ($doiPubIdPlugin->getSetting($journal->getId(), 'enableRepresentationDoi')) {
78  // Get unregistered galleys
79  $unregisteredGalleys = $plugin->getUnregisteredGalleys($journal);
80  // If there are galleys to be deposited
81  if (count($unregisteredGalleys)) {
82  $this->_registerObjects($unregisteredGalleys, 'galley=>datacite-xml', $journal, 'galleys');
83  }
84  }
85  }
86  return true;
87  }
88 
94  function _getJournals() {
95  $plugin = $this->_plugin;
96  $contextDao = Application::getContextDAO(); /* @var $contextDao JournalDAO */
97  $journalFactory = $contextDao->getAll(true);
98 
99  $journals = array();
100  while($journal = $journalFactory->next()) {
101  $journalId = $journal->getId();
102  if (!$plugin->getSetting($journalId, 'username') || !$plugin->getSetting($journalId, 'password') || !$plugin->getSetting($journalId, 'automaticRegistration')) continue;
103 
104  $doiPrefix = null;
105  $pubIdPlugins = PluginRegistry::loadCategory('pubIds', true, $journalId);
106  if (isset($pubIdPlugins['doipubidplugin'])) {
107  $doiPubIdPlugin = $pubIdPlugins['doipubidplugin'];
108  if (!$doiPubIdPlugin->getSetting($journalId, 'enabled')) continue;
109  $doiPrefix = $doiPubIdPlugin->getSetting($journalId, 'doiPrefix');
110  }
111 
112  if ($doiPrefix) {
113  $journals[] = $journal;
114  } else {
115  $this->addExecutionLogEntry(__('plugins.importexport.common.senderTask.warning.noDOIprefix', array('path' => $journal->getPath())), SCHEDULED_TASK_MESSAGE_TYPE_WARNING);
116  }
117  }
118  return $journals;
119  }
120 
121 
129  function _registerObjects($objects, $filter, $journal, $objectsFileNamePart) {
130  $plugin = $this->_plugin;
131  import('lib.pkp.classes.file.FileManager');
132  $fileManager = new FileManager();
133  foreach ($objects as $object) {
134  // export XML
135  $exportXml = $plugin->exportXML($object, $filter, $journal);
136  // Write the XML to a file.
137  // export file name example: datacite-20160723-160036-articles-1-1.xml
138  $objectFileNamePart = $objectsFileNamePart . '-' . $object->getId();
139  $exportFileName = $plugin->getExportFileName($plugin->getExportPath(), $objectFileNamePart, $journal, '.xml');
140  $fileManager->writeFile($exportFileName, $exportXml);
141  // Deposit the XML file.
142  $result = $plugin->depositXML($object, $journal, $exportFileName);
143  if ($result !== true) {
144  $this->_addLogEntry($result);
145  }
146  // Remove all temporary files.
147  $fileManager->deleteByPath($exportFileName);
148  }
149  }
150 
155  function _addLogEntry($result) {
156  if (is_array($result)) {
157  foreach($result as $error) {
158  assert(is_array($error) && count($error) >= 1);
159  $this->addExecutionLogEntry(
160  __($error[0], array('param' => (isset($error[1]) ? $error[1] : null))),
161  SCHEDULED_TASK_MESSAGE_TYPE_WARNING
162  );
163  }
164  } else {
165  $this->addExecutionLogEntry(
166  __('plugins.importexport.common.register.error.mdsError', array('param' => ' - ')),
167  SCHEDULED_TASK_MESSAGE_TYPE_WARNING
168  );
169  }
170  }
171 
172 }
173 
Application\getContextDAO
static getContextDAO()
Definition: Application.inc.php:137
DataciteInfoSender
Scheduled task to send deposits to DataCite.
Definition: DataciteInfoSender.inc.php:19
DataciteInfoSender\__construct
__construct($args)
Definition: DataciteInfoSender.inc.php:30
PluginRegistry\loadCategory
static loadCategory($category, $enabledOnly=false, $mainContextId=null)
Definition: PluginRegistry.inc.php:103
ScheduledTask
Base class for executing scheduled tasks. All scheduled task classes must extend this class and imple...
Definition: ScheduledTask.inc.php:20
Seboettg\Collection\count
count()
Definition: ArrayListTrait.php:253
DataciteInfoSender\getName
getName()
Definition: DataciteInfoSender.inc.php:45
DataciteInfoSender\_getJournals
_getJournals()
Definition: DataciteInfoSender.inc.php:97
DataciteInfoSender\_addLogEntry
_addLogEntry($result)
Definition: DataciteInfoSender.inc.php:158
ScheduledTask\addExecutionLogEntry
addExecutionLogEntry($message, $type=null)
Definition: ScheduledTask.inc.php:111
PluginRegistry\getPlugin
static getPlugin($category, $name)
Definition: PluginRegistry.inc.php:85
FileManager
Class defining basic operations for file management.
Definition: FileManager.inc.php:35
DataciteInfoSender\_registerObjects
_registerObjects($objects, $filter, $journal, $objectsFileNamePart)
Definition: DataciteInfoSender.inc.php:132
DataciteInfoSender\executeActions
executeActions()
Definition: DataciteInfoSender.inc.php:52
DataciteInfoSender\$_plugin
$_plugin
Definition: DataciteInfoSender.inc.php:24