Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 46 | Go to most recent revision | Details | 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
 */
16
class Bdtfx extends Script {
17
	protected $bdd = null;
18
	protected $table = null;
19
	protected $num = null;
20
	protected $compo_nom = array();
21
	protected $abbr = array (
22
		'infra-gen.' 	=>	'Infra-Genre',
23
		'sect.'			=> 'Section',
24
		'subsect.'		=> 'Sous-Section',
25
		'ser.'			=> 'Série',
26
		'subser.'		=> 'Sous-Série',
27
		'gr.'			=> 'Groupe',
28
		'agg.' 			=> 'Agrégat',
29
		'sp.' 			=> 'Espèce',
30
		'subsp.'		=> 'Sous-Espèce',
31
		'infra-sp.'		=> 'Infra-Espèce',
32
		'var.'			=> 'Variété',
33
		'subvar.'		=> 'Sous-Variété',
34
		'fa'			=> 'Forme',
35
		'subf.'			=> 'Sous-Forme',
36
		'f. sp.'		=> 'Forma species',
37
		'proles' 		=> 'Race prole'
38
	);
39
	private $nomSciTpl = '<span class=sci>%s</span>';
40
	private $nomSupraGenTpl = '<span class="supra_gen">%s</span>';
41
	private $genTpl = '<span class="gen">%s</span>';
42
	private $infraGenTpl = '<span class="infra-gen">%s</span>';
43
	private $spFHTpl = '<span class="gen">%s</span> <span class="sp">%s</span>';
44
	private $typeEpitheteTpl = '<abbr class="type_epithete" title="%s">%s</abbr>';
45
	private $infraSpFHTpl = '<span class="gen">%s</span> <span class="sp">%s</span> <abbr class="type-epithete" title="%s">%s</abbr> <span class="infra-sp">%s</span>';
46
	private $formuleHybTpl = '<span class="formule-hyb">%s</span>';
47
	protected $table_requete;
48
	protected $limite_req_tuples = 10000;
49
	protected $depart_req_tuples = 0;
50
 
51
	protected $parametres_autorises = array(
52
		'-t' => array(true, true, 'Nom de la table qui doit être traitée'));
53
 
54
	public function executer() {
55
		// Lancement de l'action demandée
56
		$cmd = $this->getParametre('a');
57
	    switch ($cmd) {
58
			case 'nomSciHtml' :
59
				$this->genererNomSciHtml();
60
				break;
61
			default :
62
				$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
63
		}
64
    }
65
 
66
	private function genererNomSciHtml() {
67
		$this->initialiser();
68
 
69
		$nbtot = $this->recupererNbTotalTuples();
70
		while ($this->depart_req_tuples < $nbtot) {
71
			$resultat = $this->recupererTuples();
72
			$nb_tot_res = count($resultat);
73
			$compteur = 1;
74
			foreach ($resultat as $tuples => $infos) { //pour chaque tuple
75
				$this->initialiserVariables($infos);
76
 
77
				$nomSciHtml = '';
78
				$nomSciHtml .= $this->ajouterBaliseNomSupraGen();
79
				$nomSciHtml .= $this->verifierHybridite($this->compo_nom['genre'], 'gen');
80
				$nomSciHtml .= $this->ajouterBaliseInfraGen();
81
				$nomSciHtml .= $this->verifierHybridite($this->compo_nom['epithete_sp'], 'sp');
82
				$nomSciHtml .= $this->ajouterBaliseTypeInfraSp();
83
				$nomSciHtml .= $this->verifierHybridite($this->compo_nom['epithete_infra_sp'], 'infra-sp');
84
				$nomSciHtml .= $this->ajouterCultivarGpComm();
85
 
86
				if ($nomSciHtml != '') {
87
					$this->table_requete[$this->num] = sprintf($this->nomSciTpl, trim($nomSciHtml));
88
				}
89
				if (count($this->table_requete) == 1000) {
90
					$this->lancerRequeteModification();
91
					array_splice($this->table_requete, 0);
92
				} elseif ($compteur == $nb_tot_res) {
93
					$this->lancerRequeteModification();
94
				}
95
				$compteur++;
96
				$this->afficherAvancement('Génération des noms scientifique au format HTML en cours');
97
			}
98
		}
99
		echo "\n";
100
	}
101
 
102
	private function initialiser() {
103
		$this->bdd = new Bdd();
104
		$this->table = $this->getParametre('t');
105
		$this->preparerTable();
106
	}
107
 
108
	private function preparerTable() {
109
		$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'nom_sci_html' ";
110
		$resultat = $this->bdd->recuperer($requete);
111
		if ($resultat === false) {
112
			$requete = 	"ALTER TABLE {$this->table} ".
113
						'ADD nom_sci_html VARCHAR( 500 ) '.
114
						'CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ';
115
			$this->bdd->requeter($requete);
116
		}
117
	}
118
 
119
	private function recupererNbTotalTuples(){
120
		$req = "SELECT count(*) AS nb FROM {$this->table} ";
121
		$res = $this->bdd->recuperer($req);
122
		return $res['nb'];
123
	}
124
 
125
	private function recupererTuples() {
126
		$requete = 'SELECT 	num_nom, rang, nom_supra_generique, genre, epithete_infra_generique, '.
127
					'	epithete_sp, type_epithete, epithete_infra_sp,cultivar_groupe, '.
128
					'	nom_commercial, cultivar '.
129
					"FROM {$this->table} ".
130
					"LIMIT {$this->depart_req_tuples},{$this->limite_req_tuples} ";
131
 
132
		$resultat = $this->bdd->recupererTous($requete);
133
		$this->depart_req_tuples += 10000;
134
		return $resultat;
135
	}
136
 
137
	private function initialiserVariables($infos) {
138
		$this->num = $infos['num_nom'];
139
		$this->compo_nom = $infos;
140
	}
141
 
142
	private function ajouterBaliseNomSupraGen() {
143
		$html = '';
144
		if ($this->compo_nom['nom_supra_generique'] != '') {
145
			$html = sprintf($this->nomSupraGenTpl, $this->compo_nom['nom_supra_generique']);
146
		}
147
		return $html;
148
	}
149
 
150
	private function ajouterTypeEpithete($type) {
151
		if (!array_key_exists($type, $this->abbr)) {
152
			$this->abbr[$type] = $type;
153
		}
154
	}
155
 
156
	private function ajouterBaliseInfraGen() {
157
		$html = '';
158
		if ($this->verifierTypeInfraGen()) {
159
			$html .= $this->ajouterBaliseTypeInfraGen();
160
		} else {
161
			if ($this->avoirInfo('epithete_infra_generique')) {
162
				$html .= sprintf($this->infraGenTpl, $this->compo_nom['epithete_infra_generique']);
163
			}
164
		}
165
		return $html;
166
	}
167
 
168
	private function verifierTypeInfraGen() {
169
		$ok = false;
170
		if ($this->compo_nom['type_epithete'] != '' && $this->compo_nom['epithete_infra_generique'] != '') {
171
			$this->ajouterTypeEpithete($this->compo_nom['type_epithete']);
172
			$ok = true;
173
		}
174
		return $ok;
175
	}
176
 
177
	private function ajouterBaliseTypeInfraGen() {
178
		$html = '';
179
		$type = $this->compo_nom['type_epithete'];
180
 
181
		if ($type == 'agg.') { // Ajout de l'infra gen avant le type s'il est égal à agg.
182
			$html .= ' '.$this->ajouterBaliseInfraGen().
183
				' '.sprintf($this->typeEpitheteTpl, $this->abbr[$type], $type);
184
		} else {
185
			$html .= ' '.sprintf($this->typeEpitheteTpl, $this->abbr[$type], $type).
186
				' '.$this->ajouterBaliseInfraGen();
187
		}
188
		return $html;
189
	}
190
 
191
	private function ajouterBaliseTypeInfraSp() {
192
		$html = '';
193
		$type = $this->compo_nom['type_epithete'];
194
		$infraSp = $this->compo_nom['epithete_infra_sp'];
195
 
196
		if ($infraSp != '') {
197
			if ($type != '') {
198
				$this->ajouterTypeEpithete($type);
199
				$html = ' '.sprintf($this->typeEpitheteTpl, $this->abbr[$type], $type);
200
			} else {
201
				$message = "Nom #%s contient un épithète infra-spécifique mais son type n'est pas renseigné.";
202
				$this->traiterErreur($message, array($this->num));
203
			}
204
		}
205
		return $html;
206
	}
207
 
208
	private function ajouterCultivarGpComm() {
209
		$html = '';
210
		if ($this->avoirInfo('cultivar_groupe')) {
211
			$html .= ' '.$this->ajouterCultivarGroupe();
212
		}
213
		if ($this->avoirInfo('nom_commercial')) {
214
			$html .= ' <span class="commercial">'.$this->compo_nom['nom_commercial'].'</span>';
215
		}
216
		if ($this->avoirInfo('cultivar')) {
217
			$html .= ' <span class="cultivar">\''.$this->compo_nom['cultivar'].'\'</span>';
218
		}
219
		return $html;
220
	}
221
 
222
	private function avoirInfo($valeur) {
223
		return (isset($this->compo_nom[$valeur]) && $this->compo_nom[$valeur] != '') ? true : false;
224
	}
225
 
226
	/**
227
	 * Permet d'ajouter les groupes de cultivar en fonction de la présence d'un cultivar et de la présence d'un grex
228
	 *
229
	 * L'ensemble des individus obtenu par une fécondation particulière s'appelle un le Grex (que pour les orchidées).
230
	 * Au sein du grex, certains individus peuvent se distinguer par des formes, des coloris ou autres qui font que
231
	 * l'obtenteur du grex va les sélectionner.
232
	 * les noms de groupes de cultivars sont suivis de l'abréviation « Gp » et placés entre parenthèses
233
	 * les noms de grex, s'ils sont utilisés devant une épithète de cultivar, ne se mettent pas entre parenthèses
234
	 *	 ex : Cymbidium Alexanderi gx 'Westonbirt' (cultivar_groupe = Alexanderi gx) ;
235
	 * les noms de groupe se mettent entre parenthèses s'ils sont devant une épithète de cultivar
236
	 *	 ex : Dracaena fragrans (Deremenis Gp) 'Christianne' (cultivar_groupe = Deremenis)
237
	 * Un grex peut contenir des groupes (rédaction d'un exemple de l'ICNCP)
238
	 *	 ex : × Rhyncosophrocattleya Marie Lemon Stick grex Francis Suzuki Group
239
	 *	 ou : × Rhyncosophrocattleya Marie Lemon Stick gx Francis Suzuki Gp
240
	 * @param unknown_type $val
241
	 *
242
	 */
243
	private function ajouterCultivarGroupe() {
244
		$html = '';
245
		if ($this->avoirInfo('cultivar_groupe')) { //si le champ cultivar_groupe n'est pas vide
246
			if ($this->compo_nom['cultivar']) { //si il y a un cultivar, on ajoute des parenthèses au group (mais pas au grex)
247
				if (strrpos($this->compo_nom['cultivar_groupe'], ' gx ') !== false) { //si le grex est composé de groupe
248
					$tab_gp = explode(' gx ', $this->compo_nom['cultivar_groupe']);
249
					$html = ' <span class="gp">'.$tab_gp[0].
250
							' <abbr title="grex">gx </abbr>('.$tab_gp[1].
251
							' <abbr title="groupe">Gp</abbr>)</span>';
252
				} elseif (strrpos($this->compo_nom['cultivar_groupe'], ' gx') !== false) { //si il y a un grex et pas de groupe
253
					$tab_gp = explode(' gx', $this->compo_nom['cultivar_groupe']);
254
					$html = ' <span class="gp">'.$tab_gp[0].' <abbr title="grex">gx</abbr></span>';
255
				} else { //si il n'y a pas de grex mais un groupe
256
					$html = ' <span class="gp">'.$this->compo_nom['cultivar_groupe'].' <abbr title="groupe">Gp</abbr>)</span>';
257
				}
258
			} else { //s'il n'y a pas de cultivar
259
				if (strrpos($this->compo_nom['cultivar_groupe'], ' gx ') !== false) { //si le grex est composé de groupe
260
					$tab_gp = explode(' gx ', $this->compo_nom['cultivar_groupe']);
261
					$html = ' <span class="gp">'.$tab_gp[0].
262
							' <abbr title="grex">gx</abbr>'.$tab_gp[1].
263
							' <abbr title="groupe">Gp</abbr></span>';
264
				} elseif (strrpos($this->compo_nom['cultivar_groupe'], ' gx') !== false) { //si il y a un grex et pas de groupe
265
					$tab_gp = explode(' gx', $this->compo_nom['cultivar_groupe']);
266
					$html = ' <span class="gp">'.$tab_gp[0].' <abbr title="grex">gx</abbr></span>';
267
				} else { //si il n'y a pas de grex mais un groupe
268
					$html = ' <span class="gp">'.$this->compo_nom['cultivar_groupe'].' <abbr title="groupe">Gp</abbr></span>';
269
				}
270
			}
271
		}
272
		return $html;
273
	}
274
 
275
	/**
276
	 *
277
	 * Permet de repérer s'il s'agit d'un hybride (infra-sp, genre, sp) ou d'une chimère.
278
	 * @param unknown_type $val
279
	 * @param unknown_type $type
280
	 */
281
	private function verifierHybridite($epithete, $type) {
282
		$html = '';
283
		if ($epithete != '') {
284
			if (substr($epithete, 0, 2) == 'x ') {
285
				$html = ' <span class=hyb>x <span class="'.$type.'">'.str_replace('x ', '', $epithete).'</span></span>';
286
			} elseif (substr($epithete, 0, 2) == '+ ') {
287
				$html = ' <span class=chimere>+ <span class="'.$type.'">'.str_replace('+ ', '', $epithete).'</span></span>';
288
			} else if (substr_count($epithete, ' x ') > 1) {// Cas d'une formule d'hybridité comprenant des parents hybrides
289
				$html = ' '.$this->insererBaliseFormuleHyb($epithete);
290
			} elseif (substr_count($epithete, ' x ') == 1) {// Cas d'une formule d'hybridité simple
291
				$html = ' '.$this->ajouterFomuleHybridite($epithete, $type);
292
			} else {
293
				$html = ' <span class="'.$type.'">'.$epithete.'</span>';
294
			}
295
		}
296
		return $html;
297
	}
298
 
299
	private function ajouterFomuleHybridite($formule, $type) {
300
		$tab_x = explode(' x ', $formule);
301
		$formule_hyb = array();
302
		switch ($type) {
303
			case 'gen' 		:
304
				foreach ($tab_x as $hyb) {
305
					$formule_hyb[] = sprintf($this->genTpl, $hyb);
306
				}
307
				break;
308
			case 'sp'		:
309
				foreach ($tab_x as $hyb) {
310
					if (substr_count($hyb, ' ') >= 1) {
311
						list($gen, $sp) = explode(' ', $hyb);
312
						$formule_hyb[] = sprintf($this->spFHTpl, $gen, $sp);
313
					} else {
314
						$avertissement = "Nom #%s : la valeur d'hybride '%s' ne contenait pas d'espace .";
315
						$this->traiterAvertissement($avertissement, array($this->num, $hyb));
316
					}
317
				}
318
				break;
319
			case 'infra-sp' :
320
				foreach ($tab_x as $hyb) {
321
					list($gen, $sp, $typeEpithete, $infraSp) = explode (' ', $hyb);
322
					$formule_hyb[] = sprintf($this->infraSpFHTpl, $gen, $sp, $this->abbr[$typeEpithete], $typeEpithete, $infraSp);
323
				}
324
				break;
325
			default : break;
326
		}
327
		return $this->insererBaliseFormuleHyb(implode(' x ', $formule_hyb));
328
	}
329
 
330
	private function insererBaliseFormuleHyb($formule) {
331
		return sprintf($this->formuleHybTpl, $formule);
332
	}
333
 
334
	private function lancerRequeteModification() {
335
		foreach ($this->table_requete as $id => $html) {
336
			$html = $this->bdd->proteger($html);
337
			$requete = "UPDATE {$this->table} ".
338
				"SET nom_sci_html = $html ".
339
				"WHERE num_nom = $id ";
340
			$res = $this->bdd->requeter($requete);
341
        	if ($res === false) {
342
        		$this->traiterErreur("erreur d'insertion pour le tuple %s", array($id));
343
        	}
344
		}
345
	}
346
}
347
?>