You are not logged in.

Announcement

 Téléchargez la dernière version stable de GLPI      -     Et vous, que pouvez vous faire pour le projet GLPI ? :  Contribuer
 Download last stable version of GLPI                      -     What can you do for GLPI ? :  Contribute

#1 2020-07-01 09:33:48

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,146

APPLIANCES et GLPI 9.5 : portage vers le coeur ?

bonjour,
la version 9.5 de GLPI intègre nativement l'inventaire des applicatifs (sauf les champs personnalisables), cependant il n'y a pas de reprise des données

Comme l'intérêt de maintenir le plugin semble moins pertinent, peut on envisager une ultime version du plugin qui porterait les données vers le coeur ?

j'ai déjà travaillé sur ce portage des données ça semble fonctionner ( au moins pour la version 2.5.0 mais je pense à partr de la version 1.6 que c'est OK) :

INSERT INTO glpi_appliances(id,entities_id,is_recursive,name,is_deleted,appliancetypes_id,comment,locations_id,manufacturers_id,applianceenvironments_id,users_id,users_id_tech,
groups_id,groups_id_tech,relationtype,date_mod,states_id,externalidentifier,serial,otherserial) select id,entities_id,is_recursive,name,is_deleted,plugin_appliances_appliancetypes_id,comment,locations_id,0,plugin_appliances_environments_id,users_id,users_id_tech,
groups_id,groups_id_tech,relationtype,date_mod,states_id,externalid,serial,otherserial  FROM  glpi_plugin_appliances_appliances  WHERE 1=1;
INSERT INTO glpi_appliancerelations(id,appliances_items_id,relations_id) select  id,plugin_appliances_appliances_items_id,relations_id from glpi_plugin_appliances_relations wHERE 1=1;
insert into glpi_appliances_items (id,appliances_id,items_id,itemtype) SELECT id, plugin_appliances_appliances_id,items_id,itemtype FROM glpi_plugin_appliances_appliances_items WHERE 1=1;
INSERT INTO glpi_appliancetypes(id,entities_id,is_recursive,name,comment,externalidentifier) SELECT id,entities_id,is_recursive,name,comment,externalid FROM glpi_plugin_appliances_appliancetypes WHERE 1=1;
INSERT INTO glpi_applianceenvironments (id,name,comment) SELECT id,name,comment FROM glpi_plugin_appliances_environments WHERE 1=1;
update glpi_items_tickets set itemtype='Appliance' where itemtype='PluginAppliancesAppliance';
update glpi_items_problems set itemtype='Appliance' where itemtype='PluginAppliancesAppliance';
update glpi_items_projects set itemtype='Appliance' where itemtype='PluginAppliancesAppliance';
update glpi_profiles set helpdesk_item_type=replace(helpdesk_item_type,'PluginAppliancesAppliance','Appliance') where 1=1;
update  glpi_infocoms set itemtype='Appliance' where itemtype='pluginAppliancesappliance';
update  glpi_logs set itemtype='Appliance' where itemtype='pluginAppliancesappliance';
update glpi_documents_items set itemtype='Appliance' where itemtype='pluginAppliancesappliance';
update glpi_contracts_items set itemtype='Appliance' where itemtype='pluginAppliancesappliance';
update glpi_knowbaseitems_items set itemtype='Appliance' where itemtype='pluginAppliancesappliance';

j'ai pu passer directement ces requêtes en sql et c'est ok, je les retrouve dans l'inventaire, dans les tickets etc...



pour aller plus loin et faire un truc plus propre je tente de passer mes requêtes dans un script de migration  :j'ai un message d'erreur "duplicate index" que je ne comprends pas (mais les applicatifs sont insérés dans la table ).

voici ce qui ne marche pas (bien sûr ma table est vide) : ( ce sont mes premiers essais d'écriture de script de migration glpi, il peut y avoir des erreurs de débutants)

if ($DB->tableExists('glpi_plugin_appliances_appliances')) {
   $query = "INSERT INTO glpi_appliances(id,entities_id,is_recursive,name,is_deleted,appliancetypes_id,comment,locations_id,manufacturers_id,applianceenvironments_id,users_id,users_id_tech,groups_id,groups_id_tech,relationtype,date_mod,states_id,externalidentifier,serial,otherserial)
 select id,entities_id,is_recursive,name,is_deleted,plugin_appliances_appliancetypes_id,comment,locations_id,0,plugin_appliances_environments_id,users_id,users_id_tech,
groups_id,groups_id_tech,relationtype,date_mod,states_id,externalid,serial,otherserial  
FROM  glpi_plugin_appliances_appliances  
WHERE 1=1;";
   $DB->queryOrDie($query, "9.5 add table glpi_appliancerelations");
}

Trouver la panne avant de réparer...
GLPI10.0.10 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.2 reports 1.16.0 formcreator 2.13.8, datainjection 2.13.4 fields 1.21.6

Offline

#2 2020-07-01 12:01:57

btry
Moderator
Registered: 2015-10-01
Posts: 588

Re: APPLIANCES et GLPI 9.5 : portage vers le coeur ?

Bonjour,

J'ai l'impression qu'il y a un souci dans le code PHP. Vous testez l'inexistence d'une table du plugin appliance, puis l'utilisez dans une requête INSERT ... SELECT.

Pour moi le test devrait être la négation de l'expression.

Note importante : revue faite depuis un smartphone. J'espère avoir bien lu.


No support with MP - Always run an upgrade task on a testing instance before running it on production! Always backup before any upgrade task!

Offline

#3 2020-07-01 12:12:06

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,146

Re: APPLIANCES et GLPI 9.5 : portage vers le coeur ?

c'est une erreur en recopiant sur le forum, mais sur mon serveur c'est bien  : si la table existe ( donc si le plugin est installé) on copie les données du plugin dans le coeur. et c'est ça qui m'indique que l'index existe déjà alors que la table est bien vide.


Trouver la panne avant de réparer...
GLPI10.0.10 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.2 reports 1.16.0 formcreator 2.13.8, datainjection 2.13.4 fields 1.21.6

Offline

#4 2020-07-01 22:41:08

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,146

Re: APPLIANCES et GLPI 9.5 : portage vers le coeur ?

je vais plutôt m'inspirer des scripts de migration du plugin et creer un passage vers la version 9.5.0
il me reste à gérer les marque pages

mes requêtes dans glpi/plugins/appliances/sql/update-9.5.0.sql
une modif du setup pour mettre à jour le N° de version et les version glpi compatibles
une modif du hook pour ajouter  : $DB->runFile(GLPI_ROOT ."/plugins/appliances/sql/update-9.5.0.sql");  dans la fonction install

jusque là ça marche les applis sont migrées dans le coeur, associés aux tickets, problèmes projets, lrs profils sont aussi modifiés mais il faut encore des "finitions"

un message d'alerte précisant que la manip est irréversible
quelques tests pour que une fois installé on ne puisse pas relancer la migration et supprimer les tables du plugin.


Trouver la panne avant de réparer...
GLPI10.0.10 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.2 reports 1.16.0 formcreator 2.13.8, datainjection 2.13.4 fields 1.21.6

Offline

#5 2020-07-10 07:29:41

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,146

Re: APPLIANCES et GLPI 9.5 : portage vers le coeur ?

je viens de voisr que pour le plugin domain la migration des data du plugin vers le coeur se fait via la console.
je vais explorer cette piste pour  appliances


Trouver la panne avant de réparer...
GLPI10.0.10 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.2 reports 1.16.0 formcreator 2.13.8, datainjection 2.13.4 fields 1.21.6

Offline

#6 2020-07-10 13:45:50

btry
Moderator
Registered: 2015-10-01
Posts: 588

Re: APPLIANCES et GLPI 9.5 : portage vers le coeur ?

Bonjour


Une migration par CLI sera moins pénible à mettre au point.


No support with MP - Always run an upgrade task on a testing instance before running it on production! Always backup before any upgrade task!

Offline

#7 2020-07-12 18:39:11

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,146

Re: APPLIANCES et GLPI 9.5 : portage vers le coeur ?

j'aime les défis, ça fait progresser


Trouver la panne avant de réparer...
GLPI10.0.10 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.2 reports 1.16.0 formcreator 2.13.8, datainjection 2.13.4 fields 1.21.6

Offline

#8 2020-07-14 17:17:24

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,146

Re: APPLIANCES et GLPI 9.5 : portage vers le coeur ?

/!\ attention c'est un script pas officiel, testez le bien si vous voulez l'utiliser. /!\

J'ai ecrit un script fonctionnel pour migrer les données du plugin dans le coeur : il y a quelques limitations parce que je ne comprends pas l'utilisation du mapping, alors j'utilise les mêmes it pour le plugin et pour le coeur : ça signifie qu'il faut lancer le script avant la creation manuelle l'applicatifs dans le coeur sinon ils sont perdus.
on perd les champs personnalisés qui n'existent pas dans le coeur
le script permet de garder le lien avec les tickets, problèmes changements, de conserver les onglets  (y compris gestion et log)



je crée un fichier :  glpi/inc/console/migration/appliancesplugintocorecommand.class.php   dans lequel je colle le code ci desssous
je lance la console : php bin/console glpi:migration:appliances_plugin_to_core

il manque encore quelques traductions
EDIT : mise à jour  suite au PR déposé sur github

<?php
/**
 * ---------------------------------------------------------------------
 * GLPI - Gestionnaire Libre de Parc Informatique
 * Copyright (C) 2015-2020 Teclib' and contributors.
 * Yves TESNIERE
 * http://glpi-project.org
 *
 * based on GLPI - Gestionnaire Libre de Parc Informatique
 * Copyright (C) 2003-2014 by the INDEPNET Development Team.
 *
 * ---------------------------------------------------------------------
 *
 * LICENSE
 *
 * This file is part of GLPI.
 *
 * GLPI is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GLPI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GLPI. If not, see <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------
 */
namespace Glpi\Console\Migration;

if (!defined('GLPI_ROOT')) {
    die("Sorry. You can't access this file directly");
}

use CommonDBTM;
use DB;
use Plugin;
use Appliance;
use ApplianceType;
use ApplianceRelation;
use ApplianceEnvironment;
use Appliance_Item;
use State;
use Toolbox;
use Glpi\Console\AbstractCommand;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Question\ChoiceQuestion;

class AppliancesPluginToCoreCommand extends AbstractCommand
{

    /**
     * Error code returned if plugin version or plugin data is invalid.
     *
     * @var integer
     */
    const ERROR_PLUGIN_VERSION_OR_DATA_INVALID = 1;

    /**
     * Error code returned if import failed.
     *
     * @var integer
     */
    const ERROR_PLUGIN_IMPORT_FAILED = 1;

    /**
* list of usefull plugin tables and fields
     *
     *@var array
     **/

    const PLUGIN_APPLIANCE_TABLES=[
        "glpi_plugin_appliances_appliances"=>["id","entities_id","is_recursive","name","is_deleted",
      "plugin_appliances_appliancetypes_id","comment","locations_id","plugin_appliances_environments_id","users_id","users_id_tech",
      "groups_id","groups_id_tech","relationtype","date_mod","states_id","externalid","serial","otherserial"],
        "glpi_plugin_appliances_appliancetypes"=>["id","entities_id","is_recursive","name","comment"],
        "glpi_plugin_appliances_appliances_items"=>["id", "plugin_appliances_appliances_id","items_id","itemtype"],
        "glpi_plugin_appliances_environments"=>["id","name","comment" ],
        "glpi_plugin_appliances_relations"=>["id","plugin_appliances_appliances_items_id","relations_id"]
    ];

    /**
     * itemtype corresponding to appliance in plugin
     *
     *@var string
     **/
    const PLUGIN_APPLIANCE_ITEMTYPE="pluginAppliancesappliance";

    /**
     * itemtype corresponding to appliance in core
     *
     *@var string
     **/
    const CORE_APPLIANCE_ITEMTYPE="Appliance";

    protected function configure() {
        parent::configure();

        $this->setName('glpi:migration:appliances_plugin_to_core');
        $this->setDescription(__('Migrate Appliances plugin data into GLPI core tables'));

        $this->addOption(
            'skip-errors',
            's',
            InputOption::VALUE_NONE,
            __('Do not exit on import errors')
        );
    }

    protected function execute(InputInterface $input, OutputInterface $output) {
        $no_interaction = $input->getOption('no-interaction');
       if (!$no_interaction) {
            // Ask for confirmation (unless --no-interaction)
            $output->writeln(
                [
                __('You are about to launch migration of Appliances plugin data into GLPI core tables.'),
                __('Any previous appliance created in core will be lost.'),
                __('It is better to make a backup of your existing data before continuing.')
                ]
            );

            /**
   * @var QuestionHelper $question_helper
*/
            $question_helper = $this->getHelper('question');
            $run = $question_helper->ask(
                $input,
                $output,
                new ConfirmationQuestion(
                    '<comment>' . __('Do you want to launch migration ?') . ' [yes/No]</comment>',
                    false
                )
            );
          if (!$run) {
                $output->writeln(
                    '<comment>' . __('Migration aborted.') . '</comment>',
                    OutputInterface::VERBOSITY_VERBOSE
                );
                 return 0;
          }
       }

       if (!$this->checkPlugin()) {
            return self::ERROR_PLUGIN_VERSION_OR_DATA_INVALID;
       }

       if (!$this->migratePlugin()) {
            return self::ERROR_PLUGIN_IMPORT_FAILED;
       }

        $output->writeln('<info>' . __('Migration done.') . '</info>');
        return 0; // Success
    }

    /**
     * Check that  required tables exists and fields are OK for migration.
     *
     * @throws LogicException
     *
     * @return boolean
     */
    private function checkPlugin() {

        $missing_tables = false;
       foreach (self::PLUGIN_APPLIANCE_TABLES as $table=>$fields) {
          if (!$this->db->tableExists($table)) {
                $this->output->writeln(
                    '<error>' . sprintf(__('Appliances plugin table "%s" is missing.'), $table) . '</error>',
                    OutputInterface::VERBOSITY_QUIET
                );
                $missing_tables = true;
          } else {
             foreach ($fields as $field) {
                if (!$this->db->fieldExists($table, $field)) {
                        $this->output->writeln(
                            '<error>' . sprintf(__('Appliances plugin field "%s" is missing.'), $table.'.'.$field) . '</error>',
                            OutputInterface::VERBOSITY_QUIET
                        );
                        $missing_tables = true;

                };
             }
          }
       }
       if ($missing_tables) {
            $this->output->writeln(
                '<error>' . __('Migration cannot be done.') . '</error>',
                OutputInterface::VERBOSITY_QUIET
            );
            return false;
       }

        return true;
    }

    /**
     * Clean data from core tables.
     *
     * @throws RuntimeException
     */
    private function cleanCoreTables() {

        $core_tables = [
         'glpi_appliances',
        'glpi_appliancerelations',
        'glpi_appliancetypes',
        'glpi_applianceenvironments',
        'glpi_appliances_items'
        ];

        foreach ($core_tables as $table) {
            $result = $this->db->query('TRUNCATE ' . DB::quoteName($table));

           if (!$result) {
                throw new RuntimeException(
                    sprintf('Unable to truncate table "%s"', $table)
                );
           }
        }
    }
    /**
     * copy data from plugin to core keeping same ID.
     *
     * @throws LogicException
     *
     * @return boolean
     */

    private  function backupPluginTables() {

       foreach (self::PLUGIN_APPLIANCE_TABLES as $table=>$fields) {
            $result = $this->db->query(sprintf('ALTER TABLE %s RENAME %s ', DB::quotename($table), DB::quotename('backup_'.$table)));
          if (false === $result) {
                $message = sprintf(
                    __('Migration of table "%s"  failed with message "(%s) %s".'),
                    $table,
                    $this->db->errno(),
                    $this->db->error()
                );
                $this->output->writeln(
                    '<error>' . $message . '</error>',
                    OutputInterface::VERBOSITY_QUIET
                );
                return self::ERROR_TABLE_MIGRATION_FAILED;
          }
       }

                $this->output->writeln(
                    '<info>' . __('plugin tables renamed as backup_*** can be deleted...') . '</info>',
                    OutputInterface::VERBOSITY_NORMAL
                );
        return true;
    }
    /**
     * copy plugin tables to backup tables from plugin to core keeping same ID.
     *
     * @throws LogicException
     *
     * @return boolean
     */

    private  function migratePlugin() {

        $no_interaction = $this->input->getOption('no-interaction');

        $skip_errors = $this->input->getOption('skip-errors');

        $this->cleanCoreTables();

        $failure = !$this->createApplianceType()
        || !$this->createApplianceEnvironment()
        || !$this->createApplianceRelation()
        || !$this->createApplianceItem()
        || !$this->createAppliance()
        || !$this->updateInfocoms()
        || !$this->updateItemType()
        || !$this->updateProfilesApplianceRights()
        || !$this->backupPluginTables();

        return !$failure;

    }
    private function updateInfocoms() {

        $table='glpi_infocoms';
        $where = [
         'itemtype'  => self::CORE_APPLIANCE_ITEMTYPE,
        ];
        $result = $this->db->delete($table, $where);

        if (false === $result) {
            $message = sprintf(
                __('Migration of table "%s"  failed with message "(%s) %s".'),
                $table,
                $this->db->errno(),
                $this->db->error()
            );
            $this->output->writeln(
                '<error>' . $message . '</error>',
                OutputInterface::VERBOSITY_QUIET
            );
             return self::ERROR_TABLE_MIGRATION_FAILED;
        }

        return 1;
    }

    private function updateProfilesApplianceRights() {
        $table='glpi_profiles';
        $result = $this->db->query(sprintf('update %s set helpdesk_item_type=replace(helpdesk_item_type,\''.self::PLUGIN_APPLIANCE_ITEMTYPE.'\',\''.self::CORE_APPLIANCE_ITEMTYPE.'\')', DB::quotename($table)));
       if (false === $result) {
            $message = sprintf(
                __('Migration of table "%s"  failed with message "(%s) %s".'),
                $table,
                $this->db->errno(),
                $this->db->error()
            );
            $this->output->writeln(
                '<error>' . $message . '</error>',
                OutputInterface::VERBOSITY_QUIET
            );
            return self::ERROR_TABLE_MIGRATION_FAILED;
       }

        return 1;
    }
    private function updateItemType() {
        $itemtype_tables=[
        'glpi_items_tickets',
        'glpi_items_problems',
        'glpi_items_projects',
        'glpi_logs',
        'glpi_infocoms',
        'glpi_documents_items',
        'glpi_contracts_items',
        'glpi_knowbaseitems_items'
        ];
        foreach ($itemtype_tables as $itemtype_table) {

            $where = [
              'itemtype'  => self::PLUGIN_APPLIANCE_ITEMTYPE,
            ];
            $params = [
                 'itemtype'  => self::CORE_APPLIANCE_ITEMTYPE,
            ];

            $result = $this->db->update($itemtype_table, $params, $where);

            if (false === $result) {
                $message = sprintf(
                    __('Migration of table "%s"  failed with message "(%s) %s".'),
                    $itemtype_table,
                    $this->db->errno(),
                    $this->db->error()
                );
                $this->output->writeln(
                    '<error>' . $message . '</error>',
                    OutputInterface::VERBOSITY_QUIET
                );
                return self::ERROR_TABLE_MIGRATION_FAILED;
            }
        }
        return 1;
    }
    private function createApplianceItem() {
        /*   insert into glpi_appliances_items (id,appliances_id,items_id,itemtype)
        SELECT id, plugin_appliances_appliances_id,items_id,itemtype
        FROM glpi_plugin_appliances_appliances_items WHERE 1=1;*/

        $this->output->writeln(
            '<comment>' . __('Creating Appliance Items...') . '</comment>',
            OutputInterface::VERBOSITY_VERBOSE
        );
        $this->output->writeln(
            '<info>' . __('Creating Appliance Items...') . '</info>',
            OutputInterface::VERBOSITY_NORMAL
        );
        $plugin_appliances_appliances_items  = $this->db->request(
            [
             'FROM'  => 'glpi_plugin_appliances_appliances_items'
            ]
        );
       foreach ($plugin_appliances_appliances_items AS $plugin_appliances_appliances_item) {
            $app = new applianceitem();
            $app_fields = toolbox::sanitize(
                [
                     'id'=>$plugin_appliances_appliances_item['id'],
                     'appliances_id'=>$plugin_appliances_appliances_item['plugin_appliances_appliances_id'],
                     'items_id'=>$plugin_appliances_appliances_item['items_id'],
                     'itemtype'=>$plugin_appliances_appliances_item['itemtype']
                  ]
            );

          if (!($app_id = $app->getFromDBByCrit($app_fields))) {
                $app_id = $app->add($app_fields);
          }

          if (false === $app_id) {
                $this->outputImportError(
                    '<error>' . __('Unable to create Appliance item .') . '</error>'
                );
                return null;
          }

       }
       return 1;
    }
    private function createApplianceEnvironment() {
        /*INSERT INTO glpi_applianceenvironments (id,name,comment) SELECT id,name,comment
        FROM glpi_plugin_appliances_environments WHERE 1=1;*/
        $this->output->writeln(
            '<comment>' . __('Creating Appliance Environment...') . '</comment>',
            OutputInterface::VERBOSITY_VERBOSE
        );
        $this->output->writeln(
            '<info>' . __('Creating Appliance Environment...') . '</info>',
            OutputInterface::VERBOSITY_NORMAL
        );
        $plugin_appliances_environments  = $this->db->request(
            [
                  'FROM'  => 'glpi_plugin_appliances_environments'
               ]
        );
       foreach ($plugin_appliances_environments AS $plugin_appliances_environment) {
                 $app = new applianceenvironment();
                $app_fields = toolbox::sanitize(
                    [
                     'id'=>$plugin_appliances_environment['id'],
                     'name'=>$plugin_appliances_environment['name'],
                     'comment'=>$plugin_appliances_environment['comment']
                    ]
                );

          if (!($app_id = $app->getFromDBByCrit($app_fields))) {
                $app_id = $app->add($app_fields);
          }
                    $this->output->writeln(
                        '<info>' . $plugin_appliances_environment['name']. '</info>',
                        OutputInterface::VERBOSITY_NORMAL
                    );
          if (false === $app_id) {
                $this->outputImportError(
                    '<error>' . __('Unable to create Appliance environment .') . '</error>'
                );
                return null;
          }

       }
       return 1;
    }
    private function createAppliance() {
        /*INSERT INTO glpi_appliances(id,entities_id,is_recursive,name,is_deleted,appliancetypes_id,comment,locations_id,manufacturers_id,applianceenvironments_id,
        users_id,users_id_tech,groups_id,groups_id_tech,relationtype,date_mod,states_id,externalidentifier,serial,otherserial)
        select id,entities_id,is_recursive,name,is_deleted,
        plugin_appliances_appliancetypes_id,comment,locations_id,0,plugin_appliances_environments_id,users_id,users_id_tech,
        groups_id,groups_id_tech,relationtype,date_mod,states_id,externalid,serial,otherserial  FROM  glpi_plugin_appliances_appliances*/
        $this->output->writeln(
            '<comment>' . __('Creating Appliance...') . '</comment>',
            OutputInterface::VERBOSITY_VERBOSE
        );
        $this->output->writeln(
            '<info>'. __('Creating Appliance...') . '</info>',
            OutputInterface::VERBOSITY_NORMAL
        );
            $plugin_appliances_appliances  = $this->db->request(
                [
                  'FROM'  => 'glpi_plugin_appliances_appliances'
                ]
            );
       foreach ($plugin_appliances_appliances AS $plugin_appliances_appliance) {
            $app = new appliance();
            $app_fields = toolbox::sanitize(
                [
                     'id'=>$plugin_appliances_appliance['id'],
                     'entities_id'=>$plugin_appliances_appliance['entities_id'],
                'is_recursive'=>$plugin_appliances_appliance['is_recursive'],
                     'name'=>$plugin_appliances_appliance['name'],
                     'is_deleted'=>$plugin_appliances_appliance['is_deleted'],
                     'appliancetypes_id'=>$plugin_appliances_appliance['plugin_appliances_appliancetypes_id'],
                     'comment'=>$plugin_appliances_appliance['comment'],
                     'locations_id'=>$plugin_appliances_appliance['locations_id'],
                     'manufacturers_id'=>'0',
                     'applianceenvironments_id'=>$plugin_appliances_appliance['plugin_appliances_environments_id'],
                     'users_id'=>$plugin_appliances_appliance['users_id'],
                     'users_id_tech'=>$plugin_appliances_appliance['users_id_tech'],
                     'groups_id'=>$plugin_appliances_appliance['groups_id'],
                     'groups_id_tech'=>$plugin_appliances_appliance['groups_id_tech'],
                     'date_mod'=>$plugin_appliances_appliance['date_mod'],
                     'states_id'=>$plugin_appliances_appliance['states_id'],
                     'externalidentifier'=>$plugin_appliances_appliance['externalid'],
                     'serial'=>$plugin_appliances_appliance['serial'],
                     'otherserial'=>$plugin_appliances_appliance['otherserial']
                  ]
            );

          if (!($app_id = $app->getFromDBByCrit($app_fields))) {
                $app_id = $app->add($app_fields);
          }
            $this->output->writeln(
                '<info>**********' . $plugin_appliances_appliance['name']. '</info>',
                OutputInterface::VERBOSITY_NORMAL
            );
          if (false === $app_id) {
                    $this->outputImportError(
                        '<error>' . __('Unable to create Appliance .') . '</error>'
                    );
                     return null;
          }
       }
        return 1;
    }
    private function createApplianceType() {
        /*INSERT INTO glpi_appliancetypes(id,entities_id,is_recursive,name,comment,externalidentifier)
        SELECT id,entities_id,is_recursive,name,comment,externalid FROM glpi_plugin_appliances_appliancetype*/

        $this->output->writeln(
            '<comment>' . __('Creating Appliance types...') . '</comment>',
            OutputInterface::VERBOSITY_VERBOSE
        );
        $this->output->writeln(
            '<info>' . __('Creating Appliance types...') . '</info>',
            OutputInterface::VERBOSITY_NORMAL
        );

            $plugin_appliances_types = $this->db->request(
                [
                  'FROM'  => 'glpi_plugin_appliances_appliancetypes'
                ]
            );
       foreach ($plugin_appliances_types AS $plugin_appliances_type) {

            $name=$plugin_appliances_type['name'];
            $appt = new appliancetype();
            $appt_fields = Toolbox::sanitize(
                [
                     'id'=>$plugin_appliances_type['id'],
                     'entities_id'=>$plugin_appliances_type['entities_id'],
                'is_recursive'=>$plugin_appliances_type['is_recursive'],
                     'name'=>$plugin_appliances_type['name'],
                     'comment'=>$plugin_appliances_type['comment'],
                     'externalidentifier'=>$plugin_appliances_type['externalid']
                  ]
            );

          if (!($appt_id = $appt->getFromDBByCrit($appt_fields))) {
                $appt_id = $appt->add($appt_fields);
          }

          if (false === $appt_id) {
                $this->outputImportError(
                    '<error>' . __('Unable to create Appliance type.') . '</error>'
                );
                return null;
          }

       }
        return 1;

    }
    private function createApplianceRelation() {
        /*INSERT INTO glpi_appliancerelations(id,appliances_items_id,relations_id) select  id,plugin_appliances_appliances_items_id,relations_id
        from glpi_plugin_appliances_relations wHERE 1=1;*/
        $this->output->writeln(
            '<comment>' . __('Creating Appliance relations...') . '</comment>',
            OutputInterface::VERBOSITY_VERBOSE
        );

            $plugin_appliances_relations = $this->db->request(
                [
                  'FROM'  => 'glpi_plugin_appliances_relations'
                ]
            );

       foreach ($plugin_appliances_relations AS $plugin_appliances_relation) {
                    $this->output->writeln(
                        '<info> createApplianceRelation 2</info>',
                        OutputInterface::VERBOSITY_NORMAL
                    );

            $appr = new appliancerelation();
            $appr_fields = toolbox::sanitize(
                [
                'id'=>$plugin_appliances_relation['id'],
                'appliances_items_id'=>$plugin_appliances_relation['plugin_appliances_appliances_items_id'],
                'relations_id'=>$plugin_appliances_relation['relations_id']
                  ]
            );
          if (!($appr_id = $appr->getFromDBByCrit($appr_fields))) {
                $appr_id = $appr->add($appr_fields);
          }

          if (false === $appr_id) {
                $this->outputImportError(
                    '<error>' . __('Unable to create Appliance Relation.') . '</error>'
                );
                return null;
          }

       }

        return 1;

    }


    /**
     * Returns verbosity level for import errors.
     *
     * @return number
     */
    private function getImportErrorsVerbosity() {

        return $this->input->getOption('skip-errors')
         ? OutputInterface::VERBOSITY_NORMAL
         : OutputInterface::VERBOSITY_QUIET;
    }

    /**
     * Output import error message.
     *
     * @param string           $message
     * @param ProgressBar|null $progress_bar
     *
     * @return void
     */
    private function outputImportError($message, ProgressBar $progress_bar = null) {

        $skip_errors = $this->input->getOption('skip-errors');

        $verbosity = $skip_errors
         ? OutputInterface::VERBOSITY_NORMAL
         : OutputInterface::VERBOSITY_QUIET;

        $message = '<error>' . $message . '</error>';

       if ($skip_errors && $progress_bar instanceof ProgressBar) {
            $this->writelnOutputWithProgressBar(
                $message,
                $progress_bar,
                $verbosity
            );
       } else {
          if (!$skip_errors && $progress_bar instanceof ProgressBar) {
                $this->output->write(PHP_EOL); // Keep progress bar last state and go to next line
          }
            $this->output->writeln(
                $message,
                $verbosity
            );
       }
    }
}

Trouver la panne avant de réparer...
GLPI10.0.10 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.2 reports 1.16.0 formcreator 2.13.8, datainjection 2.13.4 fields 1.21.6

Offline

#9 2020-07-16 14:26:52

orthagh
Administrator
From: TECLIB - CAEN
Registered: 2010-11-30
Posts: 662
Website

Re: APPLIANCES et GLPI 9.5 : portage vers le coeur ?

@LaDenrée quand vous serez confiant sur le script, nous apprécierons un envoi de PR sur le projet wink

Offline

#10 2020-07-17 10:23:00

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,146

Re: APPLIANCES et GLPI 9.5 : portage vers le coeur ?

je relis les consignes de codage  :
https://glpi-developer-documentation.re … dards.html

et j'essaie de faire mon premier PR, je regarde les tutos  et je me lance....


Trouver la panne avant de réparer...
GLPI10.0.10 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.2 reports 1.16.0 formcreator 2.13.8, datainjection 2.13.4 fields 1.21.6

Offline

#11 2020-07-17 11:28:11

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,146

Re: APPLIANCES et GLPI 9.5 : portage vers le coeur ?

j'ai fait mon premier PR mais j'ai plein d'erreu PHP CS principalement indentation. je corrige


Trouver la panne avant de réparer...
GLPI10.0.10 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.2 reports 1.16.0 formcreator 2.13.8, datainjection 2.13.4 fields 1.21.6

Offline

#12 2020-07-17 16:58:51

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,146

Re: APPLIANCES et GLPI 9.5 : portage vers le coeur ?

je me casse les dents sur une ligne que je n'arrive pas à corriger :
2 messages :  "erreur il faut un espace", "erreur il y a un espace"  ???????? 


FILE: ...ct/inc/console/migration/appliancesplugintocorecommand.class.php

----------------------------------------------------------------------

FOUND 2 ERRORS AFFECTING 1 LINE

----------------------------------------------------------------------

182 | ERROR | [x] Expected 1 space after closing brace; newline

     |       |     found

182 | ERROR | [x] Whitespace found at end of line


Trouver la panne avant de réparer...
GLPI10.0.10 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.2 reports 1.16.0 formcreator 2.13.8, datainjection 2.13.4 fields 1.21.6

Offline

#13 2020-07-17 17:11:28

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,146

Re: APPLIANCES et GLPI 9.5 : portage vers le coeur ?

ça y est j'ai trouvé : un saut de ligne avant mon "else"   grr....

il me reste à gérer les traductions  .


question : y a t il un moyen de tester le fichier avant de faire un commit ?  j'ai du faire 10 commits uniquement  pour corriger  quelques espaces et lignes vides et les indentations en espace et pas en tabulations.


Trouver la panne avant de réparer...
GLPI10.0.10 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.2 reports 1.16.0 formcreator 2.13.8, datainjection 2.13.4 fields 1.21.6

Offline

#14 2020-09-01 10:33:09

helgedopler
Member
Registered: 2020-08-04
Posts: 1

Re: APPLIANCES et GLPI 9.5 : portage vers le coeur ?

I have the feeling that there is an issue in the PHP code. For me the test ought to be the refutation of the articulation.

Offline

Board footer

Powered by FluxBB