Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 274 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
274 jpm 1
<?php
2
class PageTextes {
3
 
4
	private $parametres = array();
5
	private $ressources = array();
6
 
7
	const MIME_JSON = 'application/json';
8
	const PRESENCE_CHOROLOGIE = '1';
9
 
10
	private $retourFormatsSupportes = array(self::MIME_JSON);
11
	private $txtFormatsSupportes = array('txt', 'htm');
12
	private $serviceUrl = null;
13
	private $idPage = null;
14
	private $wpBot = null;
15
	private $infosPage = null;
16
 
17
	public function __construct($ressources, $parametres, Conteneur $conteneur) {
18
		$this->parametres = $parametres;
19
		$this->ressources = $ressources;
20
 
21
		$this->wpBot = $conteneur->getWikipediaBot();
22
		$url = $conteneur->getParametre('url_service').DS.$this->ressources[0];
23
		$this->serviceUrl = $conteneur->getUrl($url);
24
	}
25
 
26
	public function consulter() {
27
		$this->idPage = $this->ressources[0];
28
 
29
		$this->definirValeurParDefautDesParametres();
30
		$this->verifierParametres();
31
 
32
		$resultat = $this->obtenirResultat();
33
 
34
		return $resultat;
35
	}
36
 
37
	private function definirValeurParDefautDesParametres() {
38
		if (isset($this->parametres['retour']) == false) {
39
			$this->parametres['retour'] = self::MIME_JSON;
40
		}
41
		if (isset($this->parametres['txt.format']) == false) {
42
			$this->parametres['txt.format'] = 'txt';
43
		}
44
	}
45
 
46
	private function verifierParametres() {
47
		$erreurs = array();
48
 
49
		if (isset($this->parametres['retour']) == false) {
50
			$erreurs[] = "Le paramètre type de retour 'retour' est obligatoire.";
51
		}
52
		if ($this->verifierValeurParametreTxtFormat() == false) {
53
			$erreurs[] = "Le format du texte '{$this->parametres['txt.format']}' n'est pas supporté.";
54
		}
55
 
56
		if (count($erreurs) > 0) {
57
			$message = implode('<br />', $erreurs);
58
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
59
			throw new Exception($message, $code);
60
		}
61
	}
62
 
63
	private function verifierValeurParametreRetour() {
64
		return in_array($this->parametres['retour'], $this->retourFormatsSupportes);
65
	}
66
 
67
	private function verifierValeurParametreTxtFormat() {
68
		return in_array($this->parametres['txt.format'], $this->txtFormatsSupportes);
69
	}
70
 
71
	private function obtenirResultat() {
72
		$this->chargerPageWp();
73
 
74
		$resultat = new ResultatService();
75
		$resultat->corps = $this->infosPage;
76
		$resultat->mime = $this->parametres['retour'];
77
 
78
		return $resultat;
79
	}
80
 
81
	private function chargerPageWp() {
82
		$options = array('langue' => 'fr');
83
		$this->wpBot = new WikipediaBot($options);
84
		$this->wpBot->chargerPage($this->idPage);
85
 
86
		$this->infosPage['id'] = $this->idPage;
87
		$this->infosPage['titre'] = $this->wpBot->getPageTitre();
88
		$this->infosPage['texte'] = $this->getTxt();
89
 
90
		$this->infosPage['mime'] = $this->getTypeMime();
91
		$this->infosPage['href'] = $this->getHref();
92
	}
93
 
94
	private function getTxt() {
95
		$txt = '';
96
		if (isset($this->parametres['txt.section.position'])) {
97
			$positionSection = $this->parametres['txt.section.position'];
98
			$txt = $this->wpBot->getSectionParNumero($positionSection);
99
		} else if (isset($this->parametres['txt.section.titre'])) {
100
			$titreSection = $this->parametres['txt.section.titre'];
101
			if ($titreSection == 'taxobox') {
102
				$txt = $this->wpBot->extraireTaxobox();
103
			} else {
104
				$txt = $this->wpBot->getSectionParTitre($titreSection);
105
			}
106
		} else {
107
			$txt = $this->wpBot->getPageTxt();
108
		}
109
		if ($this->parametres['txt.format'] == 'htm') {
110
			$txt = $this->wpBot->rendre($txt);
111
		}
112
		return $txt;
113
	}
114
 
115
	private function getTypeMime() {
116
		$mime = '';
117
		if ($this->parametres['txt.format'] == 'htm') {
118
			$mime = 'txt/html';
119
		} else if ($this->parametres['txt.format'] == 'txt') {
120
			$mime = 'text/plain';
121
		}
122
		return $mime;
123
	}
124
 
125
	private function getHref() {
126
		$href = '';
127
		$this->serviceUrl->setRequete($this->parametres);
128
		$href = $this->serviceUrl->getUrl();
129
		return $href;
130
	}
131
}
132
?>