Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 349 | Blame | Last modification | View Log | RSS feed

<?php
//declare(encoding='UTF-8');
/**
 * Classe permettant de :
 *  - rajouter l'objet point centroide de la commune.
 *  - charger la bdd
 * Exemple de lancement du script : :
 * /opt/lampp/bin/php cli.php wikipedia -a chargerTous
 *
 * @category    php 5.2
 * @package             eFlore/Scripts
 * @author              Mohcen BENMOUNAH <mohcen@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 Wikipedia extends EfloreScript {
        private $tableMeta = '';
        private $cheminFichierMeta = '';
        private $tableCommunes = '';
        private $cheminFichierCommunes = '';

        public function executer() {
                try {
                        $this->initialiserProjet('wikipedia');
                        $this->tableMeta = Config::get('tables.wikipediaMeta');
                        $this->cheminFichierMeta = Config::get('chemins.wikipediaMeta');
                        $this->tableCommunes = Config::get('tables.wikipediaCommunes');
                        $this->cheminFichierCommunes = Config::get('chemins.wikipediaCommunes');

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

        protected function chargerMetaDonnees() {
                $contenuSql = $this->recupererContenu($this->cheminFichierMeta);
                $this->executerScripSql($contenuSql);
        }

        private function chargerWikipediaCommunes() {
                $requete = "LOAD DATA INFILE '{$this->cheminFichierCommunes}' ".
                        "REPLACE INTO TABLE {$this->tableCommunes} ".
                        'CHARACTER SET utf8 '.
                        'FIELDS '.
                        "       TERMINATED BY ',' ".
                        "       ENCLOSED BY '\"' ".
                        "       ESCAPED BY '\\\' ".
                        'IGNORE 1 LINES';
                $this->getBdd()->requeter($requete);
        }

        private function preparerTable() {
                $requete =      "ALTER TABLE {$this->tableCommunes} ".
                                        'DROP centroide ';
                $this->getBdd()->requeter($requete);

                $requete =      "ALTER TABLE {$this->tableCommunes} ".
                                        '       ADD centroide point NOT NULL ';
                $this->getBdd()->requeter($requete);

                $requete =      "ALTER TABLE {$this->tableCommunes} ".
                                        '       ADD INDEX (centroide) ';
                $this->getBdd()->requeter($requete);
        }

        private function recupererPoints() {
                $requete =      'SELECT * '.
                                        "FROM {$this->tableCommunes} ";
                $LatLons = $this->getBdd()->recupererTous($requete);

                foreach ($LatLons as $LatLon) {
                        $latitude_degre = $LatLon['latitude'];
                        $longitude_degre = $LatLon['longitude'];
                        $insee = $LatLon['code_insee'];
                        $this->formerPointCentre($latitude_degre, $longitude_degre, $insee);
                        $this->afficherAvancement('Analyse des communes Wikipedia');
                }
        }

        private function formerPointCentre($latitude_degre, $longitude_degre, $insee) {
                $centre = "$latitude_degre $longitude_degre";
                $requete = "UPDATE {$this->tableCommunes} ".
                                        "SET centroide = POINTFROMTEXT('POINT($centre)') ".
                                        "WHERE code_insee = $insee ";
                $this->getBdd()->requeter($requete);
        }

        private function supprimerTous() {
                $requete = "DROP TABLE IF EXISTS {$this->tableMeta}, {$this->tableCommunes}";
                $this->getBdd()->requeter($requete);
        }
}
?>