forked from kitodo/kitodo-presentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageViewController.php
More file actions
283 lines (266 loc) · 12.5 KB
/
Copy pathPageViewController.php
File metadata and controls
283 lines (266 loc) · 12.5 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
<?php
/**
* (c) Kitodo. Key to digital objects e.V. <contact@kitodo.org>
*
* This file is part of the Kitodo and TYPO3 projects.
*
* @license GNU General Public License version 3 or later.
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/
namespace Kitodo\Dlf\Controller;
use Kitodo\Dlf\Common\IiifManifest;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Core\Database\Connection;
use Ubl\Iiif\Presentation\Common\Model\Resources\ManifestInterface;
use Ubl\Iiif\Presentation\Common\Vocabulary\Motivation;
/**
* Controller class for the plugin 'Page View'.
*
* @author Sebastian Meyer <sebastian.meyer@slub-dresden.de>
* @package TYPO3
* @subpackage dlf
* @access public
*/
class PageViewController extends AbstractController
{
/**
* Holds the controls to add to the map
*
* @var array
* @access protected
*/
protected $controls = [];
/**
* Holds the current images' URLs and MIME types
*
* @var array
* @access protected
*/
protected $images = [];
/**
* Holds the current fulltexts' URLs
*
* @var array
* @access protected
*/
protected $fulltexts = [];
/**
* Holds the current AnnotationLists / AnnotationPages
*
* @var array
* @access protected
*/
protected $annotationContainers = [];
/**
* The main method of the plugin
*
* @return void
*/
public function mainAction()
{
// Load current document.
$this->loadDocument($this->requestData);
if ($this->isDocMissingOrEmpty()) {
// Quit without doing anything if required variables are not set.
return '';
} else {
if (!empty($this->requestData['logicalPage'])) {
$this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
// The logical page parameter should not appear again
unset($this->requestData['logicalPage']);
}
// Set default values if not set.
// $this->requestData['page'] may be integer or string (physical structure @ID)
if ((int) $this->requestData['page'] > 0 || empty($this->requestData['page'])) {
$this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
} else {
$this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
}
$this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'], 0, 1, 0);
}
// Get image data.
$this->images[0] = $this->getImage($this->requestData['page']);
$this->fulltexts[0] = $this->getFulltext($this->requestData['page']);
$this->annotationContainers[0] = $this->getAnnotationContainers($this->requestData['page']);
if ($this->requestData['double'] && $this->requestData['page'] < $this->document->getDoc()->numPages) {
$this->images[1] = $this->getImage($this->requestData['page'] + 1);
$this->fulltexts[1] = $this->getFulltext($this->requestData['page'] + 1);
$this->annotationContainers[1] = $this->getAnnotationContainers($this->requestData['page'] + 1);
}
// Get the controls for the map.
$this->controls = explode(',', $this->settings['features']);
$this->view->assign('forceAbsoluteUrl', $this->settings['forceAbsoluteUrl']);
$this->addViewerJS();
$this->view->assign('images', $this->images);
$this->view->assign('docId', $this->requestData['id']);
$this->view->assign('page', $this->requestData['page']);
}
/**
* Get fulltext URL and MIME type
*
* @access protected
*
* @param int $page: Page number
*
* @return array URL and MIME type of fulltext file
*/
protected function getFulltext($page)
{
$fulltext = [];
// Get fulltext link.
$fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->extConf['fileGrpFulltext']);
while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$page]]['files'][$fileGrpFulltext])) {
$fulltext['url'] = $this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$page]]['files'][$fileGrpFulltext]);
if ($this->settings['useInternalProxy']) {
// Configure @action URL for form.
$uri = $this->uriBuilder->reset()
->setTargetPageUid($GLOBALS['TSFE']->id)
->setCreateAbsoluteUri(!empty($this->settings['forceAbsoluteUrl']) ? true : false)
->setArguments([
'eID' => 'tx_dlf_pageview_proxy',
'url' => $fulltext['url'],
'uHash' => GeneralUtility::hmac($fulltext['url'], 'PageViewProxy')
])
->build();
$fulltext['url'] = $uri;
}
$fulltext['mimetype'] = $this->document->getDoc()->getFileMimeType($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$page]]['files'][$fileGrpFulltext]);
break;
} else {
$this->logger->notice('No full-text file found for page "' . $page . '" in fileGrp "' . $fileGrpFulltext . '"');
}
}
if (empty($fulltext)) {
$this->logger->notice('No full-text file found for page "' . $page . '" in fileGrps "' . $this->extConf['fileGrpFulltext'] . '"');
}
return $fulltext;
}
/**
* Adds Viewer javascript
*
* @access protected
*
* @return void
*/
protected function addViewerJS()
{
// Viewer configuration.
$viewerConfiguration = '$(document).ready(function() {
if (dlfUtils.exists(dlfViewer)) {
tx_dlf_viewer = new dlfViewer({
controls: ["' . implode('", "', $this->controls) . '"],
div: "' . $this->settings['elementId'] . '",
progressElementId: "' . $this->settings['progressElementId'] . '",
images: ' . json_encode($this->images) . ',
fulltexts: ' . json_encode($this->fulltexts) . ',
annotationContainers: ' . json_encode($this->annotationContainers) . ',
useInternalProxy: ' . ($this->settings['useInternalProxy'] ? 1 : 0) . '
});
}
});';
$this->view->assign('viewerConfiguration', $viewerConfiguration);
}
/**
* Get all AnnotationPages / AnnotationLists that contain text Annotations with motivation "painting"
*
* @access protected
*
* @param int $page: Page number
* @return array An array containing the IRIs of the AnnotationLists / AnnotationPages as well as
* some information about the canvas.
*/
protected function getAnnotationContainers($page)
{
if ($this->document->getDoc() instanceof IiifManifest) {
$canvasId = $this->document->getDoc()->physicalStructure[$page];
$iiif = $this->document->getDoc()->getIiif();
if ($iiif instanceof ManifestInterface) {
$canvas = $iiif->getContainedResourceById($canvasId);
/* @var $canvas \Ubl\Iiif\Presentation\Common\Model\Resources\CanvasInterface */
if ($canvas != null && !empty($canvas->getPossibleTextAnnotationContainers(Motivation::PAINTING))) {
$annotationContainers = [];
/*
* TODO Analyzing the annotations on the server side requires loading the annotation lists / pages
* just to determine whether they contain text annotations for painting. This will take time and lead to a bad user experience.
* It would be better to link every annotation and analyze the data on the client side.
*
* On the other hand, server connections are potentially better than client connections. Downloading annotation lists
*/
foreach ($canvas->getPossibleTextAnnotationContainers(Motivation::PAINTING) as $annotationContainer) {
if (($textAnnotations = $annotationContainer->getTextAnnotations(Motivation::PAINTING)) != null) {
foreach ($textAnnotations as $annotation) {
if (
$annotation->getBody()->getFormat() == 'text/plain'
&& $annotation->getBody()->getChars() != null
) {
$annotationListData = [];
$annotationListData['uri'] = $annotationContainer->getId();
$annotationListData['label'] = $annotationContainer->getLabelForDisplay();
$annotationContainers[] = $annotationListData;
break;
}
}
}
}
$result = [
'canvas' => [
'id' => $canvas->getId(),
'width' => $canvas->getWidth(),
'height' => $canvas->getHeight(),
],
'annotationContainers' => $annotationContainers
];
return $result;
}
}
}
return [];
}
/**
* Get image's URL and MIME type
*
* @access protected
*
* @param int $page: Page number
*
* @return array URL and MIME type of image file
*/
protected function getImage($page)
{
$image = [];
// Get @USE value of METS fileGrp.
$fileGrpsImages = GeneralUtility::trimExplode(',', $this->extConf['fileGrpImages']);
while ($fileGrpImages = array_pop($fileGrpsImages)) {
// Get image link.
if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$page]]['files'][$fileGrpImages])) {
$image['url'] = $this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$page]]['files'][$fileGrpImages]);
$image['mimetype'] = $this->document->getDoc()->getFileMimeType($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$page]]['files'][$fileGrpImages]);
// Only deliver static images via the internal PageViewProxy.
// (For IIP and IIIF, the viewer needs to build and access a separate metadata URL, see `getMetdadataURL` in `OLSources.js`.)
if ($this->settings['useInternalProxy'] && !str_contains(strtolower($image['mimetype']), 'application')) {
// Configure @action URL for form.
$uri = $this->uriBuilder->reset()
->setTargetPageUid($GLOBALS['TSFE']->id)
->setCreateAbsoluteUri(!empty($this->settings['forceAbsoluteUrl']) ? true : false)
->setArguments([
'eID' => 'tx_dlf_pageview_proxy',
'url' => $image['url'],
'uHash' => GeneralUtility::hmac($image['url'], 'PageViewProxy')
])
->build();
$image['url'] = $uri;
}
break;
} else {
$this->logger->notice('No image file found for page "' . $page . '" in fileGrp "' . $fileGrpImages . '"');
}
}
if (empty($image)) {
$this->logger->warning('No image file found for page "' . $page . '" in fileGrps "' . $this->extConf['fileGrpImages'] . '"');
}
return $image;
}
}