From d0a4f4dd2fd4018ee1de0c2d781321ec8f6edd2d Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Thu, 6 Jul 2023 09:22:02 +0200 Subject: [PATCH 1/6] Migrate to iterator --- hook.php | 26 +++++++---- inc/container.class.php | 96 +++++++++++++++++++++++++++++------------ inc/field.class.php | 89 +++++++++++++++++++++++++------------- 3 files changed, 145 insertions(+), 66 deletions(-) diff --git a/hook.php b/hook.php index 59b4d006..133ac3b3 100644 --- a/hook.php +++ b/hook.php @@ -256,15 +256,23 @@ function plugin_fields_rule_matched($params = []) if (isset($params['input']['plugin_fusioninventory_agents_id'])) { foreach ($params['output'] as $field => $value) { - // check if current field is in a tab container - $query = "SELECT c.id - FROM glpi_plugin_fields_fields f - LEFT JOIN glpi_plugin_fields_containers c - ON c.id = f.plugin_fields_containers_id - WHERE f.name = '$field'"; - $res = $DB->query($query); - if ($DB->numrows($res) > 0) { - $data = $DB->fetchAssoc($res); + // check if current field is in a tab container + $iterator = $DB->request([ + 'SELECT' => 'c.id', + 'FROM' => 'glpi_plugin_fields_fields AS f', + 'LEFT JOIN' => [ + 'glpi_plugin_fields_containers AS c' => [ + 'ON' => [ + 'c.id' => 'f.plugin_fields_containers_id' + ] + ] + ], + 'WHERE' => [ + 'f.name' => $field + ] + ]); + if (count($iterator) > 0) { + $data = $iterator->next(); //retrieve computer $agents_id = $params['input']['plugin_fusioninventory_agents_id']; diff --git a/inc/container.class.php b/inc/container.class.php index 4f7769c8..296ada3d 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -102,8 +102,11 @@ public static function installBaseData(Migration $migration, $version) $migration->changeField($table, 'itemtype', 'itemtypes', 'longtext'); $migration->migrationOneTable($table); - $query = "UPDATE `$table` SET `itemtypes` = CONCAT('[\"', `itemtypes`, '\"]')"; - $DB->query($query) or die($DB->error()); + $DB->updateOrDie( + $table, + ['itemtypes' => new \QueryExpression('CONCAT("[\"", `itemtype`, "\"]")')], + [1] + ); } //add display preferences for this class @@ -252,15 +255,36 @@ public static function installUserData(Migration $migration, $version) $compfields = $fields->find(['plugin_fields_containers_id' => $comptab, 'name' => $newname]); if ($compfields) { $newname = $newname . '_os'; - $DB->query("UPDATE glpi_plugin_fields_fields SET name='$newname' WHERE name='{$field['name']}' AND plugin_fields_containers_id='$ostab'"); + $DB->update( + 'glpi_plugin_fields_fields', + [ + 'name' => $newname + ], + [ + 'name' => $field['name'], + 'plugin_fields_containers_id' => $ostab + ] + ); } $compdata::addField($newname, $field['type']); $fieldnames[$field['name']] = $newname; } - $sql = "UPDATE glpi_plugin_fields_fields SET plugin_fields_containers_id='$comptab' WHERE plugin_fields_containers_id='$ostab'"; - $DB->query($sql); - $DB->query("DELETE FROM glpi_plugin_fields_containers WHERE id='$ostab'"); + $DB->update( + 'glpi_plugin_fields_fields', + [ + 'plugin_fields_containers_id' => $comptab + ], + [ + 'plugin_fields_containers_id' => $ostab + ] + ); + $DB->delete( + 'glpi_plugin_fields_containers', + [ + 'id' => $ostab + ] + ); //migrate existing data $existings = $osdata->find(); @@ -275,9 +299,16 @@ public static function installUserData(Migration $migration, $version) //drop old table $DB->query("DROP TABLE " . $osdata::getTable()); } else { - $sql = "UPDATE glpi_plugin_fields_containers SET type='dom', subtype=NULL WHERE id='$ostab'"; - $comptab = $ostab; - $DB->query($sql); + $DB->update( + 'glpi_plugin_fields_containers', + [ + 'type' => 'dom', + 'subtype' => null + ], + [ + 'id' => $ostab + ] + ); } } @@ -1017,14 +1048,24 @@ public static function getUsedItemtypes($type = 'all', $must_be_active = false) { global $DB; $itemtypes = []; - $where = $type == 'all' ? '1=1' : ('type = "' . $type . '"'); + $where = []; + + if ($type == 'all') { + $where['type'] = $type; + } + if ($must_be_active) { - $where .= ' AND is_active = 1'; + $where['is_active'] = 1; } - $query = 'SELECT DISTINCT `itemtypes` FROM `glpi_plugin_fields_containers` WHERE ' . $where; - $result = $DB->query($query); - while (list($data) = $DB->fetchArray($result)) { + $iterator = $DB->request([ + 'SELECT' => 'itemtypes', + 'DISTINCT' => true, + 'FROM' => self::getTable(), + 'WHERE' => $where, + ]); + + foreach ($iterator as $data) { $jsonitemtype = json_decode($data); $itemtypes = array_merge($itemtypes, $jsonitemtype); } @@ -1354,19 +1395,20 @@ public static function validateValues($data, $itemtype, $massiveaction) } else if ($field['mandatory'] == 1 && isset($data['items_id'])) { $tablename = getTableForItemType(self::getClassname($itemtype, $container->fields['name'])); - $query = "SELECT * FROM `$tablename` WHERE - `itemtype`='$itemtype' - AND `items_id`='{$data['items_id']}' - AND `plugin_fields_containers_id`='{$data['plugin_fields_containers_id']}'"; - - $db_result = []; - if ($result = $DB->query($query)) { - $db_result = $DB->fetchAssoc($result); - if (isset($db_result['plugin_fields_' . $name . 'dropdowns_id'])) { - $value = $db_result['plugin_fields_' . $name . 'dropdowns_id']; - } else if (isset($db_result[$name])) { - $value = $db_result[$name]; - } + $iterator = $DB->request([ + 'FROM' => $tablename, + 'WHERE' => [ + 'itemtype' => $itemtype, + 'items_id' => $data['items_id'], + 'plugin_fields_containers_id' => $data['plugin_fields_containers_id'], + ], + ]); + + $db_result = $iterator->next(); + if (isset($db_result['plugin_fields_' . $name . 'dropdowns_id'])) { + $value = $db_result['plugin_fields_' . $name . 'dropdowns_id']; + } else if (isset($db_result[$name])) { + $value = $db_result[$name]; } } else { if ($massiveaction) { diff --git a/inc/field.class.php b/inc/field.class.php index 378da533..9c76ec78 100644 --- a/inc/field.class.php +++ b/inc/field.class.php @@ -367,11 +367,16 @@ public function post_purgeItem() $old_container = $this->fields['plugin_fields_containers_id']; $old_ranking = $this->fields['ranking']; - $query = "UPDATE $table SET - ranking = ranking-1 - WHERE plugin_fields_containers_id = $old_container - AND ranking > $old_ranking"; - $DB->query($query); + $D->update( + $table, + [ + 'ranking' => new QueryExpression($DB->quoteName('ranking') . ' - 1') + ], + [ + 'plugin_fields_containers_id' => $old_container, + 'ranking' => ['>', $old_ranking] + ] + ); return true; } @@ -441,14 +446,18 @@ public function getNextRanking() { global $DB; - $sql = "SELECT max(`ranking`) AS `rank` - FROM `" . self::getTable() . "` - WHERE `plugin_fields_containers_id` = '" . - $this->fields['plugin_fields_containers_id'] . "'"; - $result = $DB->query($sql); + $iterator = $DB->request([ + 'SELECT' => new \QueryExpression( + 'max(' . $DB->quoteName('ranking') . ') AS ' . $DB->quoteName('rank') + ), + 'FROM' => self::getTable(), + 'WHERE' => [ + 'plugin_fields_containers_id' => $this->fields['plugin_fields_containers_id'] + ] + ]); - if ($DB->numrows($result) > 0) { - $data = $DB->fetchAssoc($result); + if (count($iterator) > 0) { + $data = $iterator->next(); return $data["rank"] + 1; } return 0; @@ -496,14 +505,14 @@ public function showSummary($container) $cID = $container->fields['id']; // Display existing Fields - $query = "SELECT `id`, `label` - FROM `" . $this->getTable() . "` - WHERE `plugin_fields_containers_id` = '$cID' - ORDER BY `ranking` ASC"; - $result = $DB->query($query); + $iterator = $DB->request([ + 'SELECT' => ['id', 'label'], + 'FROM' => self::getTable(), + 'WHERE' => ['plugin_fields_containers_id' => $cID], + 'ORDER' => 'ranking ASC' + ]); $rand = mt_rand(); - echo "
"; $ajax_params = [ @@ -525,7 +534,7 @@ public function showSummary($container) ""; echo __("Add a new field", "fields") . "
"; - if ($DB->numrows($result) == 0) { + if (count($iterator) == 0) { echo ""; echo "
" . __("No field for this block", "fields") . "
"; } else { @@ -548,7 +557,7 @@ public function showSummary($container) Session::initNavigateListItems('PluginFieldsField', __('Fields list')); - while ($data = $DB->fetchArray($result)) { + foreach ($iterator as $data) { if ($this->getFromDB($data['id'])) { echo ""; @@ -1154,20 +1163,40 @@ public static function showSingle($itemtype, $searchOption, $massiveaction = fal ); //find field - $query = "SELECT fields.plugin_fields_containers_id, fields.is_readonly, fields.multiple, fields.default_value - FROM glpi_plugin_fields_fields fields - LEFT JOIN glpi_plugin_fields_containers containers - ON containers.id = fields.plugin_fields_containers_id - AND containers.itemtypes LIKE '%$itemtype%' - WHERE fields.name = '$cleaned_linkfield'"; - $res = $DB->query($query); - if ($DB->numrows($res) == 0) { + $iterator = $DB->request([ + 'SELECT' => [ + 'fields.plugin_fields_containers_id', + 'fields.is_readonly', + 'fields.multiple', + 'fields.default_value' + ], + 'FROM' => self::getTable() . ' AS fields', + 'LEFT JOIN' => [ + 'glpi_plugin_fields_containers AS containers' => [ + 'ON' => [ + 'containers' => 'id', + 'fields' => 'plugin_fields_containers_id', [ + 'AND' => [ + 'containers' => [ + 'itemtypes' => ['LIKE' => "%$itemtype%"] + ] + ] + ] + ] + ], + 'WHERE' => [ + 'fields.name' => $cleaned_linkfield + ] + ], + ]); + + if (count($iterator) == 0) { return false; } - $data = $DB->fetchAssoc($res); + $data = $iterator->next(); - //display an hidden post field to store container id + //display a hidden post field to store container id echo Html::hidden('c_id', ['value' => $data['plugin_fields_containers_id']]); //prepare array for function prepareHtmlFields From 76a97efc8e282f9e47da7b364d78f112464cb97a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Anne?= Date: Fri, 21 Jul 2023 11:10:14 +0200 Subject: [PATCH 2/6] fix join --- hook.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/hook.php b/hook.php index 133ac3b3..7e02a022 100644 --- a/hook.php +++ b/hook.php @@ -258,17 +258,18 @@ function plugin_fields_rule_matched($params = []) foreach ($params['output'] as $field => $value) { // check if current field is in a tab container $iterator = $DB->request([ - 'SELECT' => 'c.id', - 'FROM' => 'glpi_plugin_fields_fields AS f', + 'SELECT' => 'glpi_plugin_fields_containers.id', + 'FROM' => 'glpi_plugin_fields_containers', 'LEFT JOIN' => [ - 'glpi_plugin_fields_containers AS c' => [ - 'ON' => [ - 'c.id' => 'f.plugin_fields_containers_id' + 'glpi_plugin_fields_fields' => [ + 'FKEY' => [ + 'glpi_plugin_fields_containers' => 'id', + 'glpi_plugin_fields_fields' => 'plugin_fields_containers_id' ] ] ], 'WHERE' => [ - 'f.name' => $field + 'glpi_plugin_fields_fields.name' => $field, ] ]); if (count($iterator) > 0) { From e0312e676f4526a56a026886b2b027fb3e91a506 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Anne?= Date: Fri, 21 Jul 2023 11:14:56 +0200 Subject: [PATCH 3/6] Fix expression --- inc/container.class.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/inc/container.class.php b/inc/container.class.php index 296ada3d..e84920a4 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -104,7 +104,16 @@ public static function installBaseData(Migration $migration, $version) $DB->updateOrDie( $table, - ['itemtypes' => new \QueryExpression('CONCAT("[\"", `itemtype`, "\"]")')], + [ + 'itemtypes' => new QueryExpression( + sprintf( + 'CONCAT(%s, %s, %s)', + $DB->quoteValue('[\"'), + $DB->quoteName('itemtype'), + $DB->quoteValue('\"]') + ) + ), + ], [1] ); } From 610eed89842e28870f5e69d1e924ea452acac500 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Anne?= Date: Fri, 21 Jul 2023 11:17:21 +0200 Subject: [PATCH 4/6] fix condition --- inc/container.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/container.class.php b/inc/container.class.php index e84920a4..01f16376 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -1059,7 +1059,7 @@ public static function getUsedItemtypes($type = 'all', $must_be_active = false) $itemtypes = []; $where = []; - if ($type == 'all') { + if ($type !== 'all') { $where['type'] = $type; } From 0348cbe6bd0ec6ae434d71b39dfa3fb51b2c02ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Anne?= Date: Fri, 21 Jul 2023 11:22:07 +0200 Subject: [PATCH 5/6] fix join and where --- inc/field.class.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/inc/field.class.php b/inc/field.class.php index 9c76ec78..5471a449 100644 --- a/inc/field.class.php +++ b/inc/field.class.php @@ -1173,21 +1173,20 @@ public static function showSingle($itemtype, $searchOption, $massiveaction = fal 'FROM' => self::getTable() . ' AS fields', 'LEFT JOIN' => [ 'glpi_plugin_fields_containers AS containers' => [ - 'ON' => [ + 'FKEY' => [ 'containers' => 'id', - 'fields' => 'plugin_fields_containers_id', [ + 'fields' => 'plugin_fields_containers_id', + [ 'AND' => [ - 'containers' => [ - 'itemtypes' => ['LIKE' => "%$itemtype%"] - ] + 'containers.itemtypes' => ['LIKE' => "%$itemtype%"] ] ] ] - ], - 'WHERE' => [ - 'fields.name' => $cleaned_linkfield ] ], + 'WHERE' => [ + 'fields.name' => $cleaned_linkfield + ], ]); if (count($iterator) == 0) { From d39d018b9a887fd2bc7e19c376a4cbbd9966a91e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Anne?= Date: Fri, 21 Jul 2023 11:23:18 +0200 Subject: [PATCH 6/6] fix --- inc/container.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/container.class.php b/inc/container.class.php index 01f16376..0a18ea30 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -1075,7 +1075,7 @@ public static function getUsedItemtypes($type = 'all', $must_be_active = false) ]); foreach ($iterator as $data) { - $jsonitemtype = json_decode($data); + $jsonitemtype = json_decode($data['itemtypes']); $itemtypes = array_merge($itemtypes, $jsonitemtype); }