Fixed bug #17275: Pagetree preloads huge amount of pages with initial load / refresh
[Packages/TYPO3.CMS.git] / t3lib / tree / pagetree / class.t3lib_tree_pagetree_dataprovider.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 TYPO3 Tree Team <http://forge.typo3.org/projects/typo3v4-extjstrees>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Abstract Tree Data Provider
30 *
31 * @author Stefan Galinski <stefan.galinski@gmail.com>
32 * @package TYPO3
33 * @subpackage t3lib
34 */
35 class t3lib_tree_pagetree_DataProvider extends t3lib_tree_AbstractDataProvider {
36 /**
37 * Node limit that should be loaded for this request per mount
38 *
39 * @var int
40 */
41 protected $nodeLimit = 0;
42
43 /**
44 * Current amount of nodes
45 *
46 * @var int
47 */
48 protected $nodeCounter = 0;
49
50 /**
51 * Hidden Records
52 *
53 * @var array
54 */
55 protected $hiddenRecords = array();
56
57 /**
58 * Constructor
59 *
60 * @param int $nodeLimit (optional)
61 */
62 public function __construct($nodeLimit = NULL) {
63 if ($nodeLimit === NULL) {
64 $nodeLimit = $GLOBALS['BE']['pageTree']['preloadLimit'];
65 }
66 $this->nodeLimit = abs(intval($nodeLimit));
67
68 $this->hiddenRecords = t3lib_div::trimExplode(
69 ',',
70 $GLOBALS['BE_USER']->getTSConfigVal('options.hideRecords.pages')
71 );
72 }
73
74 /**
75 * Returns the root node
76 *
77 * @return t3lib_tree_Node
78 */
79 public function getRoot() {
80 /** @var $node t3lib_tree_pagetree_Node */
81 $node = t3lib_div::makeInstance('t3lib_tree_pagetree_Node');
82 $node->setId('root');
83 $node->setExpanded(true);
84
85 return $node;
86 }
87
88 /**
89 * Fetches the sub-nodes of the given node
90 *
91 * @param t3lib_tree_Node $node
92 * @param int $mountPoint
93 * @param int $level internally used variable as a recursion limiter
94 * @return t3lib_tree_NodeCollection
95 */
96 public function getNodes(t3lib_tree_Node $node, $mountPoint = 0, $level = 0) {
97 /** @var $nodeCollection t3lib_tree_pagetree_NodeCollection */
98 $nodeCollection = t3lib_div::makeInstance('t3lib_tree_pagetree_NodeCollection');
99 if ($level >= 99) {
100 return $nodeCollection;
101 }
102
103 $subpages = $this->getSubpages($node->getId());
104 if (!is_array($subpages) || !count($subpages)) {
105 return $nodeCollection;
106 }
107
108 foreach ($subpages as $subpage) {
109 if (in_array($subpage['uid'], $this->hiddenRecords)) {
110 continue;
111 }
112
113 $subpage = t3lib_befunc::getRecordWSOL('pages', $subpage['uid'], '*', '', TRUE, TRUE);
114 if (!$subpage) {
115 continue;
116 }
117
118 $subNode = t3lib_tree_pagetree_Commands::getNewNode($subpage, $mountPoint);
119 if ($this->nodeCounter < $this->nodeLimit) {
120 $childNodes = $this->getNodes($subNode, $mountPoint, $level + 1);
121 $subNode->setChildNodes($childNodes);
122 $this->nodeCounter += $childNodes->count();
123 } else {
124 $subNode->setLeaf(!$this->hasNodeSubPages($subNode->getId()));
125 }
126
127 $nodeCollection->append($subNode);
128 }
129
130 return $nodeCollection;
131 }
132
133 /**
134 * Returns a node collection of filtered nodes
135 *
136 * @param t3lib_tree_Node $node
137 * @param string $searchFilter
138 * @param int $mountPoint
139 * @return void
140 */
141 public function getFilteredNodes(t3lib_tree_Node $node, $searchFilter, $mountPoint = 0) {
142 /** @var $nodeCollection t3lib_tree_pagetree_NodeCollection */
143 $nodeCollection = t3lib_div::makeInstance('t3lib_tree_pagetree_NodeCollection');
144
145 $records = $this->getSubpages(-1, $searchFilter);
146 if (!is_array($records) || !count($records)) {
147 return $nodeCollection;
148 }
149
150 $isNumericSearchFilter = (is_numeric($searchFilter) && $searchFilter > 0);
151 $nodeId = intval($node->getId());
152 foreach ($records as $record) {
153 $record = t3lib_tree_pagetree_Commands::getNodeRecord($record['uid']);
154 if (intval($record['pid']) === -1 || in_array($record['uid'], $this->hiddenRecords)) {
155 continue;
156 }
157
158 $rootline = t3lib_BEfunc::BEgetRootLine($record['uid'], ' AND uid != ' . $nodeId);
159 $rootline = array_reverse($rootline);
160 if ($nodeId === 0) {
161 array_shift($rootline);
162 }
163 $reference = $nodeCollection;
164
165 $inFilteredRootline = FALSE;
166 $amountOfRootlineElements = count($rootline);
167 for ($i = 0; $i < $amountOfRootlineElements; ++$i) {
168 $rootlineElement = $rootline[$i];
169 if (intval($rootlineElement['pid']) === $nodeId) {
170 $inFilteredRootline = TRUE;
171 }
172
173 if (!$inFilteredRootline) {
174 continue;
175 }
176
177 $rootlineElement = t3lib_tree_pagetree_Commands::getNodeRecord($rootlineElement['uid']);
178 $ident = intval($rootlineElement['sorting']) . intval($rootlineElement['uid']);
179 if ($reference->offsetExists($ident)) {
180 /** @var $refNode t3lib_tree_pagetree_Node */
181 $refNode = $reference->offsetGet($ident);
182 $refNode->setExpanded(TRUE);
183 $refNode->setLeaf(FALSE);
184
185 $reference = $refNode->getChildNodes();
186 continue;
187 }
188
189 $refNode = t3lib_tree_pagetree_Commands::getNewNode($rootlineElement, $mountPoint);
190 $replacement = '<span class="typo3-pagetree-filteringTree-highlight">$1</span>';
191 if ($isNumericSearchFilter && intval($rootlineElement['uid']) === intval($searchFilter)) {
192 $text = str_replace('$1', $refNode->getText(), $replacement);
193 } else {
194 $text = preg_replace('/(' . $searchFilter . ')/i', $replacement, $refNode->getText());
195 }
196
197 $refNode->setText(
198 $text,
199 $refNode->getTextSourceField(),
200 $refNode->getPrefix(),
201 $refNode->getSuffix()
202 );
203
204 /** @var $childCollection t3lib_tree_pagetree_NodeCollection */
205 $childCollection = t3lib_div::makeInstance('t3lib_tree_pagetree_NodeCollection');
206
207 if (($i +1) >= $amountOfRootlineElements) {
208 $childNodes = $this->getNodes($refNode, $mountPoint);
209 foreach ($childNodes as $childNode) {
210 /** @var $childNode t3lib_tree_pagetree_Node */
211 $childRecord = $childNode->getRecord();
212 $childIdent = intval($childRecord['sorting']) . intval($childRecord['uid']);
213 $childCollection->offsetSet($childIdent, $childNode);
214 }
215 $refNode->setChildNodes($childNodes);
216 }
217
218 $refNode->setChildNodes($childCollection);
219 $reference->offsetSet($ident, $refNode);
220 $reference->ksort();
221
222 $reference = $childCollection;
223 }
224 }
225
226 return $nodeCollection;
227 }
228
229 /**
230 * Returns the page tree mounts for the current user
231 *
232 * Note: If you add the search filter parameter, the nodes will be filtered by this string.
233 *
234 * @param string $searchFilter
235 * @return array
236 */
237 public function getTreeMounts($searchFilter = '') {
238 /** @var $nodeCollection t3lib_tree_pagetree_NodeCollection */
239 $nodeCollection = t3lib_div::makeInstance('t3lib_tree_pagetree_NodeCollection');
240
241 $isTemporaryMountPoint = FALSE;
242 $mountPoints = intval($GLOBALS['BE_USER']->uc['pageTree_temporaryMountPoint']);
243 if (!$mountPoints) {
244 $mountPoints = array_map('intval', $GLOBALS['BE_USER']->returnWebmounts());
245 $mountPoints = array_unique($mountPoints);
246 } else {
247 $isTemporaryMountPoint = TRUE;
248 $mountPoints = array($mountPoints);
249 }
250
251 if (!count($mountPoints)) {
252 return $nodeCollection;
253 }
254
255 $showRootlineAboveMounts = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showPathAboveMounts');
256 $class = (count($mountPoints) <= 1 ? 'typo3-pagetree-node-notExpandable' : '');
257 foreach ($mountPoints as $mountPoint) {
258 if ($mountPoint === 0) {
259 $sitename = 'TYPO3';
260 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] !== '') {
261 $sitename = $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
262 }
263
264 $record = array(
265 'uid' => 0,
266 'title' => $sitename,
267 );
268 $subNode = t3lib_tree_pagetree_Commands::getNewNode($record);
269 $subNode->setLabelIsEditable(FALSE);
270 $subNode->setType('pages_root');
271 } else {
272 $record = t3lib_BEfunc::getRecordWSOL('pages', $mountPoint, '*', '', TRUE);
273 if (!$record) {
274 continue;
275 }
276
277 $subNode = t3lib_tree_pagetree_Commands::getNewNode($record, $mountPoint);
278 if ($showRootlineAboveMounts && !$isTemporaryMountPoint) {
279 $rootline = t3lib_tree_pagetree_Commands::getMountPointPath($record['uid']);
280 $subNode->setReadableRootline($rootline);
281 }
282 }
283
284 $subNode->setIsMountPoint(TRUE);
285 $subNode->setExpanded(TRUE);
286 $subNode->setDraggable(FALSE);
287 $subNode->setIsDropTarget(FALSE);
288 $subNode->setCls($class);
289
290 if ($searchFilter === '') {
291 $childNodes = $this->getNodes($subNode, $mountPoint);
292 } else {
293 $childNodes = $this->getFilteredNodes($subNode, $searchFilter, $mountPoint);
294 $subNode->setExpanded(TRUE);
295 }
296
297 $subNode->setChildNodes($childNodes);
298 $nodeCollection->append($subNode);
299 }
300
301 return $nodeCollection;
302 }
303
304 /**
305 * Returns the where clause for fetching pages
306 *
307 * @param int $id
308 * @param string $searchFilter
309 * @return string
310 */
311 protected function getWhereClause($id, $searchFilter = '') {
312 $where = $GLOBALS['BE_USER']->getPagePermsClause(1) .
313 t3lib_BEfunc::deleteClause('pages') .
314 t3lib_BEfunc::versioningPlaceholderClause('pages');
315
316 if (is_numeric($id) && $id >= 0) {
317 $where .= ' AND pid= ' . $GLOBALS['TYPO3_DB']->fullQuoteStr(intval($id), 'pages');
318 }
319
320 if ($searchFilter !== '') {
321 if (is_numeric($searchFilter) && $searchFilter > 0) {
322 $seachWhere .= 'uid = ' . intval($searchFilter) . ' OR ';
323 }
324
325 $searchFilter = $GLOBALS['TYPO3_DB']->fullQuoteStr('%' . $searchFilter . '%', 'pages');
326 $useNavTitle = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showNavTitle');
327
328 if ($useNavTitle) {
329 $seachWhere .= '(nav_title LIKE ' . $searchFilter .
330 ' OR (nav_title = "" && title LIKE ' . $searchFilter . '))';
331 } else {
332 $seachWhere .= 'title LIKE ' . $searchFilter;
333 }
334
335 $where .= ' AND (' . $seachWhere . ')';
336 }
337
338 return $where;
339 }
340
341 /**
342 * Returns all sub-pages of a given id
343 *
344 * @param int $id
345 * @param string $searchFilter
346 * @return array
347 */
348 protected function getSubpages($id, $searchFilter = '') {
349 $where = $this->getWhereClause($id, $searchFilter);
350 $subpages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
351 'uid', 'pages', $where, '', 'sorting', '', 'uid'
352 );
353
354 return $subpages;
355 }
356
357 /**
358 * Returns true if the node has child's
359 *
360 * @param int $id
361 * @return bool
362 */
363 protected function hasNodeSubPages($id) {
364 $where = $this->getWhereClause($id);
365 $subpage = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
366 'uid', 'pages', $where, '', 'sorting', '', 'uid'
367 );
368
369 $returnValue = TRUE;
370 if (!$subpage['uid']) {
371 $returnValue = FALSE;
372 }
373
374 return $returnValue;
375 }
376 }
377
378 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/tree/pagetree/class.t3lib_tree_pagetree_dataprovider.php'])) {
379 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/tree/pagetree/class.t3lib_tree_pagetree_dataprovider.php']);
380 }
381
382 ?>