Skip to content

Do not disable plugins during bugfixes updates #19199

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions phpunit/functional/Glpi/Toolbox/VersionParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,126 +48,171 @@ public static function versionsProvider()
'version' => '',
'keep_stability_flag' => false,
'normalized' => '',
'major' => '',
'intermediate' => '',
'stable' => true,
'dev' => false,
],
[
'version' => '9.5+2.0',
'keep_stability_flag' => false,
'normalized' => '9.5+2.0', // not semver compatible, cannot be normalized
'major' => '9',
'intermediate' => '9.5',
'stable' => true,
'dev' => false,
],
[
'version' => '0.89',
'keep_stability_flag' => false,
'normalized' => '0.89.0',
'major' => '0',
'intermediate' => '0.89',
'stable' => true,
'dev' => false,
],
[
'version' => '9.2',
'keep_stability_flag' => false,
'normalized' => '9.2.0',
'major' => '9',
'intermediate' => '9.2',
'stable' => true,
'dev' => false,
],
[
'version' => '9.2',
'keep_stability_flag' => true, // should have no effect
'normalized' => '9.2.0',
'major' => '9',
'intermediate' => '9.2',
'stable' => true,
'dev' => false,
],
[
'version' => '9.4.1.1',
'keep_stability_flag' => false,
'normalized' => '9.4.1',
'major' => '9',
'intermediate' => '9.4',
'stable' => true,
'dev' => false,
],
[
'version' => '10.0.0-dev',
'keep_stability_flag' => false,
'normalized' => '10.0.0',
'major' => '10',
'intermediate' => '10.0',
'stable' => false,
'dev' => true,
],
[
'version' => '10.0.0-dev',
'keep_stability_flag' => true,
'normalized' => '10.0.0-dev',
'major' => '10',
'intermediate' => '10.0',
'stable' => false,
'dev' => true,
],
[
'version' => '10.0.0-alpha',
'keep_stability_flag' => false,
'normalized' => '10.0.0',
'major' => '10',
'intermediate' => '10.0',
'stable' => false,
'dev' => false,
],
[
'version' => '10.0.0-alpha2',
'keep_stability_flag' => true,
'normalized' => '10.0.0-alpha2',
'major' => '10',
'intermediate' => '10.0',
'stable' => false,
'dev' => false,
],
[
'version' => '10.0.0-beta1',
'keep_stability_flag' => false,
'normalized' => '10.0.0',
'major' => '10',
'intermediate' => '10.0',
'stable' => false,
'dev' => false,
],
[
'version' => '10.0.0-beta1',
'keep_stability_flag' => true,
'normalized' => '10.0.0-beta1',
'major' => '10',
'intermediate' => '10.0',
'stable' => false,
'dev' => false,
],
[
'version' => '10.0.0-rc3',
'keep_stability_flag' => false,
'normalized' => '10.0.0',
'major' => '10',
'intermediate' => '10.0',
'stable' => false,
'dev' => false,
],
[
'version' => '10.0.0-rc',
'keep_stability_flag' => true,
'normalized' => '10.0.0-rc',
'major' => '10',
'intermediate' => '10.0',
'stable' => false,
'dev' => false,
],
[
'version' => '10.0.3',
'keep_stability_flag' => true,
'normalized' => '10.0.3',
'major' => '10',
'intermediate' => '10.0',
'stable' => true,
'dev' => false,
],
];
}

#[DataProvider('versionsProvider')]
public function testGetNormalizeVersion(string $version, bool $keep_stability_flag, string $normalized, bool $stable, bool $dev): void
public function testGetNormalizeVersion(string $version, bool $keep_stability_flag, string $normalized, string $major, string $intermediate, bool $stable, bool $dev): void
{
$version_parser = new \Glpi\Toolbox\VersionParser();
$this->assertEquals($normalized, $version_parser->getNormalizedVersion($version, $keep_stability_flag));
}

#[DataProvider('versionsProvider')]
public function testIsStableRelease(string $version, bool $keep_stability_flag, string $normalized, bool $stable, bool $dev): void
public function testGetMajorVersion(string $version, bool $keep_stability_flag, string $normalized, string $major, string $intermediate, bool $stable, bool $dev): void
{
$version_parser = new \Glpi\Toolbox\VersionParser();
$this->assertEquals($major, $version_parser->getMajorVersion($version));
}

#[DataProvider('versionsProvider')]
public function testGetIntermediateVersion(string $version, bool $keep_stability_flag, string $normalized, string $major, string $intermediate, bool $stable, bool $dev): void
{
$version_parser = new \Glpi\Toolbox\VersionParser();
$this->assertEquals($intermediate, $version_parser->getIntermediateVersion($version));
}


#[DataProvider('versionsProvider')]
public function testIsStableRelease(string $version, bool $keep_stability_flag, string $normalized, string $major, string $intermediate, bool $stable, bool $dev): void
{
$version_parser = new \Glpi\Toolbox\VersionParser();
$this->assertSame($stable, $version_parser->isStableRelease($version));
}

#[DataProvider('versionsProvider')]
public function testIsDevVersion(string $version, bool $keep_stability_flag, string $normalized, bool $stable, bool $dev): void
public function testIsDevVersion(string $version, bool $keep_stability_flag, string $normalized, string $major, string $intermediate, bool $stable, bool $dev): void
{
$version_parser = new \Glpi\Toolbox\VersionParser();
$this->assertSame($dev, $version_parser->isDevVersion($version));
Expand Down
9 changes: 8 additions & 1 deletion src/Glpi/Kernel/Listener/InitializePlugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ public static function getSubscribedEvents(): array

public function onPostBoot(): void
{
if (!DBConnection::isDbAvailable() || (!defined('SKIP_UPDATES') && !Update::isDbUpToDate())) {
/** @var \DBmysql $DB */
global $DB;

if (
!DBConnection::isDbAvailable()
|| (!defined('SKIP_UPDATES') && !Update::isDbUpToDate())
|| !$DB->tableExists(Plugin::getTable())
) {
// Requires the database to be available.
return;
}
Expand Down
20 changes: 20 additions & 0 deletions src/Glpi/Toolbox/VersionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ public static function getNormalizedVersion(string $version, bool $keep_stabilit
return $version;
}

/**
* Get major version number (e.g. '9').
*/
public static function getMajorVersion(string $version): string
{
$normalized = self::getNormalizedVersion($version, false);

return \preg_replace('/^(\d+)[^d].+$/', '$1', $normalized);
}

/**
* Get intermediate version number (e.g. '9.5').
*/
public static function getIntermediateVersion(string $version): string
{
$normalized = self::getNormalizedVersion($version, false);

return \preg_replace('/^(\d+\.\d+)[^d].+$/', '$1', $normalized);
}

/**
* Check if given version is a stable release (i.e. does not contain a stability flag referring to unstable state).
*
Expand Down
16 changes: 10 additions & 6 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,6 @@ public function init(bool $load_plugins = false)
self::$activated_plugins = [];
self::$loaded_plugins = [];

if (!($DB instanceof DBmysql) || !$DB->connected) {
// Cannot init plugins list if DB is not connected
self::$plugins_state_checked = true;
return;
}

$this->checkStates(false);

$plugins = $this->find(['state' => [self::ACTIVATED, self::TOBECONFIGURED]]);
Expand Down Expand Up @@ -1256,6 +1250,16 @@ public function unactivateAll()
);
}

/**
* Unload all plugins.
*/
final public function unloadAll(): void
{
foreach ($this->getPlugins() as $plugin_key) {
$this->unload($plugin_key);
}
}


/**
* clean a plugin
Expand Down
15 changes: 11 additions & 4 deletions src/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,6 @@ function (string $flag) {
// To prevent problem of execution time
ini_set("max_execution_time", "0");

// Update process desactivate all plugins
$plugin = new Plugin();
$plugin->unactivateAll();

if (version_compare($current_version, GLPI_VERSION, '>')) {
$message = sprintf(
__('Unsupported version (%1$s)'),
Expand All @@ -230,6 +226,17 @@ function (string $flag) {
}
}

if (VersionParser::getIntermediateVersion($current_version) !== VersionParser::getIntermediateVersion(GLPI_VERSION)) {
// The target version is another intermediate/major version.
// Deactivate all plugins to prevent blocking the GLPI execution if a plugin is incompatible with this new version.
(new Plugin())->unactivateAll();
} else {
// The target version is the same intermediate/major version.
// Unload all plugins to prevent them to interfere with the update process.
// They will be loaded again once the update is done.
(new Plugin())->unloadAll();
}

$migrations = $this->getMigrationsToDo($current_version, $force_latest);
foreach ($migrations as $key => $migration_specs) {
include_once($migration_specs['file']);
Expand Down