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
	const DOSSIER_V2 = '../../../donnees/coste/2.00/';
4
	const DOSSIER_DSC_TXT = '../../../donnees/coste/descriptions/txt/';
5
 
484 jpm 6
	private $URL_WIKI = '';
7
	private $URL_WIKI_EFLORE = '';
477 jpm 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');
484 jpm 18
		$this->conteneur = $conteneur;
19
		$this->URL_WIKI = $this->conteneur->getParametre('wiki.travail');
20
		$this->URL_WIKI_EFLORE = $this->conteneur->getParametre('wiki.eflore');
477 jpm 21
		$this->outils = $conteneur->getOutils();
22
		$this->messages = $conteneur->getMessages();
23
		$this->restClient = $conteneur->getRestClient();
24
		$this->dossierBase = dirname(__FILE__).'/';
25
	}
26
 
27
	public function uploaderFichiersSp() {
28
		$this->chargerIndex();
29
		$envoyes = array();
30
		foreach ($this->index as $nom) {
31
			$tag = $nom['page_wiki_dsc'];
32
			if (isset($envoyes[$tag]) == false && preg_match('/^Esp([0-9]{4})/', $tag, $match)) {
33
				$fichier = $this->dossierBase.self::DOSSIER_DSC_TXT.$match[1].'.txt';
34
				if (file_exists($fichier) === true) {
35
					$txt = file_get_contents($fichier);
36
					$donnees = array('pageTag' => $tag, 'pageContenu' => $txt);
484 jpm 37
					$this->restClient->ajouter($this->URL_WIKI, $donnees);
477 jpm 38
					$envoyes[$tag] = 'OK';
39
				} else {
40
					$this->messages->traiterErreur("Le fichier $fichier est introuvable.");
41
				}
42
			}
478 jpm 43
			$this->messages->afficherAvancement("Upload des fichiers d'espèce dans le wikini");
477 jpm 44
		}
478 jpm 45
		echo "\n";
477 jpm 46
	}
47
 
48
	public function dowloaderPagesWiki() {
49
		$this->chargerIndex();
50
		$envoyes = array();
51
		foreach ($this->index as $nom) {
52
			$tagDsc = $nom['page_wiki_dsc'];
53
			$tagCle = $nom['page_wiki_cle'];
54
			if (isset($envoyes[$tagDsc]) == false) {
484 jpm 55
				$url = $this->URL_WIKI.'/'.$tagDsc;
477 jpm 56
				$txt = $this->telechargerTxt($url);
484 jpm 57
				$fichier = $this->dossierBase.self::DOSSIER_V2.'dsc/'.$tagDsc.'.txt';
477 jpm 58
				if (file_put_contents($fichier, $txt)) {
59
					$envoyes[$tagDsc] = 'OK';
60
				}
61
			}
62
			if (isset($envoyes[$tagCle]) == false) {
484 jpm 63
				$url = $this->URL_WIKI.'/'.$tagCle;
477 jpm 64
				$txt = $this->telechargerTxt($url);
484 jpm 65
				$fichier = $this->dossierBase.self::DOSSIER_V2.'cle/'.$tagCle.'.txt';
477 jpm 66
				if (file_put_contents($fichier, $txt)) {
67
					$envoyes[$tagCle] = 'OK';
68
				}
69
			}
70
			$this->messages->afficherAvancement("Download des fichiers en cours");
71
		}
478 jpm 72
		echo "\n";
477 jpm 73
	}
74
 
478 jpm 75
	public function uploaderDansWikiEflore() {
76
		$this->chargerIndex();
77
		$envoyes = array();
484 jpm 78
 
478 jpm 79
		foreach ($this->index as $nom) {
80
			$tagDsc = $nom['page_wiki_dsc'];
81
			if (isset($envoyes[$tagDsc]) == false) {
484 jpm 82
				$fichier = $this->dossierBase.self::DOSSIER_V2.'dsc/'.$tagDsc.'.txt';
478 jpm 83
				if (file_exists($fichier) === true) {
84
					$txt = file_get_contents($fichier);
484 jpm 85
					$this->envoyerPage($tagDsc, $txt);
478 jpm 86
					$envoyes[$tagDsc] = 'OK';
87
				} else {
88
					$this->messages->traiterErreur("Le fichier $fichier est introuvable.");
89
				}
90
			}
91
 
92
			$tagCle = $nom['page_wiki_cle'];
93
			if (isset($envoyes[$tagCle]) == false) {
484 jpm 94
				$fichier = $this->dossierBase.self::DOSSIER_V2.'cle/'.$tagCle.'.txt';
478 jpm 95
				if (file_exists($fichier) === true) {
96
					$txt = file_get_contents($fichier);
484 jpm 97
					$this->envoyerPage($tagDsc, $txt);
478 jpm 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
 
484 jpm 108
	public function uploaderIndexDansWikiEflore() {
109
		$this->chargerIndex();
110
		$envoyes = array();
111
		$pageIndexFamille = "==Index Famille Coste==\n\n";
112
		$pageIndexGenre = "==Index Genre Coste==\n\n";
113
		$pageIndexEspece = "==Index Espèce Coste==\n\n";
114
		foreach ($this->index as $nom) {
115
			$indentation = $this->getIndentationTxtLien($nom);
116
			$txtLien = $this->getTxtLienGenerique($nom);
117
 
118
			$tagDsc = $nom['page_wiki_dsc'];
119
			if (isset($envoyes[$tagDsc]) == false) {
120
				$fichier = $this->dossierBase.self::DOSSIER_V2.'dsc/'.$tagDsc.'.txt';
487 jpm 121
				if ($nom['rang'] <= 180) {
122
					$pageIndexFamille .= "$indentation- [[$tagDsc $txtLien]]\n";
123
				} elseif ($nom['rang'] == 220) {
124
					$pageIndexGenre .= "$indentation- [[$tagDsc $txtLien]]\n";
125
				} else {
126
					$pageIndexEspece .= "$indentation- [[$tagDsc $txtLien]]\n";
484 jpm 127
				}
487 jpm 128
				$envoyes[$tagDsc] = 'OK';
484 jpm 129
			}
130
 
131
			$tagCle = $nom['page_wiki_cle'];
132
			if (isset($envoyes[$tagCle]) == false) {
133
				$fichier = $this->dossierBase.self::DOSSIER_V2.'cle/'.$tagCle.'.txt';
487 jpm 134
				$indentation = $indentation.' ';
135
				if ($nom['rang'] <= 180) {
136
					$pageIndexFamille = rtrim($pageIndexFamille, "\n");
137
					$pageIndexFamille .= " - [[$tagCle Clé]]\n";
138
				} elseif ($nom['rang'] == 220) {
139
					$pageIndexGenre = rtrim($pageIndexGenre, "\n");
140
					$pageIndexGenre .= " - [[$tagCle Clé]]\n";
484 jpm 141
				}
487 jpm 142
				$envoyes[$tagCle] = 'OK';
484 jpm 143
			}
144
			$this->messages->afficherAvancement("Upload des index dans le wikini eFlore");
145
		}
146
		$this->envoyerPage('IndexFamille', $pageIndexFamille);
147
		$this->envoyerPage('IndexGenre', $pageIndexGenre);
148
		$this->envoyerPage('IndexEspece', $pageIndexEspece);
149
		echo "\n";
150
	}
151
 
152
	private function getIndentationTxtLien($nom) {
153
		$rangs = array('Reg' => 1, 'Emb' => 2, 'Cla' => 3, 'Fam' => 4, 'Gen' => 5, 'Esp' => 6);
154
		$indentation = '';
155
		if (preg_match('/^(Reg|Emb|Cla|Fam|Gen|Esp)[0-9]+/', $nom['page_wiki_dsc'], $match)) {
156
			$type = $match[1];
157
			$indentation = str_repeat(' ', $rangs[$type]);
158
		}
159
		return $indentation;
160
	}
161
 
162
	private function getTxtLienGenerique($nom) {
163
		$rangs = array('Reg' => 'Règne', 'Emb' => 'Embranchement', 'Cla' => 'Classe', 'Fam' => 'Famille',
164
			'Gen' => 'Genre', 'Esp' => 'Espèce');
165
		$nomSci = $nom['nom_sci'];
487 jpm 166
		$nomCoste = $nom['nom_coste'];
484 jpm 167
		$txtLien = '';
487 jpm 168
		if (preg_match('/^(Reg|Emb|Cla|Fam|Gen|Esp)([0-9]*)/', $nom['page_wiki_dsc'], $match)) {
484 jpm 169
			$numCoste = $match[2];
170
			$type = $match[1];
487 jpm 171
			$nomRang = $rangs[$type];
172
			$nom = ($type == 'Fam') ? $nomCoste : $nomSci;
173
			$txtLien = "$nomRang $numCoste - $nom";
484 jpm 174
		}
175
		return $txtLien;
176
	}
177
 
178
	private function envoyerPage($titre, $txt) {
179
		$donnees = array('pageTag' => $titre, 'pageContenu' => $txt);
180
		$this->restClient->ajouter($this->URL_WIKI_EFLORE, $donnees);
181
	}
182
 
477 jpm 183
	private function telechargerTxt($url) {
184
		$json = $this->restClient->consulter($url);
185
		$donnees = json_decode($json, true);
186
		return $donnees['texte'];
187
	}
188
 
189
	private function chargerIndex() {
484 jpm 190
		$indexTxt = $this->dossierBase.self::DOSSIER_V2.'coste_v2_00.tsv';
477 jpm 191
		$this->index = $this->outils->transformerTxtTsvEnTableau($indexTxt);
192
	}
193
}
194
?>