Open Journal Systems  3.3.0
CrossrefInfoSender.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', 'CrossRefExportPlugin'); /* @var $plugin CrossRefExportPlugin */
30  $this->_plugin = $plugin;
31 
32  if (is_a($plugin, 'CrossRefExportPlugin')) {
33  $plugin->addLocaleData();
34  }
35 
36  parent::__construct($args);
37  }
38 
42  function getName() {
43  return __('plugins.importexport.crossref.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  $notify = false;
57 
58  $pubIdPlugins = PluginRegistry::loadCategory('pubIds', true, $journal->getId());
59  $doiPubIdPlugin = $pubIdPlugins['doipubidplugin'];
60 
61  if ($doiPubIdPlugin->getSetting($journal->getId(), 'enablePublicationDoi')) {
62  // Get unregistered articles
63  $unregisteredArticles = $plugin->getUnregisteredArticles($journal);
64  // If there are articles to be deposited
65  if (count($unregisteredArticles)) {
66  $this->_registerObjects($unregisteredArticles, 'article=>crossref-xml', $journal, 'articles');
67  }
68  }
69  }
70  return true;
71  }
72 
78  function _getJournals() {
79  $plugin = $this->_plugin;
80  $contextDao = Application::getContextDAO(); /* @var $contextDao JournalDAO */
81  $journalFactory = $contextDao->getAll(true);
82 
83  $journals = array();
84  while($journal = $journalFactory->next()) {
85  $journalId = $journal->getId();
86  if (!$plugin->getSetting($journalId, 'username') || !$plugin->getSetting($journalId, 'password') || !$plugin->getSetting($journalId, 'automaticRegistration')) continue;
87 
88  $doiPrefix = null;
89  $pubIdPlugins = PluginRegistry::loadCategory('pubIds', true, $journalId);
90  if (isset($pubIdPlugins['doipubidplugin'])) {
91  $doiPubIdPlugin = $pubIdPlugins['doipubidplugin'];
92  if (!$doiPubIdPlugin->getSetting($journalId, 'enabled')) continue;
93  $doiPrefix = $doiPubIdPlugin->getSetting($journalId, 'doiPrefix');
94  }
95 
96  if ($doiPrefix) {
97  $journals[] = $journal;
98  } else {
99  $this->addExecutionLogEntry(__('plugins.importexport.common.senderTask.warning.noDOIprefix', array('path' => $journal->getPath())), SCHEDULED_TASK_MESSAGE_TYPE_WARNING);
100  }
101  }
102  return $journals;
103  }
104 
113  function _registerObjects($objects, $filter, $journal, $objectsFileNamePart) {
114  $plugin = $this->_plugin;
115  import('lib.pkp.classes.file.FileManager');
116  $fileManager = new FileManager();
117  // The new Crossref deposit API expects one request per object.
118  // On contrary the export supports bulk/batch object export, thus
119  // also the filter expects an array of objects.
120  // Thus the foreach loop, but every object will be in an one item array for
121  // the export and filter to work.
122  foreach ($objects as $object) {
123  // export XML
124  $exportXml = $plugin->exportXML(array($object), $filter, $journal);
125  // Write the XML to a file.
126  // export file name example: crossref-20160723-160036-articles-1-1.xml
127  $objectsFileNamePartId = $objectsFileNamePart . '-' . $object->getId();
128  $exportFileName = $plugin->getExportFileName($plugin->getExportPath(), $objectsFileNamePartId, $journal, '.xml');
129  $fileManager->writeFile($exportFileName, $exportXml);
130  // Deposit the XML file.
131  $result = $plugin->depositXML($object, $journal, $exportFileName);
132  if ($result !== true) {
133  $this->_addLogEntry($result);
134  }
135  // Remove all temporary files.
136  $fileManager->deleteByPath($exportFileName);
137  }
138  }
139 
144  function _addLogEntry($result) {
145  if (is_array($result)) {
146  foreach($result as $error) {
147  assert(is_array($error) && count($error) >= 1);
148  $this->addExecutionLogEntry(
149  __($error[0], array('param' => (isset($error[1]) ? $error[1] : null))),
150  SCHEDULED_TASK_MESSAGE_TYPE_WARNING
151  );
152  }
153  }
154  }
155 
156 }
157 
CrossrefInfoSender\$_plugin
$_plugin
Definition: CrossrefInfoSender.inc.php:24
Application\getContextDAO
static getContextDAO()
Definition: Application.inc.php:137
CrossrefInfoSender\_addLogEntry
_addLogEntry($result)
Definition: CrossrefInfoSender.inc.php:147
CrossrefInfoSender\getName
getName()
Definition: CrossrefInfoSender.inc.php:45
CrossrefInfoSender\_getJournals
_getJournals()
Definition: CrossrefInfoSender.inc.php:81
CrossrefInfoSender\_registerObjects
_registerObjects($objects, $filter, $journal, $objectsFileNamePart)
Definition: CrossrefInfoSender.inc.php:116
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
CrossrefInfoSender
Scheduled task to send deposits to Crossref and update statuses.
Definition: CrossrefInfoSender.inc.php:19
ScheduledTask\addExecutionLogEntry
addExecutionLogEntry($message, $type=null)
Definition: ScheduledTask.inc.php:111
CrossrefInfoSender\executeActions
executeActions()
Definition: CrossrefInfoSender.inc.php:52
PluginRegistry\getPlugin
static getPlugin($category, $name)
Definition: PluginRegistry.inc.php:85
FileManager
Class defining basic operations for file management.
Definition: FileManager.inc.php:35
CrossrefInfoSender\__construct
__construct($args)
Definition: CrossrefInfoSender.inc.php:30