2 namespace TYPO3\CMS\IndexedSearch\Controller
;
5 * This file is part of the TYPO3 CMS project.
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
17 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication
;
18 use TYPO3\CMS\Core\Database\DatabaseConnection
;
19 use TYPO3\CMS\Extbase\Mvc\Controller\ActionController
;
20 use TYPO3\CMS\Extbase\Mvc\Web\Request
as WebRequest
;
21 use TYPO3\CMS\IndexedSearch\Domain\Repository\AdministrationRepository
;
22 use TYPO3\CMS\Backend\Utility\BackendUtility
;
23 use TYPO3\CMS\Core\Utility\GeneralUtility
;
24 use TYPO3\CMS\IndexedSearch\Indexer
;
25 use TYPO3\CMS\Lang\LanguageService
;
28 * Administration controller
30 class AdministrationController
extends ActionController
{
33 * @var AdministrationRepository
35 protected $administrationRepository;
38 * @var int Current page id
40 protected $pageUid = 0;
43 * @var int Max lists per page
45 protected $maxListPerPage = 50;
48 * @var array External parsers
50 protected $external_parsers = array();
53 * @var array Configuration defined in the Extension Manager
55 protected $indexerConfig = array();
58 * @var bool is metaphone enabled
60 protected $enableMetaphoneSearch = FALSE;
65 * @var \TYPO3\CMS\IndexedSearch\Indexer
70 * Function will be called before every other action
74 public function initializeAction() {
75 $this->pageUid
= (int)GeneralUtility
::_GET('id');
76 $this->maxListPerPage
= GeneralUtility
::_GP('listALL') ?
100000 : 100;
77 $this->indexerConfig
= unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['indexed_search']);
78 $this->enableMetaphoneSearch
= (bool)$this->indexerConfig
['enableMetaphoneSearch'];
79 $this->indexer
= GeneralUtility
::makeInstance(Indexer
::class);
81 parent
::initializeAction();
85 * Override the action name if found in the uc of the user
87 * @param \TYPO3\CMS\Extbase\Mvc\RequestInterface $request
88 * @param \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response
89 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
91 public function processRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface
$request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface
$response) {
92 $vars = GeneralUtility
::_GET('tx_indexedsearch_web_indexedsearchisearch');
94 $beUser = $this->getBackendUserAuthentication();
95 if (is_array($vars) && isset($vars['action']) && method_exists($this, $vars['action'] . 'Action')) {
96 $action = $vars['action'];
99 case 'saveStopwordsKeywords':
100 $action = 'statisticDetails';
102 case 'deleteIndexedItem':
103 $action = 'statistic';
107 $beUser->uc
['indexed_search']['action'] = $action;
108 $beUser->uc
['indexed_search']['arguments'] = $request->getArguments();
110 } elseif (isset($beUser->uc
['indexed_search']['action'])) {
111 if ($request instanceof WebRequest
) {
112 $request->setControllerActionName($beUser->uc
['indexed_search']['action']);
114 if (isset($beUser->uc
['indexed_search']['arguments'])) {
115 $request->setArguments($beUser->uc
['indexed_search']['arguments']);
119 parent
::processRequest($request, $response);
123 * @param \TYPO3\CMS\IndexedSearch\Domain\Repository\AdministrationRepository $administrationRepository
126 public function injectAdministrationRepository(AdministrationRepository
$administrationRepository) {
127 $this->administrationRepository
= $administrationRepository;
131 * Index action contains the most important statistics
135 public function indexAction() {
136 $this->view
->assignMultiple(array(
137 'records' => $this->administrationRepository
->getRecordsNumbers(),
138 'phash' => $this->administrationRepository
->getPageHashTypes()
141 if ($this->pageUid
) {
142 $last24hours = ' AND tstamp > ' . ($GLOBALS['EXEC_TIME'] - 24 * 60 * 60);
143 $last30days = ' AND tstamp > ' . ($GLOBALS['EXEC_TIME'] - 30 * 24 * 60 * 60);
145 $this->view
->assignMultiple(array(
146 'pageUid' => $this->pageUid
,
147 'all' => $this->administrationRepository
->getGeneralSearchStatistic('', $this->pageUid
),
148 'last24hours' => $this->administrationRepository
->getGeneralSearchStatistic($last24hours, $this->pageUid
),
149 'last30days' => $this->administrationRepository
->getGeneralSearchStatistic($last30days, $this->pageUid
),
155 * Statistics for pages
159 public function pagesAction() {
160 $this->view
->assign('records', $this->administrationRepository
->getPageStatistic());
164 * Statistics for external documents
168 public function externalDocumentsAction() {
169 $this->view
->assign('records', $this->administrationRepository
->getExternalDocumentsStatistic());
173 * Statistics for a given page hash
175 * @param int $pageHash
178 public function statisticDetailsAction($pageHash = 0) {
179 $pageHash = (int)$pageHash;
180 $db = $this->getDatabaseConnection();
181 $pageHashRow = $db->exec_SELECTgetSingleRow('*', 'index_phash', 'phash = ' . (int)$pageHash);
183 if (!is_array($pageHashRow)) {
184 $this->redirect('statistic');
187 $debugRow = $db->exec_SELECTgetRows('*', 'index_debug', 'phash = ' . (int)$pageHash);
188 $debugInfo = array();
190 if (is_array($debugRow)) {
191 $debugInfo = unserialize($debugRow[0]['debuginfo']);
192 $lexer = $debugInfo['lexer'];
193 unset($debugInfo['lexer']);
195 $pageRecord = BackendUtility
::getRecord('pages', $pageHashRow['data_page_id']);
196 $keywords = is_array($pageRecord) ?
array_flip(GeneralUtility
::trimExplode(',', $pageRecord['keywords'], TRUE)) : array();
197 $wordRecords = $db->exec_SELECTgetRows(
198 'index_words.*, index_rel.*',
199 'index_rel, index_words',
200 'index_rel.phash = ' . (int)$pageHash . ' AND index_words.wid = index_rel.wid',
202 'index_words.baseword'
204 foreach($wordRecords as $id => $row) {
205 if (isset($keywords[$row['baseword']])) {
206 $wordRecords[$id]['is_keyword'] = TRUE;
209 $metaphoneRows = $metaphone = array();
210 if ($this->enableMetaphoneSearch
&& is_array($wordRecords)) {
211 // Group metaphone hash
212 foreach ($wordRecords as $row) {
213 $metaphoneRows[$row['metaphone']][] = $row['baseword'];
216 foreach ($metaphoneRows as $hash => $words) {
217 if (count($words) > 1) {
218 $metaphone[] = array(
219 'metaphone' => $this->indexer
->metaphone($words[0], 1), $hash,
226 $this->view
->assignMultiple(array(
227 'phash' => $pageHash,
228 'phashRow' => $pageHashRow,
229 'words' => $wordRecords,
230 'sections' => $db->exec_SELECTgetRows(
233 'index_section.phash = ' . (int)$pageHash
235 'topCount' => $db->exec_SELECTgetRows(
236 'index_words.baseword, index_words.metaphone, index_rel.*',
237 'index_rel, index_words',
238 'index_rel.phash = ' . (int)$pageHash . ' AND index_words.wid = index_rel.wid
239 AND index_words.is_stopword=0',
241 'index_rel.count DESC',
244 'topFrequency' => $db->exec_SELECTgetRows(
245 'index_words.baseword, index_words.metaphone, index_rel.*',
246 'index_rel, index_words',
247 'index_rel.phash = ' . (int)$pageHash . ' AND index_words.wid = index_rel.wid
248 AND index_words.is_stopword=0',
250 'index_rel.freq DESC',
253 'debug' => $debugInfo,
255 'metaphone' => $metaphone,
256 'page' => $pageRecord,
257 'keywords' => $keywords
262 * Save stop words and keywords
264 * @param string $pageHash
266 * @param array $stopwords
267 * @param array $keywords
270 public function saveStopwordsKeywordsAction($pageHash, $pageId, $stopwords = array(), $keywords = array()) {
271 if ($this->getBackendUserAuthentication()->isAdmin()) {
272 if (is_array($stopwords) && !empty($stopwords)) {
273 $this->administrationRepository
->saveStopWords($stopwords);
275 if (is_array($keywords) && !empty($keywords)) {
276 $this->administrationRepository
->saveKeywords($keywords, $pageId);
280 $this->redirect('statisticDetails', NULL, NULL, array('pageHash' => $pageHash));
284 * Statistics for a given word id
287 * @param int $pageHash
290 public function wordDetailAction($id = 0, $pageHash = 0) {
291 $rows = $this->getDatabaseConnection()->exec_SELECTgetRows(
292 'index_phash.*, index_section.*, index_rel.*',
293 'index_rel, index_section, index_phash',
294 'index_rel.wid = ' . (int)$id . ' AND index_rel.phash = index_section.phash' . ' AND index_section.phash = index_phash.phash',
296 'index_rel.freq DESC'
299 $this->view
->assignMultiple(array(
309 * @param string $mode
312 public function statisticAction($depth = 1, $mode = 'overview') {
313 if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['indexed_search']['external_parsers'])) {
314 foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['indexed_search']['external_parsers'] as $extension => $_objRef) {
315 /** @var \TYPO3\CMS\IndexedSearch\FileContentParser $fileContentParser */
316 $fileContentParser = GeneralUtility
::getUserObj($_objRef);
317 if ($fileContentParser->softInit($extension)) {
318 $this->external_parsers
[$extension] = $fileContentParser;
322 $this->administrationRepository
->external_parsers
= $this->external_parsers
;
324 $allLines = $this->administrationRepository
->getTree($this->pageUid
, $depth, $mode);
326 $this->view
->assignMultiple(array(
327 'levelTranslations' => explode('|', $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.enterSearchLevels')),
329 'pageUid' => $this->pageUid
,
337 * Remove item from index
341 * @param string $mode
344 public function deleteIndexedItemAction($id, $depth = 1, $mode = 'overview') {
345 $this->administrationRepository
->removeIndexedPhashRow($id, $this->pageUid
, $depth);
346 $this->redirect('statistic', NULL, NULL, array('depth' => $depth, 'mode' => $mode));
350 * @return DatabaseConnection
352 protected function getDatabaseConnection() {
353 return $GLOBALS['TYPO3_DB'];
357 * @return BackendUserAuthentication
359 protected function getBackendUserAuthentication() {
360 return $GLOBALS['BE_USER'];
364 * @return LanguageService
366 protected function getLanguageService() {
367 return $GLOBALS['LANG'];