Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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