Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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