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;
488 jpm 11
	private $restClient = null;
12
	private $bdd = null;
477 jpm 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');
484 jpm 19
		$this->conteneur = $conteneur;
20
		$this->URL_WIKI = $this->conteneur->getParametre('wiki.travail');
21
		$this->URL_WIKI_EFLORE = $this->conteneur->getParametre('wiki.eflore');
477 jpm 22
		$this->outils = $conteneur->getOutils();
23
		$this->messages = $conteneur->getMessages();
488 jpm 24
		$this->restClient = $conteneur->getRestClient();
25
		$this->bdd = $conteneur->getBdd();
477 jpm 26
		$this->dossierBase = dirname(__FILE__).'/';
27
	}
28
 
29
	public function uploaderFichiersSp() {
30
		$this->chargerIndex();
31
		$envoyes = array();
32
		foreach ($this->index as $nom) {
33
			$tag = $nom['page_wiki_dsc'];
34
			if (isset($envoyes[$tag]) == false && preg_match('/^Esp([0-9]{4})/', $tag, $match)) {
35
				$fichier = $this->dossierBase.self::DOSSIER_DSC_TXT.$match[1].'.txt';
36
				if (file_exists($fichier) === true) {
37
					$txt = file_get_contents($fichier);
38
					$donnees = array('pageTag' => $tag, 'pageContenu' => $txt);
484 jpm 39
					$this->restClient->ajouter($this->URL_WIKI, $donnees);
477 jpm 40
					$envoyes[$tag] = 'OK';
41
				} else {
42
					$this->messages->traiterErreur("Le fichier $fichier est introuvable.");
43
				}
44
			}
478 jpm 45
			$this->messages->afficherAvancement("Upload des fichiers d'espèce dans le wikini");
477 jpm 46
		}
478 jpm 47
		echo "\n";
477 jpm 48
	}
49
 
50
	public function dowloaderPagesWiki() {
51
		$this->chargerIndex();
52
		$envoyes = array();
53
		foreach ($this->index as $nom) {
54
			$tagDsc = $nom['page_wiki_dsc'];
55
			$tagCle = $nom['page_wiki_cle'];
56
			if (isset($envoyes[$tagDsc]) == false) {
484 jpm 57
				$url = $this->URL_WIKI.'/'.$tagDsc;
477 jpm 58
				$txt = $this->telechargerTxt($url);
484 jpm 59
				$fichier = $this->dossierBase.self::DOSSIER_V2.'dsc/'.$tagDsc.'.txt';
477 jpm 60
				if (file_put_contents($fichier, $txt)) {
61
					$envoyes[$tagDsc] = 'OK';
62
				}
63
			}
64
			if (isset($envoyes[$tagCle]) == false) {
484 jpm 65
				$url = $this->URL_WIKI.'/'.$tagCle;
477 jpm 66
				$txt = $this->telechargerTxt($url);
484 jpm 67
				$fichier = $this->dossierBase.self::DOSSIER_V2.'cle/'.$tagCle.'.txt';
477 jpm 68
				if (file_put_contents($fichier, $txt)) {
69
					$envoyes[$tagCle] = 'OK';
70
				}
71
			}
72
			$this->messages->afficherAvancement("Download des fichiers en cours");
73
		}
478 jpm 74
		echo "\n";
477 jpm 75
	}
76
 
478 jpm 77
	public function uploaderDansWikiEflore() {
78
		$this->chargerIndex();
79
		$envoyes = array();
484 jpm 80
 
478 jpm 81
		foreach ($this->index as $nom) {
82
			$tagDsc = $nom['page_wiki_dsc'];
83
			if (isset($envoyes[$tagDsc]) == false) {
484 jpm 84
				$fichier = $this->dossierBase.self::DOSSIER_V2.'dsc/'.$tagDsc.'.txt';
478 jpm 85
				if (file_exists($fichier) === true) {
86
					$txt = file_get_contents($fichier);
484 jpm 87
					$this->envoyerPage($tagDsc, $txt);
478 jpm 88
					$envoyes[$tagDsc] = 'OK';
89
				} else {
90
					$this->messages->traiterErreur("Le fichier $fichier est introuvable.");
91
				}
92
			}
93
 
94
			$tagCle = $nom['page_wiki_cle'];
95
			if (isset($envoyes[$tagCle]) == false) {
484 jpm 96
				$fichier = $this->dossierBase.self::DOSSIER_V2.'cle/'.$tagCle.'.txt';
478 jpm 97
				if (file_exists($fichier) === true) {
98
					$txt = file_get_contents($fichier);
488 jpm 99
					$this->envoyerPage($tagCle, $txt);
478 jpm 100
					$envoyes[$tagCle] = 'OK';
101
				} else {
102
					$this->messages->traiterErreur("Le fichier $fichier est introuvable.");
103
				}
104
			}
105
			$this->messages->afficherAvancement("Upload des textes dans le wikini eFlore");
106
		}
107
		echo "\n";
108
	}
109
 
488 jpm 110
	public function chargerTxtDansWikiEflore() {
111
		$this->chargerIndex();
112
		$envoyes = array();
113
		foreach ($this->index as $nom) {
114
			$tagDsc = $nom['page_wiki_dsc'];
115
			if (isset($envoyes[$tagDsc]) == false) {
116
				$fichier = $this->dossierBase.self::DOSSIER_V2.'dsc/'.$tagDsc.'.txt';
117
				if (file_exists($fichier) === true) {
118
					$txt = file_get_contents($fichier);
119
					$this->enregistrerPage($tagDsc, $txt);
120
					$envoyes[$tagDsc] = 'OK';
121
				} else {
122
					$this->messages->traiterErreur("Le fichier $fichier est introuvable.");
123
				}
124
			}
125
 
126
			$tagCle = $nom['page_wiki_cle'];
127
			if (isset($envoyes[$tagCle]) == false) {
128
				$fichier = $this->dossierBase.self::DOSSIER_V2.'cle/'.$tagCle.'.txt';
129
				if (file_exists($fichier) === true) {
130
					$txt = file_get_contents($fichier);
131
					$this->enregistrerPage($tagCle, $txt);
132
					$envoyes[$tagCle] = 'OK';
133
				} else {
134
					$this->messages->traiterErreur("Le fichier $fichier est introuvable.");
135
				}
136
			}
137
			$this->messages->afficherAvancement("Enregistrement des textes dans le wikini eFlore");
138
		}
139
		echo "\n";
140
	}
141
 
142
	public function chargerIndexDansWikiEflore() {
143
		$index = $this->creerIndex();
144
		foreach ($index as $titre => $txt) {
145
			$this->enregistrerPage($titre, $txt);
146
		}
147
	}
148
 
484 jpm 149
	public function uploaderIndexDansWikiEflore() {
488 jpm 150
		$index = $this->creerIndex();
151
		foreach ($index as $titre => $txt) {
152
			$this->envoyerPage($titre, $txt);
153
		}
154
	}
155
 
156
	public function creerIndex() {
484 jpm 157
		$this->chargerIndex();
158
		$envoyes = array();
488 jpm 159
		$pageIndexFamille = "==Index Famille Coste==\n\n";
160
		$pageIndexGenre = "==Index Genre Coste==\n\n";
484 jpm 161
		$pageIndexEspece = "==Index Espèce Coste==\n\n";
488 jpm 162
		foreach ($this->index as $nom) {
484 jpm 163
			$indentation = $this->getIndentationTxtLien($nom);
164
			$txtLien = $this->getTxtLienGenerique($nom);
165
 
166
			$tagDsc = $nom['page_wiki_dsc'];
167
			if (isset($envoyes[$tagDsc]) == false) {
168
				$fichier = $this->dossierBase.self::DOSSIER_V2.'dsc/'.$tagDsc.'.txt';
488 jpm 169
				if ($nom['rang'] <= 180) {
170
					$pageIndexFamille .= "$indentation- [[$tagDsc $txtLien]]\n";
171
				} elseif ($nom['rang'] == 220) {
172
					$pageIndexGenre .= "$indentation- [[$tagDsc $txtLien]]\n";
173
				} else {
174
					$pageIndexEspece .= "$indentation- [[$tagDsc $txtLien]]\n";
484 jpm 175
				}
487 jpm 176
				$envoyes[$tagDsc] = 'OK';
484 jpm 177
			}
178
 
179
			$tagCle = $nom['page_wiki_cle'];
180
			if (isset($envoyes[$tagCle]) == false) {
181
				$fichier = $this->dossierBase.self::DOSSIER_V2.'cle/'.$tagCle.'.txt';
488 jpm 182
				$indentation = $indentation.' ';
487 jpm 183
				if ($nom['rang'] <= 180) {
488 jpm 184
					$pageIndexFamille = rtrim($pageIndexFamille, "\n");
487 jpm 185
					$pageIndexFamille .= " - [[$tagCle Clé]]\n";
488 jpm 186
				} elseif ($nom['rang'] == 220) {
187
					$pageIndexGenre = rtrim($pageIndexGenre, "\n");
188
					$pageIndexGenre .= " - [[$tagCle Clé]]\n";
484 jpm 189
				}
487 jpm 190
				$envoyes[$tagCle] = 'OK';
484 jpm 191
			}
488 jpm 192
			$this->messages->afficherAvancement("Création des pages d'index pour le wikini eFlore");
484 jpm 193
		}
488 jpm 194
		echo "\n";
195
 
196
		$index = array('IndexFamille' => $pageIndexFamille, 'IndexGenre' => $pageIndexGenre, 'IndexEspece' => $pageIndexEspece);
197
		return $index;
484 jpm 198
	}
199
 
200
	private function getIndentationTxtLien($nom) {
201
		$rangs = array('Reg' => 1, 'Emb' => 2, 'Cla' => 3, 'Fam' => 4, 'Gen' => 5, 'Esp' => 6);
202
		$indentation = '';
203
		if (preg_match('/^(Reg|Emb|Cla|Fam|Gen|Esp)[0-9]+/', $nom['page_wiki_dsc'], $match)) {
204
			$type = $match[1];
205
			$indentation = str_repeat(' ', $rangs[$type]);
206
		}
207
		return $indentation;
208
	}
209
 
210
	private function getTxtLienGenerique($nom) {
211
		$rangs = array('Reg' => 'Règne', 'Emb' => 'Embranchement', 'Cla' => 'Classe', 'Fam' => 'Famille',
212
			'Gen' => 'Genre', 'Esp' => 'Espèce');
213
		$nomSci = $nom['nom_sci'];
487 jpm 214
		$nomCoste = $nom['nom_coste'];
484 jpm 215
		$txtLien = '';
487 jpm 216
		if (preg_match('/^(Reg|Emb|Cla|Fam|Gen|Esp)([0-9]*)/', $nom['page_wiki_dsc'], $match)) {
484 jpm 217
			$numCoste = $match[2];
218
			$type = $match[1];
487 jpm 219
			$nomRang = $rangs[$type];
220
			$nom = ($type == 'Fam') ? $nomCoste : $nomSci;
221
			$txtLien = "$nomRang $numCoste - $nom";
484 jpm 222
		}
223
		return $txtLien;
224
	}
225
 
226
	private function envoyerPage($titre, $txt) {
227
		$donnees = array('pageTag' => $titre, 'pageContenu' => $txt);
228
		$this->restClient->ajouter($this->URL_WIKI_EFLORE, $donnees);
229
	}
230
 
488 jpm 231
	private function enregistrerPage($titre, $txt) {
232
		$titre = $this->bdd->proteger($titre);
233
		$time = $this->bdd->proteger(date('Y-m-d H:i:s'));
234
		$txt = $this->bdd->proteger($txt);
235
		$requete = "INSERT INTO `coste_pages` (`tag`, `time`, `body`, `body_r`, `owner`, `user`, `latest`, `handler`, `comment_on`) VALUES ".
236
				"($titre, $time, $txt, '', '', 'ScriptEflore', 'Y', 'page', '')";
237
		$this->bdd->requeter($requete);
238
	}
239
 
477 jpm 240
	private function telechargerTxt($url) {
241
		$json = $this->restClient->consulter($url);
242
		$donnees = json_decode($json, true);
243
		return $donnees['texte'];
244
	}
245
 
246
	private function chargerIndex() {
484 jpm 247
		$indexTxt = $this->dossierBase.self::DOSSIER_V2.'coste_v2_00.tsv';
477 jpm 248
		$this->index = $this->outils->transformerTxtTsvEnTableau($indexTxt);
249
	}
250
}
251
?>