TYPO3 CMS  TYPO3_8-7
AdministrationController.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
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.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
29 
34 {
39 
43  protected $pageUid = 0;
44 
48  protected $external_parsers = [];
49 
53  protected $indexerConfig = [];
54 
58  protected $enableMetaphoneSearch = false;
59 
65  protected $indexer;
66 
72  protected $defaultViewObjectName = BackendTemplateView::class;
73 
79  protected $view;
80 
86  protected function initializeView(ViewInterface $view)
87  {
88  if ($view instanceof BackendTemplateView) {
90  parent::initializeView($view);
91  $permissionClause = $this->getBackendUserAuthentication()->getPagePermsClause(1);
92  $pageRecord = BackendUtility::readPageAccess($this->pageUid, $permissionClause);
93  if ($pageRecord) {
94  $view->getModuleTemplate()->getDocHeaderComponent()->setMetaInformation($pageRecord);
95  }
96  $this->generateMenu();
97  $this->view->getModuleTemplate()->setFlashMessageQueue($this->controllerContext->getFlashMessageQueue());
98  $view->assign('extensionConfiguration', $this->indexerConfig);
99  }
100  }
101 
105  protected function generateMenu()
106  {
107  $menuItems = [
108  'index' => [
109  'controller' => 'Administration',
110  'action' => 'index',
111  'label' => $this->getLanguageService()->sL('LLL:EXT:indexed_search/Resources/Private/Language/locallang.xlf:administration.menu.general')
112  ],
113  'pages' => [
114  'controller' => 'Administration',
115  'action' => 'pages',
116  'label' => $this->getLanguageService()->sL('LLL:EXT:indexed_search/Resources/Private/Language/locallang.xlf:administration.menu.pages')
117  ],
118  'externalDocuments' => [
119  'controller' => 'Administration',
120  'action' => 'externalDocuments',
121  'label' => $this->getLanguageService()->sL('LLL:EXT:indexed_search/Resources/Private/Language/locallang.xlf:administration.menu.externalDocuments')
122  ],
123  'statistic' => [
124  'controller' => 'Administration',
125  'action' => 'statistic',
126  'label' => $this->getLanguageService()->sL('LLL:EXT:indexed_search/Resources/Private/Language/locallang.xlf:administration.menu.statistic')
127  ]
128  ];
129  $uriBuilder = $this->objectManager->get(UriBuilder::class);
130  $uriBuilder->setRequest($this->request);
131 
132  $menu = $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
133  $menu->setIdentifier('IndexedSearchModuleMenu');
134 
135  foreach ($menuItems as $menuItemConfig) {
136  $isActive = $this->request->getControllerActionName() === $menuItemConfig['action'];
137  $menuItem = $menu->makeMenuItem()
138  ->setTitle($menuItemConfig['label'])
139  ->setHref($this->getHref($menuItemConfig['controller'], $menuItemConfig['action']))
140  ->setActive($isActive);
141  $menu->addMenuItem($menuItem);
142  }
143 
144  $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
145  }
146 
150  public function initializeAction()
151  {
152  $this->pageUid = (int)GeneralUtility::_GET('id');
153  $this->indexerConfig = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['indexed_search'], ['allowed_classes' => false]);
154  $this->enableMetaphoneSearch = (bool)$this->indexerConfig['enableMetaphoneSearch'];
155  $this->indexer = GeneralUtility::makeInstance(Indexer::class);
156 
157  parent::initializeAction();
158  }
159 
167  public function processRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response)
168  {
169  $vars = GeneralUtility::_GET('tx_indexedsearch_web_indexedsearchisearch');
170 
171  $beUser = $this->getBackendUserAuthentication();
172  if (is_array($vars) && isset($vars['action']) && method_exists($this, $vars['action'] . 'Action')) {
173  $action = $vars['action'];
174 
175  switch ($action) {
176  case 'saveStopwordsKeywords':
177  $action = 'statisticDetails';
178  break;
179  case 'deleteIndexedItem':
180  $action = 'statistic';
181  break;
182  }
183 
184  $beUser->uc['indexed_search']['action'] = $action;
185  $beUser->uc['indexed_search']['arguments'] = $request->getArguments();
186  $beUser->writeUC();
187  } elseif (isset($beUser->uc['indexed_search']['action'])) {
188  if ($request instanceof WebRequest) {
189  $request->setControllerActionName($beUser->uc['indexed_search']['action']);
190  }
191  if (isset($beUser->uc['indexed_search']['arguments'])) {
192  $request->setArguments($beUser->uc['indexed_search']['arguments']);
193  }
194  }
195 
196  parent::processRequest($request, $response);
197  }
198 
203  {
204  $this->administrationRepository = $administrationRepository;
205  }
206 
210  public function indexAction()
211  {
212  $this->view->assignMultiple([
213  'records' => $this->administrationRepository->getRecordsNumbers(),
214  'phash' => $this->administrationRepository->getPageHashTypes()
215  ]);
216 
217  if ($this->pageUid) {
218  $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
219  ->getQueryBuilderForTable('index_stat_word')
220  ->expr();
221 
222  $last24hours = $expressionBuilder->gt('tstamp', ($GLOBALS['EXEC_TIME'] - 86400));
223  $last30days = $expressionBuilder->gt('tstamp', ($GLOBALS['EXEC_TIME'] - 30 * 86400));
224 
225  $this->view->assignMultiple([
226  'pageUid' => $this->pageUid,
227  'all' => $this->administrationRepository->getGeneralSearchStatistic('', $this->pageUid),
228  'last24hours' => $this->administrationRepository->getGeneralSearchStatistic($last24hours, $this->pageUid),
229  'last30days' => $this->administrationRepository->getGeneralSearchStatistic($last30days, $this->pageUid),
230  ]);
231  }
232  }
233 
237  public function pagesAction()
238  {
239  $this->view->assign('records', $this->administrationRepository->getPageStatistic());
240  }
241 
245  public function externalDocumentsAction()
246  {
247  $this->view->assign('records', $this->administrationRepository->getExternalDocumentsStatistic());
248  }
249 
255  public function statisticDetailsAction($pageHash = 0)
256  {
257  $pageHash = (int)$pageHash;
258  // Set back button
259  $icon = $this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-view-go-up', Icon::SIZE_SMALL);
260  $backButton = $this->view->getModuleTemplate()->getDocHeaderComponent()
261  ->getButtonBar()->makeLinkButton()
262  ->setTitle($this->getLanguageService()->sL('LLL:EXT:indexed_search/Resources/Private/Language/locallang.xlf:administration.back'))
263  ->setIcon($icon)
264  ->setHref($this->getHref('Administration', 'statistic'));
265  $this->view->getModuleTemplate()->getDocHeaderComponent()
266  ->getButtonBar()->addButton($backButton);
267 
268  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('index_phash');
269  $pageHashRow = $queryBuilder
270  ->select('*')
271  ->from('index_phash')
272  ->where(
273  $queryBuilder->expr()->eq(
274  'phash',
275  $queryBuilder->createNamedParameter($pageHash, \PDO::PARAM_INT)
276  )
277  )
278  ->execute()
279  ->fetch();
280 
281  if (!is_array($pageHashRow)) {
282  $this->redirect('statistic');
283  }
284 
285  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('index_debug');
286  $debugRow = $queryBuilder
287  ->select('*')
288  ->from('index_debug')
289  ->where(
290  $queryBuilder->expr()->eq(
291  'phash',
292  $queryBuilder->createNamedParameter($pageHash, \PDO::PARAM_INT)
293  )
294  )
295  ->execute()
296  ->fetchAll();
297  $debugInfo = [];
298  $lexer = '';
299  if (is_array($debugRow)) {
300  $debugInfo = unserialize($debugRow[0]['debuginfo']);
301  $lexer = $debugInfo['lexer'];
302  unset($debugInfo['lexer']);
303  }
304  $pageRecord = BackendUtility::getRecord('pages', $pageHashRow['data_page_id']);
305  $keywords = is_array($pageRecord) ? array_flip(GeneralUtility::trimExplode(',', $pageRecord['keywords'], true)) : [];
306 
307  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('index_words');
308  $wordRecords = $queryBuilder
309  ->select('index_words.*', 'index_rel.*')
310  ->from('index_words')
311  ->from('index_rel')
312  ->where(
313  $queryBuilder->expr()->eq(
314  'index_rel.phash',
315  $queryBuilder->createNamedParameter($pageHash, \PDO::PARAM_INT)
316  ),
317  $queryBuilder->expr()->eq(
318  'index_words.wid',
319  $queryBuilder->quoteIdentifier('index_rel.wid')
320  )
321  )
322  ->orderBy('index_words.baseword')
323  ->execute()
324  ->fetchAll();
325  foreach ($wordRecords as $id => $row) {
326  if (isset($keywords[$row['baseword']])) {
327  $wordRecords[$id]['is_keyword'] = true;
328  }
329  }
330  $metaphoneRows = $metaphone = [];
331  if ($this->enableMetaphoneSearch && is_array($wordRecords)) {
332  // Group metaphone hash
333  foreach ($wordRecords as $row) {
334  $metaphoneRows[$row['metaphone']][] = $row['baseword'];
335  }
336 
337  foreach ($metaphoneRows as $hash => $words) {
338  if (count($words) > 1) {
339  $metaphone[] = [
340  'metaphone' => $this->indexer->metaphone($words[0], 1), $hash,
341  'words' => $words,
342  'hash' => $hash
343  ];
344  }
345  }
346  }
347 
348  // sections
349  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('index_section');
350  $sections = $queryBuilder
351  ->select('*')
352  ->from('index_section')
353  ->where(
354  $queryBuilder->expr()->eq(
355  'phash',
356  $queryBuilder->createNamedParameter($pageHash, \PDO::PARAM_INT)
357  )
358  )
359  ->execute()
360  ->fetchAll();
361 
362  // top words
363  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('index_words');
364  $topCountWords = $queryBuilder
365  ->select('index_words.baseword', 'index_words.metaphone', 'index_rel.*')
366  ->from('index_words')
367  ->from('index_rel')
368  ->setMaxResults(20)
369  ->where(
370  $queryBuilder->expr()->eq(
371  'index_rel.phash',
372  $queryBuilder->createNamedParameter($pageHash, \PDO::PARAM_INT)
373  ),
374  $queryBuilder->expr()->eq(
375  'index_words.is_stopword',
376  $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
377  ),
378  $queryBuilder->expr()->eq(
379  'index_words.wid',
380  $queryBuilder->quoteIdentifier('index_rel.wid')
381  )
382  )
383  ->orderBy('index_rel.count', 'DESC')
384  ->execute()
385  ->fetchAll();
386 
387  // top frequency
388  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('index_words');
389  $topFrequency = $queryBuilder
390  ->select('index_words.baseword', 'index_words.metaphone', 'index_rel.*')
391  ->from('index_words')
392  ->from('index_rel')
393  ->setMaxResults(20)
394  ->where(
395  $queryBuilder->expr()->eq(
396  'index_rel.phash',
397  $queryBuilder->createNamedParameter($pageHash, \PDO::PARAM_INT)
398  ),
399  $queryBuilder->expr()->eq(
400  'index_words.is_stopword',
401  $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
402  ),
403  $queryBuilder->expr()->eq(
404  'index_words.wid',
405  $queryBuilder->quoteIdentifier('index_rel.wid')
406  )
407  )
408  ->orderBy('index_rel.freq', 'DESC')
409  ->execute()
410  ->fetchAll();
411 
412  $this->view->assignMultiple([
413  'phash' => (int)$pageHash,
414  'phashRow' => $pageHashRow,
415  'words' => $wordRecords,
416  'sections' => $sections,
417  'topCount' => $topCountWords,
418  'topFrequency' => $topFrequency,
419  'debug' => $debugInfo,
420  'lexer' => $lexer,
421  'metaphone' => $metaphone,
422  'page' => $pageRecord,
423  'keywords' => $keywords
424  ]);
425  }
426 
435  public function saveStopwordsKeywordsAction($pageHash, $pageId, $stopwords = [], $keywords = [])
436  {
437  if ($this->getBackendUserAuthentication()->isAdmin()) {
438  if (is_array($stopwords) && !empty($stopwords)) {
439  $this->administrationRepository->saveStopWords($stopwords);
440  }
441  if (is_array($keywords) && !empty($keywords)) {
442  $this->administrationRepository->saveKeywords($keywords, $pageId);
443  }
444  }
445 
446  $this->redirect('statisticDetails', null, null, ['pageHash' => $pageHash]);
447  }
448 
455  public function wordDetailAction($id = 0, $pageHash = 0)
456  {
457  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('index_phash');
458  $rows = $queryBuilder
459  ->select('index_phash.*', 'index_section.*', 'index_rel.*')
460  ->from('index_rel')
461  ->from('index_section')
462  ->from('index_phash')
463  ->where(
464  $queryBuilder->expr()->eq(
465  'index_rel.wid',
466  $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT)
467  ),
468  $queryBuilder->expr()->eq(
469  'index_rel.phash',
470  $queryBuilder->quoteIdentifier('index_section.phash')
471  ),
472  $queryBuilder->expr()->eq(
473  'index_section.phash',
474  $queryBuilder->quoteIdentifier('index_phash.phash')
475  )
476  )
477  ->orderBy('index_rel.freq', 'desc')
478  ->execute()
479  ->fetchAll();
480 
481  $this->view->assignMultiple([
482  'rows' => $rows,
483  'phash' => $pageHash
484  ]);
485  }
486 
493  public function statisticAction($depth = 1, $mode = 'overview')
494  {
495  if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['indexed_search']['external_parsers'])) {
496  foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['indexed_search']['external_parsers'] as $extension => $_objRef) {
498  $fileContentParser = GeneralUtility::getUserObj($_objRef);
499  if ($fileContentParser->softInit($extension)) {
500  $this->external_parsers[$extension] = $fileContentParser;
501  }
502  }
503  }
504  $this->administrationRepository->external_parsers = $this->external_parsers;
505 
506  $allLines = $this->administrationRepository->getTree($this->pageUid, $depth, $mode);
507 
508  $this->view->assignMultiple([
509  'levelTranslations' => explode('|', $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.enterSearchLevels')),
510  'tree' => $allLines,
511  'pageUid' => $this->pageUid,
512  'mode' => $mode,
513  'depth' => $depth
514  ]);
515  }
516 
524  public function deleteIndexedItemAction($id, $depth = 1, $mode = 'overview')
525  {
526  $this->administrationRepository->removeIndexedPhashRow($id, $this->pageUid, $depth);
527  $this->redirect('statistic', null, null, ['depth' => $depth, 'mode' => $mode]);
528  }
529 
539  protected function getHref($controller, $action, $parameters = [])
540  {
541  $uriBuilder = $this->objectManager->get(UriBuilder::class);
542  $uriBuilder->setRequest($this->request);
543  return $uriBuilder->reset()->uriFor($action, $parameters, $controller);
544  }
545 
549  protected function getBackendUserAuthentication()
550  {
551  return $GLOBALS['BE_USER'];
552  }
553 
557  protected function getLanguageService()
558  {
559  return $GLOBALS['LANG'];
560  }
561 }
static readPageAccess($id, $perms_clause)
processRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response)
redirect($actionName, $controllerName=null, $extensionName=null, array $arguments=null, $pageUid=null, $delay=0, $statusCode=303)
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
static makeInstance($className,... $constructorArguments)
static getRecord($table, $uid, $fields=' *', $where='', $useDeleteClause=true)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
injectAdministrationRepository(AdministrationRepository $administrationRepository)
saveStopwordsKeywordsAction($pageHash, $pageId, $stopwords=[], $keywords=[])