diff --git a/CHANGELOG.md b/CHANGELOG.md index fdc8bb8e..9c0aa80c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fix search on number fields: `is` / `is not` ignoring values from 1000, `<` / `>` comparing values as text, and `is not` excluding items without value - Fix `Some mandatory fields are empty` error on a filled dropdown field when its block table contains a leftover column with the same name - Fix GenericObject migration error message to name the required 3.0.x version +- Fix slow forms, lists and saves on large databases: block values were read without using their index, and label translations, field definitions and block lists were queried again for every label, list cell and request ## [1.24.7] - 2026-10-28 diff --git a/inc/abstractcontainerinstance.class.php b/inc/abstractcontainerinstance.class.php index 5d89ce70..34bd8d97 100644 --- a/inc/abstractcontainerinstance.class.php +++ b/inc/abstractcontainerinstance.class.php @@ -103,12 +103,12 @@ public static function getSpecificValueToSelect($field, $name = '', $values = '' $field_id = $options['searchopt']['pfields_fields_id'] ?? null; - $field_specs = new PluginFieldsField(); - if ($field_id !== null && $field_specs->getFromDB($field_id)) { + $field_specs = $field_id !== null ? self::getFieldSpecs((int) $field_id) : null; + if ($field_specs !== null) { $dropdown_matches = []; if ( - preg_match('/^dropdown-(?.+)$/i', (string) $field_specs->fields['type'], $dropdown_matches) === 1 - && $field_specs->fields['multiple'] + preg_match('/^dropdown-(?.+)$/i', (string) $field_specs['type'], $dropdown_matches) === 1 + && $field_specs['multiple'] ) { $itemtype = $dropdown_matches['class']; if (!is_a($itemtype, CommonDBTM::class, true)) { @@ -127,10 +127,10 @@ public static function getSpecificValueToSelect($field, $name = '', $values = '' 'display' => false, ]); } elseif ( - $field_specs->fields['type'] === 'dropdown' - && $field_specs->fields['multiple'] + $field_specs['type'] === 'dropdown' + && $field_specs['multiple'] ) { - $itemtype = PluginFieldsDropdown::getClassname($field_specs->fields['name']); + $itemtype = PluginFieldsDropdown::getClassname($field_specs['name']); return Dropdown::show($itemtype, [ 'name' => $name, @@ -151,20 +151,20 @@ public static function getSpecificValueToDisplay($field, $values, array $options $field_id = $options['searchopt']['pfields_fields_id'] ?? null; - $field_specs = new PluginFieldsField(); - if ($field_id !== null && $field_specs->getFromDB($field_id)) { + $field_specs = $field_id !== null ? self::getFieldSpecs((int) $field_id) : null; + if ($field_specs !== null) { $dropdown_matches = []; if ( - preg_match('/^dropdown-(?.+)$/i', (string) $field_specs->fields['type'], $dropdown_matches) === 1 - && $field_specs->fields['multiple'] + preg_match('/^dropdown-(?.+)$/i', (string) $field_specs['type'], $dropdown_matches) === 1 + && $field_specs['multiple'] ) { $itemtype = $dropdown_matches['class']; if (!is_a($itemtype, CommonDBTM::class, true)) { return ''; // Itemtype not exists (maybe a deactivated plugin) } - if (empty($values[$field]) && !empty($field_specs->fields['default_value'])) { - $values[$field] = $field_specs->fields['default_value']; + if (empty($values[$field]) && !empty($field_specs['default_value'])) { + $values[$field] = $field_specs['default_value']; } if (empty($values[$field])) { @@ -186,13 +186,13 @@ public static function getSpecificValueToDisplay($field, $values, array $options return implode($options['separator'] ?? '
', $names); } elseif ( - $field_specs->fields['type'] === 'dropdown' - && $field_specs->fields['multiple'] + $field_specs['type'] === 'dropdown' + && $field_specs['multiple'] ) { - $itemtype = PluginFieldsDropdown::getClassname($field_specs->fields['name']); + $itemtype = PluginFieldsDropdown::getClassname($field_specs['name']); if (empty($values[$field])) { - if (!empty($field_specs->fields["default_value"])) { - $values[$field] = $field_specs->fields['default_value']; + if (!empty($field_specs["default_value"])) { + $values[$field] = $field_specs['default_value']; } else { return ''; // Value not defined } @@ -213,6 +213,19 @@ public static function getSpecificValueToDisplay($field, $values, array $options return parent::getSpecificValueToDisplay($field, $values, $options); } + private static function getFieldSpecs(int $field_id): ?array + { + /** @var array|null> $specs */ + static $specs = []; + + if (!array_key_exists($field_id, $specs)) { + $field = new PluginFieldsField(); + $specs[$field_id] = $field->getFromDB($field_id) ? $field->fields : null; + } + + return $specs[$field_id]; + } + public static function addField($fieldname, $type, array $options = []) { $migration = new PluginFieldsMigration('0'); diff --git a/inc/container.class.php b/inc/container.class.php index 6b9ec560..17392d88 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -43,6 +43,9 @@ class PluginFieldsContainer extends CommonDBTM */ private static ?WeakMap $pending_values = null; + /** @var array>|null */ + private static ?array $containers = null; + public static function canCreate(): bool { return self::canUpdate(); @@ -855,6 +858,8 @@ public function prepareInputForAdd($input) // phpcs:ignore PSR1.Methods.CamelCapsMethodName public function post_addItem() { + self::$containers = null; + if (!isset($this->input['clone']) || !$this->input['clone']) { //create profiles associated to this container PluginFieldsProfile::createForContainer($this); @@ -973,6 +978,20 @@ public static function generateTemplate($fields) return true; } + // phpcs:ignore PSR1.Methods.CamelCapsMethodName + public function post_updateItem($history = true) + { + parent::post_updateItem($history); + self::$containers = null; + } + + // phpcs:ignore PSR1.Methods.CamelCapsMethodName + public function post_purgeItem() + { + parent::post_purgeItem(); + self::$containers = null; + } + // phpcs:ignore PSR1.Methods.CamelCapsMethodName public function pre_deleteItem() { @@ -1055,7 +1074,7 @@ public static function preItemPurge($item) } $fields = new $classname(); - $fields->deleteByCriteria(['items_id' => $item->fields['id']], true); + $fields->deleteByCriteria(['itemtype' => $itemtype, 'items_id' => $item->fields['id']], true); } } @@ -1570,20 +1589,15 @@ public static function getEntries($type = 'tab', $full = false): array /** @var DBmysql $DB */ global $DB; - $condition = [ - 'is_active' => 1, - ]; - if ($type !== 'all') { - $condition[] = ['type' => $type]; - } - if (!$DB->tableExists(self::getTable())) { return []; } $itemtypes = []; - $container = new self(); - $found = $container->find($condition, 'label'); + $found = array_filter( + self::getContainers(), + fn(array $container): bool => $container['is_active'] == 1 && ($type === 'all' || $container['type'] === $type), + ); if ( $found === [] @@ -1648,28 +1662,15 @@ public static function getEntries($type = 'tab', $full = false): array public static function getUsedItemtypes($type = 'all', $must_be_active = false) { - /** @var DBmysql $DB */ - global $DB; $itemtypes = []; - $where = []; - - if ($type !== 'all') { - $where['type'] = $type; - } - - if ($must_be_active) { - $where['is_active'] = 1; - } - - $iterator = $DB->request([ - 'SELECT' => 'itemtypes', - 'DISTINCT' => true, - 'FROM' => self::getTable(), - 'WHERE' => $where, - ]); + $containers = array_filter( + self::getContainers(), + fn(array $container): bool => ($type === 'all' || $container['type'] === $type) + && (!$must_be_active || $container['is_active'] == 1), + ); - foreach ($iterator as $data) { - $jsonitemtype = PluginFieldsToolbox::decodeJSONItemtypes($data['itemtypes']); + foreach (array_unique(array_column($containers, 'itemtypes')) as $json_itemtypes) { + $jsonitemtype = PluginFieldsToolbox::decodeJSONItemtypes($json_itemtypes); if (is_array($jsonitemtype)) { $itemtypes = array_merge($itemtypes, $jsonitemtype); } @@ -1678,6 +1679,14 @@ public static function getUsedItemtypes($type = 'all', $must_be_active = false) return $itemtypes; } + /** + * @return array> + */ + private static function getContainers(): array + { + return self::$containers ??= (new self())->find([], 'label'); + } + public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { if ($withtemplate) { @@ -1790,7 +1799,7 @@ public function updateFieldsValues($data, $itemtype, $massiveaction = false) $obj = new $classname(); - $exist = $obj->getFromDBByCrit(['items_id' => $items_id]); + $exist = $obj->getFromDBByCrit(['itemtype' => $itemtype, 'items_id' => $items_id]); // Convert "multiple" values into a JSON string $multiple_fields_iterator = $DB->request([ @@ -1835,7 +1844,7 @@ public function updateFieldsValues($data, $itemtype, $massiveaction = false) $obj = $dbu->getItemForItemtype($classname); if ($obj !== false) { - if ($obj->getFromDBByCrit(['items_id' => $items_id]) === false) { + if ($obj->getFromDBByCrit(['itemtype' => $itemtype, 'items_id' => $items_id]) === false) { // add fields data $obj->add($data); } else { @@ -2296,7 +2305,7 @@ private static function getStoredValues(self $container, string $itemtype, int $ } $values_obj = (new DbUtils())->getItemForItemtype(self::getClassname($itemtype, $container->fields['name'])); - if ($values_obj === false || !$values_obj->getFromDBByCrit(['items_id' => $items_id])) { + if ($values_obj === false || !$values_obj->getFromDBByCrit(['itemtype' => $itemtype, 'items_id' => $items_id])) { return []; } @@ -2741,7 +2750,7 @@ private static function checkContainerMandatory(CommonDBTM $item, PluginFieldsCo $classname = self::getClassname($item::getType(), $loc_c->fields['name']); $dbu = new DbUtils(); $obj = $dbu->getItemForItemtype($classname); - if ($obj !== false && $obj->getFromDBByCrit(['items_id' => $item->getID()])) { + if ($obj !== false && $obj->getFromDBByCrit(['itemtype' => $item::getType(), 'items_id' => $item->getID()])) { $data += $obj->fields; } else { $data += self::getPendingValues($item, $c_id); diff --git a/inc/field.class.php b/inc/field.class.php index 82817259..c70fd07f 100644 --- a/inc/field.class.php +++ b/inc/field.class.php @@ -1280,6 +1280,7 @@ public static function prepareHtmlFields( if ($obj instanceof CommonDBTM) { $found_values = $obj->find( [ + 'itemtype' => $item->getType(), 'plugin_fields_containers_id' => $first_field['plugin_fields_containers_id'], 'items_id' => $item->getID(), ], diff --git a/inc/labeltranslation.class.php b/inc/labeltranslation.class.php index 8b1ff9ac..e8c4fb23 100644 --- a/inc/labeltranslation.class.php +++ b/inc/labeltranslation.class.php @@ -38,6 +38,9 @@ class PluginFieldsLabelTranslation extends CommonDBChild public static $items_id = 'items_id'; + /** @var array>> */ + private static array $labels_by_language = []; + /** * Install or update plugin base data. * @@ -125,6 +128,24 @@ public function prepareInputForAdd($input) return $input; } + public function post_addItem() + { + parent::post_addItem(); + self::$labels_by_language = []; + } + + public function post_updateItem($history = true) + { + parent::post_updateItem($history); + self::$labels_by_language = []; + } + + public function post_purgeItem() + { + parent::post_purgeItem(); + self::$labels_by_language = []; + } + public static function createForItem(CommonDBTM $item) { @@ -374,16 +395,30 @@ public static function getAlreadyTranslatedForItem($itemtype, $items_id) */ public static function getLabelFor(array $item) { - $obj = new self(); - $found = $obj->find(['itemtype' => $item['itemtype'], - 'items_id' => $item['id'], - 'language' => $_SESSION['glpilanguage'], - ]); + $language = $_SESSION['glpilanguage']; + self::$labels_by_language[$language] ??= self::loadLabels($language); - if (count($found) > 0) { - return array_values($found)[0]['label']; + return self::$labels_by_language[$language][$item['itemtype']][(int) $item['id']] ?? $item['label'] ?? ''; + } + + /** + * @return array> + */ + private static function loadLabels(string $language): array + { + /** @var DBmysql $DB */ + global $DB; + + $labels = []; + $iterator = $DB->request([ + 'SELECT' => ['itemtype', 'items_id', 'label'], + 'FROM' => self::getTable(), + 'WHERE' => ['language' => $language], + ]); + foreach ($iterator as $row) { + $labels[$row['itemtype']][(int) $row['items_id']] = (string) $row['label']; } - return $item['label'] ?? ''; + return $labels; } } diff --git a/inc/questiontype.class.php b/inc/questiontype.class.php index c983c7ab..73fc743c 100644 --- a/inc/questiontype.class.php +++ b/inc/questiontype.class.php @@ -521,13 +521,14 @@ public static function getFieldsFromBlock(?int $block_id): array public static function hasAvailableFields(): bool { $blocks = (new self())->getAvailableBlocks(); - foreach (array_keys($blocks) as $block_id) { - $fields = (new self())->getFieldsFromBlock($block_id); - if ($fields !== []) { - return true; - } + if ($blocks === []) { + return false; } - return false; + return countElementsInTable(PluginFieldsField::getTable(), [ + 'is_active' => 1, + 'plugin_fields_containers_id' => array_keys($blocks), + 'NOT' => ['type' => 'header'], + ]) > 0; } } diff --git a/setup.php b/setup.php index 1ed1bebc..7ed0dd97 100644 --- a/setup.php +++ b/setup.php @@ -101,7 +101,8 @@ function plugin_init_fields() $pluginfields_autoloader = new PluginFieldsAutoloader([PLUGINFIELDS_CLASS_PATH]); $pluginfields_autoloader->register(); - if (!Plugin::isPluginActive('fields')) { + // Activated plugins are listed at boot; isPluginActive() queries the DB until init is over. + if (!in_array('fields', Plugin::getPlugins(), true)) { return; } @@ -202,7 +203,7 @@ function plugin_init_fields() } // Add Fields to Datainjection - if (Plugin::isPluginActive('datainjection')) { + if (in_array('datainjection', Plugin::getPlugins(), true)) { $PLUGIN_HOOKS['plugin_datainjection_populate']['fields'] = 'plugin_datainjection_populate_fields'; }