Subversion Repositories eFlore/Projets.eflore-projets

Rev

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