Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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