14 import(
'lib.pkp.classes.file.ContextFileManager');
15 import(
'lib.pkp.classes.scheduledTask.ScheduledTask');
34 public function __construct($deposit, $task =
null) {
35 $this->_deposit = $deposit;
48 $this->_task->addExecutionLogEntry($message, SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
60 return $fileManager->getBasePath() . PLN_PLUGIN_ARCHIVE_FOLDER . DIRECTORY_SEPARATOR . $this->_deposit->getUUID();
68 return $this->
getDepositDir() . DIRECTORY_SEPARATOR . $this->_deposit->getUUID() .
'.xml';
76 return $this->
getDepositDir() . DIRECTORY_SEPARATOR . $this->_deposit->getUUID() .
'.zip';
90 protected function _generateElement($dom, $elementName, $content, $namespace =
null) {
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);
99 $node = $dom->createCDATASection($filtered);
100 $element = $dom->createElementNS($namespace, $elementName);
101 $element->appendChild($node);
112 $journal = $journalDao->getById($this->_deposit->getJournalId());
120 if (!$fileManager->fileExists($packageFile)) {
121 $this->
_logMessage(__(
'plugins.generic.pln.error.depositor.missingpackage', array(
'file' => $packageFile)));
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');
130 $entry->appendChild($this->
_generateElement($atom,
'email', $journal->getData(
'contactEmail')));
131 $entry->appendChild($this->
_generateElement($atom,
'title', $journal->getLocalizedName()));
137 $entry->appendChild($this->
_generateElement($atom,
'pkp:journal_url', $dispatcher->url($request, ROUTE_PAGE, $journal->getPath()),
'http://pkp.sfu.ca/SWORD'));
139 $entry->appendChild($this->
_generateElement($atom,
'pkp:publisherName', $journal->getData(
'publisherInstitution'),
'http://pkp.sfu.ca/SWORD'));
141 $entry->appendChild($this->
_generateElement($atom,
'pkp:publisherUrl', $journal->getData(
'publisherUrl'),
'http://pkp.sfu.ca/SWORD'));
144 if ($journal->getData(
'onlineIssn')) {
145 $issn = $journal->getData(
'onlineIssn');
146 }
else if ($journal->getData(
'printIssn')) {
147 $issn = $journal->getData(
'printIssn');
150 $entry->appendChild($this->
_generateElement($atom,
'pkp:issn', $issn,
'http://pkp.sfu.ca/SWORD'));
152 $entry->appendChild($this->
_generateElement($atom,
'id',
'urn:uuid:'.$this->_deposit->getUUID()));
154 $entry->appendChild($this->
_generateElement($atom,
'updated', strftime(
"%Y-%m-%d %H:%M:%S", strtotime($this->_deposit->getDateModified()))));
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));
162 $objectPublicationDate = 0;
164 switch ($this->_deposit->getObjectType()) {
165 case 'PublishedArticle':
166 case PLN_PLUGIN_DEPOSIT_OBJECT_SUBMISSION:
167 $depositObjects = $this->_deposit->getDepositObjects();
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);
177 case PLN_PLUGIN_DEPOSIT_OBJECT_ISSUE:
178 $depositObjects = $this->_deposit->getDepositObjects();
179 while ($depositObject = $depositObjects->next()) {
181 $issue = $issueDao->getById($depositObject->getObjectId());
182 $objectVolume = $issue->getVolume();
183 $objectIssue = $issue->getNumber();
184 if ($issue->getDatePublished() > $objectPublicationDate)
185 $objectPublicationDate = $issue->getDatePublished();
190 $pkpDetails->setAttribute(
'volume', $objectVolume);
191 $pkpDetails->setAttribute(
'issue', $objectIssue);
192 $pkpDetails->setAttribute(
'pubdate', strftime(
'%Y-%m-%d', strtotime($objectPublicationDate)));
196 $currentVersion = $versionDao->getCurrentVersion();
197 $pkpDetails->setAttribute(
'ojsVersion', $currentVersion->getVersionString());
199 switch ($plnPlugin->getSetting($journal->getId(),
'checksum_type')) {
201 $pkpDetails->setAttribute(
'checksumType',
'SHA-1');
202 $pkpDetails->setAttribute(
'checksumValue', sha1_file($packageFile));
205 $pkpDetails->setAttribute(
'checksumType',
'MD5');
206 $pkpDetails->setAttribute(
'checksumValue', md5_file($packageFile));
210 $entry->appendChild($pkpDetails);
211 $atom->appendChild($entry);
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'));
218 $mode = $atom->createElementNS(
'http://pkp.sfu.ca/SWORD',
'publishingMode');
219 switch($journal->getData(
'publishingMode')) {
220 case PUBLISHING_MODE_OPEN:
221 $mode->nodeValue =
'Open';
223 case PUBLISHING_MODE_SUBSCRIPTION:
224 $mode->nodeValue =
'Subscription';
226 case PUBLISHING_MODE_NONE:
227 $mode->nodeValue =
'None';
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'));
235 $entry->appendChild($license);
236 $atom->save($atomFile);
248 if (!@include_once(dirname(__FILE__).
'/../vendor/scholarslab/bagit/lib/bagit.php')) {
249 $this->
_logMessage(__(
'plugins.generic.pln.error.include.bagit'));
260 $supportsOptions = in_array(
'parseOpts', get_class_methods($exportPlugin));
261 @ini_set(
'memory_limit', -1);
264 $journal = $journalDao->getById($this->_deposit->getJournalId());
265 $depositObjects = $this->_deposit->getDepositObjects();
268 $bagDir = $this->
getDepositDir() . DIRECTORY_SEPARATOR . $this->_deposit->getUUID();
270 $exportFile = tempnam(sys_get_temp_dir(),
'ojs-pln-export-');
271 $termsFile = tempnam(sys_get_temp_dir(),
'ojs-pln-terms-');
273 $bag =
new BagIt($bagDir);
275 import(
'lib.pkp.classes.file.FileManager');
278 switch ($this->_deposit->getObjectType()) {
279 case 'PublishedArticle':
280 case PLN_PLUGIN_DEPOSIT_OBJECT_SUBMISSION:
281 $submissionIds = array();
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;
290 $submissionIds[] = $submission->getId();
294 $exportXml = $exportPlugin->exportSubmissions($submissionIds, $journal,
null, [
'no-embed' => 1]);
296 $this->
_logMessage(__(
'plugins.generic.pln.error.depositor.export.articles.error'));
299 if ($supportsOptions) $exportXml = $this->
_cleanFileList($exportXml, $fileList);
300 $fileManager->writeFile($exportFile, $exportXml);
302 case PLN_PLUGIN_DEPOSIT_OBJECT_ISSUE:
306 $depositObject = $depositObjects->next();
307 $issue = $issueDao->getByBestId($depositObject->getObjectId(), $journal->getId());
312 $exportXml = $exportPlugin->exportIssues(
313 (array) $issue->getId(),
315 $user = $request->getUser(),
320 $this->
_logMessage(__(
'plugins.generic.pln.error.depositor.export.issue.error'));
321 $this->
importExportErrorHandler($depositObject->getDepositId(), __(
'plugins.generic.pln.error.depositor.export.issue.error'));
324 catch (Exception $exception) {
325 $this->
_logMessage(__(
'plugins.generic.pln.error.depositor.export.issue.exception') . $exception->getMessage());
329 $callback->unregister();
330 if ($supportsOptions) $exportXml = $this->
_cleanFileList($exportXml, $fileList);
331 $fileManager->writeFile($exportFile, $exportXml);
333 default:
throw new Exception(
'Unknown deposit type!');
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);
342 $terms = unserialize($plnPlugin->getSetting($this->_deposit->getJournalId(),
'terms_of_use'));
343 $agreement = unserialize($plnPlugin->getSetting($this->_deposit->getJournalId(),
'terms_of_use_agreement'));
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);
353 $entry->appendChild($pkpTermsOfUse);
354 $termsXml->appendChild($entry);
355 $termsXml->save($termsFile);
358 $bag->addFile($exportFile, $this->_deposit->getObjectType() . $this->_deposit->getUUID() .
'.xml');
359 foreach ($fileList as $sourcePath => $targetPath) {
360 $bag->addFile($sourcePath, $targetPath);
366 '/schemaLocation="[^"]+pkp-native.xsd"/',
367 'schemaLocation="pkp-native.xsd"',
368 file_get_contents(
'plugins/importexport/native/native.xsd')
374 '/schemaLocation="[^"]+importexport.xsd"/',
375 'schemaLocation="importexport.xsd"',
376 file_get_contents(
'lib/pkp/plugins/importexport/native/pkp-native.xsd')
380 $bag->createFile(file_get_contents(
'lib/pkp/xml/importexport.xsd'),
'importexport.xsd');
383 $bag->addFile($termsFile,
'terms' . $this->_deposit->getUUID() .
'.xml');
387 $currentVersion = $versionDao->getCurrentVersion();
388 $bag->setBagInfoData(
'PKP-PLN-OJS-Version', $currentVersion->getVersionString());
393 $bag->package($packageFile,
'zip');
396 $fileManager->rmtree($bagDir);
397 $fileManager->deleteByPath($exportFile);
398 $fileManager->deleteByPath($termsFile);
410 $doc =
new DOMDocument();
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);
420 return $doc->saveXML();
427 $journalId = $this->_deposit->getJournalId();
432 $plnDir = $fileManager->getBasePath() . PLN_PLUGIN_ARCHIVE_FOLDER;
435 $url = $plnPlugin->getSetting($journalId,
'pln_network');
438 if ($this->_deposit->getLockssAgreementStatus()) {
439 $url .= PLN_PLUGIN_CONT_IRI .
'/' . $plnPlugin->getSetting($journalId,
'journal_uuid');
440 $url .=
'/' . $this->_deposit->getUUID() .
'/edit';
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(),
448 'atomPath' => $atomPath,
449 'method' =>
'PutFile')),
450 SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
452 $result = $plnPlugin->curlPutFile(
457 $url .= PLN_PLUGIN_COL_IRI .
'/' . $plnPlugin->getSetting($journalId,
'journal_uuid');
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(),
465 'atomPath' => $atomPath,
466 'method' =>
'PostFile')),
467 SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
469 $result = $plnPlugin->curlPostFile(
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);
481 $this->_deposit->setTransferredStatus();
483 $this->_deposit->setLockssReceivedStatus(
false);
485 $this->_deposit->setLockssAgreementStatus(
false);
486 $this->_deposit->setLastStatusDate(time());
487 $depositDao->updateObject($this->_deposit);
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);
497 $this->
_logMessage(__(
'plugins.generic.pln.error.network.deposit', array(
'error' => $result[
'error'])));
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);
505 $this->
_logMessage(__(
'plugins.generic.pln.error.http.deposit', array(
'error' => $result[
'status'])));
508 $this->_deposit->setLockssReceivedStatus();
509 $this->_deposit->setLastStatusDate(time());
510 $depositDao->updateObject($this->_deposit);
521 $plnDir = $fileManager->getBasePath() . PLN_PLUGIN_ARCHIVE_FOLDER;
524 if (!$fileManager->fileExists($plnDir,
'dir')) {
525 $fileManager->mkdir($plnDir);
529 $depositDir = $plnDir . DIRECTORY_SEPARATOR . $this->_deposit->getUUID();
530 if ($fileManager->fileExists($depositDir,
'dir')) {
531 $fileManager->rmtree($depositDir);
534 $fileManager->mkdir($depositDir);
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);
546 $this->_deposit->setPackagedStatus(
false);
547 $this->_deposit->setLastStatusDate(time());
548 $depositDao->updateObject($this->_deposit);
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);
557 $this->_deposit->setPackagedStatus(
false);
558 $this->_deposit->setLastStatusDate(time());
559 $depositDao->updateObject($this->_deposit);
563 $this->_task->addExecutionLogEntry(__(
'plugins.generic.pln.depositor.packagingdeposits.processing.packageSucceeded',
564 array(
'depositId' => $this->_deposit->getId())),
565 SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
568 $this->_deposit->setPackagedStatus();
569 $this->_deposit->setLastStatusDate(time());
570 $depositDao->updateObject($this->_deposit);
577 $journalId = $this->_deposit->getJournalId();
581 $url = $plnPlugin->getSetting($journalId,
'pln_network') . PLN_PLUGIN_CONT_IRI;
582 $url .=
'/' . $plnPlugin->getSetting($journalId,
'journal_uuid');
583 $url .=
'/' . $this->_deposit->getUUID() .
'/state';
586 $result = $plnPlugin->curlGet($url);
588 if ($result[
'status'] != PLN_PLUGIN_HTTP_STATUS_OK) {
590 if($result[
'status'] === FALSE) {
591 error_log(__(
'plugins.generic.pln.error.network.swordstatement', array(
'error' => $result[
'error'])));
593 error_log(__(
'plugins.generic.pln.error.http.swordstatement', array(
'error' => $result[
'status'])));
599 $contentDOM =
new DOMDocument();
600 $contentDOM->preserveWhiteSpace =
false;
601 $contentDOM->loadXML($result[
'result']);
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);
610 switch ($processingState) {
611 case 'depositedByJournal':
612 $this->_deposit->setTransferredStatus(
true);
615 case 'xml-validated':
616 case 'payload-validated':
617 case 'virus-checked':
618 $this->_deposit->setReceivedStatus(
true);
620 case 'bag-validated':
623 $this->_deposit->setValidatedStatus(
true);
626 $this->_deposit->setSentStatus(
true);
629 $this->
_logMessage(
'Deposit ' . $this->_deposit->getId() .
' has unknown processing state ' . $processingState);
632 $lockssState = $contentDOM->getElementsByTagName(
'category')->item(1)->getAttribute(
'term');
633 switch($lockssState) {
638 $this->_deposit->setLockssReceivedStatus();
641 $this->_deposit->setLockssSyncingStatus();
644 if(!$this->_deposit->getLockssAgreementStatus()) {
648 $fileManager->rmtree($depositDir);
650 $this->_deposit->setLockssAgreementStatus(
true);
653 $this->
_logMessage(
'Deposit ' . $this->_deposit->getId() .
' has unknown LOCKSS state ' . $processingState);
656 $this->_deposit->setLastStatusDate(time());
657 $depositDao->updateObject($this->_deposit);
666 $this->_depositPackageErrored =
true;
669 $deposit = $depositDao->getById($depositId);
671 $deposit->setExportDepositError($message);
672 $deposit->setPackagingFailedStatus();
673 $depositDao->updateObject($deposit);
684 private $_depositPackage;
685 private $_isUnregistered =
false;
687 public function __construct($depositId, $depositPackage) {
688 $this->_depositId = $depositId;
689 $this->_depositPackage = $depositPackage;
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);
699 $this->_depositPackage->_task->addExecutionLogEntry(__(
'plugins.generic.pln.depositor.packagingdeposits.processing.error', array(
'depositId' => $this->_depositId)), SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
709 $this->_isUnregistered =
true;