Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 11 | Rev 54 | 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
		require_once dirname(__FILE__).DS.'GenerateurNomSciHtml.php';
115
		echo dirname(__FILE__).DS.'GenerateurNomSciHtml.php';
116
		$this->table = Config::get('tables.bdtfx');
11 jpm 117
	}
118
 
119
	private function preparerTable() {
120
		$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'nom_sci_html' ";
121
		$resultat = $this->bdd->recuperer($requete);
122
		if ($resultat === false) {
123
			$requete = 	"ALTER TABLE {$this->table} ".
46 jpm 124
							'ADD nom_sci_html VARCHAR( 500 ) '.
125
							'CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ';
11 jpm 126
			$this->bdd->requeter($requete);
127
		}
128
	}
129
 
130
	private function recupererNbTotalTuples(){
46 jpm 131
		$requete = "SELECT count(*) AS nb FROM {$this->table} ";
132
		$resultat = $this->bdd->recuperer($requete);
133
		return $resultat['nb'];
11 jpm 134
	}
135
 
136
	private function recupererTuples() {
137
		$requete = 'SELECT 	num_nom, rang, nom_supra_generique, genre, epithete_infra_generique, '.
138
					'	epithete_sp, type_epithete, epithete_infra_sp,cultivar_groupe, '.
139
					'	nom_commercial, cultivar '.
140
					"FROM {$this->table} ".
46 jpm 141
					"LIMIT {$this->departInsertion},{$this->pasInsertion} ";
11 jpm 142
		$resultat = $this->bdd->recupererTous($requete);
143
		return $resultat;
144
	}
145
 
46 jpm 146
	private function lancerRequeteModification($nomsSciHtm) {
147
		foreach ($nomsSciHtm as $id => $html) {
148
			$html = $this->bdd->proteger($html);
149
			$requete = "UPDATE {$this->table} ".
150
				"SET nom_sci_html = $html ".
151
				"WHERE num_nom = $id ";
152
			$resultat = $this->bdd->requeter($requete);
153
			if ($resultat === false) {
154
				throw new Exception("Erreur d'insertion pour le tuple $id");
11 jpm 155
			}
156
		}
157
	}
158
 
46 jpm 159
	private function stopperLaBoucle() {
160
		$stop = false;
161
		static $ligneActuelle = 1;
162
		if ($nbreLignesATester = $this->getParametre('t')) {
163
			if ($nbreLignesATester == $ligneActuelle++) {
164
				$stop = true;
11 jpm 165
			}
166
		}
46 jpm 167
		return $stop;
11 jpm 168
	}
169
}
170
?>