Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 591 | Rev 688 | 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;
603 mathilde 8
 
9
 
370 mathilde 10
	public function executer() {
11
		try {
12
			$this->initialiserProjet('baseflor');
13
			$cmd = $this->getParametre('a');
433 jpm 14
			switch ($cmd) {
15
				case 'chargerStructureSql' :
16
					$this->chargerStructureSql();
370 mathilde 17
					break;
433 jpm 18
				case 'chargerMetadonnees':
19
					$this->chargerMetadonnees();
370 mathilde 20
					break;
433 jpm 21
				case 'chargerOntologies' :
22
					$this->chargerOntologies();
23
					break;
603 mathilde 24
				case 'verifierFichier' :
25
					$this->verifierFichier();
433 jpm 26
					break;
27
				case 'chargerDonnees' :
28
					$this->chargerDonnees();
29
					break;
370 mathilde 30
				case 'genererChamps' :
433 jpm 31
					 $this->genererChamps();
370 mathilde 32
					 break;
522 mathilde 33
				case 'chargerTous':
433 jpm 34
					$this->chargerStructureSql();
416 mathilde 35
					$this->chargerMetadonnees();
433 jpm 36
					$this->chargerOntologies();
37
					$this->chargerDonnees();
456 mathilde 38
					$this->genererChamps();
522 mathilde 39
					$this->insererDonneesBaseflorRangSupEcolo();
40
					$this->insererDonneesIndex();
416 mathilde 41
					break;
439 mathilde 42
				case 'insererDonneesRangSup' :
43
					$this->insererDonneesBaseflorRangSupEcolo();
44
					break;
433 jpm 45
				case 'supprimerTous' :
46
					$this->supprimerTous();
47
					break;
439 mathilde 48
				case 'voirRangSup' :
49
					$this->voirRangSup();
50
					break;
51
				case 'voirRangSupEcologie' :
52
					$this->voirRangSupEcologie();
53
					break;
522 mathilde 54
				case 'insererDonneesIndex' :
55
					$this->insererDonneesIndex();
56
					break;
370 mathilde 57
				default :
58
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
59
			}
60
		} catch (Exception $e) {
61
			$this->traiterErreur($e->getMessage());
62
		}
433 jpm 63
	}
370 mathilde 64
 
522 mathilde 65
 
439 mathilde 66
	//-- traitement de la table baseflorRangSupInsertion --//
67
 
68
	private function  getClasseBaseflorRangSupInsertion() {
69
		$conteneur = new Conteneur();
70
		require_once dirname(__FILE__)."/BaseflorRangSupInsertion.php";
522 mathilde 71
		$rangSupInsert = new BaseflorRangSupInsertion($conteneur, $this->getBdd());
439 mathilde 72
		return $rangSupInsert;
73
	}
522 mathilde 74
 
439 mathilde 75
	private function insererDonneesBaseflorRangSupEcolo(){
76
		$rangSupInsert = $this->getClasseBaseflorRangSupInsertion();
77
		$rangSupInsert->insererDonnees();
78
	}
79
 
80
	private function voirRangSup(){
81
		$rangSupInsert = $this->getClasseBaseflorRangSupInsertion();
82
		$rangSupInsert->testAscendantsDeBaseflor();
83
	}
84
 
85
	private function voirRangSupEcologie(){
86
		$rangSupInsert = $this->getClasseBaseflorRangSupInsertion();
87
		$rangSupInsert->testEcologieAscendantsDeBaseflor();
88
	}
89
 
90
 
522 mathilde 91
	//-- traitement de la table baseflorIndex --//
92
 
93
	private function  getClasseBaseflorIndex() {
94
		$conteneur = new Conteneur();
95
		require_once dirname(__FILE__)."/BaseflorIndex.php";
96
		$Index = new BaseflorIndex($conteneur, $this->getBdd());
97
		return $Index;
98
	}
99
 
100
	private function insererDonneesIndex(){
101
		$Index= $this->getClasseBaseflorIndex();
102
		$Index->insererDonnees();
103
	}
104
 
105
 
439 mathilde 106
	//-- traitement de la table generer champs --//
522 mathilde 107
 
433 jpm 108
	private function genererChamps(){
109
		$this->initialiserGenerationChamps();
110
		$this->ajouterChamps();
111
		$this->analyserChampsExistant();
112
	}
370 mathilde 113
 
433 jpm 114
	private function initialiserGenerationChamps() {
115
		$this->table = Config::get('tables.donnees');
116
	}
370 mathilde 117
 
433 jpm 118
	private function ajouterChamps() {
119
		$this->preparerTablePrChpsBDNT();
120
		$this->preparerTablePrChpsNumTaxon();
121
		$this->preparerTablePrChpsNumNomen();
122
	}
123
 
124
	private function preparerTablePrChpsBDNT() {
125
		$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'BDNT' ";
126
		$resultat = $this->getBdd()->recuperer($requete);
127
		if ($resultat === false) {
128
			$requete = 	"ALTER TABLE {$this->table} ".
129
					'ADD BDNT VARCHAR( 6 ) '.
130
					'CHARACTER SET utf8 COLLATE utf8_general_ci '.
131
					'NOT NULL AFTER catminat_code ';
132
			$this->getBdd()->requeter($requete);
133
		}
134
	}
135
 
136
	private function preparerTablePrChpsNumTaxon() {
137
		$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'num_taxon' ";
138
		$resultat = $this->getBdd()->recuperer($requete);
139
		if ($resultat === false) {
140
			$requete = "ALTER TABLE {$this->table} ".
141
					'ADD num_taxon INT( 10 ) NOT NULL '.
142
					'AFTER catminat_code';
143
			$this->getBdd()->requeter($requete);
144
		}
145
	}
146
 
147
	private function preparerTablePrChpsNumNomen() {
148
		$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'num_nomen' ";
149
		$resultat = $this->getBdd()->recuperer($requete);
150
		if ($resultat === false) {
151
			$requete = "ALTER TABLE {$this->table} ".
152
					'ADD num_nomen INT( 10 ) NOT NULL '.
153
					'AFTER catminat_code';
154
			$this->getBdd()->requeter($requete);
155
		}
156
	}
370 mathilde 157
 
433 jpm 158
	private function analyserChampsExistant() {
159
		$resultats = $this->recupererTuplesNumsOriginels();
160
		foreach ($resultats as $chps) {
161
			$cle = $chps['cle'];
162
			$nno = $chps['num_nomen_originel'];
163
			$nto = $chps['num_taxon_originel'];
370 mathilde 164
 
433 jpm 165
			$valeurs = array();
166
			$valeurs["BDNT"] = $this->genererChpsBDNT($nno, $nto);
167
			$valeurs["num_taxon"] = $this->genererChpsNumTaxon($nto);
168
			$valeurs["num_nomen"] = $this->genererChpsNumNomen($nno);
370 mathilde 169
 
433 jpm 170
			$this->remplirChamps($cle, $valeurs);
370 mathilde 171
 
433 jpm 172
			$this->afficherAvancement("Insertion des valeurs dans la base en cours");
173
		}
174
		echo "\n";
175
	}
176
 
177
	private function recupererTuplesNumsOriginels(){
178
		$requete = "SELECT cle, num_taxon_originel, num_nomen_originel FROM {$this->table} ";
179
		$resultat = $this->getBdd()->recupererTous($requete);
180
		return $resultat;
181
	}
182
 
183
	private function genererChpsBDNT($nno, $nto) {
184
		$bdnt = '';
185
		if (preg_match("/^([AB])[0-9]+$/", $nno, $retour) || preg_match("/^([AB])[0-9]+$/", $nto, $retour)){
186
			if ($retour[1]=='A') {
187
				$bdnt = "BDAFX";
188
			} else {
189
				$bdnt = "BDBFX";
190
			}
191
		} elseif (($nno == 'nc') && ($nto == 'nc')) {
192
			$bdnt = "nc";
193
		} else {
194
			$bdnt = "BDTFX";
195
		}
196
		return $bdnt;
197
	}
198
 
199
	private function genererChpsNumTaxon($nto){
200
		$num_taxon = '';
201
		if (preg_match("/^[AB]([0-9]+)$/", $nto, $retour)) {
202
			$num_taxon = intval($retour[1]);
203
		} elseif($nto == 'nc') {
204
			$num_taxon = 0;
205
		} else {
206
			$num_taxon = intval($nto);
207
		}
208
		return $num_taxon;
209
	}
210
 
211
	private function genererChpsNumNomen($nno) {
212
		$num_nomen = '';
213
		if (preg_match("/^[AB]([0-9]+)$/", $nno, $retour)) {
214
			$num_nomen = intval($retour[1]);
215
		} elseif ($nno == 'nc') {
216
			$num_nomen = 0;
217
		} else {
218
			$num_nomen = intval($nno);
219
		}
220
		return $num_nomen;
221
	}
222
 
223
	 private function remplirChamps($cle, $valeurs) {
224
		foreach ($valeurs as $nomChamp => $valeurChamp) {
225
			$valeurChamp = $this->getBdd()->proteger($valeurChamp);
226
			$requete = "UPDATE {$this->table} SET $nomChamp = $valeurChamp WHERE cle = $cle ";
227
			$resultat = $this->getBdd()->requeter($requete);
228
			if ($resultat === false) {
229
				throw new Exception("Erreur d'insertion pour le tuple clé = $cle");
230
			}
231
		}
232
	}
233
 
234
	//+------------------------------------------------------------------------------------------------------+
235
	// chargements, suppression, exécution
236
 
237
	protected function chargerMetadonnees() {
238
		$contenuSql = $this->recupererContenu(Config::get('chemins.metadonnees'));
603 mathilde 239
		$this->executerScriptSql($contenuSql);
433 jpm 240
	}
241
 
242
	private function chargerOntologies() {
243
		$chemin = Config::get('chemins.ontologies');
244
		$table = Config::get('tables.ontologies');
245
		$requete = "LOAD DATA INFILE '$chemin' ".
246
			"REPLACE INTO TABLE $table ".
247
			'CHARACTER SET utf8 '.
248
			'FIELDS '.
249
			"	TERMINATED BY '\t' ".
250
			"	ENCLOSED BY '' ".
251
			"	ESCAPED BY '\\\' "
252
			;
253
		$this->getBdd()->requeter($requete);
254
	}
255
 
256
	protected function chargerStructureSql() {
257
		$contenuSql = $this->recupererContenu(Config::get('chemins.structureSql'));
603 mathilde 258
		$this->executerScriptSql($contenuSql);
433 jpm 259
	}
260
 
603 mathilde 261
	protected function executerScriptSql($sql) {
433 jpm 262
		$requetes = Outils::extraireRequetes($sql);
263
		foreach ($requetes as $requete) {
264
			$this->getBdd()->requeter($requete);
265
		}
266
	}
267
 
591 mathilde 268
	private function chargerDonnees() {
603 mathilde 269
		$nb_err = $this->verifierFichier();
270
		if ($nb_err > 0) {
433 jpm 271
			$e = "Je ne peux pas charger les données car le fichier comporte des erreurs.".
272
					"Voir le fichier baseflor_verif.txt\n";
273
			throw new Exception($e);
274
		}
275
 
276
		$table = Config::get('tables.donnees');
277
		$requete = "LOAD DATA INFILE '".Config::get('chemins.donnees')."' ".
278
			"REPLACE INTO TABLE $table ".
279
			'CHARACTER SET utf8 '.
280
			'FIELDS '.
281
			"	TERMINATED BY '\t' ".
282
			"	ENCLOSED BY '' ".
283
			"	ESCAPED BY '\\\'";
284
		$this->getBdd()->requeter($requete);
285
	}
286
 
287
	private function supprimerTous() {
603 mathilde 288
		$requete = "DROP TABLE IF EXISTS baseflor_meta, baseflor_ontologies, baseflor_v2012_05_08,".
289
			" baseflor_rang_sup_ecologie_v2012_05_08, baseflor_index_v2012_05_08 ";
433 jpm 290
		$this->getBdd()->requeter($requete);
291
	}
292
 
603 mathilde 293
	//++------------------------------------verifierFichier------------------------------------------++//
433 jpm 294
 
603 mathilde 295
	private function  getClasseBaseflorVerif() {
296
		$conteneur = new Conteneur();
297
		require_once dirname(__FILE__)."/BaseflorVerif.php";
298
		$verif = new BaseflorVerif($conteneur,'baseflor');
299
		return $verif;
433 jpm 300
	}
603 mathilde 301
 
302
	private function verifierFichier() {
303
		$verif = $this->getClasseBaseflorVerif();
304
		$nb_erreurs = $verif->verifierFichier(Config::get('chemins.donnees'));
305
		return $nb_erreurs;
433 jpm 306
	}
603 mathilde 307
 
370 mathilde 308
}
309
?>