Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 372 | Rev 383 | 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 {
6
 
7
	private $table = null;
8
	private $nb_erreurs;
9
	private $erreurs_ligne;
10
	private $ligne_num;
11
	private $colonne_valeur;
12
	private $colonne_num;
13
	private $type_bio = array(
14
		"A","a","B","b","C","c","Cfru","cfru","Csuf","csuf","Ccou","ccou","H","h","Heri",
15
		"heri","Hsto","hsto","Hces","hces","Hros","hros","Hrub","hrub","Hbis","hbis","G",
16
		"g","Gbul","gbul","Gtub","gtub","Grhi","grhi","T","t","Tver","tver","Test","test");
17
	private $ss_type_bio = array("aqua", "lia", "épi", "hpar", "par", "suc", "semp", "cad", "car");
18
	//les deux suivants basés sur valeurs trouvées (--> pas de légende !)
19
	private $signes_seuls = array("?", "x", "x~", "x=","xb","xB","-");
20
	private $signes_nn_seuls = array("~", "=","b","B");
21
	//paramètres pour lancement de fonctions sur colonnes
22
	private $verifierIntervalle = array(23 => array(1,9), 24 => array(1,9), 25 => array(1,9),
23
		26 => array(1,9), 27 => array(1,12), 28 => array(1,9), 29 => array(1,9), 30 => array(0,9),
24
		31 => array(1,9), 32 => array(1,9), 33 => array(1,9), 34 => array(1,9), 35 => array(1,9),
25
		36 => array(1,12), 37 => array(1,9), 38 => array(1,9), 39 => array(0,9),);
26
	private $verifierColonnesBasiques = array(1 =>"/(^[0-9]*\/)|(inconnu)/", 2 => "/(^[AB]?[0-9]+$)|(^nc$)/",
27
		3 => "/(^[AB]?[0-9]+$)|(^nc$)/", 4 => "/[^0-9]+/", 5 => "/[^0-9]+/", 6 => "/[^0-9]+/", 7 => "/[^0-9]+/",
28
		8 => "/[^0-9]+/", 9 => "/[^0-9]+/", 10 => "/[^0-9]+/", 11 => "/[^0-9]+/", 12 => "/[^0-9]+/",
29
		13 => "/[^0-9]+/", 14 => "/^([1-9]|1[0-2])(\-([1-9]|1[0-2]))*$/", 16 => "/[^0-9]+/", 19 => "/[^0-9]+/",
30
		20 => "/[^0-9]+/", 21 => "/[^0-9]+/", 22 => "/[^0-9]+/",  40 => "/[^0-9]+/",
31
		41 => "/[^0-9]+/", 42 => "/[^0-9]+/", 43 => "/[^0-9]+/", 44 => "/[^0-9]+/", 45 => "/[^0-9]+/",
32
		46 => "/[^0-9]+/", 47 => "/[^0-9]+/", 48 => "/[^0-9]+/", 49 => "/[^0-9]+/", 50 => "/[^0-9]+/",
33
		51 => "/[^0-9]+/", 52 => "/[^0-9]+/", 53 => "/[^0-9]+/", 54 => "/[^0-9]+/", 55 => "/[^0-9]+/"
34
		);
35
 
36
 
37
	public function executer() {
38
		// Lancement de l'action demandée
39
		try {
40
			$this->initialiserProjet('baseflor');
41
			$cmd = $this->getParametre('a');
42
		    switch ($cmd) {
43
		    	case 'chargerTous' :
44
		    		$this->supprimerTous();
45
		    		$this->chargerStructureSql();
46
		    		$this->chargerOntologies();
47
		    		$this->verifFichier();
48
	    			if ($this->$nb_erreurs == 0){
372 mathilde 49
		    			$this->chargerDonnees();
50
		    			$This->genererChamps();
370 mathilde 51
	    			} else {
372 mathilde 52
	    				echo "je ne peux pas charger les données car le fichier comporte des erreurs.
53
	    					Voir le fichier baseflor_verif.txt\n";
370 mathilde 54
	    			}
372 mathilde 55
	    			break;
370 mathilde 56
	    		case 'chargerStructureSql' :
57
	    			$this->chargerStructureSql();
58
	    			break;
59
	    		case 'chargerDonnees' :
60
	    			$this->verifFichier();
61
	    			if($this->$nb_erreurs == 0){
62
	    				$this->chargerDonnees();
63
	    			} else {
372 mathilde 64
	    				echo "je ne peux pas charger les données car le fichier comporte des erreurs.
65
	    				Voir le fichier baseflor_verif.txt\n";
370 mathilde 66
	    			}
67
	    			break;
68
				case 'supprimerTous' :
69
					$this->supprimerTous();
70
					break;
71
				case 'chargerOntologies' :
72
					$this->chargerOntologies();
73
					break;
74
				case 'verifierDonnees' :
75
					 $this->verifFichier();
76
					 break;
77
				case 'genererChamps' :
78
					 $this->genererChpsBDNT();
79
					 $this->genererChpsNumTaxon();
80
					 $this->genererChpsNumNomen();
81
					 break;
82
				default :
83
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
84
			}
85
		} catch (Exception $e) {
86
			$this->traiterErreur($e->getMessage());
87
		}
88
    }
89
 
90
   //-- génération de champs --//
91
    private function genererChamps(){
92
    	$this->genererChpsBDNT();
93
    	$this->genererChpsNumTaxon();
94
    	$this->genererChpsNumNomen();
95
    }
96
 
97
    private function genererChpsBDNT(){
98
    	$this->initialiserGenerationChamps();
99
    	$this->preparerTablePrChpsBDNT();
100
    	$resultats = $this->recupererTuplesNumsOriginels();
101
    	$bdnt = array();
102
    	foreach ($resultats as $chps => $res) {
103
    		$cle = $res['cle'];
104
    		$nno = $res['num_nomen_originel'];
105
    		$nto = $res['num_taxon_originel'];
106
    		if ( (preg_match("/^([AB])[0-9]+$/", $nno, $retour)) || (preg_match("/^([AB])[0-9]+$/", $nto, $retour)) ){
107
    			if($retour[1]=='A'){
108
    				$bdnt[$cle]= "BDAFX";
109
    			}else{
110
    				$bdnt[$cle]= "BDBFX";
111
    			}
112
 
113
    		}elseif(($nno == 'nc') && ($nto == 'nc')){
114
    			$bdnt[$cle]= "nc";
115
    		}else{
116
    			$bdnt[$cle]= "BDTFX";
117
    		}
118
 
119
    	}
120
    	$this->remplirChamps($bdnt, "BDNT");
121
    }
122
 
123
    private function genererChpsNumTaxon(){
124
    	$this->initialiserGenerationChamps();
125
    	$this->preparerTablePrChpsNumTaxon();
126
    	$resultats = $this->recupererTuplesNumsOriginels();
127
    	$num_taxon= array();
128
    	foreach ($resultats as $chps => $res) {
129
    		$cle = $res['cle'];
130
    	  	$nto = $res['num_taxon_originel'];
131
    		if  (preg_match("/^[AB]([0-9]+)$/", $nto, $retour)){
132
    			$num_taxon[$cle]= intval($retour[1]);
133
    		}elseif($nto == 'nc'){
134
    			$num_taxon[$cle]= 0;
135
    		}else{
136
    			$num_taxon[$cle]= intval($nto);
137
    		}
138
 
139
    	}
140
    	$this->remplirChamps($num_taxon, "num_taxon");
141
    }
142
 
143
    private function genererChpsNumNomen(){
144
    	$this->initialiserGenerationChamps();
145
    	$this->preparerTablePrChpsNumNomen();
146
    	$resultats = $this->recupererTuplesNumsOriginels();
147
    	$num_nomen= array();
148
    	foreach ($resultats as $chps => $res) {
149
    		$cle = $res['cle'];
150
    		$nno = $res['num_nomen_originel'];
151
    		if  (preg_match("/^[AB]([0-9]+)$/", $nno, $retour)){
152
				$num_nomen[$cle]= intval($retour[1]);
153
    		}elseif($nno == 'nc'){
154
    			$num_nomen[$cle]= 0;
155
    		}else{
156
    			$num_nomen[$cle]=intval($nno);
157
    		}
158
    	}
159
    	$this->remplirChamps($num_nomen, "num_nomen");
160
    }
161
 
162
     private function remplirChamps($champs, $nom_colonne) {
163
    	foreach ($champs as $cle => $val) {
164
    		$val = $this->getBdd()->proteger($val);
165
    		$requete = "UPDATE {$this->table} SET $nom_colonne = $val WHERE cle = $cle ";
166
    		$resultat = $this->getBdd()->requeter($requete);
167
    		if ($resultat === false) {
168
    			throw new Exception("Erreur d'insertion pour le tuple clé = ".$cle);
169
    		}
170
    		$this->afficherAvancement("Insertion des valeurs pour ".$nom_colonne." dans la base en cours");
171
    	}
172
    	echo "\n";
173
    }
174
 
175
 
176
 
177
 
178
    private function initialiserGenerationChamps() {
179
    	$this->table = Config::get('tables.donnees');
180
    }
181
 
182
    private function preparerTablePrChpsBDNT() {
183
    	$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'BDNT' ";
184
    	$resultat = $this->getBdd()->recuperer($requete);
185
    	if ($resultat === false) {
186
    		$requete = 	"ALTER TABLE {$this->table} ".
187
    				'ADD BDNT VARCHAR( 6 ) '.
188
    				'CHARACTER SET utf8 COLLATE utf8_general_ci'.
189
    				' NOT NULL AFTER catminat_code ';
190
    		$this->getBdd()->requeter($requete);
191
    	}
192
    }
193
 
194
    private function preparerTablePrChpsNumTaxon() {
195
    	$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'num_taxon' ";
196
    	$resultat = $this->getBdd()->recuperer($requete);
197
    	if ($resultat === false) {
198
    		$requete = 	"ALTER TABLE {$this->table} ".
199
        				'ADD num_taxon INT( 10 ) NOT NULL'.
200
        				'BEFORE num_taxon_originel';
201
    		$this->getBdd()->requeter($requete);
202
    	}
203
    }
204
 
205
    private function preparerTablePrChpsNumNomen() {
206
    	$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'num_nomen' ";
207
    	$resultat = $this->getBdd()->recuperer($requete);
208
    	if ($resultat === false) {
209
    		$requete = 	"ALTER TABLE {$this->table} ".
210
            				'ADD num_nomen INT( 10 ) NOT NULL'.
211
            				'BEFORE num_taxon_originel';
212
    		$this->getBdd()->requeter($requete);
213
    	}
214
    }
215
    private function recupererTuplesNumsOriginels(){
216
    	$requete = 'SELECT cle, num_taxon_originel, num_nomen_originel '.
217
    					"FROM {$this->table} ";
218
    	$resultat = $this->getBdd()->recupererTous($requete);
219
    	return $resultat;
220
    }
221
 
222
    //-- chargements, suppression, exécution  --//
223
 
224
    private function chargerOntologies() {
225
    	$chemin = Config::get('chemins.ontologies');
226
    	$table = Config::get('tables.ontologies');
227
    	$requete = "LOAD DATA INFILE '$chemin' ".
228
    			"REPLACE INTO TABLE $table ".
229
    			'CHARACTER SET utf8 '.
230
    			'FIELDS '.
231
    			"	TERMINATED BY '\t' ".
232
    			"	ENCLOSED BY '' ".
233
    			"	ESCAPED BY '\\\' "
234
    			;
235
    	$this->getBdd()->requeter($requete);
236
    }
237
 
238
    protected function chargerStructureSql() {
239
    	$contenuSql = $this->recupererContenu(Config::get('chemins.structureSql'));
240
    	$this->executerScripSql($contenuSql);
241
    }
242
 
243
 
244
    protected function executerScripSql($sql) {
245
    	$requetes = Outils::extraireRequetes($sql);
246
    	foreach ($requetes as $requete) {
247
    		$this->getBdd()->requeter($requete);
248
    	}
249
    }
250
 
251
    private function chargerDonnees() {
252
    	$table = Config::get('tables.donnees');
253
    	$requete = "LOAD DATA INFILE '".Config::get('chemins.donnees')."' ".
254
        				"REPLACE INTO TABLE $table ".
255
        				'CHARACTER SET utf8 '.
256
        				'FIELDS '.
257
        				"	TERMINATED BY '\t' ".
258
        				"	ENCLOSED BY '' ".
259
        				"	ESCAPED BY '\\\'";
260
    	$this->getBdd()->requeter($requete);
261
    }
262
 
263
    private function supprimerTous() {
264
    	$requete = "DROP TABLE IF EXISTS baseflor_meta, baseflor_v2012_03_19";
265
    	$this->getBdd()->requeter($requete);
266
    }
267
 
268
 
269
    //-- vérifications de données  --//
270
 
271
    private function verifierSiVide(){
272
    	$bool=false;
273
    	if ( $this->colonne_valeur  == "" ){
274
    		$bool=true;
275
    	}
276
    	return $bool;
277
    }
278
 
279
 
280
    private function verifierIntervallesTous($valeur){
281
    	$intervalle=$this->verifierIntervalle[$this->colonne_num];
282
      	$this->verifierIntervalleChiffres($intervalle[0], $intervalle[1],$valeur);
283
    }
284
 
285
 
286
    private function verifierIntervalleChiffres($mini, $max, $valeur){
287
    	 if (( ($valeur < $mini) || ($valeur > $max) ) && ($valeur != "")){
288
    		$this->erreurs_ligne[$this->colonne_num] = $this->colonne_valeur;
289
   			$this->nb_erreurs++;
290
    	}
291
 
292
    }
293
 
294
 
295
    private function verifierValeursIndic(){
296
    	if (( preg_match("/^([^0-9])*([0-9]+)([^0-9])*$/", $this->colonne_valeur,$retour) == 1  )){
297
    		$this->verifierIntervallesTous($retour[2]);
298
    		if((isset($retour[3]))&&(!(in_array($retour[3], $this->signes_nn_seuls))) ){
299
    			$this->erreurs_ligne[$this->colonne_num] =$this->colonne_valeur;
300
    			$this->nb_erreurs++;
301
    		}
302
    		if(($retour[1] != "-")&&($retour[1]!="") ){
303
    			$this->erreurs_ligne[$this->colonne_num] =$this->colonne_valeur;
304
    			$this->nb_erreurs++;
305
    			if($this->ligne_num==3){
306
    			}
307
    		}
308
    	}elseif(!(in_array( $this->colonne_valeur, $this->signes_seuls))&& !($this->verifierSiVide())) {
309
    			$this->erreurs_ligne[$this->colonne_num] = $this->colonne_valeur;
310
    			$this->nb_erreurs++;
311
    		}
312
    }
313
 
314
 private function verifierTypeBio(){
315
 	if (( preg_match("/(.+)\((.+)\)$/", $this->colonne_valeur,$retour) == 1  )){
316
 		$this->verifType_SsType($retour[1]);
317
 		$this->verifType_SsType($retour[2]);
318
 	}else{
319
 		$this->verifType_SsType($this->colonne_valeur);
320
 	}
321
 }
322
 
323
    private function verifType_SsType($chaine_a_verif){
324
    	//verifie type
325
    	if(( preg_match("/^([a-zA-Zé]+)\-(.+)$|^([a-zA-Zé]+[^\-])$/", $chaine_a_verif, $retour) == 1  )){
326
    		$type=$retour[1];
327
    		if(isset($retour[3])){
328
    			$type=$retour[3];
329
    		}
330
    		 if(!(in_array($type, $this->type_bio))) {
331
    			$this->erreurs_ligne[$this->colonne_num] = $this->colonne_valeur;
332
    			$this->nb_erreurs++;
333
    		}
334
    		//verifie sous_type(s)
335
    		if ($retour[2]!=""){
336
    			$ss_type[0]=$retour[2];
337
    			if (( preg_match("/\-/", $ss_type[0])) == 1 ){
338
    				$ss_type = preg_split("/\-/", $retour[2]);
339
    			}
340
    			foreach($ss_type as $sst){
341
    				if(!(in_array($sst, $this->ss_type_bio))) {
342
    					$this->erreurs_ligne[$this->colonne_num] = $this->colonne_valeur;
343
    					$this->nb_erreurs++;
344
    				}
345
    			}
346
    		}
347
    	}
348
    }
349
 
350
    private function verifierColonne($chaine){
351
    	if ( ( preg_match($chaine, $this->colonne_valeur) == 0 ) && !($this->verifierSiVide()) ){
352
    		$this->erreurs_ligne[$this->colonne_num]=$this->colonne_valeur;
353
    		$this->nb_erreurs++;
354
    	}
355
    }
356
 
357
    //vérifie par colonnes les erreurs d'une ligne
358
   private function verifErreursLignes($colonne_array){
359
   	$this->erreurs_ligne=array();
360
     for ($this->colonne_num = 0; $this->colonne_num < count($colonne_array); $this->colonne_num++){
361
     	 if ( isset($colonne_array) ){
362
     	 	$this->colonne_valeur = $colonne_array[$this->colonne_num];
363
   			if ( (( ($this->colonne_num > 0) && ($this->colonne_num < 15) ) || ( $this->colonne_num == 16 )
364
   				|| (($this->colonne_num > 18 ) && ( $this->colonne_num < 23 ) )
365
   				||  ( $this->colonne_num > 39 ))  ){
366
   				$motif = $this->verifierColonnesBasiques[$this->colonne_num];
367
   			  	$this->verifierColonne($motif);
368
   			}elseif ( $this->colonne_num == 15 ){
369
   				$this->verifierTypeBio();
370
   			} elseif (($this->colonne_num > 22 ) && ( $this->colonne_num < 33 )){
371
   				$this->verifierIntervallesTous($this->colonne_valeur);
372
   			}elseif (( $this->colonne_num > 32) && ( $this->colonne_num < 41)){
373
   				  $this->verifierValeursIndic();
374
   			}
375
   		} else {
376
   			echo "ligne ".$this->ligne_num.": pas de tabulation  \n ";
377
   		}
378
   }
379
 
380
}
381
 
382
    //verifie la cohérence des valeurs des colonnes
383
    private function verifFichier(){
384
    	$dernier_nb_erreur =0;
373 mathilde 385
    	if (file_exists(Config::get('chemins.donnees_verif'))) {
386
    		unlink(Config::get('chemins.donnees_verif'));
387
    	}
370 mathilde 388
    	$fichier = fopen(Config::get('chemins.donnees'), "r");
389
    	$fichier2 = fopen(Config::get('chemins.donnees_verif'), "w");
390
    	$this->valeurs=array();
391
    	$this->nb_erreurs=0;
392
    	if ( ($fichier != false) && ($fichier2 != false) ){
393
    		$this->ligne_num=1;
394
    		while ( !feof($fichier) ) {
395
    			if ($this->ligne_num == 1){
396
    				fputs($fichier2," !!! REGARDEZ LES COLONNES DANS NUMERO_COLONNES_IMPORTANT.TXT.\n");
397
    			}
398
    			$this->ligne_num++;
399
    			$ligne = fgets($fichier);
400
    			$col = preg_split("/	/", $ligne);
401
    			$this->verifErreursLignes($col);
402
    			//rédaction du fichier de vérif
403
    			if(count($this->nb_erreurs)!= 0){
404
    				if($dernier_nb_erreur != $this->nb_erreurs){
405
    					$dernier_nb_erreur = $this->nb_erreurs;
406
    					fputs($fichier2," \n erreurs sur la ligne".$this->ligne_num."\n");
407
    					foreach ($this->erreurs_ligne as $cle => $v){
408
    						fputs($fichier2,"colonne ".$cle." : ".$v." - ");
409
    					}
410
    				}
411
    			}
412
    		}
413
    	}
414
    	if ($this->nb_erreurs == 0){
415
    		fputs($fichier2,"/n il n'y a pas d'erreurs./n");
416
    	}
417
    	echo $this->nb_erreurs."erreurs";
418
    	fclose($fichier);
419
    	fclose($fichier2);
420
    }
421
 
422
 
423
 
424
}
425
 
426
?>