-
Notifications
You must be signed in to change notification settings - Fork 8
/
SwordServerPlugin.inc.php
392 lines (357 loc) · 11.2 KB
/
SwordServerPlugin.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
<?php
/**
* @file SwordServerPlugin.inc.php
*
* Copyright (c) 2014-2020 Simon Fraser University
* Copyright (c) 2003-2020 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file LICENSE.
*
* @class SwordServerPlugin
* @ingroup plugins_gateways_swordserver
*
* @brief Sword gateway plugin
*/
import('lib.pkp.classes.plugins.GatewayPlugin');
import('lib.pkp.classes.security.authorization.PolicySet');
import('lib.pkp.classes.submission.Genre');
import('classes.publication.Publication');
require __DIR__ . '/ServiceDocument.inc.php';
require __DIR__ . '/DepositReceipt.inc.php';
require __DIR__ . '/SwordStatement.inc.php';
require __DIR__ . '/SwordSubmissionFileManager.inc.php';
require __DIR__ . '/SwordServerAccessPolicy.inc.php';
require __DIR__ . '/SwordError.inc.php';
class SwordServerPlugin extends GatewayPlugin {
/**
* @copydoc Plugin::__construct()
*/
function __construct() {
parent::__construct();
$this->_endpoints = [
[
'pattern' => 'servicedocument',
'method' => 'GET',
'handler' => [$this, 'serviceDocument']
],
[
'pattern' => 'sections/{id}',
'method' => 'POST',
'handler' => [$this, 'deposit']
],
[
'pattern' => 'submissions/{id}/statement',
'method' => 'GET',
'handler' => [$this, 'statement']
]
];
}
/**
* @copydoc Plugin::register()
*/
function register($category, $path, $mainContextId = null) {
$success = parent::register($category, $path, $mainContextId);
$this->addLocaleData();
return $success;
}
/**
* Get the name of the settings file to be installed on new journal
* creation.
* @return string
*/
function getContextSpecificPluginSettingsFile() {
return $this->getPluginPath() . '/settings.xml';
}
/**
* @copydoc Plugin::getName()
*/
function getName() {
return 'swordserver';
}
/**
* @copydoc Plugin::getDisplayName()
*/
function getDisplayName() {
return __('plugins.gateways.swordserver.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
function getDescription() {
return __('plugins.gateways.swordserver.description');
}
/**
* @copydoc GatewayPlugin::getPolicies()
*/
function getPolicies($request) {
yield new SwordServerAccessPolicy($request);
}
/**
* Serve a SWORD Service Document
*/
function serviceDocument() {
$journal = $this->request->getJournal();
$journalId = $journal->getId();
$sectionDAO = DAORegistry::getDAO('SectionDAO');
$resultSet = $sectionDAO->getByJournalId($journalId);
$sections = $resultSet->toAssociativeArray();
$serviceDocument = new ServiceDocument(
$journal,
$sections,
$this->request->getRequestUrl()
);
header('Content-Type: application/xml');
echo $serviceDocument->saveXML();
exit;
}
/**
* Handle a SWORD Deposit request
*
* @param $opts array
*/
function deposit($opts) {
$headers = getallheaders();
$sectionId = intval($opts['id']);
$locale = Locale::getDefault();
$submissionDAO = Application::getSubmissionDAO();
$submission = $submissionDAO->newDataObject();
$submission->setContextId($this->request->getJournal()->getId());
$submission->setDateSubmitted (Core::getCurrentDate());
$submission->setLastModified (Core::getCurrentDate());
$submission->setSubmissionProgress(0);
$submission->setStageId(WORKFLOW_STAGE_ID_SUBMISSION);
$submission->setSectionId($sectionId);
$submission->setLocale($locale);
$uploadDir = ini_get('upload_tmp_dir') . '/' . uniqid();
mkdir($uploadDir, DIRECTORY_MODE_MASK);
$uploadedFilePath = $uploadDir . '/sword_upload.dat';
file_put_contents($uploadedFilePath, file_get_contents('php://input'));
if ($headers['Content-Type'] == 'application/zip' && $headers['Packaging'] == "http://purl.org/net/sword/package/METSDSpaceSIP") {
$zip = new ZipArchive;
$res = $zip->open($uploadedFilePath);
if ($res) {
$zip->extractTo($uploadDir);
$zip->close();
} else {
throw new Exception('Zip extraction failed');
}
}
if (!file_exists($uploadDir . "/mets.xml")) {
throw new Exception('No mets.xml document in extracted Zip package');
}
$metsDoc = simplexml_load_file($uploadDir . "/mets.xml");
$metsDoc->registerXPathNamespace("mets", 'http://www.loc.gov/METS/');
$metsDoc->registerXPathNamespace("epdcx", "http://purl.org/eprint/epdcx/2006-11-16/");
$metsDoc->registerXPathNamespace("mods", "http://www.loc.gov/mods/v3");
$submissionData = [
'contextId' => $this->request->getJournal()->getId(),
'status' => STATUS_QUEUED
];
$publicationData = [
'status' => STATUS_QUEUED
];
$match = $metsDoc->xpath("//epdcx:statement[@epdcx:propertyURI='http://purl.org/dc/" .
"elements/1.1/title']/epdcx:valueString");
if (!empty($match)) {
$title = $match[0]->__toString();
$publicationData['title'] = ['en_US' => $title];
}
$submission->_data = $submissionData;
$submission = Services::get('submission')->add($submission, $this->request);
$publicationData['submissionId'] = $submission->getId();
$publication = DAORegistry::getDAO('PublicationDAO')->newDataObject();
$publication->_data = $publicationData;
$publication->setData('sectionId', $this->request->getJournal()->getId());
$publication->setData('status', STATUS_QUEUED);
if (!empty($match)) {
$title = $match[0]->__toString();
$publication->setData('title', $title, $locale);
}
$publication->setData('locale', $locale);
$publication = Services::get('publication')->add($publication, $this->request);
$submission = Services::get('submission')->edit($submission, ['currentPublicationId' => $publication->getId()], $this->request);
$nameNodes = $metsDoc->xpath("//mods:name[mods:role/mods:roleTerm='author' or mods:role/mods:roleTerm='pkp_primary_contact']");
$authorDAO = DAORegistry::getDAO("AuthorDAO");
foreach ($nameNodes as $nameNode) {
$nameNode->registerXPathNamespace("mods", "http://www.loc.gov/mods/v3");
$email = $nameNode->xpath("mods:nameIdentifier[@type='email']");
$given = $nameNode->xpath("mods:namePart[@type='given']");
$family = $nameNode->xpath("mods:namePart[@type='family']");
$primary = $nameNode->xpath("mods:role[mods:roleTerm='pkp_primary_contact']");
if (!empty($email) && (!empty($given) || !empty($family))) {
$author = $authorDAO->newDataObject();
$author->_data = [
'email' => $email[0]->__toString(),
'seq' => 1,
'publicationId' => $publication->getId(),
'userGroupId' => 14,
'includeInBrowse' => 1,
];
if (!empty($family)) {
$author->setData('familyName', $family[0]->__toString(), $locale);
}
if (!empty($given)) {
$author->setData('givenName', $given[0]->__toString(), $locale);
}
$author = Services::get('author')->add($author, $this->request);
if (!empty($primary)) {
$publication = Services::get('publication')->edit($publication, ['primaryContactId' => $author->getId()], $this->request);
}
}
}
$submissionFileManager = new SwordSubmissionFileManager($this->request->getJournal()->getId(), $submission->getId());
$hrefs = array_unique(
array_map(function($h) {
return $h['href']->__toString();
}, $metsDoc->xpath("//mets:FLocat[@LOCTYPE='URL']/@xlink:href")
)
);
foreach ($hrefs as $href) {
if (file_exists($uploadDir . "/" . $href)) {
$submissionFile = $submissionFileManager->uploadSubmissionFile(
$href, SUBMISSION_FILE_SUBMISSION, $this->user->getId(), null, GENRE_CATEGORY_SUPPLEMENTARY, null, null
);
}
}
// Attach the original package as well
$submissionFile = $submissionFileManager->uploadSubmissionFile(
'sword_upload.dat', SUBMISSION_FILE_SUBMISSION, $this->user->getId(), null, GENRE_CATEGORY_SUPPLEMENTARY, null, null
);
$serverHost = $this->request->_serverHost;
$requestPath = $this->request->_requestPath;
$editIri = $this->request->getRouter()->url($this->request,
null,
null,
null,
['swordserver', 'submissions', $submission->getId()]
);
$stmtIri = $this->request->getRouter()->url($this->request,
null,
null,
null,
['swordserver', 'submissions', $submission->getId(), 'statement']
);
$depositReceipt = new DepositReceipt(
[
'title' => $submission->getTitle($locale),
'edit-iri' => $editIri,
'stmt-iri' => $stmtIri,
]
);
$submissionFileManager->rmtree($uploadDir);
header('Content-Type: application/xml');
echo $depositReceipt->saveXML();
exit;
}
/**
* Handle a SWORD Statement request
*
* @param $opts array
*/
function statement($opts) {
$locale = Locale::getDefault();
$submissionDAO = Application::getSubmissionDAO();
$submission = $submissionDAO->getById($opts['id']);
$sectionDAO = DAORegistry::getDAO('SectionDAO');
$section = $sectionDAO->getById($submission->getSectionId());
$swordStatement = new SwordStatement(
[
'state_href' => $this->_getStateIRI(),
'state_description' => "Deposited to " . $section->getData('title', $locale),
]
);
header('Content-Type: application/xml');
echo $swordStatement->saveXML();
exit;
}
/**
* @copydoc GatewayPlugin::fetch()
*/
function fetch($args, $request) {
if (!$this->getEnabled()) {
return false;
}
$this->request = $request;
$handler = $request->_router->getHandler();
$this->user = $handler->getAuthorizedContextObject(ASSOC_TYPE_USER);
$method = $request->getRequestMethod();
$methodsAllowed = [];
foreach ($this->_endpoints as $endpoint) {
if ($opts = $this->_matchRoute($args, $endpoint['pattern'])) {
if ($method == $endpoint['method']) {
try {
call_user_func($endpoint['handler'], $opts);
} catch (Throwable $e) {
error_log("=============");
error_log($e->getMessage());
$swordError = new SwordError([
'summary' => "Application Error." . $e->__toString()
]);
header('Content-Type: application/xml');
header('HTTP/1.1 500 Not ServerError');
header('Allow ' . implode($methodsAllowed, ', '));
echo $swordError->saveXML();
exit;
}
break;
}
array_push($methodsAllowed, $endpoint['method']);
}
}
if (!empty($methodsAllowed)) {
$swordError = new SwordError([
'summary' => "Method not Allowed."
]);
header('Content-Type: application/xml');
header('HTTP/1.1 405 Not Allowed');
header('Allow ' . implode($methodsAllowed, ', '));
echo $swordError->saveXML();
exit;
} else {
$swordError = new SwordError([
'summary' => "Not found."
]);
header('Content-Type: application/xml');
header('HTTP/1.1 404 Not Found');
echo $swordError->saveXML();
exit;
}
}
/**
* Match Gateway::fetch @args to an endpoint
*
* @param $args array
* @param $test string
* @param $opts array
* @return boolean
*/
function _matchRoute($args, $test, $opts = []) {
if ($pos = strpos($test, '/')) {
$rest = substr($test, $pos);
$test = substr($test, 0, $pos);
} else {
$rest = false;
}
if (preg_match('/{([a-z]*)}/', $test, $sp)) {
$opts[$sp[1]] = array_shift($args);
} else if (array_shift($args) != $test) {
return false;
}
if ($rest) {
return $this->_matchRoute($args, substr($rest, 1), $opts);
} else if ($opts){
return $opts;
}
return true;
}
/**
* Get the IRI for the SWORD statement
*
* @return string SWORD Statement IRI
*/
function _getStateIRI() {
return $this->request->_protocol
. '://'
. $this->request->_serverHost
. $this->request->_requestPath;
}
}