Subversion Repositories eFlore/Projets.eflore-projets

Rev

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