[TASK] Speed up page ID resolving in Extbase BE
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Configuration / BackendConfigurationManager.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Configuration;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
8 * Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the text file GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * A general purpose configuration manager used in backend mode.
32 */
33 class BackendConfigurationManager extends \TYPO3\CMS\Extbase\Configuration\AbstractConfigurationManager {
34
35 /**
36 * Needed to recursively fetch a page tree
37 *
38 * @var \TYPO3\CMS\Core\Database\QueryGenerator
39 * @inject
40 */
41 protected $queryGenerator;
42
43 /**
44 * @var array
45 */
46 protected $typoScriptSetupCache = array();
47
48 /**
49 * stores the current page ID
50 * @var integer
51 */
52 protected $currentPageId;
53
54 /**
55 * Returns TypoScript Setup array from current Environment.
56 *
57 * @return array the raw TypoScript setup
58 */
59 public function getTypoScriptSetup() {
60 $pageId = $this->getCurrentPageId();
61
62 if (!array_key_exists($pageId, $this->typoScriptSetupCache)) {
63 /** @var $template \TYPO3\CMS\Core\TypoScript\TemplateService */
64 $template = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\TemplateService');
65 // do not log time-performance information
66 $template->tt_track = 0;
67 // Explicitly trigger processing of extension static files
68 $template->setProcessExtensionStatics(TRUE);
69 $template->init();
70 // Get the root line
71 $rootline = array();
72 if ($pageId > 0) {
73 /** @var $sysPage \TYPO3\CMS\Frontend\Page\PageRepository */
74 $sysPage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Page\\PageRepository');
75 // Get the rootline for the current page
76 $rootline = $sysPage->getRootLine($pageId, '', TRUE);
77 }
78 // This generates the constants/config + hierarchy info for the template.
79 $template->runThroughTemplates($rootline, 0);
80 $template->generateConfig();
81 $this->typoScriptSetupCache[$pageId] = $template->setup;
82 }
83 return $this->typoScriptSetupCache[$pageId];
84 }
85
86 /**
87 * Returns the TypoScript configuration found in module.tx_yourextension_yourmodule
88 * merged with the global configuration of your extension from module.tx_yourextension
89 *
90 * @param string $extensionName
91 * @param string $pluginName in BE mode this is actually the module signature. But we're using it just like the plugin name in FE
92 * @return array
93 */
94 protected function getPluginConfiguration($extensionName, $pluginName = NULL) {
95 $setup = $this->getTypoScriptSetup();
96 $pluginConfiguration = array();
97 if (is_array($setup['module.']['tx_' . strtolower($extensionName) . '.'])) {
98 $pluginConfiguration = $this->typoScriptService->convertTypoScriptArrayToPlainArray($setup['module.']['tx_' . strtolower($extensionName) . '.']);
99 }
100 if ($pluginName !== NULL) {
101 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
102 if (is_array($setup['module.']['tx_' . $pluginSignature . '.'])) {
103 $overruleConfiguration = $this->typoScriptService->convertTypoScriptArrayToPlainArray($setup['module.']['tx_' . $pluginSignature . '.']);
104 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($pluginConfiguration, $overruleConfiguration);
105 }
106 }
107 return $pluginConfiguration;
108 }
109
110 /**
111 * Returns the configured controller/action pairs of the specified module in the format
112 * array(
113 * 'Controller1' => array('action1', 'action2'),
114 * 'Controller2' => array('action3', 'action4')
115 * )
116 *
117 * @param string $extensionName
118 * @param string $pluginName in BE mode this is actually the module signature. But we're using it just like the plugin name in FE
119 * @return array
120 */
121 protected function getSwitchableControllerActions($extensionName, $pluginName) {
122 $switchableControllerActions = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$pluginName]['controllers'];
123 if (!is_array($switchableControllerActions)) {
124 $switchableControllerActions = array();
125 }
126 return $switchableControllerActions;
127 }
128
129 /**
130 * Returns the page uid of the current page.
131 * If no page is selected, we'll return the uid of the first root page.
132 *
133 * @return integer current page id. If no page is selected current root page id is returned
134 */
135 protected function getCurrentPageId() {
136 if ($this->currentPageId !== NULL) {
137 return $this->currentPageId;
138 }
139
140 $this->currentPageId = $this->getCurrentPageIdFromGetPostData() ?: $this->getCurrentPageIdFromCurrentSiteRoot();
141 $this->currentPageId = $this->currentPageId ?: $this->getCurrentPageIdFromRootTemplate();
142 $this->currentPageId = $this->currentPageId ?: self::DEFAULT_BACKEND_STORAGE_PID;
143
144 return $this->currentPageId;
145 }
146
147 /**
148 * Gets the current page ID from the GET/POST data.
149 *
150 * @return int the page UID, will be 0 if none has been set
151 */
152 protected function getCurrentPageIdFromGetPostData() {
153 return (int)\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('id');
154 }
155
156 /**
157 * Gets the current page ID from the first site root in tree.
158 *
159 * @return int the page UID, will be 0 if none has been set
160 */
161 protected function getCurrentPageIdFromCurrentSiteRoot() {
162 $rootPage = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
163 'uid', 'pages', 'deleted=0 AND hidden=0 AND is_siteroot=1', '', 'sorting'
164 );
165 if (empty($rootPage)) {
166 return 0;
167 }
168
169 return (int)$rootPage['uid'];
170 }
171
172 /**
173 * Gets the current page ID from the first created root template.
174 *
175 * @return int the page UID, will be 0 if none has been set
176 */
177 protected function getCurrentPageIdFromRootTemplate() {
178 $rootTemplate = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
179 'pid', 'sys_template', 'deleted=0 AND hidden=0 AND root=1', '', 'crdate'
180 );
181 if (empty($rootTemplate)) {
182 return 0;
183 }
184
185 return (int)$rootTemplate['pid'];
186 }
187
188 /**
189 * Returns the default backend storage pid
190 *
191 * @return string
192 */
193 public function getDefaultBackendStoragePid() {
194 return $this->getCurrentPageId();
195 }
196
197 /**
198 * We need to set some default request handler if the framework configuration
199 * could not be loaded; to make sure Extbase also works in Backend modules
200 * in all contexts.
201 *
202 * @param array $frameworkConfiguration
203 * @return array
204 */
205 protected function getContextSpecificFrameworkConfiguration(array $frameworkConfiguration) {
206 if (!isset($frameworkConfiguration['mvc']['requestHandlers'])) {
207 $frameworkConfiguration['mvc']['requestHandlers'] = array(
208 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\FrontendRequestHandler' => 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\FrontendRequestHandler',
209 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\BackendRequestHandler' => 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\BackendRequestHandler'
210 );
211 }
212 return $frameworkConfiguration;
213 }
214
215
216 /**
217 * Returns a comma separated list of storagePid that are below a certain storage pid.
218 *
219 * @param string $storagePid Storage PID to start at; multiple PIDs possible as comma-separated list
220 * @param integer $recursionDepth Maximum number of levels to search, 0 to disable recursive lookup
221 * @return string storage PIDs
222 */
223 protected function getRecursiveStoragePids($storagePid, $recursionDepth = 0) {
224 if ($recursionDepth <= 0) {
225 return $storagePid;
226 }
227
228 $recursiveStoragePids = '';
229 $storagePids = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $storagePid);
230 foreach ($storagePids as $startPid) {
231 $pids = $this->queryGenerator->getTreeList($startPid, $recursionDepth, 0, 1);
232 if (strlen($pids) > 0) {
233 $recursiveStoragePids .= $pids . ',';
234 }
235 }
236
237 return rtrim($recursiveStoragePids, ',');
238 }
239
240 }