Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 11 | Rev 54 | 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 nomSciHtml -t bdtfx_v1_01
 *  Classe permettant de créer le nom scientifique au format HTML.
 * @category    php 5.2
 * @package             bdtfx
 * @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 Script {

        protected $bdd = null;
        protected $table = null;
        private $projetNom = 'bdtfx';

        protected $pasInsertion = 1000;
        protected $departInsertion = 0;

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

        public function executer() {
                try {
                        $this->initialiser();

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

        private function initialiser() {
                $this->chargerConfigDuProjet();
                $this->bdd = new Bdd();
        }

        private function chargerConfigDuProjet() {
                $fichierIni = dirname(__FILE__).DS.$this->projetNom.'.ini';
                if (file_exists($fichierIni)) {
                        Config::charger($fichierIni);
                } else {
                        $m = "Veuillez configurer le projet en créant le fichier '{$this->projetNom}.ini' ".
                                "dans le dossier du module de script du projet à partir du fichier '{$this->projetNom}.defaut.ini'.";
                        throw new Exception($m);
                }
        }

        private function chargerStructureSql() {
                $chemin = Config::get('chemins.structureSql');
                $requetes = Outils::extraireRequetes($chemin);
                foreach ($requetes as $requete) {
                        $this->bdd->requeter($requete);
                }
        }

        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->bdd->requeter($requete);
        }

        private function genererNomSciHtml() {
                $this->initialiserGenerationNomsSciHtml();
                $this->preparerTable();
                $generateur = new GenerateurNomSciHtml();
                $nbreTotal = $this->recupererNbTotalTuples();
                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()) {
                                break;
                        }
                }
                echo "\n";
        }

        private function initialiserGenerationNomsSciHtml() {
                require_once dirname(__FILE__).DS.'GenerateurNomSciHtml.php';
                echo dirname(__FILE__).DS.'GenerateurNomSciHtml.php';
                $this->table = Config::get('tables.bdtfx');
        }

        private function preparerTable() {
                $requete = "SHOW COLUMNS FROM {$this->table} LIKE 'nom_sci_html' ";
                $resultat = $this->bdd->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->bdd->requeter($requete);
                }
        }

        private function recupererNbTotalTuples(){
                $requete = "SELECT count(*) AS nb FROM {$this->table} ";
                $resultat = $this->bdd->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->bdd->recupererTous($requete);
                return $resultat;
        }

        private function lancerRequeteModification($nomsSciHtm) {
                foreach ($nomsSciHtm as $id => $html) {
                        $html = $this->bdd->proteger($html);
                        $requete = "UPDATE {$this->table} ".
                                "SET nom_sci_html = $html ".
                                "WHERE num_nom = $id ";
                        $resultat = $this->bdd->requeter($requete);
                        if ($resultat === false) {
                                throw new Exception("Erreur d'insertion pour le tuple $id");
                        }
                }
        }

        private function stopperLaBoucle() {
                $stop = false;
                static $ligneActuelle = 1;
                if ($nbreLignesATester = $this->getParametre('t')) {
                        if ($nbreLignesATester == $ligneActuelle++) {
                                $stop = true;
                        }
                }
                return $stop;
        }
}
?>