Subversion Repositories eFlore/Projets.eflore-projets

Rev

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