Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

<?php
class Wiki {
        const DOSSIER_V2 = '../../../donnees/coste/2.00/';
        const DOSSIER_DSC_TXT = '../../../donnees/coste/descriptions/txt/';

        private $URL_WIKI = '';
        private $URL_WIKI_EFLORE = '';
        private $conteneur = null;
        private $outils = null;
        private $messages = null;
        private $restClient = null;
        private $bdd = 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->URL_WIKI = $this->conteneur->getParametre('wiki.travail');
                $this->URL_WIKI_EFLORE = $this->conteneur->getParametre('wiki.eflore');
                $this->outils = $conteneur->getOutils();
                $this->messages = $conteneur->getMessages();
                $this->restClient = $conteneur->getRestClient();
                $this->bdd = $conteneur->getBdd();
                $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($this->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 = $this->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 = $this->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);
                                        $this->envoyerPage($tagDsc, $txt);
                                        $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);
                                        $this->envoyerPage($tagCle, $txt);
                                        $envoyes[$tagCle] = 'OK';
                                } else {
                                        $this->messages->traiterErreur("Le fichier $fichier est introuvable.");
                                }
                        }
                        $this->messages->afficherAvancement("Upload des textes dans le wikini eFlore");
                }
                echo "\n";
        }

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

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

        public function chargerIndexDansWikiEflore() {
                $index = $this->creerIndex();
                foreach ($index as $titre => $txt) {
                        $this->enregistrerPage($titre, $txt);
                }
        }

        public function uploaderIndexDansWikiEflore() {
                $index = $this->creerIndex();
                foreach ($index as $titre => $txt) {
                        $this->envoyerPage($titre, $txt);
                }
        }

        public function creerIndex() {
                $this->chargerIndex();
                $envoyes = array();
                $pageIndexFamille = "==Index Famille Coste==\n\n";
                $pageIndexGenre = "==Index Genre Coste==\n\n";
                $pageIndexEspece = "==Index Espèce Coste==\n\n";
                foreach ($this->index as $nom) {
                        $indentation = $this->getIndentationTxtLien($nom);
                        $txtLien = $this->getTxtLienGenerique($nom);
                        $img = $nom['image'];
                        $tagDsc = $nom['page_wiki_dsc'];
                        if (isset($envoyes[$tagDsc]) == false) {
                                //$fichier = $this->dossierBase.self::DOSSIER_V2.'dsc/'.$tagDsc.'.txt';
                                if ($nom['rang'] <= 180) {
                                        $pageIndexFamille .= "$indentation- [[$tagDsc $txtLien]]\n";
                                } elseif ($nom['rang'] == 220) {
                                        $pageIndexGenre .= "$indentation- [[$tagDsc $txtLien]]\n";
                                } else {
                                        $pageIndexEspece .= "$indentation- [[$tagDsc $txtLien]] - [[http://www.tela-botanica.org/eflore/donnees/coste/2.00/img/$img]]\n";
                                }
                                $envoyes[$tagDsc] = 'OK';
                        }

                        $tagCle = $nom['page_wiki_cle'];
                        if (isset($envoyes[$tagCle]) == false) {
                                //$fichier = $this->dossierBase.self::DOSSIER_V2.'cle/'.$tagCle.'.txt';
                                $indentation = $indentation.' ';
                                if ($nom['rang'] <= 180) {
                                        $pageIndexFamille = rtrim($pageIndexFamille, "\n");
                                        $pageIndexFamille .= " - [[$tagCle Clé]]\n";
                                } elseif ($nom['rang'] == 220) {
                                        $pageIndexGenre = rtrim($pageIndexGenre, "\n");
                                        $pageIndexGenre .= " - [[$tagCle Clé]]\n";
                                }
                                $envoyes[$tagCle] = 'OK';
                        }
                        $this->messages->afficherAvancement("Création des pages d'index pour le wikini eFlore");
                }
                echo "\n";

                $index = array('IndexFamille' => $pageIndexFamille, 'IndexGenre' => $pageIndexGenre, 'IndexEspece' => $pageIndexEspece);
                return $index;
        }

        private function getIndentationTxtLien($nom) {
                $rangs = array('Reg' => 1, 'Emb' => 2, 'Cla' => 3, 'Fam' => 4, 'Gen' => 5, 'Esp' => 6);
                $indentation = '';
                if (preg_match('/^(Reg|Emb|Cla|Fam|Gen|Esp)[0-9]+/', $nom['page_wiki_dsc'], $match)) {
                        $type = $match[1];
                        $indentation = str_repeat(' ', $rangs[$type]);
                }
                return $indentation;
        }

        private function getTxtLienGenerique($nom) {
                $rangs = array('Reg' => 'Règne', 'Emb' => 'Embranchement', 'Cla' => 'Classe', 'Fam' => 'Famille',
                        'Gen' => 'Genre', 'Esp' => 'Espèce');
                $nomSci = $nom['nom_sci'];
                $nomCoste = $nom['nom_coste'];
                $txtLien = '';
                if (preg_match('/^(Reg|Emb|Cla|Fam|Gen|Esp)([0-9]*)/', $nom['page_wiki_dsc'], $match)) {
                        $numCoste = $match[2];
                        $type = $match[1];
                        $nomRang = $rangs[$type];
                        $nom = ($type == 'Fam') ? $nomCoste : $nomSci;
                        $txtLien = "$nomRang $numCoste - $nom";
                }
                return $txtLien;
        }

        private function envoyerPage($titre, $txt) {
                $donnees = array('pageTag' => $titre, 'pageContenu' => $txt);
                $this->restClient->ajouter($this->URL_WIKI_EFLORE, $donnees);
        }

        private function enregistrerPage($titre, $txt) {
                $titre = $this->bdd->proteger($titre);
                $time = $this->bdd->proteger(date('Y-m-d H:i:s'));
                $txt = $this->bdd->proteger($txt);
                $requete = "INSERT INTO `coste_pages` (`tag`, `time`, `body`, `body_r`, `owner`, `user`, `latest`, `handler`, `comment_on`) VALUES ".
                                "($titre, $time, $txt, '', '', 'ScriptEflore', 'Y', 'page', '')";
                $this->bdd->requeter($requete);
        }

        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.'coste_v2_00.tsv';
                $this->index = $this->outils->transformerTxtTsvEnTableau($indexTxt);
        }
}
?>