-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPreprintCrossrefXmlFilter.php
More file actions
381 lines (338 loc) · 17.2 KB
/
PreprintCrossrefXmlFilter.php
File metadata and controls
381 lines (338 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
<?php
/**
* @file plugins/generic/crossref/filter/PreprintCrossrefXmlFilter.php
*
* Copyright (c) 2014-2025 Simon Fraser University
* Copyright (c) 2000-2025 John Willinsky
* Distributed under The MIT License. For full terms see the file LICENSE.
*
* @class PreprintCrossrefXmlFilter
*
* @brief Class that converts a Preprint to a Crossref XML document.
*/
namespace APP\plugins\generic\crossref\filter;
use APP\core\Application;
use APP\plugins\generic\crossref\CrossrefExportDeployment;
use APP\publication\Publication;
use PKP\context\Context;
use DOMDocument;
use PKP\i18n\LocaleConversion;
use PKP\submission\PKPSubmission;
class PreprintCrossrefXmlFilter extends \PKP\plugins\importexport\native\filter\NativeExportFilter
{
/**
* Constructor
*
* @param \PKP\filter\FilterGroup $filterGroup
*/
public function __construct($filterGroup)
{
$this->setDisplayName('Crossref XML preprint export');
parent::__construct($filterGroup);
}
//
// Implement template methods from Filter
//
/**
* @see Filter::process()
*
* @param array $pubObjects Array of Issues or Submissions
*
* @return \DOMDocument
*/
public function &process(&$pubObjects)
{
// Create the XML document
$doc = new DOMDocument('1.0', 'utf-8');
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$deployment = $this->getDeployment();
$context = $deployment->getContext();
// Create the root node
$rootNode = $this->createRootNode($doc);
$doc->appendChild($rootNode);
// Create and append the 'head' node and all parts inside it
$rootNode->appendChild($this->createHeadNode($doc));
// Create and append the 'body' node, that contains everything
$bodyNode = $doc->createElementNS($deployment->getNamespace(), 'body');
$rootNode->appendChild($bodyNode);
$doiVersioning = (bool) ($context->getData(Context::SETTING_DOI_VERSIONING) ?? true);
foreach ($pubObjects as $pubObject) {
if (!$doiVersioning) {
$publications = [$pubObject->getCurrentPublication()];
} else {
$publications = $pubObject->getData('publications')->toArray();
// Use array reverse so that the latest version of the submission is first in the xml output and the DOI relations do not cause an error with Crossref
$publications = array_reverse($publications, true);
}
foreach ($publications as $publication) {
if ($publication->getDoi() && $publication->getData('status') === PKPSubmission::STATUS_PUBLISHED) {
$postedContentNode = $this->createPostedContentNode($doc, $publication, $pubObject);
$bodyNode->appendChild($postedContentNode);
}
}
}
return $doc;
}
//
// Submission conversion functions
//
/**
* Create and return the root node 'doi_batch'.
*
* @param \DOMDocument $doc
*
* @return \DOMElement
*/
public function createRootNode($doc)
{
/** @var CrossrefExportDeployment $deployment */
$deployment = $this->getDeployment();
$rootNode = $doc->createElementNS($deployment->getNamespace(), $deployment->getRootElementName());
$rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', $deployment->getXmlSchemaInstance());
$rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:jats', $deployment->getJATSNamespace());
$rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ai', $deployment->getAINamespace());
$rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:rel', $deployment->getRELNamespace());
$rootNode->setAttribute('version', $deployment->getXmlSchemaVersion());
$rootNode->setAttribute('xsi:schemaLocation', $deployment->getNamespace() . ' ' . $deployment->getSchemaFilename());
return $rootNode;
}
/**
* Create and return the head node 'head'.
*
* @param \DOMDocument $doc
*
* @return \DOMElement
*/
public function createHeadNode($doc)
{
/** @var CrossrefExportDeployment $deployment */
$deployment = $this->getDeployment();
$context = $deployment->getContext();
$plugin = $deployment->getPlugin();
$headNode = $doc->createElementNS($deployment->getNamespace(), 'head');
$headNode->appendChild($doc->createElementNS($deployment->getNamespace(), 'doi_batch_id', htmlspecialchars($context->getData('acronym', $context->getPrimaryLocale()) . '_' . time(), ENT_COMPAT, 'UTF-8')));
$timestamp = (new \DateTime())->format('YmdHisv');
$headNode->appendChild($doc->createElementNS($deployment->getNamespace(), 'timestamp', $timestamp));
$depositorNode = $doc->createElementNS($deployment->getNamespace(), 'depositor');
$depositorName = $plugin->getSetting($context->getId(), 'depositorName');
if (empty($depositorName)) {
$depositorName = $context->getData('supportName');
}
$depositorEmail = $plugin->getSetting($context->getId(), 'depositorEmail');
if (empty($depositorEmail)) {
$depositorEmail = $context->getData('supportEmail');
}
$depositorNode->appendChild($doc->createElementNS($deployment->getNamespace(), 'depositor_name', htmlspecialchars($depositorName, ENT_COMPAT, 'UTF-8')));
$depositorNode->appendChild($doc->createElementNS($deployment->getNamespace(), 'email_address', htmlspecialchars($depositorEmail, ENT_COMPAT, 'UTF-8')));
$headNode->appendChild($depositorNode);
$publisherServer = $context->getLocalizedName();
$headNode->appendChild($doc->createElementNS($deployment->getNamespace(), 'registrant', htmlspecialchars($publisherServer, ENT_COMPAT, 'UTF-8')));
return $headNode;
}
/**
* Create and return the posted content node 'posted_content'.
*
* @param \DOMDocument $doc
* @param Publication $publication
*
* @return \DOMElement
*/
public function createPostedContentNode($doc, $publication, $submission)
{
assert($publication instanceof Publication);
/** @var CrossrefExportDeployment $deployment */
$deployment = $this->getDeployment();
$request = Application::get()->getRequest();
$locale = $publication->getData('locale');
$postedContentNode = $doc->createElementNS($deployment->getNamespace(), 'posted_content');
$postedContentNode->setAttribute('type', 'preprint');
$postedContentNode->setAttribute('language', LocaleConversion::getIso1FromLocale($locale));
// contributors
$authors = $publication->getData('authors');
if ($authors->count() != 0) {
$contributorsNode = $doc->createElementNS($deployment->getNamespace(), 'contributors');
$isFirst = true;
foreach ($authors as $author) {
$personNameNode = $doc->createElementNS($deployment->getNamespace(), 'person_name');
$personNameNode->setAttribute('contributor_role', 'author');
if ($isFirst) {
$personNameNode->setAttribute('sequence', 'first');
} else {
$personNameNode->setAttribute('sequence', 'additional');
}
$familyNames = $author->getFamilyName(null);
$givenNames = $author->getGivenName(null);
// Check if both givenName and familyName is set for the submission language.
if (!empty($familyNames[$locale]) && !empty($givenNames[$locale])) {
$personNameNode->setAttribute('language', LocaleConversion::getIso1FromLocale($locale));
$personNameNode->appendChild($doc->createElementNS($deployment->getNamespace(), 'given_name', htmlspecialchars($givenNames[$locale], ENT_COMPAT, 'UTF-8')));
$personNameNode->appendChild($doc->createElementNS($deployment->getNamespace(), 'surname', htmlspecialchars($familyNames[$locale], ENT_COMPAT, 'UTF-8')));
if ($author->getData('orcid')) {
$personNameNode->appendChild($doc->createElementNS($deployment->getNamespace(), 'ORCID', $author->getData('orcid')));
}
$hasAltName = false;
foreach ($familyNames as $otherLocal => $familyName) {
if ($otherLocal != $locale && isset($familyName) && !empty($familyName)) {
if (!$hasAltName) {
$altNameNode = $doc->createElementNS($deployment->getNamespace(), 'alt-name');
$personNameNode->appendChild($altNameNode);
$hasAltName = true;
}
$nameNode = $doc->createElementNS($deployment->getNamespace(), 'name');
$nameNode->setAttribute('language', LocaleConversion::getIso1FromLocale($otherLocal));
$nameNode->appendChild($doc->createElementNS($deployment->getNamespace(), 'surname', htmlspecialchars($familyName, ENT_COMPAT, 'UTF-8')));
if (isset($givenNames[$otherLocal]) && !empty($givenNames[$otherLocal])) {
$nameNode->appendChild($doc->createElementNS($deployment->getNamespace(), 'given_name', htmlspecialchars($givenNames[$otherLocal], ENT_COMPAT, 'UTF-8')));
}
$altNameNode->appendChild($nameNode);
}
}
} else {
$personNameNode->appendChild($doc->createElementNS($deployment->getNamespace(), 'surname', htmlspecialchars($givenNames[$locale], ENT_COMPAT, 'UTF-8')));
if ($author->getData('orcid')) {
$personNameNode->appendChild($doc->createElementNS($deployment->getNamespace(), 'ORCID', $author->getData('orcid')));
}
}
$contributorsNode->appendChild($personNameNode);
$isFirst = false;
}
$postedContentNode->appendChild($contributorsNode);
}
// Titles
$titlesNode = $doc->createElementNS($deployment->getNamespace(), 'titles');
$titlesNode->appendChild($node = $doc->createElementNS($deployment->getNamespace(), 'title'));
$node->appendChild($doc->createTextNode($publication->getLocalizedTitle($submission->getLocale(), 'html')));
if ($subtitle = $publication->getLocalizedSubTitle($submission->getLocale(), 'html')) {
$titlesNode->appendChild($node = $doc->createElementNS($deployment->getNamespace(), 'subtitle'));
$node->appendChild($doc->createTextNode($subtitle));
}
$postedContentNode->appendChild($titlesNode);
// Posted date
$postedContentNode->appendChild($this->createPostedDateNode($doc, $publication->getData('datePublished')));
// abstract
$abstracts = $publication->getData('abstract') ?: [];
foreach($abstracts as $lang => $abstract) {
$abstractNode = $doc->createElementNS($deployment->getJATSNamespace(), 'jats:abstract');
$abstractNode->setAttributeNS($deployment->getXMLNamespace(), 'xml:lang', LocaleConversion::getIso1FromLocale($lang));
$abstractNode->appendChild($doc->createElementNS($deployment->getJATSNamespace(), 'jats:p', htmlspecialchars(html_entity_decode(strip_tags($abstract), ENT_COMPAT, 'UTF-8'), ENT_COMPAT, 'utf-8')));
$postedContentNode->appendChild($abstractNode);
}
// license
if ($publication->getData('licenseUrl')) {
$licenseNode = $doc->createElementNS($deployment->getAINamespace(), 'ai:program');
$licenseNode->setAttribute('name', 'AccessIndicators');
$licenseNode->appendChild($doc->createElementNS($deployment->getAINamespace(), 'ai:license_ref', htmlspecialchars($publication->getData('licenseUrl'), ENT_COMPAT, 'UTF-8')));
$postedContentNode->appendChild($licenseNode);
}
// DOI relations: if this version has a vorDoi or different DOI than the current publication (i.e. versions and DOI versioning exits), add a relation node
$parentDoi = $submission->getCurrentPublication()->getDoi() && $submission->getCurrentPublication()->getDoi() != $publication->getDoi() ? $submission->getCurrentPublication()->getDoi() : '';
$vorDoi = $publication->getData('vorDoi') ? $publication->getData('vorDoi') : '';
if ($parentDoi || $vorDoi) {
$relationsDataNode = $doc->createElementNS($deployment->getRELNamespace(), 'rel:program');
$relationsDataNode->setAttribute('name', 'relations');
if ($parentDoi) {
$relationsDataNode->appendChild($this->createParentDoiNode($doc, $parentDoi));
}
if ($vorDoi) {
$relationsDataNode->appendChild($this->createVorDoiNode($doc, $vorDoi));
}
$postedContentNode->appendChild($relationsDataNode);
}
// DOI data
$dispatcher = $this->_getDispatcher($request);
$url = $dispatcher->url($request, Application::ROUTE_PAGE, null, 'preprint', 'view', [$submission->getBestId(), 'version', $publication->getId()], null, null, true);
$postedContentNode->appendChild($this->createDOIDataNode($doc, $publication->getDoi(), $url));
return $postedContentNode;
}
/**
* Create and return the posted date node 'posted_date'.
*
* @param \DOMDocument $doc
* @param string $objectPostedDate
*
* @return \DOMElement
*/
public function createPostedDateNode($doc, $objectPostedDate)
{
$deployment = $this->getDeployment();
$postedDate = strtotime($objectPostedDate);
$postedDateNode = $doc->createElementNS($deployment->getNamespace(), 'posted_date');
if (date('m', $postedDate)) {
$postedDateNode->appendChild($doc->createElementNS($deployment->getNamespace(), 'month', date('m', $postedDate)));
}
if (date('d', $postedDate)) {
$postedDateNode->appendChild($doc->createElementNS($deployment->getNamespace(), 'day', date('d', $postedDate)));
}
$postedDateNode->appendChild($doc->createElementNS($deployment->getNamespace(), 'year', date('Y', $postedDate)));
return $postedDateNode;
}
/**
* Create and return the DOI data node 'doi_data'.
*
* @param \DOMDocument $doc
* @param string $doi
* @param string $url
*
* @return \DOMElement
*/
public function createDOIDataNode($doc, $doi, $url)
{
$deployment = $this->getDeployment();
$doiDataNode = $doc->createElementNS($deployment->getNamespace(), 'doi_data');
$doiDataNode->appendChild($doc->createElementNS($deployment->getNamespace(), 'doi', htmlspecialchars($doi, ENT_COMPAT, 'UTF-8')));
$doiDataNode->appendChild($doc->createElementNS($deployment->getNamespace(), 'resource', htmlspecialchars($url, ENT_COMPAT, 'UTF-8')));
return $doiDataNode;
}
/**
* Create and return the parent DOI relation node.
*
* @param \DOMDocument $doc
* @param string $parentDoi
*
* @return \DOMElement
*/
public function createParentDoiNode($doc, $parentDoi)
{
/** @var CrossrefExportDeployment $deployment */
$deployment = $this->getDeployment();
$parentDoiNode = $doc->createElementNS($deployment->getRELNamespace(), 'rel:related_item');
$intraWorkRelationNode = $doc->createElementNS($deployment->getRELNamespace(), 'rel:intra_work_relation', htmlspecialchars($parentDoi, ENT_COMPAT, 'UTF-8'));
$intraWorkRelationNode->setAttribute('relationship-type', 'isVersionOf');
$intraWorkRelationNode->setAttribute('identifier-type', 'doi');
$parentDoiNode->appendChild($intraWorkRelationNode);
return $parentDoiNode;
}
/**
* Create and return the VOR DOI relation node.
*
* @param \DOMDocument $doc
* @param string $vorDoi
*
* @return \DOMElement
*/
public function createVorDoiNode($doc, $vorDoi)
{
/** @var CrossrefExportDeployment $deployment */
$deployment = $this->getDeployment();
$vorDoiNode = $doc->createElementNS($deployment->getRELNamespace(), 'rel:related_item');
$intraWorkRelationNode = $doc->createElementNS($deployment->getRELNamespace(), 'rel:intra_work_relation', htmlspecialchars($vorDoi, ENT_COMPAT, 'UTF-8'));
$intraWorkRelationNode->setAttribute('relationship-type', 'isPreprintOf');
$intraWorkRelationNode->setAttribute('identifier-type', 'doi');
$vorDoiNode->appendChild($intraWorkRelationNode);
return $vorDoiNode;
}
/**
* Helper to ensure dispatcher is available even when called from CLI tools
*
*
*/
protected function _getDispatcher(\APP\core\Request $request): \PKP\core\Dispatcher
{
$dispatcher = $request->getDispatcher();
if ($dispatcher === null) {
$dispatcher = Application::get()->getDispatcher();
}
return $dispatcher;
}
}