Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 492 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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