[TASK] Doctrine: Migrate exec_DELETEquery 27/49427/4
authorMorton Jonuschat <m.jonuschat@mojocode.de>
Mon, 8 Aug 2016 22:07:50 +0000 (00:07 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Tue, 9 Aug 2016 08:34:03 +0000 (10:34 +0200)
Change-Id: I32c27de6ff45d242328584746b98218a2e71b556
Resolves: #77445
Releases: master
Reviewed-on: https://review.typo3.org/49427
Tested-by: Bamboo TYPO3com <info@typo3.com>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/core/Classes/Database/RelationHandler.php
typo3/sysext/indexed_search/Classes/Hook/CrawlerHook.php
typo3/sysext/install/Classes/Updates/ProcessedFileChecksumUpdate.php

index 3827f0c..f3a713b 100644 (file)
@@ -564,6 +564,9 @@ class RelationHandler
      */
     public function writeMM($MM_tableName, $uid, $prependTableName = false)
     {
+        $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($MM_tableName);
+        $expressionBuilder = $connection->createQueryBuilder()->expr();
+
         // In case of a reverse relation
         if ($this->MM_is_foreign) {
             $uidLocal_field = 'uid_foreign';
@@ -583,31 +586,53 @@ class RelationHandler
             $c = 0;
             $additionalWhere_tablenames = '';
             if ($this->MM_is_foreign && $prep) {
-                $additionalWhere_tablenames = ' AND tablenames=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($this->currentTable, $MM_tableName);
+                $additionalWhere_tablenames = $expressionBuilder->eq(
+                    'tablenames',
+                    $expressionBuilder->literal($this->currentTable)
+                );
             }
-            $additionalWhere = '';
+            $additionalWhere = $expressionBuilder->andX();
             // Add WHERE clause if configured
             if ($this->MM_table_where) {
-                $additionalWhere .= LF . str_replace('###THIS_UID###', (int)$uid, $this->MM_table_where);
+                $additionalWhere->add(
+                    QueryHelper::stripLogicalOperatorPrefix(
+                        str_replace('###THIS_UID###', (int)$uid, $this->MM_table_where)
+                    )
+                );
             }
             // Select, update or delete only those relations that match the configured fields
             foreach ($this->MM_match_fields as $field => $value) {
-                $additionalWhere .= ' AND ' . $field . '=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($value, $MM_tableName);
+                $additionalWhere->add($expressionBuilder->eq($field, $expressionBuilder->literal($value)));
             }
-            $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
-                $uidForeign_field . ($prep ? ', tablenames' : '') . ($this->MM_hasUidField ? ', uid' : ''),
-                $MM_tableName,
-                $uidLocal_field . '=' . $uid . $additionalWhere_tablenames . $additionalWhere,
-                '',
-                $sorting_field
-            );
+
+            $queryBuilder = $connection->createQueryBuilder();
+            $queryBuilder->getRestrictions()->removeAll();
+            $queryBuilder->select($uidForeign_field)
+                ->from($MM_tableName)
+                ->where($queryBuilder->expr()->eq($uidLocal_field, (int)$uid))
+                ->orderBy($sorting_field);
+
+            if ($prep) {
+                $queryBuilder->addSelect('tablenames');
+            }
+            if ($this->MM_hasUidField) {
+                $queryBuilder->addSelect('uid');
+            }
+            if ($additionalWhere_tablenames) {
+                $queryBuilder->andWhere($additionalWhere_tablenames);
+            }
+            if ($additionalWhere->count()) {
+                $queryBuilder->andWhere($additionalWhere);
+            }
+
+            $result = $queryBuilder->execute();
             $oldMMs = array();
             // This array is similar to $oldMMs but also holds the uid of the MM-records, if any (configured by MM_hasUidField).
             // If the UID is present it will be used to update sorting and delete MM-records.
             // This is necessary if the "multiple" feature is used for the MM relations.
             // $oldMMs is still needed for the in_array() search used to look if an item from $this->itemArray is in $oldMMs
             $oldMMs_inclUid = array();
-            while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+            while ($row = $result->fetch()) {
                 if (!$this->MM_is_foreign && $prep) {
                     $oldMMs[] = array($row['tablenames'], $row[$uidForeign_field]);
                 } else {
@@ -615,7 +640,6 @@ class RelationHandler
                 }
                 $oldMMs_inclUid[] = array($row['tablenames'], $row[$uidForeign_field], $row['uid']);
             }
-            $GLOBALS['TYPO3_DB']->sql_free_result($res);
             // For each item, insert it:
             foreach ($this->itemArray as $val) {
                 $c++;
@@ -639,12 +663,27 @@ class RelationHandler
                     // In principle, selecting on the UID is all we need to do
                     // if a uid field is available since that is unique!
                     // But as long as it "doesn't hurt" we just add it to the where clause. It should all match up.
-                    $whereClause = $uidLocal_field . '=' . $uid . ' AND ' . $uidForeign_field . '=' . $val['id']
-                        . ($this->MM_hasUidField ? ' AND uid=' . (int)$oldMMs_inclUid[$oldMMs_index][2] : '');
+                    $queryBuilder = $connection->createQueryBuilder();
+                    $queryBuilder->update($MM_tableName)
+                        ->set($sorting_field, $c)
+                        ->where(
+                            $expressionBuilder->eq($uidLocal_field, $uid),
+                            $expressionBuilder->eq($uidForeign_field, $val['id'])
+                        );
+
+                    if ($additionalWhere->count()) {
+                        $queryBuilder->andWhere($additionalWhere);
+                    }
+                    if ($this->MM_hasUidField) {
+                        $queryBuilder->andWhere($expressionBuilder->eq('uid', (int)$oldMMs_inclUid[$oldMMs_index][2]));
+                    }
                     if ($tablename) {
-                        $whereClause .= ' AND tablenames=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($tablename, $MM_tableName);
+                        $queryBuilder->andWhere(
+                            $expressionBuilder->eq('tablenames', $queryBuilder->createNamedParameter($tablename))
+                        );
                     }
-                    $GLOBALS['TYPO3_DB']->exec_UPDATEquery($MM_tableName, $whereClause . $additionalWhere, array($sorting_field => $c));
+
+                    $queryBuilder->execute();
                     // Remove the item from the $oldMMs array so after this
                     // foreach loop only the ones that need to be deleted are in there.
                     unset($oldMMs[$oldMMs_index]);
@@ -660,11 +699,7 @@ class RelationHandler
                         $insertFields['tablenames'] = $tablename;
                         $insertFields = $this->completeOppositeUsageValues($tablename, $insertFields);
                     }
-                    GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($MM_tableName)
-                        ->insert(
-                            $MM_tableName,
-                            $insertFields
-                        );
+                    $connection->insert($MM_tableName, $insertFields);
                     if ($this->MM_is_foreign) {
                         $this->updateRefIndex($val['table'], $val['id']);
                     }
@@ -672,18 +707,34 @@ class RelationHandler
             }
             // Delete all not-used relations:
             if (is_array($oldMMs) && !empty($oldMMs)) {
-                $removeClauses = array();
+                $queryBuilder = $connection->createQueryBuilder();
+                $removeClauses = $queryBuilder->expr()->orX();
                 $updateRefIndex_records = array();
                 foreach ($oldMMs as $oldMM_key => $mmItem) {
                     // If UID field is present, of course we need only use that for deleting.
                     if ($this->MM_hasUidField) {
-                        $removeClauses[] = 'uid=' . (int)$oldMMs_inclUid[$oldMM_key][2];
+                        $removeClauses->add($queryBuilder->expr()->eq('uid', (int)$oldMMs_inclUid[$oldMM_key][2]));
                     } else {
                         if (is_array($mmItem)) {
-                            $removeClauses[] = 'tablenames=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($mmItem[0], $MM_tableName)
-                                . ' AND ' . $uidForeign_field . '=' . $mmItem[1];
+                            $removeClauses->add(
+                                $queryBuilder->expr()->andX(
+                                    $queryBuilder->expr()->eq(
+                                        'tablenames',
+                                        $queryBuilder->createNamedParameter($mmItem[0])
+                                    ),
+                                    $queryBuilder->expr()->eq(
+                                        $uidForeign_field,
+                                        $queryBuilder->createNamedParameter($mmItem[1])
+                                    )
+                                )
+                            );
                         } else {
-                            $removeClauses[] = $uidForeign_field . '=' . $mmItem;
+                            $removeClauses->add(
+                                $queryBuilder->expr()->eq(
+                                    $uidForeign_field,
+                                    $queryBuilder->createNamedParameter($mmItem)
+                                )
+                            );
                         }
                     }
                     if ($this->MM_is_foreign) {
@@ -694,9 +745,22 @@ class RelationHandler
                         }
                     }
                 }
-                $deleteAddWhere = ' AND (' . implode(' OR ', $removeClauses) . ')';
-                $where = $uidLocal_field . '=' . (int)$uid . $deleteAddWhere . $additionalWhere_tablenames . $additionalWhere;
-                $GLOBALS['TYPO3_DB']->exec_DELETEquery($MM_tableName, $where);
+
+                $queryBuilder->delete($MM_tableName)
+                    ->where(
+                        $queryBuilder->expr()->eq($uidLocal_field, (int)$uid),
+                        $removeClauses
+                    );
+
+                if ($additionalWhere_tablenames) {
+                    $queryBuilder->andWhere($additionalWhere_tablenames);
+                }
+                if ($additionalWhere->count()) {
+                    $queryBuilder->andWhere($additionalWhere);
+                }
+
+                $queryBuilder->execute();
+
                 // Update ref index:
                 foreach ($updateRefIndex_records as $pair) {
                     $this->updateRefIndex($pair[0], $pair[1]);
index d3334a2..7934494 100644 (file)
@@ -431,6 +431,17 @@ class CrawlerHook
      */
     public function cleanUpOldRunningConfigurations()
     {
+        $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
+        // List of tables that store information related to the phash value
+        $tablesToClean = [
+            'index_phash',
+            'index_rel',
+            'index_section',
+            'index_grlist',
+            'index_fulltext',
+            'index_debug'
+        ];
+
         // Lookup running index configurations:
         $runningIndexingConfigurations = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid,set_id', 'index_config', 'set_id<>0' . BackendUtility::deleteClause('index_config'));
         // For each running configuration, look up how many log entries there are which are scheduled for execution and if none, clear the "set_id" (means; Processing was DONE)
@@ -440,13 +451,14 @@ class CrawlerHook
             if (!$queued_items) {
                 // Lookup old phash rows:
                 $oldPhashRows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('phash', 'index_phash', 'freeIndexUid=' . (int)$cfgRec['uid'] . ' AND freeIndexSetId<>' . (int)$cfgRec['set_id']);
-                foreach ($oldPhashRows as $pHashRow) {
-                    // Removing old registrations for all tables (code copied from \TYPO3\CMS\IndexedSearch\Domain\Repository\IndexedPagesController\AdministrationRepository)
-                    $tableArr = array('index_phash', 'index_rel', 'index_section', 'index_grlist', 'index_fulltext', 'index_debug');
-                    foreach ($tableArr as $table) {
-                        $GLOBALS['TYPO3_DB']->exec_DELETEquery($table, 'phash=' . (int)$pHashRow['phash']);
-                    }
+                // Removing old registrations for all tables
+                foreach ($tablesToClean as $table) {
+                    $queryBuilder = $connectionPool->getQueryBuilderForTable($table);
+                    $queryBuilder->delete($table)
+                        ->where($queryBuilder->expr()->in('phash', array_column($oldPhashRows, 'phash')))
+                        ->execute();
                 }
+
                 // End process by updating index-config record:
                 $field_array = array(
                     'set_id' => 0,
@@ -663,11 +675,7 @@ class CrawlerHook
         // Lookup old phash rows:
         $oldPhashRows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('phash', 'index_section', 'page_id=' . (int)$id);
         if (!empty($oldPhashRows)) {
-            $pHashesToDelete = array();
-            foreach ($oldPhashRows as $pHashRow) {
-                $pHashesToDelete[] = $pHashRow['phash'];
-            }
-            $where_clause = 'phash IN (' . implode(',', array_map('intval', $pHashesToDelete)) . ')';
+            $pHashesToDelete = array_column($oldPhashRows, 'phash');
             $tables = array(
                 'index_debug',
                 'index_fulltext',
@@ -677,7 +685,11 @@ class CrawlerHook
                 'index_section',
             );
             foreach ($tables as $table) {
-                $GLOBALS['TYPO3_DB']->exec_DELETEquery($table, $where_clause);
+                $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+                    ->getQueryBuilderForTable($table);
+                $queryBuilder->delete($table)
+                    ->where($queryBuilder->expr()->in('phash', $pHashesToDelete))
+                    ->execute();
             }
         }
     }
index 1df4ae7..92b40c6 100644 (file)
@@ -65,9 +65,18 @@ This can either happen on demand, when the processed file is first needed, or by
     public function performUpdate(array &$databaseQueries, &$customMessages)
     {
         $db = $this->getDatabaseConnection();
+        $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
+        $fileConnection = $connectionPool->getConnectionByName('sys_file_processedfile');
+        $registryConnection = $connectionPool->getConnectionForTable('sys_registry');
 
         // remove all invalid records which hold NULL values
-        $db->exec_DELETEquery('sys_file_processedfile', 'width IS NULL or height IS NULL');
+        $queryBuilder = $fileConnection->createQueryBuilder();
+        $queryBuilder->delete('sys_file_processedfile')
+            ->orWhere(
+                $queryBuilder->expr()->isNull('width'),
+                $queryBuilder->expr()->isNull('height')
+            )
+            ->execute();
 
         $factory = GeneralUtility::makeInstance(ResourceFactory::class);
 
@@ -81,13 +90,13 @@ This can either happen on demand, when the processed file is first needed, or by
             }
             if (!$storage) {
                 // invalid storage, delete record, we can't take care of the associated file
-                $db->exec_DELETEquery('sys_file_processedfile', 'uid=' . $processedFileRow['uid']);
+                $fileConnection->delete('sys_file_processedfile', ['uid' => (int)$processedFileRow['uid']]);
                 continue;
             }
 
             if ($storage->getDriverType() !== 'Local') {
                 // non-local storage, we can't treat this, skip the record and mark it done
-                GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_registry')->insert(
+                $registryConnection->insert(
                     'sys_registry',
                     [
                         'entry_namespace' => 'ProcessedFileChecksumUpdate',
@@ -110,7 +119,7 @@ This can either happen on demand, when the processed file is first needed, or by
             } catch (\Exception $e) {
                 // no original file there anymore, delete local file
                 @unlink($filePath);
-                $db->exec_DELETEquery('sys_file_processedfile', 'uid=' . $processedFileRow['uid']);
+                $fileConnection->delete('sys_file_processedfile', ['uid' => (int)$processedFileRow['uid']]);
                 continue;
             }
 
@@ -139,7 +148,7 @@ This can either happen on demand, when the processed file is first needed, or by
                 // if the rename of the file failed, keep the record, but do not bother with it again
             }
 
-            GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_registry')->insert(
+            $registryConnection->insert(
                 'sys_registry',
                 [
                     'entry_namespace' => 'ProcessedFileChecksumUpdate',
@@ -148,7 +157,7 @@ This can either happen on demand, when the processed file is first needed, or by
             );
         }
 
-        $db->exec_DELETEquery('sys_registry', 'entry_namespace = \'ProcessedFileChecksumUpdate\'');
+        $registryConnection->delete('sys_registry', ['entry_namespace' => 'ProcessedFileChecksumUpdate']);
         $this->markWizardAsDone();
         return true;
     }