Subversion Repositories eFlore/Projets.eflore-projets

Rev

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