-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCrossrefReferenceLinkingPlugin.php
496 lines (442 loc) · 18.8 KB
/
CrossrefReferenceLinkingPlugin.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
495
496
<?php
/**
* @file CrossrefReferenceLinkingPlugin.inc.php
*
* Copyright (c) 2013-2023 Simon Fraser University
* Copyright (c) 2003-2023 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file LICENSE.
*
* @class CrossrefReferenceLinkingPlugin
* @brief Reference Linking plugin class
*/
namespace APP\plugins\generic\crossrefReferenceLinking;
use APP\core\Application;
use APP\facades\Repo;
use APP\notification\NotificationManager;
use APP\notification\Notification;
use APP\publication\Publication;
use APP\submission\Submission;
use DOMDocument;
use PKP\citation\CitationDAO;
use PKP\context\Context;
use PKP\core\JSONMessage;
use PKP\db\DAORegistry;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\AjaxModal;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
use PKP\plugins\PluginRegistry;
use PKP\plugins\interfaces\HasTaskScheduler;
use PKP\scheduledTask\PKPScheduler;
class CrossrefReferenceLinkingPlugin extends GenericPlugin implements HasTaskScheduler
{
public const CROSSREF_API_REFS_URL = 'https://doi.crossref.org/getResolvedRefs';
public const CROSSREF_API_REFS_URL_DEV = 'https://test.crossref.org/getResolvedRefs';
/**
* @copydoc Plugin::register()
*/
public function register($category, $path, $mainContextId = null): bool
{
$registered = parent::register($category, $path, $mainContextId);
if (!$registered) {
return false;
}
if (Application::isUnderMaintenance()) {
return true;
}
// Additional fields added
Hook::add('Schema::get::submission', [$this, 'addSubmissionSchema']);
Hook::add('citationdao::getAdditionalFieldNames', [$this, 'getAdditionalCitationFieldNames']);
if (!$this->getEnabled($mainContextId)) {
return true;
}
if (!isset($mainContextId)) {
$mainContextId = $this->getCurrentContextId();
}
if (!$this->hasCrossrefCredentials($mainContextId) || !$this->citationsEnabled($mainContextId)) {
return true;
}
// Crossref export plugin hooks
Hook::add('articlecrossrefxmlfilter::execute', [$this, 'addCrossrefCitationsElements']);
Hook::add('crossrefexportplugin::deposited', [$this, 'getCitationsDiagnosticId']);
// Citation changed hook
Hook::add('CitationDAO::afterImportCitations', [$this, 'citationsChanged']);
// Article page hooks
Hook::add('Templates::Article::Details::Reference', [$this, 'displayReferenceDOI']);
return true;
}
/**
* Are Crossref username and password set in Crossref plugin
*/
public function hasCrossrefCredentials(int $contextId): bool
{
// If crossref plugin is set i.e. the crossref credentials exist we can assume that DOI plugin is set correctly
$crossrefPlugin = PluginRegistry::getPlugin('generic', 'crossrefplugin');
return $crossrefPlugin && strlen($crossrefPlugin->getSetting($contextId, 'username')) > 0 && strlen($crossrefPlugin->getSetting($contextId, 'password')) > 0;
}
/**
* Are citations submission metadata enabled in this journal
*/
public function citationsEnabled(int $contextId): bool
{
$contextDao = Application::getContextDAO();
$context = $contextDao->getById($contextId);
return !empty($context->getSetting('citations'));
}
/**
* @copydoc Plugin::getDisplayName()
*/
public function getDisplayName(): string
{
return __('plugins.generic.crossrefReferenceLinking.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
public function getDescription(): string
{
return __('plugins.generic.crossrefReferenceLinking.description');
}
/**
* @see Plugin::getActions()
*/
public function getActions($request, $actionArgs): array
{
$actions = parent::getActions($request, $actionArgs);
if (!$this->getEnabled()) {
return $actions;
}
$router = $request->getRouter();
$linkAction = new LinkAction(
id: 'settings',
actionRequest: new AjaxModal(
url: $router->url(
request: $request,
op: 'manage',
params: [
'verb' => 'settings',
'plugin' => $this->getName(),
'category' => 'generic'
]
),
title: $this->getDisplayName()
),
title: __('manager.plugins.settings')
);
array_unshift($actions, $linkAction);
return $actions;
}
/**
* @copydoc Plugin::manage()
*/
public function manage($args, $request): JSONMessage
{
$context = $request->getContext();
switch ($request->getUserVar('verb')) {
case 'settings':
$form = new CrossrefReferenceLinkingSettingsForm($this, $context->getId());
$form->initData();
return new JSONMessage(true, $form->fetch($request));
case 'save':
$form = new CrossrefReferenceLinkingSettingsForm($this, $context->getId());
$form->readInputData();
if ($form->validate()) {
$form->execute($request);
$notificationManager = new NotificationManager();
$notificationManager->createTrivialNotification(
$request->getUser()->getId(),
Notification::NOTIFICATION_TYPE_SUCCESS,
['contents' => __('plugins.generic.crossrefReferenceLinking.settings.form.saved')]
);
return new JSONMessage(true);
}
return new JSONMessage(true, $form->fetch($request));
}
return parent::manage($args, $request);
}
/**
* @copydoc \PKP\plugins\interfaces\HasTaskScheduler::registerSchedules()
*/
public function registerSchedules(PKPScheduler $scheduler): void
{
$scheduler
->addSchedule(new CrossrefReferenceLinkingInfoSender([]))
->hourly()
->name(CrossrefReferenceLinkingInfoSender::class)
->withoutOverlapping();
}
/**
* Add references data to the Crossref XML export
*
* @param $hookName string 'articlecrossrefxmlfilter::execute'
* @param $params array [
* @option DOMDocument Crossref filter output
* ]
*/
public function addCrossrefCitationsElements(string $hookName, array $params): bool
{
/** @var DOMDocument $preliminaryOutput */
$preliminaryOutput =& $params[0];
$request = Application::get()->getRequest();
$context = $request->getContext();
// Crossref export cannot be executed via CLI any more, thus there will always be a context
$contextId = $context->getId();
$citationDao = DAORegistry::getDAO('CitationDAO'); /** @var CitationDAO $citationDao */
$rfNamespace = 'http://www.crossref.org/schema/5.3.1';
$articleNodes = $preliminaryOutput->getElementsByTagName('journal_article');
foreach ($articleNodes as $articleNode) {
$doiDataNode = $articleNode->getElementsByTagName('doi_data')->item(0);
$doiNode = $doiDataNode->getElementsByTagName('doi')->item(0);
$doiValue = $doiNode->nodeValue;
// There should be only one DOI
/** @var Doi $doi */
$doi = Repo::doi()->getCollector()->filterByContextIds([$contextId])->filterByIdentifier($doiValue)->getMany()->first();
if (!$doi) {
return false;
}
$publications = Repo::publication()->getCollector()->filterByDoiIds([$doi->getId()])->getMany();
if ($publications->count() < 1) {
return false;
}
$submission = Repo::submission()->get($publications->first()->getData('submissionId'));
$articleCitations = $citationDao->getByPublicationId($submission->getCurrentPublication()->getId());
$articleCitationsArray = $articleCitations->toArray();
if (!empty($articleCitationsArray)) {
$citationListNode = $preliminaryOutput->createElementNS($rfNamespace, 'citation_list');
foreach ($articleCitationsArray as $citation) {
$rawCitation = $citation->getRawCitation();
if (!empty($rawCitation)) {
$citationNode = $preliminaryOutput->createElementNS($rfNamespace, 'citation');
$citationNode->setAttribute('key', $citation->getId());
// if Crossref DOI already exists for this citation, include it
// else include unstructred raw citation
if ($citation->getData($this->getCitationDoiSettingName())) {
$node = $preliminaryOutput->createElementNS($rfNamespace, 'doi');
$node->appendChild($preliminaryOutput->createTextNode($citation->getData($this->getCitationDoiSettingName())));
} else {
$node = $preliminaryOutput->createElementNS($rfNamespace, 'unstructured_citation');
$node->appendChild($preliminaryOutput->createTextNode($rawCitation));
}
$citationNode->appendChild($node);
$citationListNode->appendChild($citationNode);
}
}
$doiDataNode->parentNode->insertBefore($citationListNode, $doiDataNode->nextSibling);
}
}
return Hook::CONTINUE;
}
/**
* During the article DOI registration with Crossref, get the citations diagnostic ID from the Crossref response.
*
* @param $hookName string Hook name 'crossrefexportplugin::deposited'
* @param array [
* @option CrossrefExportPlugin
* @option string XML reposonse from Crossref deposit
* @option Submission
* ]
*/
public function getCitationsDiagnosticId(string $hookName, array $params): bool
{
/** @var string $response */
$response = & $params[1];
/** @var Submission $submission */
$submission = & $params[2];
// Get DOMDocument from the response XML string
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($response);
if ($xmlDoc->getElementsByTagName('citations_diagnostic')->length > 0) {
$citationsDiagnosticNode = $xmlDoc->getElementsByTagName('citations_diagnostic')->item(0); /** @var DOMNodeList $citationsDiagnosticNode */
$citationsDiagnosticCode = $citationsDiagnosticNode->getAttribute('deferred') ;
//set the citations diagnostic code and the setting for the automatic check
$submission->setData($this->getCitationsDiagnosticIdSettingName(), $citationsDiagnosticCode);
$submission->setData($this->getAutoCheckSettingName(), true);
$submission = Repo::submission()->edit($submission, [], Application::get()->getRequest());
}
return Hook::CONTINUE;
}
/**
* Add properties to the submission entity (SchemaDAO-based)
*
* @param $hookName string `Schema::get::submission`
* @param array $args [
* @option stdClass $schema
* ]
*/
public function addSubmissionSchema(string $hookName, array $args): bool
{
$schema = $args[0];
$schema->properties->{$this->getCitationsDiagnosticIdSettingName()} = (object) [
'type' => 'string',
'apiSummary' => true,
'validation' => ['nullable']
];
$schema->properties->{$this->getAutoCheckSettingName()} = (object) [
'type' => 'boolean',
'apiSummary' => true,
'validation' => ['nullable']
];
return Hook::CONTINUE;
}
/**
* Consider the additional citation setting name 'crossref::doi'.
*/
public function getAdditionalCitationFieldNames(string $hookName, CitationDAO $citationDao, array &$additionalFields): bool
{
$additionalFields[] = $this->getCitationDoiSettingName();
return Hook::CONTINUE;
}
/**
* Resets the submission data related to Reference Linking Plugin.
* Used every time the citations for a certain publication are imported.
*
* @param $hookName string 'CitationDAO::afterImportCitations'
* @param $params array [
* @option integer The publication ID for which the citations are imported
* ]
*/
public function citationsChanged(string $hookName, array $params): bool
{
/** @var int $publicationId */
$publicationId = $params[0];
$publication = Repo::publication()->get($publicationId);
$submission = Repo::submission()->get($publication->getData('submissionId'));
if ($submission->getData($this->getCitationsDiagnosticIdSettingName())) {
$submission->setData($this->getCitationsDiagnosticIdSettingName(), null);
$submission->setData($this->getAutoCheckSettingName(), null);
$submission = Repo::submission()->edit($submission, [], Application::get()->getRequest());
}
return Hook::CONTINUE;
}
/**
* Get found Crossref references DOIs for the given publication DOI.
*/
public function considerFoundCrossrefReferencesDOIs(Publication $publication): void
{
$doi = urlencode($publication->getDoi());
if (empty($doi)) {
return;
}
$citationDao = DAORegistry::getDAO('CitationDAO'); /** @var CitationDAO $citationDao */
$citations = $citationDao->getByPublicationId($publication->getId())->toAssociativeArray();
$citationsToCheck = [];
foreach ($citations as $citation) { /** @var Citation $citation */
if (!$citation->getData($this->getCitationDoiSettingName())) {
$citationsToCheck[$citation->getId()] = $citation;
}
}
if (empty($citationsToCheck)) {
return;
}
$citationsToCheckKeys = array_keys($citationsToCheck);
$submission = Repo::submission()->get($publication->getData('submissionId'));
$matchedReferences = $this->getResolvedRefs($doi, $submission->getData('contextId'));
if ($matchedReferences) {
$filteredMatchedReferences = array_filter(
$matchedReferences,
fn ($value) => in_array($value['key'], $citationsToCheckKeys)
);
foreach ($filteredMatchedReferences as $matchedReference) {
$citation = $citationsToCheck[$matchedReference['key']];
$citation->setData($this->getCitationDoiSettingName(), $matchedReference['doi']);
$citationDao->updateObject($citation);
}
// remove auto check setting
$submission->setData($this->getAutoCheckSettingName(), null);
$submission = Repo::submission()->edit($submission, [], Application::get()->getRequest());
}
}
/**
* Insert reference DOI on the citations and article view page.
*
* @param $hookName string Hook name
* @param $params array [
* @option Citation
* @option Smarty
* @option string Rendered smarty template
* ]
*/
public function displayReferenceDOI(string $hookName, array $params): bool
{
/** @var Citation $citation */
$citation = $params[0]['citation'];
/** @var \Smarty $smarty */
$smarty = &$params[1];
/** @var string $output */
$output = &$params[2];
if ($citation->getData($this->getCitationDoiSettingName())) {
$crossrefFullUrl = 'https://doi.org/' . $citation->getData($this->getCitationDoiSettingName());
$smarty->assign('crossrefFullUrl', $crossrefFullUrl);
$output .= $smarty->fetch($this->getTemplateResource('displayDOI.tpl'));
}
return Hook::CONTINUE;
}
/**
* Get citations diagnostic ID setting name.
*/
public function getCitationsDiagnosticIdSettingName(): string
{
return 'crossref::citationsDiagnosticId';
}
/**
* Get citation crossref DOI setting name.
*/
public function getCitationDoiSettingName(): string
{
return 'crossref::doi';
}
/**
* Get setting name, that defines if the scheduled task for the automatic check
* of the found Crossref citations DOIs should be run, if set up so in the plugin settings.
*/
public function getAutoCheckSettingName(): string
{
return 'crossref::checkCitationsDOIs';
}
/**
* Retrieve all submissions that should be automatically checked for the found Crossref citations DOIs.
*
* @return Submission[]
*/
public function getSubmissionsToCheck(Context $context): array
{
// Retrieve all published articles with their DOIs depositted together with the references.
// i.e. with the citations diagnostic ID setting
$submissionIds = Repo::submission()->getIdsBySetting($this->getAutoCheckSettingName(), true, $context->getId())->toArray();
$submissions = array_map(function ($submissionId) {
return Repo::submission()->get($submissionId);
}, $submissionIds);
return array_filter($submissions, function ($submission) {
return $submission->getData('status') === Submission::STATUS_PUBLISHED;
});
}
/**
* Use Crossref API to get the references DOIs for the the given article DOI.
*/
protected function getResolvedRefs(string $doi, int $contextId): ?array
{
$matchedReferences = null;
PluginRegistry::loadCategory('generic');
$crossrefPlugin = PluginRegistry::getPlugin('generic', 'crossrefplugin');
$username = $crossrefPlugin->getSetting($contextId, 'username');
$password = $crossrefPlugin->getSetting($contextId, 'password');
// Use a different endpoint for testing and production.
$isTestMode = $crossrefPlugin->getSetting($contextId, 'testMode') == 1;
$endpoint = ($isTestMode ? self::CROSSREF_API_REFS_URL_DEV : self::CROSSREF_API_REFS_URL);
$url = $endpoint . '?doi=' . $doi . '&usr=' . $username . '&pwd=' . $password;
$httpClient = Application::get()->getHttpClient();
try {
$response = $httpClient->request('POST', $url);
} catch (\GuzzleHttp\Exception\RequestException $e) {
return null;
}
if ($response?->getStatusCode() == 200) {
$response = json_decode($response->getBody(), true);
$matchedReferences = $response['matched-references'];
}
return $matchedReferences;
}
}
if (!PKP_STRICT_MODE) {
class_alias('\APP\plugins\generic\crossrefReferenceLinking\CrossrefReferenceLinkingPlugin', '\CrossrefReferenceLinkingPlugin');
}