-
Notifications
You must be signed in to change notification settings - Fork 7
/
MedraExportPlugin.php
361 lines (323 loc) · 12.1 KB
/
MedraExportPlugin.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/**
* @file plugins/generic/medra/MedraExportPlugin.php
*
* Copyright (c) 2014-2024 Simon Fraser University
* Copyright (c) 2003-2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class MedraExportPlugin
*
* @brief mEDRA Onix for DOI (O4DOI) export/registration plugin.
*/
namespace APP\plugins\generic\medra;
use APP\core\Application;
use APP\facades\Repo;
use APP\issue\Issue;
use APP\plugins\DOIPubIdExportPlugin;
use APP\plugins\generic\medra\classes\MedraWebservice;
use APP\publication\Publication;
use APP\submission\Submission;
use DOMDocument;
use Exception;
use PKP\context\Context;
use PKP\core\DataObject;
use PKP\doi\Doi;
use PKP\facades\Locale;
use PKP\file\FileManager;
use PKP\file\TemporaryFileManager;
use PKP\i18n\LocaleConversion;
class MedraExportPlugin extends DOIPubIdExportPlugin
{
public function __construct(protected MedraPlugin $agencyPlugin)
{
parent::__construct();
}
/**
* @copydoc Plugin::getName()
*/
public function getName()
{
return 'MedraExportPlugin';
}
/**
* @copydoc Plugin::getDisplayName()
*/
public function getDisplayName()
{
return __('plugins.importexport.medra.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
public function getDescription()
{
return __('plugins.importexport.medra.description');
}
/**
* @copydoc PubObjectsExportPlugin::getSubmissionFilter()
*/
public function getSubmissionFilter()
{
return 'article=>medra-xml';
}
/**
* @copydoc PubObjectsExportPlugin::getIssueFilter()
*/
public function getIssueFilter()
{
return 'issue=>medra-xml';
}
/**
* @copydoc PubObjectsExportPlugin::getRepresentationFilter()
*/
public function getRepresentationFilter()
{
return 'galley=>medra-xml';
}
/**
* @copydoc ImportExportPlugin::getPluginSettingsPrefix()
*/
public function getPluginSettingsPrefix()
{
return 'medra';
}
/**
* @copydoc DOIPubIdExportPlugin::getSettingsFormClassName()
*/
public function getSettingsFormClassName()
{
throw new Exception('DOI settings no longer managed via plugin settings form.');
}
/**
* @copydoc PubObjectsExportPlugin::getExportDeploymentClassName()
*/
public function getExportDeploymentClassName()
{
return '\APP\plugins\generic\medra\MedraExportDeployment';
}
/**
* @copydoc PubObjectsExportPlugin::getStatusMessage()
*/
public function getStatusMessage($request)
{
$articleId = $request->getUserVar('articleId');
$article = Repo::submission()->get((int)$articleId);
$failedMsg = $article->getData('doiObject')->getData($this->agencyPlugin->getFailedMsgSettingName());
if (!empty($failedMsg)) {
return $failedMsg;
}
}
/**
* Exports and stores XML as a TemporaryFile
*
* @param DataObject[] $objects
*
* @throws Exception
*/
public function exportAsDownload(
Context $context,
array $objects,
string $filter,
string $objectsFileNamePart,
?bool $noValidation = null,
?array &$exportErrors = null
): ?int {
$fileManager = new TemporaryFileManager();
$exportErrors = [];
$exportXml = $this->exportXML($objects, $filter, $context, $noValidation, $exportErrors);
$exportFileName = $this->getExportFileName($this->getExportPath(), $objectsFileNamePart, $context, '.xml');
$fileManager->writeFile($exportFileName, $exportXml);
$user = Application::get()->getRequest()->getUser();
return $fileManager->createTempFileFromExisting($exportFileName, $user->getId());
}
/**
* Exports and registers XML for each Submission or Issue with mEDRA.
*
* @param DataObject[] $objects
*/
public function exportAndDeposit(
Context $context,
array $objects,
string $filter,
string &$responseMessage,
?bool $noValidation = null
): bool {
$fileManager = new FileManager();
$resultErrors = [];
// Errors occurred will be accessible via the status link
// thus do not display all errors notifications (for every article),
// just one general.
$errorsOccurred = false;
// In order to get and save the errors for each object,
// we will deposit object by object.
// The export however supports bulk/batch object export, thus
// also the filter expects an array of objects.
// Thus the foreach loop, but every object will be in an one item array for
// the export and filter to work.
foreach ($objects as $object) {
/** @var Submission|Issue $object */
// Get the XML
$exportErrors = [];
$exportXml = $this->exportXML([$object], $filter, $context, $noValidation, $exportErrors);
// Write the XML to a file.
// export file name example: medra-20160723-160036-articles-1-1.xml
$objectFileNamePart = $this->_getObjectFileNamePart($object);
$exportFileName = $this->getExportFileName($this->getExportPath(), $objectFileNamePart, $context, '.xml');
$fileManager->writeFile($exportFileName, $exportXml);
// Deposit the XML file.
$result = $this->depositXML($object, $context, $exportFileName);
if (!$result) {
$errorsOccurred = true;
}
if (is_array($result)) {
$resultErrors[] = $result;
}
// Remove all temporary files.
$fileManager->deleteByPath($exportFileName);
}
// Prepare response message and return status
if (empty($resultErrors)) {
if ($errorsOccurred) {
$responseMessage = 'plugins.generic.medra.deposit.unsuccessful';
return false;
} else {
$responseMessage = $this->getDepositSuccessNotificationMessageKey();
return true;
}
} else {
$responseMessage = 'api.dois.400.depositFailed';
return false;
}
}
/**
* Registers XML with mEDRA.
*
* @see PubObjectsExportPlugin::depositXML()
* @param Submission|Issue $objects
*/
public function depositXML($objects, $context, $filename)
{
// Use a different endpoint for testing and production.
// New endpoint: use a different endpoint if the user selected the checkbox to deposit also in Crossref.
$crEnabled = $this->getSetting($context->getId(), 'crEnabled');
$endpoint =
($this->isTestMode($context) ?
($crEnabled ? MedraWebservice::MEDRA2CR_WS_ENDPOINT_DEV : MedraWebservice::MEDRA_WS_ENDPOINT_DEV) :
($crEnabled ? MedraWebservice::MEDRA2CR_WS_ENDPOINT : MedraWebservice::MEDRA_WS_ENDPOINT));
// Get credentials.
$username = $this->getSetting($context->getId(), 'username');
$password = $this->getSetting($context->getId(), 'password');
// Retrieve the XML.
$xml = file_get_contents($filename);
// Get the current user locale to get the Crossref service validation error messages in that language
// Currently only supported: eng, ita
$language = 'eng';
$supportedLanguages = array('eng', 'ita');
$user3LetterLang = LocaleConversion::get3LetterIsoFromLocale(Locale::getLocale());
if (in_array($user3LetterLang, $supportedLanguages)) {
$language = $user3LetterLang;
}
// Instantiate the mEDRA web service wrapper.
$ws = new MedraWebservice($endpoint, $username, $password);
// Register the XML with mEDRA (upload) or also with Crossref (deposit)
$result = $crEnabled ? $ws->deposit($xml, $language) : $ws->upload($xml);
if ($result === true) {
$this->_updateDepositStatus($objects, Doi::STATUS_REGISTERED);
return true;
} else {
if (!is_string($result)) return false; // When is this happening?
// Handle errors.
$doc = new DOMDocument();
$doc->loadXML($result);
$statusCode = $doc->getElementsByTagName('statusCode');
if ($statusCode->length > 0 && $statusCode->item(0)->textContent == 'FAILED') {
$errNo = $doc->getElementsByTagName('errorsNumber')->item(0)->textContent;
$errNodeList = $doc->getElementsByTagName('error');
$errors = array();
foreach($errNodeList as $errNode) {
$error = array();
if($errNode->childNodes->length) {
foreach($errNode->childNodes as $errChildNode) {
$error[$errChildNode->nodeName] = $errChildNode->nodeValue;
}
}
$errors[] = $error;
}
$status = Doi::STATUS_ERROR;
$errorMsg = $this->buildDepositErrorMsg($errNo, $errors, $xml);
$this->_updateDepositStatus($objects, $status, $errorMsg);
return false; // or a message?
}
$status = Doi::STATUS_ERROR;
$this->_updateDepositStatus($objects, $status, $result);
return array(
array('plugins.importexport.common.register.error.mdsError', $result)
);
}
}
/** Proxy to main plugin class's getSetting method */
public function getSetting($contextId, $name)
{
return $this->agencyPlugin->getSetting($contextId, $name);
}
/**
* Update stored DOI status based on if deposits and registration have been successful
*
* @param Submission|Issue $object
*/
private function _updateDepositStatus(DataObject $object, string $status, string $failedMsg = null)
{
if ($object instanceof Submission) {
$object = $object->getCurrentPublication();
}
/** @var Publication|Issue $object */
/** @var Doi $doiObject */
$doiObject = $object->getData('doiObject');
$editParams = [
'status' => $status,
// Sets new failedMsg or resets to null for removal of previous message
$this->agencyPlugin->getFailedMsgSettingName() => $failedMsg,
];
if ($status == Doi::STATUS_REGISTERED) {
$editParams['registrationAgency'] = $this->getName();
}
Repo::doi()->edit($doiObject, $editParams);
}
/**
* Build special deposit error message - to provide the similar functionality as the earlier plugins.
*/
public function buildDepositErrorMsg(string $errNo, array $errors, string $xml): string
{
$errorMsg =
__('plugins.importexport.medra.crossref.error.cause') . PHP_EOL .
__('plugins.importexport.medra.crossref.error.number') . ': ' . $errNo . PHP_EOL .
__('plugins.importexport.medra.crossref.error.details') . ': ' . PHP_EOL . PHP_EOL;
foreach ($errors as $error) {
$errorMsg .=
__('plugins.importexport.medra.crossref.error.code') . ': ' . $error['code'] . PHP_EOL .
__('plugins.importexport.medra.crossref.error.element') . ': ' . $error['reference'] . PHP_EOL .
__('plugins.importexport.medra.crossref.error.description') . ': ' . $error['description'] . PHP_EOL . PHP_EOL ;
}
$errorMsg .=
__("plugins.importexport.common.invalidXML") . ': ' . PHP_EOL .
$xml .
PHP_EOL;
return $errorMsg;
}
/**
* Get the object file name part.
*
* @param Submission|Issue $object
*/
private function _getObjectFileNamePart(DataObject $object): string
{
if ($object instanceof Submission) {
return 'articles-' . $object->getId();
} elseif ($object instanceof Issue) {
return 'issues-' . $object->getId();
} else {
return '';
}
}
}