Subversion Repositories eFlore/Projets.eflore-projets

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
186 jpm 1
<?php
2
//declare(encoding='UTF-8');
3
/**
4
 * Exemple de lancement du script : :
5
 * /opt/lampp/bin/php cli.php bdtfx -a chargerTous
6
 *
7
 * @category	php 5.2
8
 * @package		eFlore/Scripts
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 Fournier extends EfloreScript {
17
 
18
	private $table = null;
19
	private $tableMeta = null;
20
	private $pasInsertion = 1000;
21
	private $departInsertion = 0;
22
 
23
	protected $parametres_autorises = array(
24
		'-t' => array(false, false, 'Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).'));
25
 
26
	public function executer() {
27
		try {
28
			$this->initialiserProjet('fournier');
29
			$this->initialiserTables();
30
 
31
			// Lancement de l'action demandée
32
			$cmd = $this->getParametre('a');
33
			switch ($cmd) {
34
				case 'chargerTous' :
35
					$this->chargerStructureSql();
36
					$this->chargerFournier();
37
					$this->genererNomSciHtml();
38
					break;
39
				case 'chargerStructureSql' :
40
					$this->chargerStructureSql();
41
					break;
42
				case 'chargerFournier' :
43
					$this->chargerFournier();
44
					break;
45
				case 'genererNomSciHtml' :
46
					$this->genererNomSciHtml();
47
					break;
48
				case 'supprimerTous' :
49
					$this->supprimerTous();
50
					break;
51
				default :
52
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
53
			}
54
		} catch (Exception $e) {
55
			$this->traiterErreur($e->getMessage());
56
		}
57
	}
58
 
59
	private function chargerFournier() {
60
		$chemin = Config::get('chemins.fournier');
61
		$table = Config::get('tables.fournier');
62
		$requete = "LOAD DATA INFILE '$chemin' ".
63
				"REPLACE INTO TABLE $table ".
64
				'CHARACTER SET utf8 '.
65
				'FIELDS '.
66
				"	TERMINATED BY ',' ".
67
				"	ENCLOSED BY '\"' ".
68
				"	ESCAPED BY '\\\' ".
69
				'IGNORE 1 LINES';
70
		$this->getBdd()->requeter($requete);
71
	}
72
 
73
	private function genererNomSciHtml() {
74
		$this->preparerTable();
75
		$generateur = new GenerateurNomSciHtml();
76
		$nbreTotal = $this->recupererNbTotalTuples();
77
		while ($this->departInsertion < $nbreTotal) {
78
			$resultat = $this->recupererTuples();
79
			$nomsSciEnHtml = $generateur->generer($resultat);
80
			$this->lancerRequeteModification($nomsSciEnHtml);
81
			$this->departInsertion += $this->pasInsertion;
82
			$this->afficherAvancement("Insertion des noms scientifique au format HTML dans la base par paquet de {$this->pasInsertion} en cours");
83
			if ($this->stopperLaBoucle($this->getParametre('t'))) break;
84
		}
85
		echo "\n";
86
	}
87
 
88
	private function initialiserTables() {
89
		$this->table = Config::get('tables.fournier');
90
		$this->tableMeta = Config::get('tables.fournierMeta');
91
	}
92
 
93
	private function preparerTable() {
94
		$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'nom_sci_html' ";
95
		$resultat = $this->getBdd()->recuperer($requete);
96
		if ($resultat === false) {
97
			$requete = 	"ALTER TABLE {$this->table} ".
98
							'ADD nom_sci_html VARCHAR( 500 ) '.
99
							'CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ';
100
			$this->getBdd()->requeter($requete);
101
		}
102
	}
103
 
104
	private function recupererNbTotalTuples(){
105
		$requete = "SELECT count(*) AS nb FROM {$this->table} ";
106
		$resultat = $this->getBdd()->recuperer($requete);
107
		return $resultat['nb'];
108
	}
109
 
110
	private function recupererTuples() {
111
		$requete = 'SELECT 	num_nom, rang, nom_supra_generique, genre, epithete_infra_generique, '.
112
					'	epithete_sp, type_epithete, epithete_infra_sp, cultivar_groupe, '.
113
					'	nom_commercial, cultivar '.
114
					"FROM {$this->table} ".
115
					"LIMIT {$this->departInsertion},{$this->pasInsertion} ";
116
		$resultat = $this->getBdd()->recupererTous($requete);
117
		return $resultat;
118
	}
119
 
120
	private function lancerRequeteModification($nomsSciHtm) {
121
		foreach ($nomsSciHtm as $id => $html) {
122
			$html = $this->getBdd()->proteger($html);
123
			$requete = "UPDATE {$this->table} ".
124
				"SET nom_sci_html = $html ".
125
				"WHERE num_nom = $id ";
126
			$resultat = $this->getBdd()->requeter($requete);
127
			if ($resultat === false) {
128
				throw new Exception("Erreur d'insertion pour le tuple $id");
129
			}
130
		}
131
	}
132
 
133
	private function supprimerTous() {
349 jpm 134
		$requete = "DROP TABLE IF EXISTS {$this->tableMeta}, {$this->table}";
186 jpm 135
		$this->getBdd()->requeter($requete);
136
	}
137
}
138
?>