Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 335 | Rev 340 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
//declare(encoding='UTF-8');
/**
 * Exemple de lancement du script : :
 * /opt/lampp/bin/php cli.php bdtfx -a chargerTous
 *
 * @category    php 5.2
 * @package             eFlore/Scripts
 * @author              Jennifer DHÉ <jennifer@tela-botanica.org>
 * @author              Jean-Pascal MILCENT <jpm@tela-botanica.org>
 * @copyright   Copyright (c) 2011, Tela Botanica (accueil@tela-botanica.org)
 * @license             http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
 * @license             http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
 * @version             $Id$
 */
class Bdtfx extends EfloreScript {

        private $table = null;
        private $pasInsertion = 1000;
        private $departInsertion = 0;

        protected $parametres_autorises = array(
                '-t' => array(false, false, 'Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).'));

        public function executer() {
                try {
                        $this->initialiserProjet('bdtfx');

                        // Lancement de l'action demandée
                        $cmd = $this->getParametre('a');
                        switch ($cmd) {
                                case 'chargerTous' :
                                        $this->chargerStructureSql();
                                        $this->chargerBdtfx();
                                        $this->genererNomSciHtml();
                                        $this->genererDonneesTestMultiVersion();
                                        break;
                                case 'chargerStructureSql' :
                                        $this->chargerStructureSql();
                                        break;
                                case 'chargerBdtfx' :
                                        $this->chargerBdtfx();
                                        break;
                                case 'genererNomSciHtml' :
                                        $this->genererNomSciHtml();
                                        break;
                                case 'genererChpFamille' :
                                        $this->genererChpFamille();
                                        break;
                                case 'genererDonneesTestMultiVersion' :
                                        $this->genererDonneesTestMultiVersion();
                                        break;
                                case 'supprimerDonneesTestMultiVersion' :
                                        $this->supprimerDonneesTestMultiVersion();
                                        break;
                                case 'supprimerTous' :
                                        $this->supprimerTous();
                                        break;
                                default :
                                        throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
                        }
                } catch (Exception $e) {
                        $this->traiterErreur($e->getMessage());
                }
        }

        private function chargerBdtfx() {
                $chemin = Config::get('chemins.bdtfx');
                $table = Config::get('tables.bdtfx');
                $requete = "LOAD DATA INFILE '$chemin' ".
                                "REPLACE INTO TABLE $table ".
                                'CHARACTER SET utf8 '.
                                'FIELDS '.
                                "       TERMINATED BY '\t' ".
                                "       ENCLOSED BY '' ".
                                "       ESCAPED BY '\\\' ".
                                'IGNORE 1 LINES';
                $this->getBdd()->requeter($requete);
        }

        private function genererNomSciHtml() {
                $this->initialiserGenerationChamps();
                $this->preparerTablePrChpNomSciHtml();
                $generateur = new GenerateurNomSciHtml();
                $nbreTotal = $this->recupererNbTotalTuples();
                $this->departInsertion = 0;
                while ($this->departInsertion < $nbreTotal) {
                        $resultat = $this->recupererTuples();
                        $nomsSciEnHtml = $generateur->generer($resultat);
                        $this->lancerRequeteModification($nomsSciEnHtml);
                        $this->departInsertion += $this->pasInsertion;
                        $this->afficherAvancement("Insertion des noms scientifique au format HTML dans la base par paquet de {$this->pasInsertion} en cours");
                        if ($this->stopperLaBoucle($this->getParametre('t'))) break;
                }
                echo "\n";
        }

        private function initialiserGenerationChamps() {
                $this->table = Config::get('tables.bdtfx');
        }

        private function preparerTablePrChpNomSciHtml() {
                $requete = "SHOW COLUMNS FROM {$this->table} LIKE 'nom_sci_html' ";
                $resultat = $this->getBdd()->recuperer($requete);
                if ($resultat === false) {
                        $requete =      "ALTER TABLE {$this->table} ".
                                'ADD nom_sci_html VARCHAR( 500 ) '.
                                'CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ';
                        $this->getBdd()->requeter($requete);
                }
        }

        private function recupererNbTotalTuples(){
                $requete = "SELECT count(*) AS nb FROM {$this->table} ";
                $resultat = $this->getBdd()->recuperer($requete);
                return $resultat['nb'];
        }

        private function recupererTuples() {
                $requete = 'SELECT      num_nom, rang, nom_supra_generique, genre, epithete_infra_generique, '.
                        '       epithete_sp, type_epithete, epithete_infra_sp,cultivar_groupe, '.
                        '       nom_commercial, cultivar '.
                        "FROM {$this->table} ".
                        "LIMIT {$this->departInsertion},{$this->pasInsertion} ";
                $resultat = $this->getBdd()->recupererTous($requete);
                return $resultat;
        }

        private function lancerRequeteModification($nomsSciHtm) {
                foreach ($nomsSciHtm as $id => $html) {
                        $html = $this->getBdd()->proteger($html);
                        $requete = "UPDATE {$this->table} ".
                                "SET nom_sci_html = $html ".
                                "WHERE num_nom = $id ";
                        $resultat = $this->getBdd()->requeter($requete);
                        if ($resultat === false) {
                                throw new Exception("Erreur d'insertion pour le tuple $id");
                        }
                }
        }
        
        private function genererChpFamille() {
                $this->initialiserGenerationChamps();
                $this->preparerTablePrChpFamille();
                $resultats = $this->recupererTuplesPrChpFamille();
                $famille = array();
                $noms = array();
                $introuvables = array();
                foreach ($resultats as $nom) {
                        $nn = $nom['num_nom'];
                        $nts = $nom['num_tax_sup'];
                        $rg = $nom['rang'];
                        if ($rg == '180') {
                                $famille[$nn] = $nom['nom_sci'];
                        } else {
                                if (isset($noms[$nts])) {
                                        $noms[$nn] = $noms[$nts];
                                } else if (isset($famille[$nts])) {
                                        $noms[$nn] = $famille[$nts];
                                } else {
                                        $introuvables[] = $nn;
                                }
                        }
                        $this->afficherAvancement("Attribution de leur famille aux noms en cours");
                        if ($this->stopperLaBoucle($this->getParametre('t'))) break;
                }
                echo "\n";
                if ($introuvables != 0) {
                        $introuvables = implode(', ', $introuvables);
                        echo "Parent introuvable pour les noms : $introuvables\n";
                }
                $this->lancerRequeteModificationPrChpFamille($noms);
        }
        
        private function preparerTablePrChpFamille() {
                $requete = "SHOW COLUMNS FROM {$this->table} LIKE 'famille' ";
                $resultat = $this->getBdd()->recuperer($requete);
                if ($resultat === false) {
                        $requete =      "ALTER TABLE {$this->table} ".
                                'ADD famille VARCHAR(255) '.
                                'CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ';
                        $this->getBdd()->requeter($requete);
                }
        }
        
        private function recupererTuplesPrChpFamille() {
                $requete = 'SELECT num_nom, num_tax_sup, rang, nom_sci '.
                                "FROM {$this->table} ".
                                "WHERE rang >= 180 ".
                                "       AND num_tax_sup != '' ".
                                "ORDER BY rang ASC,num_tax_sup ASC ";
                $resultat = $this->getBdd()->recupererTous($requete);
                return $resultat;
        }
        
        private function lancerRequeteModificationPrChpFamille($noms) {
                foreach ($noms as $id => $famille) {
                        $famille = $this->getBdd()->proteger($famille);
                        $requete = "UPDATE {$this->table} ".
                                        "SET famille = $famille ".
                                        "WHERE num_nom = $id ";
                        $resultat = $this->getBdd()->requeter($requete);
                        if ($resultat === false) {
                                throw new Exception("Erreur d'insertion pour le tuple $id");
                        }
                        $this->afficherAvancement("Insertion des noms de famille dans la base en cours");
                }
                echo "\n";
        }

        private function genererDonneesTestMultiVersion() {
                $contenuSql = $this->recupererContenu(Config::get('chemins.structureSqlTest'));
                $this->executerScripSql($contenuSql);

                $table = Config::get('tables.bdtfx');
                $tableTest = Config::get('tables.bdtfxTest');
                $requete = "INSERT INTO $tableTest SELECT * FROM $table";
                $this->getBdd()->requeter($requete);
        }

        private function supprimerDonneesTestMultiVersion() {
                $tableMeta = Config::get('tables.bdtfxMeta');
                $requete = "DELETE FROM $tableMeta WHERE guid = 'urn:lsid:tela-botanica.org:bdtfx:1.02'";
                $this->getBdd()->requeter($requete);

                $tableTest = Config::get('tables.bdtfxTest');
                $requete = "DROP TABLE $tableTest";
                $this->getBdd()->requeter($requete);
        }

        private function supprimerTous() {
                $requete = "DROP TABLE bdtfx_meta, bdtfx_v1_01, bdtfx_v1_02";
                $this->getBdd()->requeter($requete);
        }
}
?>