Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

<?php
class Wiki {

        const URL_WIKI = 'http://www.tela-botanica.org/wikini/florecoste/api/rest/0.5/pages';
        const URL_WIKI_EFLORE = 'http://www.tela-botanica.org/wikini/Coste/api/rest/0.5/pages';
        const DOSSIER_V2 = '../../../donnees/coste/2.00/';
        const DOSSIER_DSC_TXT = '../../../donnees/coste/descriptions/txt/';

        private $conteneur = null;
        private $outils = null;
        private $messages = null;
        private $restClient = null;
        private $dossierBase = '';
        private $index = array();

        public function __construct(Conteneur $conteneur) {
                mb_internal_encoding('UTF-8');
                setlocale(LC_ALL, 'fr_FR.UTF-8');
                $this->conteneur = $conteneur;
                $this->outils = $conteneur->getOutils();
                $this->messages = $conteneur->getMessages();
                $this->restClient = $conteneur->getRestClient();
                $this->dossierBase = dirname(__FILE__).'/';
        }

        public function uploaderFichiersSp() {
                $this->chargerIndex();
                $envoyes = array();
                foreach ($this->index as $nom) {
                        $tag = $nom['page_wiki_dsc'];
                        if (isset($envoyes[$tag]) == false && preg_match('/^Esp([0-9]{4})/', $tag, $match)) {
                                $fichier = $this->dossierBase.self::DOSSIER_DSC_TXT.$match[1].'.txt';
                                if (file_exists($fichier) === true) {
                                        $txt = file_get_contents($fichier);
                                        $donnees = array('pageTag' => $tag, 'pageContenu' => $txt);
                                        $this->restClient->ajouter(self::URL_WIKI, $donnees);
                                        $envoyes[$tag] = 'OK';
                                } else {
                                        $this->messages->traiterErreur("Le fichier $fichier est introuvable.");
                                }
                        }
                        $this->messages->afficherAvancement("Upload des fichiers d'espèce dans le wikini");
                }
                echo "\n";
        }

        public function dowloaderPagesWiki() {
                $this->chargerIndex();
                $envoyes = array();
                foreach ($this->index as $nom) {
                        $tagDsc = $nom['page_wiki_dsc'];
                        $tagCle = $nom['page_wiki_cle'];
                        if (isset($envoyes[$tagDsc]) == false) {
                                $url = self::URL_WIKI.'/'.$tagDsc;
                                $txt = $this->telechargerTxt($url);
                                $fichier = $this->dossierBase.self::DOSSIER_V2.'/dsc/'.$tagDsc.'.txt';
                                if (file_put_contents($fichier, $txt)) {
                                        $envoyes[$tagDsc] = 'OK';
                                }
                        }
                        if (isset($envoyes[$tagCle]) == false) {
                                $url = self::URL_WIKI.'/'.$tagCle;
                                $txt = $this->telechargerTxt($url);
                                $fichier = $this->dossierBase.self::DOSSIER_V2.'/cle/'.$tagCle.'.txt';
                                if (file_put_contents($fichier, $txt)) {
                                        $envoyes[$tagCle] = 'OK';
                                }
                        }
                        $this->messages->afficherAvancement("Download des fichiers en cours");
                }
                echo "\n";
        }

        public function uploaderDansWikiEflore() {
                $this->chargerIndex();
                $envoyes = array();
                foreach ($this->index as $nom) {
                        $tagDsc = $nom['page_wiki_dsc'];
                        if (isset($envoyes[$tagDsc]) == false) {
                                $fichier = $this->dossierBase.self::DOSSIER_V2.'/dsc/'.$tagDsc.'.txt';
                                if (file_exists($fichier) === true) {
                                        $txt = file_get_contents($fichier);
                                        $donnees = array('pageTag' => $tagDsc, 'pageContenu' => $txt);
                                        $this->restClient->ajouter(self::URL_WIKI_EFLORE, $donnees);
                                        $envoyes[$tagDsc] = 'OK';
                                } else {
                                        $this->messages->traiterErreur("Le fichier $fichier est introuvable.");
                                }
                        }

                        $tagCle = $nom['page_wiki_cle'];
                        if (isset($envoyes[$tagCle]) == false) {
                                $fichier = $this->dossierBase.self::DOSSIER_V2.'/cle/'.$tagCle.'.txt';
                                if (file_exists($fichier) === true) {
                                        $txt = file_get_contents($fichier);
                                        $donnees = array('pageTag' => $tagCle, 'pageContenu' => $txt);
                                        $this->restClient->ajouter(self::URL_WIKI_EFLORE, $donnees);
                                        $envoyes[$tagCle] = 'OK';
                                } else {
                                        $this->messages->traiterErreur("Le fichier $fichier est introuvable.");
                                }
                        }
                        $this->messages->afficherAvancement("Upload des textes dans le wikini eFlore");
                }
                echo "\n";
        }

        private function telechargerTxt($url) {
                $json = $this->restClient->consulter($url);
                $donnees = json_decode($json, true);
                return $donnees['texte'];
        }

        private function chargerIndex() {
                $indexTxt = $this->dossierBase.self::DOSSIER_V2.'index.tsv';
                $this->index = $this->outils->transformerTxtTsvEnTableau($indexTxt);
        }
}
?>