Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 1314 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1308 delphine 1
<?php
2
/*
3
 * @copyright 2013 Tela Botanica (accueil@tela-botanica.org)
4
 * @author Raphaël Droz <raphael@tela-botanica.org>
5
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
6
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
7
 *
8
 * pattern: /service:eflore:0.1/coste/textes/bdtfx.nn:182,631
9
 * params: txt.format=(htm|txt) ,  retour.champs=(titre,texte,...) , retour.format=(min|max), ...
10
 *
11
 * Ce webservice est censé pouvoir:
12
 * 1) retourner des informations (choisies) à propos d'un ou plusieurs taxon(s) donné(s)
13
 *	  (à partir de son numéro nomenclatural
14
 * 2) retourner des informations (choisies) à propos de taxons recherchés
15
 *	  (à partir de divers critères)
16
 *
17
 * TODO: masque.titre => masque.tag
18
 * TODO: clarifier l'attribut "tag" retourné (tag de la description ou des clefs de détermination)
19
 *
20
 */
21
 
22
 
23
/* restore_error_handler();
24
   error_reporting(E_ALL); */
25
class Textes {
26
 
1311 delphine 27
    const MIME_JSON = 'application/json';
28
    private $plantuseurl = 'https://uses.plantnet-project.org/f/api.php?section=0&action=parse&format=json'.
29
        '&prop=wikitext&disabletoc=1&disableeditsection=1&disablelimitreport=1&page=';
30
 
1308 delphine 31
    private $parametres = array();
32
    private $ressources = array();
33
    private $Bdd;
34
 
35
    private $format = 'min';
36
    private $retour_mime = 'application/json';
37
    private $nbreTextes = '0';
38
 
39
    public function __construct(Bdd $bdd = null, Array $config = null) {
40
        $this->config = is_null($config) ? Config::get('Textes') : $config;
41
        $this->Bdd = is_null($bdd) ? new Bdd() : $bdd;
42
    }
43
    public function consulter($ressources, $parametres) {
44
 
45
        $this->parametres = $parametres;
46
        $this->ressources = $ressources;
47
 
48
        $this->definirValeurParDefautDesParametres();
49
 
50
        $this->format = (isset($this->parametres['retour.format']) && $this->parametres['retour.format'] != '') ?  $this->parametres['retour.format'] : $this->format;
51
        $this->retour_mime = (isset($this->parametres['retour']) && $this->parametres['retour'] != '') ?  $this->parametres['retour'] : $this->retour_mime;
1311 delphine 52
        $resultat = $this->obtenirTextes();
53
        $entete = $this->construireEntete();
54
        return array('entete' => $entete, 'resultats' => $resultat);
1308 delphine 55
 
56
    }
57
    private function construireEntete() {
58
        $entete = array('masque' => '', 'depart' => 0, 'limite' => 100, 'total' => 0);
59
 
60
        $entete['masque'] = $this->recupererMasque();
61
        $entete['depart'] = (int) $this->parametres['navigation.depart'];
62
        $entete['limite'] = (int) $this->parametres['navigation.limite'];
63
 
64
        return $entete;
65
    }
66
 
67
    private function recupererMasque() {
68
        $masqueEntete = '';
69
        foreach ($this->parametres as $param => $cle) {
70
            if ($param == 'masque') {
71
                $masqueEntete = 'masque='.$cle.',';
72
            } elseif (substr($param, 0, 7) == 'masque.') {
73
                $masqueEntete .= substr($param, 7).'='.$cle.',';
74
            }
75
        }
76
        $masqueEntete = rtrim($masqueEntete,',');
77
        return $masqueEntete;
78
    }
79
 
1310 delphine 80
    private function definirValeurParDefautDesParametres() {
81
        if (isset($this->parametres['retour']) == false) {
82
            $this->parametres['retour'] = self::MIME_JSON;
83
        }
84
        if (isset($this->parametres['retour.format']) == false) {
85
            $this->parametres['retour.format'] = 'min';
86
        }
87
        if (isset($this->parametres['navigation.depart']) == false) {
88
            $this->parametres['navigation.depart'] = 0;
89
        }
90
        if (isset($this->parametres['navigation.limite']) == false) {
91
            $this->parametres['navigation.limite'] = 100;
92
        }
93
    }
94
 
1308 delphine 95
    private function obtenirTextes() {
1314 delphine 96
        $retour = array();
1311 delphine 97
        if ($this->parametres['masque'] != "") {
98
            $this->parametres['masque'] = str_replace(" ", "_", $this->parametres['masque']);
1314 delphine 99
            $url = $this->plantuseurl.$this->parametres['masque'];
100
            $json = file_get_contents($url);
1311 delphine 101
            if ($json != false) {
102
                $tableau = json_decode($json, TRUE);
103
                if (isset($tableau['parse']['wikitext']['*'])) {
104
                    $texte = $tableau['parse']['wikitext']['*'];
1313 delphine 105
                    if (strpos($texte, "Résumé des usages") !== FALSE) {
1314 delphine 106
                        $texte = substr($texte, strpos($texte, "{{Encadr")+63, -2);
107
                        $texte = str_replace("*", "", $texte);
108
                        $retour['usages'] = array_filter(array_map('trim', explode("\n", $texte)), 'strlen');
1315 delphine 109
                        $retour['url'] = "https://uses.plantnet-project.org/fr/".$this->parametres['masque'];
1313 delphine 110
                    }
1311 delphine 111
                }
1308 delphine 112
            }
113
        }
114
        return $retour;
115
    }
116
}