-
Notifications
You must be signed in to change notification settings - Fork 7
/
WebFeedGatewayPlugin.php
230 lines (204 loc) · 7.75 KB
/
WebFeedGatewayPlugin.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
<?php
/**
* @file WebFeedGatewayPlugin.php
*
* Copyright (c) 2014-2023 Simon Fraser University
* Copyright (c) 2003-2023 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class WebFeedGatewayPlugin
*
* @brief Gateway component of web feed plugin
*
*/
namespace APP\plugins\generic\webFeed;
use APP\core\Application;
use APP\core\Request;
use APP\facades\Repo;
use APP\section\Section;
use APP\submission\Collector;
use APP\submission\Submission;
use APP\template\TemplateManager;
use Exception;
use PKP\category\Category;
use PKP\core\Registry;
use PKP\plugins\GatewayPlugin;
class WebFeedGatewayPlugin extends GatewayPlugin
{
public const ATOM = 'atom';
public const RSS = 'rss';
public const RSS2 = 'rss2';
public const FEED_MIME_TYPE = [
self::ATOM => 'application/atom+xml',
self::RSS => 'application/rdf+xml',
self::RSS2 => 'application/rss+xml'
];
public const DEFAULT_RECENT_ITEMS = 30;
/**
* Constructor
*/
public function __construct(protected WebFeedPlugin $parentPlugin)
{
parent::__construct();
}
/**
* Handle fetch requests for this plugin.
*
* @param array $args Arguments.
* @param Request $request Request object.
*/
public function fetch($args, $request): bool
{
$context = $request->getContext();
if (!$context || !$this->parentPlugin->getEnabled($context->getId())) {
return false;
}
// Make sure the feed type is specified and valid
$feedType = array_shift($args);
if (!in_array($feedType, array_keys(static::FEED_MIME_TYPE))) {
throw new Exception('Invalid feed format');
}
// Get limit setting from web feeds plugin
$displayItems = $this->parentPlugin->getSetting($context->getId(), 'displayItems');
$recentItems = abs((int) $this->parentPlugin->getSetting($context->getId(), 'recentItems')) ?: static::DEFAULT_RECENT_ITEMS;
$includeIdentifiers = (bool) $this->parentPlugin->getSetting($context->getId(), 'includeIdentifiers');
$latestDate = null;
$submissions = Repo::submission()->getCollector()
->filterByContextIds([$context->getId()])
->filterByStatus([Submission::STATUS_PUBLISHED])
->limit($recentItems)
->orderBy(Collector::ORDERBY_LAST_MODIFIED, Collector::ORDER_DIR_DESC);
// OJS only feature
// If the plugin is configured to display only the latest issue, so we filter by it and apply the needed changes to the query
if (WebFeedPlugin::hasIssues() && $displayItems === 'issue') {
$issue = Repo::issue()->getCurrent($context->getId(), true);
$submissions->filterByIssueIds([$issue?->getId() ?? 0])
->limit(null)
->orderBy(Collector::ORDERBY_SEQUENCE, Collector::ORDER_DIR_ASC);
$latestDate = $issue?->getData('datePublished');
}
$submissions = $submissions->getMany();
$latestDate ??= $submissions->first()?->getData('lastModified');
$submissions = $submissions->map(fn (Submission $submission) => ['submission' => $submission, 'identifiers' => $this->getIdentifiers($submission)]);
$userGroups = Repo::userGroup()->getCollector()->filterByContextIds([$context->getId()])->getMany();
$applicationIdentifier = strtolower(preg_replace('/[^a-z]/i', '', Application::getName()));
TemplateManager::getManager($request)
->assign(
[
'applicationName' => match ($applicationIdentifier) {
'ojs' => 'Open Journal Systems',
'omp' => 'Open Monograph Press',
'ops' => 'Open Preprint Systems'
},
'applicationVersion' => Registry::get('appVersion'),
'applicationIdentifier' => $applicationIdentifier,
'publicationPage' => match ($applicationIdentifier) {
'ojs' => 'article',
'omp' => 'catalog',
'ops' => 'preprint'
},
'publicationOp' => match ($applicationIdentifier) {
'ojs' => 'view',
'omp' => 'book',
'ops' => 'view'
},
'openAccess' => match ($applicationIdentifier) {
'ojs' => Submission::ARTICLE_ACCESS_OPEN,
'omp' => null,
'ops' => Submission::PREPRINT_ACCESS_OPEN
},
'submissions' => $submissions,
'context' => $context,
'latestDate' => $latestDate,
'feedUrl' => $request->getRequestUrl(),
'userGroups' => $userGroups,
'includeIdentifiers' => $includeIdentifiers
]
)
->setHeaders(['content-type: ' . static::FEED_MIME_TYPE[$feedType] . '; charset=utf-8'])
->display($this->parentPlugin->getTemplateResource("{$feedType}.tpl"));
return true;
}
/**
* Retrieves the identifiers assigned to a submission
*
* @return array<array{'type':string,'label':string,'values':string[]}>
*/
private function getIdentifiers(Submission $submission): array
{
$identifiers = [];
if ($section = $this->getSection($submission->getSectionId())) {
$identifiers[] = ['type' => 'section', 'label' => __('section.section'), 'values' => [$section->getLocalizedTitle()]];
}
$publication = $submission->getCurrentPublication();
$categories = Repo::category()->getCollector()
->filterByPublicationIds([$publication->getId()])
->getMany()
->map(fn (Category $category) => $category->getLocalizedTitle())
->toArray();
if (count($categories)) {
$identifiers[] = ['type' => 'category', 'label' => __('category.category'), 'values' => $categories];
}
foreach (['keywords' => 'common.keywords', 'subjects' => 'common.subjects', 'disciplines' => 'search.discipline'] as $field => $label) {
$values = $publication->getLocalizedData($field) ?? [];
if (count($values)) {
$identifiers[] = ['type' => $field, 'label' => __($label), 'values' => $values];
}
}
return $identifiers;
}
/**
* Retrieves a section
*/
private function getSection(?int $sectionId): ?Section
{
static $sections = [];
return $sectionId
? $sections[$sectionId] ??= Repo::section()->get($sectionId)
: null;
}
/**
* @copydoc Plugin::getHideManagement()
*/
public function getHideManagement(): bool
{
return true;
}
/**
* @copydoc Plugin::getName()
*/
public function getName(): string
{
return substr(static::class, strlen(__NAMESPACE__) + 1);
}
/**
* @copydoc Plugin::getDisplayName()
*/
public function getDisplayName(): string
{
return __('plugins.generic.webfeed.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
public function getDescription(): string
{
return __('plugins.generic.webfeed.description');
}
/**
* @copydoc Plugin::getPluginPath()
*/
public function getPluginPath(): string
{
return $this->parentPlugin->getPluginPath();
}
/**
* @copydoc Plugin::getEnabled()
*
* @param null|mixed $contextId
*/
public function getEnabled($contextId = null): bool
{
return $this->parentPlugin->getEnabled($contextId);
}
}