Subversion Repositories eFlore/Projets.eflore-projets

Rev

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