New file |
0,0 → 1,347 |
<?php |
/** |
* declare(encoding='UTF-8'); |
* Exemple de lancement du script : : |
* /opt/lampp/bin/php cli.php bdtfx -a nomSciHtml -t bdtfx_v1_01 |
* Classe permettant de créer le nom scientifique au format HTML. |
* @category php 5.2 |
* @package bdtfx |
* @author Jennifer DHÉ <jennifer@tela-botanica.org> |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org> |
* @copyright Copyright (c) 2011, Tela Botanica (accueil@tela-botanica.org) |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL |
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL |
* @version $Id$ |
*/ |
class Bdtfx extends Script { |
protected $bdd = null; |
protected $table = null; |
protected $num = null; |
protected $compo_nom = array(); |
protected $abbr = array ( |
'infra-gen.' => 'Infra-Genre', |
'sect.' => 'Section', |
'subsect.' => 'Sous-Section', |
'ser.' => 'Série', |
'subser.' => 'Sous-Série', |
'gr.' => 'Groupe', |
'agg.' => 'Agrégat', |
'sp.' => 'Espèce', |
'subsp.' => 'Sous-Espèce', |
'infra-sp.' => 'Infra-Espèce', |
'var.' => 'Variété', |
'subvar.' => 'Sous-Variété', |
'fa' => 'Forme', |
'subf.' => 'Sous-Forme', |
'f. sp.' => 'Forma species', |
'proles' => 'Race prole' |
); |
private $nomSciTpl = '<span class=sci>%s</span>'; |
private $nomSupraGenTpl = '<span class="supra_gen">%s</span>'; |
private $genTpl = '<span class="gen">%s</span>'; |
private $infraGenTpl = '<span class="infra-gen">%s</span>'; |
private $spFHTpl = '<span class="gen">%s</span> <span class="sp">%s</span>'; |
private $typeEpitheteTpl = '<abbr class="type_epithete" title="%s">%s</abbr>'; |
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>'; |
private $formuleHybTpl = '<span class="formule-hyb">%s</span>'; |
protected $table_requete; |
protected $limite_req_tuples = 10000; |
protected $depart_req_tuples = 0; |
|
protected $parametres_autorises = array( |
'-t' => array(true, true, 'Nom de la table qui doit être traitée')); |
|
public function executer() { |
// Lancement de l'action demandée |
$cmd = $this->getParametre('a'); |
switch ($cmd) { |
case 'nomSciHtml' : |
$this->genererNomSciHtml(); |
break; |
default : |
$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd)); |
} |
} |
|
private function genererNomSciHtml() { |
$this->initialiser(); |
|
$nbtot = $this->recupererNbTotalTuples(); |
while ($this->depart_req_tuples < $nbtot) { |
$resultat = $this->recupererTuples(); |
$nb_tot_res = count($resultat); |
$compteur = 1; |
foreach ($resultat as $tuples => $infos) { //pour chaque tuple |
$this->initialiserVariables($infos); |
|
$nomSciHtml = ''; |
$nomSciHtml .= $this->ajouterBaliseNomSupraGen(); |
$nomSciHtml .= $this->verifierHybridite($this->compo_nom['genre'], 'gen'); |
$nomSciHtml .= $this->ajouterBaliseInfraGen(); |
$nomSciHtml .= $this->verifierHybridite($this->compo_nom['epithete_sp'], 'sp'); |
$nomSciHtml .= $this->ajouterBaliseTypeInfraSp(); |
$nomSciHtml .= $this->verifierHybridite($this->compo_nom['epithete_infra_sp'], 'infra-sp'); |
$nomSciHtml .= $this->ajouterCultivarGpComm(); |
|
if ($nomSciHtml != '') { |
$this->table_requete[$this->num] = sprintf($this->nomSciTpl, trim($nomSciHtml)); |
} |
if (count($this->table_requete) == 1000) { |
$this->lancerRequeteModification(); |
array_splice($this->table_requete, 0); |
} elseif ($compteur == $nb_tot_res) { |
$this->lancerRequeteModification(); |
} |
$compteur++; |
$this->afficherAvancement('Génération des noms scientifique au format HTML en cours'); |
} |
} |
echo "\n"; |
} |
|
private function initialiser() { |
$this->bdd = new Bdd(); |
$this->table = $this->getParametre('t'); |
$this->preparerTable(); |
} |
|
private function preparerTable() { |
$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'nom_sci_html' "; |
$resultat = $this->bdd->recuperer($requete); |
if ($resultat === false) { |
$requete = "ALTER TABLE {$this->table} ". |
'ADD nom_sci_html VARCHAR( 500 ) '. |
'CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL '; |
$this->bdd->requeter($requete); |
} |
} |
|
private function recupererNbTotalTuples(){ |
$req = "SELECT count(*) AS nb FROM {$this->table} "; |
$res = $this->bdd->recuperer($req); |
return $res['nb']; |
} |
|
private function recupererTuples() { |
$requete = 'SELECT num_nom, rang, nom_supra_generique, genre, epithete_infra_generique, '. |
' epithete_sp, type_epithete, epithete_infra_sp,cultivar_groupe, '. |
' nom_commercial, cultivar '. |
"FROM {$this->table} ". |
"LIMIT {$this->depart_req_tuples},{$this->limite_req_tuples} "; |
|
$resultat = $this->bdd->recupererTous($requete); |
$this->depart_req_tuples += 10000; |
return $resultat; |
} |
|
private function initialiserVariables($infos) { |
$this->num = $infos['num_nom']; |
$this->compo_nom = $infos; |
} |
|
private function ajouterBaliseNomSupraGen() { |
$html = ''; |
if ($this->compo_nom['nom_supra_generique'] != '') { |
$html = sprintf($this->nomSupraGenTpl, $this->compo_nom['nom_supra_generique']); |
} |
return $html; |
} |
|
private function ajouterTypeEpithete($type) { |
if (!array_key_exists($type, $this->abbr)) { |
$this->abbr[$type] = $type; |
} |
} |
|
private function ajouterBaliseInfraGen() { |
$html = ''; |
if ($this->verifierTypeInfraGen()) { |
$html .= $this->ajouterBaliseTypeInfraGen(); |
} else { |
if ($this->avoirInfo('epithete_infra_generique')) { |
$html .= sprintf($this->infraGenTpl, $this->compo_nom['epithete_infra_generique']); |
} |
} |
return $html; |
} |
|
private function verifierTypeInfraGen() { |
$ok = false; |
if ($this->compo_nom['type_epithete'] != '' && $this->compo_nom['epithete_infra_generique'] != '') { |
$this->ajouterTypeEpithete($this->compo_nom['type_epithete']); |
$ok = true; |
} |
return $ok; |
} |
|
private function ajouterBaliseTypeInfraGen() { |
$html = ''; |
$type = $this->compo_nom['type_epithete']; |
|
if ($type == 'agg.') { // Ajout de l'infra gen avant le type s'il est égal à agg. |
$html .= ' '.$this->ajouterBaliseInfraGen(). |
' '.sprintf($this->typeEpitheteTpl, $this->abbr[$type], $type); |
} else { |
$html .= ' '.sprintf($this->typeEpitheteTpl, $this->abbr[$type], $type). |
' '.$this->ajouterBaliseInfraGen(); |
} |
return $html; |
} |
|
private function ajouterBaliseTypeInfraSp() { |
$html = ''; |
$type = $this->compo_nom['type_epithete']; |
$infraSp = $this->compo_nom['epithete_infra_sp']; |
|
if ($infraSp != '') { |
if ($type != '') { |
$this->ajouterTypeEpithete($type); |
$html = ' '.sprintf($this->typeEpitheteTpl, $this->abbr[$type], $type); |
} else { |
$message = "Nom #%s contient un épithète infra-spécifique mais son type n'est pas renseigné."; |
$this->traiterErreur($message, array($this->num)); |
} |
} |
return $html; |
} |
|
private function ajouterCultivarGpComm() { |
$html = ''; |
if ($this->avoirInfo('cultivar_groupe')) { |
$html .= ' '.$this->ajouterCultivarGroupe(); |
} |
if ($this->avoirInfo('nom_commercial')) { |
$html .= ' <span class="commercial">'.$this->compo_nom['nom_commercial'].'</span>'; |
} |
if ($this->avoirInfo('cultivar')) { |
$html .= ' <span class="cultivar">\''.$this->compo_nom['cultivar'].'\'</span>'; |
} |
return $html; |
} |
|
private function avoirInfo($valeur) { |
return (isset($this->compo_nom[$valeur]) && $this->compo_nom[$valeur] != '') ? true : false; |
} |
|
/** |
* Permet d'ajouter les groupes de cultivar en fonction de la présence d'un cultivar et de la présence d'un grex |
* |
* L'ensemble des individus obtenu par une fécondation particulière s'appelle un le Grex (que pour les orchidées). |
* Au sein du grex, certains individus peuvent se distinguer par des formes, des coloris ou autres qui font que |
* l'obtenteur du grex va les sélectionner. |
* les noms de groupes de cultivars sont suivis de l'abréviation « Gp » et placés entre parenthèses |
* les noms de grex, s'ils sont utilisés devant une épithète de cultivar, ne se mettent pas entre parenthèses |
* ex : Cymbidium Alexanderi gx 'Westonbirt' (cultivar_groupe = Alexanderi gx) ; |
* les noms de groupe se mettent entre parenthèses s'ils sont devant une épithète de cultivar |
* ex : Dracaena fragrans (Deremenis Gp) 'Christianne' (cultivar_groupe = Deremenis) |
* Un grex peut contenir des groupes (rédaction d'un exemple de l'ICNCP) |
* ex : × Rhyncosophrocattleya Marie Lemon Stick grex Francis Suzuki Group |
* ou : × Rhyncosophrocattleya Marie Lemon Stick gx Francis Suzuki Gp |
* @param unknown_type $val |
* |
*/ |
private function ajouterCultivarGroupe() { |
$html = ''; |
if ($this->avoirInfo('cultivar_groupe')) { //si le champ cultivar_groupe n'est pas vide |
if ($this->compo_nom['cultivar']) { //si il y a un cultivar, on ajoute des parenthèses au group (mais pas au grex) |
if (strrpos($this->compo_nom['cultivar_groupe'], ' gx ') !== false) { //si le grex est composé de groupe |
$tab_gp = explode(' gx ', $this->compo_nom['cultivar_groupe']); |
$html = ' <span class="gp">'.$tab_gp[0]. |
' <abbr title="grex">gx </abbr>('.$tab_gp[1]. |
' <abbr title="groupe">Gp</abbr>)</span>'; |
} elseif (strrpos($this->compo_nom['cultivar_groupe'], ' gx') !== false) { //si il y a un grex et pas de groupe |
$tab_gp = explode(' gx', $this->compo_nom['cultivar_groupe']); |
$html = ' <span class="gp">'.$tab_gp[0].' <abbr title="grex">gx</abbr></span>'; |
} else { //si il n'y a pas de grex mais un groupe |
$html = ' <span class="gp">'.$this->compo_nom['cultivar_groupe'].' <abbr title="groupe">Gp</abbr>)</span>'; |
} |
} else { //s'il n'y a pas de cultivar |
if (strrpos($this->compo_nom['cultivar_groupe'], ' gx ') !== false) { //si le grex est composé de groupe |
$tab_gp = explode(' gx ', $this->compo_nom['cultivar_groupe']); |
$html = ' <span class="gp">'.$tab_gp[0]. |
' <abbr title="grex">gx</abbr>'.$tab_gp[1]. |
' <abbr title="groupe">Gp</abbr></span>'; |
} elseif (strrpos($this->compo_nom['cultivar_groupe'], ' gx') !== false) { //si il y a un grex et pas de groupe |
$tab_gp = explode(' gx', $this->compo_nom['cultivar_groupe']); |
$html = ' <span class="gp">'.$tab_gp[0].' <abbr title="grex">gx</abbr></span>'; |
} else { //si il n'y a pas de grex mais un groupe |
$html = ' <span class="gp">'.$this->compo_nom['cultivar_groupe'].' <abbr title="groupe">Gp</abbr></span>'; |
} |
} |
} |
return $html; |
} |
|
/** |
* |
* Permet de repérer s'il s'agit d'un hybride (infra-sp, genre, sp) ou d'une chimère. |
* @param unknown_type $val |
* @param unknown_type $type |
*/ |
private function verifierHybridite($epithete, $type) { |
$html = ''; |
if ($epithete != '') { |
if (substr($epithete, 0, 2) == 'x ') { |
$html = ' <span class=hyb>x <span class="'.$type.'">'.str_replace('x ', '', $epithete).'</span></span>'; |
} elseif (substr($epithete, 0, 2) == '+ ') { |
$html = ' <span class=chimere>+ <span class="'.$type.'">'.str_replace('+ ', '', $epithete).'</span></span>'; |
} else if (substr_count($epithete, ' x ') > 1) {// Cas d'une formule d'hybridité comprenant des parents hybrides |
$html = ' '.$this->insererBaliseFormuleHyb($epithete); |
} elseif (substr_count($epithete, ' x ') == 1) {// Cas d'une formule d'hybridité simple |
$html = ' '.$this->ajouterFomuleHybridite($epithete, $type); |
} else { |
$html = ' <span class="'.$type.'">'.$epithete.'</span>'; |
} |
} |
return $html; |
} |
|
private function ajouterFomuleHybridite($formule, $type) { |
$tab_x = explode(' x ', $formule); |
$formule_hyb = array(); |
switch ($type) { |
case 'gen' : |
foreach ($tab_x as $hyb) { |
$formule_hyb[] = sprintf($this->genTpl, $hyb); |
} |
break; |
case 'sp' : |
foreach ($tab_x as $hyb) { |
if (substr_count($hyb, ' ') >= 1) { |
list($gen, $sp) = explode(' ', $hyb); |
$formule_hyb[] = sprintf($this->spFHTpl, $gen, $sp); |
} else { |
$avertissement = "Nom #%s : la valeur d'hybride '%s' ne contenait pas d'espace ."; |
$this->traiterAvertissement($avertissement, array($this->num, $hyb)); |
} |
} |
break; |
case 'infra-sp' : |
foreach ($tab_x as $hyb) { |
list($gen, $sp, $typeEpithete, $infraSp) = explode (' ', $hyb); |
$formule_hyb[] = sprintf($this->infraSpFHTpl, $gen, $sp, $this->abbr[$typeEpithete], $typeEpithete, $infraSp); |
} |
break; |
default : break; |
} |
return $this->insererBaliseFormuleHyb(implode(' x ', $formule_hyb)); |
} |
|
private function insererBaliseFormuleHyb($formule) { |
return sprintf($this->formuleHybTpl, $formule); |
} |
|
private function lancerRequeteModification() { |
foreach ($this->table_requete as $id => $html) { |
$html = $this->bdd->proteger($html); |
$requete = "UPDATE {$this->table} ". |
"SET nom_sci_html = $html ". |
"WHERE num_nom = $id "; |
$res = $this->bdd->requeter($requete); |
if ($res === false) { |
$this->traiterErreur("erreur d'insertion pour le tuple %s", array($id)); |
} |
} |
} |
} |
?> |