[!!!][TASK] Drop "documentation" extension
[Packages/TYPO3.CMS.git] / typo3 / sysext / adminpanel / Classes / Modules / InfoModule.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Adminpanel\Modules;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use TYPO3\CMS\Core\TimeTracker\TimeTracker;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Fluid\View\StandaloneView;
22 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
23
24 /**
25 * Admin Panel Info Module
26 */
27 class InfoModule extends AbstractModule
28 {
29 /**
30 * Creates the content for the "info" section ("module") of the Admin Panel
31 *
32 * @return string HTML content for the section. Consists of a string with table-rows with four columns.
33 * @see display()
34 */
35 public function getContent(): string
36 {
37 $view = GeneralUtility::makeInstance(StandaloneView::class);
38 $templateNameAndPath = $this->extResources . '/Templates/Modules/Info.html';
39 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templateNameAndPath));
40 $view->setPartialRootPaths([$this->extResources . '/Partials']);
41 $tsfe = $this->getTypoScriptFrontendController();
42
43 $view->assignMultiple([
44 'info' => [
45 'pageUid' => $tsfe->id,
46 'pageType' => $tsfe->type,
47 'groupList' => $tsfe->gr_list,
48 'noCache' => $this->isNoCacheEnabled(),
49 'countUserInt' => \count($tsfe->config['INTincScript'] ?? []),
50 'totalParsetime' => $this->getTimeTracker()->getParseTime(),
51 'feUser' => [
52 'uid' => $tsfe->fe_user->user['uid'] ?? 0,
53 'username' => $tsfe->fe_user->user['username'] ?? ''
54 ],
55 'imagesOnPage' => $this->collectImagesOnPage(),
56 'documentSize' => $this->collectDocumentSize()
57 ]
58 ]);
59
60 return $view->render();
61 }
62
63 /**
64 * @inheritdoc
65 */
66 public function getIdentifier(): string
67 {
68 return 'info';
69 }
70
71 /**
72 * @inheritdoc
73 */
74 public function getLabel(): string
75 {
76 $locallangFileAndPath = 'LLL:' . $this->extResources . '/Language/locallang_info.xlf:module.label';
77 return $this->getLanguageService()->sL($locallangFileAndPath);
78 }
79
80 /**
81 * @return TypoScriptFrontendController
82 */
83 protected function getTypoScriptFrontendController(): TypoScriptFrontendController
84 {
85 return $GLOBALS['TSFE'];
86 }
87
88 /**
89 * @return TimeTracker
90 */
91 protected function getTimeTracker(): TimeTracker
92 {
93 return GeneralUtility::makeInstance(TimeTracker::class);
94 }
95
96 /**
97 * Collects images from TypoScriptFrontendController and calculates the total size.
98 * Returns human readable image sizes for fluid template output
99 *
100 * @return array
101 */
102 protected function collectImagesOnPage(): array
103 {
104 $imagesOnPage = [
105 'files' => [],
106 'total' => 0,
107 'totalSize' => 0,
108 'totalSizeHuman' => GeneralUtility::formatSize(0)
109 ];
110
111 if ($this->isNoCacheEnabled() === false) {
112 return $imagesOnPage;
113 }
114
115 $count = 0;
116 $totalImageSize = 0;
117 if (!empty($this->getTypoScriptFrontendController()->imagesOnPage)) {
118 foreach ($this->getTypoScriptFrontendController()->imagesOnPage as $file) {
119 $fileSize = @filesize($file);
120 $imagesOnPage['files'][] = [
121 'name' => $file,
122 'size' => $fileSize,
123 'sizeHuman' => GeneralUtility::formatSize($fileSize)
124 ];
125 $totalImageSize += $fileSize;
126 $count++;
127 }
128 }
129 $imagesOnPage['totalSize'] = GeneralUtility::formatSize($totalImageSize);
130 $imagesOnPage['total'] = $count;
131 return $imagesOnPage;
132 }
133
134 /**
135 * Gets the document size from the current page in a human readable format
136 * @return string
137 */
138 protected function collectDocumentSize(): string
139 {
140 $documentSize = 0;
141 if ($this->isNoCacheEnabled() === true) {
142 $documentSize = \mb_strlen($this->getTypoScriptFrontendController()->content, 'UTF-8');
143 }
144
145 return GeneralUtility::formatSize($documentSize);
146 }
147
148 /**
149 * @return bool
150 */
151 protected function isNoCacheEnabled(): bool
152 {
153 return (bool)$this->getTypoScriptFrontendController()->no_cache;
154 }
155 }