Skip to content

Commit e293039

Browse files
committed
Fix massimport (peripherals, network port)
Drop raw queries Bump version
1 parent 0fbd069 commit e293039

File tree

14 files changed

+510
-266
lines changed

14 files changed

+510
-266
lines changed

hook.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ function plugin_ocsinventoryng_install()
528528
$ocsserver = new OcsServer();
529529
$dbu = new DbUtils();
530530
foreach ($dbu->getAllDataFromTable('glpi_plugin_ocsinventoryng_ocsservers') as $ocs) {
531-
if (($b64_decoded = base64_decode($ocs["ocs_db_passwd"], true)) !== false
531+
if ($ocs["ocs_db_passwd"] != null && ($b64_decoded = base64_decode($ocs["ocs_db_passwd"], true)) !== false
532532
&& json_decode($b64_decoded, true) !== null) {
533533
$ocsserver->update(['id' => $ocs['id'],
534534
'ocs_db_passwd' => rawurlencode(stripslashes((new GLPIKey())->encrypt($ocs["ocs_db_passwd"])))]);
@@ -848,9 +848,6 @@ function plugin_ocsinventoryng_postinit()
848848
{
849849
global $PLUGIN_HOOKS;
850850

851-
$PLUGIN_HOOKS['pre_item_add']['ocsinventoryng'] = [];
852-
$PLUGIN_HOOKS['item_update']['ocsinventoryng'] = [];
853-
854851
$PLUGIN_HOOKS['pre_item_add']['ocsinventoryng']
855852
= ['Asset_PeripheralAsset' => [Ocslink::class, 'addComputer_Item']];
856853

@@ -1196,7 +1193,7 @@ function plugin_ocsinventoryng_addWhere($link, $nott, $type, $ID, $val)
11961193
* @param $data
11971194
* @param $num
11981195
*
1199-
* @return string|translated
1196+
* @return string
12001197
*/
12011198
function plugin_ocsinventoryng_giveItem($type, $id, $data, $num)
12021199
{

ocsinventoryng.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
<author>Walid Nouh</author>
2929
</authors>
3030
<versions>
31+
<version>
32+
<num>2.1.3</num>
33+
<compatibility>~11.0</compatibility>
34+
<download_url>https://github.com/pluginsGLPI/ocsinventoryng/releases/download/2.1.3/glpi-ocsinventoryng-2.1.3.tar.bz2</download_url>
35+
</version>
3136
<version>
3237
<num>2.1.2</num>
3338
<compatibility>~11.0</compatibility>

public/scripts/ocsng_fullsync.php

Lines changed: 87 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*/
3030

3131
use GlpiPlugin\Ocsinventoryng\Config;
32+
use GlpiPlugin\Ocsinventoryng\Detail;
3233
use GlpiPlugin\Ocsinventoryng\Notimportedcomputer;
3334
use GlpiPlugin\Ocsinventoryng\OcsProcess;
3435
use GlpiPlugin\Ocsinventoryng\OcsServer;
@@ -66,8 +67,8 @@
6667

6768
$_SESSION["glpicronuserrunning"] = $_SESSION["glpiname"] = 'ocsinventoryng';
6869
// Check PHP Version - sometime (debian) cli version != module version
69-
if (phpversion() < "5") {
70-
die("PHP version:" . phpversion() . " - " . "You must install at least PHP5.\n\n");
70+
if (phpversion() < "8") {
71+
die("PHP version:" . phpversion() . " - " . "You must install at least PHP 8.\n\n");
7172
}
7273
// Chech Memory_limit - sometine cli limit (php-cli.ini) != module limit (php.ini)
7374
$mem = Toolbox::getMemoryLimit();
@@ -110,14 +111,19 @@
110111
echo "=====================================================\n";
111112

112113
//Import from all the OCS servers
113-
$query = "SELECT `id`, `name`
114-
FROM `glpi_plugin_ocsinventoryng_ocsservers`
115-
WHERE `is_active`
116-
AND `use_massimport`";
117-
$result = $DB->doQuery($query);
114+
$criteria = [
115+
'SELECT' => ['id', 'name'],
116+
'FROM' => 'glpi_plugin_ocsinventoryng_ocsservers',
117+
'WHERE' => [
118+
'is_active' => 1,
119+
'use_massimport' => 1,
120+
]
121+
];
122+
123+
$iterator = $DB->request($criteria);
118124

119125
echo "=====================================================\n";
120-
while ($ocsservers = $DB->fetchArray($result)) {
126+
foreach ($iterator as $ocsservers) {
121127
FirstPass($ocsservers["id"]);
122128
}
123129
echo "=====================================================\n";
@@ -178,13 +184,18 @@
178184
}
179185
} else {
180186
//Import from all the OCS servers
181-
$query = "SELECT `id`, `name`
182-
FROM `glpi_plugin_ocsinventoryng_ocsservers`
183-
WHERE `is_active`
184-
AND `use_massimport`";
185-
$res = $DB->doQuery($query);
186-
187-
while ($ocsservers = $DB->fetchArray($res)) {
187+
$criteria = [
188+
'SELECT' => ['id', 'name'],
189+
'FROM' => 'glpi_plugin_ocsinventoryng_ocsservers',
190+
'WHERE' => [
191+
'is_active' => 1,
192+
'use_massimport' => 1,
193+
]
194+
];
195+
196+
$iterator = $DB->request($criteria);
197+
198+
foreach ($iterator as $ocsservers) {
188199
$result = SecondPass($tid, $ocsservers["id"], $thread_nbr, $threadid, $fields, $config);
189200
if ($result) {
190201
$fields = $result;
@@ -236,15 +247,20 @@ function FirstPass($ocsservers_id)
236247
} else {
237248
$max_id = 0;
238249
// Compute lastest synchronization date
239-
$query = "SELECT MAX(`last_ocs_update`)
240-
FROM `glpi_plugin_ocsinventoryng_ocslinks`
241-
WHERE `plugin_ocsinventoryng_ocsservers_id` = '$ocsservers_id'";
250+
$iterator = $DB->request([
251+
'SELECT' => [
252+
'MAX' => 'last_ocs_update AS max_last_ocs_update'
253+
],
254+
'FROM' => 'glpi_plugin_ocsinventoryng_ocslinks',
255+
'WHERE' => [
256+
'plugin_ocsinventoryng_ocsservers_id' => $ocsservers_id
257+
]
258+
]);
259+
242260
$max_date = "0000-00-00 00:00:00";
243-
if ($result = $DB->doQuery($query)) {
244-
if ($DB->numrows($result) > 0) {
245-
if ($DB->result($result, 0, 0) != '') {
246-
$max_date = $DB->result($result, 0, 0);
247-
}
261+
if (count($iterator) > 0) {
262+
foreach ($iterator as $data) {
263+
$max_date = $data['max_last_ocs_update'];
248264
}
249265
}
250266
}
@@ -408,25 +424,37 @@ function plugin_ocsinventoryng_importFromOcsServer(
408424
// Maybe add this to SOAP ?
409425
if (isset($ocsResult['COMPUTERS'])) {
410426
foreach ($ocsResult['COMPUTERS'] as $ID => $computer) {
411-
$query_glpi = "SELECT `glpi_plugin_ocsinventoryng_ocslinks`.`last_update` AS last_update,
412-
`glpi_plugin_ocsinventoryng_ocslinks`.`last_ocs_update` AS last_ocs_update,
413-
`glpi_plugin_ocsinventoryng_ocslinks`.`computers_id` AS computers_id,
414-
`glpi_computers`.`serial` AS serial,
415-
`glpi_plugin_ocsinventoryng_ocslinks`.`ocsid` AS ocsid,
416-
`glpi_computers`.`name` AS name,
417-
`glpi_plugin_ocsinventoryng_ocslinks`.`use_auto_update`,
418-
`glpi_plugin_ocsinventoryng_ocslinks`.`id`
419-
FROM `glpi_plugin_ocsinventoryng_ocslinks`
420-
LEFT JOIN `glpi_computers` ON (`glpi_computers`.`id`= `glpi_plugin_ocsinventoryng_ocslinks`.`computers_id`)
421-
WHERE `glpi_plugin_ocsinventoryng_ocslinks`.`plugin_ocsinventoryng_ocsservers_id`
422-
= $ocsServerId
423-
AND `glpi_plugin_ocsinventoryng_ocslinks`.`ocsid` = $ID
424-
ORDER BY `glpi_plugin_ocsinventoryng_ocslinks`.`use_auto_update` DESC,
425-
`last_update`,
426-
`name`";
427-
$result_glpi = $DB->doQuery($query_glpi);
428-
if ($DB->numrows($result_glpi) > 0) {
429-
while ($data = $DB->fetchAssoc($result_glpi)) {
427+
428+
$query_glpi = [
429+
'SELECT' => ['glpi_plugin_ocsinventoryng_ocslinks.last_update AS last_update',
430+
'glpi_plugin_ocsinventoryng_ocslinks.last_ocs_update AS last_ocs_update',
431+
'glpi_plugin_ocsinventoryng_ocslinks.computers_id AS computers_id',
432+
'glpi_computers.serial AS serial',
433+
'glpi_plugin_ocsinventoryng_ocslinks.ocsid AS ocsid',
434+
'glpi_computers.name AS name',
435+
'glpi_plugin_ocsinventoryng_ocslinks.use_auto_update',
436+
'glpi_plugin_ocsinventoryng_ocslinks.id'
437+
],
438+
'FROM' => 'glpi_plugin_ocsinventoryng_ocslinks',
439+
'LEFT JOIN' => [
440+
'glpi_computers' => [
441+
'ON' => [
442+
'glpi_computers' => 'id',
443+
'glpi_plugin_ocsinventoryng_ocslinks' => 'computers_id'
444+
]
445+
]
446+
],
447+
'WHERE' => [
448+
'glpi_plugin_ocsinventoryng_ocslinks.plugin_ocsinventoryng_ocsservers_id' => $ocsServerId,
449+
'glpi_plugin_ocsinventoryng_ocslinks.ocsid' => $ID,
450+
],
451+
'ORDERBY' => ['glpi_plugin_ocsinventoryng_ocslinks.use_auto_update ASC', 'last_update', 'name']
452+
];
453+
454+
$iterator = $DB->request($query_glpi);
455+
456+
if (count($iterator) > 0) {
457+
foreach ($iterator as $data) {
430458
if (strtotime($computer['META']["LASTDATE"]) > strtotime($data["last_update"])) {
431459
if ($ID <= intval($server->fields["max_ocsid"]) and (!$multiThread or ($ID % $thread_nbr) == ($threadid - 1))) {
432460
$ocsComputers[$ID] = $computer;
@@ -442,6 +470,7 @@ function plugin_ocsinventoryng_importFromOcsServer(
442470
$ocsComputers = array_splice($ocsComputers, $config->fields["import_limit"]);
443471
}
444472
}
473+
445474
$nb = count($ocsComputers);
446475
echo "\tThread #$threadid: $nb computer(s)\n";
447476

@@ -465,7 +494,8 @@ function plugin_ocsinventoryng_importFromOcsServer(
465494
$process_params = ['ocsid' => $ID,
466495
'plugin_ocsinventoryng_ocsservers_id' => $ocsServerId,
467496
'lock' => 1,
468-
'force' => 1];
497+
'force' => 1,
498+
'cron' => 1];
469499

470500
$action = OcsProcess::processComputer($process_params);
471501
OcsProcess::manageImportStatistics($fields, $action['status']);
@@ -489,18 +519,24 @@ function plugin_ocsinventoryng_importFromOcsServer(
489519
}
490520
}
491521

492-
$query = "SELECT MAX(`last_ocs_update`)
493-
FROM `glpi_plugin_ocsinventoryng_ocslinks`
494-
WHERE `plugin_ocsinventoryng_ocsservers_id` = '$ocsServerId'";
522+
$iterator = $DB->request([
523+
'SELECT' => [
524+
'MAX' => 'last_ocs_update AS max_last_ocs_update'
525+
],
526+
'FROM' => 'glpi_plugin_ocsinventoryng_ocslinks',
527+
'WHERE' => [
528+
'plugin_ocsinventoryng_ocsservers_id' => $ocsServerId
529+
]
530+
]);
531+
495532
$max_date = "0000-00-00 00:00:00";
496-
if ($result = $DB->doQuery($query)) {
497-
if ($DB->numrows($result) > 0) {
498-
if ($DB->result($result, 0, 0) != '') {
499-
$max_date = $DB->result($result, 0, 0);
500-
}
533+
if (count($iterator) > 0) {
534+
foreach ($iterator as $data) {
535+
$max_date = $data['max_last_ocs_update'];
501536
}
502537
}
503538

539+
504540
// Store for next synchro
505541
$server = new Server();
506542

public/scripts/ocsng_snmpfullsync.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@
140140
//$fields["id"] = $tid;
141141
$tid = $threadid;
142142

143+
global $DB;
144+
143145
if ($ocsservers_id != -1) {
144146
$result = launchSync($tid, $ocsservers_id, $thread_nbr, $threadid, $fields, $config);
145147
if ($result) {

setup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
global $CFG_GLPI;
5656

57-
define('PLUGIN_OCSINVENTORYNG_VERSION', '2.1.2');
57+
define('PLUGIN_OCSINVENTORYNG_VERSION', '2.1.3');
5858

5959
define("PLUGIN_OCSINVENTORYNG_STATE_STARTED", 1);
6060
define("PLUGIN_OCSINVENTORYNG_STATE_RUNNING", 2);

0 commit comments

Comments
 (0)