Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 487 | Rev 492 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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