Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 456 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
370 mathilde 1
<?php
2
/** Exemple lancement:
3
 * /opt/lampp/bin/php -d memory_limit=3500M ~/web/eflore-projets/scripts/cli.php baseflor -a chargerTous
4
*/
5
class Baseflor extends EfloreScript {
433 jpm 6
 
370 mathilde 7
	private $table = null;
433 jpm 8
	private $fichierDonnees = '';
9
	private $log = '';
10
	private $nb_erreurs;
370 mathilde 11
	private $erreurs_ligne;
12
	private $ligne_num;
13
	private $colonne_valeur;
14
	private $colonne_num;
433 jpm 15
	private $type_bio = array();
16
	private $ss_type_bio = array();
17
	private $signes_seuls = array();// basés sur valeurs trouvées (--> pas de légende !)
18
	private $signes_nn_seuls = array();// basés sur valeurs trouvées (--> pas de légende !)
19
	private $intervalles = array();
20
	private $motifs = array();
370 mathilde 21
 
22
	public function executer() {
23
		try {
24
			$this->initialiserProjet('baseflor');
25
			$cmd = $this->getParametre('a');
433 jpm 26
			switch ($cmd) {
27
				case 'chargerStructureSql' :
28
					$this->chargerStructureSql();
370 mathilde 29
					break;
433 jpm 30
				case 'chargerMetadonnees':
31
					$this->chargerMetadonnees();
370 mathilde 32
					break;
433 jpm 33
				case 'chargerOntologies' :
34
					$this->chargerOntologies();
35
					break;
36
				case 'verifierDonnees' :
37
					$this->verifFichier();
38
					break;
39
				case 'chargerDonnees' :
40
					$this->chargerDonnees();
41
					break;
370 mathilde 42
				case 'genererChamps' :
433 jpm 43
					 $this->genererChamps();
370 mathilde 44
					 break;
522 mathilde 45
				case 'chargerTous':
433 jpm 46
					$this->chargerStructureSql();
416 mathilde 47
					$this->chargerMetadonnees();
433 jpm 48
					$this->chargerOntologies();
49
					$this->chargerDonnees();
456 mathilde 50
					$this->genererChamps();
522 mathilde 51
					$this->insererDonneesBaseflorRangSupEcolo();
52
					$this->insererDonneesIndex();
416 mathilde 53
					break;
439 mathilde 54
				case 'insererDonneesRangSup' :
55
					$this->insererDonneesBaseflorRangSupEcolo();
56
					break;
433 jpm 57
				case 'supprimerTous' :
58
					$this->supprimerTous();
59
					break;
439 mathilde 60
				case 'voirRangSup' :
61
					$this->voirRangSup();
62
					break;
63
				case 'voirRangSupEcologie' :
64
					$this->voirRangSupEcologie();
65
					break;
522 mathilde 66
				case 'insererDonneesIndex' :
67
					$this->insererDonneesIndex();
68
					break;
370 mathilde 69
				default :
70
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
71
			}
72
		} catch (Exception $e) {
73
			$this->traiterErreur($e->getMessage());
74
		}
433 jpm 75
	}
370 mathilde 76
 
522 mathilde 77
 
439 mathilde 78
	//-- traitement de la table baseflorRangSupInsertion --//
79
 
80
	private function  getClasseBaseflorRangSupInsertion() {
81
		$conteneur = new Conteneur();
82
		require_once dirname(__FILE__)."/BaseflorRangSupInsertion.php";
522 mathilde 83
		$rangSupInsert = new BaseflorRangSupInsertion($conteneur, $this->getBdd());
439 mathilde 84
		return $rangSupInsert;
85
	}
522 mathilde 86
 
439 mathilde 87
	private function insererDonneesBaseflorRangSupEcolo(){
88
		$rangSupInsert = $this->getClasseBaseflorRangSupInsertion();
89
		$rangSupInsert->insererDonnees();
90
	}
91
 
92
	private function voirRangSup(){
93
		$rangSupInsert = $this->getClasseBaseflorRangSupInsertion();
94
		$rangSupInsert->testAscendantsDeBaseflor();
95
	}
96
 
97
	private function voirRangSupEcologie(){
98
		$rangSupInsert = $this->getClasseBaseflorRangSupInsertion();
99
		$rangSupInsert->testEcologieAscendantsDeBaseflor();
100
	}
101
 
102
 
522 mathilde 103
	//-- traitement de la table baseflorIndex --//
104
 
105
	private function  getClasseBaseflorIndex() {
106
		$conteneur = new Conteneur();
107
		require_once dirname(__FILE__)."/BaseflorIndex.php";
108
		$Index = new BaseflorIndex($conteneur, $this->getBdd());
109
		return $Index;
110
	}
111
 
112
	private function insererDonneesIndex(){
113
		$Index= $this->getClasseBaseflorIndex();
114
		$Index->insererDonnees();
115
	}
116
 
117
 
439 mathilde 118
	//-- traitement de la table generer champs --//
522 mathilde 119
 
433 jpm 120
	private function genererChamps(){
121
		$this->initialiserGenerationChamps();
122
		$this->ajouterChamps();
123
		$this->analyserChampsExistant();
124
	}
370 mathilde 125
 
433 jpm 126
	private function initialiserGenerationChamps() {
127
		$this->table = Config::get('tables.donnees');
128
	}
370 mathilde 129
 
433 jpm 130
	private function ajouterChamps() {
131
		$this->preparerTablePrChpsBDNT();
132
		$this->preparerTablePrChpsNumTaxon();
133
		$this->preparerTablePrChpsNumNomen();
134
	}
135
 
136
	private function preparerTablePrChpsBDNT() {
137
		$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'BDNT' ";
138
		$resultat = $this->getBdd()->recuperer($requete);
139
		if ($resultat === false) {
140
			$requete = 	"ALTER TABLE {$this->table} ".
141
					'ADD BDNT VARCHAR( 6 ) '.
142
					'CHARACTER SET utf8 COLLATE utf8_general_ci '.
143
					'NOT NULL AFTER catminat_code ';
144
			$this->getBdd()->requeter($requete);
145
		}
146
	}
147
 
148
	private function preparerTablePrChpsNumTaxon() {
149
		$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'num_taxon' ";
150
		$resultat = $this->getBdd()->recuperer($requete);
151
		if ($resultat === false) {
152
			$requete = "ALTER TABLE {$this->table} ".
153
					'ADD num_taxon INT( 10 ) NOT NULL '.
154
					'AFTER catminat_code';
155
			$this->getBdd()->requeter($requete);
156
		}
157
	}
158
 
159
	private function preparerTablePrChpsNumNomen() {
160
		$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'num_nomen' ";
161
		$resultat = $this->getBdd()->recuperer($requete);
162
		if ($resultat === false) {
163
			$requete = "ALTER TABLE {$this->table} ".
164
					'ADD num_nomen INT( 10 ) NOT NULL '.
165
					'AFTER catminat_code';
166
			$this->getBdd()->requeter($requete);
167
		}
168
	}
370 mathilde 169
 
433 jpm 170
	private function analyserChampsExistant() {
171
		$resultats = $this->recupererTuplesNumsOriginels();
172
		foreach ($resultats as $chps) {
173
			$cle = $chps['cle'];
174
			$nno = $chps['num_nomen_originel'];
175
			$nto = $chps['num_taxon_originel'];
370 mathilde 176
 
433 jpm 177
			$valeurs = array();
178
			$valeurs["BDNT"] = $this->genererChpsBDNT($nno, $nto);
179
			$valeurs["num_taxon"] = $this->genererChpsNumTaxon($nto);
180
			$valeurs["num_nomen"] = $this->genererChpsNumNomen($nno);
370 mathilde 181
 
433 jpm 182
			$this->remplirChamps($cle, $valeurs);
370 mathilde 183
 
433 jpm 184
			$this->afficherAvancement("Insertion des valeurs dans la base en cours");
185
		}
186
		echo "\n";
187
	}
188
 
189
	private function recupererTuplesNumsOriginels(){
190
		$requete = "SELECT cle, num_taxon_originel, num_nomen_originel FROM {$this->table} ";
191
		$resultat = $this->getBdd()->recupererTous($requete);
192
		return $resultat;
193
	}
194
 
195
	private function genererChpsBDNT($nno, $nto) {
196
		$bdnt = '';
197
		if (preg_match("/^([AB])[0-9]+$/", $nno, $retour) || preg_match("/^([AB])[0-9]+$/", $nto, $retour)){
198
			if ($retour[1]=='A') {
199
				$bdnt = "BDAFX";
200
			} else {
201
				$bdnt = "BDBFX";
202
			}
203
		} elseif (($nno == 'nc') && ($nto == 'nc')) {
204
			$bdnt = "nc";
205
		} else {
206
			$bdnt = "BDTFX";
207
		}
208
		return $bdnt;
209
	}
210
 
211
	private function genererChpsNumTaxon($nto){
212
		$num_taxon = '';
213
		if (preg_match("/^[AB]([0-9]+)$/", $nto, $retour)) {
214
			$num_taxon = intval($retour[1]);
215
		} elseif($nto == 'nc') {
216
			$num_taxon = 0;
217
		} else {
218
			$num_taxon = intval($nto);
219
		}
220
		return $num_taxon;
221
	}
222
 
223
	private function genererChpsNumNomen($nno) {
224
		$num_nomen = '';
225
		if (preg_match("/^[AB]([0-9]+)$/", $nno, $retour)) {
226
			$num_nomen = intval($retour[1]);
227
		} elseif ($nno == 'nc') {
228
			$num_nomen = 0;
229
		} else {
230
			$num_nomen = intval($nno);
231
		}
232
		return $num_nomen;
233
	}
234
 
235
	 private function remplirChamps($cle, $valeurs) {
236
		foreach ($valeurs as $nomChamp => $valeurChamp) {
237
			$valeurChamp = $this->getBdd()->proteger($valeurChamp);
238
			$requete = "UPDATE {$this->table} SET $nomChamp = $valeurChamp WHERE cle = $cle ";
239
			$resultat = $this->getBdd()->requeter($requete);
240
			if ($resultat === false) {
241
				throw new Exception("Erreur d'insertion pour le tuple clé = $cle");
242
			}
243
		}
244
	}
245
 
246
	//+------------------------------------------------------------------------------------------------------+
247
	// chargements, suppression, exécution
248
 
249
	protected function chargerMetadonnees() {
250
		$contenuSql = $this->recupererContenu(Config::get('chemins.metadonnees'));
251
		$this->executerScripSql($contenuSql);
252
	}
253
 
254
	private function chargerOntologies() {
255
		$chemin = Config::get('chemins.ontologies');
256
		$table = Config::get('tables.ontologies');
257
		$requete = "LOAD DATA INFILE '$chemin' ".
258
			"REPLACE INTO TABLE $table ".
259
			'CHARACTER SET utf8 '.
260
			'FIELDS '.
261
			"	TERMINATED BY '\t' ".
262
			"	ENCLOSED BY '' ".
263
			"	ESCAPED BY '\\\' "
264
			;
265
		$this->getBdd()->requeter($requete);
266
	}
267
 
268
	protected function chargerStructureSql() {
269
		$contenuSql = $this->recupererContenu(Config::get('chemins.structureSql'));
270
		$this->executerScripSql($contenuSql);
271
	}
272
 
273
	protected function executerScripSql($sql) {
274
		$requetes = Outils::extraireRequetes($sql);
275
		foreach ($requetes as $requete) {
276
			$this->getBdd()->requeter($requete);
277
		}
278
	}
279
 
280
	private function chargerDonnees() {
281
		$this->verifFichier();
282
		if ($this->nb_erreurs > 0) {
283
			$e = "Je ne peux pas charger les données car le fichier comporte des erreurs.".
284
					"Voir le fichier baseflor_verif.txt\n";
285
			throw new Exception($e);
286
		}
287
 
288
		$table = Config::get('tables.donnees');
289
		$requete = "LOAD DATA INFILE '".Config::get('chemins.donnees')."' ".
290
			"REPLACE INTO TABLE $table ".
291
			'CHARACTER SET utf8 '.
292
			'FIELDS '.
293
			"	TERMINATED BY '\t' ".
294
			"	ENCLOSED BY '' ".
295
			"	ESCAPED BY '\\\'";
296
		$this->getBdd()->requeter($requete);
297
	}
298
 
299
	private function supprimerTous() {
522 mathilde 300
		$requete = "DROP TABLE IF EXISTS baseflor_meta, baseflor_ontologies, baseflor_v2012_03_19,".
301
			" baseflor_rang_sup_ecologie_v2012_03_19, baseflor_index_v2012_03_19 ";
433 jpm 302
		$this->getBdd()->requeter($requete);
303
	}
304
 
305
	//+------------------------------------------------------------------------------------------------------+
306
	// vérifications de données
307
 
308
	//verifie la cohérence des valeurs des colonnes
309
	private function verifFichier(){
310
		$this->initialiserParametresVerif();
311
 
312
		$lignes = file($this->fichierDonnees, FILE_IGNORE_NEW_LINES);
313
		if ($lignes != false) {
314
			$this->ajouterAuLog("!!! REGARDEZ LES COLONNES DANS NUMERO_COLONNES_IMPORTANT.TXT.");
315
			foreach ($lignes as $this->ligne_num => $ligne) {
316
				$this->verifierErreursLigne($ligne);
317
				$this->afficherAvancement("Vérification des lignes");
318
			}
319
			echo "\n";
320
		} else {
321
			$this->traiterErreur("Le fichier {$this->fichierDonnees} ne peut pas être ouvert.");
322
		}
323
 
324
		if ($this->nb_erreurs == 0) {
325
			$this->ajouterAuLog("Il n'y a pas d'erreurs.");
326
		}
327
		$this->traiterInfo($this->nb_erreurs." erreurs");
328
 
329
		$this->ecrireFichierLog();
330
	}
331
 
332
	//vérifie par colonnes les erreurs d'une ligne
333
	private function verifierErreursLigne($ligne){
334
		$this->erreurs_ligne = array();
335
		$colonnes = explode("\t", $ligne);
336
		if (isset($colonnes)) {
337
			foreach ($colonnes as $this->colonne_num => $this->colonne_valeur) {
338
				if (( $this->colonne_num > 0 && $this->colonne_num < 15 )
339
						|| $this->colonne_num == 16
340
						|| ($this->colonne_num > 18 && $this->colonne_num < 23)
341
						|| $this->colonne_num > 39) {
342
					$this->verifierColonne();
343
				} elseif ($this->colonne_num == 15) {
344
					$this->verifierTypeBio();
345
				} elseif ($this->colonne_num >= 23 && $this->colonne_num <= 32) {
346
					$this->verifierIntervalles($this->colonne_valeur);
347
				} elseif ($this->colonne_num >= 33 && $this->colonne_num < 41) {
348
					$this->verifierValeursIndic();
349
				}
350
			}
351
		} else {
352
			$message = "Ligne {$this->ligne_num} : pas de tabulation";
353
			$this->ajouterAuLog($message);
354
		}
355
 
356
		$this->controlerErreursLigne();
357
	}
358
 
359
	private function verifierColonne(){
360
		$motif = $this->motifs[$this->colonne_num];
361
		if (preg_match($motif, $this->colonne_valeur) == 0 && $this->verifierSiVide() == false){
362
			$this->erreurs_ligne[$this->colonne_num] = $this->colonne_valeur;
363
		}
364
	}
365
 
366
	private function verifierSiVide(){
367
		$vide = ($this->colonne_valeur  == '') ? true : false;
368
		return $vide;
369
	}
370
 
371
	private function verifierTypeBio(){
372
		if (preg_match("/(.+)\((.+)\)$/", $this->colonne_valeur, $retour) == 1) {
373
			$this->verifierTypeEtSsType($retour[1]);
374
			$this->verifierTypeEtSsType($retour[2]);
375
		} else {
376
			$this->verifierTypeEtSsType($this->colonne_valeur);
377
		}
378
	}
379
 
380
	private function verifierTypeEtSsType($chaine_a_verif){
381
		if (preg_match("/^([a-zA-Zé]+)\-(.+)$|^([a-zA-Zé]+[^\-])$/", $chaine_a_verif, $retour) == 1) {
382
			$type = (isset($retour[3])) ? $retour[3] : $retour[1];
383
			$this->verifierType($type);
384
 
385
			$sousType = $retour[2];
386
			$this->verifierSousType($sousType);
387
		}
388
	}
389
 
390
	private function verifierType($type) {
391
		if (in_array($type, $this->type_bio) == false) {
392
			$this->erreurs_ligne[$this->colonne_num] = $this->colonne_valeur;
393
		}
394
	}
395
 
396
	private function verifierSousType($sousType) {
397
		if ($sousType != ''){
398
			$ss_type = explode('-', $sousType);
399
			foreach ($ss_type as $sst) {
400
				if (in_array($sst, $this->ss_type_bio) == false) {
401
					$this->erreurs_ligne[$this->colonne_num] = $this->colonne_valeur;
402
				}
403
			}
404
		}
405
	}
406
 
407
	private function verifierIntervalles($valeur){
408
		if ($valeur != '') {
409
			list($min, $max) = explode('-', $this->intervalles[$this->colonne_num]);
410
			if ($valeur < $min || $valeur > $max){
411
				$this->erreurs_ligne[$this->colonne_num] = $this->colonne_valeur;
412
			}
413
		}
414
	}
415
 
416
	private function verifierValeursIndic(){
417
		if (preg_match("/^([^0-9])*([0-9]+)([^0-9])*$/", $this->colonne_valeur, $retour) == 1){
418
			$this->verifierIntervalles($retour[2]);
419
			if (isset($retour[3]) && in_array($retour[3], $this->signes_nn_seuls) == false){
420
				$this->erreurs_ligne[$this->colonne_num] = $this->colonne_valeur;
421
			}
422
			if ($retour[1] != '-' && $retour[1] != ''){
423
				$this->erreurs_ligne[$this->colonne_num] = $this->colonne_valeur;
424
			}
425
		} elseif (in_array( $this->colonne_valeur, $this->signes_seuls) == false && $this->verifierSiVide() == false) {
426
			$this->erreurs_ligne[$this->colonne_num] = $this->colonne_valeur;
427
		}
428
	}
429
 
430
	private function controlerErreursLigne() {
431
		$nbreErreursLigne = count($this->erreurs_ligne);
432
		$this->nb_erreurs += $nbreErreursLigne;
433
		if ($nbreErreursLigne != 0) {
434
			$this->ajouterAuLog("Erreurs sur la ligne {$this->ligne_num}");
435
			$ligneLog = '';
436
			foreach ($this->erreurs_ligne as $cle => $v){
437
				$ligneLog .= "colonne $cle : $v - ";
438
			}
439
			$this->ajouterAuLog($ligneLog);
440
		}
441
	}
442
 
443
	//+------------------------------------------------------------------------------------------------------+
444
	// Chargement Paramètres
445
 
446
	private function initialiserParametresVerif() {
447
		$this->nb_erreurs = 0;
448
		$this->fichierDonnees = Config::get('chemins.donnees');
449
		$this->type_bio = $this->getParametreTableau('Parametres.typesBio');
450
		$this->ss_type_bio = $this->getParametreTableau('Parametres.sousTypesBio');
451
		$this->signes_seuls = $this->getParametreTableau('Parametres.signesSeuls');
452
		$this->signes_nn_seuls = $this->getParametreTableau('Parametres.signesNonSeuls');
453
		$this->intervalles = $this->inverserTableau($this->getParametreTableau('Parametres.intervalles'));
454
		$this->motifs = $this->inverserTableau($this->getParametreTableau('Parametres.motifs'));
439 mathilde 455
 
433 jpm 456
	}
457
 
458
	private function getParametreTableau($cle) {
459
		$tableau = array();
460
		$parametre = Config::get($cle);
461
		if (empty($parametre) === false) {
462
			$tableauPartiel = explode(',', $parametre);
463
			$tableauPartiel = array_map('trim', $tableauPartiel);
464
			foreach ($tableauPartiel as $champ) {
465
				if (strpos($champ, '=') !== false && strlen($champ) >= 3) {
466
					list($cle, $val) = explode('=', $champ);
467
					$tableau[trim($cle)] = trim($val);
468
				} else {
469
					$tableau[] = trim($champ);
470
				}
471
			}
472
		}
473
		return $tableau;
474
	}
475
 
476
	private function inverserTableau($tableau) {
477
		$inverse = array();
478
		foreach ($tableau as $cle => $valeurs) {
479
			$valeurs = explode(';', $valeurs);
480
			foreach ($valeurs as $valeur) {
481
				$inverse[$valeur] = $cle;
482
			}
483
		}
484
		return $inverse;
485
	}
486
 
487
	//+------------------------------------------------------------------------------------------------------+
488
	// Gestion du Log
489
 
490
	private function ajouterAuLog($txt) {
491
		$this->log .= "$txt\n";
492
	}
493
 
494
	private function ecrireFichierLog() {
495
		$fichierLog = dirname(__FILE__).'/log/verification.log';
496
		file_put_contents($fichierLog, $this->log);
497
	}
370 mathilde 498
}
499
?>