‪TYPO3CMS  ‪main
AdministrationController.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
24 use TYPO3\CMS\Backend\Utility\BackendUtility;
30 use TYPO3\CMS\Core\Imaging\IconSize;
38 use TYPO3\CMS\IndexedSearch\Indexer;
39 
46 {
47  protected int ‪$pageUid = 0;
48  protected array ‪$indexerConfig = [];
49 
50  public function ‪__construct(
51  protected readonly ‪ModuleTemplateFactory $moduleTemplateFactory,
52  protected readonly ‪AdministrationRepository $administrationRepository,
53  protected readonly Indexer $indexer,
54  protected readonly ‪IconFactory $iconFactory,
55  protected readonly ‪ExtensionConfiguration $extensionConfiguration,
56  protected readonly ‪ConnectionPool $connectionPool,
57  ) {}
58 
62  protected function ‪initializeModuleTemplate(ServerRequestInterface ‪$request): ‪ModuleTemplate
63  {
64  $languageService = $this->‪getLanguageService();
65  $menuItems = [
66  'statistic' => [
67  'controller' => 'Administration',
68  'action' => 'statistic',
69  'label' => $languageService->sL('LLL:EXT:indexed_search/Resources/Private/Language/locallang.xlf:administration.menu.statistic'),
70  ],
71  'pages' => [
72  'controller' => 'Administration',
73  'action' => 'pages',
74  'label' => $languageService->sL('LLL:EXT:indexed_search/Resources/Private/Language/locallang.xlf:administration.menu.pages'),
75  ],
76  'externalDocuments' => [
77  'controller' => 'Administration',
78  'action' => 'externalDocuments',
79  'label' => $languageService->sL('LLL:EXT:indexed_search/Resources/Private/Language/locallang.xlf:administration.menu.externalDocuments'),
80  ],
81  'index' => [
82  'controller' => 'Administration',
83  'action' => 'index',
84  'label' => $languageService->sL('LLL:EXT:indexed_search/Resources/Private/Language/locallang.xlf:administration.menu.general'),
85  ],
86  ];
87 
88  ‪$view = $this->moduleTemplateFactory->create(‪$request);
89 
90  $menu = ‪$view->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
91  $menu->setIdentifier('IndexedSearchModuleMenu');
92  $menu->setLabel(
93  $languageService->sL(
94  'LLL:EXT:backend/Resources/Private/Language/locallang.xlf:moduleMenu.dropdown.label'
95  )
96  );
97 
98  $context = '';
99  foreach ($menuItems as $menuItemConfig) {
100  $isActive = $this->request->getControllerActionName() === $menuItemConfig['action'];
101  $menuItem = $menu->makeMenuItem()
102  ->setTitle($menuItemConfig['label'])
103  ->setHref($this->uriBuilder->reset()->uriFor($menuItemConfig['action'], [], $menuItemConfig['controller']))
104  ->setActive($isActive);
105  $menu->addMenuItem($menuItem);
106  if ($isActive) {
107  $context = $menuItemConfig['label'];
108  }
109  }
110 
111  ‪$view->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
112  ‪$view->setTitle(
113  $languageService->sL('LLL:EXT:indexed_search/Resources/Private/Language/locallang_mod.xlf:mlang_tabs_tab'),
114  $context
115  );
116 
117  $permissionClause = $this->‪getBackendUserAuthentication()->getPagePermsClause(‪Permission::PAGE_SHOW);
118  $pageRecord = BackendUtility::readPageAccess($this->pageUid, $permissionClause);
119  if ($pageRecord) {
120  ‪$view->getDocHeaderComponent()->setMetaInformation($pageRecord);
121  }
122  ‪$view->setFlashMessageQueue($this->‪getFlashMessageQueue());
123 
124  return ‪$view;
125  }
126 
130  protected function ‪initializeAction(): void
131  {
132  $this->pageUid = (int)($this->request->getQueryParams()['id'] ?? 0);
133  $this->indexerConfig = $this->extensionConfiguration->get('indexed_search') ?? [];
134  parent::initializeAction();
135  }
136 
140  public function ‪processRequest(‪RequestInterface ‪$request): ResponseInterface
141  {
143  $beUser = $this->‪getBackendUserAuthentication();
144 
145  if (isset(‪$arguments['action']) && method_exists($this, ‪$arguments['action'] . 'Action')) {
146  $action = ‪$arguments['action'];
147  switch ($action) {
148  case 'saveStopwords':
149  $action = 'statisticDetails';
150  break;
151  case 'deleteIndexedItem':
152  $action = 'statistic';
153  break;
154  }
155  $beUser->uc['indexed_search']['action'] = $action;
156  $beUser->uc['indexed_search']['arguments'] = ‪$arguments;
157  $beUser->writeUC();
158  } elseif (isset($beUser->uc['indexed_search']['action'])) {
159  ‪$request = ‪$request->‪withControllerActionName((string)$beUser->uc['indexed_search']['action']);
160  if (isset($beUser->uc['indexed_search']['arguments'])) {
161  ‪$request = ‪$request->‪withArguments($beUser->uc['indexed_search']['arguments']);
162  }
163  }
164 
165  return parent::processRequest(‪$request);
166  }
167 
171  protected function ‪indexAction(): ResponseInterface
172  {
173  ‪$view = $this->‪initializeModuleTemplate($this->request);
174  ‪$view->assignMultiple([
175  'records' => $this->administrationRepository->getRecordsNumbers(),
176  'phash' => $this->administrationRepository->getPageHashTypes(),
177  ]);
178  if ($this->pageUid) {
179  $expressionBuilder = $this->connectionPool->getQueryBuilderForTable('index_stat_word')->expr();
180  $last24hours = $expressionBuilder->gt('tstamp', ‪$GLOBALS['EXEC_TIME'] - 86400);
181  $last30days = $expressionBuilder->gt('tstamp', ‪$GLOBALS['EXEC_TIME'] - 30 * 86400);
182  ‪$view->assignMultiple([
183  'extensionConfiguration' => $this->indexerConfig,
184  'pageUid' => $this->pageUid,
185  'all' => $this->administrationRepository->getGeneralSearchStatistic('', $this->pageUid),
186  'last24hours' => $this->administrationRepository->getGeneralSearchStatistic($last24hours, $this->pageUid),
187  'last30days' => $this->administrationRepository->getGeneralSearchStatistic($last30days, $this->pageUid),
188  ]);
189  }
190  return ‪$view->renderResponse('Administration/Index');
191  }
192 
196  protected function ‪pagesAction(): ResponseInterface
197  {
198  ‪$view = $this->‪initializeModuleTemplate($this->request);
199  ‪$view->assignMultiple([
200  'extensionConfiguration' => $this->indexerConfig,
201  'records' => $this->administrationRepository->getPageStatistic(),
202  'pageUid' => $this->pageUid,
203  ]);
204  return ‪$view->renderResponse('Administration/Pages');
205  }
206 
210  protected function ‪externalDocumentsAction(): ResponseInterface
211  {
212  ‪$view = $this->‪initializeModuleTemplate($this->request);
213  ‪$view->assignMultiple([
214  'extensionConfiguration' => $this->indexerConfig,
215  'records' => $this->administrationRepository->getExternalDocumentsStatistic(),
216  'pageUid' => $this->pageUid,
217  ]);
218  return ‪$view->renderResponse('Administration/ExternalDocuments');
219  }
220 
224  protected function ‪statisticDetailsAction(string $pageHash): ResponseInterface
225  {
226  ‪$view = $this->‪initializeModuleTemplate($this->request);
227  $buttonBar = ‪$view->getDocHeaderComponent()->getButtonBar();
228 
229  // Set back button
230  $backButton = $buttonBar
231  ->makeLinkButton()
232  ->setTitle($this->‪getLanguageService()->sL('LLL:EXT:indexed_search/Resources/Private/Language/locallang.xlf:administration.back'))
233  ->setShowLabelText(true)
234  ->setIcon($this->iconFactory->getIcon('actions-view-go-back', IconSize::SMALL))
235  ->setHref($this->uriBuilder->reset()->uriFor('statistic', [], 'Administration'));
236  $buttonBar->addButton($backButton);
237 
238  $queryBuilder = $this->connectionPool->getQueryBuilderForTable('index_phash');
239  $pageHashRow = $queryBuilder
240  ->select('*')
241  ->from('index_phash')
242  ->where(
243  $queryBuilder->expr()->eq(
244  'phash',
245  $queryBuilder->createNamedParameter($pageHash, ‪Connection::PARAM_STR)
246  )
247  )
248  ->executeQuery()
249  ->fetchAssociative();
250 
251  if (!is_array($pageHashRow)) {
252  return $this->‪redirect('statistic');
253  }
254 
255  $queryBuilder = $this->connectionPool->getQueryBuilderForTable('index_debug');
256  $debugRow = $queryBuilder
257  ->select('debuginfo')
258  ->from('index_debug')
259  ->where(
260  $queryBuilder->expr()->eq(
261  'phash',
262  $queryBuilder->createNamedParameter($pageHash, ‪Connection::PARAM_STR)
263  )
264  )
265  ->executeQuery()
266  ->fetchAssociative();
267  $debugInfo = [];
268  if (is_array($debugRow)) {
269  $debugInfo = json_decode($debugRow['debuginfo'], true);
270  }
271  $pageRecord = BackendUtility::getRecord('pages', $pageHashRow['data_page_id']);
272 
273  $queryBuilder = $this->connectionPool->getQueryBuilderForTable('index_words');
274  $wordRecords = $queryBuilder
275  ->select('index_words.*', 'index_rel.*')
276  ->from('index_words')
277  ->from('index_rel')
278  ->where(
279  $queryBuilder->expr()->eq(
280  'index_rel.phash',
281  $queryBuilder->createNamedParameter($pageHash, ‪Connection::PARAM_STR)
282  ),
283  $queryBuilder->expr()->eq(
284  'index_words.wid',
285  $queryBuilder->quoteIdentifier('index_rel.wid')
286  )
287  )
288  ->orderBy('index_words.baseword')
289  ->executeQuery()
290  ->fetchAllAssociative();
291 
292  // sections
293  $queryBuilder = $this->connectionPool->getQueryBuilderForTable('index_section');
294  $sections = $queryBuilder
295  ->select('*')
296  ->from('index_section')
297  ->where(
298  $queryBuilder->expr()->eq(
299  'phash',
300  $queryBuilder->createNamedParameter($pageHash, ‪Connection::PARAM_STR)
301  )
302  )
303  ->executeQuery()
304  ->fetchAllAssociative();
305 
306  // top words
307  $queryBuilder = $this->connectionPool->getQueryBuilderForTable('index_words');
308  $topCountWords = $queryBuilder
309  ->select('index_words.baseword', 'index_rel.*')
310  ->from('index_words')
311  ->from('index_rel')
312  ->setMaxResults(20)
313  ->where(
314  $queryBuilder->expr()->eq(
315  'index_rel.phash',
316  $queryBuilder->createNamedParameter($pageHash, ‪Connection::PARAM_STR)
317  ),
318  $queryBuilder->expr()->eq(
319  'index_words.is_stopword',
320  $queryBuilder->createNamedParameter(0, ‪Connection::PARAM_INT)
321  ),
322  $queryBuilder->expr()->eq(
323  'index_words.wid',
324  $queryBuilder->quoteIdentifier('index_rel.wid')
325  )
326  )
327  ->orderBy('index_rel.count', 'DESC')
328  ->executeQuery()
329  ->fetchAllAssociative();
330 
331  // top frequency
332  $queryBuilder = $this->connectionPool->getQueryBuilderForTable('index_words');
333  $topFrequency = $queryBuilder
334  ->select('index_words.baseword', 'index_rel.*')
335  ->from('index_words')
336  ->from('index_rel')
337  ->setMaxResults(20)
338  ->where(
339  $queryBuilder->expr()->eq(
340  'index_rel.phash',
341  $queryBuilder->createNamedParameter($pageHash, ‪Connection::PARAM_STR)
342  ),
343  $queryBuilder->expr()->eq(
344  'index_words.is_stopword',
345  $queryBuilder->createNamedParameter(0, ‪Connection::PARAM_INT)
346  ),
347  $queryBuilder->expr()->eq(
348  'index_words.wid',
349  $queryBuilder->quoteIdentifier('index_rel.wid')
350  )
351  )
352  ->orderBy('index_rel.freq', 'DESC')
353  ->executeQuery()
354  ->fetchAllAssociative();
355 
356  ‪$view->assignMultiple([
357  'extensionConfiguration' => $this->indexerConfig,
358  'phash' => $pageHash,
359  'phashRow' => $pageHashRow,
360  'words' => $wordRecords,
361  'sections' => $sections,
362  'topCount' => $topCountWords,
363  'topFrequency' => $topFrequency,
364  'debug' => $debugInfo,
365  'page' => $pageRecord,
366  ]);
367 
368  return ‪$view->renderResponse('Administration/StatisticDetails');
369  }
370 
374  protected function ‪saveStopwordsAction(string $pageHash, array $stopwords = []): ResponseInterface
375  {
376  if ($this->‪getBackendUserAuthentication()->isAdmin()) {
377  if (is_array($stopwords) && !empty($stopwords)) {
378  $this->administrationRepository->saveStopWords($stopwords);
379  }
380  }
381  return $this->‪redirect('statisticDetails', null, null, ['pageHash' => $pageHash]);
382  }
383 
387  protected function ‪wordDetailAction(string $wordHash, string $pageHash, string $wordTitle): ResponseInterface
388  {
389  $queryBuilder = $this->connectionPool->getQueryBuilderForTable('index_phash');
390  $rows = $queryBuilder
391  ->select('index_phash.*', 'index_section.*', 'index_rel.*')
392  ->from('index_rel')
393  ->from('index_section')
394  ->from('index_phash')
395  ->where(
396  $queryBuilder->expr()->eq(
397  'index_rel.wid',
398  $queryBuilder->createNamedParameter($wordHash, ‪Connection::PARAM_STR)
399  ),
400  $queryBuilder->expr()->eq(
401  'index_rel.phash',
402  $queryBuilder->quoteIdentifier('index_section.phash')
403  ),
404  $queryBuilder->expr()->eq(
405  'index_section.phash',
406  $queryBuilder->quoteIdentifier('index_phash.phash')
407  )
408  )
409  ->orderBy('index_rel.freq', 'desc')
410  ->executeQuery()
411  ->fetchAllAssociative();
412 
413  ‪$view = $this->‪initializeModuleTemplate($this->request);
414  $buttonBar = ‪$view->getDocHeaderComponent()->getButtonBar();
415 
416  // Set back button
417  $backButton = $buttonBar
418  ->makeLinkButton()
419  ->setTitle($this->‪getLanguageService()->sL('LLL:EXT:indexed_search/Resources/Private/Language/locallang.xlf:administration.back'))
420  ->setShowLabelText(true)
421  ->setIcon($this->iconFactory->getIcon('actions-view-go-back', IconSize::SMALL))
422  ->setHref($this->uriBuilder->reset()->uriFor('statisticDetails', ['pageHash' => $pageHash], 'Administration'));
423  $buttonBar->addButton($backButton);
424 
425  ‪$view->assignMultiple([
426  'extensionConfiguration' => $this->indexerConfig,
427  'rows' => $rows,
428  'phash' => $pageHash,
429  'wordTitle' => $wordTitle,
430  ]);
431  return ‪$view->renderResponse('Administration/WordDetail');
432  }
433 
437  protected function ‪statisticAction(int $depth = 1, string $mode = 'overview'): ResponseInterface
438  {
439  $externalParsers = [];
440  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['indexed_search']['external_parsers'] ?? [] as $extension => $className) {
442  $fileContentParser = GeneralUtility::makeInstance($className);
443  if ($fileContentParser->softInit($extension)) {
444  $externalParsers[$extension] = $fileContentParser;
445  }
446  }
447  $this->administrationRepository->external_parsers = $externalParsers;
448  $allLines = $this->administrationRepository->getTree($this->pageUid, $depth, $mode);
449  ‪$view = $this->‪initializeModuleTemplate($this->request);
450  ‪$view->assignMultiple([
451  'extensionConfiguration' => $this->indexerConfig,
452  'levelTranslations' => explode('|', $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.enterSearchLevels')),
453  'tree' => $allLines,
454  'pageUid' => $this->pageUid,
455  'mode' => $mode,
456  'depth' => $depth,
457  ]);
458  return ‪$view->renderResponse('Administration/Statistic');
459  }
460 
464  protected function ‪deleteIndexedItemAction(string $itemId, int $depth = 1, string $mode = 'overview'): ResponseInterface
465  {
466  $this->administrationRepository->removeIndexedPhashRow($itemId, $this->pageUid, $depth);
467  return $this->‪redirect('statistic', null, null, ['depth' => $depth, 'mode' => $mode]);
468  }
469 
471  {
472  return ‪$GLOBALS['BE_USER'];
473  }
474 
476  {
477  return ‪$GLOBALS['LANG'];
478  }
479 }
‪TYPO3\CMS\IndexedSearch\Controller\AdministrationController\initializeModuleTemplate
‪initializeModuleTemplate(ServerRequestInterface $request)
Definition: AdministrationController.php:62
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:52
‪TYPO3\CMS\IndexedSearch\Controller\AdministrationController\getLanguageService
‪getLanguageService()
Definition: AdministrationController.php:475
‪TYPO3\CMS\IndexedSearch\Controller\AdministrationController\saveStopwordsAction
‪saveStopwordsAction(string $pageHash, array $stopwords=[])
Definition: AdministrationController.php:374
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\$request
‪RequestInterface $request
Definition: ActionController.php:106
‪TYPO3\CMS\IndexedSearch\Controller\AdministrationController\pagesAction
‪pagesAction()
Definition: AdministrationController.php:196
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration
Definition: ExtensionConfiguration.php:47
‪TYPO3\CMS\Backend\Template\ModuleTemplateFactory
Definition: ModuleTemplateFactory.php:33
‪TYPO3\CMS\IndexedSearch\Controller\AdministrationController\initializeAction
‪initializeAction()
Definition: AdministrationController.php:130
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\$view
‪ViewInterface $view
Definition: ActionController.php:82
‪TYPO3\CMS\IndexedSearch\Controller\AdministrationController\deleteIndexedItemAction
‪deleteIndexedItemAction(string $itemId, int $depth=1, string $mode='overview')
Definition: AdministrationController.php:464
‪TYPO3\CMS\IndexedSearch\Controller\AdministrationController\getBackendUserAuthentication
‪getBackendUserAuthentication()
Definition: AdministrationController.php:470
‪TYPO3\CMS\IndexedSearch\Controller\AdministrationController\statisticAction
‪statisticAction(int $depth=1, string $mode='overview')
Definition: AdministrationController.php:437
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\IndexedSearch\Controller\AdministrationController\$indexerConfig
‪array $indexerConfig
Definition: AdministrationController.php:48
‪TYPO3\CMS\Core\Database\Connection\PARAM_STR
‪const PARAM_STR
Definition: Connection.php:57
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:46
‪TYPO3\CMS\Core\Type\Bitmask\Permission
Definition: Permission.php:26
‪TYPO3\CMS\IndexedSearch\Controller\AdministrationController
Definition: AdministrationController.php:46
‪TYPO3\CMS\IndexedSearch\Domain\Repository\AdministrationRepository
Definition: AdministrationRepository.php:37
‪TYPO3\CMS\IndexedSearch\Controller
Definition: AdministrationController.php:18
‪TYPO3\CMS\IndexedSearch\Controller\AdministrationController\statisticDetailsAction
‪statisticDetailsAction(string $pageHash)
Definition: AdministrationController.php:224
‪TYPO3\CMS\Extbase\Mvc\RequestInterface\withControllerActionName
‪withControllerActionName(string $actionName)
‪TYPO3\CMS\IndexedSearch\FileContentParser
Definition: FileContentParser.php:36
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Type\Bitmask\Permission\PAGE_SHOW
‪const PAGE_SHOW
Definition: Permission.php:35
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\$arguments
‪Arguments $arguments
Definition: ActionController.php:119
‪TYPO3\CMS\IndexedSearch\Controller\AdministrationController\wordDetailAction
‪wordDetailAction(string $wordHash, string $pageHash, string $wordTitle)
Definition: AdministrationController.php:387
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\redirect
‪redirect(?string $actionName, ?string $controllerName=null, ?string $extensionName=null, ?array $arguments=null, ?int $pageUid=null, $_=null, int $statusCode=303)
Definition: ActionController.php:684
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Extbase\Mvc\RequestInterface\withArguments
‪withArguments(array $arguments)
‪TYPO3\CMS\Extbase\Mvc\RequestInterface
Definition: RequestInterface.php:24
‪TYPO3\CMS\IndexedSearch\Controller\AdministrationController\$pageUid
‪int $pageUid
Definition: AdministrationController.php:47
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController
Definition: ActionController.php:63
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\getFlashMessageQueue
‪getFlashMessageQueue(string $identifier=null)
Definition: ActionController.php:658
‪TYPO3\CMS\IndexedSearch\Controller\AdministrationController\processRequest
‪processRequest(RequestInterface $request)
Definition: AdministrationController.php:140
‪TYPO3\CMS\IndexedSearch\Controller\AdministrationController\indexAction
‪indexAction()
Definition: AdministrationController.php:171
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\IndexedSearch\Controller\AdministrationController\__construct
‪__construct(protected readonly ModuleTemplateFactory $moduleTemplateFactory, protected readonly AdministrationRepository $administrationRepository, protected readonly Indexer $indexer, protected readonly IconFactory $iconFactory, protected readonly ExtensionConfiguration $extensionConfiguration, protected readonly ConnectionPool $connectionPool,)
Definition: AdministrationController.php:50
‪TYPO3\CMS\Extbase\Mvc\RequestInterface\getArguments
‪getArguments()
‪TYPO3\CMS\IndexedSearch\Controller\AdministrationController\externalDocumentsAction
‪externalDocumentsAction()
Definition: AdministrationController.php:210