We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 49 |
| Total Lines | 361 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 1 |
Complex classes like PageView often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PageView, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class PageView extends \Kitodo\Dlf\Common\AbstractPlugin |
||
| 29 | { |
||
| 30 | public $scriptRelPath = 'Classes/Plugin/PageView.php'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Holds the controls to add to the map |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | * @access protected |
||
| 37 | */ |
||
| 38 | protected $controls = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Holds the current images' URLs and MIME types |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | * @access protected |
||
| 45 | */ |
||
| 46 | protected $images = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Holds the current fulltexts' URLs |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | * @access protected |
||
| 53 | */ |
||
| 54 | protected $fulltexts = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Holds the current AnnotationLists / AnnotationPages |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | * @access protected |
||
| 61 | */ |
||
| 62 | protected $annotationContainers = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Adds Viewer javascript |
||
| 66 | * |
||
| 67 | * @access protected |
||
| 68 | * |
||
| 69 | * @return string The output string for the ###JAVASCRIPT### template marker |
||
| 70 | */ |
||
| 71 | protected function addViewerJS() |
||
| 72 | { |
||
| 73 | $markerArray = ''; |
||
| 74 | // CSS files. |
||
| 75 | $cssFiles = [ |
||
| 76 | 'Resources/Public/Javascript/OpenLayers/ol3.css' |
||
| 77 | ]; |
||
| 78 | // Javascript files. |
||
| 79 | $jsFiles = [ |
||
| 80 | // OpenLayers |
||
| 81 | 'Resources/Public/Javascript/OpenLayers/glif.min.js', |
||
| 82 | 'Resources/Public/Javascript/OpenLayers/ol3-dlf.js', |
||
| 83 | // Viewer |
||
| 84 | 'Resources/Public/Javascript/PageView/Utility.js', |
||
| 85 | 'Resources/Public/Javascript/PageView/OL3.js', |
||
| 86 | 'Resources/Public/Javascript/PageView/OL3Styles.js', |
||
| 87 | 'Resources/Public/Javascript/PageView/OL3Sources.js', |
||
| 88 | 'Resources/Public/Javascript/PageView/AltoParser.js', |
||
| 89 | 'Resources/Public/Javascript/PageView/AnnotationParser.js', |
||
| 90 | 'Resources/Public/Javascript/PageView/AnnotationControl.js', |
||
| 91 | 'Resources/Public/Javascript/PageView/ImageManipulationControl.js', |
||
| 92 | 'Resources/Public/Javascript/PageView/FulltextControl.js', |
||
| 93 | 'Resources/Public/Javascript/PageView/PageView.js' |
||
| 94 | ]; |
||
| 95 | // Viewer configuration. |
||
| 96 | $viewerConfiguration = ' |
||
| 97 | $(document).ready(function() { |
||
| 98 | if (dlfUtils.exists(dlfViewer)) { |
||
| 99 | tx_dlf_viewer = new dlfViewer({ |
||
| 100 | controls: ["' . implode('", "', $this->controls) . '"], |
||
| 101 | div: "' . $this->conf['elementId'] . '", |
||
| 102 | images: ' . json_encode($this->images) . ', |
||
| 103 | fulltexts: ' . json_encode($this->fulltexts) . ', |
||
| 104 | annotationContainers: ' . json_encode($this->annotationContainers) . ', |
||
| 105 | useInternalProxy: ' . ($this->conf['useInternalProxy'] ? 1 : 0) . ' |
||
| 106 | }); |
||
| 107 | } |
||
| 108 | }); |
||
| 109 | '; |
||
| 110 | // Add Javascript to page footer if not configured otherwise. |
||
| 111 | if (empty($this->conf['addJStoBody'])) { |
||
| 112 | $pageRenderer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Page\PageRenderer::class); |
||
| 113 | foreach ($cssFiles as $cssFile) { |
||
| 114 | $pageRenderer->addCssFile(\TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($this->extKey)) . $cssFile); |
||
| 115 | } |
||
| 116 | foreach ($jsFiles as $jsFile) { |
||
| 117 | $pageRenderer->addJsFooterFile(\TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($this->extKey)) . $jsFile); |
||
| 118 | } |
||
| 119 | $pageRenderer->addJsFooterInlineCode('kitodo-pageview-configuration', $viewerConfiguration); |
||
| 120 | } else { |
||
| 121 | foreach ($jsFiles as $jsFile) { |
||
| 122 | $markerArray .= '<script type="text/javascript" src="' . \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($this->extKey)) . $jsFile . '"></script>' . "\n"; |
||
| 123 | } |
||
| 124 | $markerArray .= ' |
||
| 125 | <script type="text/javascript"> |
||
| 126 | /*<![CDATA[*/ |
||
| 127 | /*kitodo-pageview-configuration*/ |
||
| 128 | ' . $viewerConfiguration . ' |
||
| 129 | /*]]>*/ |
||
| 130 | </script>'; |
||
| 131 | } |
||
| 132 | return $markerArray; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Adds pageview interaction (crop, magnifier and rotation) |
||
| 137 | * |
||
| 138 | * @access protected |
||
| 139 | * |
||
| 140 | * @return array Marker array |
||
| 141 | */ |
||
| 142 | protected function addInteraction() |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Adds form to save cropping data to basket |
||
| 164 | * |
||
| 165 | * @access protected |
||
| 166 | * |
||
| 167 | * @return array Marker array |
||
| 168 | */ |
||
| 169 | protected function addBasketForm() |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Get image's URL and MIME type |
||
| 211 | * |
||
| 212 | * @access protected |
||
| 213 | * |
||
| 214 | * @param int $page: Page number |
||
| 215 | * |
||
| 216 | * @return array URL and MIME type of image file |
||
| 217 | */ |
||
| 218 | protected function getImage($page) |
||
| 219 | { |
||
| 220 | $image = []; |
||
| 221 | // Get @USE value of METS fileGrp. |
||
| 222 | $fileGrps = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->conf['fileGrps']); |
||
| 223 | while ($fileGrp = @array_pop($fileGrps)) { |
||
| 224 | // Get image link. |
||
| 225 | if (!empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$page]]['files'][$fileGrp])) { |
||
| 226 | $image['url'] = $this->doc->getFileLocation($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$page]]['files'][$fileGrp]); |
||
| 227 | if ($this->conf['useInternalProxy']) { |
||
| 228 | // Configure @action URL for form. |
||
| 229 | $linkConf = [ |
||
| 230 | 'parameter' => $GLOBALS['TSFE']->id, |
||
| 231 | 'forceAbsoluteUrl' => !empty($this->conf['forceAbsoluteUrl']) ? 1 : 0, |
||
| 232 | 'forceAbsoluteUrl.' => ['scheme' => !empty($this->conf['forceAbsoluteUrl']) && !empty($this->conf['forceAbsoluteUrlHttps']) ? 'https' : 'http'], |
||
| 233 | 'additionalParams' => '&eID=tx_dlf_pageview_proxy&url=' . urlencode($image['url']), |
||
| 234 | ]; |
||
| 235 | $image['url'] = $this->cObj->typoLink_URL($linkConf); |
||
| 236 | } |
||
| 237 | $image['mimetype'] = $this->doc->getFileMimeType($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$page]]['files'][$fileGrp]); |
||
| 238 | break; |
||
| 239 | } else { |
||
| 240 | Helper::devLog('File not found in fileGrp "' . $fileGrp . '"', DEVLOG_SEVERITY_WARNING); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | return $image; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Get fulltext URL and MIME type |
||
| 248 | * |
||
| 249 | * @access protected |
||
| 250 | * |
||
| 251 | * @param int $page: Page number |
||
| 252 | * |
||
| 253 | * @return array URL and MIME type of fulltext file |
||
| 254 | */ |
||
| 255 | protected function getFulltext($page) |
||
| 256 | { |
||
| 257 | $fulltext = []; |
||
| 258 | // Get fulltext link. |
||
| 259 | if (!empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$page]]['files'][$this->conf['fileGrpFulltext']])) { |
||
| 260 | $fulltext['url'] = $this->doc->getFileLocation($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$page]]['files'][$this->conf['fileGrpFulltext']]); |
||
| 261 | if ($this->conf['useInternalProxy']) { |
||
| 262 | // Configure @action URL for form. |
||
| 263 | $linkConf = [ |
||
| 264 | 'parameter' => $GLOBALS['TSFE']->id, |
||
| 265 | 'forceAbsoluteUrl' => !empty($this->conf['forceAbsoluteUrl']) ? 1 : 0, |
||
| 266 | 'forceAbsoluteUrl.' => ['scheme' => !empty($this->conf['forceAbsoluteUrl']) && !empty($this->conf['forceAbsoluteUrlHttps']) ? 'https' : 'http'], |
||
| 267 | 'additionalParams' => '&eID=tx_dlf_pageview_proxy&url=' . urlencode($fulltext['url']), |
||
| 268 | ]; |
||
| 269 | $fulltext['url'] = $this->cObj->typoLink_URL($linkConf); |
||
| 270 | } |
||
| 271 | $fulltext['mimetype'] = $this->doc->getFileMimeType($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$page]]['files'][$this->conf['fileGrpFulltext']]); |
||
| 272 | } else { |
||
| 273 | Helper::devLog('File not found in fileGrp "' . $this->conf['fileGrpFulltext'] . '"', DEVLOG_SEVERITY_WARNING); |
||
| 274 | } |
||
| 275 | return $fulltext; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Get all AnnotationPages / AnnotationLists that contain text Annotations with motivation "painting" |
||
| 280 | * |
||
| 281 | * @access protected |
||
| 282 | * |
||
| 283 | * @param int $page: Page number |
||
| 284 | * @return array An array containing the IRIs of the AnnotationLists / AnnotationPages as well as |
||
| 285 | * some information about the canvas. |
||
| 286 | */ |
||
| 287 | protected function getAnnotationContainers($page) |
||
| 288 | { |
||
| 289 | if ($this->doc instanceof IiifManifest) { |
||
| 290 | $canvasId = $this->doc->physicalStructure[$page]; |
||
| 291 | $iiif = $this->doc->getIiif(); |
||
| 292 | if ($iiif instanceof ManifestInterface) { |
||
|
|
|||
| 293 | $canvas = $iiif->getContainedResourceById($canvasId); |
||
| 294 | /* @var $canvas \Ubl\Iiif\Presentation\Common\Model\Resources\CanvasInterface */ |
||
| 295 | if ($canvas != null && !empty($canvas->getPossibleTextAnnotationContainers(Motivation::PAINTING))) { |
||
| 296 | $annotationContainers = []; |
||
| 297 | /* |
||
| 298 | * TODO Analyzing the annotations on the server side requires loading the annotation lists / pages |
||
| 299 | * just to determine wether they contain text annotations for painting. This will take time and lead to a bad user experience. |
||
| 300 | * It would be better to link every annotation and analyze the data on the client side. |
||
| 301 | * |
||
| 302 | * On the other hand, server connections are potentially better than client connections. Downloading annotation lists |
||
| 303 | */ |
||
| 304 | foreach ($canvas->getPossibleTextAnnotationContainers(Motivation::PAINTING) as $annotationContainer) { |
||
| 305 | if (($textAnnotations = $annotationContainer->getTextAnnotations(Motivation::PAINTING)) != null) { |
||
| 306 | foreach ($textAnnotations as $annotation) { |
||
| 307 | if ( |
||
| 308 | $annotation->getBody()->getFormat() == 'text/plain' |
||
| 309 | && $annotation->getBody()->getChars() != null |
||
| 310 | ) { |
||
| 311 | $annotationListData = []; |
||
| 312 | $annotationListData['uri'] = $annotationContainer->getId(); |
||
| 313 | $annotationListData['label'] = $annotationContainer->getLabelForDisplay(); |
||
| 314 | $annotationContainers[] = $annotationListData; |
||
| 315 | break; |
||
| 316 | } |
||
| 317 | } |
||
| 318 | } |
||
| 319 | } |
||
| 320 | $result = [ |
||
| 321 | 'canvas' => [ |
||
| 322 | 'id' => $canvas->getId(), |
||
| 323 | 'width' => $canvas->getWidth(), |
||
| 324 | 'height' => $canvas->getHeight(), |
||
| 325 | ], |
||
| 326 | 'annotationContainers' => $annotationContainers |
||
| 327 | ]; |
||
| 328 | return $result; |
||
| 329 | } |
||
| 330 | } |
||
| 331 | } |
||
| 332 | return []; |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * The main method of the PlugIn |
||
| 337 | * |
||
| 338 | * @access public |
||
| 339 | * |
||
| 340 | * @param string $content: The PlugIn content |
||
| 341 | * @param array $conf: The PlugIn configuration |
||
| 342 | * |
||
| 343 | * @return string The content that is displayed on the website |
||
| 344 | */ |
||
| 345 | public function main($content, $conf) |
||
| 389 | } |
||
| 390 | } |
||
| 391 |