diff --git a/Classes/Database/DatabaseConnection.php b/Classes/Database/DatabaseConnection.php index b1178b6..715fde7 100644 --- a/Classes/Database/DatabaseConnection.php +++ b/Classes/Database/DatabaseConnection.php @@ -13,7 +13,9 @@ * * The TYPO3 project - inspiring people to share! */ - +use Doctrine\DBAL\DriverManager; +use Doctrine\DBAL\Exception\ConnectionException; +use TYPO3\CMS\Core\Utility\DebugUtility; use TYPO3\CMS\Core\Core\Environment; use TYPO3\CMS\Core\Database\Connection; use TYPO3\CMS\Core\Database\Query\QueryHelper; @@ -57,14 +59,14 @@ class DatabaseConnection * * @var string */ - const AND_Constraint = 'AND'; + public const AND_Constraint = 'AND'; /** * The OR constraint in where clause * * @var string */ - const OR_Constraint = 'OR'; + public const OR_Constraint = 'OR'; /** * Set "TRUE" or "1" if you want database errors outputted. Set to "2" if you also want successful database actions outputted. @@ -445,7 +447,7 @@ public function exec_SELECTcountRows($field, $table, $where = '1=1') $count = false; $resultSet = $this->exec_SELECTquery('COUNT(' . $field . ')', $table, $where); if ($resultSet !== false) { - list($count) = $this->sql_fetch_row($resultSet); + [$count] = $this->sql_fetch_row($resultSet); $count = (int)$count; $this->sql_free_result($resultSet); } @@ -749,7 +751,7 @@ public function listQuery($field, $value, $table) { $this->logDeprecation(); $value = (string)$value; - if (strpos($value, ',') !== false) { + if (str_contains($value, ',')) { throw new \InvalidArgumentException('$value must not contain a comma (,) in $this->listQuery() !', 1294585862); } $pattern = $this->quoteStr($value, $table); @@ -769,13 +771,10 @@ public function listQuery($field, $value, $table) public function searchQuery($searchWords, $fields, $table, $constraint = self::AND_Constraint) { $this->logDeprecation(); - switch ($constraint) { - case self::OR_Constraint: - $constraint = 'OR'; - break; - default: - $constraint = 'AND'; - } + $constraint = match ($constraint) { + self::OR_Constraint => 'OR', + default => 'AND', + }; $queryParts = []; foreach ($searchWords as $sw) { @@ -802,14 +801,14 @@ public function searchQuery($searchWords, $fields, $table, $constraint = self::A * @param string $orderBy See exec_SELECTquery() * @param string $limit See exec_SELECTquery() * @param array $input_parameters An array of values with as many elements as there are bound parameters in the SQL statement being executed. All values are treated as \TYPO3\CMS\Typo3DbLegacy\Database\PreparedStatement::PARAM_AUTOTYPE. - * @return \TYPO3\CMS\Typo3DbLegacy\Database\PreparedStatement Prepared statement + * @return PreparedStatement Prepared statement */ public function prepare_SELECTquery($select_fields, $from_table, $where_clause, $groupBy = '', $orderBy = '', $limit = '', array $input_parameters = []) { $this->logDeprecation(); $query = $this->SELECTquery($select_fields, $from_table, $where_clause, $groupBy, $orderBy, $limit); /** @var $preparedStatement \TYPO3\CMS\Typo3DbLegacy\Database\PreparedStatement */ - $preparedStatement = GeneralUtility::makeInstance(\TYPO3\CMS\Typo3DbLegacy\Database\PreparedStatement::class, $query, $from_table, []); + $preparedStatement = GeneralUtility::makeInstance(PreparedStatement::class, $query, $from_table, []); // Bind values to parameters foreach ($input_parameters as $key => $value) { $preparedStatement->bindValue($key, $value, PreparedStatement::PARAM_AUTOTYPE); @@ -823,7 +822,7 @@ public function prepare_SELECTquery($select_fields, $from_table, $where_clause, * * @param array $queryParts Query parts array * @param array $input_parameters An array of values with as many elements as there are bound parameters in the SQL statement being executed. All values are treated as \TYPO3\CMS\Typo3DbLegacy\Database\PreparedStatement::PARAM_AUTOTYPE. - * @return \TYPO3\CMS\Typo3DbLegacy\Database\PreparedStatement Prepared statement + * @return PreparedStatement Prepared statement */ public function prepare_SELECTqueryArray(array $queryParts, array $input_parameters = []) { @@ -1289,7 +1288,7 @@ public function sql_pconnect() // We are not using the TYPO3 CMS shim here as the database parameters in this class // are settable externally. This requires building the connection parameter array // just in time when establishing the connection. - $connection = \Doctrine\DBAL\DriverManager::getConnection([ + $connection = DriverManager::getConnection([ 'driver' => 'mysqli', 'wrapperClass' => Connection::class, 'host' => $host, @@ -1305,7 +1304,7 @@ public function sql_pconnect() /** @var \Doctrine\DBAL\Driver\Mysqli\MysqliConnection $mysqliConnection */ $mysqliConnection = $connection->getWrappedConnection(); $this->link = $mysqliConnection->getWrappedResourceHandle(); - } catch (\Doctrine\DBAL\Exception\ConnectionException $exception) { + } catch (ConnectionException) { return false; } @@ -1381,7 +1380,7 @@ public function admin_get_dbs() if ($this->sql_select_db()) { $dbArr[] = $row->SCHEMA_NAME; } - } catch (\RuntimeException $exception) { + } catch (\RuntimeException) { // The exception happens if we cannot connect to the database // (usually due to missing permissions). This is ok here. // We catch the exception, skip the database and continue. @@ -1836,17 +1835,14 @@ public function debug($func, $query = '') $this->logDeprecation(); $error = $this->sql_error(); if ($error || (int)$this->debugOutput === 2) { - \TYPO3\CMS\Core\Utility\DebugUtility::debug( + DebugUtility::debug( [ 'caller' => \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection::class . '::' . $func, 'ERROR' => $error, - 'lastBuiltQuery' => $query ? $query : $this->debug_lastBuiltQuery, - 'debug_backtrace' => \TYPO3\CMS\Core\Utility\DebugUtility::debugTrail() + 'lastBuiltQuery' => $query ?: $this->debug_lastBuiltQuery, + 'debug_backtrace' => DebugUtility::debugTrail() ], - $func, - is_object($GLOBALS['error']) && @is_callable([$GLOBALS['error'], 'debug']) - ? '' - : 'DB Error' + $func ); } } @@ -1914,10 +1910,10 @@ protected function explain($query, $from_table, $row_count) return false; } $error = $this->sql_error(); - $trail = \TYPO3\CMS\Core\Utility\DebugUtility::debugTrail(); + $trail = DebugUtility::debugTrail(); $explain_tables = []; $explain_output = []; - $res = $this->sql_query('EXPLAIN ' . $query, $this->link); + $res = $this->sql_query('EXPLAIN ' . $query); if (is_a($res, '\\mysqli_result')) { while ($tempRow = $this->sql_fetch_assoc($res)) { $explain_output[] = $tempRow; @@ -1934,7 +1930,7 @@ protected function explain($query, $from_table, $row_count) $tableRes = $this->sql_query('SHOW TABLE STATUS LIKE \'' . $table . '\''); $isTable = $this->sql_num_rows($tableRes); if ($isTable) { - $res = $this->sql_query('SHOW INDEX FROM ' . $table, $this->link); + $res = $this->sql_query('SHOW INDEX FROM ' . $table); if (is_a($res, '\\mysqli_result')) { while ($tempRow = $this->sql_fetch_assoc($res)) { $indices_output[] = $tempRow; @@ -1963,7 +1959,7 @@ protected function explain($query, $from_table, $row_count) $data['indices'] = $indices_output; } if ($explainMode == 1) { - \TYPO3\CMS\Core\Utility\DebugUtility::debug($data, 'Tables: ' . $from_table, 'DB SQL EXPLAIN'); + DebugUtility::debug($data, 'Tables: ' . $from_table); } elseif ($explainMode == 2) { /** @var TimeTracker $timeTracker */ $timeTracker = GeneralUtility::makeInstance(TimeTracker::class); @@ -2021,7 +2017,7 @@ protected function logError($message) GeneralUtility::SYSLOG_SEVERITY_ERROR ); } else { - GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__)->error($message); + GeneralUtility::makeInstance(LogManager::class)->getLogger(self::class)->error($message); } } @@ -2034,7 +2030,7 @@ protected function logFatalError($message) GeneralUtility::SYSLOG_SEVERITY_FATAL ); } else { - GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__)->emergency($message); + GeneralUtility::makeInstance(LogManager::class)->getLogger(self::class)->emergency($message); } } } diff --git a/Classes/Database/PostProcessQueryHookInterface.php b/Classes/Database/PostProcessQueryHookInterface.php index f6e7a1f..996eb3a 100644 --- a/Classes/Database/PostProcessQueryHookInterface.php +++ b/Classes/Database/PostProcessQueryHookInterface.php @@ -29,9 +29,8 @@ interface PostProcessQueryHookInterface * @param string $groupBy Group by statement * @param string $orderBy Order by statement * @param int $limit Database return limit - * @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject */ - public function exec_SELECTquery_postProcessAction(&$select_fields, &$from_table, &$where_clause, &$groupBy, &$orderBy, &$limit, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject); + public function exec_SELECTquery_postProcessAction(&$select_fields, &$from_table, &$where_clause, &$groupBy, &$orderBy, &$limit, DatabaseConnection $parentObject); /** * Post-processor for the exec_INSERTquery method. @@ -39,9 +38,8 @@ public function exec_SELECTquery_postProcessAction(&$select_fields, &$from_table * @param string $table Database table name * @param array $fieldsValues Field values as key => value pairs * @param string|array $noQuoteFields List/array of keys NOT to quote - * @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject */ - public function exec_INSERTquery_postProcessAction(&$table, array &$fieldsValues, &$noQuoteFields, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject); + public function exec_INSERTquery_postProcessAction(&$table, array &$fieldsValues, &$noQuoteFields, DatabaseConnection $parentObject); /** * Post-processor for the exec_INSERTmultipleRows method. @@ -50,9 +48,8 @@ public function exec_INSERTquery_postProcessAction(&$table, array &$fieldsValues * @param array $fields Field names * @param array $rows Table rows * @param string|array $noQuoteFields List/array of keys NOT to quote - * @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject */ - public function exec_INSERTmultipleRows_postProcessAction(&$table, array &$fields, array &$rows, &$noQuoteFields, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject); + public function exec_INSERTmultipleRows_postProcessAction(&$table, array &$fields, array &$rows, &$noQuoteFields, DatabaseConnection $parentObject); /** * Post-processor for the exec_UPDATEquery method. @@ -61,24 +58,21 @@ public function exec_INSERTmultipleRows_postProcessAction(&$table, array &$field * @param string $where WHERE clause * @param array $fieldsValues Field values as key => value pairs * @param string|array $noQuoteFields List/array of keys NOT to quote - * @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject */ - public function exec_UPDATEquery_postProcessAction(&$table, &$where, array &$fieldsValues, &$noQuoteFields, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject); + public function exec_UPDATEquery_postProcessAction(&$table, &$where, array &$fieldsValues, &$noQuoteFields, DatabaseConnection $parentObject); /** * Post-processor for the exec_DELETEquery method. * * @param string $table Database table name * @param string $where WHERE clause - * @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject */ - public function exec_DELETEquery_postProcessAction(&$table, &$where, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject); + public function exec_DELETEquery_postProcessAction(&$table, &$where, DatabaseConnection $parentObject); /** * Post-processor for the exec_TRUNCATEquery method. * * @param string $table Database table name - * @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject */ - public function exec_TRUNCATEquery_postProcessAction(&$table, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject); + public function exec_TRUNCATEquery_postProcessAction(&$table, DatabaseConnection $parentObject); } diff --git a/Classes/Database/PreProcessQueryHookInterface.php b/Classes/Database/PreProcessQueryHookInterface.php index 15fc383..a23d987 100644 --- a/Classes/Database/PreProcessQueryHookInterface.php +++ b/Classes/Database/PreProcessQueryHookInterface.php @@ -29,9 +29,8 @@ interface PreProcessQueryHookInterface * @param string $groupBy Group by statement * @param string $orderBy Order by statement * @param int $limit Database return limit - * @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject */ - public function SELECTquery_preProcessAction(&$select_fields, &$from_table, &$where_clause, &$groupBy, &$orderBy, &$limit, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject); + public function SELECTquery_preProcessAction(&$select_fields, &$from_table, &$where_clause, &$groupBy, &$orderBy, &$limit, DatabaseConnection $parentObject); /** * Pre-processor for the INSERTquery method. @@ -39,9 +38,8 @@ public function SELECTquery_preProcessAction(&$select_fields, &$from_table, &$wh * @param string $table Database table name * @param array $fieldsValues Field values as key => value pairs * @param string|array $noQuoteFields List/array of keys NOT to quote - * @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject */ - public function INSERTquery_preProcessAction(&$table, array &$fieldsValues, &$noQuoteFields, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject); + public function INSERTquery_preProcessAction(&$table, array &$fieldsValues, &$noQuoteFields, DatabaseConnection $parentObject); /** * Pre-processor for the INSERTmultipleRows method. @@ -52,9 +50,8 @@ public function INSERTquery_preProcessAction(&$table, array &$fieldsValues, &$no * @param array $fields Field names * @param array $rows Table rows * @param string|array $noQuoteFields List/array of keys NOT to quote - * @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject */ - public function INSERTmultipleRows_preProcessAction(&$table, array &$fields, array &$rows, &$noQuoteFields, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject); + public function INSERTmultipleRows_preProcessAction(&$table, array &$fields, array &$rows, &$noQuoteFields, DatabaseConnection $parentObject); /** * Pre-processor for the UPDATEquery method. @@ -63,24 +60,21 @@ public function INSERTmultipleRows_preProcessAction(&$table, array &$fields, arr * @param string $where WHERE clause * @param array $fieldsValues Field values as key => value pairs * @param string|array $noQuoteFields List/array of keys NOT to quote - * @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject */ - public function UPDATEquery_preProcessAction(&$table, &$where, array &$fieldsValues, &$noQuoteFields, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject); + public function UPDATEquery_preProcessAction(&$table, &$where, array &$fieldsValues, &$noQuoteFields, DatabaseConnection $parentObject); /** * Pre-processor for the DELETEquery method. * * @param string $table Database table name * @param string $where WHERE clause - * @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject */ - public function DELETEquery_preProcessAction(&$table, &$where, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject); + public function DELETEquery_preProcessAction(&$table, &$where, DatabaseConnection $parentObject); /** * Pre-processor for the TRUNCATEquery method. * * @param string $table Database table name - * @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject */ - public function TRUNCATEquery_preProcessAction(&$table, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject); + public function TRUNCATEquery_preProcessAction(&$table, DatabaseConnection $parentObject); } diff --git a/Classes/Database/PreparedStatement.php b/Classes/Database/PreparedStatement.php index 52dd6ff..86e3b59 100644 --- a/Classes/Database/PreparedStatement.php +++ b/Classes/Database/PreparedStatement.php @@ -42,35 +42,35 @@ class PreparedStatement * * @var int */ - const PARAM_NULL = 0; + public const PARAM_NULL = 0; /** * Represents the SQL INTEGER data type. * * @var int */ - const PARAM_INT = 1; + public const PARAM_INT = 1; /** * Represents the SQL CHAR, VARCHAR, or other string data type. * * @var int */ - const PARAM_STR = 2; + public const PARAM_STR = 2; /** * Represents a boolean data type. * * @var int */ - const PARAM_BOOL = 3; + public const PARAM_BOOL = 3; /** * Automatically detects underlying type * * @var int */ - const PARAM_AUTOTYPE = 4; + public const PARAM_AUTOTYPE = 4; /** * Specifies that the fetch method shall return each row as an array indexed by @@ -80,7 +80,7 @@ class PreparedStatement * * @var int */ - const FETCH_ASSOC = 2; + public const FETCH_ASSOC = 2; /** * Specifies that the fetch method shall return each row as an array indexed by @@ -88,14 +88,7 @@ class PreparedStatement * * @var int */ - const FETCH_NUM = 3; - - /** - * Query to be executed. - * - * @var string - */ - protected $query; + public const FETCH_NUM = 3; /** * Components of the query to be executed. @@ -104,13 +97,6 @@ class PreparedStatement */ protected $precompiledQueryParts; - /** - * Table (used to call $GLOBALS['TYPO3_DB']->fullQuoteStr(). - * - * @var string - */ - protected $table; - /** * Binding parameters. * @@ -165,20 +151,24 @@ class PreparedStatement * @access private * @deprecated since TYPO3 v8, will be removed in TYPO3 v9, use Doctrine DBAL as it does PreparedStatements built-in */ - public function __construct($query, $table, array $precompiledQueryParts = []) + public function __construct(/** + * Query to be executed. + */ + protected $query, /** + * Table (used to call $GLOBALS['TYPO3_DB']->fullQuoteStr(). + */ + protected $table, array $precompiledQueryParts = []) { if (method_exists('GeneralUtility', 'logDeprecatedFunction')) { GeneralUtility::logDeprecatedFunction(); } else { trigger_error('PreparedStatement from EXT:typo3db_legacy is marked as deprecated, use Doctrine DBAL as it does PreparedStatements built-in', E_USER_DEPRECATED); } - $this->query = $query; $this->precompiledQueryParts = $precompiledQueryParts; - $this->table = $table; $this->parameters = []; // Test if named placeholders are used - if ($this->hasNamedPlaceholders($query) || !empty($precompiledQueryParts)) { + if ($this->hasNamedPlaceholders($this->query) || !empty($precompiledQueryParts)) { $this->statement = null; } else { // Only question mark placeholders are used @@ -241,7 +231,7 @@ public function bindValues(array $values) * @return \TYPO3\CMS\Typo3DbLegacy\Database\PreparedStatement The current prepared statement to allow method chaining * @api */ - public function bindValue($parameter, $value, $data_type = self::PARAM_AUTOTYPE) + public function bindValue(mixed $parameter, mixed $value, $data_type = self::PARAM_AUTOTYPE) { switch ($data_type) { case self::PARAM_INT: @@ -556,23 +546,18 @@ public function errorInfo() */ public function setFetchMode($mode) { - switch ($mode) { - case self::FETCH_ASSOC: - case self::FETCH_NUM: - $this->defaultFetchMode = $mode; - break; - default: - throw new \InvalidArgumentException('$mode must be either TYPO3\\CMS\\Typo3DbLegacy\\Database\\PreparedStatement::FETCH_ASSOC or TYPO3\\CMS\\Typo3DbLegacy\\Database\\PreparedStatement::FETCH_NUM', 1281875340); - } + $this->defaultFetchMode = match ($mode) { + self::FETCH_ASSOC, self::FETCH_NUM => $mode, + default => throw new \InvalidArgumentException('$mode must be either TYPO3\\CMS\\Typo3DbLegacy\\Database\\PreparedStatement::FETCH_ASSOC or TYPO3\\CMS\\Typo3DbLegacy\\Database\\PreparedStatement::FETCH_NUM', 1281875340), + }; } /** * Guesses the type of a given value. * - * @param mixed $value * @return int One of the \TYPO3\CMS\Typo3DbLegacy\Database\PreparedStatement::PARAM_* constants */ - protected function guessValueType($value) + protected function guessValueType(mixed $value) { if (is_bool($value)) { $type = self::PARAM_BOOL; @@ -602,8 +587,6 @@ protected function hasNamedPlaceholders($query) * Converts named placeholders into question mark placeholders in a query. * * @param string $query - * @param array $parameterValues - * @param array $precompiledQueryParts */ protected function convertNamedPlaceholdersToQuestionMarks(&$query, array &$parameterValues, array &$precompiledQueryParts) { @@ -656,7 +639,6 @@ protected function convertNamedPlaceholdersToQuestionMarks(&$query, array &$para * Replace the markers with unpredictable token markers. * * @param string $query - * @param array $parameterValues * @return string * @throws \InvalidArgumentException */ diff --git a/composer.json b/composer.json index 4ee5b58..5cdf38e 100644 --- a/composer.json +++ b/composer.json @@ -8,9 +8,10 @@ "issues": "https://github.com/FriendsOfTYPO3/typo3db_legacy/issues", "source": "https://github.com/FriendsOfTYPO3/typo3db_legacy" }, - "license": ["GPL-2.0+"], + "license": ["GPL-2.0-or-later"], "require": { + "php": "^8.0", "typo3/cms-core": "^10.0 || ^11.0 || ^12.0" }, "extra": { diff --git a/ext_emconf.php b/ext_emconf.php index 31764c7..2a6aa4d 100644 --- a/ext_emconf.php +++ b/ext_emconf.php @@ -13,6 +13,7 @@ 'version' => '1.2.0', 'constraints' => [ 'depends' => [ + 'php' => '8.0.0-8.4.99', 'typo3' => '10.4.0-12.4.99', 'backend' => '10.4.0-12.4.99', ], diff --git a/ext_localconf.php b/ext_localconf.php index 8c5f6c4..8dc36d9 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -26,7 +26,7 @@ $databaseConnection->setDatabasePort($GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']['port']); } elseif (strpos($databaseHost, ':') > 0) { // @TODO: Find a way to handle this case in the install tool and drop this - list($databaseHost, $databasePort) = explode(':', $databaseHost); + [$databaseHost, $databasePort] = explode(':', $databaseHost); $databaseConnection->setDatabasePort($databasePort); } if (isset($GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']['unix_socket'])) {