Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

Rev 1310 Rev 1311
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
    
-
 
27
    const MIME_JSON = 'application/json';
26
    private $plantuseurl = 'https://uses.plantnet-project.org/f/api.php?section=0&action=parse&format=json
28
    private $plantuseurl = 'https://uses.plantnet-project.org/f/api.php?section=0&action=parse&format=json'.
27
        &prop=wikitext&disabletoc=1&disableeditsection=1&disablelimitreport=1&page=';
29
        '&prop=wikitext&disabletoc=1&disableeditsection=1&disablelimitreport=1&page=';
28
    
30
    
29
    private $parametres = array();
31
    private $parametres = array();
30
    private $ressources = array();
32
    private $ressources = array();
31
    private $Bdd;
33
    private $Bdd;
32
    
34
    
33
    private $format = 'min';
35
    private $format = 'min';
34
    private $retour_mime = 'application/json';
36
    private $retour_mime = 'application/json';
35
    private $nbreTextes = '0';
37
    private $nbreTextes = '0';
36
    
38
    
37
    public function __construct(Bdd $bdd = null, Array $config = null) {
39
    public function __construct(Bdd $bdd = null, Array $config = null) {
38
        $this->config = is_null($config) ? Config::get('Textes') : $config;
40
        $this->config = is_null($config) ? Config::get('Textes') : $config;
39
        $this->Bdd = is_null($bdd) ? new Bdd() : $bdd;
41
        $this->Bdd = is_null($bdd) ? new Bdd() : $bdd;
40
    }
42
    }
41
    public function consulter($ressources, $parametres) {
43
    public function consulter($ressources, $parametres) {
42
        
44
        
43
        $this->parametres = $parametres;
45
        $this->parametres = $parametres;
44
        $this->ressources = $ressources;
46
        $this->ressources = $ressources;
45
        
47
        
46
        $this->definirValeurParDefautDesParametres();
48
        $this->definirValeurParDefautDesParametres();
47
        
49
        
48
        $this->format = (isset($this->parametres['retour.format']) && $this->parametres['retour.format'] != '') ?  $this->parametres['retour.format'] : $this->format;
50
        $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;
51
        $this->retour_mime = (isset($this->parametres['retour']) && $this->parametres['retour'] != '') ?  $this->parametres['retour'] : $this->retour_mime;
50
        $textes = $this->obtenirTextes();
52
        $resultat = $this->obtenirTextes();
51
        $this->nbreTextes = count($textes);
-
 
52
            $textes_formatees = $this->formaterRetourJson($textes);
-
 
53
            $resultat = $textes_formatees;
-
 
54
            $entete = $this->construireEntete();
53
        $entete = $this->construireEntete();
55
            return array('entete' => $entete, 'resultats' => $resultat);
54
        return array('entete' => $entete, 'resultats' => $resultat);
56
        
55
        
57
    }
56
    }
58
    private function construireEntete() {
57
    private function construireEntete() {
59
        $entete = array('masque' => '', 'depart' => 0, 'limite' => 100, 'total' => 0);
58
        $entete = array('masque' => '', 'depart' => 0, 'limite' => 100, 'total' => 0);
60
        
59
        
61
        $entete['masque'] = $this->recupererMasque();
60
        $entete['masque'] = $this->recupererMasque();
62
        $entete['depart'] = (int) $this->parametres['navigation.depart'];
61
        $entete['depart'] = (int) $this->parametres['navigation.depart'];
63
        $entete['limite'] = (int) $this->parametres['navigation.limite'];
62
        $entete['limite'] = (int) $this->parametres['navigation.limite'];
64
        $entete['total'] = $this->nbreTextes;
-
 
65
        
63
        
66
        return $entete;
64
        return $entete;
67
    }
65
    }
68
    
66
    
69
    private function recupererMasque() {
67
    private function recupererMasque() {
70
        $masqueEntete = '';
68
        $masqueEntete = '';
71
        foreach ($this->parametres as $param => $cle) {
69
        foreach ($this->parametres as $param => $cle) {
72
            if ($param == 'masque') {
70
            if ($param == 'masque') {
73
                $masqueEntete = 'masque='.$cle.',';
71
                $masqueEntete = 'masque='.$cle.',';
74
            } elseif (substr($param, 0, 7) == 'masque.') {
72
            } elseif (substr($param, 0, 7) == 'masque.') {
75
                $masqueEntete .= substr($param, 7).'='.$cle.',';
73
                $masqueEntete .= substr($param, 7).'='.$cle.',';
76
            }
74
            }
77
        }
75
        }
78
        $masqueEntete = rtrim($masqueEntete,',');
76
        $masqueEntete = rtrim($masqueEntete,',');
79
        return $masqueEntete;
77
        return $masqueEntete;
80
    }
78
    }
81
    
79
    
82
    private function definirValeurParDefautDesParametres() {
80
    private function definirValeurParDefautDesParametres() {
83
        if (isset($this->parametres['retour']) == false) {
81
        if (isset($this->parametres['retour']) == false) {
84
            $this->parametres['retour'] = self::MIME_JSON;
82
            $this->parametres['retour'] = self::MIME_JSON;
85
        }
83
        }
86
        if (isset($this->parametres['retour.format']) == false) {
84
        if (isset($this->parametres['retour.format']) == false) {
87
            $this->parametres['retour.format'] = 'min';
85
            $this->parametres['retour.format'] = 'min';
88
        }
86
        }
89
        if (isset($this->parametres['navigation.depart']) == false) {
87
        if (isset($this->parametres['navigation.depart']) == false) {
90
            $this->parametres['navigation.depart'] = 0;
88
            $this->parametres['navigation.depart'] = 0;
91
        }
89
        }
92
        if (isset($this->parametres['navigation.limite']) == false) {
90
        if (isset($this->parametres['navigation.limite']) == false) {
93
            $this->parametres['navigation.limite'] = 100;
91
            $this->parametres['navigation.limite'] = 100;
94
        }
92
        }
95
    }
93
    }
96
    
94
    
97
    private function obtenirTextes() {
95
    private function obtenirTextes() {
98
        $retour = "";
96
        $retour = "";
-
 
97
        if ($this->parametres['masque'] != "") {
-
 
98
            $this->parametres['masque'] = str_replace(" ", "_", $this->parametres['masque']);
99
        $json = file_gets_content($this->plantuseurl.$this->parametres['masque']);
99
            $json = file_get_contents($this->plantuseurl.$this->parametres['masque']);
100
        if ($json != false) {
100
            if ($json != false) {
101
            $tableau = json_decode($json);
101
                $tableau = json_decode($json, TRUE);
102
            if (isset($tableau['parse']['wikitext']['*'])) {
102
                if (isset($tableau['parse']['wikitext']['*'])) {
103
                $texte = $tableau['parse']['wikitext']['*'];
103
                    $texte = $tableau['parse']['wikitext']['*'];
104
                $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="+63), -2);
105
                
105
                    
-
 
106
                }
106
            }
107
            }
107
        }
108
        }
108
        return $retour;
109
        return $retour;
109
    }
110
    }
110
}
111
}