Skip to content

Commit

Permalink
Merge pull request #8281 from Sesquipedalian/3.0/backward_compatibili…
Browse files Browse the repository at this point in the history
…ty_auto
  • Loading branch information
Sesquipedalian authored Jul 3, 2024
2 parents 44369f1 + fe3c9fc commit b0f3971
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 20 deletions.
38 changes: 30 additions & 8 deletions Sources/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ class Config
* Public static properties
**************************/

/**
* @var bool
*
* Master switch to enable backward compatibility behaviours.
*/
public static bool $backward_compatibility = true;

########## Maintenance ##########
/**
* @var int 0, 1, 2
Expand Down Expand Up @@ -254,6 +247,18 @@ class Config
*/
public static string $tasksdir;

######### Modification Support #########
/**
* @var int
*
* Master switch to enable backward compatibility behaviours:
* 0: Off. This is the default.
* 1: On. This will be set automatically if an installed modification needs it.
* 2: Forced on. Use this to enable backward compatibility behaviours even when
* no installed modifications require them. This is usually not necessary.
*/
public static int $backward_compatibility;

######### Legacy settings #########
/**
* @var string
Expand Down Expand Up @@ -777,6 +782,23 @@ class Config
'raw_default' => true,
'type' => 'string',
],
'backward_compatibility' => [
'text' => <<<'END'
######### Modification Support #########
/**
* @var int
*
* Master switch to enable backward compatibility behaviours:
* 0: Off. This is the default.
* 1: On. This will be set automatically if an installed modification needs it.
* 2: Forced on. Use this to enable backward compatibility behaviours even when
* no installed modifications require them. This is usually not necessary.
*/
END,
'default' => 0,
'type' => 'integer',
],
'db_character_set' => [
'text' => <<<'END'
Expand Down Expand Up @@ -942,7 +964,7 @@ public static function set(array $settings): void

// For backward compatibility, make settings available as global variables.
// Must do this manually because SMF\BackwardCompatibility is not loaded yet.
if (self::$backward_compatibility && !self::$exported) {
if (!empty(self::$backward_compatibility) && !self::$exported) {
foreach ($settings as $var => $val) {
if (property_exists(__CLASS__, $var)) {
$GLOBALS[$var] = &self::${$var};
Expand Down
99 changes: 87 additions & 12 deletions Sources/PackageManager/PackageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -1229,8 +1229,12 @@ public function install(): void
Db::$db->query(
'',
'UPDATE {db_prefix}log_packages
SET install_state = {int:not_installed}, member_removed = {string:member_name},
id_member_removed = {int:current_member}, time_removed = {int:current_time}, sha256_hash = {string:package_hash}
SET
install_state = {int:not_installed},
member_removed = {string:member_name},
id_member_removed = {int:current_member},
time_removed = {int:current_time},
sha256_hash = {string:package_hash}
WHERE package_id = {string:package_id}
AND id_install = {int:install_id}',
[
Expand All @@ -1253,8 +1257,12 @@ public function install(): void
Db::$db->query(
'',
'UPDATE {db_prefix}log_packages
SET install_state = {int:not_installed}, member_removed = {string:member_name},
id_member_removed = {int:current_member}, time_removed = {int:current_time}, sha256_hash = {string:package_hash}
SET
install_state = {int:not_installed},
member_removed = {string:member_name},
id_member_removed = {int:current_member},
time_removed = {int:current_time},
sha256_hash = {string:package_hash}
WHERE package_id = {string:package_id}
AND version = {string:old_version}',
[
Expand Down Expand Up @@ -1322,21 +1330,44 @@ public function install(): void

// Credits tag?
$credits_tag = (empty($credits_tag)) ? '' : Utils::jsonEncode($credits_tag);

// Log that we installed it.
Db::$db->insert(
'',
'{db_prefix}log_packages',
[
'filename' => 'string', 'name' => 'string', 'package_id' => 'string', 'version' => 'string',
'id_member_installed' => 'int', 'member_installed' => 'string', 'time_installed' => 'int',
'install_state' => 'int', 'failed_steps' => 'string', 'themes_installed' => 'string',
'member_removed' => 'int', 'db_changes' => 'string', 'credits' => 'string',
'filename' => 'string',
'name' => 'string',
'package_id' => 'string',
'version' => 'string',
'id_member_installed' => 'int',
'member_installed' => 'string',
'time_installed' => 'int',
'install_state' => 'int',
'failed_steps' => 'string',
'themes_installed' => 'string',
'member_removed' => 'int',
'db_changes' => 'string',
'credits' => 'string',
'sha256_hash' => 'string',
'smf_version' => 'string',
],
[
$package_filename, $package_name, $package_id, $package_version,
User::$me->id, User::$me->name, time(),
$is_upgrade ? 2 : 1, $failed_step_insert, $themes_installed,
0, $db_changes, $credits_tag, Utils::$context['package_sha256_hash'],
$package_filename,
$package_name,
$package_id,
$package_version,
User::$me->id,
User::$me->name,
time(),
$is_upgrade ? 2 : 1,
$failed_step_insert,
$themes_installed,
0,
$db_changes,
$credits_tag,
Utils::$context['package_sha256_hash'],
Utils::$context['smf_version'],
],
['id_install'],
);
Expand Down Expand Up @@ -1381,6 +1412,50 @@ public function install(): void

// Restore file permissions?
SubsPackage::create_chmod_control([], [], true);

// Does Config::$backward_compatibility need to be updated?
$this->updateBackwardCompatibility();
}

/**
* Enables Config::$backward_compatibility if it is needed.
*
* Once support for backward compatibility behaviours has been discontinued
* in a future version of SMF, this method can be removed.
*/
public function updateBackwardCompatibility(): void
{
// If it has been forced on, leave it alone.
if ((Config::$backward_compatibility ?? null) === 2) {
return;
}

$min_version = preg_replace('/^(\d+\.\d+).*/', '$1.dev.0', SMF_VERSION);

$request = Db::$db->query(
'',
'SELECT smf_version
FROM {db_prefix}log_packages
WHERE install_state != {int:not_installed}
ORDER BY smf_version ASC',
[
'not_installed' => 0,
],
);

while ($row = Db::$db->fetch_assoc($request)) {
$row['smf_version'] = strtr(strtolower($row['smf_version']), ' ', '.');

if (version_compare($row['smf_version'], $min_version, '<')) {
break;
}
}

Db::$db->free_result($request);

Config::updateSettingsFile([
'backward_compatibility' => version_compare($row['smf_version'], $min_version, '<'),
]);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions Sources/PackageManager/SubsPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,9 @@ public static function parsePackageInfo(XmlArray &$packageXML, bool $testing_onl
return [];
}

// Keep track of what version of SMF we are emulating (if any).
Utils::$context['smf_version'] = preg_replace('/^(\d+\.\d+).*/', '$1', $the_version);

// Find all the actions in this method - in theory, these should only be allowed actions. (* means all.)
$actions = $script->set('*');
$return = [];
Expand Down
12 changes: 12 additions & 0 deletions other/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@
*/
$languagesdir = dirname(__FILE__) . '/Languages';

######### Modification Support #########
/**
* @var int
*
* Master switch to enable backward compatibility behaviours:
* 0: Off. This is the default.
* 1: On. This will be set automatically if an installed modification needs it.
* 2: Forced on. Use this to enable backward compatibility behaviours even when
* no installed modifications require them. This is usually not necessary.
*/
$backward_compatibility = 0;

######### Legacy Settings #########
/**
* @var string
Expand Down
12 changes: 12 additions & 0 deletions other/Settings_bak.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@
*/
$languagesdir = dirname(__FILE__) . '/Languages';

######### Modification Support #########
/**
* @var int
*
* Master switch to enable backward compatibility behaviours:
* 0: Off. This is the default.
* 1: On. This will be set automatically if an installed modification needs it.
* 2: Forced on. Use this to enable backward compatibility behaviours even when
* no installed modifications require them. This is usually not necessary.
*/
$backward_compatibility = 0;

######### Legacy Settings #########
/**
* @var string
Expand Down
1 change: 1 addition & 0 deletions other/install_3-0_MySQL.sql
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ CREATE TABLE {$db_prefix}log_packages (
db_changes TEXT NOT NULL,
credits TEXT NOT NULL,
sha256_hash TEXT,
smf_version VARCHAR(5) NOT NULL DEFAULT '',
PRIMARY KEY (id_install),
INDEX idx_filename (filename(15))
) ENGINE={$engine};
Expand Down
1 change: 1 addition & 0 deletions other/install_3-0_PostgreSQL.sql
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ CREATE TABLE {$db_prefix}log_packages (
db_changes text NOT NULL,
credits text NOT NULL,
sha256_hash TEXT,
smf_version varchar(5) NOT NULL DEFAULT '',
PRIMARY KEY (id_install)
);

Expand Down
9 changes: 9 additions & 0 deletions other/upgrade_3-0_MySQL.sql
Original file line number Diff line number Diff line change
Expand Up @@ -968,4 +968,13 @@ Db::$db->add_index(

---# Adding new "spoofdetector_censor" setting
INSERT IGNORE INTO {$db_prefix}settings (variable, value) VALUES ('spoofdetector_censor', '1');
---#

/******************************************************************************/
--- Adding SMF version information to log_packages
/******************************************************************************/

---# Adding a new column "smf_version" to log_packages table
ALTER TABLE {$db_prefix}log_packages
ADD COLUMN smf_version VARCHAR(5) NOT NULL DEFAULT '';
---#
9 changes: 9 additions & 0 deletions other/upgrade_3-0_PostgreSQL.sql
Original file line number Diff line number Diff line change
Expand Up @@ -850,4 +850,13 @@ CREATE INDEX {$db_prefix}idx_spoofdetector_name_id ON {$db_prefix}members (spoof

---# Adding new "spoofdetector_censor" setting
INSERT INTO {$db_prefix}settings (variable, value) VALUES ('spoofdetector_censor', '1') ON CONFLICT DO NOTHING;
---#

/******************************************************************************/
--- Adding SMF version information to log_packages
/******************************************************************************/

---# Adding a new column "smf_version" to log_packages table
ALTER TABLE {$db_prefix}log_packages
ADD COLUMN IF NOT EXISTS smf_version VARCHAR(5) NOT NULL DEFAULT '';
---#

0 comments on commit b0f3971

Please sign in to comment.