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(
|
105 |
jpm |
23 |
'-t' => array(false, false, 'Permet de tester le script sur un jeux 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();
|
46 |
jpm |
38 |
break;
|
|
|
39 |
case 'chargerStructureSql' :
|
|
|
40 |
$this->chargerStructureSql();
|
|
|
41 |
break;
|
|
|
42 |
case 'chargerBdtfx' :
|
|
|
43 |
$this->chargerBdtfx();
|
|
|
44 |
break;
|
|
|
45 |
case 'genererNomSciHtml' :
|
340 |
jpm |
46 |
$this->genererChpNomSciHtml();
|
46 |
jpm |
47 |
break;
|
335 |
jpm |
48 |
case 'genererChpFamille' :
|
|
|
49 |
$this->genererChpFamille();
|
|
|
50 |
break;
|
103 |
jpm |
51 |
case 'genererDonneesTestMultiVersion' :
|
|
|
52 |
$this->genererDonneesTestMultiVersion();
|
|
|
53 |
break;
|
|
|
54 |
case 'supprimerDonneesTestMultiVersion' :
|
|
|
55 |
$this->supprimerDonneesTestMultiVersion();
|
|
|
56 |
break;
|
130 |
jpm |
57 |
case 'supprimerTous' :
|
|
|
58 |
$this->supprimerTous();
|
|
|
59 |
break;
|
46 |
jpm |
60 |
default :
|
|
|
61 |
throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
|
|
|
62 |
}
|
|
|
63 |
} catch (Exception $e) {
|
|
|
64 |
$this->traiterErreur($e->getMessage());
|
11 |
jpm |
65 |
}
|
46 |
jpm |
66 |
}
|
11 |
jpm |
67 |
|
46 |
jpm |
68 |
private function chargerBdtfx() {
|
|
|
69 |
$chemin = Config::get('chemins.bdtfx');
|
|
|
70 |
$table = Config::get('tables.bdtfx');
|
|
|
71 |
$requete = "LOAD DATA INFILE '$chemin' ".
|
|
|
72 |
"REPLACE INTO TABLE $table ".
|
|
|
73 |
'CHARACTER SET utf8 '.
|
|
|
74 |
'FIELDS '.
|
|
|
75 |
" TERMINATED BY '\t' ".
|
|
|
76 |
" ENCLOSED BY '' ".
|
|
|
77 |
" ESCAPED BY '\\\' ".
|
|
|
78 |
'IGNORE 1 LINES';
|
68 |
jpm |
79 |
$this->getBdd()->requeter($requete);
|
46 |
jpm |
80 |
}
|
|
|
81 |
|
340 |
jpm |
82 |
private function genererChpNomSciHtml() {
|
335 |
jpm |
83 |
$this->initialiserGenerationChamps();
|
|
|
84 |
$this->preparerTablePrChpNomSciHtml();
|
46 |
jpm |
85 |
$generateur = new GenerateurNomSciHtml();
|
|
|
86 |
$nbreTotal = $this->recupererNbTotalTuples();
|
335 |
jpm |
87 |
$this->departInsertion = 0;
|
46 |
jpm |
88 |
while ($this->departInsertion < $nbreTotal) {
|
340 |
jpm |
89 |
$resultat = $this->recupererTuplesPrChpNomSciHtml();
|
46 |
jpm |
90 |
$nomsSciEnHtml = $generateur->generer($resultat);
|
340 |
jpm |
91 |
$this->remplirChpNomSciHtm($nomsSciEnHtml);
|
46 |
jpm |
92 |
$this->departInsertion += $this->pasInsertion;
|
|
|
93 |
$this->afficherAvancement("Insertion des noms scientifique au format HTML dans la base par paquet de {$this->pasInsertion} en cours");
|
68 |
jpm |
94 |
if ($this->stopperLaBoucle($this->getParametre('t'))) break;
|
11 |
jpm |
95 |
}
|
|
|
96 |
echo "\n";
|
|
|
97 |
}
|
|
|
98 |
|
335 |
jpm |
99 |
private function initialiserGenerationChamps() {
|
46 |
jpm |
100 |
$this->table = Config::get('tables.bdtfx');
|
11 |
jpm |
101 |
}
|
|
|
102 |
|
335 |
jpm |
103 |
private function preparerTablePrChpNomSciHtml() {
|
11 |
jpm |
104 |
$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'nom_sci_html' ";
|
68 |
jpm |
105 |
$resultat = $this->getBdd()->recuperer($requete);
|
11 |
jpm |
106 |
if ($resultat === false) {
|
|
|
107 |
$requete = "ALTER TABLE {$this->table} ".
|
335 |
jpm |
108 |
'ADD nom_sci_html VARCHAR( 500 ) '.
|
|
|
109 |
'CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ';
|
68 |
jpm |
110 |
$this->getBdd()->requeter($requete);
|
11 |
jpm |
111 |
}
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
private function recupererNbTotalTuples(){
|
46 |
jpm |
115 |
$requete = "SELECT count(*) AS nb FROM {$this->table} ";
|
68 |
jpm |
116 |
$resultat = $this->getBdd()->recuperer($requete);
|
46 |
jpm |
117 |
return $resultat['nb'];
|
11 |
jpm |
118 |
}
|
|
|
119 |
|
340 |
jpm |
120 |
private function recupererTuplesPrChpNomSciHtml() {
|
11 |
jpm |
121 |
$requete = 'SELECT num_nom, rang, nom_supra_generique, genre, epithete_infra_generique, '.
|
335 |
jpm |
122 |
' epithete_sp, type_epithete, epithete_infra_sp,cultivar_groupe, '.
|
|
|
123 |
' nom_commercial, cultivar '.
|
|
|
124 |
"FROM {$this->table} ".
|
|
|
125 |
"LIMIT {$this->departInsertion},{$this->pasInsertion} ";
|
68 |
jpm |
126 |
$resultat = $this->getBdd()->recupererTous($requete);
|
11 |
jpm |
127 |
return $resultat;
|
|
|
128 |
}
|
|
|
129 |
|
340 |
jpm |
130 |
private function remplirChpNomSciHtm($nomsSciHtm) {
|
46 |
jpm |
131 |
foreach ($nomsSciHtm as $id => $html) {
|
68 |
jpm |
132 |
$html = $this->getBdd()->proteger($html);
|
340 |
jpm |
133 |
$requete = "UPDATE {$this->table} SET nom_sci_html = $html WHERE num_nom = $id ";
|
68 |
jpm |
134 |
$resultat = $this->getBdd()->requeter($requete);
|
46 |
jpm |
135 |
if ($resultat === false) {
|
|
|
136 |
throw new Exception("Erreur d'insertion pour le tuple $id");
|
11 |
jpm |
137 |
}
|
|
|
138 |
}
|
|
|
139 |
}
|
339 |
jpm |
140 |
|
335 |
jpm |
141 |
private function genererChpFamille() {
|
|
|
142 |
$this->initialiserGenerationChamps();
|
|
|
143 |
$this->preparerTablePrChpFamille();
|
336 |
jpm |
144 |
$resultats = $this->recupererTuplesPrChpFamille();
|
|
|
145 |
$noms = array();
|
|
|
146 |
$introuvables = array();
|
339 |
jpm |
147 |
$introuvablesSyno = array();
|
|
|
148 |
foreach ($resultats as $id => $nom) {
|
336 |
jpm |
149 |
$nn = $nom['num_nom'];
|
339 |
jpm |
150 |
$nnr = $nom['num_nom_retenu'];
|
336 |
jpm |
151 |
$nts = $nom['num_tax_sup'];
|
|
|
152 |
$rg = $nom['rang'];
|
339 |
jpm |
153 |
if ($nnr != '') {
|
|
|
154 |
if ($rg == '180') {
|
340 |
jpm |
155 |
$noms[$nn] = $nom['nom_sci'];
|
336 |
jpm |
156 |
} else {
|
339 |
jpm |
157 |
if ($nn == $nnr) {// nom retenu
|
|
|
158 |
if (isset($noms[$nts])) {
|
|
|
159 |
$noms[$nn] = $noms[$nts];
|
|
|
160 |
} else {
|
|
|
161 |
$introuvables[] = $nn;
|
|
|
162 |
}
|
|
|
163 |
} else {// nom synonyme
|
|
|
164 |
if (isset($noms[$nnr])) {
|
|
|
165 |
$noms[$nn] = $noms[$nnr];
|
|
|
166 |
} else {
|
|
|
167 |
$introuvablesSyno[] = $nom;
|
|
|
168 |
}
|
|
|
169 |
}
|
336 |
jpm |
170 |
}
|
|
|
171 |
}
|
339 |
jpm |
172 |
unset($resultats[$id]);
|
336 |
jpm |
173 |
$this->afficherAvancement("Attribution de leur famille aux noms en cours");
|
335 |
jpm |
174 |
if ($this->stopperLaBoucle($this->getParametre('t'))) break;
|
|
|
175 |
}
|
336 |
jpm |
176 |
echo "\n";
|
339 |
jpm |
177 |
|
|
|
178 |
foreach ($introuvablesSyno as $id => $nom) {
|
|
|
179 |
$nn = $nom['num_nom'];
|
|
|
180 |
$nnr = $nom['num_nom_retenu'];
|
|
|
181 |
if (isset($noms[$nnr])) {
|
|
|
182 |
$noms[$nn] = $noms[$nnr];
|
|
|
183 |
} else {
|
|
|
184 |
$introuvables[] = $nn;
|
|
|
185 |
}
|
|
|
186 |
unset($introuvablesSyno[$id]);
|
|
|
187 |
$this->afficherAvancement("Attribution de leur famille aux synonymes en cours");
|
336 |
jpm |
188 |
}
|
339 |
jpm |
189 |
echo "\n";
|
|
|
190 |
|
|
|
191 |
if (count($introuvables) != 0) {
|
|
|
192 |
$introuvablesNbre = count($introuvables);
|
|
|
193 |
echo "Famille introuvable pour $introuvablesNbre noms ! Voir le log.\n";
|
|
|
194 |
|
|
|
195 |
$logContenu = implode(", \n", $introuvables);
|
|
|
196 |
$logFichier = realpath(dirname(__FILE__)).'/log/famille_introuvable.log';
|
|
|
197 |
echo $logFichier."\n";
|
|
|
198 |
file_put_contents($logFichier, $logContenu);
|
|
|
199 |
}
|
340 |
jpm |
200 |
$this->remplirChpFamille($noms);
|
335 |
jpm |
201 |
}
|
339 |
jpm |
202 |
|
335 |
jpm |
203 |
private function preparerTablePrChpFamille() {
|
|
|
204 |
$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'famille' ";
|
|
|
205 |
$resultat = $this->getBdd()->recuperer($requete);
|
|
|
206 |
if ($resultat === false) {
|
|
|
207 |
$requete = "ALTER TABLE {$this->table} ".
|
|
|
208 |
'ADD famille VARCHAR(255) '.
|
|
|
209 |
'CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ';
|
|
|
210 |
$this->getBdd()->requeter($requete);
|
|
|
211 |
}
|
|
|
212 |
}
|
339 |
jpm |
213 |
|
335 |
jpm |
214 |
private function recupererTuplesPrChpFamille() {
|
339 |
jpm |
215 |
$requete = 'SELECT num_nom, num_nom_retenu, num_tax_sup, rang, nom_sci '.
|
335 |
jpm |
216 |
"FROM {$this->table} ".
|
|
|
217 |
"WHERE rang >= 180 ".
|
340 |
jpm |
218 |
"ORDER BY rang ASC, num_tax_sup ASC, num_nom_retenu DESC ";
|
335 |
jpm |
219 |
$resultat = $this->getBdd()->recupererTous($requete);
|
|
|
220 |
return $resultat;
|
|
|
221 |
}
|
339 |
jpm |
222 |
|
340 |
jpm |
223 |
private function remplirChpFamille($noms) {
|
336 |
jpm |
224 |
foreach ($noms as $id => $famille) {
|
|
|
225 |
$famille = $this->getBdd()->proteger($famille);
|
340 |
jpm |
226 |
$requete = "UPDATE {$this->table} SET famille = $famille WHERE num_nom = $id ";
|
336 |
jpm |
227 |
$resultat = $this->getBdd()->requeter($requete);
|
|
|
228 |
if ($resultat === false) {
|
|
|
229 |
throw new Exception("Erreur d'insertion pour le tuple $id");
|
|
|
230 |
}
|
|
|
231 |
$this->afficherAvancement("Insertion des noms de famille dans la base en cours");
|
|
|
232 |
}
|
|
|
233 |
echo "\n";
|
|
|
234 |
}
|
11 |
jpm |
235 |
|
103 |
jpm |
236 |
private function genererDonneesTestMultiVersion() {
|
|
|
237 |
$contenuSql = $this->recupererContenu(Config::get('chemins.structureSqlTest'));
|
|
|
238 |
$this->executerScripSql($contenuSql);
|
68 |
jpm |
239 |
|
103 |
jpm |
240 |
$table = Config::get('tables.bdtfx');
|
|
|
241 |
$tableTest = Config::get('tables.bdtfxTest');
|
|
|
242 |
$requete = "INSERT INTO $tableTest SELECT * FROM $table";
|
|
|
243 |
$this->getBdd()->requeter($requete);
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
private function supprimerDonneesTestMultiVersion() {
|
|
|
247 |
$tableMeta = Config::get('tables.bdtfxMeta');
|
|
|
248 |
$requete = "DELETE FROM $tableMeta WHERE guid = 'urn:lsid:tela-botanica.org:bdtfx:1.02'";
|
|
|
249 |
$this->getBdd()->requeter($requete);
|
|
|
250 |
|
|
|
251 |
$tableTest = Config::get('tables.bdtfxTest');
|
342 |
jpm |
252 |
$requete = "DROP TABLE IF EXISTS $tableTest";
|
103 |
jpm |
253 |
$this->getBdd()->requeter($requete);
|
|
|
254 |
}
|
130 |
jpm |
255 |
|
|
|
256 |
private function supprimerTous() {
|
342 |
jpm |
257 |
$requete = "DROP TABLE IF EXISTS bdtfx_meta, bdtfx_v1_01, bdtfx_v1_02";
|
130 |
jpm |
258 |
$this->getBdd()->requeter($requete);
|
|
|
259 |
}
|
11 |
jpm |
260 |
}
|
|
|
261 |
?>
|