From 59f9c8a176d98fd2765ac77de2092a097b196e8a Mon Sep 17 00:00:00 2001
From: Julien Durand <300495670+jdurand-teclib@users.noreply.github.com>
Date: Fri, 13 Nov 2026 17:10:04 +0100
Subject: [PATCH 1/2] chore: Optimize performances
---
CHANGELOG.md | 1 +
inc/preference.class.php | 43 +++++++++++++++++++++++++++++++---------
setup.php | 5 +++--
3 files changed, 38 insertions(+), 11 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a893d11..9237edb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
- CI: fix Psalm cache directory, drop the stray `glpi-project/tools` composer dependency, declare a unique composer autoloader suffix
+- Speed up the Lifecycle tab of the user preferences and the location lookup when applying a model, and remove the SQL queries the plugin ran on every page to check its own activation
## [2.10.6] - 2026-09-18
diff --git a/inc/preference.class.php b/inc/preference.class.php
index 52afa9a..b86000d 100644
--- a/inc/preference.class.php
+++ b/inc/preference.class.php
@@ -58,6 +58,9 @@ public function handleLocationPref(array $input): array
public function showFormUserPreferences()
{
+ /** @var DBmysql $DB */
+ global $DB;
+
$entity = $_SESSION['glpiactive_entity'];
$userID = Session::getLoginUserID();
$templates = PluginUninstallUninstall::getAllTemplatesByEntity(
@@ -78,21 +81,39 @@ public function showFormUserPreferences()
echo "
" . __s("Item's location after uninstall", "uninstall") . " | ";
echo "";
- foreach ($templates as $ID => $name) {
- $pref_ID = self::checkIfPreferenceExistsByEntity($userID, $ID, $entity);
+ // One query for all the user preferences in this entity instead of two per model
+ $prefs = [];
+ foreach (
+ $DB->request([
+ 'SELECT' => ['id', 'templates_id', 'locations_action', 'locations_id'],
+ 'FROM' => self::getTable(),
+ 'WHERE' => [
+ 'users_id' => $userID,
+ 'entities_id' => $entity,
+ ],
+ ]) as $row
+ ) {
+ $prefs[$row['templates_id']] ??= $row;
+ }
- if (!$pref_ID) {
- $pref_ID = self::addDefaultPreference($userID, $ID, $entity);
+ foreach ($templates as $ID => $name) {
+ if (!isset($prefs[$ID])) {
+ // Default preference: keep the previous location (see handleLocationPref())
+ $prefs[$ID] = [
+ 'id' => self::addDefaultPreference($userID, $ID, $entity),
+ 'locations_action' => 'old',
+ 'locations_id' => 0,
+ ];
}
-
- $this->getFromDB($pref_ID);
+ $pref = $prefs[$ID];
+ $pref_ID = $pref['id'];
echo "| " . $name . " | ";
echo "";
- $value = ($this->fields["locations_id"] ?? 0);
+ $value = ($pref["locations_id"] ?? 0);
// Handle "old" special value, dropdown expect "-1" in this case
- if ($this->fields['locations_action'] == "old" && $value == 0) {
+ if ($pref['locations_action'] == "old" && $value == 0) {
$value = -1;
}
@@ -321,6 +342,9 @@ public static function install(Migration $migration)
),
);
}
+
+ // Preferences are always searched by user, entity and model
+ $migration->addKey($table, ['users_id', 'entities_id', 'templates_id'], 'users_entities_templates');
} else {
// plugin nevers installed
$query = "CREATE TABLE `" . $table . "` (
@@ -330,7 +354,8 @@ public static function install(Migration $migration)
`templates_id` int {$default_key_sign} DEFAULT '0',
`locations_action` varchar(10) NOT NULL DEFAULT 'set',
`locations_id` int {$default_key_sign} DEFAULT '0',
- PRIMARY KEY (`id`)
+ PRIMARY KEY (`id`),
+ KEY `users_entities_templates` (`users_id`, `entities_id`, `templates_id`)
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;";
$DB->doQuery($query);
}
diff --git a/setup.php b/setup.php
index a6191a2..539ccb3 100644
--- a/setup.php
+++ b/setup.php
@@ -51,8 +51,9 @@ function plugin_init_uninstall()
Plugin::registerClass(PluginUninstallPreference::class, ['addtabon' => [Preference::class]]);
Plugin::registerClass(PluginUninstallProfile::class, ['addtabon' => [Profile::class]]);
- $plugin = new Plugin();
- if ($plugin->isActivated('uninstall')) {
+ // Core also runs this init while installing or activating the plugin: keep the guard,
+ // but read the list of active plugins filled at boot instead of querying glpi_plugins.
+ if (in_array('uninstall', Plugin::getPlugins(), true)) {
$UNINSTALL_TYPES = [Computer::class, Monitor::class, NetworkEquipment::class, Peripheral::class, Phone::class, Printer::class];
$UNINSTALL_DIRECT_CONNECTIONS_TYPE = [Monitor::class, Peripheral::class, Phone::class, Printer::class];
From 150fe9eb92d095d4d5e4cc5c08e436753ace365c Mon Sep 17 00:00:00 2001
From: Julien Durand <300495670+jdurand-teclib@users.noreply.github.com>
Date: Fri, 13 Nov 2026 17:16:04 +0100
Subject: [PATCH 2/2] fix: rector
---
inc/preference.class.php | 1 +
1 file changed, 1 insertion(+)
diff --git a/inc/preference.class.php b/inc/preference.class.php
index b86000d..7968def 100644
--- a/inc/preference.class.php
+++ b/inc/preference.class.php
@@ -105,6 +105,7 @@ public function showFormUserPreferences()
'locations_id' => 0,
];
}
+
$pref = $prefs[$ID];
$pref_ID = $pref['id'];
|