-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEpubJsViewerPlugin.php
More file actions
315 lines (281 loc) · 11.7 KB
/
Copy pathEpubJsViewerPlugin.php
File metadata and controls
315 lines (281 loc) · 11.7 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
/**
* @file plugins/generic/epubJsViewer/EpubJsViewerPlugin.php
*
* Copyright (c) 2026 OJSBR (https://ojsbr.com.br)
* Inspired by the original Bibi EPUB Viewer plugin by Lepidus Tecnologia,
* discontinued in 2025 because the Bibi reader was no longer maintained.
* Reader replaced by epub.js (FuturePress), which is actively maintained.
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class EpubJsViewerPlugin
*
* @brief Leitura embutida de EPUB com epub.js.
*
* Porte do plugin de OJS/OPS para o OMP. As tres aplicacoes exibem
* o arquivo por caminhos diferentes:
*
* OJS ArticleHandler::view::galley [$request, $issue, $galley, $submission]
* OPS PreprintHandler::view::galley [$request, $galley, $submission]
* OMP CatalogBookHandler::view [&$handler, &$submission,
* &$publicationFormat, &$submissionFile]
*
* O OMP nao tem galley: o par formato de publicacao + arquivo faz o
* papel que no OJS cabe a um unico objeto ArticleGalley. Tambem nao
* existe galley de fasciculo, entao o caminho de issue nao se aplica.
*
* Nenhum arquivo do core e alterado.
*/
namespace APP\plugins\generic\epubJsViewer;
use APP\core\Application;
use APP\template\TemplateManager;
use Exception;
use PKP\plugins\Hook;
class EpubJsViewerPlugin extends \PKP\plugins\GenericPlugin
{
public const EPUB_MIME_TYPE = 'application/epub+zip';
/**
* @copydoc Plugin::register()
*
* @param null|mixed $mainContextId
*/
public function register($category, $path, $mainContextId = null)
{
if (parent::register($category, $path, $mainContextId)) {
if ($this->getEnabled($mainContextId)) {
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 true;
}
return false;
}
/**
* Nome estavel no registry e nas URLs do gerenciador de plugins.
*/
public function getName()
{
return 'epubjsviewerplugin';
}
public function getDisplayName()
{
return __('plugins.generic.epubJsViewer.displayName');
}
public function getDescription()
{
return __('plugins.generic.epubJsViewer.description');
}
/**
* OMP: exibe um arquivo EPUB de um formato de publicacao no leitor.
*
* O hook e disparado por CatalogBookHandler::download() com $view = true,
* ou seja, depois de o core validar formato disponivel e nao remoto,
* publicacao publicada, arquivo pertencente ao formato, acesso aberto ou
* compra paga e a restricao de acesso da editora. Devolver true faz o
* core encerrar sem entregar o arquivo.
*
* @param string $hookName
* @param array $args [&$handler, &$submission, &$publicationFormat, &$submissionFile]
*
* @return bool true quando o leitor foi exibido
*/
public function bookCallback($hookName, $args)
{
$handler = $args[0] ?? null;
$submission = $args[1] ?? null;
$publicationFormat = $args[2] ?? null;
$submissionFile = $args[3] ?? null;
if (!$handler || !$submission || !$publicationFormat || !$submissionFile) {
return false;
}
if (!$this->isEpub($submissionFile)) {
return false;
}
$request = Application::get()->getRequest();
$application = Application::get();
// A publicacao pedida na URL: o handler ja resolveu qual versao e.
$publication = $handler->publication ?? $submission->getCurrentPublication();
if (!$publication) {
return false;
}
$isLatest = (int) $publication->getId() === (int) $submission->getData('currentPublicationId');
// Caminho comum das URLs do catalogo. Versoes antigas levam o
// segmento version/{publicationId}, como no proprio core.
$path = [$submission->getBestId()];
if (!$isLatest) {
$path[] = 'version';
$path[] = $publication->getId();
}
$parentUrl = $request->url(null, 'catalog', 'book', $path);
$filePath = array_merge($path, [$publicationFormat->getBestId(), $submissionFile->getBestId()]);
$epubUrl = $request->url(null, 'catalog', 'download', $filePath);
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign([
'displayTemplateResource' => $this->getTemplateResource('display.tpl'),
'pluginUrl' => $request->getBaseUrl() . '/' . $this->getPluginPath(),
'submission' => $submission,
'publication' => $publication,
'publicationFormat' => $publicationFormat,
'submissionFile' => $submissionFile,
'currentVersionString' => $application->getCurrentVersion()->getVersionString(false),
'isLatestPublication' => $isLatest,
'title' => $publication->getLocalizedFullTitle(),
'epubUrl' => $epubUrl,
'parentUrl' => $parentUrl,
'galleyTitle' => __('submission.representationOfTitle', [
'representation' => $publicationFormat->getLocalizedName(),
'title' => $publication->getLocalizedFullTitle(),
]),
'datePublished' => __('submission.outdatedVersion', [
'datePublished' => $publication->getData('datePublished'),
'urlRecentVersion' => $request->url(null, 'catalog', 'book', [$submission->getBestId()]),
]),
]);
$templateMgr->display($this->getTemplateResource('display.tpl'));
return true;
}
/**
* O OMP guarda o mimetype no arquivo, nao no formato de publicacao.
* Alguns envios chegam como application/octet-stream, entao a extensao
* decide quando o mimetype registrado nao ajuda.
*/
private function isEpub($submissionFile): bool
{
return self::isEpubFile(
$submissionFile->getData('mimetype'),
$submissionFile->getLocalizedData('name'),
$submissionFile->getData('path')
);
}
/**
* Decide se o arquivo e um EPUB.
*
* Puro de proposito: separado do objeto de arquivo, e a regra que decide se
* o plugin assume ou nao a renderizacao, e da para cobrir por teste sem subir
* aplicacao. A extensao tem voz porque nem todo envio chega com o mimetype
* correto — o OMP grava application/octet-stream em alguns casos.
*/
public static function isEpubFile(?string $mimetype, ?string $name, ?string $path): bool
{
if ($mimetype === self::EPUB_MIME_TYPE) {
return true;
}
foreach ([$name, $path] as $nome) {
if ($nome && strtolower(pathinfo($nome, PATHINFO_EXTENSION)) === 'epub') {
return true;
}
}
return false;
}
/**
* Present an EPUB article galley inside the epub.js reader.
*/
public function submissionCallback($hookName, $args)
{
$request = &$args[0];
$application = Application::get();
switch ($application->getName()) {
case 'ojs2':
$issue = &$args[1];
$galley = &$args[2];
$submission = &$args[3];
$submissionNoun = 'article';
break;
case 'ops':
$galley = &$args[1];
$submission = &$args[2];
$submissionNoun = 'preprint';
$issue = null;
break;
default:
throw new Exception('Unknown application!');
}
if (!$galley || $galley->getFileType() !== self::EPUB_MIME_TYPE) {
return false;
}
$galleyPublication = null;
foreach ($submission->getData('publications') as $publication) {
if ($publication->getId() === $galley->getData('publicationId')) {
$galleyPublication = $publication;
break;
}
}
if (!$galleyPublication) {
return false;
}
$templateMgr = TemplateManager::getManager($request);
$epubUrl = $request->url(
null,
$submissionNoun,
'download',
[$submission->getBestId(), $galley->getBestGalleyId(), $galley->getFile()->getId()]
);
$parentUrl = $request->url(null, $submissionNoun, 'view', [$submission->getBestId()]);
$templateMgr->assign([
'displayTemplateResource' => $this->getTemplateResource('display.tpl'),
'pluginUrl' => $request->getBaseUrl() . '/' . $this->getPluginPath(),
'galleyFile' => $galley->getFile(),
'issue' => $issue,
'submission' => $submission,
'submissionNoun' => $submissionNoun,
'bestId' => $galleyPublication->getData('urlPath') ?? $submission->getId(),
'galley' => $galley,
'galleyPublication' => $galleyPublication,
'currentVersionString' => $application->getCurrentVersion()->getVersionString(false),
'isLatestPublication' => $submission->getData('currentPublicationId') === $galley->getData('publicationId'),
'title' => $galleyPublication->getLocalizedTitle(null, 'html'),
'isTitleHtml' => true,
'epubUrl' => $epubUrl,
'parentUrl' => $parentUrl,
'galleyTitle' => __('submission.representationOfTitle', [
'representation' => $galley->getLabel(),
'title' => $galleyPublication->getLocalizedFullTitle(),
]),
'datePublished' => __('submission.outdatedVersion', [
'datePublished' => $galleyPublication->getData('datePublished'),
'urlRecentVersion' => $parentUrl,
]),
]);
$templateMgr->display($this->getTemplateResource('display.tpl'));
return true;
}
/**
* Present an EPUB issue galley inside the epub.js reader.
*/
public function issueCallback($hookName, $args)
{
$request = &$args[0];
$issue = &$args[1];
$galley = &$args[2];
if (!$galley || $galley->getFileType() !== self::EPUB_MIME_TYPE) {
return false;
}
$templateMgr = TemplateManager::getManager($request);
$parentUrl = $request->url(null, 'issue', 'view', [$issue->getBestIssueId()]);
$templateMgr->assign([
'displayTemplateResource' => $this->getTemplateResource('display.tpl'),
'pluginUrl' => $request->getBaseUrl() . '/' . $this->getPluginPath(),
'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' => $parentUrl,
'galleyTitle' => $issue->getLocalizedTitle(),
]);
$templateMgr->display($this->getTemplateResource('display.tpl'));
return true;
}
}