Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 456 | Rev 591 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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