[FEATURE] Add Contexts for storing data access modes
[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\Context\Context;
20 use TYPO3\CMS\Core\Context\UserAspect;
21 use TYPO3\CMS\Core\TimeTracker\TimeTracker;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Fluid\View\StandaloneView;
24 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
25
26 /**
27 * Admin Panel Info Module
28 */
29 class InfoModule extends AbstractModule
30 {
31 /**
32 * Creates the content for the "info" section ("module") of the Admin Panel
33 *
34 * @return string HTML content for the section. Consists of a string with table-rows with four columns.
35 * @see display()
36 */
37 public function getContent(): string
38 {
39 $view = GeneralUtility::makeInstance(StandaloneView::class);
40 $templateNameAndPath = $this->extResources . '/Templates/Modules/Info.html';
41 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templateNameAndPath));
42 $view->setPartialRootPaths([$this->extResources . '/Partials']);
43 $tsfe = $this->getTypoScriptFrontendController();
44
45 /** @var UserAspect $frontendUserAspect */
46 $frontendUserAspect = GeneralUtility::makeInstance(Context::class)->getAspect('frontend.user');
47 $view->assignMultiple([
48 'info' => [
49 'pageUid' => $tsfe->id,
50 'pageType' => $tsfe->type,
51 'groupList' => implode(',', $frontendUserAspect->getGroupIds()),
52 'noCache' => $this->isNoCacheEnabled(),
53 'countUserInt' => \count($tsfe->config['INTincScript'] ?? []),
54 'totalParsetime' => $this->getTimeTracker()->getParseTime(),
55 'feUser' => [
56 'uid' => $frontendUserAspect->get('id'),
57 'username' => $frontendUserAspect->get('username')
58 ],
59 'imagesOnPage' => $this->collectImagesOnPage(),
60 'documentSize' => $this->collectDocumentSize()
61 ]
62 ]);
63
64 return $view->render();
65 }
66
67 /**
68 * @inheritdoc
69 */
70 public function getIdentifier(): string
71 {
72 return 'info';
73 }
74
75 /**
76 * @inheritdoc
77 */
78 public function getLabel(): string
79 {
80 $locallangFileAndPath = 'LLL:' . $this->extResources . '/Language/locallang_info.xlf:module.label';
81 return $this->getLanguageService()->sL($locallangFileAndPath);
82 }
83
84 /**
85 * @return TypoScriptFrontendController
86 */
87 protected function getTypoScriptFrontendController(): TypoScriptFrontendController
88 {
89 return $GLOBALS['TSFE'];
90 }
91
92 /**
93 * @return TimeTracker
94 */
95 protected function getTimeTracker(): TimeTracker
96 {
97 return GeneralUtility::makeInstance(TimeTracker::class);
98 }
99
100 /**
101 * Collects images from TypoScriptFrontendController and calculates the total size.
102 * Returns human readable image sizes for fluid template output
103 *
104 * @return array
105 */
106 protected function collectImagesOnPage(): array
107 {
108 $imagesOnPage = [
109 'files' => [],
110 'total' => 0,
111 'totalSize' => 0,
112 'totalSizeHuman' => GeneralUtility::formatSize(0)
113 ];
114
115 if ($this->isNoCacheEnabled() === false) {
116 return $imagesOnPage;
117 }
118
119 $count = 0;
120 $totalImageSize = 0;
121 if (!empty($this->getTypoScriptFrontendController()->imagesOnPage)) {
122 foreach ($this->getTypoScriptFrontendController()->imagesOnPage as $file) {
123 $fileSize = @filesize($file);
124 $imagesOnPage['files'][] = [
125 'name' => $file,
126 'size' => $fileSize,
127 'sizeHuman' => GeneralUtility::formatSize($fileSize)
128 ];
129 $totalImageSize += $fileSize;
130 $count++;
131 }
132 }
133 $imagesOnPage['totalSize'] = GeneralUtility::formatSize($totalImageSize);
134 $imagesOnPage['total'] = $count;
135 return $imagesOnPage;
136 }
137
138 /**
139 * Gets the document size from the current page in a human readable format
140 * @return string
141 */
142 protected function collectDocumentSize(): string
143 {
144 $documentSize = 0;
145 if ($this->isNoCacheEnabled() === true) {
146 $documentSize = \mb_strlen($this->getTypoScriptFrontendController()->content, 'UTF-8');
147 }
148
149 return GeneralUtility::formatSize($documentSize);
150 }
151
152 /**
153 * @return bool
154 */
155 protected function isNoCacheEnabled(): bool
156 {
157 return (bool)$this->getTypoScriptFrontendController()->no_cache;
158 }
159 }