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