2929 */
3030
3131use GlpiPlugin \Ocsinventoryng \Config ;
32+ use GlpiPlugin \Ocsinventoryng \Detail ;
3233use GlpiPlugin \Ocsinventoryng \Notimportedcomputer ;
3334use GlpiPlugin \Ocsinventoryng \OcsProcess ;
3435use GlpiPlugin \Ocsinventoryng \OcsServer ;
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 ();
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" ;
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
0 commit comments