14,106 → 14,95 |
* @version $Id$ |
*/ |
class Bdtfx extends Script { |
|
protected $bdd = null; |
protected $table = null; |
private $projetNom = 'bdtfx'; |
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 $pasInsertion = 1000; |
protected $departInsertion = 0; |
|
protected $parametres_autorises = array( |
'-t' => array(false, true, 'Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).')); |
'-t' => array(true, true, 'Nom de la table qui doit être traitée')); |
|
public function executer() { |
try { |
$this->initialiser(); |
|
// Lancement de l'action demandée |
$cmd = $this->getParametre('a'); |
switch ($cmd) { |
case 'chargerTous' : |
$this->chargerStructureSql(); |
$this->chargerBdtfx(); |
$this->genererNomSciHtml(); |
break; |
case 'chargerStructureSql' : |
$this->chargerStructureSql(); |
break; |
case 'chargerBdtfx' : |
$this->chargerBdtfx(); |
break; |
case 'genererNomSciHtml' : |
$this->genererNomSciHtml(); |
break; |
default : |
throw new Exception("Erreur : la commande '$cmd' n'existe pas!"); |
} |
} catch (Exception $e) { |
$this->traiterErreur($e->getMessage()); |
// 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 initialiser() { |
$this->chargerConfigDuProjet(); |
$this->bdd = new Bdd(); |
} |
private function genererNomSciHtml() { |
$this->initialiser(); |
|
private function chargerConfigDuProjet() { |
$fichierIni = dirname(__FILE__).DS.$this->projetNom.'.ini'; |
if (file_exists($fichierIni)) { |
Config::charger($fichierIni); |
} else { |
$m = "Veuillez configurer le projet en créant le fichier '{$this->projetNom}.ini' ". |
"dans le dossier du module de script du projet à partir du fichier '{$this->projetNom}.defaut.ini'."; |
throw new Exception($m); |
} |
} |
$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); |
|
private function chargerStructureSql() { |
$chemin = Config::get('chemins.structureSql'); |
$requetes = Outils::extraireRequetes($chemin); |
foreach ($requetes as $requete) { |
$this->bdd->requeter($requete); |
} |
} |
$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(); |
|
private function chargerBdtfx() { |
$chemin = Config::get('chemins.bdtfx'); |
$table = Config::get('tables.bdtfx'); |
$requete = "LOAD DATA INFILE '$chemin' ". |
"REPLACE INTO TABLE $table ". |
'CHARACTER SET utf8 '. |
'FIELDS '. |
" TERMINATED BY '\t' ". |
" ENCLOSED BY '' ". |
" ESCAPED BY '\\\' ". |
'IGNORE 1 LINES'; |
$this->bdd->requeter($requete); |
} |
|
private function genererNomSciHtml() { |
$this->initialiserGenerationNomsSciHtml(); |
$this->preparerTable(); |
$generateur = new GenerateurNomSciHtml(); |
$nbreTotal = $this->recupererNbTotalTuples(); |
while ($this->departInsertion < $nbreTotal) { |
$resultat = $this->recupererTuples(); |
$nomsSciEnHtml = $generateur->generer($resultat); |
$this->lancerRequeteModification($nomsSciEnHtml); |
$this->departInsertion += $this->pasInsertion; |
$this->afficherAvancement("Insertion des noms scientifique au format HTML dans la base par paquet de {$this->pasInsertion} en cours"); |
if ($this->stopperLaBoucle()) { |
break; |
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 initialiserGenerationNomsSciHtml() { |
require_once dirname(__FILE__).DS.'GenerateurNomSciHtml.php'; |
echo dirname(__FILE__).DS.'GenerateurNomSciHtml.php'; |
$this->table = Config::get('tables.bdtfx'); |
private function initialiser() { |
$this->bdd = new Bdd(); |
$this->table = $this->getParametre('t'); |
$this->preparerTable(); |
} |
|
private function preparerTable() { |
121,16 → 110,16 |
$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 '; |
'ADD nom_sci_html VARCHAR( 500 ) '. |
'CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL '; |
$this->bdd->requeter($requete); |
} |
} |
|
private function recupererNbTotalTuples(){ |
$requete = "SELECT count(*) AS nb FROM {$this->table} "; |
$resultat = $this->bdd->recuperer($requete); |
return $resultat['nb']; |
$req = "SELECT count(*) AS nb FROM {$this->table} "; |
$res = $this->bdd->recuperer($req); |
return $res['nb']; |
} |
|
private function recupererTuples() { |
138,33 → 127,221 |
' epithete_sp, type_epithete, epithete_infra_sp,cultivar_groupe, '. |
' nom_commercial, cultivar '. |
"FROM {$this->table} ". |
"LIMIT {$this->departInsertion},{$this->pasInsertion} "; |
"LIMIT {$this->depart_req_tuples},{$this->limite_req_tuples} "; |
|
$resultat = $this->bdd->recupererTous($requete); |
$this->depart_req_tuples += 10000; |
return $resultat; |
} |
|
private function lancerRequeteModification($nomsSciHtm) { |
foreach ($nomsSciHtm as $id => $html) { |
$html = $this->bdd->proteger($html); |
$requete = "UPDATE {$this->table} ". |
"SET nom_sci_html = $html ". |
"WHERE num_nom = $id "; |
$resultat = $this->bdd->requeter($requete); |
if ($resultat === false) { |
throw new Exception("Erreur d'insertion pour le tuple $id"); |
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 stopperLaBoucle() { |
$stop = false; |
static $ligneActuelle = 1; |
if ($nbreLignesATester = $this->getParametre('t')) { |
if ($nbreLignesATester == $ligneActuelle++) { |
$stop = true; |
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 $stop; |
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)); |
} |
} |
} |
} |
?> |