Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

<?php
/** Exemple lancement:
 * /opt/lampp/bin/php -d memory_limit=3500M ~/web/eflore-projets/scripts/cli.php nva -a chargerTous
*/
class Nva extends EfloreScript {
        
        public function executer() {
                // Lancement de l'action demandée
                try {
                        $this->initialiserProjet('nva');

                        $cmd = $this->getParametre('a');
                    switch ($cmd) {
                        case 'chargerTous' :
                                $this->chargerStructureSql();
                                $this->chargerDonnees();
                                $this->genererChampCodeLangue();
                                $this->genererChampNumTax();
                                break;
                        case 'chargerStructureSql' :
                                $this->chargerStructureSql();
                                break;
                                case 'chargerDonnees' :
                                        $this->chargerDonnees();
                                        break;
                                case 'genererChampNumTax' :
                                        $this->genererChampNumTax();
                                        break;
                                case 'genererChampCodeLangue' :
                                        $this->genererChampCodeLangue();
                                        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.nva');
                $table = Config::get('tables.nva');
                $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 genererChampNumTax() {
                $this->preparerTablePrChpNumTax();
                $nns_taxons = $this->recupererTuplesPrNumTax();
                $this->remplirChpNumTax($nns_taxons);
        }
        
        private function preparerTablePrChpNumTax() {
                $table = Config::get('tables.nva');
                $requete = "SHOW COLUMNS FROM $table LIKE 'num_taxon' ";
                $resultat = $this->getBdd()->recuperer($requete);
                if ($resultat === false) {
                        $requete =      "ALTER TABLE $table ".
                                        'ADD num_taxon INT(10) ';
                        $this->getBdd()->requeter($requete);
                }
        }
        
        private function recupererTuplesPrNumTax() {
                $table_ref = Config::get('tables.bdtxa');
                $requete = 'SELECT  num_tax, num_nom '.
                                        "FROM ".$table_ref." ";
                $resultat = $this->getBdd()->recupererTous($requete);
                return $resultat;
        }
        
        private function remplirChpNumTax($nns_taxons) {
                $table = Config::get('tables.nva');
                foreach ($nns_taxons as $nn) {
                        if (!empty($nn['num_tax']) && is_numeric($nn['num_tax'])) {
                                $requete = "UPDATE $table SET num_taxon = ".$nn['num_tax']." WHERE num_nom = ".$nn['num_nom']." ";
                                $resultat = $this->getBdd()->requeter($requete);
                        }
                        if ($resultat === false) {
                                throw new Exception("Erreur d'insertion pour le tuple $id");
                        }
                        $this->afficherAvancement("Insertion des nums taxonomiques dans la base en cours");
                }
                echo "\n";
        }
        
        private function genererChampCodeLangue() {
                $this->preparerTablePrChpCodeLangue();
                $codes_langues = $this->recupererTuplesPrCodeLangue();
                $this->remplirChpCodeLangue($codes_langues);
        }
        
        private function preparerTablePrChpCodeLangue() {
                $table = Config::get('tables.nva');
                $requete = "SHOW COLUMNS FROM $table LIKE 'code_langue' ";
                $resultat = $this->getBdd()->recuperer($requete);
                if ($resultat === false) {
                        $requete =      "ALTER TABLE $table ".
                                                'ADD code_langue VARCHAR(3) ';
                        $this->getBdd()->requeter($requete);
                }
        }
        
        private function recupererTuplesPrCodeLangue() {
                $codes_langues = array();
                if (($handle = fopen(Config::get("dossierTsv")."codes_langues.csv", "r")) !== false) {
                    while (($data = fgetcsv($handle, 1000, ",")) !== false) {
                        $codes_langues[] = array('code_iso_639_3' => $data[0],
                                                                'nom' => $data[1]);
                    }
                    fclose($handle);
                }
                                
                return $codes_langues;
        }
        
        private function remplirChpCodeLangue($codes_langues) {
                $table = Config::get('tables.nva');
                foreach ($codes_langues as $code) {
                        if (!empty($code['nom'])) {
                                $requete = "UPDATE $table SET code_langue = ".$this->getBdd()->proteger($code['code_iso_639_3'])." ".
                                                   "WHERE TRIM(langue) = ".$this->getBdd()->proteger(trim($code['nom']))." ";
                                $resultat = $this->getBdd()->requeter($requete);
                        }
                        if ($resultat === false) {
                                throw new Exception("Erreur d'insertion pour le tuple $id");
                        }
                        $this->afficherAvancement("Insertion des codes de langues dans la base en cours");
                }
                echo "\n";
        }

        private function supprimerTous() {
                $requete = "DROP TABLE IF EXISTS ".Config::get('tables.nva').", nva_meta ";
                $this->getBdd()->requeter($requete);
        }
}
?>