Skip to content

Commit 76a3155

Browse files
committed
Do not disable plugins during bugfixes updates
1 parent b4e685b commit 76a3155

File tree

5 files changed

+97
-14
lines changed

5 files changed

+97
-14
lines changed

phpunit/functional/Glpi/Toolbox/VersionParserTest.php

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,126 +48,171 @@ public static function versionsProvider()
4848
'version' => '',
4949
'keep_stability_flag' => false,
5050
'normalized' => '',
51+
'major' => '',
52+
'intermediate' => '',
5153
'stable' => true,
5254
'dev' => false,
5355
],
5456
[
5557
'version' => '9.5+2.0',
5658
'keep_stability_flag' => false,
5759
'normalized' => '9.5+2.0', // not semver compatible, cannot be normalized
60+
'major' => '9',
61+
'intermediate' => '9.5',
5862
'stable' => true,
5963
'dev' => false,
6064
],
6165
[
6266
'version' => '0.89',
6367
'keep_stability_flag' => false,
6468
'normalized' => '0.89.0',
69+
'major' => '0',
70+
'intermediate' => '0.89',
6571
'stable' => true,
6672
'dev' => false,
6773
],
6874
[
6975
'version' => '9.2',
7076
'keep_stability_flag' => false,
7177
'normalized' => '9.2.0',
78+
'major' => '9',
79+
'intermediate' => '9.2',
7280
'stable' => true,
7381
'dev' => false,
7482
],
7583
[
7684
'version' => '9.2',
7785
'keep_stability_flag' => true, // should have no effect
7886
'normalized' => '9.2.0',
87+
'major' => '9',
88+
'intermediate' => '9.2',
7989
'stable' => true,
8090
'dev' => false,
8191
],
8292
[
8393
'version' => '9.4.1.1',
8494
'keep_stability_flag' => false,
8595
'normalized' => '9.4.1',
96+
'major' => '9',
97+
'intermediate' => '9.4',
8698
'stable' => true,
8799
'dev' => false,
88100
],
89101
[
90102
'version' => '10.0.0-dev',
91103
'keep_stability_flag' => false,
92104
'normalized' => '10.0.0',
105+
'major' => '10',
106+
'intermediate' => '10.0',
93107
'stable' => false,
94108
'dev' => true,
95109
],
96110
[
97111
'version' => '10.0.0-dev',
98112
'keep_stability_flag' => true,
99113
'normalized' => '10.0.0-dev',
114+
'major' => '10',
115+
'intermediate' => '10.0',
100116
'stable' => false,
101117
'dev' => true,
102118
],
103119
[
104120
'version' => '10.0.0-alpha',
105121
'keep_stability_flag' => false,
106122
'normalized' => '10.0.0',
123+
'major' => '10',
124+
'intermediate' => '10.0',
107125
'stable' => false,
108126
'dev' => false,
109127
],
110128
[
111129
'version' => '10.0.0-alpha2',
112130
'keep_stability_flag' => true,
113131
'normalized' => '10.0.0-alpha2',
132+
'major' => '10',
133+
'intermediate' => '10.0',
114134
'stable' => false,
115135
'dev' => false,
116136
],
117137
[
118138
'version' => '10.0.0-beta1',
119139
'keep_stability_flag' => false,
120140
'normalized' => '10.0.0',
141+
'major' => '10',
142+
'intermediate' => '10.0',
121143
'stable' => false,
122144
'dev' => false,
123145
],
124146
[
125147
'version' => '10.0.0-beta1',
126148
'keep_stability_flag' => true,
127149
'normalized' => '10.0.0-beta1',
150+
'major' => '10',
151+
'intermediate' => '10.0',
128152
'stable' => false,
129153
'dev' => false,
130154
],
131155
[
132156
'version' => '10.0.0-rc3',
133157
'keep_stability_flag' => false,
134158
'normalized' => '10.0.0',
159+
'major' => '10',
160+
'intermediate' => '10.0',
135161
'stable' => false,
136162
'dev' => false,
137163
],
138164
[
139165
'version' => '10.0.0-rc',
140166
'keep_stability_flag' => true,
141167
'normalized' => '10.0.0-rc',
168+
'major' => '10',
169+
'intermediate' => '10.0',
142170
'stable' => false,
143171
'dev' => false,
144172
],
145173
[
146174
'version' => '10.0.3',
147175
'keep_stability_flag' => true,
148176
'normalized' => '10.0.3',
177+
'major' => '10',
178+
'intermediate' => '10.0',
149179
'stable' => true,
150180
'dev' => false,
151181
],
152182
];
153183
}
154184

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

162192
#[DataProvider('versionsProvider')]
163-
public function testIsStableRelease(string $version, bool $keep_stability_flag, string $normalized, bool $stable, bool $dev): void
193+
public function testGetMajorVersion(string $version, bool $keep_stability_flag, string $normalized, string $major, string $intermediate, bool $stable, bool $dev): void
194+
{
195+
$version_parser = new \Glpi\Toolbox\VersionParser();
196+
$this->assertEquals($major, $version_parser->getMajorVersion($version));
197+
}
198+
199+
#[DataProvider('versionsProvider')]
200+
public function testGetIntermediateVersion(string $version, bool $keep_stability_flag, string $normalized, string $major, string $intermediate, bool $stable, bool $dev): void
201+
{
202+
$version_parser = new \Glpi\Toolbox\VersionParser();
203+
$this->assertEquals($intermediate, $version_parser->getIntermediateVersion($version));
204+
}
205+
206+
207+
#[DataProvider('versionsProvider')]
208+
public function testIsStableRelease(string $version, bool $keep_stability_flag, string $normalized, string $major, string $intermediate, bool $stable, bool $dev): void
164209
{
165210
$version_parser = new \Glpi\Toolbox\VersionParser();
166211
$this->assertSame($stable, $version_parser->isStableRelease($version));
167212
}
168213

169214
#[DataProvider('versionsProvider')]
170-
public function testIsDevVersion(string $version, bool $keep_stability_flag, string $normalized, bool $stable, bool $dev): void
215+
public function testIsDevVersion(string $version, bool $keep_stability_flag, string $normalized, string $major, string $intermediate, bool $stable, bool $dev): void
171216
{
172217
$version_parser = new \Glpi\Toolbox\VersionParser();
173218
$this->assertSame($dev, $version_parser->isDevVersion($version));

src/Glpi/Kernel/Listener/InitializePlugins.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,14 @@ public static function getSubscribedEvents(): array
5959

6060
public function onPostBoot(): void
6161
{
62-
if (!DBConnection::isDbAvailable() || (!defined('SKIP_UPDATES') && !Update::isDbUpToDate())) {
62+
/** @var \DBmysql $DB */
63+
global $DB;
64+
65+
if (
66+
!DBConnection::isDbAvailable()
67+
|| (!defined('SKIP_UPDATES') && !Update::isDbUpToDate())
68+
|| !$DB->tableExists(Plugin::getTable())
69+
) {
6370
// Requires the database to be available.
6471
return;
6572
}

src/Glpi/Toolbox/VersionParser.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ public static function getNormalizedVersion(string $version, bool $keep_stabilit
7676
return $version;
7777
}
7878

79+
/**
80+
* Get major version number (e.g. '9').
81+
*/
82+
public static function getMajorVersion(string $version): string
83+
{
84+
$normalized = self::getNormalizedVersion($version, false);
85+
86+
return \preg_replace('/^(\d+)[^d].+$/', '$1', $normalized);
87+
}
88+
89+
/**
90+
* Get intermediate version number (e.g. '9.5').
91+
*/
92+
public static function getIntermediateVersion(string $version): string
93+
{
94+
$normalized = self::getNormalizedVersion($version, false);
95+
96+
return \preg_replace('/^(\d+\.\d+)[^d].+$/', '$1', $normalized);
97+
}
98+
7999
/**
80100
* Check if given version is a stable release (i.e. does not contain a stability flag referring to unstable state).
81101
*

src/Plugin.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,6 @@ public function init(bool $load_plugins = false)
285285
self::$activated_plugins = [];
286286
self::$loaded_plugins = [];
287287

288-
if (!($DB instanceof DBmysql) || !$DB->connected) {
289-
// Cannot init plugins list if DB is not connected
290-
self::$plugins_state_checked = true;
291-
return;
292-
}
293-
294288
$this->checkStates(false);
295289

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

1253+
/**
1254+
* Unload all plugins.
1255+
*/
1256+
final public function unloadAll(): void
1257+
{
1258+
foreach ($this->getPlugins() as $plugin_key) {
1259+
$this->unload($plugin_key);
1260+
}
1261+
}
1262+
12591263

12601264
/**
12611265
* clean a plugin

src/Update.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,6 @@ function (string $flag) {
212212
// To prevent problem of execution time
213213
ini_set("max_execution_time", "0");
214214

215-
// Update process desactivate all plugins
216-
$plugin = new Plugin();
217-
$plugin->unactivateAll();
218-
219215
if (version_compare($current_version, GLPI_VERSION, '>')) {
220216
$message = sprintf(
221217
__('Unsupported version (%1$s)'),
@@ -230,6 +226,17 @@ function (string $flag) {
230226
}
231227
}
232228

229+
if (VersionParser::getIntermediateVersion($current_version) !== VersionParser::getIntermediateVersion(GLPI_VERSION)) {
230+
// The target version is another intermediate/major version.
231+
// Deactivate all plugins to prevent blocking the GLPI execution if a plugin is incompatible with this new version.
232+
(new Plugin())->unactivateAll();
233+
} else {
234+
// The target version is the same intermediate/major version.
235+
// Unload all plugins to prevent them to interfere with the update process.
236+
// They will be loaded again once the update is done.
237+
(new Plugin())->unloadAll();
238+
}
239+
233240
$migrations = $this->getMigrationsToDo($current_version, $force_latest);
234241
foreach ($migrations as $key => $migration_specs) {
235242
include_once($migration_specs['file']);

0 commit comments

Comments
 (0)