[TASK] Cleanup EXT:indexed_search
[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 * Constructor
74 */
75 public function __construct() {
76 $GLOBALS['BE_USER']->modAccess($GLOBALS['MCONF'], TRUE);
77 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('indexed_search', TRUE);
78 }
79
80 /**
81 * Initialization
82 *
83 * @return void
84 * @todo Define visibility
85 */
86 public function init() {
87 $this->MCONF = $GLOBALS['MCONF'];
88 $this->menuConfig();
89 $this->doc = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Template\\DocumentTemplate');
90 $this->doc->form = '<form action="" method="post">';
91 $this->doc->backPath = $GLOBALS['BACK_PATH'];
92 $this->doc->setModuleTemplate(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extRelPath('indexed_search') . '/mod/mod_template.html');
93 $this->doc->tableLayout = array(
94 'defRow' => array(
95 '0' => array('<td valign="top" nowrap>', '</td>'),
96 'defCol' => array('<td><img src="' . $this->doc->backPath . 'clear.gif" width=10 height=1></td><td valign="top" nowrap>', '</td>')
97 )
98 );
99 $indexer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\IndexedSearch\\Indexer');
100 $indexer->initializeExternalParsers();
101 }
102
103 /**
104 * MENU-ITEMS:
105 * If array, then it's a selector box menu
106 * If empty string it's just a variable, that'll be saved.
107 * Values NOT in this array will not be saved in the settings-array for the module.
108 *
109 * @return void
110 * @todo Define visibility
111 */
112 public function menuConfig() {
113 $this->MOD_MENU = array(
114 'function' => array(
115 'stat' => 'General statistics',
116 'typo3pages' => 'List: TYPO3 Pages',
117 'externalDocs' => 'List: External documents'
118 )
119 );
120 // cleanse settings
121 $this->MOD_SETTINGS = BackendUtility::getModuleData($this->MOD_MENU, \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('SET'), $this->MCONF['name'], 'ses');
122 }
123
124 /**
125 * Main function to generate the content
126 *
127 * @return void
128 * @todo Define visibility
129 */
130 public function main() {
131 $this->content = $this->doc->header('Indexing Engine Statistics');
132 $this->content .= $this->doc->spacer(5);
133 switch ($this->MOD_SETTINGS['function']) {
134 case 'stat':
135 $this->content .= $this->doc->section('Records', $this->doc->table($this->getRecordsNumbers()), 0, 1);
136 $this->content .= $this->doc->spacer(15);
137 $this->content .= $this->doc->section('index_phash TYPES', $this->doc->table($this->getPhashTypes()), 1);
138 $this->content .= $this->doc->spacer(15);
139 break;
140 case 'externalDocs':
141 $this->content .= $this->doc->section('External documents', $this->doc->table($this->getPhashExternalDocs()), 0, 1);
142 $this->content .= $this->doc->spacer(15);
143 break;
144 case 'typo3pages':
145 $this->content .= $this->doc->section('TYPO3 Pages', $this->doc->table($this->getPhashT3pages()), 0, 1);
146 $this->content .= $this->doc->spacer(15);
147 break;
148 }
149 $docHeaderButtons = $this->getButtons();
150 $markers = array(
151 'CSH' => $docHeaderButtons['csh'],
152 'FUNC_MENU' => BackendUtility::getFuncMenu(0, 'SET[function]', $this->MOD_SETTINGS['function'], $this->MOD_MENU['function']),
153 'CONTENT' => $this->content
154 );
155 $this->content = $this->doc->startPage('Indexing Engine Statistics');
156 $this->content .= $this->doc->moduleBody($this->pageinfo, $docHeaderButtons, $markers);
157 $this->content .= $this->doc->endPage();
158 $this->content = $this->doc->insertStylesAndJS($this->content);
159 }
160
161 /**
162 * Print content
163 *
164 * @return void
165 * @todo Define visibility
166 */
167 public function printContent() {
168 echo $this->content;
169 }
170
171 /**
172 * Create the panel of buttons for submitting the form or otherwise perform operations.
173 *
174 * @return array all available buttons as an assoc. array
175 */
176 protected function getButtons() {
177 $buttons = array(
178 'csh' => '',
179 'shortcut' => ''
180 );
181 // Shortcut
182 if ($GLOBALS['BE_USER']->mayMakeShortcut()) {
183 $buttons['shortcut'] = $this->doc->makeShortcutIcon('', 'function', $this->MCONF['name']);
184 }
185 return $buttons;
186 }
187
188 /***************************
189 *
190 * OTHER FUNCTIONS:
191 *
192 ***************************/
193 /**
194 * @return [type] ...
195 * @todo Define visibility
196 */
197 public function getRecordsNumbers() {
198 $tables = explode(',', 'index_phash,index_words,index_rel,index_grlist,index_section,index_fulltext');
199 $recList = array();
200 foreach ($tables as $t) {
201 $recList[] = array(
202 $this->tableHead($t),
203 $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', $t)
204 );
205 }
206 return $recList;
207 }
208
209 /**
210 * [Describe function...]
211 *
212 * @param [type] $str: ...
213 * @return [type] ...
214 * @todo Define visibility
215 */
216 public function tableHead($str) {
217 return '<strong>' . $str . ':&nbsp;&nbsp;&nbsp;</strong>';
218 }
219
220 /**
221 * [Describe function...]
222 *
223 * @return [type] ...
224 * @todo Define visibility
225 */
226 public function getPhashStat() {
227 $recList = array();
228 // TYPO3 pages, unique
229 $items = array();
230 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*),phash', 'index_phash', 'data_page_id<>0', 'phash_grouping,pcount,phash');
231 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res)) {
232 $items[] = $row;
233 }
234 $recList[] = array($this->tableHead('TYPO3 pages'), count($items));
235 // TYPO3 pages:
236 $recList[] = array(
237 $this->tableHead('TYPO3 pages, raw'),
238 $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('phash', 'index_phash', 'data_page_id<>0')
239 );
240 // External files, unique
241 $items = array();
242 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*),phash', 'index_phash', 'data_filename<>\'\'', 'phash_grouping');
243 $row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
244 $recList[] = array($this->tableHead('External files'), $row[0]);
245 // External files
246 $recList[] = array(
247 $this->tableHead('External files, raw'),
248 $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('phash', 'index_phash', 'data_filename<>\'\'')
249 );
250 return $recList;
251 }
252
253 /**
254 * [Describe function...]
255 *
256 * @return [type] ...
257 * @todo Define visibility
258 */
259 public function getPhashT3pages() {
260 $recList[] = array(
261 $this->tableHead('id/type'),
262 $this->tableHead('Title'),
263 $this->tableHead('Size'),
264 $this->tableHead('Words'),
265 $this->tableHead('mtime'),
266 $this->tableHead('Indexed'),
267 $this->tableHead('Updated'),
268 $this->tableHead('Parsetime'),
269 $this->tableHead('#sec/gr/full'),
270 $this->tableHead('#sub'),
271 $this->tableHead('Lang'),
272 $this->tableHead('cHash'),
273 $this->tableHead('phash')
274 );
275 // TYPO3 pages, unique
276 $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');
277 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
278 $cHash = count(unserialize($row['cHashParams'])) ? $this->formatCHash(unserialize($row['cHashParams'])) : '';
279 $grListRec = $this->getGrlistRecord($row['phash']);
280 $recList[] = array(
281 $row['data_page_id'] . ($row['data_page_type'] ? '/' . $row['data_page_type'] : ''),
282 htmlentities(\TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs($row['item_title'], 30)),
283 \TYPO3\CMS\Core\Utility\GeneralUtility::formatSize($row['item_size']),
284 $this->getNumberOfWords($row['phash']),
285 BackendUtility::datetime($row['item_mtime']),
286 BackendUtility::datetime($row['crdate']),
287 $row['tstamp'] != $row['crdate'] ? BackendUtility::datetime($row['tstamp']) : '',
288 $row['parsetime'],
289 $this->getNumberOfSections($row['phash']) . '/' . $grListRec[0]['pcount'] . '/' . $this->getNumberOfFulltext($row['phash']),
290 $row['pcount'] . '/' . $this->formatFeGroup($grListRec),
291 $row['sys_language_uid'],
292 $cHash,
293 $row['phash']
294 );
295 if ($row['pcount'] > 1) {
296 $res2 = $GLOBALS['TYPO3_DB']->exec_SELECTquery('index_phash.*', 'index_phash', 'phash_grouping=' . (int)$row['phash_grouping'] . ' AND phash<>' . (int)$row['phash']);
297 while ($row2 = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res2)) {
298 $grListRec = $this->getGrlistRecord($row2['phash']);
299 $recList[] = array(
300 '',
301 '',
302 \TYPO3\CMS\Core\Utility\GeneralUtility::formatSize($row2['item_size']),
303 $this->getNumberOfWords($row2['phash']),
304 BackendUtility::datetime($row2['item_mtime']),
305 BackendUtility::datetime($row2['crdate']),
306 $row2['tstamp'] != $row2['crdate'] ? BackendUtility::datetime($row2['tstamp']) : '',
307 $row2['parsetime'],
308 $this->getNumberOfSections($row2['phash']) . '/' . $grListRec[0]['pcount'] . '/' . $this->getNumberOfFulltext($row2['phash']),
309 '-/' . $this->formatFeGroup($grListRec),
310 '',
311 '',
312 $row2['phash']
313 );
314 }
315 }
316 }
317 return $recList;
318 }
319
320 /**
321 * [Describe function...]
322 *
323 * @return [type] ...
324 * @todo Define visibility
325 */
326 public function getPhashExternalDocs() {
327 $recList[] = array(
328 $this->tableHead('Filename'),
329 $this->tableHead('Size'),
330 $this->tableHead('Words'),
331 $this->tableHead('mtime'),
332 $this->tableHead('Indexed'),
333 $this->tableHead('Updated'),
334 $this->tableHead('Parsetime'),
335 $this->tableHead('#sec/gr/full'),
336 $this->tableHead('#sub'),
337 $this->tableHead('cHash'),
338 $this->tableHead('phash'),
339 $this->tableHead('Path')
340 );
341 // TYPO3 pages, unique
342 $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');
343 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
344 $cHash = count(unserialize($row['cHashParams'])) ? $this->formatCHash(unserialize($row['cHashParams'])) : '';
345 $grListRec = $this->getGrlistRecord($row['phash']);
346 $recList[] = array(
347 htmlentities(\TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs($row['item_title'], 30)),
348 \TYPO3\CMS\Core\Utility\GeneralUtility::formatSize($row['item_size']),
349 $this->getNumberOfWords($row['phash']),
350 BackendUtility::datetime($row['item_mtime']),
351 BackendUtility::datetime($row['crdate']),
352 $row['tstamp'] != $row['crdate'] ? BackendUtility::datetime($row['tstamp']) : '',
353 $row['parsetime'],
354 $this->getNumberOfSections($row['phash']) . '/' . $grListRec[0]['pcount'] . '/' . $this->getNumberOfFulltext($row['phash']),
355 $row['pcount'],
356 $cHash,
357 $row['phash'],
358 htmlentities(\TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs($row['data_filename'], 100))
359 );
360 if ($row['pcount'] > 1) {
361 $res2 = $GLOBALS['TYPO3_DB']->exec_SELECTquery('index_phash.*', 'index_phash', 'phash_grouping=' . (int)$row['phash_grouping'] . ' AND phash<>' . (int)$row['phash']);
362 while ($row2 = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res2)) {
363 $cHash = count(unserialize($row2['cHashParams'])) ? $this->formatCHash(unserialize($row2['cHashParams'])) : '';
364 $grListRec = $this->getGrlistRecord($row2['phash']);
365 $recList[] = array(
366 '',
367 '',
368 $this->getNumberOfWords($row2['phash']),
369 '',
370 BackendUtility::datetime($row2['crdate']),
371 $row2['tstamp'] != $row2['crdate'] ? BackendUtility::datetime($row2['tstamp']) : '',
372 $row2['parsetime'],
373 $this->getNumberOfSections($row2['phash']) . '/' . $grListRec[0]['pcount'] . '/' . $this->getNumberOfFulltext($row2['phash']),
374 '',
375 $cHash,
376 $row2['phash'],
377 ''
378 );
379 }
380 }
381 }
382 return $recList;
383 }
384
385 /**
386 * [Describe function...]
387 *
388 * @param [type] $fegroup_recs: ...
389 * @return [type] ...
390 * @todo Define visibility
391 */
392 public function formatFeGroup($fegroup_recs) {
393 $str = array();
394 foreach ($fegroup_recs as $row) {
395 $str[] = $row['gr_list'] == '0,-1' ? 'NL' : $row['gr_list'];
396 }
397 arsort($str);
398 return implode('|', $str);
399 }
400
401 /**
402 * [Describe function...]
403 *
404 * @param [type] $arr: ...
405 * @return [type] ...
406 * @todo Define visibility
407 */
408 public function formatCHash($arr) {
409 $list = array();
410 foreach ($arr as $k => $v) {
411 $list[] = htmlspecialchars($k) . '=' . htmlspecialchars($v);
412 }
413 return implode('<br />', $list);
414 }
415
416 /**
417 * [Describe function...]
418 *
419 * @param [type] $phash: ...
420 * @return [type] ...
421 * @todo Define visibility
422 */
423 public function getNumberOfSections($phash) {
424 return $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('phash', 'index_section', 'phash=' . (int)$phash);
425 }
426
427 /**
428 * [Describe function...]
429 *
430 * @param [type] $phash: ...
431 * @return [type] ...
432 * @todo Define visibility
433 */
434 public function getNumberOfWords($phash) {
435 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*)', 'index_rel', 'phash=' . (int)$phash);
436 $row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
437 return $row[0];
438 }
439
440 /**
441 * [Describe function...]
442 *
443 * @param [type] $phash: ...
444 * @return [type] ...
445 * @todo Define visibility
446 */
447 public function getGrlistRecord($phash) {
448 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('index_grlist.*', 'index_grlist', 'phash=' . (int)$phash);
449 $allRows = array();
450 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
451 $row['pcount'] = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
452 $allRows[] = $row;
453 }
454 return $allRows;
455 }
456
457 /**
458 * [Describe function...]
459 *
460 * @param [type] $phash: ...
461 * @return [type] ...
462 * @todo Define visibility
463 */
464 public function getNumberOfFulltext($phash) {
465 return $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('phash', 'index_fulltext', 'phash=' . (int)$phash);
466 }
467
468 /**
469 * [Describe function...]
470 *
471 * @return [type] ...
472 * @todo Define visibility
473 */
474 public function getPhashTypes() {
475 $recList = array();
476 // Types:
477 $Itypes = array(
478 'html' => 1,
479 'htm' => 1,
480 'pdf' => 2,
481 'doc' => 3,
482 'txt' => 4
483 );
484 $revTypes = array_flip($Itypes);
485 $revTypes[0] = 'TYPO3 page';
486 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*),item_type', 'index_phash', '', 'item_type', 'item_type');
487 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res)) {
488 $iT = $row[1];
489 $recList[] = array($this->tableHead($revTypes[$iT] . ' (' . $iT . ')'), $this->countUniqueTypes($iT) . '/' . $row[0]);
490 }
491 return $recList;
492 }
493
494 /**
495 * [Describe function...]
496 *
497 * @param [type] $item_type: ...
498 * @return [type] ...
499 * @todo Define visibility
500 */
501 public function countUniqueTypes($item_type) {
502 // TYPO3 pages, unique
503 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*)', 'index_phash', 'item_type=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($item_type, 'index_phash'), 'phash_grouping');
504 $items = array();
505 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res)) {
506 $items[] = $row;
507 }
508 return count($items);
509 }
510
511 }