Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 369 | Rev 699 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 jpm 1
<?php
121 jpm 2
//declare(encoding='UTF-8');
11 jpm 3
/**
4
 * Exemple de lancement du script : :
121 jpm 5
 * /opt/lampp/bin/php cli.php bdtfx -a chargerTous
6
 *
11 jpm 7
 * @category	php 5.2
121 jpm 8
 * @package		eFlore/Scripts
11 jpm 9
 * @author		Jennifer DHÉ <jennifer@tela-botanica.org>
10
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
11
 * @copyright	Copyright (c) 2011, Tela Botanica (accueil@tela-botanica.org)
12
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
13
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
14
 * @version		$Id$
15
 */
68 jpm 16
class Bdtfx extends EfloreScript {
46 jpm 17
 
68 jpm 18
	private $table = null;
19
	private $pasInsertion = 1000;
20
	private $departInsertion = 0;
11 jpm 21
 
22
	protected $parametres_autorises = array(
369 mathilde 23
		'-t' => array(false, false, 'Permet de tester le script sur un jeu réduit de données (indiquer le nombre de lignes).'));
11 jpm 24
 
25
	public function executer() {
46 jpm 26
		try {
68 jpm 27
			$this->initialiserProjet('bdtfx');
46 jpm 28
 
29
			// Lancement de l'action demandée
30
			$cmd = $this->getParametre('a');
31
			switch ($cmd) {
32
				case 'chargerTous' :
33
					$this->chargerStructureSql();
34
					$this->chargerBdtfx();
340 jpm 35
					$this->genererChpNomSciHtml();
36
					$this->genererChpFamille();
103 jpm 37
					$this->genererDonneesTestMultiVersion();
46 jpm 38
					break;
39
				case 'chargerStructureSql' :
40
					$this->chargerStructureSql();
41
					break;
42
				case 'chargerBdtfx' :
43
					$this->chargerBdtfx();
44
					break;
45
				case 'genererNomSciHtml' :
340 jpm 46
					$this->genererChpNomSciHtml();
46 jpm 47
					break;
335 jpm 48
				case 'genererChpFamille' :
49
					$this->genererChpFamille();
50
					break;
103 jpm 51
				case 'genererDonneesTestMultiVersion' :
52
					$this->genererDonneesTestMultiVersion();
53
					break;
54
				case 'supprimerDonneesTestMultiVersion' :
55
					$this->supprimerDonneesTestMultiVersion();
56
					break;
130 jpm 57
				case 'supprimerTous' :
58
					$this->supprimerTous();
59
					break;
46 jpm 60
				default :
61
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
62
			}
63
		} catch (Exception $e) {
64
			$this->traiterErreur($e->getMessage());
11 jpm 65
		}
46 jpm 66
	}
11 jpm 67
 
46 jpm 68
	private function chargerBdtfx() {
69
		$chemin = Config::get('chemins.bdtfx');
70
		$table = Config::get('tables.bdtfx');
71
		$requete = "LOAD DATA INFILE '$chemin' ".
72
				"REPLACE INTO TABLE $table ".
73
				'CHARACTER SET utf8 '.
74
				'FIELDS '.
75
				"	TERMINATED BY '\t' ".
76
				"	ENCLOSED BY '' ".
77
				"	ESCAPED BY '\\\' ".
78
				'IGNORE 1 LINES';
68 jpm 79
		$this->getBdd()->requeter($requete);
46 jpm 80
	}
81
 
340 jpm 82
	private function genererChpNomSciHtml() {
335 jpm 83
		$this->initialiserGenerationChamps();
84
		$this->preparerTablePrChpNomSciHtml();
46 jpm 85
		$generateur = new GenerateurNomSciHtml();
86
		$nbreTotal = $this->recupererNbTotalTuples();
661 jpm 87
		$erreurs = array();
335 jpm 88
		$this->departInsertion = 0;
46 jpm 89
		while ($this->departInsertion < $nbreTotal) {
340 jpm 90
			$resultat = $this->recupererTuplesPrChpNomSciHtml();
661 jpm 91
 
92
			try {
93
				$nomsSciEnHtml = $generateur->generer($resultat);
94
			} catch (Exception $e) {
95
				$erreurs[] = $e->getMessage();
96
			}
97
 
340 jpm 98
			$this->remplirChpNomSciHtm($nomsSciEnHtml);
46 jpm 99
			$this->departInsertion += $this->pasInsertion;
100
			$this->afficherAvancement("Insertion des noms scientifique au format HTML dans la base par paquet de {$this->pasInsertion} en cours");
68 jpm 101
			if ($this->stopperLaBoucle($this->getParametre('t'))) break;
11 jpm 102
		}
103
		echo "\n";
661 jpm 104
 
105
		$this->creerFichierLog('Erreurs lors de la génération HTML des noms scientifiques', $erreurs, 'erreurs_noms_sci_html');
11 jpm 106
	}
107
 
335 jpm 108
	private function initialiserGenerationChamps() {
46 jpm 109
		$this->table = Config::get('tables.bdtfx');
11 jpm 110
	}
111
 
335 jpm 112
	private function preparerTablePrChpNomSciHtml() {
11 jpm 113
		$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'nom_sci_html' ";
68 jpm 114
		$resultat = $this->getBdd()->recuperer($requete);
11 jpm 115
		if ($resultat === false) {
116
			$requete = 	"ALTER TABLE {$this->table} ".
335 jpm 117
				'ADD nom_sci_html VARCHAR( 500 ) '.
118
				'CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ';
68 jpm 119
			$this->getBdd()->requeter($requete);
11 jpm 120
		}
121
	}
122
 
123
	private function recupererNbTotalTuples(){
46 jpm 124
		$requete = "SELECT count(*) AS nb FROM {$this->table} ";
68 jpm 125
		$resultat = $this->getBdd()->recuperer($requete);
46 jpm 126
		return $resultat['nb'];
11 jpm 127
	}
128
 
340 jpm 129
	private function recupererTuplesPrChpNomSciHtml() {
11 jpm 130
		$requete = 'SELECT 	num_nom, rang, nom_supra_generique, genre, epithete_infra_generique, '.
335 jpm 131
			'	epithete_sp, type_epithete, epithete_infra_sp,cultivar_groupe, '.
132
			'	nom_commercial, cultivar '.
133
			"FROM {$this->table} ".
134
			"LIMIT {$this->departInsertion},{$this->pasInsertion} ";
68 jpm 135
		$resultat = $this->getBdd()->recupererTous($requete);
11 jpm 136
		return $resultat;
137
	}
138
 
340 jpm 139
	private function remplirChpNomSciHtm($nomsSciHtm) {
46 jpm 140
		foreach ($nomsSciHtm as $id => $html) {
68 jpm 141
			$html = $this->getBdd()->proteger($html);
340 jpm 142
			$requete = "UPDATE {$this->table} SET nom_sci_html = $html WHERE num_nom = $id ";
68 jpm 143
			$resultat = $this->getBdd()->requeter($requete);
46 jpm 144
			if ($resultat === false) {
145
				throw new Exception("Erreur d'insertion pour le tuple $id");
11 jpm 146
			}
147
		}
148
	}
339 jpm 149
 
335 jpm 150
	private function genererChpFamille() {
151
		$this->initialiserGenerationChamps();
152
		$this->preparerTablePrChpFamille();
336 jpm 153
		$resultats = $this->recupererTuplesPrChpFamille();
154
		$noms = array();
155
		$introuvables = array();
339 jpm 156
		$introuvablesSyno = array();
157
		foreach ($resultats as $id => $nom) {
336 jpm 158
			$nn = $nom['num_nom'];
339 jpm 159
			$nnr = $nom['num_nom_retenu'];
336 jpm 160
			$nts = $nom['num_tax_sup'];
161
			$rg = $nom['rang'];
339 jpm 162
			if ($nnr != '') {
163
				if ($rg == '180') {
340 jpm 164
					$noms[$nn] = $nom['nom_sci'];
336 jpm 165
				} else {
339 jpm 166
					if ($nn == $nnr) {// nom retenu
167
						if (isset($noms[$nts])) {
168
							$noms[$nn] = $noms[$nts];
169
						} else {
170
							$introuvables[] = $nn;
171
						}
172
					} else {// nom synonyme
173
						if (isset($noms[$nnr])) {
174
							$noms[$nn] = $noms[$nnr];
175
						} else {
176
							$introuvablesSyno[] = $nom;
177
						}
178
					}
336 jpm 179
				}
180
			}
339 jpm 181
			unset($resultats[$id]);
336 jpm 182
			$this->afficherAvancement("Attribution de leur famille aux noms en cours");
335 jpm 183
			if ($this->stopperLaBoucle($this->getParametre('t'))) break;
184
		}
336 jpm 185
		echo "\n";
339 jpm 186
 
187
		foreach ($introuvablesSyno as $id => $nom) {
188
			$nn = $nom['num_nom'];
189
			$nnr = $nom['num_nom_retenu'];
190
			if (isset($noms[$nnr])) {
191
				$noms[$nn] = $noms[$nnr];
192
			} else {
193
				$introuvables[] = $nn;
194
			}
195
			unset($introuvablesSyno[$id]);
196
			$this->afficherAvancement("Attribution de leur famille aux synonymes en cours");
336 jpm 197
		}
339 jpm 198
		echo "\n";
199
 
661 jpm 200
		$msg = 'Plusieurs familles sont introuvables';
201
		$this->creerFichierLog($msg, $introuvables, 'famille_introuvable');
339 jpm 202
 
340 jpm 203
		$this->remplirChpFamille($noms);
335 jpm 204
	}
339 jpm 205
 
335 jpm 206
	private function preparerTablePrChpFamille() {
207
		$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'famille' ";
208
		$resultat = $this->getBdd()->recuperer($requete);
209
		if ($resultat === false) {
210
			$requete = 	"ALTER TABLE {$this->table} ".
211
				'ADD famille VARCHAR(255) '.
212
				'CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ';
213
			$this->getBdd()->requeter($requete);
214
		}
215
	}
339 jpm 216
 
335 jpm 217
	private function recupererTuplesPrChpFamille() {
339 jpm 218
		$requete = 'SELECT num_nom, num_nom_retenu, num_tax_sup, rang, nom_sci '.
335 jpm 219
				"FROM {$this->table} ".
220
				"WHERE rang >= 180 ".
340 jpm 221
				"ORDER BY rang ASC, num_tax_sup ASC, num_nom_retenu DESC ";
335 jpm 222
		$resultat = $this->getBdd()->recupererTous($requete);
223
		return $resultat;
224
	}
339 jpm 225
 
340 jpm 226
	private function remplirChpFamille($noms) {
336 jpm 227
		foreach ($noms as $id => $famille) {
228
			$famille = $this->getBdd()->proteger($famille);
340 jpm 229
			$requete = "UPDATE {$this->table} SET famille = $famille WHERE num_nom = $id ";
336 jpm 230
			$resultat = $this->getBdd()->requeter($requete);
231
			if ($resultat === false) {
232
				throw new Exception("Erreur d'insertion pour le tuple $id");
233
			}
234
			$this->afficherAvancement("Insertion des noms de famille dans la base en cours");
235
		}
236
		echo "\n";
237
	}
11 jpm 238
 
103 jpm 239
	private function genererDonneesTestMultiVersion() {
240
		$contenuSql = $this->recupererContenu(Config::get('chemins.structureSqlTest'));
241
		$this->executerScripSql($contenuSql);
68 jpm 242
 
103 jpm 243
		$table = Config::get('tables.bdtfx');
244
		$tableTest = Config::get('tables.bdtfxTest');
245
		$requete = "INSERT INTO $tableTest SELECT * FROM $table";
246
		$this->getBdd()->requeter($requete);
247
	}
248
 
249
	private function supprimerDonneesTestMultiVersion() {
250
		$tableMeta = Config::get('tables.bdtfxMeta');
251
		$requete = "DELETE FROM $tableMeta WHERE guid = 'urn:lsid:tela-botanica.org:bdtfx:1.02'";
252
		$this->getBdd()->requeter($requete);
253
 
254
		$tableTest = Config::get('tables.bdtfxTest');
342 jpm 255
		$requete = "DROP TABLE IF EXISTS $tableTest";
103 jpm 256
		$this->getBdd()->requeter($requete);
257
	}
130 jpm 258
 
259
	private function supprimerTous() {
342 jpm 260
		$requete = "DROP TABLE IF EXISTS bdtfx_meta, bdtfx_v1_01, bdtfx_v1_02";
130 jpm 261
		$this->getBdd()->requeter($requete);
262
	}
661 jpm 263
 
264
	private function creerFichierLog($message, $lignes, $nomFichier) {
265
		$lignesNbre = count($lignes);
266
		if ($lignesNbre != 0) {
267
			echo "$message. Voir le log de $lignesNbre lignes :\n";
268
 
269
			$logContenu = implode(", \n", $lignes);
270
			$logFichier = realpath(dirname(__FILE__))."/log/$nomFichier.log";
271
			echo $logFichier."\n";
272
					file_put_contents($logFichier, $logContenu);
273
		}
274
	}
11 jpm 275
}
276
?>