forked from pkp/crossref-ojs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrossRefExportPlugin.inc.php
496 lines (449 loc) · 16 KB
/
CrossRefExportPlugin.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<?php
/**
* @file plugins/importexport/crossref/CrossRefExportPlugin.inc.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2003-2021 John Willinsky
* Distributed under The MIT License. For full terms see the file LICENSE.
*
* @class CrossRefExportPlugin
* @ingroup plugins_importexport_crossref
*
* @brief CrossRef/MEDLINE XML metadata export plugin
*/
use APP\facades\Repo;
import('classes.plugins.DOIPubIdExportPlugin');
// The status of the Crossref DOI.
// any, notDeposited, and markedRegistered are reserved
define('CROSSREF_STATUS_FAILED', 'failed');
define('CROSSREF_API_DEPOSIT_OK', 200);
define('CROSSREF_API_DEPOSIT_ERROR_FROM_CROSSREF', 403);
define('CROSSREF_API_URL', 'https://api.crossref.org/v2/deposits');
//TESTING
define('CROSSREF_API_URL_DEV', 'https://test.crossref.org/v2/deposits');
define('CROSSREF_API_STATUS_URL', 'https://doi.crossref.org/servlet/submissionDownload');
//TESTING
define('CROSSREF_API_STATUS_URL_DEV', 'https://test.crossref.org/servlet/submissionDownload');
// The name of the setting used to save the registered DOI and the URL with the deposit status.
define('CROSSREF_DEPOSIT_STATUS', 'depositStatus');
use PKP\file\FileManager;
use PKP\linkAction\LinkAction;
use PKP\notification\PKPNotification;
use APP\submission\Submission;
// FIXME: Add namespacing
// use Issue;
class CrossRefExportPlugin extends DOIPubIdExportPlugin {
/**
* @copydoc Plugin::getName()
*/
function getName() {
return 'CrossRefExportPlugin';
}
/**
* @copydoc Plugin::getDisplayName()
*/
function getDisplayName() {
return __('plugins.importexport.crossref.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
function getDescription() {
return __('plugins.importexport.crossref.description');
}
/**
* @copydoc PubObjectsExportPlugin::getSubmissionFilter()
*/
function getSubmissionFilter() {
return 'article=>crossref-xml';
}
/**
* @copydoc PubObjectsExportPlugin::getStatusNames()
*/
function getStatusNames() {
return array_merge(parent::getStatusNames(), array(
EXPORT_STATUS_REGISTERED => __('plugins.importexport.crossref.status.registered'),
CROSSREF_STATUS_FAILED => __('plugins.importexport.crossref.status.failed'),
EXPORT_STATUS_MARKEDREGISTERED => __('plugins.importexport.crossref.status.markedRegistered'),
));
}
/**
* @copydoc PubObjectsExportPlugin::getStatusActions()
*/
function getStatusActions($pubObject) {
$request = Application::get()->getRequest();
$dispatcher = $request->getDispatcher();
return array(
CROSSREF_STATUS_FAILED =>
new LinkAction(
'failureMessage',
new AjaxModal(
$dispatcher->url(
$request, PKPApplication::ROUTE_COMPONENT, null,
'grid.settings.plugins.settingsPluginGridHandler',
'manage', null, array('plugin' => 'CrossRefExportPlugin', 'category' => 'importexport', 'verb' => 'statusMessage',
'batchId' => $pubObject->getData($this->getDepositBatchIdSettingName()), 'articleId' => $pubObject->getId())
),
__('plugins.importexport.crossref.status.failed'),
'failureMessage'
),
__('plugins.importexport.crossref.status.failed')
)
);
}
/**
* @copydoc PubObjectsExportPlugin::getStatusMessage()
*/
function getStatusMessage($request) {
// if the failure occured on request and the message was saved
// return that message
$articleId = $request->getUserVar('articleId');
$article = Repo::submission()->get((int) $articleId);
$failedMsg = $article->getData($this->getFailedMsgSettingName());
if (!empty($failedMsg)) {
return $failedMsg;
}
$context = $request->getContext();
$httpClient = Application::get()->getHttpClient();
try {
$response = $httpClient->request(
'POST',
$this->isTestMode($context) ? CROSSREF_API_STATUS_URL_DEV : CROSSREF_API_STATUS_URL,
[
'form_params' => [
'doi_batch_id' => $request->getUserVar('batchId'),
'type' => 'result',
'usr' => $this->getSetting($context->getId(), 'username'),
'pwd' => $this->getSetting($context->getId(), 'password'),
]
]
);
} catch (GuzzleHttp\Exception\RequestException $e) {
$returnMessage = $e->getMessage();
if ($e->hasResponse()) {
$returnMessage = $e->getResponse()->getBody(true) . ' (' .$e->getResponse()->getStatusCode() . ' ' . $e->getResponse()->getReasonPhrase() . ')';
}
return __('plugins.importexport.common.register.error.mdsError', array('param' => $returnMessage));
}
return (string) $response->getBody();
}
/**
* @copydoc PubObjectsExportPlugin::getExportActionNames()
*/
function getExportActionNames() {
return array(
EXPORT_ACTION_DEPOSIT => __('plugins.importexport.crossref.action.register'),
EXPORT_ACTION_EXPORT => __('plugins.importexport.crossref.action.export'),
EXPORT_ACTION_MARKREGISTERED => __('plugins.importexport.crossref.action.markRegistered'),
);
}
/**
* Get a list of additional setting names that should be stored with the objects.
* @return array
*/
protected function _getObjectAdditionalSettings() {
return array_merge(parent::_getObjectAdditionalSettings(), array(
$this->getDepositBatchIdSettingName(),
$this->getFailedMsgSettingName(),
));
}
/**
* @copydoc ImportExportPlugin::getPluginSettingsPrefix()
*/
function getPluginSettingsPrefix() {
return 'crossref';
}
/**
* @copydoc PubObjectsExportPlugin::getSettingsFormClassName()
*/
function getSettingsFormClassName() {
return 'CrossRefSettingsForm';
}
/**
* @copydoc PubObjectsExportPlugin::getExportDeploymentClassName()
*/
function getExportDeploymentClassName() {
return 'CrossrefExportDeployment';
}
/**
* @copydoc PubObjectsExportPlugin::executeExportAction()
*/
function executeExportAction($request, $objects, $filter, $tab, $objectsFileNamePart, $noValidation = null) {
$context = $request->getContext();
$path = array('plugin', $this->getName());
$fileManager = new FileManager();
$resultErrors = array();
if ($request->getUserVar(EXPORT_ACTION_DEPOSIT)) {
assert($filter != null);
// Errors occured will be accessible via the status link
// thus do not display all errors notifications (for every article),
// just one general.
// Warnings occured when the registration was successfull will however be
// displayed for each article.
$errorsOccured = false;
// The new Crossref deposit API expects one request per object.
// On contrary the export 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) {
// Get the XML
$exportXml = $this->exportXML(array($object), $filter, $context, $noValidation);
// Write the XML to a file.
// export file name example: crossref-20160723-160036-articles-1-1.xml
$objectsFileNamePart = $objectsFileNamePart . '-' . $object->getId();
$exportFileName = $this->getExportFileName($this->getExportPath(), $objectsFileNamePart, $context, '.xml');
$fileManager->writeFile($exportFileName, $exportXml);
// Deposit the XML file.
$result = $this->depositXML($object, $context, $exportFileName);
if (!$result) {
$errorsOccured = true;
}
if (is_array($result)) {
$resultErrors[] = $result;
}
// Remove all temporary files.
$fileManager->deleteByPath($exportFileName);
}
// send notifications
if (empty($resultErrors)) {
if ($errorsOccured) {
$this->_sendNotification(
$request->getUser(),
'plugins.importexport.crossref.register.error.mdsError',
PKPNotification::NOTIFICATION_TYPE_ERROR
);
} else {
$this->_sendNotification(
$request->getUser(),
$this->getDepositSuccessNotificationMessageKey(),
PKPNotification::NOTIFICATION_TYPE_SUCCESS
);
}
} else {
foreach($resultErrors as $errors) {
foreach ($errors as $error) {
assert(is_array($error) && count($error) >= 1);
$this->_sendNotification(
$request->getUser(),
$error[0],
PKPNotification::NOTIFICATION_TYPE_ERROR,
(isset($error[1]) ? $error[1] : null)
);
}
}
}
// redirect back to the right tab
$request->redirect(null, null, null, $path, null, $tab);
} else {
parent::executeExportAction($request, $objects, $filter, $tab, $objectsFileNamePart, $noValidation);
}
}
/**
* @see PubObjectsExportPlugin::depositXML()
*
* @param $objects Submission
* @param $context Context
* @param $filename string Export XML filename
*/
function depositXML($objects, $context, $filename) {
$status = null;
$msgSave = null;
$httpClient = Application::get()->getHttpClient();
assert(is_readable($filename));
try {
$response = $httpClient->request('POST',
$this->isTestMode($context) ? CROSSREF_API_URL_DEV : CROSSREF_API_URL,
[
'multipart' => [
[
'name' => 'usr',
'contents' => $this->getSetting($context->getId(), 'username'),
],
[
'name' => 'pwd',
'contents' => $this->getSetting($context->getId(), 'password'),
],
[
'name' => 'operation',
'contents' => 'doMDUpload',
],
[
'name' => 'mdFile',
'contents' => fopen($filename, 'r'),
],
]
]
);
} catch (GuzzleHttp\Exception\RequestException $e) {
$returnMessage = $e->getMessage();
if ($e->hasResponse()) {
$eResponseBody = $e->getResponse()->getBody(true);
$eStatusCode = $e->getResponse()->getStatusCode();
if ($eStatusCode == CROSSREF_API_DEPOSIT_ERROR_FROM_CROSSREF) {
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($eResponseBody);
$batchIdNode = $xmlDoc->getElementsByTagName('batch_id')->item(0);
$msg = $xmlDoc->getElementsByTagName('msg')->item(0)->nodeValue;
$msgSave = $msg . PHP_EOL . $eResponseBody;
$status = CROSSREF_STATUS_FAILED;
$this->updateDepositStatus($context, $objects, $status, $batchIdNode->nodeValue, $msgSave);
$this->updateObject($objects);
$returnMessage = $msg . ' (' .$eStatusCode . ' ' . $e->getResponse()->getReasonPhrase() . ')';
} else {
$returnMessage = $eResponseBody . ' (' .$eStatusCode . ' ' . $e->getResponse()->getReasonPhrase() . ')';
}
}
return [['plugins.importexport.common.register.error.mdsError', $returnMessage]];
}
// Get DOMDocument from the response XML string
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($response->getBody());
$batchIdNode = $xmlDoc->getElementsByTagName('batch_id')->item(0);
// Get the DOI deposit status
// If the deposit failed
$failureCountNode = $xmlDoc->getElementsByTagName('failure_count')->item(0);
$failureCount = (int) $failureCountNode->nodeValue;
if ($failureCount > 0) {
$status = CROSSREF_STATUS_FAILED;
$result = false;
} else {
// Deposit was received
$status = EXPORT_STATUS_REGISTERED;
$result = true;
// If there were some warnings, display them
$warningCountNode = $xmlDoc->getElementsByTagName('warning_count')->item(0);
$warningCount = (int) $warningCountNode->nodeValue;
if ($warningCount > 0) {
$result = array(array('plugins.importexport.crossref.register.success.warning', htmlspecialchars($response->getBody())));
}
// A possibility for other plugins (e.g. reference linking) to work with the response
HookRegistry::call('crossrefexportplugin::deposited', array($this, $response->getBody(), $objects));
}
// Update the status
if ($status) {
$this->updateDepositStatus($context, $objects, $status, $batchIdNode->nodeValue, $msgSave);
$this->updateObject($objects);
}
return $result;
}
/**
* Check the CrossRef APIs, if deposits and registration have been successful
* @param $context Context
* @param $object The object getting deposited
* @param $status CROSSREF_STATUS_...
* @param $batchId string
* @param $failedMsg string (opitonal)
*/
function updateDepositStatus($context, $object, $status, $batchId, $failedMsg = null) {
assert($object instanceof Submission || $object instanceof Issue);
// remove the old failure message, if exists
$object->setData($this->getFailedMsgSettingName(), null);
$object->setData($this->getDepositStatusSettingName(), $status);
$object->setData($this->getDepositBatchIdSettingName(), $batchId);
if ($failedMsg) {
$object->setData($this->getFailedMsgSettingName(), $failedMsg);
}
if ($status == EXPORT_STATUS_REGISTERED) {
// Save the DOI -- the object will be updated
$this->saveRegisteredDoi($context, $object);
}
}
/**
* @copydoc DOIPubIdExportPlugin::markRegistered()
*/
function markRegistered($context, $objects) {
foreach ($objects as $object) {
// remove the old failure message, if exists
$object->setData($this->getFailedMsgSettingName(), null);
$object->setData($this->getDepositStatusSettingName(), EXPORT_STATUS_MARKEDREGISTERED);
$this->saveRegisteredDoi($context, $object);
}
}
/**
* Get request failed message setting name.
* @return string
*/
function getFailedMsgSettingName() {
return $this->getPluginSettingsPrefix().'::failedMsg';
}
/**
* Get deposit batch ID setting name.
* @return string
*/
function getDepositBatchIdSettingName() {
return $this->getPluginSettingsPrefix().'::batchId';
}
/**
* @copydoc PubObjectsExportPlugin::getDepositSuccessNotificationMessageKey()
*/
function getDepositSuccessNotificationMessageKey() {
return 'plugins.importexport.common.register.success';
}
/**
* @copydoc PKPImportExportPlugin::executeCLI()
*/
function executeCLICommand($scriptName, $command, $context, $outputFile, $objects, $filter, $objectsFileNamePart) {
switch ($command) {
case 'export':
PluginRegistry::loadCategory('generic', true, $context->getId());
$exportXml = $this->exportXML($objects, $filter, $context);
if ($outputFile) file_put_contents($outputFile, $exportXml);
break;
case 'register':
PluginRegistry::loadCategory('generic', true, $context->getId());
$fileManager = new FileManager();
$resultErrors = array();
// Errors occured will be accessible via the status link
// thus do not display all errors notifications (for every article),
// just one general.
// Warnings occured when the registration was successfull will however be
// displayed for each article.
$errorsOccured = false;
// The new Crossref deposit API expects one request per object.
// On contrary the export 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) {
// Get the XML
$exportXml = $this->exportXML(array($object), $filter, $context);
// Write the XML to a file.
// export file name example: crossref-20160723-160036-articles-1-1.xml
$objectsFileNamePartId = $objectsFileNamePart . '-' . $object->getId();
$exportFileName = $this->getExportFileName($this->getExportPath(), $objectsFileNamePartId, $context, '.xml');
$fileManager->writeFile($exportFileName, $exportXml);
// Deposit the XML file.
$result = $this->depositXML($object, $context, $exportFileName);
if (!$result) {
$errorsOccured = true;
}
if (is_array($result)) {
$resultErrors[] = $result;
}
// Remove all temporary files.
$fileManager->deleteByPath($exportFileName);
}
// display deposit result status messages
if (empty($resultErrors)) {
if ($errorsOccured) {
echo __('plugins.importexport.crossref.register.error.mdsError') . "\n";
} else {
echo __('plugins.importexport.common.register.success') . "\n";
}
} else {
echo __('plugins.importexport.common.cliError') . "\n";
foreach($resultErrors as $errors) {
foreach ($errors as $error) {
assert(is_array($error) && count($error) >= 1);
$errorMessage = __($error[0], array('param' => (isset($error[1]) ? $error[1] : null)));
echo "*** $errorMessage\n";
}
}
echo "\n";
$this->usage($scriptName);
}
break;
}
}
}