[!!!][TASK] Doctrine: migrate ext:impexp 67/48467/22
authorArtus Kolanowski <artus@ionoi.net>
Mon, 6 Jun 2016 16:37:11 +0000 (18:37 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Wed, 8 Jun 2016 13:16:08 +0000 (15:16 +0200)
Change the return type of `ImportExportController::exec_listQueryPid()`
to `\Doctrine\DBAL\Driver\Statement`.

Resolves: #76469
Releases: master
Change-Id: Ic7bb8dc92e452efd05e9f0458537e846e6bc7676
Reviewed-on: https://review.typo3.org/48467
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Reviewed-by: Susanne Moog <typo3@susannemoog.de>
Tested-by: Susanne Moog <typo3@susannemoog.de>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Eugene Kenah Djomo <kdeugene@yahoo.fr>
Tested-by: Eugene Kenah Djomo <kdeugene@yahoo.fr>
Reviewed-by: Joerg Boesche <typo3@joergboesche.de>
Tested-by: Joerg Boesche <typo3@joergboesche.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/core/Documentation/Changelog/master/Breaking-76469-DoctrineMigrateExtImpExp.rst [new file with mode: 0644]
typo3/sysext/impexp/Classes/Controller/ImportExportController.php
typo3/sysext/impexp/Classes/Domain/Repository/PresetRepository.php
typo3/sysext/impexp/Classes/Import.php
typo3/sysext/impexp/Classes/ImportExport.php
typo3/sysext/impexp/Classes/Task/ImportExportTask.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Breaking-76469-DoctrineMigrateExtImpExp.rst b/typo3/sysext/core/Documentation/Changelog/master/Breaking-76469-DoctrineMigrateExtImpExp.rst
new file mode 100644 (file)
index 0000000..f11ad77
--- /dev/null
@@ -0,0 +1,29 @@
+===============================================
+Breaking: #76469 - Doctrine: migrate ext:ImpExp
+===============================================
+
+Description
+===========
+
+The return type of the :php:``ImportExportController::exec_listQueryPid()``
+has been changed. Instead of returning either :php:``bool``, :php:``\mysqli_result``
+or :php:``object`` the return value always is a :php:``\Doctrine\DBAL\Driver\Statement``.
+
+
+Impact
+======
+
+Using the mentioned method will not yield the expected result type.
+
+
+Affected Installations
+======================
+
+All installations with a 3rd party extension using :php:``ImportExportController::exec_listQueryPid()``.
+
+
+Migration
+=========
+
+Migrate all calls that work with the result of :php:``ImportExportController::exec_listQueryPid()``
+to be able to handle :php:``\Doctrine\DBAL\Driver\Statement`` objects.
index a685b65..358e984 100644 (file)
@@ -21,7 +21,10 @@ use TYPO3\CMS\Backend\Template\DocumentTemplate;
 use TYPO3\CMS\Backend\Template\ModuleTemplate;
 use TYPO3\CMS\Backend\Tree\View\PageTreeView;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
-use TYPO3\CMS\Core\Database\DatabaseConnection;
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Database\Query\QueryHelper;
+use TYPO3\CMS\Core\Database\Query\Restriction\BackendWorkspaceRestriction;
+use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
 use TYPO3\CMS\Core\Imaging\Icon;
 use TYPO3\CMS\Core\Imaging\IconFactory;
 use TYPO3\CMS\Core\Messaging\FlashMessage;
@@ -404,15 +407,13 @@ class ImportExportController extends BaseScriptClass
         }
         // Configure which tables to export
         if (is_array($inData['list'])) {
-            $db = $this->getDatabaseConnection();
             foreach ($inData['list'] as $ref) {
                 $rParts = explode(':', $ref);
                 if ($beUser->check('tables_select', $rParts[0])) {
-                    $res = $this->exec_listQueryPid($rParts[0], $rParts[1], MathUtility::forceIntegerInRange($inData['listCfg']['maxNumber'], 1));
-                    while ($subTrow = $db->sql_fetch_assoc($res)) {
+                    $statement = $this->exec_listQueryPid($rParts[0], $rParts[1], MathUtility::forceIntegerInRange($inData['listCfg']['maxNumber'], 1));
+                    while ($subTrow = $statement->fetch()) {
                         $this->export->export_addRecord($rParts[0], $subTrow);
                     }
-                    $db->sql_free_result($res);
                 }
             }
         }
@@ -591,15 +592,13 @@ class ImportExportController extends BaseScriptClass
         if (!is_array($tables)) {
             return;
         }
-        $db = $this->getDatabaseConnection();
         foreach ($GLOBALS['TCA'] as $table => $value) {
             if ($table != 'pages' && (in_array($table, $tables) || in_array('_ALL', $tables))) {
                 if ($this->getBackendUser()->check('tables_select', $table) && !$GLOBALS['TCA'][$table]['ctrl']['is_static']) {
-                    $res = $this->exec_listQueryPid($table, $k, MathUtility::forceIntegerInRange($maxNumber, 1));
-                    while ($subTrow = $db->sql_fetch_assoc($res)) {
+                    $statement = $this->exec_listQueryPid($table, $k, MathUtility::forceIntegerInRange($maxNumber, 1));
+                    while ($subTrow = $statement->fetch()) {
                         $this->export->export_addRecord($table, $subTrow);
                     }
-                    $db->sql_free_result($res);
                 }
             }
         }
@@ -611,29 +610,36 @@ class ImportExportController extends BaseScriptClass
      * @param string $table Table to select from
      * @param int $pid Page ID to select from
      * @param int $limit Max number of records to select
-     * @return \mysqli_result|object Database resource
+     * @return \Doctrine\DBAL\Driver\Statement Query statement
      */
     public function exec_listQueryPid($table, $pid, $limit)
     {
-        $db = $this->getDatabaseConnection();
-        $orderBy = $GLOBALS['TCA'][$table]['ctrl']['sortby']
-            ? 'ORDER BY ' . $GLOBALS['TCA'][$table]['ctrl']['sortby']
-            : $GLOBALS['TCA'][$table]['ctrl']['default_sortby'];
-
-        $whereClause = 'pid=' . (int)$pid . BackendUtility::deleteClause($table) . BackendUtility::versioningPlaceholderClause($table);
-        if ($this->excludeDisabledRecords) {
-            $whereClause .= BackendUtility::BEenableFields($table);
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
+
+        $orderBy = $GLOBALS['TCA'][$table]['ctrl']['sortby'] ?: $GLOBALS['TCA'][$table]['ctrl']['default_sortby'];
+        $queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
+
+        if ($this->excludeDisabledRecords === false) {
+            $queryBuilder->getRestrictions()
+                ->removeAll()
+                ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
+                ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
         }
-        $res = $db->exec_SELECTquery(
-            '*',
-            $table,
-            $whereClause,
-            '',
-            $db->stripOrderBy($orderBy),
-            $limit
-        );
+
+        $queryBuilder->select('*')
+            ->from($table)
+            ->where($queryBuilder->expr()->eq('pid', (int)$pid))
+            ->setMaxResults($limit);
+
+        foreach (QueryHelper::parseOrderBy((string)$orderBy) as $orderPair) {
+            list($fieldName, $order) = $orderPair;
+            $queryBuilder->addOrderBy($fieldName, $order);
+        }
+
+        $statement = $queryBuilder->execute();
+
         // Warning about hitting limit:
-        if ($db->sql_num_rows($res) == $limit) {
+        if ($statement->rowCount() == $limit) {
             $limitWarning = sprintf($this->lang->getLL('makeconfig_anSqlQueryReturned'), $limit);
             /** @var FlashMessage $flashMessage */
             $flashMessage = GeneralUtility::makeInstance(
@@ -648,7 +654,8 @@ class ImportExportController extends BaseScriptClass
             $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
             $defaultFlashMessageQueue->enqueue($flashMessage);
         }
-        return $res;
+
+        return $statement;
     }
 
     /**
@@ -754,19 +761,7 @@ class ImportExportController extends BaseScriptClass
      */
     public function makeSaveForm($inData)
     {
-
-        // Presets:
-        $opt = array('');
-        $where = '(public>0 OR user_uid=' . (int)$this->getBackendUser()->user['uid'] . ')'
-            . ($inData['pagetree']['id'] ? ' AND (item_uid=' . (int)$inData['pagetree']['id'] . ' OR item_uid=0)' : '');
-        $presets = $this->getDatabaseConnection()->exec_SELECTgetRows('*', 'tx_impexp_presets', $where);
-        if (is_array($presets)) {
-            foreach ($presets as $presetCfg) {
-                $opt[$presetCfg['uid']] = $presetCfg['title'] . ' [' . $presetCfg['uid'] . ']'
-                    . ($presetCfg['public'] ? ' [Public]' : '')
-                    . ($presetCfg['user_uid'] === $this->getBackendUser()->user['uid'] ? ' [Own]' : '');
-            }
-        }
+        $opt = $this->presetRepository->getPresets((int)$inData['pagetree']['id']);
 
         $this->standaloneView->assign('presetSelectOptions', $opt);
 
@@ -1025,14 +1020,6 @@ class ImportExportController extends BaseScriptClass
     }
 
     /**
-     * @return DatabaseConnection
-     */
-    protected function getDatabaseConnection()
-    {
-        return $GLOBALS['TYPO3_DB'];
-    }
-
-    /**
      * @return LanguageService
      */
     protected function getLanguageService()
index bfa3b3d..dd70ddb 100644 (file)
@@ -14,7 +14,7 @@ namespace TYPO3\CMS\Impexp\Domain\Repository;
  * The TYPO3 project - inspiring people to share!
  */
 
-use TYPO3\CMS\Core\Database\DatabaseConnection;
+use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Messaging\FlashMessage;
 use TYPO3\CMS\Core\Messaging\FlashMessageQueue;
 use TYPO3\CMS\Core\Messaging\FlashMessageService;
@@ -31,16 +31,33 @@ class PresetRepository
      */
     public function getPresets($pageId)
     {
-        $options = array('');
-        $where = '(public>0 OR user_uid=' . (int)$this->getBackendUser()->user['uid'] . ')'
-            . ($pageId ? ' AND (item_uid=' . (int)$pageId . ' OR item_uid=0)' : '');
-        $presets = $this->getDatabaseConnection()->exec_SELECTgetRows('*', 'tx_impexp_presets', $where);
-        if (is_array($presets)) {
-            foreach ($presets as $presetCfg) {
-                $options[$presetCfg['uid']] = $presetCfg['title'] . ' [' . $presetCfg['uid'] . ']'
-                    . ($presetCfg['public'] ? ' [Public]' : '')
-                    . ($presetCfg['user_uid'] === $this->getBackendUser()->user['uid'] ? ' [Own]' : '');
-            }
+        $options = [''];
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getQueryBuilderForTable('tx_impexp_presets');
+
+        $queryBuilder->select('*')
+            ->from('tx_impexp_presets')
+            ->where(
+                $queryBuilder->expr()->orX(
+                    $queryBuilder->expr()->gt('public', 0),
+                    $queryBuilder->expr()->eq('user_uid', (int)$this->getBackendUser()->user['uid'])
+                )
+            );
+
+        if ($pageId) {
+            $queryBuilder->andWhere(
+                $queryBuilder->expr()->orX(
+                    $queryBuilder->expr()->eq('item_uid', (int)$pageId),
+                    $queryBuilder->expr()->eq('item_uid', 0)
+                )
+            );
+        }
+
+        $presets = $queryBuilder->execute();
+        while ($presetCfg = $presets->fetch()) {
+            $options[$presetCfg['uid']] = $presetCfg['title'] . ' [' . $presetCfg['uid'] . ']'
+                . ($presetCfg['public'] ? ' [Public]' : '')
+                . ($presetCfg['user_uid'] === $this->getBackendUser()->user['uid'] ? ' [Own]' : '');
         }
         return $options;
     }
@@ -53,7 +70,14 @@ class PresetRepository
      */
     public function getPreset($uid)
     {
-        return $this->getDatabaseConnection()->exec_SELECTgetSingleRow('*', 'tx_impexp_presets', 'uid=' . (int)$uid);
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getQueryBuilderForTable('tx_impexp_presets');
+
+        return $queryBuilder->select('*')
+            ->from('tx_impexp_presets')
+            ->where($queryBuilder->expr()->eq('uid', (int)$uid))
+            ->execute()
+            ->fetch();
     }
 
     /**
@@ -64,6 +88,7 @@ class PresetRepository
      */
     public function processPresets(&$inData)
     {
+        $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tx_impexp_presets');
         $presetData = GeneralUtility::_GP('preset');
         $err = false;
         $msg = '';
@@ -76,13 +101,17 @@ class PresetRepository
             // Update existing
             if (is_array($preset)) {
                 if ($beUser->isAdmin() || $preset['user_uid'] === $beUser->user['uid']) {
-                    $fields_values = array(
-                        'public' => $inData['preset']['public'],
-                        'title' => $inData['preset']['title'],
-                        'item_uid' => $inData['pagetree']['id'],
-                        'preset_data' => serialize($inData)
+                    $connection->update(
+                        'tx_impexp_presets',
+                        [
+                            'public' => $inData['preset']['public'],
+                            'title' => $inData['preset']['title'],
+                            'item_uid' => $inData['pagetree']['id'],
+                            'preset_data' => serialize($inData)
+                        ],
+                        ['uid' => (int)$preset['uid']]
                     );
-                    $this->getDatabaseConnection()->exec_UPDATEquery('tx_impexp_presets', 'uid=' . (int)$preset['uid'], $fields_values);
+
                     $msg = 'Preset #' . $preset['uid'] . ' saved!';
                 } else {
                     $msg = 'ERROR: The preset was not saved because you were not the owner of it!';
@@ -90,14 +119,17 @@ class PresetRepository
                 }
             } else {
                 // Insert new:
-                $fields_values = array(
-                    'user_uid' => $beUser->user['uid'],
-                    'public' => $inData['preset']['public'],
-                    'title' => $inData['preset']['title'],
-                    'item_uid' => (int)$inData['pagetree']['id'],
-                    'preset_data' => serialize($inData)
+                $connection->insert(
+                    'tx_impexp_presets',
+                    [
+                        'user_uid' => $beUser->user['uid'],
+                        'public' => $inData['preset']['public'],
+                        'title' => $inData['preset']['title'],
+                        'item_uid' => (int)$inData['pagetree']['id'],
+                        'preset_data' => serialize($inData)
+                    ]
                 );
-                $this->getDatabaseConnection()->exec_INSERTquery('tx_impexp_presets', $fields_values);
+
                 $msg = 'New preset "' . htmlspecialchars($inData['preset']['title']) . '" is created';
             }
         }
@@ -107,7 +139,11 @@ class PresetRepository
             if (is_array($preset)) {
                 // Update existing
                 if ($beUser->isAdmin() || $preset['user_uid'] === $beUser->user['uid']) {
-                    $this->getDatabaseConnection()->exec_DELETEquery('tx_impexp_presets', 'uid=' . (int)$preset['uid']);
+                    $connection->delete(
+                        'tx_impexp_presets',
+                        ['uid' => (int)$preset['uid']]
+                    );
+
                     $msg = 'Preset #' . $preset['uid'] . ' deleted!';
                 } else {
                     $msg = 'ERROR: You were not the owner of the preset so you could not delete it.';
@@ -171,12 +207,4 @@ class PresetRepository
     {
         return $GLOBALS['BE_USER'];
     }
-
-    /**
-     * @return DatabaseConnection
-     */
-    protected function getDatabaseConnection()
-    {
-        return $GLOBALS['TYPO3_DB'];
-    }
 }
index ce53dd4..1200a1e 100644 (file)
@@ -15,6 +15,7 @@ namespace TYPO3\CMS\Impexp;
  */
 
 use TYPO3\CMS\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\DataHandling\DataHandler;
 use TYPO3\CMS\Core\Exception;
 use TYPO3\CMS\Core\Resource\File;
@@ -537,20 +538,13 @@ class Import extends ImportExport
      */
     protected function fetchStorageRecords()
     {
-        $whereClause = BackendUtility::BEenableFields('sys_file_storage');
-        $whereClause .= BackendUtility::deleteClause('sys_file_storage');
-
-        $rows = $this->getDatabaseConnection()->exec_SELECTgetRows(
-            '*',
-            'sys_file_storage',
-            '1=1' . $whereClause,
-            '',
-            '',
-            '',
-            'uid'
-        );
-
-        return $rows;
+        return GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getQueryBuilderForTable('sys_file_storage')
+            ->select('*')
+            ->from('sys_file_storage')
+            ->orderBy('uid')
+            ->execute()
+            ->fetchAll();
     }
 
     /**
@@ -837,11 +831,17 @@ class Import extends ImportExport
             } elseif ($table === 'sys_file_metadata' && $record['sys_language_uid'] == '0' && $this->import_mapId['sys_file'][$record['file']]) {
                 // on adding sys_file records the belonging sys_file_metadata record was also created
                 // if there is one the record need to be overwritten instead of creating a new one.
-                $recordInDatabase = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
-                    'uid',
-                    'sys_file_metadata',
-                    'file = ' . $this->import_mapId['sys_file'][$record['file']] . ' AND sys_language_uid = 0 AND pid = 0'
-                );
+                $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+                    ->getQueryBuilderForTable('sys_file_metadata');
+                $recordInDatabase = $queryBuilder->select('uid')
+                    ->from('sys_file_metadata')
+                    ->where(
+                        $queryBuilder->expr()->eq('file', (int)$this->import_mapId['sys_file'][$record['file']]),
+                        $queryBuilder->expr()->eq('sys_language_uid', 0),
+                        $queryBuilder->expr()->eq('pid', 0)
+                    )
+                    ->execute()
+                    ->fetch();
                 // if no record could be found, $this->import_mapId['sys_file'][$record['file']] is pointing
                 // to a file, that was already there, thus a new metadata record should be created
                 if (is_array($recordInDatabase)) {
index bcfa8f2..f4fc27f 100644 (file)
@@ -16,7 +16,6 @@ namespace TYPO3\CMS\Impexp;
 
 use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
-use TYPO3\CMS\Core\Database\DatabaseConnection;
 use TYPO3\CMS\Core\Imaging\Icon;
 use TYPO3\CMS\Core\Imaging\IconFactory;
 use TYPO3\CMS\Core\Utility\DebugUtility;
@@ -1286,14 +1285,6 @@ abstract class ImportExport
     }
 
     /**
-     * @return DatabaseConnection
-     */
-    protected function getDatabaseConnection()
-    {
-        return $GLOBALS['TYPO3_DB'];
-    }
-
-    /**
      * @return LanguageService
      */
     protected function getLanguageService()
index ab743f3..95229cf 100644 (file)
@@ -15,6 +15,7 @@ namespace TYPO3\CMS\Impexp\Task;
  */
 
 use TYPO3\CMS\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Messaging\FlashMessage;
 use TYPO3\CMS\Core\Messaging\FlashMessageService;
 use TYPO3\CMS\Core\Resource\Exception;
@@ -177,19 +178,25 @@ class ImportExportTask implements TaskInterface
     /**
      * Select presets for this user
      *
-     * @return array Array of preset records
+     * @return array|bool Array of preset records
      */
     protected function getPresets()
     {
-        $db = $this->getDatabase();
-        $presets = $db->exec_SELECTgetRows(
-            '*',
-            'tx_impexp_presets',
-            '(public > 0 OR user_uid=' . $this->getBackendUser()->user['uid'] . ')',
-            '',
-            'item_uid DESC, title'
-        );
-        return $presets;
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getQueryBuilderForTable('tx_impexp_presets');
+
+        return $queryBuilder->select('*')
+            ->from('tx_impexp_presets')
+            ->where(
+                $queryBuilder->expr()->orX(
+                    $queryBuilder->expr()->gt('public', 0),
+                    $queryBuilder->expr()->eq('user_uid', (int)$this->getBackendUser()->user['uid'])
+                )
+            )
+            ->orderBy('item_uid', 'DESC')
+            ->addOrderBy('title')
+            ->execute()
+            ->fetchAll();
     }
 
     /**