Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 1074 | 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 cel -a chargerTous
 *
 * @category    php 5.2
 * @package             eFlore/Scripts
 * @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 Cel extends EfloreScript {

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

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

        protected function initialiserProjet($projet) {
                $bases = $this->getListeBases();
                parent::initialiserProjet($projet);
                $this->verifierPresenceBdd($bases);
        }

        private function getListeBases() {
                $requete = "SHOW DATABASES";
                $bases = $this->getBdd()->recupererTous($requete);
                return $bases;
        }

        private function verifierPresenceBdd($bases) {
                $bddNom = Config::get('bdd_nom');
                $existe = false;
                foreach ($bases as $base) {
                        if ($base['Database'] == $bddNom) {
                                $existe = true;
                                break;
                        }
                }
                if ($existe === false) {
                        $message = "Veuillez créer la base de données '$bddNom'.";
                        throw new Exception($message);
                }
        }

        public function chargerCel() {
                $tablesCodes = array_keys(Config::get('tables'));
                foreach ($tablesCodes as $code) {
                        echo "Chargement de la table : $code\n";
                        $this->chargerFichierTsvDansTable($code);
                }
        }

        private function chargerFichierTsvDansTable($code) {
                $chemin = Config::get('chemins.'.$code);
                $table = Config::get('tables.'.$code);
                $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 supprimerTous() {
                $requete = "DROP TABLE IF EXISTS cel_meta, cel_images, cel_inventory, cel_mots_cles_images, cel_mots_cles_obs, ".
                        "cel_obs_images, locations ";
                $this->getBdd()->requeter($requete);
        }
        
        private function genererScriptTableReference() {
                $script_sql = file_get_contents(__DIR__.'/cel_references.tpl.sql');
                $config_references = Config::get('references');
                foreach($config_references as $a_remplacer => $remplacement) {
                        $script_sql = str_replace($a_remplacer, $remplacement, $script_sql);
                        file_put_contents(__DIR__.'/cel_references.sql', $script_sql);
                }
        }
}
?>