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

- Make the plugin compatible with the Content Security Policy

### Fixed

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

## [2.10.1] - 2026-10-05

### 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 $rightname = 'config';
public $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
80 changes: 49 additions & 31 deletions inc/configfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,58 @@ class PluginGeninventorynumberConfigField extends CommonDBChild
public static $itemtype = 'PluginGeninventorynumberConfig';
public static $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)
{
$infos = getAllDataFromTable(getTableForItemType(self::class), ['itemtype' => $itemtype]);
if (!empty($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;
}
}

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 +281,7 @@ public function prepareInputForUpdate($input)
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 @@ -339,38 +379,15 @@ public static function disableCapacityForAsset(AssetDefinition $definition): boo

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 Down Expand Up @@ -495,6 +512,7 @@ public static function unregisterNewItemType($itemtype)
if (countElementsInTable(getTableForItemType(self::class), ['itemtype' => $itemtype])) {
$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'] ?? '');
}
}