Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the installer/upgrader #7849

Merged
merged 11 commits into from
Nov 27, 2023
2 changes: 1 addition & 1 deletion Sources/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
}

// Do any third-party scripts want in on the fun?
if (class_exists('SMF\\Config', false) && $hook_value !== (Config::$modSettings['integrate_autoload'] ?? '')) {
if (!defined('SMF_INSTALLING') && class_exists('SMF\\Config', false) && $hook_value !== (Config::$modSettings['integrate_autoload'] ?? '')) {
if (!class_exists('SMF\\IntegrationHook', false) && is_file($sourcedir . '/IntegrationHook.php')) {
require_once $sourcedir . '/IntegrationHook.php';
}
Expand Down
14 changes: 10 additions & 4 deletions Sources/Db/APIs/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -1984,18 +1984,24 @@ protected function __construct(array $options = [])
$this->prefixReservedTables();
}

$this->get_version();
$this->supports_pcre = version_compare($this->version, strpos($this->version, 'MariaDB') !== false ? '10.0.5' : '8.0.4', '>=');

// For backward compatibility.
if (!is_object(self::$db_connection)) {
self::$db_connection = $this->connection;
}

// At this point, if we don't have a connection, nothing else can be done.
if (empty($this->connection)) {
return;
}

jdarwood007 marked this conversation as resolved.
Show resolved Hide resolved
$this->get_version();
$this->supports_pcre = version_compare($this->version, strpos($this->version, 'MariaDB') !== false ? '10.0.5' : '8.0.4', '>=');

// Ensure database has UTF-8 as its default input charset.
$this->query(
'',
'SET NAMES {string:db_character_set}',
'
SET NAMES {string:db_character_set}',
jdarwood007 marked this conversation as resolved.
Show resolved Hide resolved
[
'db_character_set' => $this->character_set,
],
Expand Down
5 changes: 5 additions & 0 deletions Sources/Db/APIs/PostgreSQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -2117,6 +2117,11 @@ protected function __construct(array $options = [])
self::$db_connection = $this->connection;
}

// At this point, if we don't have a connection, nothing else can be done.
if (empty($this->connection)) {
return;
}

jdarwood007 marked this conversation as resolved.
Show resolved Hide resolved
// Ensure database has UTF-8 as its default input charset.
$this->query(
'',
Expand Down
41 changes: 24 additions & 17 deletions Sources/Db/DatabaseApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,27 @@ final public static function load(array $options = [])
}

// Figure out what type of database we are using.
switch (!empty(Config::$db_type) ? strtolower(Config::$db_type) : 'mysql') {
$class = self::getClass(!empty(Config::$db_type) ? strtolower(Config::$db_type) : 'mysql');

if (!class_exists(__NAMESPACE__ . '\\APIs\\' . $class)) {
ErrorHandler::displayDbError();
}

$class = __NAMESPACE__ . '\\APIs\\' . $class;
self::$db = new $class($options);

// Double check that we found what we expected.
if (!(self::$db instanceof DatabaseApi)) {
unset(self::$db);
ErrorHandler::displayDbError();
}

return self::$db;
}

public static function getClass($db_type)
{
switch (strtolower($db_type)) {
// PostgreSQL is known by many names.
case 'postgresql':
case 'postgres':
Expand All @@ -335,14 +355,14 @@ final public static function load(array $options = [])
$class = POSTGRE_TITLE;
break;

// MySQL and its forks.
// MySQL and its forks.
case 'mysql':
case 'mariadb':
case 'percona':
$class = MYSQL_TITLE;
break;

// Something else?
// Something else?
default:
$class = ucwords(Config::$db_type);

Expand All @@ -354,20 +374,7 @@ final public static function load(array $options = [])
break;
}

if (!class_exists(__NAMESPACE__ . '\\APIs\\' . $class)) {
ErrorHandler::displayDbError();
}

$class = __NAMESPACE__ . '\\APIs\\' . $class;
self::$db = new $class($options);

// Double check that we found what we expected.
if (!(self::$db instanceof DatabaseApi)) {
unset(self::$db);
ErrorHandler::displayDbError();
}

return self::$db;
return $class;
}

/**
Expand Down
14 changes: 7 additions & 7 deletions other/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ function initialize_inputs()
foreach ($databases as $key => $dummy)
{
$type = ($key == 'mysqli') ? 'mysql' : $key;
$ftp->unlink('install_' . DB_SCRIPT_VERSION . '_' . $type . '.sql');
$ftp->unlink('install_' . DB_SCRIPT_VERSION . '_' . Db::getClass($type) . '.sql');
}

$ftp->close();
Expand All @@ -268,7 +268,7 @@ function initialize_inputs()
foreach ($databases as $key => $dummy)
{
$type = ($key == 'mysqli') ? 'mysql' : $key;
@unlink(Config::$boarddir . '/install_' . DB_SCRIPT_VERSION . '_' . $type . '.sql');
@unlink(Config::$boarddir . '/install_' . DB_SCRIPT_VERSION . '_' . Db::getClass($type) . '.sql');
}
}

Expand Down Expand Up @@ -479,11 +479,11 @@ function Welcome()
if ($db['supported'])
{
$type = ($key == 'mysqli') ? 'mysql' : $key;
if (!file_exists(Config::$boarddir . '/install_' . DB_SCRIPT_VERSION . '_' . $type . '.sql'))
if (!file_exists(Config::$boarddir . '/install_' . DB_SCRIPT_VERSION . '_' . Db::getClass($type) . '.sql'))
{
$databases[$key]['supported'] = false;
$notFoundSQLFile = true;
Lang::$txt['error_db_script_missing'] = sprintf(Lang::$txt['error_db_script_missing'], 'install_' . DB_SCRIPT_VERSION . '_' . $type . '.sql');
Lang::$txt['error_db_script_missing'] = sprintf(Lang::$txt['error_db_script_missing'], 'install_' . DB_SCRIPT_VERSION . '_' . Db::getClass($type) . '.sql');
}
else
$incontext['supported_databases'][] = $db;
Expand Down Expand Up @@ -868,9 +868,9 @@ function DatabaseSettings()
Config::load();

// Better find the database file!
if (!file_exists(Config::$sourcedir . '/Db/APIs/' . Config::$db_type . '.php'))
if (!file_exists(Config::$sourcedir . '/Db/APIs/' . Db::getClass(Config::$db_type) . '.php'))
{
$incontext['error'] = sprintf(Lang::$txt['error_db_file'], 'Db/APIs/' . Config::$db_type . '.php');
$incontext['error'] = sprintf(Lang::$txt['error_db_file'], 'Db/APIs/' . Db::getClass(Config::$db_type) . '.php');
return false;
}

Expand Down Expand Up @@ -1222,7 +1222,7 @@ function DatabasePopulation()

// Read in the SQL. Turn this on and that off... internationalize... etc.
$type = (Config::$db_type == 'mysqli' ? 'mysql' : Config::$db_type);
$sql_lines = explode("\n", strtr(implode(' ', file(Config::$boarddir . '/install_' . DB_SCRIPT_VERSION . '_' . $type . '.sql')), $replaces));
$sql_lines = explode("\n", strtr(implode(' ', file(Config::$boarddir . '/install_' . DB_SCRIPT_VERSION . '_' . Db::getClass($type) . '.sql')), $replaces));

// Execute the SQL.
$current_statement = '';
Expand Down
File renamed without changes.
File renamed without changes.
24 changes: 12 additions & 12 deletions other/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ function loadEssentialData()

require_once(Config::$sourcedir . '/Autoloader.php');

if (class_exists('SMF\\Db\\APIs\\' . Config::$db_type))
if (class_exists('SMF\\Db\\APIs\\' . Db::getClass(Config::$db_type)))
{
// Make the connection...
if (empty(Db::$db_connection))
Expand Down Expand Up @@ -782,10 +782,10 @@ function loadEssentialData()
Db::$db->free_result($request);
}
else
return throw_error(sprintf(Lang::$txt['error_sourcefile_missing'], 'Db/APIs/' . Config::$db_type . '.php'));
return die(sprintf(Lang::$txt['error_sourcefile_missing'], 'Db/APIs/' . Db::getClass(Config::$db_type) . '.php'));

// If they don't have the file, they're going to get a warning anyway so we won't need to clean request vars.
if (class_exists('SMF\\QueryString', false) && php_version_check())
if (class_exists('SMF\\QueryString') && php_version_check())
{
QueryString::cleanRequest();
}
Expand Down Expand Up @@ -815,8 +815,8 @@ function initialize_inputs()
// And the extra little files ;).
deleteFile(dirname(__FILE__) . '/upgrade_1-0.sql');
deleteFile(dirname(__FILE__) . '/upgrade_1-1.sql');
deleteFile(dirname(__FILE__) . '/upgrade_2-0_' . Config::$db_type . '.sql');
deleteFile(dirname(__FILE__) . '/upgrade_2-1_' . Config::$db_type . '.sql');
deleteFile(dirname(__FILE__) . '/upgrade_2-0_' . Db::getClass(Config::$db_type) . '.sql');
deleteFile(dirname(__FILE__) . '/upgrade_2-1_' . Db::getClass(Config::$db_type) . '.sql');
deleteFile(dirname(__FILE__) . '/upgrade-helper.php');

$dh = opendir(dirname(__FILE__));
Expand Down Expand Up @@ -873,12 +873,12 @@ function WelcomeLogin()
// Check for some key files - one template, one language, and a new and an old source file.
$check = @file_exists(Config::$modSettings['theme_dir'] . '/index.template.php')
&& @file_exists(Config::$sourcedir . '/QueryString.php')
&& @file_exists(Config::$sourcedir . '/Db/APIs/' . Config::$db_type . '.php')
&& @file_exists(dirname(__FILE__) . '/upgrade_2-1_' . Config::$db_type . '.sql');
&& @file_exists(Config::$sourcedir . '/Db/APIs/' . Db::getClass(Config::$db_type) . '.php')
&& @file_exists(dirname(__FILE__) . '/upgrade_2-1_' . Db::getClass(Config::$db_type) . '.sql');

// Need legacy scripts?
if (!isset(Config::$modSettings['smfVersion']) || Config::$modSettings['smfVersion'] < 2.1)
$check &= @file_exists(dirname(__FILE__) . '/upgrade_2-0_' . Config::$db_type . '.sql');
$check &= @file_exists(dirname(__FILE__) . '/upgrade_2-0_' . Db::getClass(Config::$db_type) . '.sql');
if (!isset(Config::$modSettings['smfVersion']) || Config::$modSettings['smfVersion'] < 2.0)
$check &= @file_exists(dirname(__FILE__) . '/upgrade_1-1.sql');
if (!isset(Config::$modSettings['smfVersion']) || Config::$modSettings['smfVersion'] < 1.1)
Expand Down Expand Up @@ -1677,8 +1677,8 @@ function DatabaseChanges()
$files = array(
array('upgrade_1-0.sql', '1.1', '1.1 RC0', false),
array('upgrade_1-1.sql', '2.0', '2.0 a', false),
array('upgrade_2-0_' . Config::$db_type . '.sql', '2.1', '2.1 dev0', false),
array('upgrade_2-1_' . Config::$db_type . '.sql', '3.0', SMF_VERSION, true),
array('upgrade_2-0_' . Db::getClass(Config::$db_type) . '.sql', '2.1', '2.1 dev0', false),
array('upgrade_2-1_' . Db::getClass(Config::$db_type) . '.sql', '3.0', SMF_VERSION, true),
);

// How many files are there in total?
Expand Down Expand Up @@ -4098,13 +4098,13 @@ function template_welcome_message()
{
$ago = time() - $upcontext['started'];
$ago_hours = floor($ago / 3600);
$ago_minutes = intval(($ago / 60) % 60);
$ago_minutes = (int) (((int) ($ago / 60)) % 60);
$ago_seconds = intval($ago % 60);
$agoTxt = $ago < 60 ? 'upgrade_time_s' : ($ago < 3600 ? 'upgrade_time_ms' : 'upgrade_time_hms');

$updated = time() - $upcontext['updated'];
$updated_hours = floor($updated / 3600);
$updated_minutes = intval(($updated / 60) % 60);
$updated_minutes = intval(((int) ($updated / 60)) % 60);
$updated_seconds = intval($updated % 60);
$updatedTxt = $updated < 60 ? 'upgrade_time_updated_s' : ($updated < 3600 ? 'upgrade_time_updated_hm' : 'upgrade_time_updated_hms');

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ INSERT INTO {$db_prefix}settings (variable, value) VALUES ('defaultMaxListItems'
Db::$db->insert('replace',
'{db_prefix}settings',
array('variable' => 'string', 'value' => 'string'),
array('bcrypt_hash_cost', Security::hashVerifyPassword()),
array('bcrypt_hash_cost', Security::hashBenchmark()),
array('variable')
);
---}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2089,7 +2089,7 @@ WHERE variable IN ('show_board_desc', 'display_quick_reply', 'show_mark_read', '
Db::$db->insert('replace',
'{db_prefix}settings',
array('variable' => 'string', 'value' => 'string'),
array('bcrypt_hash_cost', Security::hashVerifyPassword()),
array('bcrypt_hash_cost', Security::hashBenchmark()),
array('variable')
);
---}
Expand Down