Open Journal Systems  3.3.0
DepositPackage.inc.php
1 <?php
2 
14 import('lib.pkp.classes.file.ContextFileManager');
15 import('lib.pkp.classes.scheduledTask.ScheduledTask');
16 
18 
20  var $_deposit;
21 
27  var $_task;
28 
34  public function __construct($deposit, $task = null) {
35  $this->_deposit = $deposit;
36  $this->_task = $task;
37  }
38 
46  protected function _logMessage($message) {
47  if($this->_task) {
48  $this->_task->addExecutionLogEntry($message, SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
49  } else {
50  error_log($message);
51  }
52  }
53 
58  public function getDepositDir() {
59  $fileManager = new ContextFileManager($this->_deposit->getJournalId());
60  return $fileManager->getBasePath() . PLN_PLUGIN_ARCHIVE_FOLDER . DIRECTORY_SEPARATOR . $this->_deposit->getUUID();
61  }
62 
67  public function getAtomDocumentPath() {
68  return $this->getDepositDir() . DIRECTORY_SEPARATOR . $this->_deposit->getUUID() . '.xml';
69  }
70 
75  public function getPackageFilePath() {
76  return $this->getDepositDir() . DIRECTORY_SEPARATOR . $this->_deposit->getUUID() . '.zip';
77  }
78 
90  protected function _generateElement($dom, $elementName, $content, $namespace = null) {
91  // remove any invalid UTF-8.
92  $original = mb_substitute_character();
93  mb_substitute_character(0xFFFD);
94  $filtered = mb_convert_encoding($content, 'UTF-8', 'UTF-8');
95  mb_substitute_character($original);
96 
97  // put the filtered content in a CDATA, as it may contain markup that
98  // isn't valid XML.
99  $node = $dom->createCDATASection($filtered);
100  $element = $dom->createElementNS($namespace, $elementName);
101  $element->appendChild($node);
102  return $element;
103  }
104 
109  public function generateAtomDocument() {
110  $plnPlugin = PluginRegistry::getPlugin('generic', PLN_PLUGIN_NAME);
111  $journalDao = DAORegistry::getDAO('JournalDAO');
112  $journal = $journalDao->getById($this->_deposit->getJournalId());
113  $fileManager = new ContextFileManager($this->_deposit->getJournalId());
114 
115  // set up folder and file locations
116  $atomFile = $this->getAtomDocumentPath();
117  $packageFile = $this->getPackageFilePath();
118 
119  // make sure our bag is present
120  if (!$fileManager->fileExists($packageFile)) {
121  $this->_logMessage(__('plugins.generic.pln.error.depositor.missingpackage', array('file' => $packageFile)));
122  return false;
123  }
124 
125  $atom = new DOMDocument('1.0', 'utf-8');
126  $entry = $atom->createElementNS('http://www.w3.org/2005/Atom', 'entry');
127  $entry->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:dcterms', 'http://purl.org/dc/terms/');
128  $entry->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:pkp', 'http://pkp.sfu.ca/SWORD');
129 
130  $entry->appendChild($this->_generateElement($atom, 'email', $journal->getData('contactEmail')));
131  $entry->appendChild($this->_generateElement($atom, 'title', $journal->getLocalizedName()));
132 
133  $request = PKPApplication::getRequest();
135  $dispatcher = $application->getDispatcher();
136 
137  $entry->appendChild($this->_generateElement($atom, 'pkp:journal_url', $dispatcher->url($request, ROUTE_PAGE, $journal->getPath()), 'http://pkp.sfu.ca/SWORD'));
138 
139  $entry->appendChild($this->_generateElement($atom, 'pkp:publisherName', $journal->getData('publisherInstitution'), 'http://pkp.sfu.ca/SWORD'));
140 
141  $entry->appendChild($this->_generateElement($atom, 'pkp:publisherUrl', $journal->getData('publisherUrl'), 'http://pkp.sfu.ca/SWORD'));
142 
143  $issn = '';
144  if ($journal->getData('onlineIssn')) {
145  $issn = $journal->getData('onlineIssn');
146  } else if ($journal->getData('printIssn')) {
147  $issn = $journal->getData('printIssn');
148  }
149 
150  $entry->appendChild($this->_generateElement($atom, 'pkp:issn', $issn, 'http://pkp.sfu.ca/SWORD'));
151 
152  $entry->appendChild($this->_generateElement($atom, 'id', 'urn:uuid:'.$this->_deposit->getUUID()));
153 
154  $entry->appendChild($this->_generateElement($atom, 'updated', strftime("%Y-%m-%d %H:%M:%S", strtotime($this->_deposit->getDateModified()))));
155 
156  $url = $dispatcher->url($request, ROUTE_PAGE, $journal->getPath()) . '/' . PLN_PLUGIN_ARCHIVE_FOLDER . '/deposits/' . $this->_deposit->getUUID();
157  $pkpDetails = $this->_generateElement($atom, 'pkp:content', $url, 'http://pkp.sfu.ca/SWORD');
158  $pkpDetails->setAttribute('size', ceil(filesize($packageFile)/1000));
159 
160  $objectVolume = '';
161  $objectIssue = '';
162  $objectPublicationDate = 0;
163 
164  switch ($this->_deposit->getObjectType()) {
165  case 'PublishedArticle': // Legacy (OJS pre-3.2)
166  case PLN_PLUGIN_DEPOSIT_OBJECT_SUBMISSION:
167  $depositObjects = $this->_deposit->getDepositObjects();
168  $submissionDao = DAORegistry::getDAO('SubmissionDAO');
169  while ($depositObject = $depositObjects->next()) {
170  $submission = $submissionDao->getById($depositObject->getObjectId());
171  $publication = $submission->getCurrentPublication();
172  $publicationDate = $publication?$publication->getData('publicationDate'):null;
173  if ($publicationDate && strtotime($publicationDate) > $objectPublicationDate)
174  $objectPublicationDate = strtotime($publicationDate);
175  }
176  break;
177  case PLN_PLUGIN_DEPOSIT_OBJECT_ISSUE:
178  $depositObjects = $this->_deposit->getDepositObjects();
179  while ($depositObject = $depositObjects->next()) {
180  $issueDao = DAORegistry::getDAO('IssueDAO');
181  $issue = $issueDao->getById($depositObject->getObjectId());
182  $objectVolume = $issue->getVolume();
183  $objectIssue = $issue->getNumber();
184  if ($issue->getDatePublished() > $objectPublicationDate)
185  $objectPublicationDate = $issue->getDatePublished();
186  }
187  break;
188  }
189 
190  $pkpDetails->setAttribute('volume', $objectVolume);
191  $pkpDetails->setAttribute('issue', $objectIssue);
192  $pkpDetails->setAttribute('pubdate', strftime('%Y-%m-%d', strtotime($objectPublicationDate)));
193 
194  // Add OJS Version
195  $versionDao = DAORegistry::getDAO('VersionDAO');
196  $currentVersion = $versionDao->getCurrentVersion();
197  $pkpDetails->setAttribute('ojsVersion', $currentVersion->getVersionString());
198 
199  switch ($plnPlugin->getSetting($journal->getId(), 'checksum_type')) {
200  case 'SHA-1':
201  $pkpDetails->setAttribute('checksumType', 'SHA-1');
202  $pkpDetails->setAttribute('checksumValue', sha1_file($packageFile));
203  break;
204  case 'MD5':
205  $pkpDetails->setAttribute('checksumType', 'MD5');
206  $pkpDetails->setAttribute('checksumValue', md5_file($packageFile));
207  break;
208  }
209 
210  $entry->appendChild($pkpDetails);
211  $atom->appendChild($entry);
212 
213  $locale = $journal->getPrimaryLocale();
214  $license = $atom->createElementNS('http://pkp.sfu.ca/SWORD', 'license');
215  $license->appendChild($this->_generateElement($atom, 'openAccessPolicy', $journal->getLocalizedSetting('openAccessPolicy', $locale), 'http://pkp.sfu.ca/SWORD'));
216  $license->appendChild($this->_generateElement($atom, 'licenseURL', $journal->getLocalizedSetting('licenseURL', $locale), 'http://pkp.sfu.ca/SWORD'));
217 
218  $mode = $atom->createElementNS('http://pkp.sfu.ca/SWORD', 'publishingMode');
219  switch($journal->getData('publishingMode')) {
220  case PUBLISHING_MODE_OPEN:
221  $mode->nodeValue = 'Open';
222  break;
223  case PUBLISHING_MODE_SUBSCRIPTION:
224  $mode->nodeValue = 'Subscription';
225  break;
226  case PUBLISHING_MODE_NONE:
227  $mode->nodeValue = 'None';
228  break;
229  }
230  $license->appendChild($mode);
231  $license->appendChild($this->_generateElement($atom, 'copyrightNotice', $journal->getLocalizedSetting('copyrightNotice', $locale), 'http://pkp.sfu.ca/SWORD'));
232  $license->appendChild($this->_generateElement($atom, 'copyrightBasis', $journal->getLocalizedSetting('copyrightBasis'), 'http://pkp.sfu.ca/SWORD'));
233  $license->appendChild($this->_generateElement($atom, 'copyrightHolder', $journal->getLocalizedSetting('copyrightHolder'), 'http://pkp.sfu.ca/SWORD'));
234 
235  $entry->appendChild($license);
236  $atom->save($atomFile);
237 
238  return $atomFile;
239  }
240 
247  public function generatePackage() {
248  if (!@include_once(dirname(__FILE__).'/../vendor/scholarslab/bagit/lib/bagit.php')) {
249  $this->_logMessage(__('plugins.generic.pln.error.include.bagit'));
250  return;
251  }
252 
253  // get DAOs, plugins and settings
254  $journalDao = DAORegistry::getDAO('JournalDAO');
255  $issueDao = DAORegistry::getDAO('IssueDAO');
256  $sectionDao = DAORegistry::getDAO('SectionDAO');
257  $submissionDao = DAORegistry::getDAO('SubmissionDAO');
258  PluginRegistry::loadCategory('importexport');
259  $exportPlugin = PluginRegistry::getPlugin('importexport', 'NativeImportExportPlugin');
260  $supportsOptions = in_array('parseOpts', get_class_methods($exportPlugin));
261  @ini_set('memory_limit', -1);
262  $plnPlugin = PluginRegistry::getPlugin('generic', PLN_PLUGIN_NAME);
263 
264  $journal = $journalDao->getById($this->_deposit->getJournalId());
265  $depositObjects = $this->_deposit->getDepositObjects();
266 
267  // set up folder and file locations
268  $bagDir = $this->getDepositDir() . DIRECTORY_SEPARATOR . $this->_deposit->getUUID();
269  $packageFile = $this->getPackageFilePath();
270  $exportFile = tempnam(sys_get_temp_dir(), 'ojs-pln-export-');
271  $termsFile = tempnam(sys_get_temp_dir(), 'ojs-pln-terms-');
272 
273  $bag = new BagIt($bagDir);
274  $fileList = array();
275  import('lib.pkp.classes.file.FileManager');
276  $fileManager = new FileManager();
277 
278  switch ($this->_deposit->getObjectType()) {
279  case 'PublishedArticle': // Legacy (OJS pre-3.2)
280  case PLN_PLUGIN_DEPOSIT_OBJECT_SUBMISSION:
281  $submissionIds = array();
282 
283  // we need to add all of the relevant submissions to an array to export as a batch
284  while ($depositObject = $depositObjects->next()) {
285  $submission = $submissionDao->getById($this->_deposit->getObjectId());
286  $currentPublication = $submission->getCurrentPublication();
287  if ($submission->getContextId() != $journal->getId()) continue;
288  if (!$currentPublication || $currentPublication->getStatus() != STATUS_PUBLISHED) continue;
289 
290  $submissionIds[] = $submission->getId();
291  }
292 
293  // export all of the submissions together
294  $exportXml = $exportPlugin->exportSubmissions($submissionIds, $journal, null, ['no-embed' => 1]);
295  if (!$exportXml) {
296  $this->_logMessage(__('plugins.generic.pln.error.depositor.export.articles.error'));
297  return false;
298  }
299  if ($supportsOptions) $exportXml = $this->_cleanFileList($exportXml, $fileList);
300  $fileManager->writeFile($exportFile, $exportXml);
301  break;
302  case PLN_PLUGIN_DEPOSIT_OBJECT_ISSUE:
303  // we only ever do one issue at a time, so get that issue
305  $request = $application->getRequest();
306  $depositObject = $depositObjects->next();
307  $issue = $issueDao->getByBestId($depositObject->getObjectId(), $journal->getId());
308 
309  $callback = new DepositUnregisterableErrorCallback($depositObject->getDepositId(), $this);
310 
311  try {
312  $exportXml = $exportPlugin->exportIssues(
313  (array) $issue->getId(),
314  $journal,
315  $user = $request->getUser(),
316  ['no-embed' => 1]
317  );
318 
319  if (!$exportXml) {
320  $this->_logMessage(__('plugins.generic.pln.error.depositor.export.issue.error'));
321  $this->importExportErrorHandler($depositObject->getDepositId(), __('plugins.generic.pln.error.depositor.export.issue.error'));
322  }
323  }
324  catch (Exception $exception) {
325  $this->_logMessage(__('plugins.generic.pln.error.depositor.export.issue.exception') . $exception->getMessage());
326  $this->importExportErrorHandler($depositObject->getDepositId(), $exception->getMessage());
327  }
328 
329  $callback->unregister();
330  if ($supportsOptions) $exportXml = $this->_cleanFileList($exportXml, $fileList);
331  $fileManager->writeFile($exportFile, $exportXml);
332  break;
333  default: throw new Exception('Unknown deposit type!');
334  }
335 
336  // add the current terms to the bag
337  $termsXml = new DOMDocument('1.0', 'utf-8');
338  $entry = $termsXml->createElementNS('http://www.w3.org/2005/Atom', 'entry');
339  $entry->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:dcterms', 'http://purl.org/dc/terms/');
340  $entry->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:pkp', PLN_PLUGIN_NAME);
341 
342  $terms = unserialize($plnPlugin->getSetting($this->_deposit->getJournalId(), 'terms_of_use'));
343  $agreement = unserialize($plnPlugin->getSetting($this->_deposit->getJournalId(), 'terms_of_use_agreement'));
344 
345  $pkpTermsOfUse = $termsXml->createElementNS(PLN_PLUGIN_NAME, 'pkp:terms_of_use');
346  foreach ($terms as $termName => $termData) {
347  $element = $termsXml->createElementNS(PLN_PLUGIN_NAME, $termName, $termData['term']);
348  $element->setAttribute('updated',$termData['updated']);
349  $element->setAttribute('agreed', $agreement[$termName]);
350  $pkpTermsOfUse->appendChild($element);
351  }
352 
353  $entry->appendChild($pkpTermsOfUse);
354  $termsXml->appendChild($entry);
355  $termsXml->save($termsFile);
356 
357  // add the exported content to the bag
358  $bag->addFile($exportFile, $this->_deposit->getObjectType() . $this->_deposit->getUUID() . '.xml');
359  foreach ($fileList as $sourcePath => $targetPath) {
360  $bag->addFile($sourcePath, $targetPath);
361  }
362 
363  // Add the schema files to the bag (adjusting the XSD references to flatten them)
364  $bag->createFile(
365  preg_replace(
366  '/schemaLocation="[^"]+pkp-native.xsd"/',
367  'schemaLocation="pkp-native.xsd"',
368  file_get_contents('plugins/importexport/native/native.xsd')
369  ),
370  'native.xsd'
371  );
372  $bag->createFile(
373  preg_replace(
374  '/schemaLocation="[^"]+importexport.xsd"/',
375  'schemaLocation="importexport.xsd"',
376  file_get_contents('lib/pkp/plugins/importexport/native/pkp-native.xsd')
377  ),
378  'pkp-native.xsd'
379  );
380  $bag->createFile(file_get_contents('lib/pkp/xml/importexport.xsd'), 'importexport.xsd');
381 
382  // add the exported content to the bag
383  $bag->addFile($termsFile, 'terms' . $this->_deposit->getUUID() . '.xml');
384 
385  // Add OJS Version
386  $versionDao = DAORegistry::getDAO('VersionDAO');
387  $currentVersion = $versionDao->getCurrentVersion();
388  $bag->setBagInfoData('PKP-PLN-OJS-Version', $currentVersion->getVersionString());
389 
390  $bag->update();
391 
392  // create the bag
393  $bag->package($packageFile, 'zip');
394 
395  // remove the temporary bag directory and temp files
396  $fileManager->rmtree($bagDir);
397  $fileManager->deleteByPath($exportFile);
398  $fileManager->deleteByPath($termsFile);
399 
400  return $packageFile;
401  }
402 
409  function _cleanFileList($xml, &$fileList) {
410  $doc = new DOMDocument();
411  $doc->loadXML($xml);
412  $xpath = new DOMXPath($doc);
413  $xpath->registerNameSpace('pkp', 'http://pkp.sfu.ca');
414  foreach($xpath->query('//pkp:submission_file//pkp:href') as $hrefNode ) {
415  $filePath = $hrefNode->getAttribute('src');
416  $targetPath = 'files/' . basename($filePath);
417  $fileList[$filePath] = $targetPath;
418  $hrefNode->setAttribute('src', $targetPath);
419  }
420  return $doc->saveXML();
421  }
422 
426  public function transferDeposit() {
427  $journalId = $this->_deposit->getJournalId();
428  $depositDao = DAORegistry::getDAO('DepositDAO');
429  $journalDao = DAORegistry::getDAO('JournalDAO');
430  $plnPlugin = PluginRegistry::getPlugin('generic',PLN_PLUGIN_NAME);
431  $fileManager = new ContextFileManager($journalId);
432  $plnDir = $fileManager->getBasePath() . PLN_PLUGIN_ARCHIVE_FOLDER;
433 
434  // post the atom document
435  $url = $plnPlugin->getSetting($journalId, 'pln_network');
436  $atomPath = $this->getAtomDocumentPath();
437 
438  if ($this->_deposit->getLockssAgreementStatus()) {
439  $url .= PLN_PLUGIN_CONT_IRI . '/' . $plnPlugin->getSetting($journalId, 'journal_uuid');
440  $url .= '/' . $this->_deposit->getUUID() . '/edit';
441 
442  $this->_task->addExecutionLogEntry(__('plugins.generic.pln.depositor.transferringdeposits.processing.postAtom',
443  array('depositId' => $this->_deposit->getId(),
444  'statusLocal' => $this->_deposit->getLocalStatus(),
445  'statusProcessing' => $this->_deposit->getProcessingStatus(),
446  'statusLockss' => $this->_deposit->getLockssStatus(),
447  'url' => $url,
448  'atomPath' => $atomPath,
449  'method' => 'PutFile')),
450  SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
451 
452  $result = $plnPlugin->curlPutFile(
453  $url,
454  $atomPath
455  );
456  } else {
457  $url .= PLN_PLUGIN_COL_IRI . '/' . $plnPlugin->getSetting($journalId, 'journal_uuid');
458 
459  $this->_task->addExecutionLogEntry(__('plugins.generic.pln.depositor.transferringdeposits.processing.postAtom',
460  array('depositId' => $this->_deposit->getId(),
461  'statusLocal' => $this->_deposit->getLocalStatus(),
462  'statusProcessing' => $this->_deposit->getProcessingStatus(),
463  'statusLockss' => $this->_deposit->getLockssStatus(),
464  'url' => $url,
465  'atomPath' => $atomPath,
466  'method' => 'PostFile')),
467  SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
468 
469  $result = $plnPlugin->curlPostFile(
470  $url,
471  $atomPath
472  );
473  }
474 
475  // if we get the OK, set the status as transferred
476  if (($result['status'] == PLN_PLUGIN_HTTP_STATUS_OK) || ($result['status'] == PLN_PLUGIN_HTTP_STATUS_CREATED)) {
477  $this->_task->addExecutionLogEntry(__('plugins.generic.pln.depositor.transferringdeposits.processing.resultSucceeded',
478  array('depositId' => $this->_deposit->getId())),
479  SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
480 
481  $this->_deposit->setTransferredStatus();
482  // unset a remote error if this worked
483  $this->_deposit->setLockssReceivedStatus(false);
484  // if this was an update, unset the update flag
485  $this->_deposit->setLockssAgreementStatus(false);
486  $this->_deposit->setLastStatusDate(time());
487  $depositDao->updateObject($this->_deposit);
488  } else {
489  // we got an error back from the staging server
490  if($result['status'] == FALSE) {
491  $this->_task->addExecutionLogEntry(__('plugins.generic.pln.depositor.transferringdeposits.processing.resultFailed',
492  array('depositId' => $this->_deposit->getId(),
493  'error' => $result['error'],
494  'result' => $result['result'])),
495  SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
496 
497  $this->_logMessage(__('plugins.generic.pln.error.network.deposit', array('error' => $result['error'])));
498  } else {
499  $this->_task->addExecutionLogEntry(__('plugins.generic.pln.depositor.transferringdeposits.processing.resultFailed',
500  array('depositId' => $this->_deposit->getId(),
501  'error' => $result['status'],
502  'result' => $result['result'])),
503  SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
504 
505  $this->_logMessage(__('plugins.generic.pln.error.http.deposit', array('error' => $result['status'])));
506  }
507 
508  $this->_deposit->setLockssReceivedStatus();
509  $this->_deposit->setLastStatusDate(time());
510  $depositDao->updateObject($this->_deposit);
511  }
512  }
513 
517  public function packageDeposit() {
518  $depositDao = DAORegistry::getDAO('DepositDAO');
519  $journalDao = DAORegistry::getDAO('JournalDAO');
520  $fileManager = new ContextFileManager($this->_deposit->getJournalId());
521  $plnDir = $fileManager->getBasePath() . PLN_PLUGIN_ARCHIVE_FOLDER;
522 
523  // make sure the pln work directory exists
524  if (!$fileManager->fileExists($plnDir, 'dir')) {
525  $fileManager->mkdir($plnDir);
526  }
527 
528  // make a location for our work and clear it out if it's there
529  $depositDir = $plnDir . DIRECTORY_SEPARATOR . $this->_deposit->getUUID();
530  if ($fileManager->fileExists($depositDir, 'dir')) {
531  $fileManager->rmtree($depositDir);
532  }
533 
534  $fileManager->mkdir($depositDir);
535 
536  $packagePath = $this->generatePackage();
537  if (!$packagePath) {
538  return;
539  }
540 
541  if (!$fileManager->fileExists($packagePath)) {
542  $this->_task->addExecutionLogEntry(__('plugins.generic.pln.depositor.packagingdeposits.processing.packageFailed',
543  array('depositId' => $this->_deposit->getId())),
544  SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
545 
546  $this->_deposit->setPackagedStatus(false);
547  $this->_deposit->setLastStatusDate(time());
548  $depositDao->updateObject($this->_deposit);
549  return;
550  }
551 
552  if (!$fileManager->fileExists($this->generateAtomDocument())) {
553  $this->_task->addExecutionLogEntry(__('plugins.generic.pln.depositor.packagingdeposits.processing.packageFailed',
554  array('depositId' => $this->_deposit->getId())),
555  SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
556 
557  $this->_deposit->setPackagedStatus(false);
558  $this->_deposit->setLastStatusDate(time());
559  $depositDao->updateObject($this->_deposit);
560  return;
561  }
562 
563  $this->_task->addExecutionLogEntry(__('plugins.generic.pln.depositor.packagingdeposits.processing.packageSucceeded',
564  array('depositId' => $this->_deposit->getId())),
565  SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
566 
567  // update the deposit's status
568  $this->_deposit->setPackagedStatus();
569  $this->_deposit->setLastStatusDate(time());
570  $depositDao->updateObject($this->_deposit);
571  }
572 
576  public function updateDepositStatus() {
577  $journalId = $this->_deposit->getJournalId();
578  $depositDao = DAORegistry::getDAO('DepositDAO');
579  $plnPlugin = PluginRegistry::getPlugin('generic', 'plnplugin');
580 
581  $url = $plnPlugin->getSetting($journalId, 'pln_network') . PLN_PLUGIN_CONT_IRI;
582  $url .= '/' . $plnPlugin->getSetting($journalId, 'journal_uuid');
583  $url .= '/' . $this->_deposit->getUUID() . '/state';
584 
585  // retrieve the content document
586  $result = $plnPlugin->curlGet($url);
587 
588  if ($result['status'] != PLN_PLUGIN_HTTP_STATUS_OK) {
589  // stop here if we didn't get an OK
590  if($result['status'] === FALSE) {
591  error_log(__('plugins.generic.pln.error.network.swordstatement', array('error' => $result['error'])));
592  } else {
593  error_log(__('plugins.generic.pln.error.http.swordstatement', array('error' => $result['status'])));
594  }
595 
596  return;
597  }
598 
599  $contentDOM = new DOMDocument();
600  $contentDOM->preserveWhiteSpace = false;
601  $contentDOM->loadXML($result['result']);
602 
603  // get the remote deposit state
604  $processingState = $contentDOM->getElementsByTagName('category')->item(0)->getAttribute('term');
605  $this->_task->addExecutionLogEntry(__('plugins.generic.pln.depositor.statusupdates.processing.processingState',
606  array('depositId' => $this->_deposit->getId(),
607  'processingState' => $processingState)),
608  SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
609 
610  switch ($processingState) {
611  case 'depositedByJournal':
612  $this->_deposit->setTransferredStatus(true);
613  break;
614  case 'harvested':
615  case 'xml-validated':
616  case 'payload-validated':
617  case 'virus-checked':
618  $this->_deposit->setReceivedStatus(true);
619  break;
620  case 'bag-validated':
621  case 'reserialized':
622  case 'hold':
623  $this->_deposit->setValidatedStatus(true);
624  break;
625  case 'deposited':
626  $this->_deposit->setSentStatus(true);
627  break;
628  default:
629  $this->_logMessage('Deposit ' . $this->_deposit->getId() . ' has unknown processing state ' . $processingState);
630  }
631 
632  $lockssState = $contentDOM->getElementsByTagName('category')->item(1)->getAttribute('term');
633  switch($lockssState) {
634  case '':
635  // do nothing.
636  break;
637  case 'received':
638  $this->_deposit->setLockssReceivedStatus();
639  break;
640  case 'syncing':
641  $this->_deposit->setLockssSyncingStatus();
642  break;
643  case 'agreement':
644  if(!$this->_deposit->getLockssAgreementStatus()) {
645  $journalDao = DAORegistry::getDAO('JournalDAO');
646  $fileManager = new ContextFileManager($this->_deposit->getJournalId());
647  $depositDir = $this->getDepositDir();
648  $fileManager->rmtree($depositDir);
649  }
650  $this->_deposit->setLockssAgreementStatus(true);
651  break;
652  default:
653  $this->_logMessage('Deposit ' . $this->_deposit->getId() . ' has unknown LOCKSS state ' . $processingState);
654  }
655 
656  $this->_deposit->setLastStatusDate(time());
657  $depositDao->updateObject($this->_deposit);
658  }
659 
665  public function importExportErrorHandler($depositId, $message) {
666  $this->_depositPackageErrored = true;
667 
668  $depositDao = DAORegistry::getDAO('DepositDAO');
669  $deposit = $depositDao->getById($depositId);
670  if ($deposit) {
671  $deposit->setExportDepositError($message);
672  $deposit->setPackagingFailedStatus();
673  $depositDao->updateObject($deposit);
674  }
675  }
676 }
677 
683  private $_depositId;
684  private $_depositPackage;
685  private $_isUnregistered = false;
686 
687  public function __construct($depositId, $depositPackage) {
688  $this->_depositId = $depositId;
689  $this->_depositPackage = $depositPackage;
690  }
691 
692  public function __destruct() {
693  if (!$this->_isUnregistered) {
694  $this->_depositPackage->importExportErrorHandler($this->_depositId, "Deposit Import/export error");
695  $taskDao = DAORegistry::getDao('ScheduledTaskDAO');
697  $taskDao->updateLastRunTime('plugins.generic.pln.classes.tasks.Depositor', 0);
698 
699  $this->_depositPackage->_task->addExecutionLogEntry(__('plugins.generic.pln.depositor.packagingdeposits.processing.error', array('depositId' => $this->_depositId)), SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
700 
701  $this->unregister();
702  }
703  }
704 
708  public function unregister() {
709  $this->_isUnregistered = true;
710  }
711 }
DepositPackage\generatePackage
generatePackage()
Definition: DepositPackage.inc.php:253
$application
$application
Definition: index.php:65
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
DepositPackage\updateDepositStatus
updateDepositStatus()
Definition: DepositPackage.inc.php:582
DepositPackage\_cleanFileList
_cleanFileList($xml, &$fileList)
Definition: DepositPackage.inc.php:415
DepositPackage\getAtomDocumentPath
getAtomDocumentPath()
Definition: DepositPackage.inc.php:73
DepositPackage\getDepositDir
getDepositDir()
Definition: DepositPackage.inc.php:64
DepositPackage\getPackageFilePath
getPackageFilePath()
Definition: DepositPackage.inc.php:81
DepositPackage\__construct
__construct($deposit, $task=null)
Definition: DepositPackage.inc.php:40
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
DepositPackage\_logMessage
_logMessage($message)
Definition: DepositPackage.inc.php:52
DepositPackage\$_deposit
$_deposit
Definition: DepositPackage.inc.php:23
DepositPackage\packageDeposit
packageDeposit()
Definition: DepositPackage.inc.php:523
DepositUnregisterableErrorCallback\unregister
unregister()
Definition: DepositPackage.inc.php:714
DepositPackage\generateAtomDocument
generateAtomDocument()
Definition: DepositPackage.inc.php:115
DepositPackage\importExportErrorHandler
importExportErrorHandler($depositId, $message)
Definition: DepositPackage.inc.php:671
DepositUnregisterableErrorCallback\__destruct
__destruct()
Definition: DepositPackage.inc.php:698
PKPApplication\getApplication
static getApplication()
Definition: PKPApplication.inc.php:227
DepositUnregisterableErrorCallback\__construct
__construct($depositId, $depositPackage)
Definition: DepositPackage.inc.php:693
DepositPackage
Represent a PLN deposit package.
Definition: DepositPackage.inc.php:17
DepositPackage\transferDeposit
transferDeposit()
Definition: DepositPackage.inc.php:432
PluginRegistry\getPlugin
static getPlugin($category, $name)
Definition: PluginRegistry.inc.php:85
DepositPackage\_generateElement
_generateElement($dom, $elementName, $content, $namespace=null)
Definition: DepositPackage.inc.php:96
FileManager
Class defining basic operations for file management.
Definition: FileManager.inc.php:35
PKPApplication\getRequest
getRequest()
Definition: PKPApplication.inc.php:270
DepositPackage\$_task
$_task
Definition: DepositPackage.inc.php:33
DepositUnregisterableErrorCallback
Definition: DepositPackage.inc.php:688
BagIt
Definition: bagit.php:69