Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

Rev 68 Rev 103
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 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, 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).'));
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
					break;
37
					break;
37
				case 'chargerStructureSql' :
38
				case 'chargerStructureSql' :
38
					$this->chargerStructureSql();
39
					$this->chargerStructureSql();
39
					break;
40
					break;
40
				case 'chargerBdtfx' :
41
				case 'chargerBdtfx' :
41
					$this->chargerBdtfx();
42
					$this->chargerBdtfx();
42
					break;
43
					break;
43
				case 'genererNomSciHtml' :
44
				case 'genererNomSciHtml' :
44
					$this->genererNomSciHtml();
45
					$this->genererNomSciHtml();
45
					break;
46
					break;
-
 
47
				case 'genererDonneesTestMultiVersion' :
-
 
48
					$this->genererDonneesTestMultiVersion();
-
 
49
					break;
-
 
50
				case 'supprimerDonneesTestMultiVersion' :
-
 
51
					$this->supprimerDonneesTestMultiVersion();
-
 
52
					break;
46
				default :
53
				default :
47
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
54
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
48
			}
55
			}
49
		} catch (Exception $e) {
56
		} catch (Exception $e) {
50
			$this->traiterErreur($e->getMessage());
57
			$this->traiterErreur($e->getMessage());
51
		}
58
		}
52
	}
59
	}
53
 
60
 
54
	private function chargerBdtfx() {
61
	private function chargerBdtfx() {
55
		$chemin = Config::get('chemins.bdtfx');
62
		$chemin = Config::get('chemins.bdtfx');
56
		$table = Config::get('tables.bdtfx');
63
		$table = Config::get('tables.bdtfx');
57
		$requete = "LOAD DATA INFILE '$chemin' ".
64
		$requete = "LOAD DATA INFILE '$chemin' ".
58
				"REPLACE INTO TABLE $table ".
65
				"REPLACE INTO TABLE $table ".
59
				'CHARACTER SET utf8 '.
66
				'CHARACTER SET utf8 '.
60
				'FIELDS '.
67
				'FIELDS '.
61
				"	TERMINATED BY '\t' ".
68
				"	TERMINATED BY '\t' ".
62
				"	ENCLOSED BY '' ".
69
				"	ENCLOSED BY '' ".
63
				"	ESCAPED BY '\\\' ".
70
				"	ESCAPED BY '\\\' ".
64
				'IGNORE 1 LINES';
71
				'IGNORE 1 LINES';
65
		$this->getBdd()->requeter($requete);
72
		$this->getBdd()->requeter($requete);
66
	}
73
	}
67
 
74
 
68
	private function genererNomSciHtml() {
75
	private function genererNomSciHtml() {
69
		$this->initialiserGenerationNomsSciHtml();
76
		$this->initialiserGenerationNomsSciHtml();
70
		$this->preparerTable();
77
		$this->preparerTable();
71
		$generateur = new GenerateurNomSciHtml();
78
		$generateur = new GenerateurNomSciHtml();
72
		$nbreTotal = $this->recupererNbTotalTuples();
79
		$nbreTotal = $this->recupererNbTotalTuples();
73
		while ($this->departInsertion < $nbreTotal) {
80
		while ($this->departInsertion < $nbreTotal) {
74
			$resultat = $this->recupererTuples();
81
			$resultat = $this->recupererTuples();
75
			$nomsSciEnHtml = $generateur->generer($resultat);
82
			$nomsSciEnHtml = $generateur->generer($resultat);
76
			$this->lancerRequeteModification($nomsSciEnHtml);
83
			$this->lancerRequeteModification($nomsSciEnHtml);
77
			$this->departInsertion += $this->pasInsertion;
84
			$this->departInsertion += $this->pasInsertion;
78
			$this->afficherAvancement("Insertion des noms scientifique au format HTML dans la base par paquet de {$this->pasInsertion} en cours");
85
			$this->afficherAvancement("Insertion des noms scientifique au format HTML dans la base par paquet de {$this->pasInsertion} en cours");
79
			if ($this->stopperLaBoucle($this->getParametre('t'))) break;
86
			if ($this->stopperLaBoucle($this->getParametre('t'))) break;
80
		}
87
		}
81
		echo "\n";
88
		echo "\n";
82
	}
89
	}
83
 
90
 
84
	private function initialiserGenerationNomsSciHtml() {
91
	private function initialiserGenerationNomsSciHtml() {
85
		$this->table = Config::get('tables.bdtfx');
92
		$this->table = Config::get('tables.bdtfx');
86
	}
93
	}
87
 
94
 
88
	private function preparerTable() {
95
	private function preparerTable() {
89
		$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'nom_sci_html' ";
96
		$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'nom_sci_html' ";
90
		$resultat = $this->getBdd()->recuperer($requete);
97
		$resultat = $this->getBdd()->recuperer($requete);
91
		if ($resultat === false) {
98
		if ($resultat === false) {
92
			$requete = 	"ALTER TABLE {$this->table} ".
99
			$requete = 	"ALTER TABLE {$this->table} ".
93
							'ADD nom_sci_html VARCHAR( 500 ) '.
100
							'ADD nom_sci_html VARCHAR( 500 ) '.
94
							'CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ';
101
							'CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ';
95
			$this->getBdd()->requeter($requete);
102
			$this->getBdd()->requeter($requete);
96
		}
103
		}
97
	}
104
	}
98
 
105
 
99
	private function recupererNbTotalTuples(){
106
	private function recupererNbTotalTuples(){
100
		$requete = "SELECT count(*) AS nb FROM {$this->table} ";
107
		$requete = "SELECT count(*) AS nb FROM {$this->table} ";
101
		$resultat = $this->getBdd()->recuperer($requete);
108
		$resultat = $this->getBdd()->recuperer($requete);
102
		return $resultat['nb'];
109
		return $resultat['nb'];
103
	}
110
	}
104
 
111
 
105
	private function recupererTuples() {
112
	private function recupererTuples() {
106
		$requete = 'SELECT 	num_nom, rang, nom_supra_generique, genre, epithete_infra_generique, '.
113
		$requete = 'SELECT 	num_nom, rang, nom_supra_generique, genre, epithete_infra_generique, '.
107
					'	epithete_sp, type_epithete, epithete_infra_sp,cultivar_groupe, '.
114
					'	epithete_sp, type_epithete, epithete_infra_sp,cultivar_groupe, '.
108
					'	nom_commercial, cultivar '.
115
					'	nom_commercial, cultivar '.
109
					"FROM {$this->table} ".
116
					"FROM {$this->table} ".
110
					"LIMIT {$this->departInsertion},{$this->pasInsertion} ";
117
					"LIMIT {$this->departInsertion},{$this->pasInsertion} ";
111
		$resultat = $this->getBdd()->recupererTous($requete);
118
		$resultat = $this->getBdd()->recupererTous($requete);
112
		return $resultat;
119
		return $resultat;
113
	}
120
	}
114
 
121
 
115
	private function lancerRequeteModification($nomsSciHtm) {
122
	private function lancerRequeteModification($nomsSciHtm) {
116
		foreach ($nomsSciHtm as $id => $html) {
123
		foreach ($nomsSciHtm as $id => $html) {
117
			$html = $this->getBdd()->proteger($html);
124
			$html = $this->getBdd()->proteger($html);
118
			$requete = "UPDATE {$this->table} ".
125
			$requete = "UPDATE {$this->table} ".
119
				"SET nom_sci_html = $html ".
126
				"SET nom_sci_html = $html ".
120
				"WHERE num_nom = $id ";
127
				"WHERE num_nom = $id ";
121
			$resultat = $this->getBdd()->requeter($requete);
128
			$resultat = $this->getBdd()->requeter($requete);
122
			if ($resultat === false) {
129
			if ($resultat === false) {
123
				throw new Exception("Erreur d'insertion pour le tuple $id");
130
				throw new Exception("Erreur d'insertion pour le tuple $id");
124
			}
131
			}
125
		}
132
		}
126
	}
133
	}
-
 
134
 
-
 
135
	private function genererDonneesTestMultiVersion() {
-
 
136
		$contenuSql = $this->recupererContenu(Config::get('chemins.structureSqlTest'));
-
 
137
		$this->executerScripSql($contenuSql);
-
 
138
 
-
 
139
		$table = Config::get('tables.bdtfx');
-
 
140
		$tableTest = Config::get('tables.bdtfxTest');
-
 
141
		$requete = "INSERT INTO $tableTest SELECT * FROM $table";
-
 
142
		$this->getBdd()->requeter($requete);
-
 
143
	}
-
 
144
 
-
 
145
	private function supprimerDonneesTestMultiVersion() {
-
 
146
		$tableMeta = Config::get('tables.bdtfxMeta');
-
 
147
		$requete = "DELETE FROM $tableMeta WHERE guid = 'urn:lsid:tela-botanica.org:bdtfx:1.02'";
-
 
148
		$this->getBdd()->requeter($requete);
-
 
149
 
-
 
150
		$tableTest = Config::get('tables.bdtfxTest');
-
 
151
		$requete = "DROP TABLE $tableTest";
127
 
152
		$this->getBdd()->requeter($requete);
128
 
153
	}
129
}
154
}
130
?>
155
?>