Subversion Repositories eFlore/Projets.eflore-projets

Rev

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