-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelationsPlugin.php
More file actions
516 lines (449 loc) · 17.2 KB
/
Copy pathRelationsPlugin.php
File metadata and controls
516 lines (449 loc) · 17.2 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
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<?php
/**
* @file plugins/generic/relations/RelationsPlugin.php
*
* Copyright (c) 2014-2026 Simon Fraser University
* Copyright (c) 2003-2026 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class RelationsPlugin
*
* @ingroup plugins_generic_relations
*
* @brief This plugin adds a "Relations" field to the publication metadata form, allowing users to specify relationships between publications (e.g. "is translation of", "has preprint", etc.). The relations are stored in the database and can be exported to Crossref and DataCite XML.
*/
namespace APP\plugins\generic\relations;
use APP\core\Application;
use APP\facades\Repo;
use APP\template\TemplateManager;
use PKP\core\PKPApplication;
use PKP\dataCitation\pid\PidResolver;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
use PKP\oai\OAIRecord;
class RelationsPlugin extends GenericPlugin
{
/**
* @copydoc Plugin::register()
*
* @param null|mixed $mainContextId
*/
public function register($category, $path, $mainContextId = null)
{
if (parent::register($category, $path, $mainContextId)) {
if ($this->getEnabled($mainContextId)) {
Hook::add('Schema::get::publication', $this->addRelationsToPublicationSchema(...));
Hook::add('Publication::validate', $this->validateRelations(...));
Hook::add('articlecrossrefxmlfilter::execute', $this->addCrossrefElement(...));
Hook::add('datacitexmlfilter::execute', $this->addDataCiteElement(...));
Hook::add('JatsTemplatePlugin::jats', $this->addJatsElement(...));
Hook::add('Templates::Article::Main', $this->displayRelations(...));
// Registering build file for JS to be loaded
$request = Application::get()->getRequest();
$templateMgr = TemplateManager::getManager($request);
$this->addJavaScript($request, $templateMgr);
}
return true;
}
return false;
}
/**
* Get the display name of this plugin
*
* @return string
*/
public function getDisplayName(): string
{
return __('plugins.generic.relations.name');
}
/**
* Get the description of this plugin
*
* @return string
*/
public function getDescription(): string
{
return __('plugins.generic.relations.description');
}
/**
* Add a JavaScript file to the backend interface.
*
* @param \PKP\core\Request $request The current request
* @param TemplateManager $templateMgr Template manager instance
*
* @return void
*/
public function addJavaScript($request, $templateMgr): void
{
$templateMgr->addJavaScript(
'relations',
"{$request->getBaseUrl()}/{$this->getPluginPath()}/public/build/build.iife.js",
[
'inline' => false,
'contexts' => ['backend'],
'priority' => TemplateManager::STYLE_SEQUENCE_LAST
]
);
}
/**
* Add relations to the publication schema.
*
* @param string $hookName `Schema::get::publication`
* @param array $params
*
* @return bool
*/
public function addRelationsToPublicationSchema($hookName, $params): bool
{
$schema = &$params[0];
$schema->properties->{'relations'} = (object) [
'type' => 'array',
'items' => (object) [
'type' => 'object',
'properties' => (object) [
'relationType' => (object) ['type' => 'string'],
'identifierType' => (object) ['type' => 'string'],
'value' => (object) ['type' => 'string'],
],
],
'apiSummary' => false,
'validation' => ['nullable'],
];
return false;
}
/**
* Validate the relations field on publication save.
*
* @hook Publication::validate [[&$errors, $publication, $props, $allowedLocales, $primaryLocale]]
*/
public function validateRelations(string $hookName, array $params): bool
{
$errors = &$params[0];
$props = $params[2];
if (!isset($props['relations'])) {
return false;
}
foreach ($props['relations'] as $index => $relation) {
$identifierType = $relation['identifierType'] ?? null;
$value = $relation['value'] ?? null;
if (!$identifierType || !$value) {
continue;
}
$pidClass = PidResolver::resolveByIdentifierType($identifierType);
if (!$pidClass) {
continue;
}
$parsed = $pidClass::extractFromString($value) ?: $pidClass::removePrefix($value);
if (!$pidClass::isValid($parsed)) {
$errors["relations.{$index}.value"][] = __('plugins.generic.relations.validation.invalidValue', [
'type' => $identifierType,
'value' => $value,
]);
}
}
return false;
}
/**
* Get the relation types supported by this plugin.
*
* @return array
*/
protected function getRelationTypes(): array {
return [
['value' => 'hasPreprint', 'label' => 'hasPreprint'],
['value' => 'isTranslationOf', 'label' => 'isTranslationOf'],
['value' => 'hasTranslation', 'label' => 'hasTranslation'],
['value' => 'isReviewOf', 'label' => 'isReviewOf'],
['value' => 'hasReview', 'label' => 'hasReview'],
['value' => 'isCommentOn', 'label' => 'isCommentOn'],
['value' => 'hasComment', 'label' => 'hasComment'],
];
}
/**
* Get the identifier types supported by this plugin.
*
* @return array
*/
protected function getIdentifierTypes(): array {
return [
['value' => 'DOI', 'label' => 'DOI'],
['value' => 'ISSN', 'label' => 'ISSN'],
['value' => 'ISBN', 'label' => 'ISBN'],
['value' => 'URI', 'label' => 'URI'],
['value' => 'Handle', 'label' => 'Handle'],
['value' => 'ARXIV', 'label' => 'arXiv'],
['value' => 'PMID', 'label' => 'PMID'],
];
}
/**
* Map our identifierType values to Crossref identifier-type attribute values.
*
* @param string $identifierType The identifier type to map
*
* @return string
*/
protected function getCrossrefIdentifierType(string $identifierType): string
{
return match (strtoupper($identifierType)) {
'DOI' => 'doi',
'ISSN' => 'issn',
'ISBN' => 'isbn',
'URI' => 'uri',
'HANDLE' => 'handle',
'ARXIV' => 'arxiv',
'PMID' => 'pmid',
default => throw new \UnexpectedValueException("Unsupported identifier type: {$identifierType}"),
};
}
/**
* Map our identifierType values to DataCite relatedIdentifierType attribute values.
*
* @param string $identifierType The identifier type to map
*
* @return string
*/
protected function getDataCiteIdentifierType(string $identifierType): string
{
return match (strtoupper($identifierType)) {
'DOI' => 'DOI',
'ISSN' => 'ISSN',
'ISBN' => 'ISBN',
'URI' => 'URL',
'HANDLE' => 'Handle',
'ARXIV' => 'arXiv',
'PMID' => 'PMID',
default => throw new \UnexpectedValueException("Unsupported identifier type: {$identifierType}"),
};
}
/**
* Map our relationType values to DataCite relationType attribute values.
*
* @param string $relationType The relation type to map
*
* @return string
*/
protected function getDataCiteRelationType(string $relationType): string
{
return match ($relationType) {
'hasPreprint' => 'HasPreprint',
'isTranslationOf' => 'IsVariantFormOf',
'hasTranslation' => 'HasVariantForm',
'isReviewOf' => 'Reviews',
'isCommentOn' => 'References',
'hasComment' => 'IsReferencedBy',
default => 'References',
};
}
/**
* Add <rel:program> relations block to Crossref XML export.
*
* @hook articlecrossrefxmlfilter::execute [[&$preliminaryOutput]]
*
* @param string $hookName The name of the hook
* @param array $params The parameters passed to the hook
*
* @return bool
*/
public function addCrossrefElement(string $hookName, array $params): bool
{
$preliminaryOutput = &$params[0];
$request = Application::get()->getRequest();
$context = $request->getContext();
if (!$context) {
return false;
}
$relationsNS = 'http://www.crossref.org/relations.xsd';
$rootNode = $preliminaryOutput->documentElement;
$rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:rel', $relationsNS);
$articleNodes = $preliminaryOutput->getElementsByTagName('journal_article');
foreach ($articleNodes as $articleNode) {
$doiDataNode = $articleNode->getElementsByTagName('doi_data')->item(0);
$doiNode = $doiDataNode->getElementsByTagName('doi')->item(0);
$doi = $doiNode->nodeValue;
$submission = Repo::submission()->getByDoi($doi, $context->getId());
if (!$submission) {
continue;
}
$publication = $submission->getCurrentPublication();
$relations = $publication->getData('relations') ?? [];
if (empty($relations)) {
continue;
}
$programNode = $preliminaryOutput->createElementNS($relationsNS, 'rel:program');
$programNode->setAttribute('name', 'relations');
foreach ($relations as $relation) {
$relationType = $relation['relationType'] ?? null;
$identifierType = $relation['identifierType'] ?? null;
$value = $relation['value'] ?? null;
if (!$relationType || !$identifierType || !$value) {
continue;
}
$relatedItemNode = $preliminaryOutput->createElementNS($relationsNS, 'rel:related_item');
$elementName = $this->getCrossrefRelationElement($relationType);
$relationNode = $preliminaryOutput->createElementNS(
$relationsNS,
$elementName,
htmlspecialchars($value, ENT_COMPAT, 'UTF-8')
);
$relationNode->setAttribute('relationship-type', $relationType);
$relationNode->setAttribute('identifier-type', $this->getCrossrefIdentifierType($identifierType));
$relatedItemNode->appendChild($relationNode);
$programNode->appendChild($relatedItemNode);
}
if ($programNode->hasChildNodes()) {
$articleNode->insertBefore($programNode, $doiDataNode);
}
}
return false;
}
/**
* Get the appropriate Crossref relation element based on the relation type.
*
* @param string $relationType The relation type
*
* @return string The corresponding Crossref relation element
*/
protected function getCrossrefRelationElement(string $relationType): string
{
return match ($relationType) {
'hasPreprint',
'isTranslationOf',
'hasTranslation' => 'rel:intra_work_relation',
default => 'rel:inter_work_relation',
};
}
/**
* Add <relatedIdentifiers> block to DataCite XML export.
*
* @hook datacitexmlfilter::execute [[&$preliminaryOutput]]
*
* @param string $hookName The name of the hook
* @param array $params The parameters passed to the hook
*
* @return bool
*/
public function addDataCiteElement(string $hookName, array $params): bool
{
$preliminaryOutput = &$params[0];
$dataciteNS = 'http://datacite.org/schema/kernel-4';
$rootNode = $preliminaryOutput->documentElement;
$alternateIdentifierNodes = $preliminaryOutput->getElementsByTagName('alternateIdentifier');
foreach ($alternateIdentifierNodes as $alternateIdentifierNode) {
if ($alternateIdentifierNode->getAttribute('alternateIdentifierType') !== 'publisherId') {
continue;
}
$idsArray = explode('-', $alternateIdentifierNode->nodeValue);
if (count($idsArray) !== 3) {
continue;
}
$submissionId = (int) $idsArray[2];
$submission = Repo::submission()->get($submissionId);
if (!$submission) {
continue;
}
$publication = $submission->getCurrentPublication();
$relations = $publication->getData('relations') ?? [];
if (empty($relations)) {
continue;
}
$relatedIdentifiersNode = $preliminaryOutput->createElementNS($dataciteNS, 'relatedIdentifiers');
foreach ($relations as $relation) {
$relationType = $relation['relationType'] ?? null;
$identifierType = $relation['identifierType'] ?? null;
$value = $relation['value'] ?? null;
if (!$relationType || !$identifierType || !$value) {
continue;
}
$relatedIdentifierNode = $preliminaryOutput->createElementNS(
$dataciteNS,
'relatedIdentifier',
htmlspecialchars($value, ENT_COMPAT, 'UTF-8')
);
$relatedIdentifierNode->setAttribute(
'relatedIdentifierType',
$this->getDataCiteIdentifierType($identifierType)
);
$relatedIdentifierNode->setAttribute(
'relationType',
$this->getDataCiteRelationType($relationType)
);
$relatedIdentifiersNode->appendChild($relatedIdentifierNode);
}
if ($relatedIdentifiersNode->hasChildNodes()) {
$rootNode->appendChild($relatedIdentifiersNode);
}
}
return false;
}
/**
* Add <related-article> elements to JATS XML export.
*
* @hook JatsTemplatePlugin::jats [[&$doc]]
*
* @param string $hookName The name of the hook
* @param OAIRecord $record The OAI record being exported
* @param \DOMDocument $doc The DOMDocument representing the JATS XML
*
* @return bool
*/
public function addJatsElement(string $hookName, OAIRecord $record, \DOMDocument $doc): bool
{
$submission = $record->getData('article');
$publication = $submission->getCurrentPublication();
$relations = $publication->getData('relations') ?? [];
if (empty($relations)) {
return Hook::CONTINUE;
}
$xpath = new \DOMXPath($doc);
$articleMetaNodes = $xpath->query('//article/front/article-meta');
if (!$articleMetaNodes->length) {
return Hook::CONTINUE;
}
$articleMeta = $articleMetaNodes->item(0);
foreach ($relations as $relation) {
$relationType = $relation['relationType'] ?? null;
$identifierType = $relation['identifierType'] ?? null;
$value = $relation['value'] ?? null;
if (!$relationType || !$identifierType || !$value) {
continue;
}
$relatedArticleNode = $doc->createElement('related-article');
$relatedArticleNode->setAttribute('related-article-type', $relationType);
$relatedArticleNode->setAttribute('ext-link-type', strtolower($identifierType));
$relatedArticleNode->setAttributeNS(
'http://www.w3.org/1999/xlink',
'xlink:href',
$value
);
$articleMeta->appendChild($relatedArticleNode);
}
return Hook::CONTINUE;
}
/**
* Display the relations on the publication view page.
*
* @hook Templates::Article::Main [[&$smarty, &$output]]
*
* @param string $hookName The name of the hook
* @param array $params The parameters passed to the hook
*
* @return bool
*/
public function displayRelations(string $hookName, array $params): bool
{
$smarty = &$params[1];
$output = &$params[2];
$submission = $smarty->getTemplateVars('article');
if (!$submission) {
return Hook::CONTINUE;
}
$publication = $submission->getCurrentPublication();
$relations = $publication->getData('relations') ?? [];
if (empty($relations)) {
return Hook::CONTINUE;
}
$smarty->assign('relations', $relations);
$output .= $smarty->fetch($this->getTemplateResource('relationsList.tpl'));
return Hook::CONTINUE;
}
}