-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRestPlugin.inc.php
More file actions
298 lines (258 loc) · 9.78 KB
/
RestPlugin.inc.php
File metadata and controls
298 lines (258 loc) · 9.78 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
<?php
/**
* @file plugins/gateway/rest/RestPlugin.inc.php
*
* Copyright (c) 2003-2011 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class RestPlugin
* @ingroup plugins_gateways_rest
*
* @brief OJS REST gateway plugin. Retrieve JSON-formatted information from an OJS installation via HTTP calls .
*/
import('classes.plugins.GatewayPlugin');
import('classes.file.PublicFileManager');
class RestPlugin extends GatewayPlugin {
/**
* Called as a plugin is registered to the registry
* @param $category String Name of category plugin was registered to
* @return boolean True iff plugin initialized successfully; if false,
* the plugin will not be registered.
*/
function register($category, $path) {
$success = parent::register($category, $path);
$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';
}
/**
* Get the name of this plugin. The name must be unique within
* its category.
* @return String name of plugin
*/
function getName() {
return 'RestPlugin';
}
function getDisplayName() {
return Locale::translate('plugins.gateways.rest.displayName');
}
function getDescription() {
return Locale::translate('plugins.gateways.rest.description');
}
/**
* Handle fetch requests for this plugin.
* @param $args array
* @param $request PKPRequest
*/
function fetch($args, &$request) {
if (!$this->getEnabled()) {
return false;
}
$journal =& $request->getJournal();
if (!isset($journal)) $this->showError();
$issueDao =& DAORegistry::getDAO('IssueDAO');
$journalId = $journal->getId();
$operator = array_shift($args);
switch ($operator) {
case 'journalInfo': // Basic journal metadata
$response = array(
'id' => $journalId,
'title' => $journal->getLocalizedTitle(),
'url' => $journal->getUrl(),
'initials' => $journal->getLocalizedInitials(),
'description' => $journal->getLocalizedDescription(),
);
echo json_encode($response);
break;
case 'articleInfo': // Article metadata
// Takes article ID as input
$articleId = (int) array_shift($args);
$response = $this->_getArticleInfo($request, $articleId);
echo json_encode($response);
break;
case 'issueData': // Issue metadata
//Takes article ID as input
$issueId = (int) array_shift($args);
$issue =& $issueDao->getIssueById($issueId, $journalId);
$response = $this->_getIssueInfo($request, $journalId, $issue);
echo json_encode($response);
break;
case 'issueDataWithArticles': //Issue metadata along with all included article metadata
// Takes issue ID as input
$issueId = (int) array_shift($args);
$issue =& $issueDao->getIssueById($issueId, $journalId);
$response = $this->_getIssueInfo($request, $journalId, $issue, true);
echo json_encode($response);
break;
case 'currentIssueData': // Current issue metadata
$issue =& $issueDao->getCurrentIssue($journalId, true);
$response = $this->_getIssueInfo($request, $journalId, $issue);
echo json_encode($response);
break;
case 'currentIssueDataWithArticles': // Current issue metadata along with all included article metadata
$issue =& $issueDao->getCurrentIssue($journalId, true);
$response = $this->_getIssueInfo($request, $journalId, $issue, true);
echo json_encode($response);
break;
case 'allIssueData': // Metadata for all published issues
$issues =& $issueDao->getPublishedIssues($journalId);
$response = array();
while ($issue =& $issues->next()) {
$response[] = $this->_getIssueInfo($request, $journalId, $issue);
unset($issue);
}
echo json_encode($response);
break;
case 'allIssueDataWithArticles': // Metadata for all published issues and all their articles (can be big!)
$issues =& $issueDao->getPublishedIssues($journalId);
$response = array();
while ($issue =& $issues->next()) {
$response[] = $this->_getIssueInfo($request, $journalId, $issue, true);
unset($issue);
}
echo json_encode($response);
break;
case 'announcements': // Announcements
$announcementDao =& DAORegistry::getDAO('AnnouncementDAO');
$announcements =& $announcementDao->getAnnouncementsNotExpiredByAssocId(ASSOC_TYPE_JOURNAL, $journalId);
$response = array();
while($announcement =& $announcements->next()) {
$response[] = array(
'url' => $request->url(null, 'announcement', 'view', array($announcement->getId())),
'id' => $announcement->getId(),
'title' => $announcement->getLocalizedTitleFull(),
'description' => $announcement->getLocalizedDescription(),
'datePosted' => $announcement->getDatetimePosted()
);
unset($announcement);
}
echo json_encode($response);
break;
default:
// Not a valid request
$this->showError();
}
return true;
}
/**
* Display an error message and exit
*/
function showError() {
header("HTTP/1.0 500 Internal Server Error");
echo Locale::translate('plugins.gateways.rest.errors.errorMessage');
exit;
}
/**
* Get data for an issue
* @param $request PKPRequest
* @param $journalId The journal ID
* @param $issueId Object
* @param $withArticles boolean Whether to include the issue's articles in the response
*/
function _getIssueInfo(&$request, $journalId, $issue, $withArticles = false) {
if(!isset($issue)) $this->showError();
Locale::requireComponents(array(LOCALE_COMPONENT_APPLICATION_COMMON));
//Handle getting the image URL
$publicFileManager = new PublicFileManager();
$coverPagePath = $request->getBaseUrl() . '/';
$coverPagePath .= $publicFileManager->getJournalFilesPath($journalId) . '/';
$imageFileName = $issue->getIssueFileName();
$imageUrl = $coverPagePath . $imageFileName;
$response = array(
'url' => $request->url(null, 'issue', 'view', $issue->getId()),
'issueId' => $issue->getId(),
'title' => $issue->getLocalizedTitle(),
'description' => $issue->getLocalizedDescription(),
'identification' => $issue->getIssueIdentification(),
'volume' => $issue->getVolume(),
'number' => $issue->getNumber(),
'year' => $issue->getYear(),
'datePublished' => $issue->getDatePublished(),
'imageUrl' => $imageUrl,
'imageDescription' => $issue->getIssueCoverPageDescription()
);
if ($withArticles) {
$publishedArticleDao =& DAORegistry::getDAO('PublishedArticleDAO');
$publishedArticles =& $publishedArticleDao->getPublishedArticles($issue->getId());
$articles = array();
foreach($publishedArticles as $publishedArticle) {
$articles[] = $this->_getArticleInfo($request, $publishedArticle->getId());
}
$response['articles'] = $articles;
}
return $response;
}
/**
* Get data for an article
* @param $request PKPRequest
* @param $articleId int
* @return array
*/
function _getArticleInfo(&$request, $articleId) {
$articleDao =& DAORegistry::getDAO('ArticleDAO');
$article =& $articleDao->getArticle($articleId);
if(!isset($article)) $this->showError();
// Create an array of author information
$authors =& $article->getAuthors();
$authorInfo = array();
for ($i=0, $count=count($authors); $i < $count; $i++) {
$currentAuthor = array(
'firstName' => $authors[$i]->getFirstName(),
'middleName' => $authors[$i]->getMiddleName(),
'lastName' => $authors[$i]->getLastName(),
'affiliation' => $authors[$i]->getAffiliation(null), // Localized
'country' => $authors[$i]->getCountry(),
'countryLocalized' => $authors[$i]->getCountryLocalized(),
'email' => $authors[$i]->getEmail(),
'url' => $authors[$i]->getUrl(),
'competingInterests' => $authors[$i]->getCompetingInterests(null), // Localized
'biography' => $authors[$i]->getBiography(null) // Localized
);
if ($authors[$i]->getPrimaryContact()) {
$currentAuthor['primaryContact'] = true;
}
array_push($authorInfo, $currentAuthor);
}
// Construct the article response data to be encoded
$response = array(
'url' => $request->url(null, 'article', 'view', $articleId),
'id' => $articleId,
'title' => $article->getLocalizedTitle(),
'abstract' => $article->getLocalizedAbstract(),
'authorString' => $article->getAuthorString(),
'authors' => $authorInfo,
'sectionId' => $article->getSectionId(),
'sectionTitle' => $article->getSectionTitle()
);
$articleGalleyDAO =& DAORegistry::getDAO('ArticleGalleyDAO');
$articleGalleys =& $articleGalleyDAO->getGalleysByArticle($articleId);
$galleysResponse = array();
foreach ($articleGalleys as $articleGalley){
$galleyId = $articleGalley->getId();
$galleyInfo = array(
'id' => $galleyId,
'label' => $articleGalley->getLabel(),
'url' => Request::url(null, 'article', 'viewFile', array($articleId, $galleyId))
);
$galleysResponse[] = $galleyInfo;
}
$response['galleys'] = $galleysResponse;
// Add some optional metadata. There may be other items the could be included here.
if($article->getLocalizedDiscipline()) $response['discipline'] = $article->getLocalizedDiscipline();
if($article->getLocalizedSubjectClass()) $response['subjectClass'] = $article->getLocalizedSubjectClass();
if($article->getLocalizedCoverageGeo()) $response['coverageGeo'] = $article->getLocalizedCoverageGeo();
if($article->getLocalizedCoverageChron()) $response['coverageChron'] = $article->getLocalizedCoverageChron();
if($article->getLocalizedType()) $response['type'] = $article->getLocalizedType();
if($article->getLocalizedSponsor()) $response['sponsor'] = $article->getLocalizedSponsor();
if($article->getCitations()) $response['citations'] = $article->getCitations();
return $response;
}
}
?>