Subversion Repositories eFlore/Projets.eflore-projets

Compare Revisions

Ignore whitespace Rev 273 → Rev 274

/trunk/services/modules/0.1/Projets.php
122,10 → 122,12
return null;
}
 
$cheminBiblio = Config::get('chemin_bibliotheque');
$chemins = array();
$chemins[] = $this->cheminCourrant.$this->projetNom.DS;
$chemins[] = $this->cheminCourrant.'commun'.DS;
$chemins[] = Config::get('chemin_bibliotheque');
$chemins[] = $cheminBiblio;
$chemins[] = $cheminBiblio.'robots'.DS;
 
foreach ($chemins as $chemin) {
$chemin = $chemin.$classe.'.php';
/trunk/services/modules/0.1/wikipedia/Textes.php
New file
0,0 → 1,79
<?php
// declare(encoding='UTF-8');
/**
* Classe implémentant l'API d'eFlore Textes pour le projet WIKIPEDIA.
*
* @see http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=EfloreApi01Textes
*
* @package eFlore/services
* @author Jean-Pascal MILCENT <jpm@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-2012 Tela Botanica (accueil@tela-botanica.org)
*/
class Textes {
 
private $parametres = array();
private $ressources = array();
 
public function consulter($ressources, $parametres) {
$this->parametres = $parametres;
$this->ressources = $ressources;
 
$this->analyserRessources();
$resultat = $this->executerSousService();
 
return $resultat;
}
 
private function analyserRessources() {
$nbreRessources = count($this->ressources);
if ($nbreRessources == 0) {
$message = "A implémenter : listes des pages trouvées";
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
throw new Exception($message, $code);
} else if ($nbreRessources == 1) {
if ($this->etreRessourceIdentifiants(0)) {
$this->sousService = 'Page';
} else {
$message = "La ressource n°1 '{$this->ressources[0]} indiquée n'est pas valable.";
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
throw new Exception($message, $code);
}
} else if ($nbreRessources > 1) {
$message = "Les ressources indiquées ne sont pas valables.";
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
throw new Exception($message, $code);
}
}
 
private function etreRessourceIdentifiants($position) {
$ok = true;
if (isset($this->ressources[$position])) {
$ids = $this->ressources[$position];
$supraSpPattern = "[A-Z][a-z]+";
$spPattern = "{$supraSpPattern}_[a-z]+";
$pagePattern = "/^(?:$supraSpPattern|$spPattern)$/i";
$ok = preg_match($pagePattern, $ids) ? true : false;
}
return $ok;
}
 
private function executerSousService() {
if (isset($this->sousService)) {
$classe = $this->sousService.'Textes';
require_once dirname(__FILE__).DS.'textes'.DS.$classe.'.php';
$sousService = new $classe($this->ressources, $this->parametres, new Conteneur());
$resultat = $sousService->consulter();
} else {
$message = "L'analyse des ressources n'a pu aboutir à déterminer le sous service à exécuter.";
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
throw new Exception($message, $code);
}
return $resultat;
}
 
 
}
?>
/trunk/services/modules/0.1/wikipedia/textes/PageTextes.php
New file
0,0 → 1,132
<?php
class PageTextes {
 
private $parametres = array();
private $ressources = array();
 
const MIME_JSON = 'application/json';
const PRESENCE_CHOROLOGIE = '1';
 
private $retourFormatsSupportes = array(self::MIME_JSON);
private $txtFormatsSupportes = array('txt', 'htm');
private $serviceUrl = null;
private $idPage = null;
private $wpBot = null;
private $infosPage = null;
 
public function __construct($ressources, $parametres, Conteneur $conteneur) {
$this->parametres = $parametres;
$this->ressources = $ressources;
 
$this->wpBot = $conteneur->getWikipediaBot();
$url = $conteneur->getParametre('url_service').DS.$this->ressources[0];
$this->serviceUrl = $conteneur->getUrl($url);
}
 
public function consulter() {
$this->idPage = $this->ressources[0];
 
$this->definirValeurParDefautDesParametres();
$this->verifierParametres();
 
$resultat = $this->obtenirResultat();
 
return $resultat;
}
 
private function definirValeurParDefautDesParametres() {
if (isset($this->parametres['retour']) == false) {
$this->parametres['retour'] = self::MIME_JSON;
}
if (isset($this->parametres['txt.format']) == false) {
$this->parametres['txt.format'] = 'txt';
}
}
 
private function verifierParametres() {
$erreurs = array();
 
if (isset($this->parametres['retour']) == false) {
$erreurs[] = "Le paramètre type de retour 'retour' est obligatoire.";
}
if ($this->verifierValeurParametreTxtFormat() == false) {
$erreurs[] = "Le format du texte '{$this->parametres['txt.format']}' n'est pas supporté.";
}
 
if (count($erreurs) > 0) {
$message = implode('<br />', $erreurs);
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
throw new Exception($message, $code);
}
}
 
private function verifierValeurParametreRetour() {
return in_array($this->parametres['retour'], $this->retourFormatsSupportes);
}
 
private function verifierValeurParametreTxtFormat() {
return in_array($this->parametres['txt.format'], $this->txtFormatsSupportes);
}
 
private function obtenirResultat() {
$this->chargerPageWp();
 
$resultat = new ResultatService();
$resultat->corps = $this->infosPage;
$resultat->mime = $this->parametres['retour'];
 
return $resultat;
}
 
private function chargerPageWp() {
$options = array('langue' => 'fr');
$this->wpBot = new WikipediaBot($options);
$this->wpBot->chargerPage($this->idPage);
 
$this->infosPage['id'] = $this->idPage;
$this->infosPage['titre'] = $this->wpBot->getPageTitre();
$this->infosPage['texte'] = $this->getTxt();
 
$this->infosPage['mime'] = $this->getTypeMime();
$this->infosPage['href'] = $this->getHref();
}
 
private function getTxt() {
$txt = '';
if (isset($this->parametres['txt.section.position'])) {
$positionSection = $this->parametres['txt.section.position'];
$txt = $this->wpBot->getSectionParNumero($positionSection);
} else if (isset($this->parametres['txt.section.titre'])) {
$titreSection = $this->parametres['txt.section.titre'];
if ($titreSection == 'taxobox') {
$txt = $this->wpBot->extraireTaxobox();
} else {
$txt = $this->wpBot->getSectionParTitre($titreSection);
}
} else {
$txt = $this->wpBot->getPageTxt();
}
if ($this->parametres['txt.format'] == 'htm') {
$txt = $this->wpBot->rendre($txt);
}
return $txt;
}
 
private function getTypeMime() {
$mime = '';
if ($this->parametres['txt.format'] == 'htm') {
$mime = 'txt/html';
} else if ($this->parametres['txt.format'] == 'txt') {
$mime = 'text/plain';
}
return $mime;
}
 
private function getHref() {
$href = '';
$this->serviceUrl->setRequete($this->parametres);
$href = $this->serviceUrl->getUrl();
return $href;
}
}
?>