Rev 1313 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?php
/*
* @copyright 2013 Tela Botanica (accueil@tela-botanica.org)
* @author Raphaël Droz <raphael@tela-botanica.org>
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
*
* pattern: /service:eflore:0.1/coste/textes/bdtfx.nn:182,631
* params: txt.format=(htm|txt) , retour.champs=(titre,texte,...) , retour.format=(min|max), ...
*
* Ce webservice est censé pouvoir:
* 1) retourner des informations (choisies) à propos d'un ou plusieurs taxon(s) donné(s)
* (à partir de son numéro nomenclatural
* 2) retourner des informations (choisies) à propos de taxons recherchés
* (à partir de divers critères)
*
* TODO: masque.titre => masque.tag
* TODO: clarifier l'attribut "tag" retourné (tag de la description ou des clefs de détermination)
*
*/
/* restore_error_handler();
error_reporting(E_ALL); */
class Textes {
const MIME_JSON = 'application/json';
private $plantuseurl = 'https://uses.plantnet-project.org/f/api.php?section=0&action=parse&format=json'.
'&prop=wikitext&disabletoc=1&disableeditsection=1&disablelimitreport=1&page=';
private $parametres = array();
private $ressources = array();
private $Bdd;
private $format = 'min';
private $retour_mime = 'application/json';
private $nbreTextes = '0';
public function __construct(Bdd $bdd = null, Array $config = null) {
$this->config = is_null($config) ? Config::get('Textes') : $config;
$this->Bdd = is_null($bdd) ? new Bdd() : $bdd;
}
public function consulter($ressources, $parametres) {
$this->parametres = $parametres;
$this->ressources = $ressources;
$this->definirValeurParDefautDesParametres();
$this->format = (isset($this->parametres['retour.format']) && $this->parametres['retour.format'] != '') ? $this->parametres['retour.format'] : $this->format;
$this->retour_mime = (isset($this->parametres['retour']) && $this->parametres['retour'] != '') ? $this->parametres['retour'] : $this->retour_mime;
$resultat = $this->obtenirTextes();
$entete = $this->construireEntete();
return array('entete' => $entete, 'resultats' => $resultat);
}
private function construireEntete() {
$entete = array('masque' => '', 'depart' => 0, 'limite' => 100, 'total' => 0);
$entete['masque'] = $this->recupererMasque();
$entete['depart'] = (int) $this->parametres['navigation.depart'];
$entete['limite'] = (int) $this->parametres['navigation.limite'];
return $entete;
}
private function recupererMasque() {
$masqueEntete = '';
foreach ($this->parametres as $param => $cle) {
if ($param == 'masque') {
$masqueEntete = 'masque='.$cle.',';
} elseif (substr($param, 0, 7) == 'masque.') {
$masqueEntete .= substr($param, 7).'='.$cle.',';
}
}
$masqueEntete = rtrim($masqueEntete,',');
return $masqueEntete;
}
private function definirValeurParDefautDesParametres() {
if (isset($this->parametres['retour']) == false) {
$this->parametres['retour'] = self::MIME_JSON;
}
if (isset($this->parametres['retour.format']) == false) {
$this->parametres['retour.format'] = 'min';
}
if (isset($this->parametres['navigation.depart']) == false) {
$this->parametres['navigation.depart'] = 0;
}
if (isset($this->parametres['navigation.limite']) == false) {
$this->parametres['navigation.limite'] = 100;
}
}
private function obtenirTextes() {
$retour = array();
if ($this->parametres['masque'] != "") {
$this->parametres['masque'] = str_replace(" ", "_", $this->parametres['masque']);
$url = $this->plantuseurl.$this->parametres['masque'];
$json = file_get_contents($url);
if ($json != false) {
$tableau = json_decode($json, TRUE);
if (isset($tableau['parse']['wikitext']['*'])) {
$texte = $tableau['parse']['wikitext']['*'];
if (strpos($texte, "Résumé des usages") !== FALSE) {
$texte = substr($texte, strpos($texte, "{{Encadr")+63, -2);
$texte = str_replace("*", "", $texte);
$retour['usages'] = array_filter(array_map('trim', explode("\n", $texte)), 'strlen');
$retour['url'] = $url;
}
}
}
}
return $retour;
}
}