5affbdc661a6ee5e5b60204015a43de155aff70f
[Packages/TYPO3.CMS.git] / typo3 / sysext / indexed_search / Classes / Controller / ModuleController.php
1 <?php
2 namespace TYPO3\CMS\IndexedSearch\Controller;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2013 Kasper Skårhøj (kasperYYYY@typo3.com)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use TYPO3\CMS\Backend\Utility\BackendUtility;
31
32 /**
33 * Backend module providing boring statistics of the index-tables.
34 *
35 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
36 */
37 class ModuleController {
38
39 /**
40 * @todo Define visibility
41 */
42 public $MCONF = array();
43
44 /**
45 * @todo Define visibility
46 */
47 public $MOD_MENU = array();
48
49 /**
50 * @todo Define visibility
51 */
52 public $MOD_SETTINGS = array();
53
54 /**
55 * document template object
56 *
57 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
58 * @todo Define visibility
59 */
60 public $doc;
61
62 /**
63 * @todo Define visibility
64 */
65 public $include_once = array();
66
67 /**
68 * @todo Define visibility
69 */
70 public $content;
71
72 /**
73 * Initialization
74 *
75 * @return void
76 * @todo Define visibility
77 */
78 public function init() {
79 $this->MCONF = $GLOBALS['MCONF'];
80 $this->menuConfig();
81 $this->doc = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Template\\DocumentTemplate');
82 $this->doc->form = '<form action="" method="post">';
83 $this->doc->backPath = $GLOBALS['BACK_PATH'];
84 $this->doc->setModuleTemplate(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extRelPath('indexed_search') . '/mod/mod_template.html');
85 $this->doc->tableLayout = array(
86 'defRow' => array(
87 '0' => array('<td valign="top" nowrap>', '</td>'),
88 'defCol' => array('<td><img src="' . $this->doc->backPath . 'clear.gif" width=10 height=1></td><td valign="top" nowrap>', '</td>')
89 )
90 );
91 $indexer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\IndexedSearch\\Indexer');
92 $indexer->initializeExternalParsers();
93 }
94
95 /**
96 * MENU-ITEMS:
97 * If array, then it's a selector box menu
98 * If empty string it's just a variable, that'll be saved.
99 * Values NOT in this array will not be saved in the settings-array for the module.
100 *
101 * @return void
102 * @todo Define visibility
103 */
104 public function menuConfig() {
105 $this->MOD_MENU = array(
106 'function' => array(
107 'stat' => 'General statistics',
108 'typo3pages' => 'List: TYPO3 Pages',
109 'externalDocs' => 'List: External documents'
110 )
111 );
112 // cleanse settings
113 $this->MOD_SETTINGS = BackendUtility::getModuleData($this->MOD_MENU, \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('SET'), $this->MCONF['name'], 'ses');
114 }
115
116 /**
117 * Main function to generate the content
118 *
119 * @return void
120 * @todo Define visibility
121 */
122 public function main() {
123 $this->content = $this->doc->header('Indexing Engine Statistics');
124 $this->content .= $this->doc->spacer(5);
125 switch ($this->MOD_SETTINGS['function']) {
126 case 'stat':
127 $this->content .= $this->doc->section('Records', $this->doc->table($this->getRecordsNumbers()), 0, 1);
128 $this->content .= $this->doc->spacer(15);
129 $this->content .= $this->doc->section('index_phash TYPES', $this->doc->table($this->getPhashTypes()), 1);
130 $this->content .= $this->doc->spacer(15);
131 break;
132 case 'externalDocs':
133 $this->content .= $this->doc->section('External documents', $this->doc->table($this->getPhashExternalDocs()), 0, 1);
134 $this->content .= $this->doc->spacer(15);
135 break;
136 case 'typo3pages':
137 $this->content .= $this->doc->section('TYPO3 Pages', $this->doc->table($this->getPhashT3pages()), 0, 1);
138 $this->content .= $this->doc->spacer(15);
139 break;
140 }
141 $docHeaderButtons = $this->getButtons();
142 $markers = array(
143 'CSH' => $docHeaderButtons['csh'],
144 'FUNC_MENU' => BackendUtility::getFuncMenu(0, 'SET[function]', $this->MOD_SETTINGS['function'], $this->MOD_MENU['function']),
145 'CONTENT' => $this->content
146 );
147 $this->content = $this->doc->startPage('Indexing Engine Statistics');
148 $this->content .= $this->doc->moduleBody($this->pageinfo, $docHeaderButtons, $markers);
149 $this->content .= $this->doc->endPage();
150 $this->content = $this->doc->insertStylesAndJS($this->content);
151 }
152
153 /**
154 * Print content
155 *
156 * @return void
157 * @todo Define visibility
158 */
159 public function printContent() {
160 echo $this->content;
161 }
162
163 /**
164 * Create the panel of buttons for submitting the form or otherwise perform operations.
165 *
166 * @return array all available buttons as an assoc. array
167 */
168 protected function getButtons() {
169 $buttons = array(
170 'csh' => '',
171 'shortcut' => ''
172 );
173 // Shortcut
174 if ($GLOBALS['BE_USER']->mayMakeShortcut()) {
175 $buttons['shortcut'] = $this->doc->makeShortcutIcon('', 'function', $this->MCONF['name']);
176 }
177 return $buttons;
178 }
179
180 /***************************
181 *
182 * OTHER FUNCTIONS:
183 *
184 ***************************/
185 /**
186 * @return [type] ...
187 * @todo Define visibility
188 */
189 public function getRecordsNumbers() {
190 $tables = explode(',', 'index_phash,index_words,index_rel,index_grlist,index_section,index_fulltext');
191 $recList = array();
192 foreach ($tables as $t) {
193 $recList[] = array(
194 $this->tableHead($t),
195 $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', $t)
196 );
197 }
198 return $recList;
199 }
200
201 /**
202 * [Describe function...]
203 *
204 * @param [type] $str: ...
205 * @return [type] ...
206 * @todo Define visibility
207 */
208 public function tableHead($str) {
209 return '<strong>' . $str . ':&nbsp;&nbsp;&nbsp;</strong>';
210 }
211
212 /**
213 * [Describe function...]
214 *
215 * @return [type] ...
216 * @todo Define visibility
217 */
218 public function getPhashStat() {
219 $recList = array();
220 // TYPO3 pages, unique
221 $items = array();
222 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*),phash', 'index_phash', 'data_page_id<>0', 'phash_grouping,pcount,phash');
223 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res)) {
224 $items[] = $row;
225 }
226 $recList[] = array($this->tableHead('TYPO3 pages'), count($items));
227 // TYPO3 pages:
228 $recList[] = array(
229 $this->tableHead('TYPO3 pages, raw'),
230 $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('phash', 'index_phash', 'data_page_id<>0')
231 );
232 // External files, unique
233 $items = array();
234 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*),phash', 'index_phash', 'data_filename<>\'\'', 'phash_grouping');
235 $row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
236 $recList[] = array($this->tableHead('External files'), $row[0]);
237 // External files
238 $recList[] = array(
239 $this->tableHead('External files, raw'),
240 $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('phash', 'index_phash', 'data_filename<>\'\'')
241 );
242 return $recList;
243 }
244
245 /**
246 * [Describe function...]
247 *
248 * @return [type] ...
249 * @todo Define visibility
250 */
251 public function getPhashT3pages() {
252 $recList[] = array(
253 $this->tableHead('id/type'),
254 $this->tableHead('Title'),
255 $this->tableHead('Size'),
256 $this->tableHead('Words'),
257 $this->tableHead('mtime'),
258 $this->tableHead('Indexed'),
259 $this->tableHead('Updated'),
260 $this->tableHead('Parsetime'),
261 $this->tableHead('#sec/gr/full'),
262 $this->tableHead('#sub'),
263 $this->tableHead('Lang'),
264 $this->tableHead('cHash'),
265 $this->tableHead('phash')
266 );
267 // TYPO3 pages, unique
268 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*) AS pcount,index_phash.*', 'index_phash', 'data_page_id<>0', 'phash_grouping,phash,cHashParams,data_filename,data_page_id,data_page_reg1,data_page_type,data_page_mp,gr_list,item_type,item_title,item_description,item_mtime,tstamp,item_size,contentHash,crdate,parsetime,sys_language_uid,item_crdate,externalUrl,recordUid,freeIndexUid,freeIndexSetId', 'data_page_id');
269 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
270 $cHash = count(unserialize($row['cHashParams'])) ? $this->formatCHash(unserialize($row['cHashParams'])) : '';
271 $grListRec = $this->getGrlistRecord($row['phash']);
272 $recList[] = array(
273 $row['data_page_id'] . ($row['data_page_type'] ? '/' . $row['data_page_type'] : ''),
274 htmlentities(\TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs($row['item_title'], 30)),
275 \TYPO3\CMS\Core\Utility\GeneralUtility::formatSize($row['item_size']),
276 $this->getNumberOfWords($row['phash']),
277 BackendUtility::datetime($row['item_mtime']),
278 BackendUtility::datetime($row['crdate']),
279 $row['tstamp'] != $row['crdate'] ? BackendUtility::datetime($row['tstamp']) : '',
280 $row['parsetime'],
281 $this->getNumberOfSections($row['phash']) . '/' . $grListRec[0]['pcount'] . '/' . $this->getNumberOfFulltext($row['phash']),
282 $row['pcount'] . '/' . $this->formatFeGroup($grListRec),
283 $row['sys_language_uid'],
284 $cHash,
285 $row['phash']
286 );
287 if ($row['pcount'] > 1) {
288 $res2 = $GLOBALS['TYPO3_DB']->exec_SELECTquery('index_phash.*', 'index_phash', 'phash_grouping=' . (int)$row['phash_grouping'] . ' AND phash<>' . (int)$row['phash']);
289 while ($row2 = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res2)) {
290 $grListRec = $this->getGrlistRecord($row2['phash']);
291 $recList[] = array(
292 '',
293 '',
294 \TYPO3\CMS\Core\Utility\GeneralUtility::formatSize($row2['item_size']),
295 $this->getNumberOfWords($row2['phash']),
296 BackendUtility::datetime($row2['item_mtime']),
297 BackendUtility::datetime($row2['crdate']),
298 $row2['tstamp'] != $row2['crdate'] ? BackendUtility::datetime($row2['tstamp']) : '',
299 $row2['parsetime'],
300 $this->getNumberOfSections($row2['phash']) . '/' . $grListRec[0]['pcount'] . '/' . $this->getNumberOfFulltext($row2['phash']),
301 '-/' . $this->formatFeGroup($grListRec),
302 '',
303 '',
304 $row2['phash']
305 );
306 }
307 }
308 }
309 return $recList;
310 }
311
312 /**
313 * [Describe function...]
314 *
315 * @return [type] ...
316 * @todo Define visibility
317 */
318 public function getPhashExternalDocs() {
319 $recList[] = array(
320 $this->tableHead('Filename'),
321 $this->tableHead('Size'),
322 $this->tableHead('Words'),
323 $this->tableHead('mtime'),
324 $this->tableHead('Indexed'),
325 $this->tableHead('Updated'),
326 $this->tableHead('Parsetime'),
327 $this->tableHead('#sec/gr/full'),
328 $this->tableHead('#sub'),
329 $this->tableHead('cHash'),
330 $this->tableHead('phash'),
331 $this->tableHead('Path')
332 );
333 // TYPO3 pages, unique
334 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*) AS pcount,index_phash.*', 'index_phash', 'item_type<>\'0\'', 'phash_grouping,phash,cHashParams,data_filename,data_page_id,data_page_reg1,data_page_type,data_page_mp,gr_list,item_type,item_title,item_description,item_mtime,tstamp,item_size,contentHash,crdate,parsetime,sys_language_uid,item_crdate,externalUrl,recordUid,freeIndexUid,freeIndexSetId', 'item_type');
335 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
336 $cHash = count(unserialize($row['cHashParams'])) ? $this->formatCHash(unserialize($row['cHashParams'])) : '';
337 $grListRec = $this->getGrlistRecord($row['phash']);
338 $recList[] = array(
339 htmlentities(\TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs($row['item_title'], 30)),
340 \TYPO3\CMS\Core\Utility\GeneralUtility::formatSize($row['item_size']),
341 $this->getNumberOfWords($row['phash']),
342 BackendUtility::datetime($row['item_mtime']),
343 BackendUtility::datetime($row['crdate']),
344 $row['tstamp'] != $row['crdate'] ? BackendUtility::datetime($row['tstamp']) : '',
345 $row['parsetime'],
346 $this->getNumberOfSections($row['phash']) . '/' . $grListRec[0]['pcount'] . '/' . $this->getNumberOfFulltext($row['phash']),
347 $row['pcount'],
348 $cHash,
349 $row['phash'],
350 htmlentities(\TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs($row['data_filename'], 100))
351 );
352 if ($row['pcount'] > 1) {
353 $res2 = $GLOBALS['TYPO3_DB']->exec_SELECTquery('index_phash.*', 'index_phash', 'phash_grouping=' . (int)$row['phash_grouping'] . ' AND phash<>' . (int)$row['phash']);
354 while ($row2 = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res2)) {
355 $cHash = count(unserialize($row2['cHashParams'])) ? $this->formatCHash(unserialize($row2['cHashParams'])) : '';
356 $grListRec = $this->getGrlistRecord($row2['phash']);
357 $recList[] = array(
358 '',
359 '',
360 $this->getNumberOfWords($row2['phash']),
361 '',
362 BackendUtility::datetime($row2['crdate']),
363 $row2['tstamp'] != $row2['crdate'] ? BackendUtility::datetime($row2['tstamp']) : '',
364 $row2['parsetime'],
365 $this->getNumberOfSections($row2['phash']) . '/' . $grListRec[0]['pcount'] . '/' . $this->getNumberOfFulltext($row2['phash']),
366 '',
367 $cHash,
368 $row2['phash'],
369 ''
370 );
371 }
372 }
373 }
374 return $recList;
375 }
376
377 /**
378 * [Describe function...]
379 *
380 * @param [type] $fegroup_recs: ...
381 * @return [type] ...
382 * @todo Define visibility
383 */
384 public function formatFeGroup($fegroup_recs) {
385 $str = array();
386 foreach ($fegroup_recs as $row) {
387 $str[] = $row['gr_list'] == '0,-1' ? 'NL' : $row['gr_list'];
388 }
389 arsort($str);
390 return implode('|', $str);
391 }
392
393 /**
394 * [Describe function...]
395 *
396 * @param [type] $arr: ...
397 * @return [type] ...
398 * @todo Define visibility
399 */
400 public function formatCHash($arr) {
401 $list = array();
402 foreach ($arr as $k => $v) {
403 $list[] = htmlspecialchars($k) . '=' . htmlspecialchars($v);
404 }
405 return implode('<br />', $list);
406 }
407
408 /**
409 * [Describe function...]
410 *
411 * @param [type] $phash: ...
412 * @return [type] ...
413 * @todo Define visibility
414 */
415 public function getNumberOfSections($phash) {
416 return $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('phash', 'index_section', 'phash=' . (int)$phash);
417 }
418
419 /**
420 * [Describe function...]
421 *
422 * @param [type] $phash: ...
423 * @return [type] ...
424 * @todo Define visibility
425 */
426 public function getNumberOfWords($phash) {
427 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*)', 'index_rel', 'phash=' . (int)$phash);
428 $row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
429 return $row[0];
430 }
431
432 /**
433 * [Describe function...]
434 *
435 * @param [type] $phash: ...
436 * @return [type] ...
437 * @todo Define visibility
438 */
439 public function getGrlistRecord($phash) {
440 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('index_grlist.*', 'index_grlist', 'phash=' . (int)$phash);
441 $allRows = array();
442 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
443 $row['pcount'] = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
444 $allRows[] = $row;
445 }
446 return $allRows;
447 }
448
449 /**
450 * [Describe function...]
451 *
452 * @param [type] $phash: ...
453 * @return [type] ...
454 * @todo Define visibility
455 */
456 public function getNumberOfFulltext($phash) {
457 return $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('phash', 'index_fulltext', 'phash=' . (int)$phash);
458 }
459
460 /**
461 * [Describe function...]
462 *
463 * @return [type] ...
464 * @todo Define visibility
465 */
466 public function getPhashTypes() {
467 $recList = array();
468 // Types:
469 $Itypes = array(
470 'html' => 1,
471 'htm' => 1,
472 'pdf' => 2,
473 'doc' => 3,
474 'txt' => 4
475 );
476 $revTypes = array_flip($Itypes);
477 $revTypes[0] = 'TYPO3 page';
478 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*),item_type', 'index_phash', '', 'item_type', 'item_type');
479 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res)) {
480 $iT = $row[1];
481 $recList[] = array($this->tableHead($revTypes[$iT] . ' (' . $iT . ')'), $this->countUniqueTypes($iT) . '/' . $row[0]);
482 }
483 return $recList;
484 }
485
486 /**
487 * [Describe function...]
488 *
489 * @param [type] $item_type: ...
490 * @return [type] ...
491 * @todo Define visibility
492 */
493 public function countUniqueTypes($item_type) {
494 // TYPO3 pages, unique
495 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*)', 'index_phash', 'item_type=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($item_type, 'index_phash'), 'phash_grouping');
496 $items = array();
497 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res)) {
498 $items[] = $row;
499 }
500 return count($items);
501 }
502
503 }