-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEpubJsViewerPlugin.php
More file actions
267 lines (240 loc) · 10.2 KB
/
Copy pathEpubJsViewerPlugin.php
File metadata and controls
267 lines (240 loc) · 10.2 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
<?php
/**
* @file plugins/generic/epubJsViewer/EpubJsViewerPlugin.php
*
* Copyright (c) 2026 OJSBR (https://ojsbr.com)
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class EpubJsViewerPlugin
*
* @brief Embedded reading of EPUB files with epub.js.
*
* Based on the idea of the epubViewer plugin by Lepidus Tecnologia, discontinued
* in 2025 because its Bibi reader was no longer maintained. The reader is
* epub.js (FuturePress), which is.
*
* The three applications show a file through different handlers:
*
* OJS ArticleHandler::view::galley [$request, $issue, $galley, $submission]
* IssueHandler::view::galley [$request, $issue, $galley]
* OPS PreprintHandler::view::galley [$request, $galley, $submission]
* OMP CatalogBookHandler::view [$handler, $submission, $publicationFormat, $submissionFile]
*
* OMP has no galleys: a publication format plus a submission file play the part
* of the OJS galley, and there are no issue galleys. No core file is replaced.
*/
namespace APP\plugins\generic\epubJsViewer;
use APP\core\Application;
use APP\template\TemplateManager;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
class EpubJsViewerPlugin extends GenericPlugin
{
public const EPUB_MIME_TYPE = 'application/epub+zip';
/**
* Register the plugin and, where it is enabled, the hook of the application.
*
* @param string $category
* @param string $path
* @param null|int $mainContextId
*/
public function register($category, $path, $mainContextId = null): bool
{
$success = parent::register($category, $path, $mainContextId);
if (!$success || Application::isUnderMaintenance() || !$this->getEnabled($mainContextId)) {
return $success;
}
// Only reader-facing requests reach these handlers, and they always carry a context.
switch (Application::get()->getName()) {
case 'ojs2':
Hook::add('ArticleHandler::view::galley', $this->submissionCallback(...), Hook::SEQUENCE_LAST);
Hook::add('IssueHandler::view::galley', $this->issueCallback(...), Hook::SEQUENCE_LAST);
break;
case 'ops':
Hook::add('PreprintHandler::view::galley', $this->submissionCallback(...), Hook::SEQUENCE_LAST);
break;
case 'omp':
Hook::add('CatalogBookHandler::view', $this->bookCallback(...), Hook::SEQUENCE_LAST);
break;
}
return $success;
}
/**
* Name shown in the plugins list.
*/
public function getDisplayName(): string
{
return __('plugins.generic.epubJsViewer.displayName');
}
/**
* Description shown in the plugins list.
*/
public function getDescription(): string
{
return __('plugins.generic.epubJsViewer.description');
}
/**
* Whether a file is an EPUB.
*
* The extension counts too: not every upload arrives with the right mimetype,
* and OMP stores application/octet-stream for some of them.
*/
public static function isEpubFile(?string $mimetype, ?string $name, ?string $path): bool
{
if ($mimetype === self::EPUB_MIME_TYPE) {
return true;
}
foreach ([$name, $path] as $candidate) {
if ($candidate && strtolower(pathinfo($candidate, PATHINFO_EXTENSION)) === 'epub') {
return true;
}
}
return false;
}
/**
* OMP: show an EPUB file of a publication format in the reader.
*
* The hook is called by CatalogBookHandler::download() with $view = true, after
* the core has checked that the format is available and not remote, that the
* publication is published, that the file belongs to the format and that access
* is open or paid for. No authorization rule is repeated here.
*
* @param string $hookName
* @param array $args [$handler, $submission, $publicationFormat, $submissionFile]
*/
public function bookCallback($hookName, $args): bool
{
[$handler, $submission, $publicationFormat, $submissionFile] = array_pad($args, 4, null);
if (!$handler || !$submission || !$publicationFormat || !$submissionFile) {
return Hook::CONTINUE;
}
if (!self::isEpubFile($submissionFile->getData('mimetype'), $submissionFile->getLocalizedData('name'), $submissionFile->getData('path'))) {
return Hook::CONTINUE;
}
// The publication of the URL: the handler has already resolved the version.
$publication = $handler->publication ?? $submission->getCurrentPublication();
if (!$publication) {
return Hook::CONTINUE;
}
$request = Application::get()->getRequest();
$isLatest = (int) $publication->getId() === (int) $submission->getData('currentPublicationId');
// Older versions carry the version/{publicationId} segment, as in the core.
$path = [$submission->getBestId()];
if (!$isLatest) {
array_push($path, 'version', $publication->getId());
}
$this->display($request, [
'submission' => $submission,
'publication' => $publication,
'publicationFormat' => $publicationFormat,
'submissionFile' => $submissionFile,
'isLatestPublication' => $isLatest,
'title' => $publication->getLocalizedFullTitle(),
'isTitleHtml' => false,
'epubUrl' => $request->url(null, 'catalog', 'download', array_merge($path, [$publicationFormat->getBestId(), $submissionFile->getBestId()])),
'parentUrl' => $request->url(null, 'catalog', 'book', $path),
'galleyTitle' => __('submission.representationOfTitle', [
'representation' => $publicationFormat->getLocalizedName(),
'title' => $publication->getLocalizedFullTitle(),
]),
'datePublished' => __('submission.outdatedVersion', [
'datePublished' => $publication->getData('datePublished'),
'urlRecentVersion' => $request->url(null, 'catalog', 'book', [$submission->getBestId()]),
]),
]);
return Hook::ABORT;
}
/**
* OJS and OPS: show an EPUB galley of a submission in the reader.
*
* @param string $hookName
* @param array $args OJS [$request, $issue, $galley, $submission]; OPS [$request, $galley, $submission]
*/
public function submissionCallback($hookName, $args): bool
{
$request = $args[0];
if (Application::get()->getName() === 'ojs2') {
[, $issue, $galley, $submission] = $args;
$submissionNoun = 'article';
} else {
[, $galley, $submission] = $args;
$issue = null;
$submissionNoun = 'preprint';
}
if (!$galley || $galley->getFileType() !== self::EPUB_MIME_TYPE) {
return Hook::CONTINUE;
}
$galleyPublication = null;
foreach ($submission->getData('publications') as $publication) {
if ($publication->getId() === $galley->getData('publicationId')) {
$galleyPublication = $publication;
break;
}
}
if (!$galleyPublication) {
return Hook::CONTINUE;
}
$parentUrl = $request->url(null, $submissionNoun, 'view', [$submission->getBestId()]);
$this->display($request, [
'galleyFile' => $galley->getFile(),
'issue' => $issue,
'submission' => $submission,
'submissionNoun' => $submissionNoun,
'bestId' => $galleyPublication->getData('urlPath') ?? $submission->getId(),
'galley' => $galley,
'galleyPublication' => $galleyPublication,
'isLatestPublication' => $submission->getData('currentPublicationId') === $galley->getData('publicationId'),
'title' => $galleyPublication->getLocalizedTitle(null, 'html'),
'isTitleHtml' => true,
'epubUrl' => $request->url(null, $submissionNoun, 'download', [$submission->getBestId(), $galley->getBestGalleyId(), $galley->getFile()->getId()]),
'parentUrl' => $parentUrl,
'galleyTitle' => __('submission.representationOfTitle', [
'representation' => $galley->getLabel(),
'title' => $galleyPublication->getLocalizedFullTitle(),
]),
'datePublished' => __('submission.outdatedVersion', [
'datePublished' => $galleyPublication->getData('datePublished'),
'urlRecentVersion' => $parentUrl,
]),
]);
return Hook::ABORT;
}
/**
* OJS: show an EPUB issue galley in the reader.
*
* @param string $hookName
* @param array $args [$request, $issue, $galley]
*/
public function issueCallback($hookName, $args): bool
{
[$request, $issue, $galley] = $args;
if (!$galley || $galley->getFileType() !== self::EPUB_MIME_TYPE) {
return Hook::CONTINUE;
}
$this->display($request, [
'issue' => $issue,
'galley' => $galley,
'galleyFile' => $galley->getFile(),
'isLatestPublication' => true,
'title' => $issue->getLocalizedTitle(),
'isTitleHtml' => false,
'epubUrl' => $request->url(null, 'issue', 'download', [$issue->getBestIssueId(), $galley->getBestGalleyId()]),
'parentUrl' => $request->url(null, 'issue', 'view', [$issue->getBestIssueId()]),
'galleyTitle' => $issue->getLocalizedTitle(),
]);
return Hook::ABORT;
}
/**
* Render the reader page.
*/
private function display($request, array $variables): void
{
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign(array_merge([
'displayTemplateResource' => $this->getTemplateResource('display.tpl'),
'pluginUrl' => $request->getBaseUrl() . '/' . $this->getPluginPath(),
'currentVersionString' => Application::get()->getCurrentVersion()->getVersionString(false),
], $variables));
$templateMgr->display($this->getTemplateResource('display.tpl'));
}
}