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; |
} |
} |
?> |