Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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