TYPO3 CMS  TYPO3_7-6
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  }
99  }
100 
104  protected function generateMenu()
105  {
106  $menuItems = [
107  'index' => [
108  'controller' => 'Administration',
109  'action' => 'index',
110  'label' => $this->getLanguageService()->sL('LLL:EXT:indexed_search/Resources/Private/Language/locallang.xml:administration.menu.general')
111  ],
112  'pages' => [
113  'controller' => 'Administration',
114  'action' => 'pages',
115  'label' => $this->getLanguageService()->sL('LLL:EXT:indexed_search/Resources/Private/Language/locallang.xml:administration.menu.pages')
116  ],
117  'externalDocuments' => [
118  'controller' => 'Administration',
119  'action' => 'externalDocuments',
120  'label' => $this->getLanguageService()->sL('LLL:EXT:indexed_search/Resources/Private/Language/locallang.xml:administration.menu.externalDocuments')
121  ],
122  'statistic' => [
123  'controller' => 'Administration',
124  'action' => 'statistic',
125  'label' => $this->getLanguageService()->sL('LLL:EXT:indexed_search/Resources/Private/Language/locallang.xml:administration.menu.statistic')
126  ]
127  ];
128  $uriBuilder = $this->objectManager->get(UriBuilder::class);
129  $uriBuilder->setRequest($this->request);
130 
131  $menu = $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
132  $menu->setIdentifier('IndexedSearchModuleMenu');
133 
134  foreach ($menuItems as $menuItemConfig) {
135  $isActive = $this->request->getControllerActionName() === $menuItemConfig['action'];
136  $menuItem = $menu->makeMenuItem()
137  ->setTitle($menuItemConfig['label'])
138  ->setHref($this->getHref($menuItemConfig['controller'], $menuItemConfig['action']))
139  ->setActive($isActive);
140  $menu->addMenuItem($menuItem);
141  }
142 
143  $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
144  }
145 
151  public function initializeAction()
152  {
153  $this->pageUid = (int)GeneralUtility::_GET('id');
154  $this->indexerConfig = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['indexed_search']);
155  $this->enableMetaphoneSearch = (bool)$this->indexerConfig['enableMetaphoneSearch'];
156  $this->indexer = GeneralUtility::makeInstance(Indexer::class);
157 
158  parent::initializeAction();
159  }
160 
168  public function processRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response)
169  {
170  $vars = GeneralUtility::_GET('tx_indexedsearch_web_indexedsearchisearch');
171 
172  $beUser = $this->getBackendUserAuthentication();
173  if (is_array($vars) && isset($vars['action']) && method_exists($this, $vars['action'] . 'Action')) {
174  $action = $vars['action'];
175 
176  switch ($action) {
177  case 'saveStopwordsKeywords':
178  $action = 'statisticDetails';
179  break;
180  case 'deleteIndexedItem':
181  $action = 'statistic';
182  break;
183  }
184 
185  $beUser->uc['indexed_search']['action'] = $action;
186  $beUser->uc['indexed_search']['arguments'] = $request->getArguments();
187  $beUser->writeUC();
188  } elseif (isset($beUser->uc['indexed_search']['action'])) {
189  if ($request instanceof WebRequest) {
190  $request->setControllerActionName($beUser->uc['indexed_search']['action']);
191  }
192  if (isset($beUser->uc['indexed_search']['arguments'])) {
193  $request->setArguments($beUser->uc['indexed_search']['arguments']);
194  }
195  }
196 
197  parent::processRequest($request, $response);
198  }
199 
205  {
206  $this->administrationRepository = $administrationRepository;
207  }
208 
214  public function indexAction()
215  {
216  $this->view->assignMultiple([
217  'records' => $this->administrationRepository->getRecordsNumbers(),
218  'phash' => $this->administrationRepository->getPageHashTypes()
219  ]);
220 
221  if ($this->pageUid) {
222  $last24hours = ' AND tstamp > ' . ($GLOBALS['EXEC_TIME'] - 24 * 60 * 60);
223  $last30days = ' AND tstamp > ' . ($GLOBALS['EXEC_TIME'] - 30 * 24 * 60 * 60);
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 
239  public function pagesAction()
240  {
241  $this->view->assign('records', $this->administrationRepository->getPageStatistic());
242  }
243 
249  public function externalDocumentsAction()
250  {
251  $this->view->assign('records', $this->administrationRepository->getExternalDocumentsStatistic());
252  }
253 
260  public function statisticDetailsAction($pageHash = 0)
261  {
262  // Set back button
263  $icon = $this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-view-go-up', Icon::SIZE_SMALL);
264  $backButton = $this->view->getModuleTemplate()->getDocHeaderComponent()
265  ->getButtonBar()->makeLinkButton()
266  ->setTitle($this->getLanguageService()->sL('LLL:EXT:indexed_search/Resources/Private/Language/locallang.xml:administration.back'))
267  ->setIcon($icon)
268  ->setHref($this->getHref('Administration', 'statistic'));
269  $this->view->getModuleTemplate()->getDocHeaderComponent()
270  ->getButtonBar()->addButton($backButton);
271 
272  $pageHash = (int)$pageHash;
273  $db = $this->getDatabaseConnection();
274  $pageHashRow = $db->exec_SELECTgetSingleRow('*', 'index_phash', 'phash = ' . (int)$pageHash);
275 
276  if (!is_array($pageHashRow)) {
277  $this->redirect('statistic');
278  }
279 
280  $debugRow = $db->exec_SELECTgetRows('*', 'index_debug', 'phash = ' . (int)$pageHash);
281  $debugInfo = [];
282  $lexer = '';
283  if (is_array($debugRow)) {
284  $debugInfo = unserialize($debugRow[0]['debuginfo']);
285  $lexer = $debugInfo['lexer'];
286  unset($debugInfo['lexer']);
287  }
288  $pageRecord = BackendUtility::getRecord('pages', $pageHashRow['data_page_id']);
289  $keywords = is_array($pageRecord) ? array_flip(GeneralUtility::trimExplode(',', $pageRecord['keywords'], true)) : [];
290  $wordRecords = $db->exec_SELECTgetRows(
291  'index_words.*, index_rel.*',
292  'index_rel, index_words',
293  'index_rel.phash = ' . (int)$pageHash . ' AND index_words.wid = index_rel.wid',
294  '',
295  'index_words.baseword'
296  );
297  foreach ($wordRecords as $id => $row) {
298  if (isset($keywords[$row['baseword']])) {
299  $wordRecords[$id]['is_keyword'] = true;
300  }
301  }
302  $metaphoneRows = $metaphone = [];
303  if ($this->enableMetaphoneSearch && is_array($wordRecords)) {
304  // Group metaphone hash
305  foreach ($wordRecords as $row) {
306  $metaphoneRows[$row['metaphone']][] = $row['baseword'];
307  }
308 
309  foreach ($metaphoneRows as $hash => $words) {
310  if (count($words) > 1) {
311  $metaphone[] = [
312  'metaphone' => $this->indexer->metaphone($words[0], 1), $hash,
313  'words' => $words,
314  'hash' => $hash
315  ];
316  }
317  }
318  }
319  $this->view->assignMultiple([
320  'phash' => $pageHash,
321  'phashRow' => $pageHashRow,
322  'words' => $wordRecords,
323  'sections' => $db->exec_SELECTgetRows(
324  '*',
325  'index_section',
326  'index_section.phash = ' . (int)$pageHash
327  ),
328  'topCount' => $db->exec_SELECTgetRows(
329  'index_words.baseword, index_words.metaphone, index_rel.*',
330  'index_rel, index_words',
331  'index_rel.phash = ' . (int)$pageHash . ' AND index_words.wid = index_rel.wid
332  AND index_words.is_stopword=0',
333  '',
334  'index_rel.count DESC',
335  '20'
336  ),
337  'topFrequency' => $db->exec_SELECTgetRows(
338  'index_words.baseword, index_words.metaphone, index_rel.*',
339  'index_rel, index_words',
340  'index_rel.phash = ' . (int)$pageHash . ' AND index_words.wid = index_rel.wid
341  AND index_words.is_stopword=0',
342  '',
343  'index_rel.freq DESC',
344  '20'
345  ),
346  'debug' => $debugInfo,
347  'lexer' => $lexer,
348  'metaphone' => $metaphone,
349  'page' => $pageRecord,
350  'keywords' => $keywords
351  ]);
352  }
353 
363  public function saveStopwordsKeywordsAction($pageHash, $pageId, $stopwords = [], $keywords = [])
364  {
365  if ($this->getBackendUserAuthentication()->isAdmin()) {
366  if (is_array($stopwords) && !empty($stopwords)) {
367  $this->administrationRepository->saveStopWords($stopwords);
368  }
369  if (is_array($keywords) && !empty($keywords)) {
370  $this->administrationRepository->saveKeywords($keywords, $pageId);
371  }
372  }
373 
374  $this->redirect('statisticDetails', null, null, ['pageHash' => $pageHash]);
375  }
376 
384  public function wordDetailAction($id = 0, $pageHash = 0)
385  {
386  $rows = $this->getDatabaseConnection()->exec_SELECTgetRows(
387  'index_phash.*, index_section.*, index_rel.*',
388  'index_rel, index_section, index_phash',
389  'index_rel.wid = ' . (int)$id . ' AND index_rel.phash = index_section.phash' . ' AND index_section.phash = index_phash.phash',
390  '',
391  'index_rel.freq DESC'
392  );
393 
394  $this->view->assignMultiple([
395  'rows' => $rows,
396  'phash' => $pageHash
397  ]);
398  }
399 
407  public function statisticAction($depth = 1, $mode = 'overview')
408  {
409  if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['indexed_search']['external_parsers'])) {
410  foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['indexed_search']['external_parsers'] as $extension => $_objRef) {
412  $fileContentParser = GeneralUtility::getUserObj($_objRef);
413  if ($fileContentParser->softInit($extension)) {
414  $this->external_parsers[$extension] = $fileContentParser;
415  }
416  }
417  }
418  $this->administrationRepository->external_parsers = $this->external_parsers;
419 
420  $allLines = $this->administrationRepository->getTree($this->pageUid, $depth, $mode);
421 
422  $this->view->assignMultiple([
423  'levelTranslations' => explode('|', $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.enterSearchLevels')),
424  'tree' => $allLines,
425  'pageUid' => $this->pageUid,
426  'mode' => $mode,
427  'depth' => $depth
428  ]);
429  }
430 
439  public function deleteIndexedItemAction($id, $depth = 1, $mode = 'overview')
440  {
441  $this->administrationRepository->removeIndexedPhashRow($id, $this->pageUid, $depth);
442  $this->redirect('statistic', null, null, ['depth' => $depth, 'mode' => $mode]);
443  }
444 
454  protected function getHref($controller, $action, $parameters = [])
455  {
456  $uriBuilder = $this->objectManager->get(UriBuilder::class);
457  $uriBuilder->setRequest($this->request);
458  return $uriBuilder->reset()->uriFor($action, $parameters, $controller);
459  }
460 
464  protected function getDatabaseConnection()
465  {
466  return $GLOBALS['TYPO3_DB'];
467  }
468 
472  protected function getBackendUserAuthentication()
473  {
474  return $GLOBALS['BE_USER'];
475  }
476 
480  protected function getLanguageService()
481  {
482  return $GLOBALS['LANG'];
483  }
484 }
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 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=[])