New file |
0,0 → 1,233 |
<?php |
|
/** |
* Description : |
* Classe Aide.php fournit des informations sur les services des projets : leur état (up ou down), une description, |
* le lien vers le wikini |
* La ressource /projets donne la liste de tous les projets concernés par cette api. |
* |
* Encodage en entrée : utf8 |
* Encodage en sortie : utf8 |
* @package framework-v3 |
* @author Jennifer Dhé <jennifer.dhe@tela-botanica.org> |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt> |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt> |
* @version 1.0 |
* @copyright 1999-${year} Tela Botanica (accueil@tela-botanica.org) |
*/ |
|
class Aide extends RestService { |
|
protected $retour_format = 'max'; |
protected $url_wikini = 'http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki='; |
protected $url_liste_projet = ''; |
protected $table_retour = array(); |
protected $corps_http = ''; |
protected $entete_http = ''; |
protected $projet = ''; |
|
|
public function consulter($ressources, $parametres) { |
$this->url_liste_projet = Config::get('url_service_base').'commun/aide/projets'; |
$this->projet = Config::get('nom_projet'); |
$this->traiterRessources($ressources); |
return $this->formerReponseHTTP(json_encode($this->table_retour)); |
} |
|
//-----------------------------------------traiter reponse http------------------------------------------------------------- |
|
public function formerReponseHTTP($resultat_formate, $mime = 'application/json', $encodage= 'utf-8') { |
// Si aucune erreur n'a été établie (donc un tableau de résultat a bien ete renvoyé...) |
if ($this->corps_http == '' && $this->entete_http == '') { |
$this->entete_http = RestServeur::HTTP_CODE_OK; |
$this->corps_http = $resultat_formate; |
} |
if ($this->entete_http != RestServeur::HTTP_CODE_OK) { |
$mime = 'text/html'; |
} |
// Gestion du type de contenu |
if (!is_null($mime) && !is_null($encodage)) { |
header("Content-Type: $mime; charset=$encodage"); |
} else if (!is_null($mime) && is_null($encodage)) { |
header("Content-Type: $mime"); |
} |
// Envoie de l'entête |
RestServeur::envoyerEnteteStatutHttp($this->entete_http); |
// Envoie du corps |
echo $this->corps_http; |
} |
|
public function renvoyerErreur($e, $c) { |
$this->entete_http = $e; |
$this->corps_http = $c; |
} |
|
|
//---------------------------------traiter Ressources------------------------------------------------------------------ |
|
public function traiterRessources($ressources) { |
if (isset($ressources) && !empty($ressources)) { |
//-----------------------service /aide/projets------------------------------------ |
if ($ressources[0] == 'projets') { |
$this->afficherInfosTousProjets(); |
} else { |
$r = 'Erreur dans votre requête </br> Ressources disponibles : |
<br/><li> /aide </li><li> /aide/projets </li>'; |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $r); |
} |
} else { |
//-----------------------service /aide------------------------------------ |
$this->afficherInfosProjet(); |
$this->formaterAideListeProjetEtRessources(); |
} |
|
} |
|
|
public function afficherInfosTousProjets() { |
$req = 'show tables'; |
$tables = $this->getBdd()->recupererTous($req); |
foreach ($tables as $table) { |
if (preg_match('/^(.+)_meta$/', array_shift($table), $projet)) { |
if (isset($projet[1])) { |
$projets[] = $projet[1]; |
} else { |
$e = "erreur."; |
trigger_error($e, E_USER_ERROR); |
} |
} |
} |
foreach ($projets as $projet) { |
$this->table_retour[$projet] = $this->recupererInfoProjet($projet); |
} |
} |
|
|
public function recupererInfoProjet($projet) { |
$res_return = null; |
$projet = str_replace('_', '-', $projet); |
$url = Config::get('url_service_base').$projet.'/aide'; |
$intitule = 'PROJET '.strtoupper($projet); |
$res = $this->consulterHref($url); |
//$res = $this->rest_client->consulter($url); |
//$entete = $this->rest_client->getReponseEntetes(); |
if ($res) { |
$res_return = $res->$intitule; |
} |
/* if (isset($entete['wrapper_data']) && $res != '') { |
$res = json_decode($res); |
if (!is_object($res)) print_r($res); |
else $res_return = $res->$intitule; |
} else { |
$e = 'Le service aide du projet '.$projet.' ne fonctionne pas et renvoie comme entête : '.$entete.' et comme résultat : '.$res; |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e); |
}*/ |
return $res_return; |
} |
|
|
public function formaterAideListeProjetEtRessources() { |
$projets = 'LISTE DES PROJETS'; |
$services = 'Ressources'; |
$this->table_retour[$projets]['liste des projets en place']['href'] = $this->url_liste_projet; |
$this->table_retour[$projets]['liste de tous les projets']['wikini'] = $this->url_wikini.'EfloreIntegrationProjets'; |
|
} |
|
|
|
public function afficherInfosProjet() { |
$res = array(); |
$ressources = $this->recupererListeDesRessources(); |
if (isset($ressources['href']) && $ressources['href'] != '') { |
$this->table_retour['PROJET '.strtoupper($this->projet)]['projet.href'] = $ressources['href']; |
} |
$etats = $this->verifierEtatDesRessources($ressources['WS']); |
$this->table_retour['PROJET '.strtoupper($this->projet)]['projet.services'] = $etats['projet.services']; |
} |
|
|
|
public function recupererListeDesRessources() { |
$resultat = null; |
$req = 'SELECT url_projet, version, web_services FROM '.Config::get('bdd_table_meta').' ORDER BY CAST(version as DECIMAL)'; |
$res = $this->getBdd()->recuperer($req); |
if ($res == '') { |
$r = 'La requête SQL formée comporte une erreur !!'; |
$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $r); |
Debug::printr($req); |
} elseif ($res) { |
$resultat['WS'] = $res['web_services']; |
$resultat['href'] = $res['url_projet']; |
} else { |
$d = 'Les données recherchées sont introuvables dans la version '.$res[0]['version'].'de la table de méta données'; |
$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $d); |
Debug::printr($req); |
} |
return $resultat; |
} |
|
|
public function verifierEtatDesRessources($ressources) { |
$ressources = explode(';', $ressources); |
$etats = array(); |
foreach ($ressources as $key => $ressApi) { |
list($ress, $api) = explode(':', $ressApi); |
$this->translitererRess($ress); |
$url = Config::get('url_service').'/'.$ress; |
$wikini = $this->url_wikini.'EfloreApi'.$api.$this->creerChaMot($ress); |
$url_service = Config::get('url_service').'/'.$ress; |
$etat = array( |
'service.nom' => $ress, |
'service.href' => $url_service, |
'service.wikini' => $wikini, |
'service.etat' => 'UP'); |
|
$res = $this->consulterHref($url); |
if (!$res) { |
$etat['service.etat'] = 'DOWN ('.$reponse[0].')'; |
} |
$etats['projet.services'][] = $etat; |
} |
return $etats; |
} |
|
|
/**Permet de consulter une url et retourne le résultat ou une erreur |
* @param $url */ |
public function consulterHref($url) { |
$res = $this->getRestClient()->consulter($url); |
$entete = $this->getRestClient()->getReponseEntetes(); |
//Si le service meta-donnees fonctionne correctement, l'entete comprend la clé wrapper_data |
if (isset($entete['wrapper_data'])) { |
$res = json_decode($res); |
return $res; |
} else { |
$u = 'L\'url <a href="'.$url.'">'.$url.'</a> lancée via RestClient renvoie une erreur'; |
$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $u); |
} |
} |
|
|
public function translitererRess(&$ress) { |
$ress = strtolower($ress); |
if ($ress == 'metadonnees') { |
$ress = 'meta-donnees'; |
} |
} |
|
|
public function creerChaMot($nom) { |
$chaine = new Chaine(); |
$nom = $chaine->supprimerAccents($nom); |
$nom_modif = ''; |
$nom_dec = preg_split('/-/', $nom); |
foreach ($nom_dec as $dec) { |
$nom_modif .= ucfirst($dec); |
} |
return trim($nom_modif); |
} |
|
} |
|
?> |