Skip to content
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Make the plugin compatible with the Content Security Policy

<<<<<<< HEAD
## [2.11.0] - 2026-10-05

### Added

- GLPI 12 compatibility
=======
### Fixed

- Fewer SQL queries when generating inventory numbers, especially with the massive actions

## [2.10.1] - 2026-10-05
>>>>>>> b1d348a (Perf: read the configuration once per request (#180))

### Fixed

Expand Down
31 changes: 28 additions & 3 deletions inc/config.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class PluginGeninventorynumberConfig extends CommonDBTM
public static string $rightname = 'config';
public bool $dohistory = true;

/**
* Generation switch, read once per request (null: not read yet).
*
* @var mixed
*/
private static $generation_active = null;

/**
* Never reset the index
* @var int
Expand Down Expand Up @@ -345,10 +352,28 @@ public static function updateIndex()

public static function isGenerationActive()
{
$config = new self();
$config->getFromDB(1);
if (self::$generation_active === null) {
$config = new self();
$config->getFromDB(1);
self::$generation_active = $config->fields['is_active'];
}

return self::$generation_active;
}

return $config->fields['is_active'];
/**
* Forget the configuration read during the request, so the next read sees the saved values.
*/
public static function clearCache(): void
{
self::$generation_active = null;
PluginGeninventorynumberConfigField::clearCache();
}

public function post_updateItem($history = true)
{
parent::post_updateItem($history);
self::clearCache();
}

public static function getIcon()
Expand Down
78 changes: 52 additions & 26 deletions inc/configfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,66 @@
public static string $itemtype = 'PluginGeninventorynumberConfig';
public static string $items_id = 'plugin_geninventorynumber_configs_id';

/**
* Configuration rows by itemtype, read once per request (null: not read yet).
* Their `index` and `date_last_generated` columns go stale as numbers are generated:
* the index helpers always read them from the database.
*
* @var array<string, array<string, mixed>>|null
*/
private static ?array $rows_by_itemtype = null;

public static function getTypeName($nb = 0)
{
return __s('Specific configuration', 'geninventorynumber');
}

public static function getConfigFieldByItemType($itemtype)
{
<<<<<<< HEAD

Check failure on line 61 in inc/configfield.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_SL on line 61

Check failure on line 61 in inc/configfield.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_SL on line 61
$infos = getAllDataFromTable(getTableForItemType(self::class), ['itemtype' => $itemtype]);
if ($infos !== []) {
return array_pop($infos);
} else {
return $infos;
=======
return self::getRowsByItemtype()[$itemtype] ?? [];
}

/**
* @return array<string, array<string, mixed>>
*/
private static function getRowsByItemtype(): array
{
if (self::$rows_by_itemtype === null) {
self::$rows_by_itemtype = [];
foreach (getAllDataFromTable(self::getTable(), ['ORDER' => 'id']) as $row) {
self::$rows_by_itemtype[$row['itemtype']] = $row;

Check failure on line 79 in inc/configfield.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected '[', expecting ']' on line 79

Check failure on line 79 in inc/configfield.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Cannot use empty array elements in arrays on line 79

Check failure on line 79 in inc/configfield.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected '[', expecting ']' on line 79

Check failure on line 79 in inc/configfield.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Cannot use empty array elements in arrays on line 79
}
>>>>>>> b1d348a (Perf: read the configuration once per request (#180))
}

return self::$rows_by_itemtype;
}

/**
* Forget the rows read during the request, so the next read sees the saved values.
*/
public static function clearCache(): void
{
self::$rows_by_itemtype = null;
}

public function post_addItem()
{
parent::post_addItem();
self::clearCache();
}

public function post_purgeItem()
{
parent::post_purgeItem();
self::clearCache();
}

public static function install(Migration $migration)
Expand Down Expand Up @@ -242,6 +289,7 @@
public function post_updateItem($history = true)
{
parent::post_updateItem($history);
self::clearCache();

// Check if itemtype is a custom asset and if is_active changed
if (isset($this->fields['itemtype']) && in_array('is_active', $this->updates)) {
Expand Down Expand Up @@ -300,8 +348,8 @@
];

// Update the definition
return $definition->update([

Check failure on line 351 in inc/configfield.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 351

Check failure on line 351 in inc/configfield.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 351
'id' => $definition->getID(),

Check failure on line 352 in inc/configfield.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 352

Check failure on line 352 in inc/configfield.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 352
'capacities' => $current_capacities,
]);
}
Expand All @@ -317,7 +365,7 @@
$capacity_classname = HasInventoryNumberGenerationCapacity::class;

// Get current capacities (decoded from JSON)
$current_capacities = json_decode($definition->fields['capacities'] ?? '[]', true) ?: [];

Check failure on line 368 in inc/configfield.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 368

Check failure on line 368 in inc/configfield.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 368

// Check if capacity is not enabled
if (!in_array($capacity_classname, array_column($current_capacities, 'name'), true)) {
Expand All @@ -331,46 +379,23 @@
);

// Update the definition
return $definition->update([

Check failure on line 382 in inc/configfield.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 382

Check failure on line 382 in inc/configfield.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 382
'id' => $definition->getID(),

Check failure on line 383 in inc/configfield.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 383

Check failure on line 383 in inc/configfield.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 383
'capacities' => $current_capacities,
]);
}

public static function getEnabledItemTypes()
{
/** @var DBmysql $DB */
global $DB;

$it = $DB->request([
'SELECT' => ['itemtype'],
'DISTINCT' => true,
'FROM' => getTableForItemType(self::class),
'ORDER' => ['itemtype'],
]);
$types = [];
foreach ($it as $data) {
$types[] = $data['itemtype'];
}
$types = array_keys(self::getRowsByItemtype());
sort($types, SORT_STRING | SORT_FLAG_CASE);

return $types;
}

public static function isActiveForItemType($itemtype)
{
/** @var DBmysql $DB */
global $DB;

$it = $DB->request([
'SELECT' => ['is_active'],
'FROM' => getTableForItemType(self::class),
'WHERE' => ['itemtype' => $itemtype],
]);
if (count($it) > 0) {
return $it->current()['is_active'];
}

return false;
return self::getRowsByItemtype()[$itemtype]['is_active'] ?? false;
}

/**
Expand All @@ -383,14 +408,14 @@
/** @var DBmysql $DB */
global $DB;

$iterator = $DB->request([

Check failure on line 411 in inc/configfield.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 411

Check failure on line 411 in inc/configfield.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 411
'SELECT' => ['date_last_generated', 'auto_reset_method'],
'FROM' => self::getTable(),
'WHERE' => [
'itemtype' => $itemtype,
],
]);
if ($iterator->count() > 0) {

Check failure on line 418 in inc/configfield.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 418

Check failure on line 418 in inc/configfield.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 418
$data = $iterator->current();
if (
$data['auto_reset_method'] === PluginGeninventorynumberConfig::AUTO_RESET_NONE
Expand Down Expand Up @@ -495,6 +520,7 @@
if (countElementsInTable(getTableForItemType(self::class), ['itemtype' => $itemtype]) !== 0) {
$config = new self();
$is_delete = $config->deleteByCriteria(['itemtype' => $itemtype]);
self::clearCache();
if ($is_delete) {
unset($GENINVENTORYNUMBER_TYPES[$itemtype]);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/GenInventoryNumberTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
*/
abstract class GenInventoryNumberTestCase extends DbTestCase
{
public function setUp(): void
{
parent::setUp();

// The configuration is cached for the request, and each test rolls the database back
PluginGeninventorynumberConfig::clearCache();
}

/**
* Initialize plugin configuration
*
Expand Down
31 changes: 31 additions & 0 deletions tests/Units/GenerationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -613,4 +613,35 @@ public function testItemLifecycleWithConfigFieldInactive(): void
$config = $this->getConfig();
$this->assertEquals(0, $config->fields['index'], "[UPDATE] Index should still not be incremented");
}

/**
* The configuration is cached for the request: a saved change must be seen by the next
* generation, and the counter must keep increasing between generations.
*/
public function testConfigurationChangeIsSeenInTheSameRequest(): void
{
$this->initConfig(['is_active' => 1]);
$this->setConfigField(Computer::class, [
'is_active' => 1,
'use_index' => 0,
'template' => 'A-<####>',
'index' => 0,
]);

// Two generations in a row: consecutive numbers
$first = $this->createItem(Computer::class, ['name' => 'cache 1', 'entities_id' => 0], ['otherserial']);
$second = $this->createItem(Computer::class, ['name' => 'cache 2', 'entities_id' => 0], ['otherserial']);
$this->assertSame('A-0001', $first->fields['otherserial']);
$this->assertSame('A-0002', $second->fields['otherserial']);

// New template: used by the next generation
$this->setConfigField(Computer::class, ['template' => 'B-<####>']);
$third = $this->createItem(Computer::class, ['name' => 'cache 3', 'entities_id' => 0], ['otherserial']);
$this->assertSame('B-0003', $third->fields['otherserial']);

// Generation switched off: no number anymore
$this->updateConfig(['is_active' => 0]);
$fourth = $this->createItem(Computer::class, ['name' => 'cache 4', 'entities_id' => 0]);
$this->assertEmpty($fourth->fields['otherserial'] ?? '');
}
}
Loading