584 |
mathilde |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
*
|
|
|
4 |
* fonctions et paramètres communs aux traitement des syntaxons
|
|
|
5 |
* - traitement de l'ontologie
|
|
|
6 |
* - fonction utiles pour les paramètres de configuration entre autres
|
|
|
7 |
* - traitement des tables et de leur version
|
|
|
8 |
* - traitement de la requête
|
|
|
9 |
*
|
|
|
10 |
* @package eflore-projets
|
|
|
11 |
* @author mathilde Salthun-Lassalle
|
|
|
12 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
13 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
14 |
* @version 1.0
|
|
|
15 |
* @copyright 1999-2012 Tela Botanica (accueil@tela-botanica.org)
|
|
|
16 |
*/
|
|
|
17 |
|
|
|
18 |
class SyntaxonsCommun extends Commun {
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
protected $table = "";
|
|
|
22 |
protected $format_reponse = 'syntaxons';
|
|
|
23 |
protected $serviceNom = 'syntaxons';
|
|
|
24 |
protected $Bdd;
|
|
|
25 |
protected $syn;
|
|
|
26 |
protected $niv;
|
|
|
27 |
protected $limite_requete = array('depart' => 0, 'limite' => 100);
|
|
|
28 |
protected $requete_condition = '';
|
|
|
29 |
protected $total_resultat;
|
|
|
30 |
protected $champs_recherches = ' * ';
|
|
|
31 |
protected $table_version;
|
|
|
32 |
protected $version_projet = '+' ;
|
901 |
raphael |
33 |
protected $cache = array();
|
584 |
mathilde |
34 |
|
|
|
35 |
|
|
|
36 |
public function __construct(Conteneur $conteneur) {
|
|
|
37 |
$this->Bdd = $conteneur->getBdd();
|
|
|
38 |
$this->syn = $this->getParametreTableauSpe('synonymes');
|
|
|
39 |
$this->niv = $this->getParametreTableauSpe('niveaux');
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
//+---------------------------------------------------outils------------------------------------------------//
|
|
|
47 |
protected function getParametreTableauSpe($cle) {
|
|
|
48 |
$tableau = array();
|
|
|
49 |
$parametre = Config::get($cle);
|
|
|
50 |
if (empty($parametre) === false) {
|
|
|
51 |
$tableauPartiel = explode(',', $parametre);
|
|
|
52 |
$tableauPartiel = array_map('trim', $tableauPartiel);
|
|
|
53 |
foreach ($tableauPartiel as $champ) {
|
|
|
54 |
$tableau[] = trim($champ,"'");// spécificité pour le = ? des synonymes
|
|
|
55 |
}
|
|
|
56 |
}
|
|
|
57 |
return $tableau;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
//la fonction php array_filters ne marche pas sur le serveur
|
|
|
61 |
protected function enleverValeursVides($tableau) {
|
|
|
62 |
$tab = array();
|
|
|
63 |
foreach ($tableau as $cle => $valeur) {
|
|
|
64 |
if ($valeur != '' && is_int($cle)) {
|
|
|
65 |
$tab[] = $valeur;
|
|
|
66 |
} elseif ($valeur != '' && is_string($cle)) {
|
|
|
67 |
$tab[$cle] = $valeur;
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
return $tab;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
//+++------------------------------traitement des versions----------------------------------------++
|
|
|
74 |
|
|
|
75 |
protected function traiterVersion($valeur) {
|
|
|
76 |
if (preg_match('/^[0-9]+(?:[._][0-9]+)*$/', $valeur) || $valeur == '+' || $valeur == '*') {
|
|
|
77 |
$this->version_projet = $valeur;
|
|
|
78 |
} else {
|
|
|
79 |
$e = "Erreur : La version est inconnue.";
|
|
|
80 |
throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
protected function definirTables() {
|
|
|
85 |
$table_num_version = $this->recupererVersionDisponible();
|
|
|
86 |
$prefixe_table = config::get('bdd_table');
|
|
|
87 |
if ( in_array($this->version_projet,$table_num_version) ) {
|
|
|
88 |
$this->table_version[] = $prefixe_table.'_v'.$this->version_projet;
|
|
|
89 |
} elseif ($this->version_projet == '+') {
|
|
|
90 |
$derniere_version = $table_num_version[count($table_num_version) - 1];
|
|
|
91 |
$this->table_version[] = $prefixe_table.'_v'.str_replace('.', '_', $derniere_version);
|
|
|
92 |
} elseif ($this->version_projet == '*') {
|
|
|
93 |
foreach ($table_num_version as $num_version) {
|
|
|
94 |
$this->table_version[] = $prefixe_table.'_v'.str_replace('.', '_', $num_version);
|
|
|
95 |
}
|
|
|
96 |
} else {
|
|
|
97 |
$e = "Erreur : La version est inconnue.";
|
|
|
98 |
throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
|
|
|
99 |
}
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
//+------------------------------------ontologies----------------------------------------------------+
|
|
|
103 |
|
|
|
104 |
protected function recupererOntologiesNiveau($valeur, $code){
|
|
|
105 |
$ontologie = array();
|
|
|
106 |
$url = Config::get('url_service_base').Config::get('nom_projet').
|
|
|
107 |
'/ontologies/'.$code.':'.urlencode(urlencode($valeur));
|
901 |
raphael |
108 |
if(array_key_exists($url, $this->cache)) {
|
|
|
109 |
$val = $this->cache[$url];
|
|
|
110 |
} else {
|
|
|
111 |
$val = $this->consulterHref($url);
|
|
|
112 |
$this->cache[$url] = $val;
|
|
|
113 |
}
|
584 |
mathilde |
114 |
$ontologie['niveau.code'] = $valeur;
|
|
|
115 |
$ontologie['niveau.libelle'] = $val->nom;
|
|
|
116 |
$ontologie['niveau.href'] = $url;
|
|
|
117 |
return $ontologie;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
protected function recupererOntologiesNiveauSyn($niveau){
|
|
|
121 |
$ontologie = array();
|
|
|
122 |
$synonymes = explode(' ',$niveau[1]);
|
|
|
123 |
$libelle = array();
|
|
|
124 |
$detail = array();
|
|
|
125 |
foreach ($synonymes as $syn) {
|
|
|
126 |
if ($syn == '=') {
|
|
|
127 |
$syn = 'equi';
|
|
|
128 |
} elseif ($syn == '= ?') {
|
|
|
129 |
$syn = 'prob';
|
|
|
130 |
}
|
|
|
131 |
if ($syn != '' && $syn != '?') {
|
|
|
132 |
$detail[] = $this->recupererOntologiesNiveau($syn, 'syn');
|
|
|
133 |
$dernier = end($detail);
|
|
|
134 |
$libelle[] = $dernier['niveau.libelle'].' ';
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
$ontologie['niveau.code'] = $niveau[0];
|
|
|
138 |
$ontologie['niveau.libelle'] = 'synonyme '.implode(' - ',$libelle);
|
|
|
139 |
if ($niveau[2]) {
|
|
|
140 |
$ontologie['niveau.libelle'] .= ' '.$niveau[2];
|
|
|
141 |
}
|
|
|
142 |
$ontologie['niveau.detail'] = $detail;
|
|
|
143 |
return $ontologie;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
protected function traiterOntologieNiveau($niveau) {
|
|
|
147 |
$ontologie = array();
|
|
|
148 |
|
|
|
149 |
if ( preg_match('/syn {0,1}(['.implode('',$this->syn).']*)( *\[.+\])*/',$niveau,$retour)) {
|
|
|
150 |
|
|
|
151 |
$ontologie = $this->recupererOntologiesNiveauSyn($retour);
|
|
|
152 |
|
|
|
153 |
} else {
|
|
|
154 |
$ontologie = $this->recupererOntologiesNiveau($niveau, 'UP');
|
|
|
155 |
}
|
|
|
156 |
return $ontologie;
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
//+--------------------------FONCTIONS D'ASSEMBLAGE DE LA REQUETE-------------------------------------------+
|
|
|
160 |
|
|
|
161 |
protected function assemblerLaRequete() {
|
|
|
162 |
$requete = ' SELECT '.$this->champs_recherches.' FROM '.$this->table.' '
|
|
|
163 |
.$this->retournerRequeteCondition().' '.$this->delimiterResultatsRequete();
|
|
|
164 |
|
|
|
165 |
return $requete;
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
protected function calculerTotalResultat() {
|
|
|
169 |
$requete = 'SELECT count(*) as nombre FROM '.$this->table.' '.$this->retournerRequeteCondition();
|
|
|
170 |
|
|
|
171 |
$res = $this->getBdd()->recuperer($requete);
|
|
|
172 |
if ($res) {
|
|
|
173 |
$this->total_resultat = $res['nombre'];
|
|
|
174 |
} else {
|
|
|
175 |
$this->total_resultat = 0;
|
|
|
176 |
$e = 'Données introuvables dans la base';
|
|
|
177 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $e);
|
|
|
178 |
}
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
protected function delimiterResultatsRequete() {
|
|
|
182 |
$this->calculerTotalResultat();
|
|
|
183 |
$requete_limite = '';
|
|
|
184 |
if (($this->limite_requete['depart'] <= $this->total_resultat) ){
|
|
|
185 |
if (($this->limite_requete['limite'] + $this->limite_requete['depart'] )
|
|
|
186 |
< $this->total_resultat ){
|
|
|
187 |
$requete_limite = 'LIMIT '.$this->limite_requete['depart'].', '
|
|
|
188 |
.$this->limite_requete['limite'];
|
|
|
189 |
}
|
|
|
190 |
} else {
|
|
|
191 |
$e = "Erreur : la valeur pour le paramètre navigation.départ est supérieure".
|
|
|
192 |
" au nombre total de résultats.";
|
|
|
193 |
throw new Exception($e, RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
|
|
|
194 |
}
|
|
|
195 |
return $requete_limite;
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
|
|
|
199 |
|
|
|
200 |
protected function retournerRequeteCondition() {
|
|
|
201 |
$condition = '';
|
|
|
202 |
if (empty($this->requete_condition) == false) {
|
|
|
203 |
$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
return $condition;
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
}
|
|
|
210 |
?>
|