[TASK] Doctrine: Migrate indexed_search part 1 79/48579/13
authorChristian Kuhn <lolli@schwarzbu.ch>
Wed, 3 Aug 2016 15:30:37 +0000 (17:30 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Wed, 3 Aug 2016 16:58:39 +0000 (18:58 +0200)
Migrate the "obvious" queries of ext:indexed_search

Resolves: #77347
Releases: master
Change-Id: I8f4f4c1bb4091689dcf9b500771a3d09e127a0ed
Reviewed-on: https://review.typo3.org/48579
Tested-by: Bamboo TYPO3com <info@typo3.com>
Reviewed-by: Tobias Adolph <mail@tobiasadolph.de>
Tested-by: Tobias Adolph <mail@tobiasadolph.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/indexed_search/Classes/Controller/AdministrationController.php
typo3/sysext/indexed_search/Classes/Controller/SearchController.php
typo3/sysext/indexed_search/Classes/Domain/Repository/AdministrationRepository.php
typo3/sysext/indexed_search/Classes/Hook/CrawlerHook.php
typo3/sysext/indexed_search/Classes/Indexer.php

index 30643ff..5742de0 100644 (file)
@@ -16,6 +16,7 @@ namespace TYPO3\CMS\IndexedSearch\Controller;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Backend\View\BackendTemplateView;
 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
+use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Database\DatabaseConnection;
 use TYPO3\CMS\Core\Imaging\Icon;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -269,15 +270,25 @@ class AdministrationController extends ActionController
         $this->view->getModuleTemplate()->getDocHeaderComponent()
             ->getButtonBar()->addButton($backButton);
 
-        $pageHash = (int)$pageHash;
-        $db = $this->getDatabaseConnection();
-        $pageHashRow = $db->exec_SELECTgetSingleRow('*', 'index_phash', 'phash = ' . (int)$pageHash);
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('index_phash');
+        $pageHashRow = $queryBuilder
+            ->select('*')
+            ->from('index_phash')
+            ->where($queryBuilder->expr()->eq('phash', (int)$pageHash))
+            ->execute()
+            ->fetch();
 
         if (!is_array($pageHashRow)) {
             $this->redirect('statistic');
         }
 
-        $debugRow = $db->exec_SELECTgetRows('*', 'index_debug', 'phash = ' . (int)$pageHash);
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('index_debug');
+        $debugRow = $queryBuilder
+            ->select('*')
+            ->from('index_debug')
+            ->where($queryBuilder->expr()->eq('phash', (int)$pageHash))
+            ->execute()
+            ->fetchAll();
         $debugInfo = array();
         $lexer = '';
         if (is_array($debugRow)) {
@@ -317,7 +328,7 @@ class AdministrationController extends ActionController
             }
         }
         $this->view->assignMultiple(array(
-            'phash' => $pageHash,
+            'phash' => (int)$pageHash,
             'phashRow' => $pageHashRow,
             'words' => $wordRecords,
             'sections' => $db->exec_SELECTgetRows(
index 671b010..2eff078 100644 (file)
@@ -15,6 +15,7 @@ namespace TYPO3\CMS\IndexedSearch\Controller;
  */
 
 use TYPO3\CMS\Core\Charset\CharsetConverter;
+use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Html\HtmlParser;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\MathUtility;
@@ -659,13 +660,18 @@ class SearchController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionControlle
         if ($row['show_resume']) {
             if (!$noMarkup) {
                 $markedSW = '';
-                $res = $this->getDatabaseConnection()->exec_SELECTquery('*', 'index_fulltext', 'phash=' . (int)$row['phash']);
-                if ($ftdrow = $this->getDatabaseConnection()->sql_fetch_assoc($res)) {
+                $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('index_fulltext');
+                $ftdrow = $queryBuilder
+                    ->select('*')
+                    ->from('index_fulltext')
+                    ->where($queryBuilder->expr()->eq('phash', (int)$row['phash']))
+                    ->execute()
+                    ->fetch();
+                if ($ftdrow !== false) {
                     // Cut HTTP references after some length
                     $content = preg_replace('/(http:\\/\\/[^ ]{' . $this->settings['results.']['hrefInSummaryCropAfter'] . '})([^ ]+)/i', '$1...', $ftdrow['fulltextdata']);
                     $markedSW = $this->markupSWpartsOfString($content);
                 }
-                $this->getDatabaseConnection()->sql_free_result($res);
             }
             if (!trim($markedSW)) {
                 $outputStr = $this->charsetConverter->crop('utf-8', $row['item_description'], $length, $this->settings['results.']['summaryCropSignifier']);
@@ -772,9 +778,11 @@ class SearchController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionControlle
             // Time stamp
             'tstamp' => $GLOBALS['EXEC_TIME']
         );
-        $this->getDatabaseConnection()->exec_INSERTquery('index_stat_search', $insertFields);
-        $newId = $this->getDatabaseConnection()->sql_insert_id();
+        $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('index_search_stat');
+        $connection->insert('index_stat_search', $insertFields);
+        $newId = $connection->lastInsertId();
         if ($newId) {
+            $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('index_stat_word');
             foreach ($searchWords as $val) {
                 $insertFields = array(
                     'word' => $val['sword'],
@@ -784,7 +792,7 @@ class SearchController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionControlle
                     // search page id for indexed search stats
                     'pageid' => $GLOBALS['TSFE']->id
                 );
-                $this->getDatabaseConnection()->exec_INSERTquery('index_stat_word', $insertFields);
+                $connection->insert('index_stat_word', $insertFields);
             }
         }
     }
index 9a91b5c..78deac2 100644 (file)
@@ -19,6 +19,7 @@ use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
 use TYPO3\CMS\Core\Cache\CacheManager;
 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
+use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\DataHandling\DataHandler;
 use TYPO3\CMS\Core\Imaging\Icon;
 use TYPO3\CMS\Core\Imaging\IconFactory;
@@ -56,15 +57,20 @@ class AdministrationRepository
      */
     public function getGrlistRecord($phash)
     {
-        $db = $this->getDatabaseConnection();
-        $res = $db->exec_SELECTquery('index_grlist.*', 'index_grlist', 'phash=' . (int)$phash);
-        $allRows = array();
-        $numberOfRows = $db->sql_num_rows($res);
-        while ($row = $db->sql_fetch_assoc($res)) {
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('index_grlist');
+        $res = $queryBuilder
+            ->select('*')
+            ->from('index_grlist')
+            ->where(
+                $queryBuilder->expr()->eq('phash', (int)$phash)
+            )
+            ->execute();
+        $numberOfRows = $res->rowCount();
+        $allRows = [];
+        while ($row = $res->fetch()) {
             $row['pcount'] = $numberOfRows;
             $allRows[] = $row;
         }
-        $db->sql_free_result($res);
         return $allRows;
     }
 
@@ -76,7 +82,13 @@ class AdministrationRepository
      */
     public function getNumberOfFulltextRecords($phash)
     {
-        return $this->getDatabaseConnection()->exec_SELECTcountRows('phash', 'index_fulltext', 'phash=' . (int)$phash);
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('index_fulltext');
+        return $queryBuilder
+            ->count('phash')
+            ->from('index_fulltext')
+            ->where($queryBuilder->expr()->eq('phash', (int)$phash))
+            ->execute()
+            ->fetchColumn(0);
     }
 
     /**
@@ -87,7 +99,13 @@ class AdministrationRepository
      */
     public function getNumberOfWords($phash)
     {
-        return $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'index_rel', 'phash=' . (int)$phash);
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('index_rel');
+        return $queryBuilder
+            ->count('*')
+            ->from('index_rel')
+            ->where($queryBuilder->expr()->eq('phash', (int)$phash))
+            ->execute()
+            ->fetchColumn(0);
     }
 
     /**
@@ -147,7 +165,12 @@ class AdministrationRepository
         );
         $recordList = array();
         foreach ($tables as $tableName) {
-            $recordList[$tableName] = $this->getDatabaseConnection()->exec_SELECTcountRows('*', $tableName);
+            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);
+            $recordList[$tableName] = $queryBuilder
+                ->count('*')
+                ->from($tableName)
+                ->execute()
+                ->fetchColumn(0);
         }
         return $recordList;
     }
@@ -193,19 +216,17 @@ class AdministrationRepository
      */
     protected function countUniqueTypes($itemType)
     {
-        $db = $this->getDatabaseConnection();
-        $res = $db->exec_SELECTquery(
-            'count(*)',
-            'index_phash',
-            'item_type=' . $db->fullQuoteStr($itemType, 'index_phash'),
-            'phash_grouping'
-        );
         $items = array();
-        while ($row = $db->sql_fetch_row($res)) {
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('index_phash');
+        $res = $queryBuilder
+            ->count('*')
+            ->from('index_phash')
+            ->where($queryBuilder->expr()->eq('item_type', $queryBuilder->createNamedParameter($itemType)))
+            ->groupBy('phash_grouping')
+            ->execute();
+        while ($row = $res->fetch()) {
             $items[] = $row;
         }
-        $db->sql_free_result($res);
-
         return count($items);
     }
 
@@ -217,7 +238,13 @@ class AdministrationRepository
      */
     public function getNumberOfSections($pageHash)
     {
-        return $this->getDatabaseConnection()->exec_SELECTcountRows('phash', 'index_section', 'phash=' . (int)$pageHash);
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('index_section');
+        return $queryBuilder
+            ->count('phash')
+            ->from('index_section')
+            ->where($queryBuilder->expr()->eq('phash', (int)$pageHash))
+            ->execute()
+            ->fetchColumn(0);
     }
 
     /**
@@ -457,16 +484,21 @@ class AdministrationRepository
             $phashRows = GeneralUtility::trimExplode(',', $phashList, true);
         }
 
-        $db = $this->getDatabaseConnection();
         foreach ($phashRows as $phash) {
             $phash = (int)$phash;
             if ($phash > 0) {
                 $idList = array();
-                $res = $db->exec_SELECTquery('page_id', 'index_section', 'phash=' . $phash);
-                while ($row = $db->sql_fetch_assoc($res)) {
+                $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('index_section');
+                $res = $queryBuilder
+                    ->select('page_id')
+                    ->from('index_section')
+                    ->where(
+                        $queryBuilder->expr()->eq('phash', (int)$phash)
+                    )
+                    ->execute();
+                while ($row = $res->fetch()) {
                     $idList[] = (int)$row['page_id'];
                 }
-                $db->sql_free_result($res);
 
                 if (!empty($idList)) {
                     /** @var FrontendInterface $pageCache */
@@ -479,7 +511,13 @@ class AdministrationRepository
                 // Removing old registrations for all tables.
                 $tableArr = array('index_phash', 'index_rel', 'index_section', 'index_grlist', 'index_fulltext', 'index_debug');
                 foreach ($tableArr as $table) {
-                    $db->exec_DELETEquery($table, 'phash=' . $phash);
+                    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
+                    $queryBuilder
+                        ->delete($table)
+                        ->where(
+                            $queryBuilder->expr()->eq('phash', (int)$phash)
+                        )
+                        ->execute();
                 }
             }
         }
@@ -494,10 +532,12 @@ class AdministrationRepository
     public function saveStopWords(array $words)
     {
         foreach ($words as $wid => $state) {
-            $fieldArray = array(
-                'is_stopword' => (int)$state
-            );
-            $this->getDatabaseConnection()->exec_UPDATEquery('index_words', 'wid=' . (int)$wid, $fieldArray);
+            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('index_words');
+            $queryBuilder
+                ->update('index_words')
+                ->set('is_stopword', (int)$state)
+                ->where($queryBuilder->expr()->eq('wid', (int)$wid))
+                ->execute();
         }
     }
 
index 8602b63..e47534e 100644 (file)
@@ -16,6 +16,7 @@ namespace TYPO3\CMS\IndexedSearch\Hook;
 
 use TYPO3\CMS\Backend\Form\FormEngine;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
@@ -82,7 +83,8 @@ class CrawlerHook
                 'timer_next_indexing' => $nextTime,
                 'session_data' => ''
             );
-            $GLOBALS['TYPO3_DB']->exec_UPDATEquery('index_config', 'uid=' . (int)$cfgRec['uid'], $field_array);
+            $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('index_config');
+            $connection->update('index_config', $field_array, ['uid' => (int)$cfgRec['uid']]);
             // Based on configuration type:
             switch ($cfgRec['type']) {
                 case 1:
@@ -212,10 +214,14 @@ class CrawlerHook
                         }
                 }
                 // Save process data which might be modified:
-                $field_array = array(
-                    'session_data' => serialize($session_data)
-                );
-                $GLOBALS['TYPO3_DB']->exec_UPDATEquery('index_config', 'uid=' . (int)$cfgRec['uid'], $field_array);
+                $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('index_config');
+                $queryBuilder
+                    ->update('index_config')
+                    ->where(
+                        $queryBuilder->expr()->eq('uid', (int)$cfgRec['uid'])
+                    )
+                    ->set('session_data', serialize($session_data))
+                    ->execute();
             }
         }
         return array('log' => $params);
index 4749ed2..f07c061 100644 (file)
@@ -1483,7 +1483,9 @@ class Indexer
             'freeIndexSetId' => (int)$this->conf['freeIndexSetId']
         );
         if (IndexedSearchUtility::isTableUsed('index_phash')) {
-            $GLOBALS['TYPO3_DB']->exec_INSERTquery('index_phash', $fields);
+            $connection = GeneralUtility::makeInstance(ConnectionPool::class)
+                ->getConnectionForTable('index_phash');
+            $connection->insert('index_phash', $fields);
         }
         // PROCESSING index_section
         $this->submit_section($this->hash['phash'], $this->hash['phash']);
@@ -1499,7 +1501,9 @@ class Indexer
             $fields['fulltextdata'] = substr($fields['fulltextdata'], 0, $this->indexerConfig['fullTextDataLength']);
         }
         if (IndexedSearchUtility::isTableUsed('index_fulltext')) {
-            $GLOBALS['TYPO3_DB']->exec_INSERTquery('index_fulltext', $fields);
+            $connection = GeneralUtility::makeInstance(ConnectionPool::class)
+                ->getConnectionForTable('index_fulltext');
+            $connection->insert('index_fulltext', $fields);
         }
         // PROCESSING index_debug
         if ($this->indexerConfig['debugMode']) {
@@ -1515,7 +1519,9 @@ class Indexer
                 ))
             );
             if (IndexedSearchUtility::isTableUsed('index_debug')) {
-                $GLOBALS['TYPO3_DB']->exec_INSERTquery('index_debug', $fields);
+                $connection = GeneralUtility::makeInstance(ConnectionPool::class)
+                    ->getConnectionForTable('index_debug');
+                $connection->insert('index_debug', $fields);
             }
         }
     }
@@ -1538,7 +1544,9 @@ class Indexer
             'gr_list' => $this->conf['gr_list']
         );
         if (IndexedSearchUtility::isTableUsed('index_grlist')) {
-            $GLOBALS['TYPO3_DB']->exec_INSERTquery('index_grlist', $fields);
+            $connection = GeneralUtility::makeInstance(ConnectionPool::class)
+                ->getConnectionForTable('index_grlist');
+            $connection->insert('index_grlist', $fields);
         }
     }
 
@@ -1559,7 +1567,9 @@ class Indexer
         );
         $this->getRootLineFields($fields);
         if (IndexedSearchUtility::isTableUsed('index_section')) {
-            $GLOBALS['TYPO3_DB']->exec_INSERTquery('index_section', $fields);
+            $connection = GeneralUtility::makeInstance(ConnectionPool::class)
+                ->getConnectionForTable('index_section');
+            $connection->insert('index_section', $fields);
         }
     }
 
@@ -1575,12 +1585,26 @@ class Indexer
         $tableArray = explode(',', 'index_phash,index_section,index_grlist,index_fulltext,index_debug');
         foreach ($tableArray as $table) {
             if (IndexedSearchUtility::isTableUsed($table)) {
-                $GLOBALS['TYPO3_DB']->exec_DELETEquery($table, 'phash=' . (int)$phash);
+                $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+                    ->getQueryBuilderForTable($table);
+                $queryBuilder
+                    ->delete($table)
+                    ->where(
+                        $queryBuilder->expr()->eq('phash', (int)$phash)
+                    )
+                    ->execute();
             }
         }
         // Removing all index_section records with hash_t3 set to this hash (this includes such records set for external media on the page as well!). The re-insert of these records are done in indexRegularDocument($file).
         if (IndexedSearchUtility::isTableUsed('index_section')) {
-            $GLOBALS['TYPO3_DB']->exec_DELETEquery('index_section', 'phash_t3=' . (int)$phash);
+            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+                ->getQueryBuilderForTable('index_section');
+            $queryBuilder
+                ->delete('index_section')
+                ->where(
+                    $queryBuilder->expr()->eq('phash_t3', (int)$phash)
+                )
+                ->execute();
         }
     }
 
@@ -1635,7 +1659,9 @@ class Indexer
             'sys_language_uid' => (int)$this->conf['sys_language_uid']
         );
         if (IndexedSearchUtility::isTableUsed('index_phash')) {
-            $GLOBALS['TYPO3_DB']->exec_INSERTquery('index_phash', $fields);
+            $connection = GeneralUtility::makeInstance(ConnectionPool::class)
+                ->getConnectionForTable('index_phash');
+            $connection->insert('index_phash', $fields);
         }
         // PROCESSING index_fulltext
         $fields = array(
@@ -1647,7 +1673,9 @@ class Indexer
             $fields['fulltextdata'] = substr($fields['fulltextdata'], 0, $this->indexerConfig['fullTextDataLength']);
         }
         if (IndexedSearchUtility::isTableUsed('index_fulltext')) {
-            $GLOBALS['TYPO3_DB']->exec_INSERTquery('index_fulltext', $fields);
+            $connection = GeneralUtility::makeInstance(ConnectionPool::class)
+                ->getConnectionForTable('index_fulltext');
+            $connection->insert('index_fulltext', $fields);
         }
         // PROCESSING index_debug
         if ($this->indexerConfig['debugMode']) {
@@ -1661,7 +1689,9 @@ class Indexer
                 ))
             );
             if (IndexedSearchUtility::isTableUsed('index_debug')) {
-                $GLOBALS['TYPO3_DB']->exec_INSERTquery('index_debug', $fields);
+                $connection = GeneralUtility::makeInstance(ConnectionPool::class)
+                    ->getConnectionForTable('index_debug');
+                $connection->insert('index_debug', $fields);
             }
         }
     }
@@ -1712,7 +1742,14 @@ class Indexer
         $tableArray = explode(',', 'index_phash,index_grlist,index_fulltext,index_debug');
         foreach ($tableArray as $table) {
             if (IndexedSearchUtility::isTableUsed($table)) {
-                $GLOBALS['TYPO3_DB']->exec_DELETEquery($table, 'phash=' . (int)$phash);
+                $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+                    ->getQueryBuilderForTable($table);
+                $queryBuilder
+                    ->delete($table)
+                    ->where(
+                        $queryBuilder->expr()->eq('phash', (int)$phash)
+                    )
+                    ->execute();
             }
         }
     }
@@ -1982,7 +2019,9 @@ class Indexer
                             'metaphone' => $val['metaphone']
                         );
                         // A duplicate-key error will occur here if a word is NOT unset in the unset() line. However as long as the words in $wl are NOT longer as 60 chars (the baseword varchar is 60 characters...) this is not a problem.
-                        $GLOBALS['TYPO3_DB']->exec_INSERTquery('index_words', $insertFields);
+                        $connection = GeneralUtility::makeInstance(ConnectionPool::class)
+                            ->getConnectionForTable('index_words');
+                        $connection->insert('index_words', $insertFields);
                     }
                 }
             }
@@ -2001,7 +2040,14 @@ class Indexer
         if (IndexedSearchUtility::isTableUsed('index_rel')) {
             $stopWords = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('wid', 'index_words', 'is_stopword != 0', '', '', '', 'wid');
 
-            $GLOBALS['TYPO3_DB']->exec_DELETEquery('index_rel', 'phash=' . (int)$phash);
+            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+                ->getQueryBuilderForTable('index_rel');
+            $queryBuilder
+                ->delete('index_rel')
+                ->where(
+                    $queryBuilder->expr()->eq('phash', (int)$phash)
+                )
+                ->execute();
             $fields = array('phash', 'wid', 'count', 'first', 'freq', 'flags');
             $rows = array();
             foreach ($wordList as $val) {