Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 1308 | Rev 1311 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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