Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

Rev 54 Rev 68
1
<?php
1
<?php
2
/**
2
/**
3
 * declare(encoding='UTF-8');
3
 * declare(encoding='UTF-8');
4
 * Exemple de lancement du script : :
4
 * Exemple de lancement du script : :
5
 * /opt/lampp/bin/php cli.php bdtfx -a nomSciHtml -t bdtfx_v1_01
5
 * /opt/lampp/bin/php cli.php bdtfx -a nomSciHtml -t bdtfx_v1_01
6
 *  Classe permettant de créer le nom scientifique au format HTML.
6
 *  Classe permettant de créer le nom scientifique au format HTML.
7
 * @category	php 5.2
7
 * @category	php 5.2
8
 * @package		bdtfx
8
 * @package		bdtfx
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 Script {
16
class Bdtfx extends EfloreScript {
17
 
-
 
18
	protected $bdd = null;
17
 
19
	protected $table = null;
-
 
20
	private $projetNom = 'bdtfx';
-
 
21
 
18
	private $table = null;
22
	protected $pasInsertion = 1000;
19
	private $pasInsertion = 1000;
23
	protected $departInsertion = 0;
20
	private $departInsertion = 0;
24
 
21
 
25
	protected $parametres_autorises = array(
22
	protected $parametres_autorises = array(
26
		'-t' => array(false, true, 'Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).'));
23
		'-t' => array(false, true, 'Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).'));
27
 
24
 
28
	public function executer() {
25
	public function executer() {
29
		try {
26
		try {
30
			$this->initialiser();
27
			$this->initialiserProjet('bdtfx');
31
 
28
 
32
			// Lancement de l'action demandée
29
			// Lancement de l'action demandée
33
			$cmd = $this->getParametre('a');
30
			$cmd = $this->getParametre('a');
34
			switch ($cmd) {
31
			switch ($cmd) {
35
				case 'chargerTous' :
32
				case 'chargerTous' :
36
					$this->chargerStructureSql();
33
					$this->chargerStructureSql();
37
					$this->chargerBdtfx();
34
					$this->chargerBdtfx();
38
					$this->genererNomSciHtml();
35
					$this->genererNomSciHtml();
39
					break;
36
					break;
40
				case 'chargerStructureSql' :
37
				case 'chargerStructureSql' :
41
					$this->chargerStructureSql();
38
					$this->chargerStructureSql();
42
					break;
39
					break;
43
				case 'chargerBdtfx' :
40
				case 'chargerBdtfx' :
44
					$this->chargerBdtfx();
41
					$this->chargerBdtfx();
45
					break;
42
					break;
46
				case 'genererNomSciHtml' :
43
				case 'genererNomSciHtml' :
47
					$this->genererNomSciHtml();
44
					$this->genererNomSciHtml();
48
					break;
45
					break;
49
				default :
46
				default :
50
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
47
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
51
			}
48
			}
52
		} catch (Exception $e) {
49
		} catch (Exception $e) {
53
			$this->traiterErreur($e->getMessage());
50
			$this->traiterErreur($e->getMessage());
54
		}
51
		}
55
	}
52
	}
56
 
-
 
57
	private function initialiser() {
-
 
58
		$this->chargerConfigDuProjet();
-
 
59
		$this->bdd = new Bdd();
-
 
60
	}
-
 
61
 
-
 
62
	private function chargerConfigDuProjet() {
-
 
63
		$fichierIni = dirname(__FILE__).DS.$this->projetNom.'.ini';
-
 
64
		if (file_exists($fichierIni)) {
-
 
65
			Config::charger($fichierIni);
-
 
66
		} else {
-
 
67
			$m = "Veuillez configurer le projet en créant le fichier '{$this->projetNom}.ini' ".
-
 
68
				"dans le dossier du module de script du projet à partir du fichier '{$this->projetNom}.defaut.ini'.";
-
 
69
			throw new Exception($m);
-
 
70
		}
-
 
71
	}
-
 
72
 
-
 
73
	private function chargerStructureSql() {
-
 
74
		$chemin = Config::get('chemins.structureSql');
-
 
75
		$requetes = Outils::extraireRequetes($chemin);
-
 
76
		foreach ($requetes as $requete) {
-
 
77
			$this->bdd->requeter($requete);
-
 
78
		}
-
 
79
	}
-
 
80
 
53
 
81
	private function chargerBdtfx() {
54
	private function chargerBdtfx() {
82
		$chemin = Config::get('chemins.bdtfx');
55
		$chemin = Config::get('chemins.bdtfx');
83
		$table = Config::get('tables.bdtfx');
56
		$table = Config::get('tables.bdtfx');
84
		$requete = "LOAD DATA INFILE '$chemin' ".
57
		$requete = "LOAD DATA INFILE '$chemin' ".
85
				"REPLACE INTO TABLE $table ".
58
				"REPLACE INTO TABLE $table ".
86
				'CHARACTER SET utf8 '.
59
				'CHARACTER SET utf8 '.
87
				'FIELDS '.
60
				'FIELDS '.
88
				"	TERMINATED BY '\t' ".
61
				"	TERMINATED BY '\t' ".
89
				"	ENCLOSED BY '' ".
62
				"	ENCLOSED BY '' ".
90
				"	ESCAPED BY '\\\' ".
63
				"	ESCAPED BY '\\\' ".
91
				'IGNORE 1 LINES';
64
				'IGNORE 1 LINES';
92
		$this->bdd->requeter($requete);
65
		$this->getBdd()->requeter($requete);
93
	}
66
	}
94
 
67
 
95
	private function genererNomSciHtml() {
68
	private function genererNomSciHtml() {
96
		$this->initialiserGenerationNomsSciHtml();
69
		$this->initialiserGenerationNomsSciHtml();
97
		$this->preparerTable();
70
		$this->preparerTable();
98
		$generateur = new GenerateurNomSciHtml();
71
		$generateur = new GenerateurNomSciHtml();
99
		$nbreTotal = $this->recupererNbTotalTuples();
72
		$nbreTotal = $this->recupererNbTotalTuples();
100
		while ($this->departInsertion < $nbreTotal) {
73
		while ($this->departInsertion < $nbreTotal) {
101
			$resultat = $this->recupererTuples();
74
			$resultat = $this->recupererTuples();
102
			$nomsSciEnHtml = $generateur->generer($resultat);
75
			$nomsSciEnHtml = $generateur->generer($resultat);
103
			$this->lancerRequeteModification($nomsSciEnHtml);
76
			$this->lancerRequeteModification($nomsSciEnHtml);
104
			$this->departInsertion += $this->pasInsertion;
77
			$this->departInsertion += $this->pasInsertion;
105
			$this->afficherAvancement("Insertion des noms scientifique au format HTML dans la base par paquet de {$this->pasInsertion} en cours");
78
			$this->afficherAvancement("Insertion des noms scientifique au format HTML dans la base par paquet de {$this->pasInsertion} en cours");
106
			if ($this->stopperLaBoucle()) {
79
			if ($this->stopperLaBoucle($this->getParametre('t'))) break;
107
				break;
-
 
108
			}
-
 
109
		}
80
		}
110
		echo "\n";
81
		echo "\n";
111
	}
82
	}
112
 
83
 
113
	private function initialiserGenerationNomsSciHtml() {
84
	private function initialiserGenerationNomsSciHtml() {
114
		$this->table = Config::get('tables.bdtfx');
85
		$this->table = Config::get('tables.bdtfx');
115
	}
86
	}
116
 
87
 
117
	private function preparerTable() {
88
	private function preparerTable() {
118
		$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'nom_sci_html' ";
89
		$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'nom_sci_html' ";
119
		$resultat = $this->bdd->recuperer($requete);
90
		$resultat = $this->getBdd()->recuperer($requete);
120
		if ($resultat === false) {
91
		if ($resultat === false) {
121
			$requete = 	"ALTER TABLE {$this->table} ".
92
			$requete = 	"ALTER TABLE {$this->table} ".
122
							'ADD nom_sci_html VARCHAR( 500 ) '.
93
							'ADD nom_sci_html VARCHAR( 500 ) '.
123
							'CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ';
94
							'CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ';
124
			$this->bdd->requeter($requete);
95
			$this->getBdd()->requeter($requete);
125
		}
96
		}
126
	}
97
	}
127
 
98
 
128
	private function recupererNbTotalTuples(){
99
	private function recupererNbTotalTuples(){
129
		$requete = "SELECT count(*) AS nb FROM {$this->table} ";
100
		$requete = "SELECT count(*) AS nb FROM {$this->table} ";
130
		$resultat = $this->bdd->recuperer($requete);
101
		$resultat = $this->getBdd()->recuperer($requete);
131
		return $resultat['nb'];
102
		return $resultat['nb'];
132
	}
103
	}
133
 
104
 
134
	private function recupererTuples() {
105
	private function recupererTuples() {
135
		$requete = 'SELECT 	num_nom, rang, nom_supra_generique, genre, epithete_infra_generique, '.
106
		$requete = 'SELECT 	num_nom, rang, nom_supra_generique, genre, epithete_infra_generique, '.
136
					'	epithete_sp, type_epithete, epithete_infra_sp,cultivar_groupe, '.
107
					'	epithete_sp, type_epithete, epithete_infra_sp,cultivar_groupe, '.
137
					'	nom_commercial, cultivar '.
108
					'	nom_commercial, cultivar '.
138
					"FROM {$this->table} ".
109
					"FROM {$this->table} ".
139
					"LIMIT {$this->departInsertion},{$this->pasInsertion} ";
110
					"LIMIT {$this->departInsertion},{$this->pasInsertion} ";
140
		$resultat = $this->bdd->recupererTous($requete);
111
		$resultat = $this->getBdd()->recupererTous($requete);
141
		return $resultat;
112
		return $resultat;
142
	}
113
	}
143
 
114
 
144
	private function lancerRequeteModification($nomsSciHtm) {
115
	private function lancerRequeteModification($nomsSciHtm) {
145
		foreach ($nomsSciHtm as $id => $html) {
116
		foreach ($nomsSciHtm as $id => $html) {
146
			$html = $this->bdd->proteger($html);
117
			$html = $this->getBdd()->proteger($html);
147
			$requete = "UPDATE {$this->table} ".
118
			$requete = "UPDATE {$this->table} ".
148
				"SET nom_sci_html = $html ".
119
				"SET nom_sci_html = $html ".
149
				"WHERE num_nom = $id ";
120
				"WHERE num_nom = $id ";
150
			$resultat = $this->bdd->requeter($requete);
121
			$resultat = $this->getBdd()->requeter($requete);
151
			if ($resultat === false) {
122
			if ($resultat === false) {
152
				throw new Exception("Erreur d'insertion pour le tuple $id");
123
				throw new Exception("Erreur d'insertion pour le tuple $id");
153
			}
124
			}
154
		}
125
		}
155
	}
126
	}
156
 
-
 
157
	private function stopperLaBoucle() {
-
 
158
		$stop = false;
-
 
159
		static $ligneActuelle = 1;
-
 
160
		if ($nbreLignesATester = $this->getParametre('t')) {
-
 
161
			if ($nbreLignesATester == $ligneActuelle++) {
-
 
162
				$stop = true;
-
 
163
			}
-
 
164
		}
-
 
165
		return $stop;
127
 
166
	}
128
 
167
}
129
}
168
?>
130
?>