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