-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathCrossrefCitationDoiHandler.php
More file actions
274 lines (244 loc) · 10.7 KB
/
CrossrefCitationDoiHandler.php
File metadata and controls
274 lines (244 loc) · 10.7 KB
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
<?php
/**
* @file plugins/generic/crossref/CrossrefCitationDoiHandler.php
*
* Copyright (c) 2014-2026 Simon Fraser University
* Copyright (c) 2003-2026 John Willinsky
* Distributed under The MIT License. For full terms see the file LICENSE.
*
* @class CrossrefCitationDoiHandler
*
* @brief Handles citation DOI matching via the Crossref API: tracking deposit responses,
* fetching matched citation DOIs, and displaying them on the article detail page.
*/
namespace APP\plugins\generic\crossref;
use APP\core\Application;
use APP\facades\Repo;
use APP\publication\Publication;
use APP\submission\Submission;
use DOMDocument;
use PKP\citation\Citation;
use PKP\pid\Doi;
use PKP\context\Context;
use PKP\plugins\Hook;
class CrossrefCitationDoiHandler
{
public function __construct(
protected CrossrefPlugin $plugin
) {
}
/**
* 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';
}
/**
* Get citations diagnostic ID setting name.
*/
public function getCitationsDiagnosticIdSettingName(): string
{
return 'crossref::citationsDiagnosticId';
}
/**
* Register hooks for citation DOI handling.
*/
public function registerHooks(): void
{
Hook::add('Schema::get::submission', $this->addSubmissionSchema(...));
Hook::add('Citation::importCitations::after', $this->citationsChanged(...));
}
/**
* Register hooks that require the plugin to be enabled.
*/
public function registerEnabledHooks(): void
{
Hook::add('Templates::Article::Details::Reference', $this->displayReferenceDOI(...));
}
/**
* Add properties to the submission schema for citation diagnostic tracking.
*
* @param string $hookName `Schema::get::submission`
*/
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;
}
/**
* Resets the submission's citations diagnostic ID and automatic check settings every time
* all citations are changed, so that the submission will not be checked for found Crossref
* references DOIs in the next scheduled task run.
*
* @param string $hookName Hook name 'Citation::importCitations::after'
*/
public function citationsChanged(string $hookName, int $publicationId, array $existingCitations, array $importedCitations): bool
{
if (!$this->plugin->getEnabled() ||
!$this->plugin->hasCrossrefCredentials() ||
!$this->plugin->citationsEnabled()) {
return Hook::CONTINUE;
}
$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);
Repo::submission()->edit($submission, []);
}
return Hook::CONTINUE;
}
/**
* Append DOI link to a citation on the article detail page, if the citation text does not contain it.
*
* @param string $hookName Hook name 'Templates::Article::Details::Reference'
* @param array $params [
* @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('doi')) {
// Normalise to https so that Doi::extractFromString detects DOI links regardless of how they were stored.
$rawString = str_ireplace('http://', 'https://', $citation->getRawCitation());
$doi = Doi::extractFromString($rawString);
// Display DOI only if the raw citation string doesn't already contain the DOI as a link, to avoid duplicate DOIs on the page
if (empty($doi)) {
$crossrefFullUrl = 'https://doi.org/' . $citation->getData('doi');
$smarty->assign('crossrefFullUrl', $crossrefFullUrl);
$output .= $smarty->fetch($this->plugin->getTemplateResource('displayDOI.tpl'));
}
}
return Hook::CONTINUE;
}
/**
* Store the citations diagnostic ID from a Crossref deposit response,
* so the scheduled task can later check for found reference DOIs.
*/
public function handleDepositResponse(string $responseBody, Submission $submission): void
{
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($responseBody);
if ($xmlDoc->getElementsByTagName('citations_diagnostic')->length > 0) {
$citationsDiagnosticNode = $xmlDoc->getElementsByTagName('citations_diagnostic')->item(0);
$citationsDiagnosticCode = $citationsDiagnosticNode->getAttribute('deferred');
$submission->setData($this->getCitationsDiagnosticIdSettingName(), $citationsDiagnosticCode);
$submission->setData($this->getAutoCheckSettingName(), true);
Repo::submission()->edit($submission, []);
}
}
/**
* Fetch resolved citation DOIs from Crossref and store them for all pending submissions.
*/
public function processPendingCitationDois(Context $context): void
{
// Retrieve all submissions flagged for auto-check, i.e. whose DOIs were deposited
// with references and are awaiting resolved citation DOIs from Crossref.
$submissionIds = Repo::submission()->getIdsBySetting($this->getAutoCheckSettingName(), true, $context->getId())->toArray();
$submissions = Repo::submission()->getCollector()->filterByContextIds([$context->getId()])->filterBySubmissionIds($submissionIds)->getMany();
foreach ($submissions as $submission) {
$publicationsToCheck = $this->getPublicationsToCheck($submission, $context); // always contain DOI and are published
$apiCallFailed = false;
foreach ($publicationsToCheck as $publication) { /** @var Publication $publication */
$citations = $publication->getData('citations') ?? [];
$citationsToCheck = [];
foreach ($citations as $citation) { /** @var Citation $citation */
if (!$citation->getData('doi')) {
$citationsToCheck[$citation->getId()] = $citation;
}
}
if (empty($citationsToCheck)) {
continue;
}
$matchedReferences = $this->getResolvedRefs($publication->getDoi(), $context->getId());
if ($matchedReferences === null) {
$apiCallFailed = true;
continue;
}
foreach ($matchedReferences as $matchedReference) {
$key = $matchedReference['key'] ?? null;
if ($key === null || !isset($citationsToCheck[$key])) {
continue;
}
$citation = $citationsToCheck[$key];
$citation->setData('doi', $matchedReference['doi']);
Repo::citation()->edit($citation, []);
}
}
// Only remove the auto check setting if all API calls succeeded,
// so the submission will be retried in the next scheduled task run on failure.
if (!$apiCallFailed) {
$submission->setData($this->getAutoCheckSettingName(), null);
Repo::submission()->edit($submission, []);
}
}
}
/**
* Retrieve the submission's publications eligible for citation DOI matching.
*
* @return Publication[]
*/
protected function getPublicationsToCheck(Submission $submission, Context $context): array
{
$publicationsToCheck = [];
if (!$context->getData(Context::SETTING_DOI_VERSIONING)) {
$publication = $submission->getCurrentPublication();
if ($publication->getDoi() && $publication->getData('status') == Publication::STATUS_PUBLISHED) {
$publicationsToCheck[] = $publication;
}
} else {
$latestMinorPublications = Repo::doi()->getLatestMinorPublicationsForDoiDeposit($submission->getData('publications'));
foreach ($latestMinorPublications as $publicationsByMajor) {
foreach ($publicationsByMajor as $publication) {
$publicationsToCheck[] = $publication;
}
}
}
return $publicationsToCheck;
}
/**
* Query the Crossref API for matched citation DOIs for the given article DOI.
*/
protected function getResolvedRefs(string $doi, int $contextId): ?array
{
$matchedReferences = null;
$username = $this->plugin->getSetting($contextId, 'username');
$password = $this->plugin->getSetting($contextId, 'password');
// Use a different endpoint for testing and production.
$isTestMode = $this->plugin->getSetting($contextId, 'testMode') == 1;
$endpoint = ($isTestMode ? CrossrefPlugin::CROSSREF_API_REFS_URL_DEV : CrossrefPlugin::CROSSREF_API_REFS_URL);
$url = $endpoint . '?doi=' . urlencode($doi) . '&usr=' . urlencode($username) . '&pwd=' . urlencode($password);
$httpClient = Application::get()->getHttpClient();
try {
$response = $httpClient->request('POST', $url);
} catch (\GuzzleHttp\Exception\RequestException $e) {
error_log('Crossref getResolvedRefs failed for DOI ' . $doi . ': ' . $e->getMessage());
return null;
}
if ($response?->getStatusCode() == 200) {
$response = json_decode($response->getBody(), true);
$matchedReferences = $response['matched-references'] ?? [];
}
return $matchedReferences;
}
}