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';
5
	const DOSSIER_V2 = '../../../donnees/coste/2.00/';
6
	const DOSSIER_DSC_TXT = '../../../donnees/coste/descriptions/txt/';
7
 
8
	private $conteneur = null;
9
	private $outils = null;
10
	private $messages = null;
11
	private $restClient = null;
12
	private $dossierBase = '';
13
	private $index = array();
14
 
15
	public function __construct(Conteneur $conteneur) {
16
		mb_internal_encoding('UTF-8');
17
		setlocale(LC_ALL, 'fr_FR.UTF-8');
18
		$this->conteneur = $conteneur;
19
		$this->outils = $conteneur->getOutils();
20
		$this->messages = $conteneur->getMessages();
21
		$this->restClient = $conteneur->getRestClient();
22
		$this->dossierBase = dirname(__FILE__).'/';
23
	}
24
 
25
	public function uploaderFichiersSp() {
26
		$this->chargerIndex();
27
		$envoyes = array();
28
		foreach ($this->index as $nom) {
29
			$tag = $nom['page_wiki_dsc'];
30
			if (isset($envoyes[$tag]) == false && preg_match('/^Esp([0-9]{4})/', $tag, $match)) {
31
				$fichier = $this->dossierBase.self::DOSSIER_DSC_TXT.$match[1].'.txt';
32
				if (file_exists($fichier) === true) {
33
					$txt = file_get_contents($fichier);
34
					$donnees = array('pageTag' => $tag, 'pageContenu' => $txt);
35
					$this->restClient->ajouter(self::URL_WIKI, $donnees);
36
					$envoyes[$tag] = 'OK';
37
				} else {
38
					$this->messages->traiterErreur("Le fichier $fichier est introuvable.");
39
				}
40
			}
41
			$this->messages->afficherAvancement("Upload des fichiers dans le wikini");
42
		}
43
	}
44
 
45
	public function dowloaderPagesWiki() {
46
		$this->chargerIndex();
47
		$envoyes = array();
48
		foreach ($this->index as $nom) {
49
			$tagDsc = $nom['page_wiki_dsc'];
50
			$tagCle = $nom['page_wiki_cle'];
51
			if (isset($envoyes[$tagDsc]) == false) {
52
				$url = self::URL_WIKI.'/'.$tagDsc;
53
				$txt = $this->telechargerTxt($url);
54
				$fichier = $this->dossierBase.self::DOSSIER_V2.'/dsc/'.$tagDsc.'.txt';
55
				if (file_put_contents($fichier, $txt)) {
56
					$envoyes[$tagDsc] = 'OK';
57
				}
58
			}
59
			if (isset($envoyes[$tagCle]) == false) {
60
				$url = self::URL_WIKI.'/'.$tagCle;
61
				$txt = $this->telechargerTxt($url);
62
				$fichier = $this->dossierBase.self::DOSSIER_V2.'/cle/'.$tagCle.'.txt';
63
				if (file_put_contents($fichier, $txt)) {
64
					$envoyes[$tagCle] = 'OK';
65
				}
66
			}
67
			$this->messages->afficherAvancement("Download des fichiers en cours");
68
		}
69
	}
70
 
71
	private function telechargerTxt($url) {
72
		$json = $this->restClient->consulter($url);
73
		$donnees = json_decode($json, true);
74
		return $donnees['texte'];
75
	}
76
 
77
	private function chargerIndex() {
78
		$indexTxt = $this->dossierBase.self::DOSSIER_V2.'index.tsv';
79
		$this->index = $this->outils->transformerTxtTsvEnTableau($indexTxt);
80
	}
81
}
82
?>