Subversion Repositories eFlore/Projets.eflore-projets

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
477 jpm 1
<?php
2
class Wiki {
3
 
4
	const URL_WIKI = 'http://www.tela-botanica.org/wikini/florecoste/api/rest/0.5/pages';
478 jpm 5
	const URL_WIKI_EFLORE = 'http://www.tela-botanica.org/wikini/Coste/api/rest/0.5/pages';
477 jpm 6
	const DOSSIER_V2 = '../../../donnees/coste/2.00/';
7
	const DOSSIER_DSC_TXT = '../../../donnees/coste/descriptions/txt/';
8
 
9
	private $conteneur = null;
10
	private $outils = null;
11
	private $messages = null;
12
	private $restClient = null;
13
	private $dossierBase = '';
14
	private $index = array();
15
 
16
	public function __construct(Conteneur $conteneur) {
17
		mb_internal_encoding('UTF-8');
18
		setlocale(LC_ALL, 'fr_FR.UTF-8');
19
		$this->conteneur = $conteneur;
20
		$this->outils = $conteneur->getOutils();
21
		$this->messages = $conteneur->getMessages();
22
		$this->restClient = $conteneur->getRestClient();
23
		$this->dossierBase = dirname(__FILE__).'/';
24
	}
25
 
26
	public function uploaderFichiersSp() {
27
		$this->chargerIndex();
28
		$envoyes = array();
29
		foreach ($this->index as $nom) {
30
			$tag = $nom['page_wiki_dsc'];
31
			if (isset($envoyes[$tag]) == false && preg_match('/^Esp([0-9]{4})/', $tag, $match)) {
32
				$fichier = $this->dossierBase.self::DOSSIER_DSC_TXT.$match[1].'.txt';
33
				if (file_exists($fichier) === true) {
34
					$txt = file_get_contents($fichier);
35
					$donnees = array('pageTag' => $tag, 'pageContenu' => $txt);
36
					$this->restClient->ajouter(self::URL_WIKI, $donnees);
37
					$envoyes[$tag] = 'OK';
38
				} else {
39
					$this->messages->traiterErreur("Le fichier $fichier est introuvable.");
40
				}
41
			}
478 jpm 42
			$this->messages->afficherAvancement("Upload des fichiers d'espèce dans le wikini");
477 jpm 43
		}
478 jpm 44
		echo "\n";
477 jpm 45
	}
46
 
47
	public function dowloaderPagesWiki() {
48
		$this->chargerIndex();
49
		$envoyes = array();
50
		foreach ($this->index as $nom) {
51
			$tagDsc = $nom['page_wiki_dsc'];
52
			$tagCle = $nom['page_wiki_cle'];
53
			if (isset($envoyes[$tagDsc]) == false) {
54
				$url = self::URL_WIKI.'/'.$tagDsc;
55
				$txt = $this->telechargerTxt($url);
56
				$fichier = $this->dossierBase.self::DOSSIER_V2.'/dsc/'.$tagDsc.'.txt';
57
				if (file_put_contents($fichier, $txt)) {
58
					$envoyes[$tagDsc] = 'OK';
59
				}
60
			}
61
			if (isset($envoyes[$tagCle]) == false) {
62
				$url = self::URL_WIKI.'/'.$tagCle;
63
				$txt = $this->telechargerTxt($url);
64
				$fichier = $this->dossierBase.self::DOSSIER_V2.'/cle/'.$tagCle.'.txt';
65
				if (file_put_contents($fichier, $txt)) {
66
					$envoyes[$tagCle] = 'OK';
67
				}
68
			}
69
			$this->messages->afficherAvancement("Download des fichiers en cours");
70
		}
478 jpm 71
		echo "\n";
477 jpm 72
	}
73
 
478 jpm 74
	public function uploaderDansWikiEflore() {
75
		$this->chargerIndex();
76
		$envoyes = array();
77
		foreach ($this->index as $nom) {
78
			$tagDsc = $nom['page_wiki_dsc'];
79
			if (isset($envoyes[$tagDsc]) == false) {
80
				$fichier = $this->dossierBase.self::DOSSIER_V2.'/dsc/'.$tagDsc.'.txt';
81
				if (file_exists($fichier) === true) {
82
					$txt = file_get_contents($fichier);
83
					$donnees = array('pageTag' => $tagDsc, 'pageContenu' => $txt);
84
					$this->restClient->ajouter(self::URL_WIKI_EFLORE, $donnees);
85
					$envoyes[$tagDsc] = 'OK';
86
				} else {
87
					$this->messages->traiterErreur("Le fichier $fichier est introuvable.");
88
				}
89
			}
90
 
91
			$tagCle = $nom['page_wiki_cle'];
92
			if (isset($envoyes[$tagCle]) == false) {
93
				$fichier = $this->dossierBase.self::DOSSIER_V2.'/cle/'.$tagCle.'.txt';
94
				if (file_exists($fichier) === true) {
95
					$txt = file_get_contents($fichier);
96
					$donnees = array('pageTag' => $tagCle, 'pageContenu' => $txt);
97
					$this->restClient->ajouter(self::URL_WIKI_EFLORE, $donnees);
98
					$envoyes[$tagCle] = 'OK';
99
				} else {
100
					$this->messages->traiterErreur("Le fichier $fichier est introuvable.");
101
				}
102
			}
103
			$this->messages->afficherAvancement("Upload des textes dans le wikini eFlore");
104
		}
105
		echo "\n";
106
	}
107
 
477 jpm 108
	private function telechargerTxt($url) {
109
		$json = $this->restClient->consulter($url);
110
		$donnees = json_decode($json, true);
111
		return $donnees['texte'];
112
	}
113
 
114
	private function chargerIndex() {
115
		$indexTxt = $this->dossierBase.self::DOSSIER_V2.'index.tsv';
116
		$this->index = $this->outils->transformerTxtTsvEnTableau($indexTxt);
117
	}
118
}
119
?>