-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathK10PlusInfoSender.inc.php
147 lines (122 loc) · 4.41 KB
/
K10PlusInfoSender.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/**
* @file plugins/importexport/k10Plus/K10PlusInfoSender.inc.php
* @brief Scheduled task to send deposits to the BSZ.
*/
import('lib.pkp.classes.scheduledTask.ScheduledTask');
define('EXPORT_STATUS_NOT_DEPOSITED', 'notDeposited');
define('EXPORT_STATUS_MARKEDUNREGISTERED', 'markedUnregistered');
define('EXPORT_STATUS_FAILED', 'failed');
class K10PlusInfoSender extends ScheduledTask
{
/** @var $_plugin K10PlusExportPlugin */
var $_plugin;
/**
* Constructor.
* @param $argv array task arguments
*/
function __construct($args)
{
PluginRegistry::loadCategory('importexport');
$plugin = PluginRegistry::getPlugin('importexport', 'K10PlusExportPlugin'); /* @var $plugin K10PlusExportPlugin */
$this->_plugin = $plugin;
if (is_a($plugin, 'K10PlusExportPlugin')) {
$plugin->addLocaleData();
}
parent::__construct($args);
}
/**
* @copydoc ScheduledTask::getName()
*/
function getName()
{
return __('plugins.importexport.k10Plus.senderTask.name');
}
/**
* @copydoc ScheduledTask::executeActions()
*/
function executeActions()
{
if (!$this->_plugin) return false;
// Get plugin and journals.
$plugin = $this->_plugin;
$journals = $this->_getJournals();
// Iterate through journals.
foreach ($journals as $journal) {
// Get unregistered articles.
$unregisteredArticles = $plugin->getUnregisteredArticles($journal);
// Check if there are articles to be deposited.
if (count($unregisteredArticles) != '0') {
$this->_registerObjects($unregisteredArticles, 'article=>k10Plus-xml', $journal, 'articles');
}
}
return true;
}
/**
* Get all journals that meet the requirements to have
* their articles automatically sent to the BSZ.
* @return array
*/
function _getJournals()
{
// Fetch necessary vars.
$plugin = $this->_plugin;
$contextDao = Application::getContextDAO(); /* @var $contextDao JournalDAO */
$journalFactory = $contextDao->getAll(true);
$journals = array();
while ($journal = $journalFactory->next()) {
$journalId = $journal->getId();
// Check if settings are complete.
if (
!$plugin->getSetting($journalId, 'username') ||
!$plugin->getSetting($journalId, 'password') ||
!$plugin->getSetting($journalId, 'serverAddress') ||
!$plugin->getSetting($journalId, 'folderId') ||
!$plugin->getSetting($journalId, 'automaticRegistration')
) continue;
$journals[] = $journal;
}
return $journals;
}
/**
* Register objects.
* @param $objects array
* @param $filter string
* @param $journal Journal
* @param $objectsFileNamePart string
*/
function _registerObjects($objects, $filter, $journal, $objectsFileNamePart)
{
// Fetch plugin and file manager.
$plugin = $this->_plugin;
import('lib.pkp.classes.file.FileManager');
$fileManager = new FileManager();
// Do not validate XML to skip errors during automatic registration.
$noValidation = 1;
// Iterate over articles, collect exportable articles.
foreach ($objects as $object) {
// Journal name for logging.
$journalName = $journal->getLocalizedName();
// Log submission.
$this->addExecutionLogEntry("Depositing: [Submission ID: " . $object->getData('id') . "]" . "[Journal: " . $journalName . "]", SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
// Try constructing and depositing XML.
try {
$exportXml = $plugin->exportXML(array($object), $filter, $journal, $noValidation);
// Write to file and collect in array.
$objectsFileNamePartId = $objectsFileNamePart . '-' . $object->getId();
$exportFileName = $plugin->getExportFileName($plugin->getExportPath(), $objectsFileNamePartId, $journal, '.xml');
$fileManager->writeFile($exportFileName, $exportXml);
$result = $plugin->depositXML($object, $journal, $exportFileName);
$fileManager->deleteByPath($exportFileName);
$plugin->updateDepositStatus($object, EXPORT_STATUS_REGISTERED, '');
$this->addExecutionLogEntry("Success depositing: [Submission ID: " . $object->getData('id') . "]" . "[Journal: " . $journalName . "]", SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
// Keep track of errors.
} catch (ErrorException $e) {
$error = [$e->getMessage()];
$plugin->updateDepositStatus($object, EXPORT_STATUS_FAILED, $error[0]);
$this->addExecutionLogEntry("Error depositing: [Submission ID: " . $object->getData('id') . "]" . "[Journal: " . $journalName . "]" . "[Error message: " . $error[0]. "]", SCHEDULED_TASK_MESSAGE_TYPE_WARNING);
}
}
return true;
}
}