Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 931 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
/** Exemple lancement:
 * /opt/lampp/bin/php -d memory_limit=3500M cli.php sptb -a chargerTous
*/
class ListeRouge extends EfloreScript {

        public function executer() {
                // Lancement de l'action demandée
                try {
                        $this->initialiserProjet('liste_rouge');

                        $cmd = $this->getParametre('a');
                        switch ($cmd) {
                                case 'chargerTous' :
                                        $this->chargerStructureSql();
                                        $this->chargerDonnees();
                                        $this->genererChampNumNomRetenu();
                                        break;
                                case 'chargerStructureSql' :
                                        $this->chargerStructureSql();
                                        break;
                                case 'chargerDonnees' :
                                        $this->chargerDonnees();
                                        $this->genererChampNumNomRetenu();
                                        break;
                                case 'genererChampNumNomRetenu' :
                                        $this->genererChampNumNomRetenu();
                                        break;
                                case 'genererChampLegende' :
                                    $this->genererChampLegende();
                                    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 chargerDonnees() {
                $chemin = Config::get('chemins.listeRouge');
                $table = Config::get('tables.listeRouge');
                $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 genererChampNumNomRetenu() {
                $this->preparerTablePrChpNumNomRetenu();
                $this->genererNumNomRetenu();
                $this->recupererNumNomNonTrouve();
        }
        
        private function preparerTablePrChpNumNomRetenu() {
                $table = Config::get('tables.listeRouge');
                $requete = "SHOW COLUMNS FROM $table LIKE 'num_nom_retenu' ";
                $resultat = $this->getBdd()->recuperer($requete);
                if ($resultat === false) {
                        $requete =      "ALTER TABLE $table ".
                                        ' ADD `num_nom_retenu` VARCHAR( 10 ) NOT NULL, '.
                                        ' ADD INDEX ( `num_nom_retenu` ) ';
                        $this->getBdd()->requeter($requete);
                }
        }
        
        private function genererNumNomRetenu() {
                $table = Config::get('tables.listeRouge');
                $table_referentiel = Config::get('tables.referentielTaxo');
                $requete = "SHOW COLUMNS FROM $table_referentiel";
                $resultat = $this->getBdd()->recuperer($requete);
                if ($resultat === true) {
                $requete = 'UPDATE '.$table.' s, '.$table_referentiel.' r '.
                                        'SET s.num_nom_retenu = r.num_nom '.
                                        'WHERE s.cd_nom = r.cd_nom ';
                
                $this->getBdd()->requeter($requete);
                } else {
                    Debug::printr('Il manque la table de liaison entre taxref et bdtfx');
                }
        }
        
        private function genererCategories() {
            $table = Config::get('tables.listeRouge');
            $table_categories = Config::get('tables.categories');
            $requete = "SHOW COLUMNS FROM $table_categories LIKE 'categorie' ";
            $resultat = $this->getBdd()->recuperer($requete);
            if ($resultat === true) {
                $requete = 'UPDATE '.$table.' s, '.$table_categories.' r '.
                        'SET s.categorie = r.categorie '.
                        'WHERE s.num_categorie = r.num_categorie ';
                
                $this->getBdd()->requeter($requete);
            } else {
                Debug::printr('Il manque la table de liaison entre taxref et bdtfx');
            }
        }
        
        private function supprimerTous() {
                $requete = "DROP TABLE IF EXISTS liste_rouge_meta, liste_rouge_v2012";
                $this->getBdd()->requeter($requete);
                Debug::printr('suppression');
        }
}
?>