-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDNBInfoSender.inc.php
180 lines (160 loc) · 5.41 KB
/
DNBInfoSender.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* @file plugins/importexport/dnb/DNBInfoSender.php
*
* Copyright (c) 2017 Center for Digital Systems (CeDiS), Freie Universität Berlin
* Distributed under the GNU GPL v2. For full terms see the plugin file LICENSE.
* Author: Bozana Bokan
* Last update: May 15, 2017
*
* @class DNBInfoSender
* @ingroup plugins_importexport_dnb
*
* @brief Scheduled task to send article information to the DNB server.
*/
import('lib.pkp.classes.scheduledTask.ScheduledTask');
class DNBInfoSender extends ScheduledTask {
/** @var $_plugin DNBExportPlugin */
var $_plugin;
/**
* Constructor.
* @param $argv array task arguments
*/
function __construct($args) {
PluginRegistry::loadCategory('importexport');
$plugin = PluginRegistry::getPlugin('importexport', 'DNBExportPlugin'); /* @var $plugin DNBExportPlugin */
$this->_plugin = $plugin;
if (is_a($plugin, 'DNBExportPlugin')) {
$plugin->addLocaleData();
}
parent::__construct($args);
}
/**
* @see ScheduledTask::getName()
*/
function getName() {
return __('plugins.importexport.dnb.senderTask.name');
}
/**
* @see ScheduledTask::executeActions()
*/
function executeActions() {
if (!$this->_plugin) return false;
$plugin = $this->_plugin;
// check if TAR command is configured
$checkForTarResult = $plugin->checkForTar();
if (is_array($checkForTarResult)) {
$this->addExecutionLogEntry(
__('plugins.importexport.dnb.noTAR'),
SCHEDULED_TASK_MESSAGE_TYPE_WARNING
);
return false;
}
$filter = $plugin->getSubmissionFilter();
$genreDao = DAORegistry::getDAO('GenreDAO');
$fileManager = new FileManager();
// get all journals that meet the requirements
$journals = $this->_getJournals();
$errors = array();
foreach ($journals as $journal) {
// Get not deposited articles
$notDepositedArticles = $plugin->getUnregisteredArticles($journal);
if (!empty($notDepositedArticles)) {
// Get the journal target export directory.
// The data will be exported in this structure:
// dnb/<journalId>-<dateTime>/
$result = $plugin->getExportPath($journal->getId());
if (is_array($result)) {
$this->addExecutionLogEntry(
__($result[0], array('param' => (isset($result[1]) ? $result[1] : null))),
SCHEDULED_TASK_MESSAGE_TYPE_WARNING
);
return false;
}
$journalExportPath = $result;
foreach ($notDepositedArticles as $submission) {
if (is_a($submission, 'Submission')) {
$issue = null;
$galleys = array();
// Get issue and galleys, and check if the article can be exported
if (!$plugin->canBeExported($submission, $issue, $galleys)) {
$errors[] = array('plugins.importexport.dnb.export.error.articleCannotBeExported', $submission->getId());
// continue with other articles
continue;
}
$fullyDeposited = true;
$submissionId = $submission->getId();
foreach ($galleys as $galley) {
// check if it is a full text
$galleyFile = $galley->getFile();
$genre = $genreDao->getById($galleyFile->getGenreId());
// if it is not a full text, continue
if ($genre->getCategory() != 1 || $genre->getSupplementary() || $genre->getDependent()) continue;
$exportFile = '';
// Get the TAR package for the galley
$result = $plugin->getGalleyPackage($galley, $filter, null, $journal, $journalExportPath, $exportFile);
// If errors occured, remove all created directories and log the errors
if (is_array($result)) {
$fileManager->rmtree($journalExportPath);
$this->addExecutionLogEntry(
__($result[0], array('param' => (isset($result[1]) ? $result[1] : null))),
SCHEDULED_TASK_MESSAGE_TYPE_WARNING
);
return false;
}
// Depost the article
$result = $plugin->depositXML($galley, $journal, $exportFile);
if (is_array($result)) {
// If error occured add it to the list of errors
$errors[] = $result;
$fullyDeposited = false;
}
}
if ($fullyDeposited) {
// Update article status
$submissionDao = DAORegistry::getDAO('SubmissionDAO');
$submission->setData($plugin->getDepositStatusSettingName(), DNB_STATUS_DEPOSITED);
$submissionDao->updateObject($submission);
}
}
}
// Remove the generated directories
$fileManager->rmtree($journalExportPath);
}
}
if (!empty($errors)) {
// If there were some deposit errors, log them
foreach($errors as $error) {
$this->addExecutionLogEntry(
__($error[0], array('param' => (isset($error[1]) ? $error[1] : null))),
SCHEDULED_TASK_MESSAGE_TYPE_WARNING
);
}
}
return true;
}
/**
* Get all journals that meet the requirements for
* automatic articles deposit to DNB.
* @return array
*/
function _getJournals() {
$plugin = $this->_plugin;
$contextDao = Application::getContextDAO(); /* @var $contextDao JournalDAO */
$journalFactory = $contextDao->getAll(true);
$journals = array();
while($journal = $journalFactory->next()) {
$journalId = $journal->getId();
// check required plugin settings
if (!$plugin->getSetting($journalId, 'username') ||
!$plugin->getSetting($journalId, 'password') ||
!$plugin->getSetting($journalId, 'folderId') ||
!$plugin->getSetting($journalId, 'automaticDeposit') ||
!$plugin->checkPluginSettings($journal)) continue;
$journals[] = $journal;
unset($journal);
}
return $journals;
}
}
?>