1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-13 04:04:12 +02:00

[ticket/16741] General fixes

PHPBB3-16741
This commit is contained in:
Tristan Darricau
2021-11-09 02:48:34 +01:00
parent 300e5399f5
commit 6ce708539b
20 changed files with 941 additions and 385 deletions

View File

@@ -4,7 +4,7 @@
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
@@ -33,8 +33,8 @@ class connection_factory
*
* @return Connection Doctrine DBAL connection.
*
* @throws runtime_exception If the database connection could not be established.
* @throws InvalidArgumentException If the provided driver name is not a valid phpBB database driver.
* @throws runtime_exception If the database connection could not be established.
* @throws InvalidArgumentException If the provided driver name is not a valid phpBB database driver.
*/
public static function get_connection(config_php_file $config): Connection
{
@@ -58,17 +58,17 @@ class connection_factory
/**
* Creates a database connection from the specified parameters.
*
* @param string $driver Driver name.
* @param string $host Hostname.
* @param string|null $user Username.
* @param string|null $password Password.
* @param string|null $name Database name.
* @param string|null $port Database port.
* @param string $driver Driver name.
* @param string $host Hostname.
* @param string|null $user Username.
* @param string|null $password Password.
* @param string|null $name Database name.
* @param string|null $port Database port.
*
* @return Connection Doctrine DBAL connection.
*
* @throws runtime_exception If the database connection could not be established.
* @throws InvalidArgumentException If $driver is not a valid phpBB database driver.
* @throws runtime_exception If the database connection could not be established.
* @throws InvalidArgumentException If $driver is not a valid phpBB database driver.
*/
public static function get_connection_from_params(
string $driver,
@@ -97,13 +97,16 @@ class connection_factory
try
{
$connection = DriverManager::getConnection($params);
Type::addType(case_insensitive_string::CASE_INSENSITIVE_STRING, case_insensitive_string::class);
if (!Type::hasType(case_insensitive_string::CASE_INSENSITIVE_STRING))
{
Type::addType(case_insensitive_string::CASE_INSENSITIVE_STRING, case_insensitive_string::class);
}
$connection->getDatabasePlatform()->registerDoctrineTypeMapping('varchar_ci', case_insensitive_string::CASE_INSENSITIVE_STRING);
return $connection;
}
catch (Exception $e)
{
throw new runtime_exception('DB_CONNECTION_FAILED');
throw new runtime_exception('DB_CONNECTION_FAILED', [], $e);
}
}

View File

@@ -14,6 +14,7 @@
namespace phpbb\db\doctrine;
use InvalidArgumentException;
use phpbb\db\doctrine\oci8\driver as oci8_driver;
/**
* Helper class to generate Doctrine DBAL configuration.
@@ -152,9 +153,14 @@ class connection_parameter_factory
'oci8' => [
'charset' => 'UTF8',
'platform' => new oracle_platform(),
'driverClass' => oci8_driver::class,
],
'pdo_pgsql' => [
'charset' => 'UTF8',
'platform' => new postgresql_platform(),
],
'pdo_sqlsrv' => [
'platform' => new sqlsrv_platform(),
],
];

View File

@@ -4,7 +4,7 @@
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
@@ -27,7 +27,7 @@ class table_helper
public static function convert_column_data(array $column_data, string $dbms_layer): array
{
$options = self::resolve_dbms_specific_options($column_data, $dbms_layer);
list($type, $opts) = type_converter::convert($column_data[0]);
list($type, $opts) = type_converter::convert($column_data[0], $dbms_layer);
$options = array_merge($options, $opts);
return [$type, $options];
}
@@ -35,8 +35,8 @@ class table_helper
/**
* Resolve DBMS specific options in column data.
*
* @param array $column_data Original column data.
* @param string $dbms_layer DBMS layer name.
* @param array $column_data Original column data.
* @param string $dbms_layer DBMS layer name.
*
* @return array Doctrine column options.
*/
@@ -54,12 +54,19 @@ class table_helper
$doctrine_options['default'] = $column_data[1];
$doctrine_options['notnull'] = true;
}
else
{
$doctrine_options['notnull'] = false;
}
$non_string_pattern = '/^[a-z]*(?:int|decimal|bool|timestamp)(?::[0-9]+)?$/';
if ($dbms_layer === 'oracle' && !preg_match($non_string_pattern, strtolower($column_data[0]))
&& array_key_exists('default', $doctrine_options[0]) && $doctrine_options[0]['default'] === '')
if ($dbms_layer === 'oracle'
&& !preg_match($non_string_pattern, strtolower($column_data[0]))
&& array_key_exists('default', $doctrine_options)
&& $doctrine_options['default'] === '')
{
unset($doctrine_options['notnull']);
// Not null is true by default and Oracle does not allow empty strings in not null columns
$doctrine_options['notnull'] = false;
}
if (isset($column_data[2]))
@@ -80,8 +87,8 @@ class table_helper
/**
* Returns the DBMS specific default value for a column definition.
*
* @param array $default_options Database specific default value options.
* @param string $dbms_layer Name of the DBMS layer.
* @param array $default_options Database specific default value options.
* @param string $dbms_layer Name of the DBMS layer.
*
* @return mixed Default value for the current DBMS.
*

View File

@@ -24,6 +24,7 @@ class type_converter
* @var array
*/
private const TYPE_MAP = [
'INT' => ['integer', []],
'BINT' => ['bigint', []],
'ULINT' => ['integer', ['unsigned' => true]],
'UINT' => ['integer', ['unsigned' => true]],
@@ -41,8 +42,8 @@ class type_converter
'MTEXT' => ['text', ['length' => ((1 << 24) - 1)]],
'MTEXT_UNI' => ['text', ['length' => ((1 << 24) - 1)]],
'TIMESTAMP' => ['integer', ['unsigned' => true]],
'DECIMAL' => ['integer', ['precision' => 5, 'scale' => 2]],
'PDECIMAL' => ['integer', ['precision' => 6, 'scale' => 3]],
'DECIMAL' => ['decimal', ['precision' => 5, 'scale' => 2]],
'PDECIMAL' => ['decimal', ['precision' => 6, 'scale' => 3]],
'VCHAR_UNI' => ['string', ['length' => 255]],
'VCHAR_CI' => ['string_ci', ['length' => 255]],
'VARBINARY' => ['binary', ['length' => 255]],
@@ -55,7 +56,7 @@ class type_converter
*
* @return array<string, array> Pair of type name and options.
*/
public static function convert(string $type): array
public static function convert(string $type, string $dbms): array
{
if (strpos($type, ':') !== false)
{
@@ -63,7 +64,7 @@ class type_converter
return self::mapWithLength($typename, (int) $length);
}
return self::mapType($type);
return self::mapType($type, $dbms);
}
/**
@@ -108,13 +109,40 @@ class type_converter
*
* @return array<string, array> Pair of type name and an array of options.
*/
private static function mapType(string $type): array
private static function mapType(string $type, string $dbms): array
{
if (!in_array($type, self::TYPE_MAP, true))
if (!array_key_exists($type, self::TYPE_MAP))
{
throw new \InvalidArgumentException("Database type is undefined.");
}
// Historically, on mssql varbinary fields were stored as varchar.
// For compatibility reasons we have to keep it (because when
// querying the database, mssql does not convert strings to their
// binary representation automatically like the other dbms.
if ($type === 'VARBINARY' && $dbms === 'mssql')
{
return self::TYPE_MAP['VCHAR'];
}
// Historically, on mssql bool fields were stored as integer.
// For compatibility reasons we have to keep it because is
// some queries we are using MIN() to these columns which
// is forbidden by MSSQL for bool (bit) columns.
if ($type === 'BOOL' && $dbms === 'mssql')
{
return self::TYPE_MAP['TINT'];
}
// Historically, on postgres bool fields were stored as integer.
// For compatibility reasons we have to keep it because when
// querying the database, postgres does not convert automatically
// 0 and 1 to their boolean representation like the other dbms.
if ($type === 'BOOL' && $dbms === 'postgresql')
{
return self::TYPE_MAP['TINT'];
}
return self::TYPE_MAP[$type];
}
}

View File

@@ -72,7 +72,7 @@ class timezone extends \phpbb\db\migration\migration
foreach ($update_blocks as $timezone => $user_ids)
{
$timezone = explode(':', $timezone);
$converted_timezone = $this->convert_phpbb30_timezone($timezone[0], $timezone[1]);
$converted_timezone = static::convert_phpbb30_timezone($timezone[0], $timezone[1]);
$sql = 'UPDATE ' . $this->table_prefix . "users
SET user_timezone = '" . $this->db->sql_escape($converted_timezone) . "'
@@ -88,7 +88,7 @@ class timezone extends \phpbb\db\migration\migration
// Update board default timezone
$sql = 'UPDATE ' . $this->table_prefix . "config
SET config_value = '" . $this->convert_phpbb30_timezone($this->config['board_timezone'], $this->config['board_dst']) . "'
SET config_value = '" . static::convert_phpbb30_timezone($this->config['board_timezone'], $this->config['board_dst']) . "'
WHERE config_name = 'board_timezone'";
$this->sql_query($sql);
}
@@ -101,7 +101,7 @@ class timezone extends \phpbb\db\migration\migration
* @param $dst int Users daylight saving time
* @return string Users new php Timezone which is used since 3.1
*/
public function convert_phpbb30_timezone($timezone, $dst)
public static function convert_phpbb30_timezone($timezone, $dst)
{
$offset = (float) $timezone + (int) $dst;

File diff suppressed because it is too large Load Diff

View File

@@ -13,32 +13,18 @@
namespace phpbb\db\tools;
use Doctrine\DBAL\Connection;
/**
* A factory which serves the suitable tools instance for the given dbal
*/
class factory
{
/**
* @param mixed $db_driver
* @param bool $return_statements
* @return \phpbb\db\tools\tools_interface
* @return tools_interface
*/
public function get($db_driver, $return_statements = false)
public function get(Connection $connection, $return_statements = false)
{
// @todo: only create the doctrine tools object.
if ($db_driver instanceof \phpbb\db\driver\mssql_base)
{
return new \phpbb\db\tools\mssql($db_driver, $return_statements);
}
else if ($db_driver instanceof \phpbb\db\driver\postgres)
{
return new \phpbb\db\tools\postgres($db_driver, $return_statements);
}
else if ($db_driver instanceof \phpbb\db\driver\driver_interface)
{
return new \phpbb\db\tools\tools($db_driver, $return_statements);
}
throw new \InvalidArgumentException('Invalid database driver given');
return new doctrine($connection, $return_statements);
}
}

View File

@@ -19,6 +19,6 @@ namespace phpbb\db\tools;
*
* @deprecated 4.0.0-a1
*/
class tools extends doctrine
interface tools extends tools_interface
{
}

View File

@@ -1,15 +1,15 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\db\tools;
@@ -22,38 +22,41 @@ interface tools_interface
* Handle passed database update array.
* Expected structure...
* Key being one of the following
* drop_tables: Drop tables
* add_tables: Add tables
* change_columns: Column changes (only type, not name)
* add_columns: Add columns to a table
* drop_keys: Dropping keys
* drop_columns: Removing/Dropping columns
* add_primary_keys: adding primary keys
* add_unique_index: adding an unique index
* add_index: adding an index (can be column:index_size if you need to provide size)
* drop_tables: Drop tables
* add_tables: Add tables
* change_columns: Column changes (only type, not name)
* add_columns: Add columns to a table
* drop_keys: Dropping keys
* drop_columns: Removing/Dropping columns
* add_primary_keys: adding primary keys
* add_unique_index: adding an unique index
* add_index: adding an index (can be column:index_size if you need to provide size)
*
* The values are in this format:
* {TABLE NAME} => array(
* {COLUMN NAME} => array({COLUMN TYPE}, {DEFAULT VALUE}, {OPTIONAL VARIABLES}),
* {KEY/INDEX NAME} => array({COLUMN NAMES}),
* )
* {TABLE NAME} => array(
* {COLUMN NAME} => array({COLUMN TYPE}, {DEFAULT VALUE}, {OPTIONAL VARIABLES}),
* {KEY/INDEX NAME} => array({COLUMN NAMES}),
* )
*
*
* @param array $schema_changes
*
* @return bool|string[]
*/
public function perform_schema_changes(array $schema_changes): void;
public function perform_schema_changes(array $schema_changes);
/**
* Gets a list of tables in the database.
*
* @return array Array of table names (all lower case)
* @return array Array of table names (all lower case)
*/
public function sql_list_tables(): array;
/**
* Check if table exists
*
* @param string $table_name The table name to check for
* @param string $table_name The table name to check for
*
* @return bool True if table exists, else false
*/
public function sql_table_exists(string $table_name): bool;
@@ -61,68 +64,72 @@ interface tools_interface
/**
* Create SQL Table
*
* @param string $table_name The table name to create
* @param array $table_data Array containing table data.
* @return bool True if the statements have been executed
* @param string $table_name The table name to create
* @param array $table_data Array containing table data.
*
* @return bool|string[] True if the statements have been executed
*/
public function sql_create_table(string $table_name, array $table_data): bool;
public function sql_create_table(string $table_name, array $table_data);
/**
* Drop Table
*
* @param string $table_name The table name to drop
* @return bool True if the statements have been executed
* @param string $table_name The table name to drop
*
* @return bool|string[] True if the statements have been executed
*/
public function sql_table_drop(string $table_name): bool;
public function sql_table_drop(string $table_name);
/**
* Gets a list of columns of a table.
*
* @param string $table_name Table name
* @return array Array of column names (all lower case)
* @param string $table_name Table name
*
* @return array Array of column names (all lower case)
*/
public function sql_list_columns(string $table_name): array;
/**
* Check whether a specified column exist in a table
*
* @param string $table_name Table to check
* @param string $column_name Column to check
* @return bool True if column exists, false otherwise
* @param string $table_name Table to check
* @param string $column_name Column to check
*
* @return bool True if column exists, false otherwise
*/
public function sql_column_exists(string $table_name, string $column_name): bool;
/**
* Add new column
*
* @param string $table_name Table to modify
* @param string $column_name Name of the column to add
* @param array $column_data Column data
* @param string $table_name Table to modify
* @param string $column_name Name of the column to add
* @param array $column_data Column data
*
* @return bool True if the statements have been executed
* @return bool|string[] True if the statements have been executed
*/
public function sql_column_add(string $table_name, string $column_name, array $column_data): bool;
public function sql_column_add(string $table_name, string $column_name, array $column_data);
/**
* Change column type (not name!)
*
* @param string $table_name Table to modify
* @param string $column_name Name of the column to modify
* @param array $column_data Column data
* @param string $table_name Table to modify
* @param string $column_name Name of the column to modify
* @param array $column_data Column data
*
* @return bool True if the statements have been executed
* @return bool|string[] True if the statements have been executed
*/
public function sql_column_change(string $table_name, string $column_name, array $column_data): bool;
public function sql_column_change(string $table_name, string $column_name, array $column_data);
/**
* Drop column
*
* @param string $table_name Table to modify
* @param string $column_name Name of the column to drop
* @param string $table_name Table to modify
* @param string $column_name Name of the column to drop
*
* @return bool True if the statements have been executed
* @return bool|string[] True if the statements have been executed
*/
public function sql_column_remove(string $table_name, string $column_name): bool;
public function sql_column_remove(string $table_name, string $column_name);
/**
* List all of the indices that belong to a table
@@ -131,66 +138,73 @@ interface tools_interface
* - UNIQUE indices
* - PRIMARY keys
*
* @param string $table_name Table to check
* @return array Array with index names
* @param string $table_name Table to check
*
* @return array Array with index names
*/
public function sql_list_index(string $table_name): array;
/**
* Check if a specified index exists in table. Does not return PRIMARY KEY and UNIQUE indexes.
*
* @param string $table_name Table to check the index at
* @param string $index_name The index name to check
* @return bool True if index exists, else false
* @param string $table_name Table to check the index at
* @param string $index_name The index name to check
*
* @return bool True if index exists, else false
*/
public function sql_index_exists(string $table_name, string $index_name): bool;
/**
* Add index
*
* @param string $table_name Table to modify
* @param string $index_name Name of the index to create
* @param string|array $column Either a string with a column name, or an array with columns
* @return bool True if the statements have been executed
* @param string $table_name Table to modify
* @param string $index_name Name of the index to create
* @param string|array $column Either a string with a column name, or an array with columns
*
* @return bool|string[] True if the statements have been executed
*/
public function sql_create_index(string $table_name, string $index_name, $column): bool;
public function sql_create_index(string $table_name, string $index_name, $column);
/**
* Drop Index
*
* @param string $table_name Table to modify
* @param string $index_name Name of the index to delete
* @return bool True if the statements have been executed
* @param string $table_name Table to modify
* @param string $index_name Name of the index to delete
*
* @return bool|string[] True if the statements have been executed
*/
public function sql_index_drop(string $table_name, string $index_name): bool;
public function sql_index_drop(string $table_name, string $index_name);
/**
* Check if a specified index exists in table.
*
* NOTE: Does not return normal and PRIMARY KEY indexes
*
* @param string $table_name Table to check the index at
* @param string $index_name The index name to check
* @return bool True if index exists, else false
* @param string $table_name Table to check the index at
* @param string $index_name The index name to check
*
* @return bool|string[] True if index exists, else false
*/
public function sql_unique_index_exists(string $table_name, string $index_name): bool;
public function sql_unique_index_exists(string $table_name, string $index_name);
/**
* Add unique index
*
* @param string $table_name Table to modify
* @param string $index_name Name of the unique index to create
* @param string|array $column Either a string with a column name, or an array with columns
* @return bool True if the statements have been executed
* @param string $table_name Table to modify
* @param string $index_name Name of the unique index to create
* @param string|array $column Either a string with a column name, or an array with columns
*
* @return bool|string[] True if the statements have been executed
*/
public function sql_create_unique_index(string $table_name, string $index_name, $column): bool;
public function sql_create_unique_index(string $table_name, string $index_name, $column);
/**
* Add primary key
*
* @param string $table_name Table to modify
* @param string|array $column Either a string with a column name, or an array with columns
* @return bool True if the statements have been executed
* @param string $table_name Table to modify
* @param string|array $column Either a string with a column name, or an array with columns
*
* @return bool|string[] True if the statements have been executed
*/
public function sql_create_primary_key(string $table_name, $column): bool;
public function sql_create_primary_key(string $table_name, $column);
}