Subversion Repositories Applications.projet

Compare Revisions

Ignore whitespace Rev 430 → Rev 431

/trunk/jrest/services/ProjetSyndication.php
New file
0,0 → 1,342
<?php
/**
* Service fournissant des informations concernant PROJET au format RSS1, RSS2 ou ATOM.
* Encodage en entrée : utf8
* Encodage en sortie : utf8
*
* @author Grégoire Duché <gregoire@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 $Id: CoelSyndication.php 381 2010-05-17 17:10:37Z jpm $
* @copyright 2009
*/
 
class ProjetSyndication extends ProjetService {
private $format = null;
private $service = null;
private $squelette = null;
private $squelette_dossier = null;
private $squelette_diff = null;
private $flux = array();
/**
* Méthode appelée avec une requête de type GET.
*/
public function getElement($param = array()) {
// Initialisation des variables
$info = array();
$contenu = '';
// Pré traitement des paramètres
$pour_bdd = false;
$p = $this->traiterParametresUrl(array('service', 'format'), $param, $pour_bdd);
// Récupération de la liste des flux
$this->chargerListeDesFlux();
// Chargement du bon type de service demandé
if (isset($p['service'])) {
$this->service = strtolower($p['service']);
$methode = $this->getNomMethodeService();
if (method_exists($this, $methode)) {
if ($this->service != 'liste_des_flux') {
if (isset($p['format']) && preg_match('/^(?:rss1|rss2|atom)$/i', $p['format'])) {
// Multiplication par deux de la limite car nous récupérons deux lignes par item
$this->limit = $this->limit*2;
// Mise en minuscule de l'indication du format
$this->format = strtolower($p['format']);
// Définition du fichier squelette demandé
$this->squelette_dossier = dirname(__FILE__).DIRECTORY_SEPARATOR.'squelettes'.DIRECTORY_SEPARATOR;
$this->squelette = $this->squelette_dossier.$this->format.'.tpl.xml';
$this->squelette_diff = $this->squelette_dossier.'diff.tpl.html';
} else {
$this->format = '';
$this->messages[] = "Le service Projet Syndication nécessite d'indiquer en second paramètre le format : rss1, rss2 ou atom.";
}
}
// Récupération du contenu à renvoyer
$contenu = $this->$methode();
} else {
$this->messages[] = "Le type d'information demandé '$this->service' n'est pas disponible.";
}
} else {
$this->messages[] = "Le service Projet Syndication nécessite d'indiquer en premier paramètre le type d'information demandé.";
}
// Envoie sur la sortie standard
$encodage = 'utf-8';
$mime = $this->getTypeMime();
$formatage_json = $this->getFormatageJson();
$this->envoyer($contenu, $mime, $encodage, $formatage_json);
}
private function getUrlServiceBase() {
$url_service = $this->config['coel']['urlBaseJrest'].'CoelSyndication/'.$this->service.'/'.$this->format;
return $url_service;
}
private function getNomMethodeService() {
$methode = '';
$service_formate = str_replace(' ', '', ucwords(implode(' ', explode('_', $this->service))));
$methode = 'getService'.$service_formate;
return $methode;
}
private function getTypeMime() {
$mime = '';
switch ($this->format) {
case 'atom' :
$mime = 'application/atom+xml';
break;
case 'rss1' :
case 'rss2' :
$mime = 'application/rss+xml';
break;
default:
$mime = 'text/html';
}
return $mime;
}
private function getFormatageJson() {
$json = false;
switch ($this->service) {
case 'liste_des_flux' :
$json = true;
break;
default:
$json = false;
}
return $json;
}
private function getFlux($nom) {
$nom = strtolower($nom);
return isset($this->flux[$nom]) ? $this->flux[$nom] : array();
}
 
private function setFlux($nom, $titre, $description) {
$url_base = $this->config['appli']['urlBaseJrest'].'ProjetSyndication/';
$formats = array('atom', 'rss2', 'rss1');
$flux = array();
foreach ($formats as $format) {
$url = $url_base.$nom.'/'.$format;
$flux[$format] = $url;
}
$this->flux[$nom] = array('titre' => $titre, 'description' => $description, 'urls' => $flux);
}
private function chargerListeDesFlux() {
$this->setFlux('actifs', 'Flux de syndication des projets les plus actifs',
'Ce flux fournit des informations sur les projets les plus actifs de l\'espace projet');
$this->setFlux('derniers_messages', 'Flux de syndication des derniers messages',
'Ce flux fournit des informations sur les derniers messages de l\'espace projet');
}
private function getServiceListeDesFlux() {
return $this->flux;
}
private function getServiceActifs() {
// Construction de la requête
$requete_projets = ' select p_id, p_titre as titre, p_resume as description, pl_id_liste, plle_id_liste, p_avoir_document, ps_nombre_inscrit, ps_pourcent,'.
' p_wikini, ps_doc_derniere_semaine, p_avoir_document, ps_nombre_inscrit_derniere_semaine, '.
' ps_nombre_membre_yahoo, ps_msg_derniere_semaine, ps_modifwiki_derniere_semaine, p_en_dormance from'.
' projet left join projet_lien_liste on p_id=pl_id_projet'.
' left join projet_lien_liste_externe on p_id=plle_id_projet'.
' left join projet_statistiques on p_id=ps_ce_projet and ps_dernier=1'.
' WHERE p_en_dormance = 0'.
' group by p_id order by ps_pourcent desc';
$elements = $this->executerRequete($requete_projets);
// Création du contenu
$contenu = $this->executerService('cpr_nom', $elements);
return $contenu;
}
 
private function getServiceDerniersMessages() {
/*include_once("lib/ezmlmAccessObject.class.php");
$xml_parser = &new ezmlmAccessObject('list_info', 'tela-botanica.org',
'determination_plantes', 'fr', 'http://localhost') ;
$xml_parser->load();
ob_start();
$resultat = $xml_parser->parse() ;
$calendrier = ob_get_contents();
ob_end_clean();*/
}
private function executerRequete($requete) {
try {
$infos = $this->bdd->query($requete)->fetchAll(PDO::FETCH_ASSOC);
if ($infos === false) {
$this->messages[] = "La requête a retourné aucun résultat.";
}
} catch (PDOException $e) {
$this->messages[] = sprintf($this->getTxt('sql_erreur'), $e->getFile(), $e->getLine(), $e->getMessage());
}
return $infos;
}
private function executerService($champ_titre, $elements) {
// Prétraitement des données
$donnees = $this->construireDonneesCommunesAuFlux($elements);
foreach ($elements as $element) {
//$xml = $this->getXmlHisto($element);
//$enrg = $this->getTableauDepuisXmlHisto($xml);
//$diff = $this->getDiffInfos($element);
//$diff['differences'] = $this->getDiff($element);
//$diff_html = (!is_null($diff['differences'])) ? Coel::traiterSquelettePhp($this->squelette_diff, $diff) : '';
$item = $this->construireDonneesCommunesAuxItems($element);
$item['titre'] = $this->creerTitre($champ_titre, $element, $enrg);
$item['guid'] = sprintf($this->config['appli']['guid'], 'projet', $element['p_id']);
$item['lien'] = $this->config['appli']['url_base_projet'].'?id_projet='.urlencode($element['p_id']);
$item['description'] = "<h4>".$element['ps_nombre_inscrit'].' inscrits - '.$element['ps_msg_derniere_semaine'].' nouveaux messages</h4>';
$item['description'] .= '<p>'.$this->creerDescription($element, $enrg).'</p>';
$item['description'] .= $diff_html;
$item['description'] = $this->nettoyerTexte($item['description']);
$item['description_encodee'] = htmlspecialchars($item['description']);
$donnees['items'][] = $item;
}
// Création du contenu à partir d'un template PHP
$contenu = ProjetService::traiterSquelettePhp($this->squelette, $donnees);
return $contenu;
}
 
private function creerTitre($champ, $element, $enrg) {
$titre = '';
if (isset($element['titre'])) {
$titre = $element['titre'];
} else if (isset($element[$champ])) {
$titre = $element[$champ];
} else if (isset($enrg[$champ])) {
$titre = $enrg[$champ];
}
$titre = $this->nettoyerTexte($titre);
return $titre;
}
private function creerDescription($element, $enrg) {
$description = '';
if (isset($element['description'])) {
$description = strip_tags($element['description']);
}
return $description;
}
private function nettoyerNomChamps($infos) {
$sortie = array();
foreach ($infos as $champ => $valeur) {
if (preg_match('/^__(.+)$/', $champ, $match)) {
$sortie[$match[1]] = $valeur;
} else {
$sortie[$champ] = $valeur;
}
}
return $sortie;
}
private function traiterInfosPrecedentes($infos_courantes, $infos_precedentes) {
$infos_precedentes_traitees = array();
foreach ($infos_precedentes as $champ => $valeur) {
if ($champ == 'cmhl_date_modification') {
$infos_precedentes_traitees['date_prec'] = $valeur;
} else if ($champ == 'cmhl_enregistrement') {
$infos_precedentes_traitees['enrg_prec'] = $valeur;
} else if (preg_match('/^__(.+)$/', $champ, $match)) {
$infos_precedentes_traitees[$match[1].'_prec'] = $valeur;
}
}
$sortie = array_merge($infos_courantes, $infos_precedentes_traitees);
return $sortie;
}
private function nettoyerTexte($txt) {
$txt = preg_replace('/&(?!amp;)/i', '&amp;', $txt, -1);
return $txt;
}
private function getMessageModif($item) {
$message = $item['etat'].' le '.$item['date_maj_simple'].' par '.$item['modifier_par'].' depuis l\'IP '.$item['ip'];
return $message;
}
private function construireDonneesCommunesAuxItems($info) {
$item = array();
$date_modification_timestamp = strtotime($info['cmhl_date_modification']);
$item['date_maj_simple'] = strftime('%A %d %B %Y à %H:%M', $date_modification_timestamp);
$item['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
$item['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
$item['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
$item['guid'] = $info['cmhl_id_historique_ligne'];
$item['cle'] = $info['cmhl_cle_ligne'];
$item['ip'] = $info['cmhl_ip'];
$item['modifier_par'] = $info['modifier_par'];
$item['etat'] = isset($info['cmhl_ce_etat']) ? $this->getTexteEtat($info['cmhl_ce_etat']) : '';
return $item;
}
private function getTexteEtat($code) {
$etat = '';
switch ($code) {
case '1' :
$etat = 'Ajouté';
break;
case '2' :
$etat = 'Modifié';
break;
case '3' :
$etat = 'Supprimé';
break;
default :
$etat = '!Problème!';
$e = "Le champ cmhl_ce_etat possède une valeur innatendue : $code";
$this->messages[] = $e;
}
return $etat;
}
private function construireDonneesCommunesAuFlux($infos) {
$donnees = $this->getFlux($this->service);
$donnees['guid'] = $this->getUrlServiceBase();
$donnees['lien_service'] = $this->creerUrlService();
$donnees['lien_projet'] = $this->config['appli']['url_base_projet'];
$donnees['editeur'] = $this->config['coel']['editeur'];
$derniere_info_en_date = reset($infos);
$date_modification_timestamp = strtotime($derniere_info_en_date['cmhl_date_modification']);
$donnees['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
$donnees['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
$donnees['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
$donnees['annee_courante'] = date('Y');
$donnees['generateur'] = 'COEL - Jrest';
preg_match('/([0-9]+)/', '$Revision: 381 $', $match);
$donnees['generateur_version'] = $match[1];
return $donnees;
}
private function creerUrlService() {
$url_service = $this->getUrlServiceBase();
if (isset($this->start) || isset($this->limit)) {
$arguments = array();
if (isset($this->start) && isset($_GET['start'])) {
$arguments[] = 'start='.$this->start;
}
if (isset($this->limit) && isset($_GET['limit'])) {
$arguments[] = 'limit='.($this->limit/2);
}
if (count($arguments) > 0) {
$url_service .= '?'.implode('&', $arguments);
}
}
return $url_service;
}
}