New file |
0,0 → 1,400 |
<?php |
/** Exemple lancement: |
* /opt/lampp/bin/php -d memory_limit=3500M ~/web/eflore-projets/scripts/cli.php sauvages -a chargerTous |
*/ |
class Sauvages extends EfloreScript { |
private $contenu_fichier = array(); |
protected $parametres_autorises = array( |
'-f' => array(true, true, 'Nom du fichier ou du dossier à traiter')); |
private $caracteresAccentues = array( |
'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', |
'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', |
'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', |
'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', |
'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', |
'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ', |
'ğ', 'Ġ', 'ġ', 'Ģ', 'ģ', 'Ĥ', 'ĥ', 'Ħ', 'ħ', 'Ĩ', 'ĩ', 'Ī', 'ī', 'Ĭ', 'ĭ', |
'Į', 'į', 'İ', 'ı', 'IJ', 'ij', 'Ĵ', 'ĵ', 'Ķ', 'ķ', 'Ĺ', 'ĺ', 'Ļ', 'ļ', 'Ľ', |
'ľ', 'Ŀ', 'ŀ', 'Ł', 'ł', 'Ń', 'ń', 'Ņ', 'ņ', 'Ň', 'ň', 'ʼn', 'Ō', 'ō', 'Ŏ', |
'ŏ', 'Ő', 'ő', 'Œ', 'œ', 'Ŕ', 'ŕ', 'Ŗ', 'ŗ', 'Ř', 'ř', 'Ś', 'ś', 'Ŝ', 'ŝ', |
'Ş', 'ş', 'Š', 'š', 'Ţ', 'ţ', 'Ť', 'ť', 'Ŧ', 'ŧ', 'Ũ', 'ũ', 'Ū', 'ū', 'Ŭ', |
'ŭ', 'Ů', 'ů', 'Ű', 'ű', 'Ų', 'ų', 'Ŵ', 'ŵ', 'Ŷ', 'ŷ', 'Ÿ', 'Ź', 'ź', 'Ż', |
'ż', 'Ž', 'ž', 'ſ', 'ƒ', 'Ơ', 'ơ', 'Ư', 'ư', 'Ǎ', 'ǎ', 'Ǐ', 'ǐ', 'Ǒ', 'ǒ', |
'Ǔ', 'ǔ', 'Ǖ', 'ǖ', 'Ǘ', 'ǘ', 'Ǚ', 'ǚ', 'Ǜ', 'ǜ', 'Ǻ', 'ǻ', 'Ǽ', 'ǽ', 'Ǿ', 'ǿ'); |
private $caracteresNormaux = array( |
'A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', |
'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's', |
'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', |
'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a', |
'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', |
'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', |
'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', |
'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', |
'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', |
'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', |
'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', |
'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', |
'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o'); |
|
public function executer() { |
// Lancement de l'action demandée |
try { |
$this->initialiserProjet('sauvages'); |
|
$cmd = $this->getParametre('a'); |
switch ($cmd) { |
case 'chargerTous' : |
$this->creerXmlTaxons(); |
$this->creerXmlCriteres(); |
break; |
case 'creerXmlCriteres' : |
$this->creerXmlCriteres(); |
break; |
case 'creerXmlTaxons' : |
$this->creerXmlTaxons(); |
break; |
default : |
throw new Exception("Erreur : la commande '$cmd' n'existe pas!"); |
} |
} catch (Exception $e) { |
$this->traiterErreur($e->getMessage()); |
} |
} |
|
public function creerXmlCriteres() { |
$this->recupererContenuFichier(); |
$criteres_nom = $this->contenu_fichier[0]; |
unset($this->contenu_fichier[0]); |
$criteres_label = $this->contenu_fichier[1]; |
unset($this->contenu_fichier[1]); |
$description_label = $this->contenu_fichier[2]; |
unset($this->contenu_fichier[2]); |
$criteres_valeurs = $this->extraireValeurs(); |
$xml = $this->formerXmlCriteres($criteres_nom, $criteres_label, $description_label, $criteres_valeurs);//print_r($xml); |
file_put_contents("./xml_criteres.xml", $xml); |
} |
|
|
public function formerXmlCriteres($nom, $label, $description, $valeurs) { |
$xml='<keys> |
<groupe id="1"> |
<nom>Arbres</nom> |
<media>images/pictos/icone_arbre.png</media> |
<criteres>'; |
foreach ($label as $id=>$criteres) { |
if ($id > 3) { |
$xml .= '<critere id="1.'.$id.'"> |
<label>'.$criteres.'</label> |
<description>'.$description[$id].'</description> |
<valeurs>'; |
foreach ($valeurs[$id] as $id_val=>$valeur) { |
if ($valeur != "0") { |
$crit = str_replace(" ", "_", $this->supprimerAccents($nom[$id])); |
$val = str_replace(" ", "_", $this->supprimerAccents($valeur)); |
$image = $crit."_".$val; |
$xml .= '<valeur code="1.'.$id.'.'.$id_val.'" media="images/pictos/'.$image.'.png">'.$valeur.'</valeur>'; |
} |
} |
$xml .= '</valeurs></critere>'; |
} |
} |
$xml .= "</criteres> |
</groupe> |
</keys>"; |
return $xml; |
} |
|
private function extraireValeurs() { |
$valeurs = array(); |
foreach ($this->contenu_fichier as $ligne) { |
$i = 0; |
if ($ligne != "") { |
foreach ($ligne as $critere) { |
$valeur_multiple = explode("/", $critere); |
foreach ($valeur_multiple as $valeur) { |
if (!isset($valeurs[$i]) || !in_array(trim($valeur), $valeurs[$i])) { |
$valeurs[$i][] = trim($valeur); |
} |
} |
$i++; |
} |
} |
} |
return $valeurs; |
} |
|
|
public function creerXmlTaxons() { |
$this->recupererContenuFichier(); |
$criteres_label = array_flip($this->contenu_fichier[0]); |
unset($this->contenu_fichier[0]); |
unset($this->contenu_fichier[1]); |
unset($this->contenu_fichier[2]); |
$criteres_valeurs = $this->extraireValeurs(); |
$xml = $this->formerXmlTaxons($criteres_label, $criteres_valeurs); //print_r($xml); |
file_put_contents("./xml_taxons.xml", $xml); |
} |
|
public function formerXmlTaxons($criteres_label, $criteres_valeurs) { |
$xml='<?xml version="1.0" ?><TAXONS SUBJECT="XML">'; |
$rest = new RestClient(); |
foreach ($this->contenu_fichier as $id=>$taxon) { |
if ($taxon != "") { |
$infos_taxon = $this->rechercherInfosTaxon($rest, $taxon[$criteres_label["nom scientifique"]]); |
$xml .= '<TAXON id="'.$id.'" value="'. |
ucfirst($taxon[$criteres_label["nom vernaculaire"]]).'" sciName="'.trim($taxon[$criteres_label["nom scientifique"]]).'" groupe="1"> |
<DESCRIPTION>'.$infos_taxon["description"].'</DESCRIPTION> |
<PICTURES>'; |
if ($infos_taxon["images"] != array()) { |
if (isset($infos_taxon["images"][$taxon[$criteres_label["image priorite"]]])) { |
$auteur = $this->rechercherInfosAuteurImage($infos_taxon["images"][$taxon[$criteres_label["image priorite"]]], $rest).", www.tela-botanica.org, CC-Licence (by-sa)"; |
$xml .= '<PICTURE media="'.$infos_taxon["images"][$taxon[$criteres_label["image priorite"]]]["binaire.href"].'"><author>'.$auteur.'</author></PICTURE>'; |
unset($infos_taxon["images"][$taxon[$criteres_label["image priorite"]]]); |
} |
foreach ($infos_taxon["images"] as $image) { |
$auteur = $this->rechercherInfosAuteurImage($image, $rest).", www.tela-botanica.org, CC-Licence (by-sa)"; |
$xml .= '<PICTURE media="'.$image["binaire.href"].'"><author>'.$auteur.'</author></PICTURE>'; |
} |
} |
$xml .= '</PICTURES><CRITERIAS>'; |
for ($i=4; $i < count($taxon); $i++) { |
if (trim($taxon[$i]) !== "0") { |
$valeurs = array(); |
foreach (explode("/",$taxon[$i]) as $valeur) { |
$valeurs[] = '1.'.$i.'.'.array_search(trim($valeur), $criteres_valeurs[$i]); |
} |
$xml .= '<VALUE code="'.implode(',', $valeurs).'"/>'; |
} |
} |
$xml .= '</CRITERIAS></TAXON>'; |
} |
} |
$xml .= "</TAXONS>"; |
return $xml; |
} |
|
// interroge l'annuaire pour récupérer le pseudo |
private function rechercherInfosAuteurImage($image, $rest) { |
$courriel = $image["observation"]["auteur.courriel"]; |
$url_auteur = "http://www.tela-botanica.org/service:annuaire:utilisateur/identite-par-courriel/".$courriel; |
$reponse = $rest->consulter($url_auteur); |
$reponse = json_decode($reponse, true); |
if (isset($reponse[$courriel]) && $reponse[$courriel]["pseudoUtilise"] == true) { |
$auteur = $reponse[$courriel]["pseudo"]; |
} else{ |
$auteur = $image["observation"]["auteur.prenom"]." ".$image["observation"]["auteur.nom"]; |
} |
return $auteur; |
} |
|
private function rechercherInfosTaxon($rest, $ns) { |
$info = array("description" => "", "images" => array()); |
|
$reponse = $this->consulterWebService($rest, "bdtfx", |
"noms", "?retour.champs=nom_retenu.id,num_taxonomique&retour.tri=retenu&masque=".urlencode(trim($ns))); |
if (is_array($reponse["resultat"])) { |
$num_nom = $reponse["resultat"][key($reponse["resultat"])]["nom_retenu.id"]; |
$num_taxon = $reponse["resultat"][key($reponse["resultat"])]['num_taxonomique']; |
} |
|
$info["description"] = $this->rechercherDescription($num_taxon, $num_nom, $rest); |
$info["images"] =array(); |
$tags = array('fleur', 'feuille', 'rameau', 'port', 'fruit'); |
$cles = array(); |
foreach ($tags as $tag) { |
$url_image = "http://www.tela-botanica.org/service:del:0.1/images". |
"?masque.referentiel=bdtfx&masque.tag={$tag}&tri=votes&ordre=desc&protocole=3&navigation.limite=1&format=CRS&masque.nn=".$num_nom; |
$image = $rest->consulter($url_image); |
$image = json_decode($image, true); |
if ($image["resultats"] !== array()) { |
$info["images"][$tag]["binaire.href"] = $image["resultats"][key($image["resultats"])]["binaire.href"]; |
$info["images"][$tag]["observation"] = $image["resultats"][key($image["resultats"])]["observation"]; |
$cles[key($image["resultats"])] = ""; |
} |
} |
|
//si image taguée absente |
$url_image = "http://www.tela-botanica.org/service:del:0.1/images". |
"?masque.referentiel=bdtfx&tri=votes&ordre=desc&protocole=3&navigation.limite=5&format=CRS&masque.nn=".$num_nom; |
$reponse = $rest->consulter($url_image); |
$reponse = json_decode($reponse, true); |
|
if ($cles !== array()) { |
// supprimer les images communes au tag et les mieux notés |
$reponse["resultats"] = array_diff_key($reponse["resultats"], $cles); |
for ($i = 0; $i < 5-count($cles); $i++) { |
$image = array_shift($reponse["resultats"]); |
$info["images"][$i]["binaire.href"] = $image["binaire.href"]; |
$info["images"][$i]["observation"] = $image["observation"]; |
} |
} else { |
$info["images"] = $reponse["resultats"]; |
} |
|
return $info; |
} |
|
|
|
private function rechercherDescription($num_taxon, $num_nom, $rest) { |
$url_wiki = "http://www.tela-botanica.org/wikini/eFloreRedaction/api/rest/0.5/pages/SmartFloreBDTFXnt". |
$num_taxon."?txt.format=text/plain&txt.section.titre=Comment%20la%20reconnaître%20%3F"; |
$wiki = $rest->consulter($url_wiki); $wiki = json_decode($wiki, true); |
if ($wiki['texte'] != "" && strstr($wiki['texte'], 'Brève phrase sur son type biologique (simplifié)') === false ){ |
$description = $this->formaterSmartflore($wiki['texte']); |
} else { |
$coste = $this->consulterWebService($rest, "coste", "textes", "/bdtfx.nn:".$num_nom); |
if ($coste['resultats'] != array() && $coste['resultats'][key($coste["resultats"])]['texte'] != "") { |
$description = $this->formaterCoste($coste['resultats']); |
} else { |
$baseflor = $this->consulterWebService($rest, "baseflor", "informations", "/bdtfx.nn:".$num_nom); |
$description = $this->formaterBaseflor($baseflor); |
} |
} |
return $description; |
} |
|
//remplacement des \n par <br /> |
private function formaterSmartflore($texte) { |
$a_enlever = array(Chr(10).'=', '='.Chr(10), Chr(10).''.Chr(10), '*'); |
$texte = str_replace($a_enlever, '', $texte); |
$texte = str_replace(Chr(10), '<br />', $texte); |
//supprimer image et lien |
$texte = preg_replace('/\[\[http\:\/\/upload.*\.jpg [a-z| |A-Z]*\]\]/', '', $texte); |
$texte = str_replace('[[http://fr.wikipedia.org/wiki/Corymbe corymbes]]', 'corymbes', $texte); |
$texte = preg_replace('/\[\[SmartFlore.*[0-9] (.*)\]\]/', '\1', $texte); |
$texte = preg_replace('/\[\[http\:\/\/www\..* (.*)\]\]/', '\1', $texte); |
$texte = trim(str_replace('<br /><br />', '<br />', $texte)); |
return $texte; |
} |
|
|
private function formaterCoste($coste) { |
$texte = $this->mettreEnFormeCoste($coste[key($coste)]['texte']); |
return $texte; |
} |
|
static function mettreEnFormeCoste($texte) { |
$txt_fmt = array(); |
//decouper elements remarquables avant le texte |
self::separerNomScientifique_a_NomCommun($texte, $txt_fmt); |
$txt_fmt = array(); |
$texte = preg_replace('/\//','',$texte); |
//decouper elements remarquables après le texte |
self::separerEcologie_a_Usages($texte, $txt_fmt); |
//le morceau qui reste est le gros de la description |
$texte = str_replace(';','</br> -','- '.$texte); |
$texte = str_replace('–','',$texte); |
$txt_fmt['description'] = $texte; |
$retour = ""; |
foreach ($txt_fmt as $titre=>$parag) { |
$parag = preg_replace('/\- \. /','- ',$parag); |
$parag = preg_replace('/\. \./','.',$parag); |
$retour .= ucfirst($titre)." : ".trim($parag)." ; "; |
} |
return $retour; |
} |
static function separerNomScientifique_a_NomCommun(&$txt, &$txt_fmt){ |
if ( preg_match('/\*\*(.+)\*\*([^–]*)–/', $txt, $retour)){ |
/* !! attention on enlève un tiret cadratin – pas un trait d'union - !! */ |
$a_enlever = array('/–/','/\./' ); |
$txt_fmt['nom_scientifique'] = preg_replace($a_enlever,'',$retour[1]); |
if(preg_match('/\((.+)\)/',$retour[2],$synonymes)){ |
$txt_fmt['synonymes'] = $synonymes[1]; |
} else { |
$txt_fmt['nom_scientifique'] .= $retour[2]; |
} |
$txt = str_replace($retour[0],'',$txt); |
} |
/* !! attention il y a un espace avant les // du début !! */ |
if ( preg_match('/^ \/\/([^\/\/]+)\/\//', $txt, $retour)){ |
$a_enlever = array('/–/','/\./' ); |
$txt_fmt['nom_commun'] = preg_replace($a_enlever,'',$retour[1]); |
$txt = str_replace($retour[0],'',$txt); |
} |
} |
|
|
static function separerEcologie_a_Usages(&$txt, &$txt_fmt) { |
if (preg_match('/\.\s*([A-ZÉÀÈ].+)$/',$txt, $retour)) { |
$txt_fmt['ecologie'] = $retour[1]; |
$txt = str_replace($retour[0],'.',$txt); |
if (isset($txt_fmt['ecologie']) && preg_match('/–(.+)/', $txt_fmt['ecologie'] , $retour)){ |
$txt_fmt['repartition'] = $retour[1]; |
$txt_fmt['ecologie'] = str_replace($retour[0],'',$txt_fmt['ecologie']); |
} |
if (isset($txt_fmt['repartition']) && preg_match('/=(.+)$/', $txt_fmt['repartition'], $retour)){ |
$txt_fmt['floraison'] = $retour[1]; |
$txt_fmt['repartition'] = str_replace($retour[0],'',$txt_fmt['repartition']); |
$txt_fmt['repartition'] = str_replace(';',',',$txt_fmt['repartition']); |
} |
if (isset($txt_fmt['floraison']) && preg_match('/–(.+)$|\n(.+)$/',$txt_fmt['floraison'], $retour)){ |
$txt_fmt['usages'] = isset($retour[1]) ? $retour[1] : $retour[2]; |
$txt_fmt['floraison'] = str_replace($retour[0],'.',$txt_fmt['floraison']); |
} |
if (isset($txt_fmt['floraison']) && preg_match('/([Ff]l\.) (.+)/',$txt_fmt['floraison'], $retour)){ |
$txt_fmt['floraison'] = $retour[2]; |
$txt_fmt['floraison'] = str_replace($retour[1],'',$txt_fmt['floraison']); |
} |
if (isset($txt_fmt['floraison']) && preg_match('/([Ff]r\.) (.+)/',$txt_fmt['floraison'], $retour)){ |
$txt_fmt['fructification'] = $retour[2]; |
$txt_fmt['floraison'] = str_replace($retour[0],'',$txt_fmt['floraison']); |
$txt_fmt['floraison'] = str_replace(',','',$txt_fmt['floraison']); |
$txt_fmt['fructification'] = str_replace($retour[1],'',$txt_fmt['fructification']); |
$txt_fmt['fructification'] = str_replace('.','',$txt_fmt['fructification']); |
} |
} |
} |
|
private function formaterBaseflor($reponse) { |
$description = "En cours de rédaction."; |
if (isset($reponse["nom_sci"]) && $reponse["nom_sci"] != "") { |
$description = "Formation végétale : ".$reponse["form_vegetale"]."; ". |
"Inflorescence : ".$reponse["inflorescence"]."; ". |
"Couleur de la fleur : ".$reponse["couleur_fleur"]."; ". |
"Sexualité : ".$reponse["sexualite"]."; ". |
"Fruit : ".$reponse["fruit"]."; ". |
"Pollinisation : ".$reponse["pollinisation"]."; ". |
"Dissémination : ".$reponse["dissemination"]."; ". |
"Ecologie : ".$reponse["carac_ecolo"]." – ". |
"Chorologie : ".$reponse["chorologie"]."; ". |
"Floraison : ".$reponse["floraison"]; |
} |
return $description; |
} |
|
private function consulterWebService($rest, $projet,$resssource,$parametres) { |
$url_id = "http://api.tela-botanica.org/service:eflore:0.1/".$projet."/".$resssource.$parametres; |
$reponse = $rest->consulter($url_id); |
return json_decode($reponse, true); |
} |
|
|
private function recupererContenuFichier() { |
$nomFichier = Config::get('dossierCsv').Config::get('projet'); |
if ($nomFichier && file_exists($nomFichier) ){ |
$extensionFichier = strtolower(strrchr($nomFichier, '.')); |
if ($extensionFichier === ".csv"){ |
$file = new SplFileObject($nomFichier); |
$file->setFlags(SplFileObject::SKIP_EMPTY); |
$i = 0; |
echo "Traitement du fichier : "; |
while (!$file->eof()){ |
$ligne_csv = $file->fgetcsv($delimiter = ';'); |
$this->contenu_fichier[$i] = $ligne_csv; |
echo str_repeat(chr(8), ( strlen( $i ) + 1 ))."\t".$i++; |
} |
echo "\n"; |
} else { |
$this->traiterErreur("Le fichier : $nomFichier n'est pas au format csv."); |
} |
} else { |
$this->traiterErreur("Le fichier : $nomFichier n'existe pas."); |
} |
} |
|
public function supprimerAccents($chaine) { |
return str_replace($this->caracteresAccentues, $this->caracteresNormaux, $chaine); |
} |
} |
?> |