-
Notifications
You must be signed in to change notification settings - Fork 1
/
SignpostingPlugin.inc.php
385 lines (360 loc) · 11.2 KB
/
SignpostingPlugin.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
<?php
/**
* @file plugins/generic/signposting/Signposting.inc.php
*
* Copyright (c) 2015-2017 University of Pittsburgh
* Copyright (c) 2014-2017 Simon Fraser University Library
* Copyright (c) 2003-2017 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* Contributed by 4Science (http://www.4science.it).
*
* @class SignpostingPlugin
* @ingroup plugins_generic_signposting
*
* @brief Signposting plugin class
*/
// Linkset URL example:
// https://<hostname>/<journal-url/sp-linkset/article/11176
import('lib.pkp.classes.plugins.GenericPlugin');
define('SIGNPOSTING_MAX_LINKS', 10);
class SignpostingPlugin extends GenericPlugin {
protected $_landingPageConfig = Array('author',
'bibliographic metadata',
'identifier',
'publication boundary');
protected $_biblioConfig = Array('bibliographic metadata');
protected $_boundariesConfig = Array('publication boundary');
protected $_citationFormats = Array();
/**
* Called as a plugin is registered to the registry
* @param $category String Name of category plugin was registered to
* @return boolean True if plugin initialized successfully; if false,
* the plugin will not be registered.
*/
public function register($category, $path, $mainContextId = null) {
$success = parent::register($category, $path, $mainContextId);
if (!Config::getVar('general', 'installed') || defined('RUNNING_UPGRADE')) return true;
if ($success && $this->getEnabled($mainContextId)) {
// Register callback for the dispatcher
HookRegistry::register('LoadHandler', Array($this, 'dispatcher'));
}
return $success;
}
/**
* @copydoc Plugin::getDisplayName()
*/
public function getDisplayName() {
return __('plugins.generic.signposting.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
public function getDescription() {
return __('plugins.generic.signposting.description');
}
/**
* Hook callback: Operations dispatcher
* @param $page
* @param $op
* @param $sourceFile
*/
public function dispatcher($page, $op) {
$request = Application::getRequest();
$articleDao = DAORegistry::getDAO('SubmissionDAO');
$issueDao = DAORegistry::getDAO('IssueDAO');
$journal = $request->getJournal();
$args = $request->getRequestedArgs();
$params = $request->getQueryArray();
$returnTrue = false;
$returnHead = false;
$mode = false;
if ($op[0] == 'sp-linkset') {
$this->import('pages/SignpostingLinksetHandler');
define('HANDLER_CLASS', 'SignpostingLinksetHandler');
$returnTrue = true;
$mode = 'linkset';
} elseif ($op[0] == 'sp-citation') {
if(array_key_exists('format', $params)) {
if ($this->checkCitation($params['format'])) {
$this->import('pages/SignpostingCitationHandler');
define('HANDLER_CLASS', 'SignpostingCitationHandler');
$returnTrue = true;
$mode = 'biblio';
} else {
return false;
}
} else {
return false;
}
} elseif ($op[0] == 'article' && $op[1] == 'view' && count($args) < 2) {
$mode = 'article';
// Intercept HEAD call
// NB: Pagespeed issue prevent headers to be displayed on HEAD call
if($_SERVER['REQUEST_METHOD'] == 'HEAD'){
$returnHead = true;
}
// Changed to include the PDF preview page
// } elseif ($op[0] == 'article' && $op[1] == 'download') {
} elseif (($op[0] == 'article' && $op[1] == 'download') || ($op[0] == 'article' && $op[1] == 'view' && count($args) < 3)) {
if ($this->checkBoundary($args[0])) {
$mode = 'boundary';
} else {
return false;
}
} else {
return false;
}
$article = $articleDao->getById($args[0]);
if (empty($article)) return false;
$headers = Array();
$modeParams = $this->getModeParameters($mode);
if (count($modeParams) > 0) {
$configVarName = $modeParams['configVarName'];
$patternMode = $modeParams['patternMode'];
$this->buildHeaders($headers,
$configVarName,
$patternMode,
$journal,
$article);
}
if (count($headers) > SIGNPOSTING_MAX_LINKS) {
switch ($mode) {
case 'article' : $params = $article->getId();
break;
case 'boundary': $params = Array($article->getId(), $args[1]);
break;
case 'biblio' : $params = Array($article->getId(), $params['format']);
break;
}
$linksetUrl = $request->url(null,
'sp-linkset',
$mode,
$params);
$headers = Array();
$headers[] = Array('value' => $linksetUrl,
'rel' => 'linkset',
'type' => 'application/linkset+json');
}
$this->_outputHeaders($headers);
if ($returnTrue) {
return true;
} elseif ($returnHead) {
return false;
}
}
/**
* Returns the parameters for the headers choice
* @param $mode
* @return Array
*/
public function getModeParameters($mode) {
$output = Array();
switch ($mode) {
case 'article' : $output = Array('configVarName' => '_landingPageConfig',
'patternMode' => 'toItem');
break;
case 'boundary': $output = Array('configVarName' => '_boundariesConfig',
'patternMode' => 'toCollection');
break;
case 'biblio' : $output = Array('configVarName' => '_biblioConfig',
'patternMode' => 'toCollection');
break;
}
return $output;
}
/**
* Check if the citation is available
* @param $citation
* @return bool
*/
public function checkCitation($citation) {
$citationFormats = $this->_getCitationFormats();
if (isset($citationFormats[$citation])) {
return true;
}
return false;
}
/**
* Check if the boundary resource is available
* @param $galleyId
* @return bool
*/
public function checkBoundary($galleyId) {
$articleDao = DAORegistry::getDAO('ArticleGalleyDAO');
$galley = $articleDao->getById($galleyId);
if (!empty($galley)) {
return true;
}
return false;
}
/**
* Builds the list of headers
* @param Array $headers
* @param string $configVarName
* @param string $patternMode
* @param object $journal
* @param object $article
*/
public function buildHeaders(&$headers, $configVarName, $patternMode, $journal, $article) {
foreach ($this->$configVarName as $pattern) {
switch ($pattern) {
case 'author':
$this->_authorPattern($headers, $article);
break;
case 'bibliographic metadata':
$this->_bibliographicMetadataPattern($headers,
$journal,
$article,
$patternMode);
break;
case 'identifier':
$this->_identifierPattern($headers, $journal, $article);
break;
case 'publication boundary':
$this->_publicationBoundary($headers,
$journal,
$article,
$patternMode);
break;
}
}
}
/**
* Signposting Author pattern implementation
* @param Array $headers
* @param object $article
*/
protected function _authorPattern(&$headers, $article) {
foreach ($article->getAuthors() as $author) {
$orcid = $author->getData('orcid');
if (!empty($orcid)) {
$headers[] = Array('value' => $orcid,
'rel' => 'author');
}
}
}
/**
* Signposting Bibliographic Metadata pattern implementation
* @param Array $headers
* @param object $article
* @param string $mode
*/
protected function _bibliographicMetadataPattern(&$headers, $journal, $article, $mode) {
$request = Application::getRequest();
if ($mode == 'toItem') {
$rel = 'describedby';
$citationFormats = $this->_getCitationFormats();
foreach ($citationFormats as $format => $mimeType) {
$link = $request->url(null, 'sp-citation', 'serveCitation', $article->getId(), Array('format' => $format));
$headers[] = Array('value' => $link,
'rel' => $rel,
'type' => $mimeType['contentType']);
}
$pubIdPlugin = PluginRegistry::loadPlugin('pubIds', 'doi');
$pubId = $pubIdPlugin->getPubId($article);
if (!empty($pubId)) {
$headers[] = Array('value' => $pubIdPlugin->getResolvingURL($journal->getId(), $pubId),
'rel' => $rel,
'type' => 'application/vnd.citationstyles.csl+json');
}
} else {
$rel = 'describes';
$link = $request->url(null, 'article', 'view', $article->getId());
$headers[] = Array('value' => $link,
'rel' => $rel);
}
}
/**
* Signposting Identifier pattern implementation
* @param Array $headers
* @param object $journal
* @param object $article
*/
protected function _identifierPattern(&$headers, $journal, $article) {
$pubIdPlugins = PluginRegistry::loadCategory('pubIds', true);
foreach ($pubIdPlugins as $pubIdPlugin) {
$pubId = $pubIdPlugin->getPubId($article);
if (!empty($pubId)) {
$headers[] = Array('value' => $pubIdPlugin->getResolvingURL($journal->getId(), $pubId),
'rel' => 'cite-as');
}
}
}
/**
* Signposting Publication Boundary pattern implementation
* @param Array $headers
* @param object $journal
* @param object $article
* @param string $mode
*/
protected function _publicationBoundary(&$headers, $journal, $article, $mode) {
$request = Application::getRequest();
$articleId = $article->getBestArticleId($journal);
if ($mode == 'toItem') {
$rel = 'item';
foreach ($article->getGalleys() as $galley) {
$link = $request->url(null, 'article', 'download',
Array($article->getId(),
$galley->getBestGalleyId($journal)));
$mimeType = $galley->getFileType();
$headers[] = Array('value' => $link,
'rel' => $rel,
'type' => $mimeType);
}
} else {
$rel = 'collection';
$link = $request->url(null, 'article', 'view', $article->getId());
$headers[] = Array('value' => $link,
'rel' => $rel);
}
}
/**
* Get Citation formats from 'citationStyleLanguage' Plugin
*
*/
protected function _getCitationFormats() {
if(count($this->_citationFormats) < 1){
// The DOI plugin is loaded to register the hook: 'CitationStyleLanguage::citation'
// Used by 'citationStyleLanguage' Plugin
$doi = PluginRegistry::loadPlugin('pubIds' , 'doi');
$plugin = PluginRegistry::loadPlugin('generic', 'citationStyleLanguage');
if(!empty($plugin)){
$citationStyles = $plugin->getEnabledCitationStyles();
$citationDwn = $plugin->getEnabledCitationDownloads();
$citationFormats = array_merge($citationStyles, $citationDwn);
foreach($citationFormats as $citationFormat){
if(array_key_exists('contentType', $citationFormat)){
$fileExt = $citationFormat['fileExtension'];
$cntType = $citationFormat['contentType'];
}else{
$fileExt = 'txt';
$cntType = 'text/plain';
}
$this->_citationFormats[$citationFormat['id']] = Array(
'fileExtension' => $fileExt,
'contentType' => $cntType);
}
}
}
return $this->_citationFormats;
}
/**
* HTTP Headers: formatting and sending
* @param Array $headers
*/
protected function _outputHeaders($headers) {
if(count($headers) > 0){
$indentation = '';
$headerString = 'Link: ';
$headerArray = Array();
foreach ($headers as $header) {
$headerArray[] = '<' . $header['value'] . '>'
. $indentation . '; rel="' . $header['rel'] . '"'
. (isset($header['type'])?$indentation . '; type="' . $header['type'] . '"':'');
}
$headerString .= join(','.$indentation, $headerArray);
header($headerString);
}
}
}