406 |
jpm |
1 |
<?php
|
|
|
2 |
//declare(encoding='UTF-8');
|
|
|
3 |
/**
|
|
|
4 |
* Exemple de lancement du script : :
|
|
|
5 |
* /opt/lampp/bin/php cli.php bdtxa -a chargerTous
|
|
|
6 |
*
|
|
|
7 |
* @category php 5.2
|
|
|
8 |
* @package eFlore/Scripts
|
|
|
9 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
10 |
* @copyright Copyright (c) 2012, Tela Botanica (accueil@tela-botanica.org)
|
|
|
11 |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
|
|
|
12 |
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
|
|
|
13 |
* @version $Id$
|
|
|
14 |
*/
|
|
|
15 |
class Bdtxa extends EfloreScript {
|
|
|
16 |
|
|
|
17 |
private $table = null;
|
|
|
18 |
private $pasInsertion = 1000;
|
|
|
19 |
private $departInsertion = 0;
|
|
|
20 |
|
|
|
21 |
protected $parametres_autorises = array(
|
|
|
22 |
'-t' => array(false, false, 'Permet de tester le script sur un jeu réduit de données (indiquer le nombre de lignes).'));
|
|
|
23 |
|
|
|
24 |
public function executer() {
|
|
|
25 |
try {
|
|
|
26 |
$this->initialiserProjet('bdtxa');
|
|
|
27 |
|
|
|
28 |
// Lancement de l'action demandée
|
|
|
29 |
$cmd = $this->getParametre('a');
|
|
|
30 |
switch ($cmd) {
|
|
|
31 |
case 'chargerTous' :
|
|
|
32 |
$this->chargerStructureSql();
|
|
|
33 |
$this->chargerBdtxa();
|
|
|
34 |
$this->genererChpNomSciHtml();
|
|
|
35 |
$this->genererChpFamille();
|
636 |
aurelien |
36 |
$this->genererChpNomComplet();
|
699 |
aurelien |
37 |
$this->genererChpHierarchie();
|
406 |
jpm |
38 |
break;
|
|
|
39 |
case 'chargerStructureSql' :
|
|
|
40 |
$this->chargerStructureSql();
|
|
|
41 |
break;
|
|
|
42 |
case 'chargerBdtxa' :
|
|
|
43 |
$this->chargerBdtxa();
|
|
|
44 |
break;
|
637 |
delphine |
45 |
case 'genererChpNomSciHtml' :
|
406 |
jpm |
46 |
$this->genererChpNomSciHtml();
|
|
|
47 |
break;
|
637 |
delphine |
48 |
case 'genererChpNomComplet' :
|
|
|
49 |
$this->initialiserGenerationChamps();
|
|
|
50 |
$this->genererChpNomComplet();
|
|
|
51 |
break;
|
406 |
jpm |
52 |
case 'genererChpFamille' :
|
|
|
53 |
$this->genererChpFamille();
|
|
|
54 |
break;
|
699 |
aurelien |
55 |
case 'genererChpHierarchie' :
|
|
|
56 |
$this->genererChpHierarchie();
|
|
|
57 |
break;
|
406 |
jpm |
58 |
case 'supprimerTous' :
|
|
|
59 |
$this->supprimerTous();
|
|
|
60 |
break;
|
|
|
61 |
default :
|
|
|
62 |
throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
|
|
|
63 |
}
|
|
|
64 |
} catch (Exception $e) {
|
|
|
65 |
$this->traiterErreur($e->getMessage());
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
private function chargerBdtxa() {
|
661 |
jpm |
70 |
$chemin = Config::get('chemins.bdt');
|
634 |
delphine |
71 |
$table = Config::get('tables.bdtxa');
|
406 |
jpm |
72 |
$requete = "LOAD DATA INFILE '$chemin' ".
|
|
|
73 |
"REPLACE INTO TABLE $table ".
|
|
|
74 |
'CHARACTER SET utf8 '.
|
|
|
75 |
'FIELDS '.
|
|
|
76 |
" TERMINATED BY '\t' ".
|
|
|
77 |
" ENCLOSED BY '' ".
|
|
|
78 |
" ESCAPED BY '\\\' ".
|
|
|
79 |
'IGNORE 1 LINES';
|
|
|
80 |
$this->getBdd()->requeter($requete);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
private function genererChpNomSciHtml() {
|
|
|
84 |
$this->initialiserGenerationChamps();
|
|
|
85 |
$this->preparerTablePrChpNomSciHtml();
|
|
|
86 |
$generateur = new GenerateurNomSciHtml();
|
661 |
jpm |
87 |
$nbreTotal = $this->recupererNbTotalTuples();
|
|
|
88 |
$erreurs = array();
|
406 |
jpm |
89 |
$this->departInsertion = 0;
|
|
|
90 |
while ($this->departInsertion < $nbreTotal) {
|
661 |
jpm |
91 |
$resultat = $this->recupererTuplesPrChpNomSciHtml();
|
|
|
92 |
|
|
|
93 |
try {
|
|
|
94 |
$nomsSciEnHtml = $generateur->generer($resultat);
|
|
|
95 |
} catch (Exception $e) {
|
|
|
96 |
$erreurs[] = $e->getMessage();
|
|
|
97 |
}
|
|
|
98 |
|
406 |
jpm |
99 |
$this->remplirChpNomSciHtm($nomsSciEnHtml);
|
|
|
100 |
$this->departInsertion += $this->pasInsertion;
|
|
|
101 |
$this->afficherAvancement("Insertion des noms scientifique au format HTML dans la base par paquet de {$this->pasInsertion} en cours");
|
|
|
102 |
if ($this->stopperLaBoucle($this->getParametre('t'))) break;
|
661 |
jpm |
103 |
}
|
|
|
104 |
echo "\n";
|
|
|
105 |
|
|
|
106 |
$this->creerFichierLog('Erreurs lors de la génération HTML des noms scientifiques', $erreurs, 'erreurs_noms_sci_html');
|
406 |
jpm |
107 |
}
|
|
|
108 |
|
|
|
109 |
private function initialiserGenerationChamps() {
|
699 |
aurelien |
110 |
$this->table = Config::get('tables.bdtxa');
|
|
|
111 |
echo $this->table;
|
406 |
jpm |
112 |
}
|
|
|
113 |
|
|
|
114 |
private function preparerTablePrChpNomSciHtml() {
|
|
|
115 |
$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'nom_sci_html' ";
|
|
|
116 |
$resultat = $this->getBdd()->recuperer($requete);
|
|
|
117 |
if ($resultat === false) {
|
|
|
118 |
$requete = "ALTER TABLE {$this->table} ".
|
|
|
119 |
'ADD nom_sci_html VARCHAR( 500 ) '.
|
|
|
120 |
'CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ';
|
|
|
121 |
$this->getBdd()->requeter($requete);
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
private function recupererNbTotalTuples(){
|
|
|
126 |
$requete = "SELECT count(*) AS nb FROM {$this->table} ";
|
|
|
127 |
$resultat = $this->getBdd()->recuperer($requete);
|
|
|
128 |
return $resultat['nb'];
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
private function recupererTuplesPrChpNomSciHtml() {
|
|
|
132 |
$requete = 'SELECT num_nom, rang, nom_supra_generique, genre, epithete_infra_generique, '.
|
|
|
133 |
' epithete_sp, type_epithete, epithete_infra_sp,cultivar_groupe, '.
|
|
|
134 |
' nom_commercial, cultivar '.
|
|
|
135 |
"FROM {$this->table} ".
|
|
|
136 |
"LIMIT {$this->departInsertion},{$this->pasInsertion} ";
|
|
|
137 |
$resultat = $this->getBdd()->recupererTous($requete);
|
|
|
138 |
return $resultat;
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
private function remplirChpNomSciHtm($nomsSciHtm) {
|
|
|
142 |
foreach ($nomsSciHtm as $id => $html) {
|
|
|
143 |
$html = $this->getBdd()->proteger($html);
|
|
|
144 |
$requete = "UPDATE {$this->table} SET nom_sci_html = $html WHERE num_nom = $id ";
|
|
|
145 |
$resultat = $this->getBdd()->requeter($requete);
|
|
|
146 |
if ($resultat === false) {
|
|
|
147 |
throw new Exception("Erreur d'insertion pour le tuple $id");
|
|
|
148 |
}
|
|
|
149 |
}
|
636 |
aurelien |
150 |
}
|
661 |
jpm |
151 |
|
636 |
aurelien |
152 |
private function genererChpNomComplet() {
|
|
|
153 |
$this->preparerTablePrChpNomComplet();
|
|
|
154 |
$this->remplirChpNomComplet();
|
|
|
155 |
}
|
661 |
jpm |
156 |
|
636 |
aurelien |
157 |
private function preparerTablePrChpNomComplet() {
|
|
|
158 |
$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'nom_complet' ";
|
|
|
159 |
$resultat = $this->getBdd()->recuperer($requete);
|
|
|
160 |
if ($resultat === false) {
|
|
|
161 |
$requete = "ALTER TABLE {$this->table} ".
|
|
|
162 |
'ADD nom_complet VARCHAR( 500 ) '.
|
|
|
163 |
'CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ';
|
|
|
164 |
$this->getBdd()->requeter($requete);
|
|
|
165 |
}
|
|
|
166 |
}
|
661 |
jpm |
167 |
|
636 |
aurelien |
168 |
private function remplirChpNomComplet() {
|
661 |
jpm |
169 |
echo "Attribution du champ nom complet au taxons : ";
|
636 |
aurelien |
170 |
$requete = "UPDATE {$this->table} SET nom_complet = CONCAT(nom_sci,' ',auteur)";
|
|
|
171 |
$resultat = $this->getBdd()->requeter($requete);
|
|
|
172 |
if ($resultat === false) {
|
661 |
jpm |
173 |
echo "KO\n";
|
636 |
aurelien |
174 |
throw new Exception("Erreur de génération du champ nom complet");
|
661 |
jpm |
175 |
} else {
|
|
|
176 |
echo "OK\n";
|
636 |
aurelien |
177 |
}
|
406 |
jpm |
178 |
}
|
|
|
179 |
|
|
|
180 |
private function genererChpFamille() {
|
|
|
181 |
$this->initialiserGenerationChamps();
|
|
|
182 |
$this->preparerTablePrChpFamille();
|
|
|
183 |
$resultats = $this->recupererTuplesPrChpFamille();
|
|
|
184 |
$noms = array();
|
634 |
delphine |
185 |
$introuvables = array();
|
|
|
186 |
$introuvablesSyno = array();
|
406 |
jpm |
187 |
foreach ($resultats as $id => $nom) {
|
|
|
188 |
$nn = $nom['num_nom'];
|
|
|
189 |
$nnr = $nom['num_nom_retenu'];
|
|
|
190 |
$nts = $nom['num_tax_sup'];
|
|
|
191 |
$rg = $nom['rang'];
|
|
|
192 |
if ($nnr != '') {
|
|
|
193 |
if ($rg == '180') {
|
|
|
194 |
$noms[$nn] = $nom['nom_sci'];
|
|
|
195 |
} else {
|
|
|
196 |
if ($nn == $nnr) {// nom retenu
|
|
|
197 |
if (isset($noms[$nts])) {
|
|
|
198 |
$noms[$nn] = $noms[$nts];
|
|
|
199 |
} else {
|
|
|
200 |
$introuvables[] = $nn;
|
|
|
201 |
}
|
|
|
202 |
} else {// nom synonyme
|
|
|
203 |
if (isset($noms[$nnr])) {
|
|
|
204 |
$noms[$nn] = $noms[$nnr];
|
|
|
205 |
} else {
|
|
|
206 |
$introuvablesSyno[] = $nom;
|
|
|
207 |
}
|
|
|
208 |
}
|
|
|
209 |
}
|
|
|
210 |
}
|
|
|
211 |
unset($resultats[$id]);
|
|
|
212 |
$this->afficherAvancement("Attribution de leur famille aux noms en cours");
|
|
|
213 |
if ($this->stopperLaBoucle($this->getParametre('t'))) break;
|
|
|
214 |
}
|
|
|
215 |
echo "\n";
|
|
|
216 |
|
|
|
217 |
foreach ($introuvablesSyno as $id => $nom) {
|
|
|
218 |
$nn = $nom['num_nom'];
|
|
|
219 |
$nnr = $nom['num_nom_retenu'];
|
|
|
220 |
if (isset($noms[$nnr])) {
|
|
|
221 |
$noms[$nn] = $noms[$nnr];
|
|
|
222 |
} else {
|
|
|
223 |
$introuvables[] = $nn;
|
|
|
224 |
}
|
|
|
225 |
unset($introuvablesSyno[$id]);
|
|
|
226 |
$this->afficherAvancement("Attribution de leur famille aux synonymes en cours");
|
|
|
227 |
}
|
|
|
228 |
echo "\n";
|
661 |
jpm |
229 |
|
|
|
230 |
$msg = 'Plusieurs familles sont introuvables';
|
|
|
231 |
$this->creerFichierLog($msg, $introuvables, 'famille_introuvable');
|
406 |
jpm |
232 |
|
|
|
233 |
$this->remplirChpFamille($noms);
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
private function preparerTablePrChpFamille() {
|
|
|
237 |
$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'famille' ";
|
|
|
238 |
$resultat = $this->getBdd()->recuperer($requete);
|
|
|
239 |
if ($resultat === false) {
|
|
|
240 |
$requete = "ALTER TABLE {$this->table} ".
|
|
|
241 |
'ADD famille VARCHAR(255) '.
|
|
|
242 |
'CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ';
|
|
|
243 |
$this->getBdd()->requeter($requete);
|
|
|
244 |
}
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
private function recupererTuplesPrChpFamille() {
|
|
|
248 |
$requete = 'SELECT num_nom, num_nom_retenu, num_tax_sup, rang, nom_sci '.
|
|
|
249 |
"FROM {$this->table} ".
|
|
|
250 |
"WHERE rang >= 180 ".
|
|
|
251 |
"ORDER BY rang ASC, num_tax_sup ASC, num_nom_retenu DESC ";
|
|
|
252 |
$resultat = $this->getBdd()->recupererTous($requete);
|
|
|
253 |
return $resultat;
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
private function remplirChpFamille($noms) {
|
|
|
257 |
foreach ($noms as $id => $famille) {
|
|
|
258 |
$famille = $this->getBdd()->proteger($famille);
|
|
|
259 |
$requete = "UPDATE {$this->table} SET famille = $famille WHERE num_nom = $id ";
|
|
|
260 |
$resultat = $this->getBdd()->requeter($requete);
|
|
|
261 |
if ($resultat === false) {
|
|
|
262 |
throw new Exception("Erreur d'insertion pour le tuple $id");
|
|
|
263 |
}
|
|
|
264 |
$this->afficherAvancement("Insertion des noms de famille dans la base en cours");
|
|
|
265 |
}
|
|
|
266 |
echo "\n";
|
|
|
267 |
}
|
699 |
aurelien |
268 |
|
|
|
269 |
private function genererChpHierarchie() {
|
|
|
270 |
$this->initialiserGenerationChamps();
|
|
|
271 |
$this->preparerTablePrChpHierarchie();
|
|
|
272 |
$table = Config::get('tables.bdtxa');
|
|
|
273 |
|
|
|
274 |
$requete = "UPDATE $table SET hierarchie = NULL ";
|
|
|
275 |
$mise_a_jour = $this->getBdd()->requeter($requete);
|
|
|
276 |
|
|
|
277 |
$requete_hierarchie = "SELECT num_nom, num_nom_retenu, num_tax_sup FROM ".$table." ORDER BY rang DESC";
|
|
|
278 |
|
|
|
279 |
$resultat = $this->getBdd()->recupererTous($requete_hierarchie);
|
|
|
280 |
$num_nom_a_num_sup = array();
|
|
|
281 |
foreach($resultat as &$taxon) {
|
|
|
282 |
$num_nom_a_num_sup[$taxon['num_nom']] = $taxon['num_tax_sup'];
|
|
|
283 |
}
|
|
|
284 |
$chemin_taxo = "";
|
|
|
285 |
foreach($resultat as &$taxon) {
|
|
|
286 |
$chemin_taxo = $this->traiterHierarchieNumTaxSup($taxon['num_nom_retenu'], $num_nom_a_num_sup).'-';
|
|
|
287 |
$requete = "UPDATE $table SET hierarchie = ".$this->getBdd()->proteger($chemin_taxo)." WHERE num_nom = ".$taxon['num_nom']." ";
|
|
|
288 |
$mise_a_jour = $this->getBdd()->requeter($requete);
|
|
|
289 |
$this->afficherAvancement("Insertion de la hierarchie taxonomique en cours");
|
|
|
290 |
}
|
|
|
291 |
echo "\n";
|
|
|
292 |
}
|
|
|
293 |
|
|
|
294 |
private function traiterHierarchieNumTaxSup($num_nom_retenu, &$num_nom_a_num_sup) {
|
|
|
295 |
$chaine_hierarchie = "";
|
|
|
296 |
if(isset($num_nom_a_num_sup[$num_nom_retenu])) {
|
|
|
297 |
$num_tax_sup = $num_nom_a_num_sup[$num_nom_retenu];
|
|
|
298 |
$chaine_hierarchie = '-'.$num_tax_sup;
|
|
|
299 |
if($num_tax_sup != 0 && $num_tax_sup != '') {
|
|
|
300 |
$chaine_hierarchie = $this->traiterHierarchieNumTaxSup($num_tax_sup, $num_nom_a_num_sup).$chaine_hierarchie;
|
|
|
301 |
}
|
|
|
302 |
}
|
|
|
303 |
return $chaine_hierarchie;
|
|
|
304 |
}
|
|
|
305 |
|
|
|
306 |
private function preparerTablePrChpHierarchie() {
|
|
|
307 |
$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'hierarchie' ";
|
|
|
308 |
$resultat = $this->getBdd()->recuperer($requete);
|
|
|
309 |
if ($resultat === false) {
|
|
|
310 |
$requete = "ALTER TABLE {$this->table} ".
|
|
|
311 |
'ADD hierarchie VARCHAR(1000) '.
|
|
|
312 |
'CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ';
|
|
|
313 |
$this->getBdd()->requeter($requete);
|
|
|
314 |
}
|
|
|
315 |
}
|
|
|
316 |
|
634 |
delphine |
317 |
private function genererDonneesTestMultiVersion() {
|
|
|
318 |
$contenuSql = $this->recupererContenu(Config::get('chemins.structureSqlTest'));
|
|
|
319 |
$this->executerScripSql($contenuSql);
|
661 |
jpm |
320 |
|
634 |
delphine |
321 |
$table = Config::get('tables.bdtxa');
|
|
|
322 |
$tableTest = Config::get('tables.bdtxaTest');
|
|
|
323 |
$requete = "INSERT INTO $tableTest SELECT * FROM $table";
|
|
|
324 |
$this->getBdd()->requeter($requete);
|
|
|
325 |
}
|
661 |
jpm |
326 |
|
634 |
delphine |
327 |
private function supprimerDonneesTestMultiVersion() {
|
|
|
328 |
$tableMeta = Config::get('tables.bdtxaMeta');
|
661 |
jpm |
329 |
$requete = "DELETE FROM $tableMeta WHERE guid = 'urn:lsid:tela-botanica.org:bdtxa:1.00'";
|
634 |
delphine |
330 |
$this->getBdd()->requeter($requete);
|
661 |
jpm |
331 |
|
634 |
delphine |
332 |
$tableTest = Config::get('tables.bdtxaTest');
|
|
|
333 |
$requete = "DROP TABLE IF EXISTS $tableTest";
|
|
|
334 |
$this->getBdd()->requeter($requete);
|
|
|
335 |
}
|
406 |
jpm |
336 |
private function supprimerTous() {
|
661 |
jpm |
337 |
$requete = "DROP TABLE IF EXISTS bdtxa_meta, bdtxa_v0_01, bdtxa_v1_00";
|
406 |
jpm |
338 |
$this->getBdd()->requeter($requete);
|
|
|
339 |
}
|
661 |
jpm |
340 |
|
|
|
341 |
private function creerFichierLog($message, $lignes, $nomFichier) {
|
|
|
342 |
$lignesNbre = count($lignes);
|
|
|
343 |
if ($lignesNbre != 0) {
|
|
|
344 |
echo "$message. Voir le log de $lignesNbre lignes :\n";
|
|
|
345 |
|
|
|
346 |
$logContenu = implode(", \n", $lignes);
|
|
|
347 |
$logFichier = realpath(dirname(__FILE__))."/log/$nomFichier.log";
|
|
|
348 |
echo $logFichier."\n";
|
|
|
349 |
file_put_contents($logFichier, $logContenu);
|
|
|
350 |
}
|
|
|
351 |
}
|
406 |
jpm |
352 |
}
|
|
|
353 |
?>
|