Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 899 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 899 Rev 952
1
<?php
1
<?php
2
 
2
 
3
/**
3
/**
4
* Description :
4
* Description :
5
* Classe Aide.php fournit des informations sur les services des projets : leur état (up ou down), une description, 
5
* Classe Aide.php fournit des informations sur les services des projets : leur état (up ou down), une description, 
6
* le lien vers le wikini
6
* le lien vers le wikini
7
* La ressource /projets donne la liste de tous les projets concernés par cette api.
7
* La ressource /projets donne la liste de tous les projets concernés par cette api.
8
* 
8
* 
9
* Encodage en entrée : utf8
9
* Encodage en entrée : utf8
10
* Encodage en sortie : utf8
10
* Encodage en sortie : utf8
11
* @package framework-v3
11
* @package framework-v3
12
* @author Jennifer Dhé <jennifer.dhe@tela-botanica.org>
12
* @author Jennifer Dhé <jennifer.dhe@tela-botanica.org>
13
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
13
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
14
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
14
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
15
* @version 1.0
15
* @version 1.0
16
* @copyright 1999-${year} Tela Botanica (accueil@tela-botanica.org)
16
* @copyright 1999-${year} Tela Botanica (accueil@tela-botanica.org)
17
*/
17
*/
18
		
18
		
19
class Aide extends RestService {
19
class Aide extends RestService {
20
 
20
 
21
	protected $retour_format = 'max';
21
	protected $retour_format = 'max';
22
	protected $url_wikini = 'http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=';
22
	protected $url_wikini = 'http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=';
23
	protected $url_liste_projet = '';
23
	protected $url_liste_projet = '';
24
	protected $table_retour = array();
24
	protected $table_retour = array();
25
	protected $corps_http = '';
25
	protected $corps_http = '';
26
	protected $entete_http = '';
26
	protected $entete_http = '';
27
	protected $projet = '';
27
	protected $projet = '';
28
		
28
		
29
 
29
 
30
	public function consulter($ressources, $parametres) {
30
	public function consulter($ressources, $parametres) {
31
		$this->url_liste_projet = Config::get('url_service_base').'commun/aide/projets';
31
		$this->url_liste_projet = Config::get('url_service_base').'commun/aide/projets';
32
		$this->projet = Config::get('nom_projet');
32
		$this->projet = Config::get('nom_projet');
33
		$this->traiterRessources($ressources);
33
		$this->traiterRessources($ressources);
34
		return $this->formerReponseHTTP($this->table_retour);
34
		return $this->formerReponseHTTP($this->table_retour);
35
	}
35
	}
36
	
36
	
37
//-----------------------------------------traiter reponse http-------------------------------------------------------------	
37
//-----------------------------------------traiter reponse http-------------------------------------------------------------	
38
	
38
	
39
	public function formerReponseHTTP($resultat_formate, $mime = 'application/json', $encodage= 'utf-8') {
39
	public function formerReponseHTTP($resultat_formate, $mime = 'application/json', $encodage= 'utf-8') {
40
		// Si aucune erreur n'a été établie (donc un tableau de résultat a bien ete renvoyé...)
40
		// Si aucune erreur n'a été établie (donc un tableau de résultat a bien ete renvoyé...)
41
		if ($this->corps_http == '' && $this->entete_http == '') {
41
		if ($this->corps_http == '' && $this->entete_http == '') {
42
			$this->entete_http = RestServeur::HTTP_CODE_OK;
42
			$this->entete_http = RestServeur::HTTP_CODE_OK;
43
			$this->corps_http  = $resultat_formate;
43
			$this->corps_http  = $resultat_formate;
44
		}
44
		}
45
		if ($this->entete_http != RestServeur::HTTP_CODE_OK) {
45
		if ($this->entete_http != RestServeur::HTTP_CODE_OK) {
46
			$mime = 'text/html';
46
			$mime = 'text/html';
47
		}
47
		}
48
		// Gestion du type de contenu
48
		// Gestion du type de contenu
49
		if (!is_null($mime) && !is_null($encodage)) {
49
		if (!is_null($mime) && !is_null($encodage)) {
50
			header("Content-Type: $mime; charset=$encodage");
50
			if(strpos($_SERVER['SCRIPT_NAME'], 'phpunit') === FALSE) header("Content-Type: $mime; charset=$encodage");
51
		} else if (!is_null($mime) && is_null($encodage)) {
51
		} else if (!is_null($mime) && is_null($encodage)) {
52
			header("Content-Type: $mime");
52
			if(strpos($_SERVER['SCRIPT_NAME'], 'phpunit') === FALSE) header("Content-Type: $mime");
53
		}
53
		}
54
		// Envoie de l'entête
54
		// Envoie de l'entête
55
		RestServeur::envoyerEnteteStatutHttp($this->entete_http);
55
		RestServeur::envoyerEnteteStatutHttp($this->entete_http);
56
		// Envoie du corps
56
		// Envoie du corps
57
		return $this->corps_http;
57
		return $this->corps_http;
58
	}
58
	}
59
		
59
		
60
	public function renvoyerErreur($e, $c) {
60
	public function renvoyerErreur($e, $c) {
61
		$this->entete_http = $e;
61
		$this->entete_http = $e;
62
		$this->corps_http  = $c;
62
		$this->corps_http  = $c;
63
	}
63
	}
64
	
64
	
65
	
65
	
66
//---------------------------------traiter Ressources------------------------------------------------------------------
66
//---------------------------------traiter Ressources------------------------------------------------------------------
67
 
67
 
68
	public function traiterRessources($ressources) {
68
	public function traiterRessources($ressources) {
69
		if (isset($ressources) && !empty($ressources)) {
69
		if (isset($ressources) && !empty($ressources)) {
70
			//-----------------------service /aide/projets------------------------------------
70
			//-----------------------service /aide/projets------------------------------------
71
			if ($ressources[0] == 'projets') {
71
			if ($ressources[0] == 'projets') {
72
				$this->afficherInfosTousProjets();
72
				$this->afficherInfosTousProjets();
73
			} else {
73
			} else {
74
				$r = 'Erreur dans votre requête </br> Ressources disponibles :
74
				$r = 'Erreur dans votre requête </br> Ressources disponibles :
75
					 <br/><li> /aide </li><li> /aide/projets </li>';
75
					 <br/><li> /aide </li><li> /aide/projets </li>';
76
				$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $r);
76
				$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $r);
77
			}
77
			}
78
		} else {
78
		} else {
79
			//-----------------------service /aide------------------------------------
79
			//-----------------------service /aide------------------------------------
80
			$this->afficherInfosProjet();
80
			$this->afficherInfosProjet();
81
			$this->formaterAideListeProjetEtRessources();
81
			$this->formaterAideListeProjetEtRessources();
82
		}
82
		}
83
 
83
 
84
	}
84
	}
85
 
85
 
86
	
86
	
87
	public function afficherInfosTousProjets() {
87
	public function afficherInfosTousProjets() {
88
		$req = 'show tables';
88
		$req = 'show tables';
89
		$tables = $this->getBdd()->recupererTous($req);
89
		$tables = $this->getBdd()->recupererTous($req);
90
		foreach ($tables as $table) {
90
		foreach ($tables as $table) {
91
			if (preg_match('/^(.+)_meta$/', array_shift($table), $projet)) {
91
			if (preg_match('/^(.+)_meta$/', array_shift($table), $projet)) {
92
				if (isset($projet[1])) {
92
				if (isset($projet[1])) {
93
					$projets[] = $projet[1];
93
					$projets[] = $projet[1];
94
				} else {
94
				} else {
95
					$e = "erreur.";
95
					$e = "erreur.";
96
					trigger_error($e, E_USER_ERROR);
96
					trigger_error($e, E_USER_ERROR);
97
				}
97
				}
98
			}
98
			}
99
		}
99
		}
100
		foreach ($projets as $projet) {
100
		foreach ($projets as $projet) {
101
			$this->table_retour[$projet] = $this->recupererInfoProjet($projet);
101
			$this->table_retour[$projet] = $this->recupererInfoProjet($projet);
102
		}
102
		}
103
	}	
103
	}	
104
 
104
 
105
	
105
	
106
	public function recupererInfoProjet($projet) {
106
	public function recupererInfoProjet($projet) {
107
		$res_return = null;
107
		$res_return = null;
108
		$projet = str_replace('_', '-', $projet);
108
		$projet = str_replace('_', '-', $projet);
109
		$url = Config::get('url_service_base').$projet.'/aide';
109
		$url = Config::get('url_service_base').$projet.'/aide';
110
		$intitule = 'PROJET '.strtoupper($projet);
110
		$intitule = 'PROJET '.strtoupper($projet);
111
		$res = $this->consulterHref($url);
111
		$res = $this->consulterHref($url);
112
		if ($res) {
112
		if ($res) {
113
			$res_return = $res->$intitule;
113
			$res_return = $res->$intitule;
114
		}
114
		}
115
		return $res_return;
115
		return $res_return;
116
	}
116
	}
117
	
117
	
118
	
118
	
119
	public function formaterAideListeProjetEtRessources() {
119
	public function formaterAideListeProjetEtRessources() {
120
		$projets = 'LISTE DES PROJETS';
120
		$projets = 'LISTE DES PROJETS';
121
		$services = 'Ressources';
121
		$services = 'Ressources';
122
		$this->table_retour[$projets]['liste des projets en place']['href'] = $this->url_liste_projet;
122
		$this->table_retour[$projets]['liste des projets en place']['href'] = $this->url_liste_projet;
123
		$this->table_retour[$projets]['liste de tous les projets']['wikini'] = $this->url_wikini.'EfloreIntegrationProjets';
123
		$this->table_retour[$projets]['liste de tous les projets']['wikini'] = $this->url_wikini.'EfloreIntegrationProjets';
124
		
124
		
125
	}
125
	}
126
	
126
	
127
	
127
	
128
 
128
 
129
	public function afficherInfosProjet() {
129
	public function afficherInfosProjet() {
130
		$res = array();
130
		$res = array();
131
		$ressources = $this->recupererListeDesRessources();
131
		$ressources = $this->recupererListeDesRessources();
132
		if (isset($ressources['href']) && $ressources['href'] != '') {
132
		if (isset($ressources['href']) && $ressources['href'] != '') {
133
			$this->table_retour['PROJET '.strtoupper($this->projet)]['projet.href'] = $ressources['href'];
133
			$this->table_retour['PROJET '.strtoupper($this->projet)]['projet.href'] = $ressources['href'];
134
		}
134
		}
135
		$etats = $this->verifierEtatDesRessources($ressources['WS']);
135
		$etats = $this->verifierEtatDesRessources($ressources['WS']);
136
		$this->table_retour['PROJET '.strtoupper($this->projet)]['projet.services'] = $etats['projet.services'];
136
		$this->table_retour['PROJET '.strtoupper($this->projet)]['projet.services'] = $etats['projet.services'];
137
	}
137
	}
138
	
138
	
139
 
139
 
140
	
140
	
141
	public function recupererListeDesRessources() {
141
	public function recupererListeDesRessources() {
142
		$resultat = null;
142
		$resultat = null;
143
		$req = 'SELECT url_projet, version, web_services FROM '.Config::get('bdd_table_meta').' ORDER BY CAST(version as DECIMAL)';
143
		$req = 'SELECT url_projet, version, web_services FROM '.Config::get('bdd_table_meta').' ORDER BY CAST(version as DECIMAL)';
144
		$res = $this->getBdd()->recuperer($req);
144
		$res = $this->getBdd()->recuperer($req);
145
		if ($res == '') {
145
		if ($res == '') {
146
			$r = 'La requête SQL formée comporte une erreur !!';
146
			$r = 'La requête SQL formée comporte une erreur !!';
147
			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $r);
147
			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $r);
148
			Debug::printr($req);
148
			Debug::printr($req);
149
		} elseif ($res) {
149
		} elseif ($res) {
150
			$resultat['WS'] = $res['web_services'];
150
			$resultat['WS'] = $res['web_services'];
151
			$resultat['href'] = $res['url_projet'];
151
			$resultat['href'] = $res['url_projet'];
152
		} else {
152
		} else {
153
			$d = 'Les données recherchées sont introuvables dans la version '.$res[0]['version'].'de la table de méta données';
153
			$d = 'Les données recherchées sont introuvables dans la version '.$res[0]['version'].'de la table de méta données';
154
			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $d);
154
			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $d);
155
			Debug::printr($req);
155
			Debug::printr($req);
156
		}
156
		}
157
		return $resultat;
157
		return $resultat;
158
	}
158
	}
159
	
159
	
160
 
160
 
161
	public function verifierEtatDesRessources($ressources) {
161
	public function verifierEtatDesRessources($ressources) {
162
		$ressources = explode(';', $ressources);
162
		$ressources = explode(';', $ressources);
163
		$etats = array();
163
		$etats = array();
164
		foreach ($ressources as $key => $ressApi) {
164
		foreach ($ressources as $key => $ressApi) {
165
			list($ress, $api) = explode(':', $ressApi);
165
			list($ress, $api) = explode(':', $ressApi);
166
			$this->translitererRess($ress);
166
			$this->translitererRess($ress);
167
			$url = Config::get('url_service').'/'.$ress;
167
			$url = Config::get('url_service').'/'.$ress;
168
			$wikini = $this->url_wikini.'EfloreApi'.str_replace('.', '', $api).$this->creerChaMot($ress);
168
			$wikini = $this->url_wikini.'EfloreApi'.str_replace('.', '', $api).$this->creerChaMot($ress);
169
			$url_service = Config::get('url_service').'/'.$ress;
169
			$url_service = Config::get('url_service').'/'.$ress;
170
			$etat = array(
170
			$etat = array(
171
					'service.nom' => $ress, 
171
					'service.nom' => $ress, 
172
					'service.href' => $url_service, 
172
					'service.href' => $url_service, 
173
					'service.wikini' => $wikini, 
173
					'service.wikini' => $wikini, 
174
					'service.etat' => 'UP');
174
					'service.etat' => 'UP');
175
 
175
 
176
			$res = $this->consulterHref($url);
176
			$res = $this->consulterHref($url);
177
			if (!$res) {
177
			if (!$res) {
178
				$etat['service.etat'] = 'DOWN';
178
				$etat['service.etat'] = 'DOWN';
179
			}
179
			}
180
			$etats['projet.services'][] = $etat;
180
			$etats['projet.services'][] = $etat;
181
		}
181
		}
182
		return $etats;
182
		return $etats;
183
	}
183
	}
184
	
184
	
185
	
185
	
186
	/**Permet de consulter une url et retourne le résultat ou une erreur
186
	/**Permet de consulter une url et retourne le résultat ou une erreur
187
	 * @param $url	 */
187
	 * @param $url	 */
188
	public function consulterHref($url) {
188
	public function consulterHref($url) {
189
		$res = $this->getRestClient()->consulter($url);
189
		$res = $this->getRestClient()->consulter($url);
190
		$entete = $this->getRestClient()->getReponseEntetes();
190
		$entete = $this->getRestClient()->getReponseEntetes();
191
		//Si le service meta-donnees fonctionne correctement, l'entete comprend la clé wrapper_data
191
		//Si le service meta-donnees fonctionne correctement, l'entete comprend la clé wrapper_data
192
		if (isset($entete['wrapper_data'])) {
192
		if (isset($entete['wrapper_data'])) {
193
			$res = json_decode($res);
193
			$res = json_decode($res);
194
			return $res;
194
			return $res;
195
		} else {
195
		} else {
196
			$u = 'L\'url <a href="'.$url.'">'.$url.'</a> lancée via RestClient renvoie une erreur';
196
			$u = 'L\'url <a href="'.$url.'">'.$url.'</a> lancée via RestClient renvoie une erreur';
197
			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $u);
197
			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $u);
198
		}
198
		}
199
	}
199
	}
200
		
200
		
201
	
201
	
202
	public function translitererRess(&$ress) {
202
	public function translitererRess(&$ress) {
203
		$ress = strtolower($ress);
203
		$ress = strtolower($ress);
204
		if ($ress == 'metadonnees') {
204
		if ($ress == 'metadonnees') {
205
			$ress = 'meta-donnees';
205
			$ress = 'meta-donnees';
206
		}
206
		}
207
	}
207
	}
208
	
208
	
209
	
209
	
210
	public function creerChaMot($nom) {
210
	public function creerChaMot($nom) {
211
		$chaine = new Chaine();
211
		$chaine = new Chaine();
212
		$nom = $chaine->supprimerAccents($nom);
212
		$nom = $chaine->supprimerAccents($nom);
213
		$nom_modif = '';
213
		$nom_modif = '';
214
		$nom_dec = preg_split('/-/', $nom);
214
		$nom_dec = preg_split('/-/', $nom);
215
		foreach ($nom_dec as $dec) {
215
		foreach ($nom_dec as $dec) {
216
			$nom_modif .= ucfirst($dec);
216
			$nom_modif .= ucfirst($dec);
217
		}
217
		}
218
        return trim($nom_modif);
218
        return trim($nom_modif);
219
    }
219
    }
220
	
220
	
221
}
221
}
222
 
222
 
223
?>
223
?>