Open Journal Systems  3.3.0
Depositor.inc.php
1 <?php
2 
14 import('lib.pkp.classes.file.ContextFileManager');
15 import('lib.pkp.classes.scheduledTask.ScheduledTask');
16 
17 class Depositor extends ScheduledTask {
18 
20  var $_plugin;
21 
26  public function __construct($args) {
28  $this->_plugin = PluginRegistry::getPlugin('generic', PLN_PLUGIN_NAME);
29  parent::__construct($args);
30  }
31 
35  public function getName() {
36  return __('plugins.generic.pln.depositorTask.name');
37  }
38 
42  public function executeActions() {
43  if (!$this->_plugin) return false;
44 
45  $this->addExecutionLogEntry('PLN Depositor executeActions started', SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
46 
47  $journalDao = DAORegistry::getDAO('JournalDAO');
48 
49  // Get all journals
50  $journals = $journalDao->getAll(true);
51 
52  // For all journals
53  while ($journal = $journals->next()) {
54 
55  // if the plugin isn't enabled for this journal, skip it
56  if (!$this->_plugin->getSetting($journal->getId(), 'enabled'))
57  continue;
58 
59  $this->_plugin->registerDAOs();
60  $this->_plugin->import('classes.Deposit');
61  $this->_plugin->import('classes.DepositObject');
62  $this->_plugin->import('classes.DepositPackage');
63 
64  $this->addExecutionLogEntry(__('plugins.generic.pln.notifications.processing_for', array('title' => $journal->getLocalizedName())), SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
65 
66  // check to make sure zip is installed
67  if (!$this->_plugin->zipInstalled()) {
68  $this->addExecutionLogEntry(__('plugins.generic.pln.notifications.zip_missing'), SCHEDULED_TASK_MESSAGE_TYPE_WARNING);
69  $this->_plugin->createJournalManagerNotification($journal->getId(), PLN_PLUGIN_NOTIFICATION_TYPE_ZIP_MISSING);
70  continue;
71  }
72 
73  if (!$this->_plugin->tarInstalled()) {
74  $this->addExecutionLogEntry(__('plugins.generic.pln.notifications.tar_missing'), SCHEDULED_TASK_MESSAGE_TYPE_WARNING);
75  $this->_plugin->createJournalManagerNotification($journal->getId(), PLN_PLUGIN_NOTIFICATION_TYPE_TAR_MISSING);
76  continue;
77  }
78 
79  $this->addExecutionLogEntry(__('plugins.generic.pln.notifications.getting_servicedocument'), SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
80  // get the sword service document
81  $sdResult = $this->_plugin->getServiceDocument($journal->getId());
82 
83  // if for some reason we didn't get a valid reponse, skip this journal
84  if ($sdResult != PLN_PLUGIN_HTTP_STATUS_OK) {
85  $this->addExecutionLogEntry(__('plugins.generic.pln.notifications.http_error'), SCHEDULED_TASK_MESSAGE_TYPE_WARNING);
86  $this->_plugin->createJournalManagerNotification($journal->getId(), PLN_PLUGIN_NOTIFICATION_TYPE_HTTP_ERROR);
87  continue;
88  }
89 
90  // TODO: DEFSTAT - REMOVE COMMENTS HERE
91  // if the pln isn't accepting deposits, skip this journal
92  //if (!$this->_plugin->getSetting($journal->getId(), 'pln_accepting')) {
93  // $this->addExecutionLogEntry(__('plugins.generic.pln.notifications.pln_not_accepting'), SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
94  // continue;
95  //}
96 
97  // if the terms haven't been agreed to, skip transfer
98  if (!$this->_plugin->termsAgreed($journal->getId())) {
99  $this->addExecutionLogEntry(__('plugins.generic.pln.notifications.terms_updated'), SCHEDULED_TASK_MESSAGE_TYPE_WARNING);
100  $this->_plugin->createJournalManagerNotification($journal->getId(), PLN_PLUGIN_NOTIFICATION_TYPE_TERMS_UPDATED);
101  continue;
102  }
103 
104  // it's necessary that the journal have an issn set
105  if (!$journal->getSetting('onlineIssn') &&
106  !$journal->getSetting('printIssn') &&
107  !$journal->getSetting('issn')) {
108  $this->addExecutionLogEntry(__('plugins.generic.pln.notifications.issn_missing'), SCHEDULED_TASK_MESSAGE_TYPE_WARNING);
109  $this->_plugin->createJournalManagerNotification($journal->getId(), PLN_PLUGIN_NOTIFICATION_TYPE_ISSN_MISSING);
110  continue;
111  }
112 
113  // update the statuses of existing deposits
114  $this->addExecutionLogEntry(__("plugins.generic.pln.depositor.statusupdates"), SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
115  $this->_processStatusUpdates($journal);
116 
117  // flag any deposits that have been updated and need to be rebuilt
118  $this->addExecutionLogEntry(__("plugins.generic.pln.depositor.updatedcontent"), SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
119  $this->_processHavingUpdatedContent($journal);
120 
121  // create new deposits for new deposit objects
122  $this->addExecutionLogEntry(__("plugins.generic.pln.depositor.newcontent"), SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
123  $this->_processNewDepositObjects($journal);
124 
125  // package any deposits that need packaging
126  $this->addExecutionLogEntry(__("plugins.generic.pln.depositor.packagingdeposits"), SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
127  $this->_processNeedPackaging($journal);
128 
129  // transfer the deposit atom documents
130  $this->addExecutionLogEntry(__("plugins.generic.pln.depositor.transferringdeposits"), SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
131  $this->_processNeedTransferring($journal);
132  }
133 
134  return true;
135  }
136 
141  protected function _processStatusUpdates($journal) {
142  // get deposits that need status updates
143  $depositDao = DAORegistry::getDAO('DepositDAO');
144  $depositQueue = $depositDao->getNeedStagingStatusUpdate($journal->getId());
145 
146  while ($deposit = $depositQueue->next()) {
147  $this->addExecutionLogEntry(__('plugins.generic.pln.depositor.statusupdates.processing',
148  array('depositId' => $deposit->getId(),
149  'statusLocal' => $deposit->getLocalStatus(),
150  'statusProcessing' => $deposit->getProcessingStatus(),
151  'statusLockss' => $deposit->getLockssStatus(),
152  'objectId' => $deposit->getObjectId(),
153  'objectType' => $deposit->getObjectType())),
154  SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
155 
156  $depositPackage = new DepositPackage($deposit, $this);
157  $depositPackage->updateDepositStatus();
158  }
159  }
160 
166  protected function _processHavingUpdatedContent(&$journal) {
167  // get deposits that have updated content
168  $depositObjectDao = DAORegistry::getDAO('DepositObjectDAO');
169  $depositObjectDao->markHavingUpdatedContent($journal->getId(), $this->_plugin->getSetting($journal->getId(), 'object_type'));
170  }
171 
176  protected function _processNeedTransferring($journal) {
177  // fetch the deposits we need to send to the pln
178  $depositDao = DAORegistry::getDAO('DepositDAO');
179  $depositQueue = $depositDao->getNeedTransferring($journal->getId());
180 
181  while ($deposit = $depositQueue->next()) {
182  $this->addExecutionLogEntry(__('plugins.generic.pln.depositor.transferringdeposits.processing',
183  array('depositId' => $deposit->getId(),
184  'statusLocal' => $deposit->getLocalStatus(),
185  'statusProcessing' => $deposit->getProcessingStatus(),
186  'statusLockss' => $deposit->getLockssStatus(),
187  'objectId' => $deposit->getObjectId(),
188  'objectType' => $deposit->getObjectType())),
189  SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
190 
191  $depositPackage = new DepositPackage($deposit, $this);
192  $depositPackage->transferDeposit();
193  unset($deposit);
194  }
195  }
196 
201  protected function _processNeedPackaging($journal) {
202  $depositDao = DAORegistry::getDAO('DepositDAO');
203  $depositQueue = $depositDao->getNeedPackaging($journal->getId());
204  $fileManager = new ContextFileManager($journal->getId());
205  $plnDir = $fileManager->getBasePath() . PLN_PLUGIN_ARCHIVE_FOLDER;
206 
207  // make sure the pln work directory exists
208  // TOOD: use FileManager calls instead of PHP ones where possible
209  if ($fileManager->fileExists($plnDir, 'dir') !== true) {
210  $fileManager->mkdirtree($plnDir);
211  }
212 
213  // loop though all of the deposits that need packaging
214  while ($deposit = $depositQueue->next()) {
215  $this->addExecutionLogEntry(__('plugins.generic.pln.depositor.packagingdeposits.processing',
216  array('depositId' => $deposit->getId(),
217  'statusLocal' => $deposit->getLocalStatus(),
218  'statusProcessing' => $deposit->getProcessingStatus(),
219  'statusLockss' => $deposit->getLockssStatus(),
220  'objectId' => $deposit->getObjectId(),
221  'objectType' => $deposit->getObjectType())),
222  SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
223 
224  $depositPackage = new DepositPackage($deposit, $this);
225  $depositPackage->packageDeposit();
226  }
227  }
228 
233  protected function _processNewDepositObjects($journal) {
234  // get the object type we'll be dealing with
235  $objectType = $this->_plugin->getSetting($journal->getId(), 'object_type');
236 
237  // create new deposit objects for any new OJS content
238  $depositDao = DAORegistry::getDAO('DepositDAO');
239  $depositObjectDao = DAORegistry::getDAO('DepositObjectDAO');
240  $depositObjectDao->createNew($journal->getId(), $objectType);
241 
242  // retrieve all deposit objects that don't belong to a deposit
243  $newObjects = $depositObjectDao->getNew($journal->getId(), $objectType);
244 
245  switch ($objectType) {
246  case 'PublishedArticle': // Legacy (OJS pre-3.2)
247  case PLN_PLUGIN_DEPOSIT_OBJECT_SUBMISSION:
248 
249  // get the new object threshold per deposit and split the objects into arrays of that size
250  $objectThreshold = $this->_plugin->getSetting($journal->getId(), 'object_threshold');
251  foreach (array_chunk($newObjects->toArray(),$objectThreshold) as $newObject_array) {
252 
253  // only create a deposit for the complete threshold, we'll worry about the remainder another day
254  if (count($newObject_array) == $objectThreshold) {
255 
256  //create a new deposit
257  $newDeposit = new Deposit($this->_plugin->newUUID());
258  $newDeposit->setJournalId($journal->getId());
259  $depositDao->insertObject($newDeposit);
260 
261  // add each object to the deposit
262  foreach ($newObject_array as $newObject) {
263  $newObject->setDepositId($newDeposit->getId());
264  $depositObjectDao->updateObject($newObject);
265  }
266  }
267  }
268  break;
269  case PLN_PLUGIN_DEPOSIT_OBJECT_ISSUE:
270  // create a new deposit for reach deposit object
271  while ($newObject = $newObjects->next()) {
272  $newDeposit = new Deposit($this->_plugin->newUUID());
273  $newDeposit->setJournalId($journal->getId());
274  $depositDao->insertObject($newDeposit);
275  $newObject->setDepositId($newDeposit->getId());
276  $depositObjectDao->updateObject($newObject);
277  unset($newObject);
278  }
279  break;
280  default: assert(false);
281  }
282  }
283 }
Depositor\_processHavingUpdatedContent
_processHavingUpdatedContent(&$journal)
Definition: Depositor.inc.php:169
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
Depositor\__construct
__construct($args)
Definition: Depositor.inc.php:29
Depositor
Definition: Depositor.inc.php:17
Depositor\_processNewDepositObjects
_processNewDepositObjects($journal)
Definition: Depositor.inc.php:236
Deposit
Container for deposit objects that are submitted to a PLN.
Definition: Deposit.inc.php:14
ContextFileManager
Class defining operations for private context file management.
Definition: ContextFileManager.inc.php:19
PluginRegistry\loadCategory
static loadCategory($category, $enabledOnly=false, $mainContextId=null)
Definition: PluginRegistry.inc.php:103
Depositor\getName
getName()
Definition: Depositor.inc.php:38
ScheduledTask
Base class for executing scheduled tasks. All scheduled task classes must extend this class and imple...
Definition: ScheduledTask.inc.php:20
Depositor\executeActions
executeActions()
Definition: Depositor.inc.php:45
DepositPackage
Represent a PLN deposit package.
Definition: DepositPackage.inc.php:17
ScheduledTask\addExecutionLogEntry
addExecutionLogEntry($message, $type=null)
Definition: ScheduledTask.inc.php:111
Depositor\_processNeedPackaging
_processNeedPackaging($journal)
Definition: Depositor.inc.php:204
PluginRegistry\getPlugin
static getPlugin($category, $name)
Definition: PluginRegistry.inc.php:85
Depositor\_processNeedTransferring
_processNeedTransferring($journal)
Definition: Depositor.inc.php:179
Depositor\$_plugin
$_plugin
Definition: Depositor.inc.php:23
Depositor\_processStatusUpdates
_processStatusUpdates($journal)
Definition: Depositor.inc.php:144