Skip to content

Commit

Permalink
Intelligently sets Config::$backward_compatibility as needed for mods
Browse files Browse the repository at this point in the history
Signed-off-by: Jon Stovell <[email protected]>
  • Loading branch information
Sesquipedalian committed Jul 3, 2024
1 parent 2be4d51 commit fe3c9fc
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 14 deletions.
24 changes: 16 additions & 8 deletions Sources/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,15 @@ class Config

######### Modification Support #########
/**
* @var bool
* @var int
*
* Master switch to enable backward compatibility behaviours.
* 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 bool $backward_compatibility = true;
public static int $backward_compatibility;

######### Legacy settings #########
/**
Expand Down Expand Up @@ -783,13 +787,17 @@ class Config
######### Modification Support #########
/**
* @var bool
* @var int
*
* Master switch to enable backward compatibility behaviours.
* 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' => true,
'type' => 'boolean',
'default' => 0,
'type' => 'integer',
],
'db_character_set' => [
'text' => <<<'END'
Expand Down Expand Up @@ -956,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
44 changes: 44 additions & 0 deletions Sources/PackageManager/PackageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -1412,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
10 changes: 7 additions & 3 deletions other/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,15 @@

######### Modification Support #########
/**
* @var bool
* @var int
*
* Master switch to enable backward compatibility behaviours.
* 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 = true;
$backward_compatibility = 0;

######### Legacy Settings #########
/**
Expand Down
10 changes: 7 additions & 3 deletions other/Settings_bak.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,15 @@

######### Modification Support #########
/**
* @var bool
* @var int
*
* Master switch to enable backward compatibility behaviours.
* 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 = true;
$backward_compatibility = 0;

######### Legacy Settings #########
/**
Expand Down

0 comments on commit fe3c9fc

Please sign in to comment.