forked from quoideneuf/swordServer
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSwordServerPlugin.php
460 lines (411 loc) · 14.9 KB
/
SwordServerPlugin.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
<?php
/**
* @file SwordServerPlugin.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2003-2021 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file LICENSE.
*
* @class SwordServerPlugin
* @brief Sword gateway plugin
*/
namespace APP\plugins\gateways\swordServer;
use PKP\plugins\GatewayPlugin;
use PKP\security\authorization\PolicySet;
use PKP\submission\Genre;
use PKP\linkAction\request\AjaxModal;
use PKP\linkAction\LinkAction;
use PKP\core\JSONMessage;
use PKP\db\DAORegistry;
use PKP\security\Role;
use PKP\core\Core;
use PKP\submission\PKPSubmission;
use PKP\submissionFile\SubmissionFile;
use APP\facades\Repo;
use APP\publication\Publication;
use APP\core\Application;
use APP\notification\NotificationManager;
use APP\core\Services;
use APP\plugins\gateways\swordServer\ServiceDocument;
use APP\plugins\gateways\swordServer\DepositReceipt;
use APP\plugins\gateways\swordServer\SwordStatement;
use APP\plugins\gateways\swordServer\SwordServerAccessPolicy;
use APP\plugins\gateways\swordServer\SwordError;
use APP\plugins\gateways\swordServer\SwordServerSettingsForm;
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->getContext();
$sections = iterator_to_array(Repo::section()->getCollector()->filterByContextIds([$journal->getId()])->getMany(), true);
// Exclude inactive sections
$sections = array_filter($sections, function($section) {
return !$section->getIsInactive();
});
if ($this->request->getUser()->hasRole(ROLE_ID_MANAGER, $journal->getId())) {
// This is not a managerial user; exclude editor restricted sections.
$sections = array_filter($sections, function($section) {
return !$section->getEditorRestricted();
});
}
$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) {
$journal = $this->request->getContext();
$locale = $journal->getPrimaryLocale();
$user = $this->request->getUser();
// Validate and fetch the target section for this deposit.
$section = Repo::section()->get(intval($opts['id']), $journal->getId());
$isManager = $user->hasRole(ROLE_ID_MANAGER, $journal->getId());
if (!$section || $section->getIsInactive() || (!$isManager && $section->getEditorRestricted())) {
throw new Exception('Unable to determine target section!');
}
$authorUserGroup = Repo::userGroup()->getCollector()->filterByContextIds([$journal->getId()])->filterByIsDefault(true)->filterByRoleIds([Role::ROLE_ID_AUTHOR])->getMany()->first();
if (!$authorUserGroup) throw new Exception('Unable to determine author user group!');
// Save the sword package to a local file.
$headers = getallheaders();
if ($headers['Content-Type'] != 'application/zip' || $headers['Packaging'] != 'http://purl.org/net/sword/package/METSDSpaceSIP') throw new Exception('Unknown content type or packaging.');
$zipPath = tempnam(sys_get_temp_dir(), 'sword');
$zipContents = file_get_contents('php://input');
file_put_contents($zipPath, $zipContents);
$zip = new \ZipArchive;
if (!$zip->open($zipPath)) throw new Exception('Unable to open zip file.');
// Load the metadata from the package.
$metsString = $zip->getFromName('mets.xml');
if ($metsString === false) throw new Exception('No mets.xml document in extracted Zip package');
$metsDoc = simplexml_load_string($metsString);
$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");
// Populate a Publication object
$publication = Repo::publication()->newDataObject();
$publication->setData('sectionId', $section->getId());
$publication->setData('status', PKPSubmission::STATUS_QUEUED);
// Populate a Submission object
$submission = Repo::submission()->newDataObject();
$submission->setData('contextId', $journal->getId());
$submission->setData('dateSubmitted', Core::getCurrentDate());
$submission->stampModified();
$submission->setData('submissionProgress', $isManager ? 0 : 1); // Force non-editor users to review submission steps.
$submission->setData('stageId', WORKFLOW_STAGE_ID_SUBMISSION);
$submission->setData('locale', $locale);
$submission->setData('status', STATUS_QUEUED);
Repo::submission()->add($submission, $publication, $journal);
$match = $metsDoc->xpath("//epdcx:statement[@epdcx:propertyURI='http://purl.org/dc/elements/1.1/title']/epdcx:valueString");
if (!empty($match)) {
$publication->setData('title', $match[0]->__toString(), $locale);
}
$match = $metsDoc->xpath("//epdcx:statement[@epdcx:propertyURI='http://purl.org/dc/terms/abstract']/epdcx:valueString");
if (!empty($match)) {
$publication->setData('abstract', $match[0]->__toString(), $locale);
}
$publication->setData('locale', $locale);
Repo::publication()->add($publication);
// Set the current submission publication to the new publication object.
Repo::submission()->edit($submission, ['currentPublicationId' => $publication->getId()]);
// Assign the user author to the stage
$stageAssignmentDao = DAORegistry::getDAO('StageAssignmentDAO');
$stageAssignmentDao->build($submission->getId(), $authorUserGroup->getId(), $user->getId());
// Store the list of authors.
$nameNodes = $metsDoc->xpath("//mods:name[mods:role/mods:roleTerm='author' or mods:role/mods:roleTerm='pkp_primary_contact']");
$i = 0;
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 = Repo::author()->newDataObject();
$author->setEmail($email[0]->__toString());
$author->setSequence(++$i);
$author->setData('publicationId', $publication->getId());
$author->setUserGroupId($authorUserGroup->getId());
$author->setIncludeInBrowse(true);
if ($family) $author->setData('familyName', $family[0]->__toString(), $locale);
if ($given) $author->setData('givenName', $given[0]->__toString(), $locale);
Repo::author()->add($author);
if ($primary) Repo::publication()->edit($publication, ['primaryContactId' => $author->getId()]);
}
}
// Attach all included files to the submission
$hrefs = array_unique(
array_map(function($h) {
return $h['href']->__toString();
}, $metsDoc->xpath("//mets:FLocat[@LOCTYPE='URL']/@xlink:href")
)
);
foreach ($hrefs as $href) {
if (($fileContents = $zip->getFromName($href)) !== false) {
// Add a file entry
$filePath = tempnam(sys_get_temp_dir(), 'sword');
file_put_contents($filePath, $fileContents);
$this->_addFile($submission, $filePath, $href, $locale);
unlink($filePath);
}
}
$zip->close();
// Create and send the deposit receipt
$depositReceipt = new DepositReceipt([
'title' => $publication->getData('title', $locale),
'edit-iri' => $this->request->getRouter()->url($this->request, null, null, null, ['swordServer', 'submissions', $submission->getId()]),
'stmt-iri' => $this->request->getRouter()->url($this->request, null, null, null, ['swordServer', 'submissions', $submission->getId(), 'statement']),
'alternateLink' => $this->request->getRouter()->url($this->request, null, 'submission', 'wizard', [1], ['submissionId' => $submission->getId()]),
]);
header('Content-Type: application/xml');
echo $depositReceipt->saveXML();
exit;
}
/**
* Add a file to the submission from the SWORD deposit.
* @param Submission $submission
* @param string $localFilename The name of the file to add on the local filesystem
* @param string $targetFilename The name of the file to represent in the target submission
* @param string $locale Locale code xx_YY
*/
protected function _addFile($submission, $localFilename, $targetFilename, $locale) {
// Identify a genre for uploaded files.
$genre = DAORegistry::getDAO('GenreDAO')->getByKey('OTHER', $this->request->getJournal()->getId());
if (!$genre) throw new Exception('Could not find genre with key OTHER for SWORD deposit!');
$submissionDir = Repo::submissionFile()->getSubmissionDir($submission->getData('contextId'), $submission->getId());
$fileService = Services::get('file');
$info = pathinfo($targetFilename);
$newFileId = $fileService->add(
$localFilename,
$submissionDir . '/' . uniqid() . '.' . ($info['extension'] ?? 'txt')
);
// Add a submission file entry
$submissionFile = new SubmissionFile();
$submissionFile->setData('fileStage', SUBMISSION_FILE_SUBMISSION);
$submissionFile->setData('viewable', true);
$submissionFile->setData('fileId', $newFileId);
$submissionFile->setData('submissionId', $submission->getId());
$submissionFile->setData('name', $targetFilename, $locale);
$submissionFile->setData('genreId', $genre->getId());
return Repo::submissionFile()->add($submissionFile);
}
/**
* Handle a SWORD Statement request
*
* @param $opts array
*/
function statement($opts) {
$locale = Locale::getDefault();
$submission = Repo::submission()->get($opts['id']);
// Ensure that the requested submission is in the appropriate journal
if ($submission->getData('contextId') != $this->getRequest()->getContext()->getId()) {
throw new Exception('The specified submission is not allowed!');
}
$section = Repo::section()->get($submission->getSectionId());
$swordStatement = new SwordStatement(
[
'state_href' => $this->request->getRouter()->url($this->request, null, null, null, ['swordServer', 'submissions', $submission->getId(), 'statement']),
'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;
}
/**
* @copydoc Plugin::getActions()
*/
public function getActions($request, $verb) {
$router = $request->getRouter();
return array_merge(
$this->getEnabled()?array(
new LinkAction(
'settings',
new AjaxModal(
$router->url($request, null, null, 'manage', null, array('verb' => 'settings', 'plugin' => $this->getName(), 'category' => 'gateways')),
$this->getDisplayName()
),
__('manager.plugins.settings'),
null
),
):array(),
parent::getActions($request, $verb)
);
}
/**
* @copydoc Plugin::manage()
*/
public function manage($args, $request) {
switch ($request->getUserVar('verb')) {
case 'settings':
$form = new SwordServerSettingsForm($this, $request->getContext()->getId());
if ($request->getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
$form->execute();
$notificationManager = new NotificationManager();
$notificationManager->createTrivialNotification($request->getUser()->getId());
return new JSONMessage(true);
}
} else {
$form->initData();
}
return new JSONMessage(true, $form->fetch($request));
}
return parent::manage($args, $request);
}
}
if (!PKP_STRICT_MODE) {
class_alias('\APP\plugins\gateways\swordServer\SwordServerPlugin', '\SwordServerPlugin');
}