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 = '+' ;
|
|
|
33 |
|
|
|
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));
|
|
|
108 |
$val = $this->consulterHref($url);
|
|
|
109 |
$ontologie['niveau.code'] = $valeur;
|
|
|
110 |
$ontologie['niveau.libelle'] = $val->nom;
|
|
|
111 |
$ontologie['niveau.href'] = $url;
|
|
|
112 |
return $ontologie;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
protected function recupererOntologiesNiveauSyn($niveau){
|
|
|
116 |
$ontologie = array();
|
|
|
117 |
$synonymes = explode(' ',$niveau[1]);
|
|
|
118 |
$libelle = array();
|
|
|
119 |
$detail = array();
|
|
|
120 |
foreach ($synonymes as $syn) {
|
|
|
121 |
if ($syn == '=') {
|
|
|
122 |
$syn = 'equi';
|
|
|
123 |
} elseif ($syn == '= ?') {
|
|
|
124 |
$syn = 'prob';
|
|
|
125 |
}
|
|
|
126 |
if ($syn != '' && $syn != '?') {
|
|
|
127 |
$detail[] = $this->recupererOntologiesNiveau($syn, 'syn');
|
|
|
128 |
$dernier = end($detail);
|
|
|
129 |
$libelle[] = $dernier['niveau.libelle'].' ';
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
$ontologie['niveau.code'] = $niveau[0];
|
|
|
133 |
$ontologie['niveau.libelle'] = 'synonyme '.implode(' - ',$libelle);
|
|
|
134 |
if ($niveau[2]) {
|
|
|
135 |
$ontologie['niveau.libelle'] .= ' '.$niveau[2];
|
|
|
136 |
}
|
|
|
137 |
$ontologie['niveau.detail'] = $detail;
|
|
|
138 |
return $ontologie;
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
protected function traiterOntologieNiveau($niveau) {
|
|
|
142 |
$ontologie = array();
|
|
|
143 |
|
|
|
144 |
if ( preg_match('/syn {0,1}(['.implode('',$this->syn).']*)( *\[.+\])*/',$niveau,$retour)) {
|
|
|
145 |
|
|
|
146 |
$ontologie = $this->recupererOntologiesNiveauSyn($retour);
|
|
|
147 |
|
|
|
148 |
} else {
|
|
|
149 |
$ontologie = $this->recupererOntologiesNiveau($niveau, 'UP');
|
|
|
150 |
}
|
|
|
151 |
return $ontologie;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
//+--------------------------FONCTIONS D'ASSEMBLAGE DE LA REQUETE-------------------------------------------+
|
|
|
155 |
|
|
|
156 |
protected function assemblerLaRequete() {
|
|
|
157 |
$requete = ' SELECT '.$this->champs_recherches.' FROM '.$this->table.' '
|
|
|
158 |
.$this->retournerRequeteCondition().' '.$this->delimiterResultatsRequete();
|
|
|
159 |
|
|
|
160 |
return $requete;
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
protected function calculerTotalResultat() {
|
|
|
164 |
$requete = 'SELECT count(*) as nombre FROM '.$this->table.' '.$this->retournerRequeteCondition();
|
|
|
165 |
|
|
|
166 |
$res = $this->getBdd()->recuperer($requete);
|
|
|
167 |
if ($res) {
|
|
|
168 |
$this->total_resultat = $res['nombre'];
|
|
|
169 |
} else {
|
|
|
170 |
$this->total_resultat = 0;
|
|
|
171 |
$e = 'Données introuvables dans la base';
|
|
|
172 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $e);
|
|
|
173 |
}
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
protected function delimiterResultatsRequete() {
|
|
|
177 |
$this->calculerTotalResultat();
|
|
|
178 |
$requete_limite = '';
|
|
|
179 |
if (($this->limite_requete['depart'] <= $this->total_resultat) ){
|
|
|
180 |
if (($this->limite_requete['limite'] + $this->limite_requete['depart'] )
|
|
|
181 |
< $this->total_resultat ){
|
|
|
182 |
$requete_limite = 'LIMIT '.$this->limite_requete['depart'].', '
|
|
|
183 |
.$this->limite_requete['limite'];
|
|
|
184 |
}
|
|
|
185 |
} else {
|
|
|
186 |
$e = "Erreur : la valeur pour le paramètre navigation.départ est supérieure".
|
|
|
187 |
" au nombre total de résultats.";
|
|
|
188 |
throw new Exception($e, RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
|
|
|
189 |
}
|
|
|
190 |
return $requete_limite;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
|
|
|
194 |
|
|
|
195 |
protected function retournerRequeteCondition() {
|
|
|
196 |
$condition = '';
|
|
|
197 |
if (empty($this->requete_condition) == false) {
|
|
|
198 |
$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
return $condition;
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
}
|
|
|
205 |
?>
|