Subversion Repositories Applications.wikini

Rev

Rev 36 | Rev 38 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 36 Rev 37
1
<?php
1
<?php
2
// declare(encoding='UTF-8');
2
// declare(encoding='UTF-8');
3
/**
3
/**
4
 * Web service de consultation d'un page wiki
4
 * Web service de consultation d'un page wiki
5
 *
5
 *
6
 * @category	php 5.2
6
 * @category	php 5.2
7
 * @package		wapi
7
 * @package		wapi
8
 * @author		Aurélien Peronnet < aurelien@tela-botanica.org>
8
 * @author		Aurélien Peronnet < aurelien@tela-botanica.org>
9
 * @author		Jean-Pascal Milcent < jpm@tela-botanica.org>
9
 * @author		Jean-Pascal Milcent < jpm@tela-botanica.org>
10
 * @copyright	Copyright (c) 2011, Tela Botanica (accueil@tela-botanica.org)
10
 * @copyright	Copyright (c) 2011, Tela Botanica (accueil@tela-botanica.org)
11
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
11
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
12
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
12
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
13
 * @version		$Id$
13
 * @version		$Id$
14
 */
14
 */
15
class Pages extends Service {
15
class Pages extends Service {
16
	
16
	
17
	private $wiki = null;
17
	private $wiki = null;
18
	private $pageNom = null;
18
	private $pageNom = null;
19
	private $section = null;
19
	private $section = null;
20
	
20
	
21
	private $retour = 'txt';
21
	private $retour = 'txt';
22
	private $formats_retour = array('text/plain','text/html');
22
	private $formats_retour = array('text/plain','text/html');
23
	private $format_texte;
23
	private $format_texte;
24
	
24
	
25
	const MIME_JSON = 'application/json';
25
	const MIME_JSON = 'application/json';
26
	const MIME_HTML = 'text/html';
26
	const MIME_HTML = 'text/html';
27
	const MIME_TEXT = 'text/plain';
27
	const MIME_TEXT = 'text/plain';
28
	
28
	
29
	public function consulter($ressources, $parametres) {
29
	public function consulter($ressources, $parametres) {
30
		
30
		
31
		try {
31
		try {
32
			$this->definirValeurParDefautDesParametres();
32
			$this->definirValeurParDefautDesParametres();
33
			$this->verifierParametres($parametres);
33
			$this->verifierParametres($parametres);
34
			$this->analyserParametres($ressources, $parametres);
34
			$this->analyserParametres($ressources, $parametres);
35
			
35
			
36
			$page = $this->consulterPage($this->pageNom, $this->section);
36
			$page = $this->consulterPage($this->pageNom, $this->section);
-
 
37
			
37
			if ($page == null) {
38
			if ($page == null) {
38
				$message = 'La page demandée n\'existe pas';
39
				$message = 'La page demandée n\'existe pas';
39
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
40
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
40
				throw new Exception($message, $code);
41
				throw new Exception($message, $code);
41
			}
42
			}
42
			$retour = $this->formaterRetour($page);
43
			$retour = $this->formaterRetour($page);
43
			
44
			
44
			$this->envoyerContenuJson($retour);
45
			$this->envoyerContenuJson($retour);
45
		} catch (Exception $e) {
46
		} catch (Exception $e) {
46
			$this->envoyerErreur($e);
47
			$this->envoyerErreur($e);
47
		}
48
		}
48
	}
49
	}
49
	
50
	
50
	private function definirValeurParDefautDesParametres() {
51
	private function definirValeurParDefautDesParametres() {
51
		if (isset($this->parametres['txt_format']) == false) {
52
		if (isset($this->parametres['txt_format']) == false) {
52
			$this->parametres['txt_format'] = 'text/plain';
53
			$this->parametres['txt_format'] = 'text/plain';
53
		}
54
		}
54
	}
55
	}
55
	
56
	
56
	private function verifierParametres($parametres) {
57
	private function verifierParametres($parametres) {
57
		$erreurs = array();
58
		$erreurs = array();
58
		
59
		
59
		if (isset($parametres['txt_format'])) {
60
		if (isset($parametres['txt_format'])) {
60
			if(!in_array($parametres['txt_format'], $this->formats_retour)) {
61
			if(!in_array($parametres['txt_format'], $this->formats_retour)) {
61
				$message = "La valeur du paramètre 'txt.format' peut seulement prendre les valeurs : text/plain et text/html.";
62
				$message = "La valeur du paramètre 'txt.format' peut seulement prendre les valeurs : text/plain et text/html.";
62
				$erreurs[] = $message;
63
				$erreurs[] = $message;
63
			}
64
			}
64
		}
65
		}
65
		
66
		
66
		if(isset($parametres['txt_section_position']) && !is_numeric($parametres['txt_section_position'])) {
67
		if (isset($parametres['txt_section_position']) && !is_numeric($parametres['txt_section_position'])) {
67
			$message = "La valeur du paramètre 'txt.section.position' peut seulement prendre des valeurs numeriques";
68
			$message = "La valeur du paramètre 'txt.section.position' peut seulement prendre des valeurs numeriques";
68
			$erreurs[] = $message;
69
			$erreurs[] = $message;
69
		}
70
		}
70
		
71
		
71
		if(isset($parametres['txt_section_titre']) && trim($parametres['txt_section_titre']) == '') {
72
		if (isset($parametres['txt_section_titre']) && trim($parametres['txt_section_titre']) == '') {
72
			$message = "La valeur du paramètre 'txt.section.titre' ne peut pas être vide si celui-ci est présent";
73
			$message = "La valeur du paramètre 'txt.section.titre' ne peut pas être vide si celui-ci est présent";
73
			$erreurs[] = $message;
74
			$erreurs[] = $message;
74
		}
75
		}
75
				
76
				
76
		if (count($erreurs) > 0) {
77
		if (count($erreurs) > 0) {
77
			$message = implode('<br />', $erreurs);
78
			$message = implode('<br />', $erreurs);
78
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
79
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
79
			throw new Exception($message, $code);
80
			throw new Exception($message, $code);
80
		}
81
		}
81
	}
82
	}
82
	
83
	
83
	private function analyserParametres($ressources, $parametres) {	
84
	private function analyserParametres($ressources, $parametres) {	
84
		$this->pageNom = $ressources[0];
85
		$this->pageNom = $ressources[0];
85
		if(isset($parametres['txt_section_titre'])) {
86
		if (isset($parametres['txt_section_titre'])) {
86
			$this->section = $parametres['txt_section_titre'];
87
			$this->section = $parametres['txt_section_titre'];
87
		}
88
		}
88
		if(isset($parametres['txt_section_position'])) {
89
		if (isset($parametres['txt_section_position'])) {
89
			$this->section = $parametres['txt_section_position'];
90
			$this->section = $parametres['txt_section_position'];
90
		}
91
		}
91
		if (isset($parametres['txt_format'])) {
92
		if (isset($parametres['txt_format'])) {
92
			$this->retour = $parametres['txt_format'];
93
			$this->retour = $parametres['txt_format'];
93
		}
94
		}
94
	}
95
	}
95
	
96
	
96
	private function consulterPage($page, $section = null) {
97
	private function consulterPage($page, $section = null) {
97
		
98
		
98
		$this->wiki = Registre::get('wikiApi');
99
		$this->wiki = Registre::get('wikiApi');
99
		$this->wiki->setPageCourante($this->pageNom);
100
		$this->wiki->setPageCourante($this->pageNom);
100
		$page = $this->wiki->LoadPage($page);
101
		$page = $this->wiki->LoadPage($page);
101
		
102
				
102
		if($page != null) {
103
		if ($page != null) {
103
			// attention les wikis sont souvent en ISO !
104
			// attention les wikis sont souvent en ISO !
104
			$page["body"] = $this->convertirTexteWikiVersEncodageAppli($page['body']);
105
			$page["body"] = $this->convertirTexteWikiVersEncodageAppli($page['body']);
105
		
-
 
106
			if($section != null) {
106
			if($section != null) {
107
				$page["body"] = $this->decouperPageSection($page["body"], $section);
107
				$page["body"] = $this->decouperPageSection($page["body"], $section);
108
			}
108
			}
109
		}
109
		}
110
	
110
	
111
		return $page;
111
		return $page;
112
	}
112
	}
113
	
113
	
114
	private function decouperPageSection($contenu_page, $section) {
114
	private function decouperPageSection($contenu_page, $section) {
115
	
115
	
116
		$section_retour = '';
116
		$section_retour = '';
117
	
-
 
118
		if(is_numeric($section)) {
117
		if (is_numeric($section)) {
119
			$section_retour =  $this->getSectionParNumero($contenu_page, $section);
118
			$section_retour =  $this->getSectionParNumero($contenu_page, $section);
120
		} else {
119
		} else {
121
			$section_retour =  $this->getSectionParTitre($contenu_page, $section);
120
			$section_retour =  $this->getSectionParTitre($contenu_page, $section);
122
		}
121
		}
123
	
-
 
124
		return $section_retour;
122
		return $section_retour;
125
	}
123
	}
126
	
124
	
127
	public function getSectionParNumero($page, $num) {
125
	public function getSectionParNumero($page, $num) {
128
		preg_match_all('/(=[=]+[ ]*)(.[.^=]*)+[ ]*=[=]+[.]*/i', $page, $sections, PREG_OFFSET_CAPTURE);
126
		preg_match_all('/(=[=]+[ ]*)(.[.^=]*)+[ ]*=[=]+[.]*/i', $page, $sections, PREG_OFFSET_CAPTURE);
129
		$sectionTxt = '';
127
		$sectionTxt = '';
130
		$debut_section = 0;
128
		$debut_section = 0;
131
		$lg_page = strlen($page);
129
		$lg_page = strlen($page);
132
		$fin_section = $lg_page;
130
		$fin_section = $lg_page;
133
		
131
		
134
		if($num <= count($sections[1]) && $num > 0) {	
132
		if ($num <= count($sections[1]) && $num > 0) {	
135
						
133
						
136
			$debut_section = $sections[1][$num - 1][1];
134
			$debut_section = $sections[1][$num - 1][1];
137
			$separateur = trim($sections[1][$num - 1][0]);
135
			$separateur = trim($sections[1][$num - 1][0]);
138
			$separateur_trouve = false;
136
			$separateur_trouve = false;
139
						
137
						
140
			for($i = $num; $i < count($sections[1]); $i++) {
138
			for ($i = $num; $i < count($sections[1]); $i++) {
141
				$fin_section = $sections[1][$i][1];
139
				$fin_section = $sections[1][$i][1];
142
				if($separateur == trim($sections[1][$i][0])) {
140
				if($separateur == trim($sections[1][$i][0])) {
143
					$separateur_trouve = true;
141
					$separateur_trouve = true;
144
					break;
142
					break;
145
				}
143
				}
146
			}
144
			}
147
			
145
			
148
			$fin_section = $separateur_trouve ? $fin_section : $lg_page;
146
			$fin_section = $separateur_trouve ? $fin_section : $lg_page;
149
			$sectionTxt = substr($page, $debut_section, $fin_section - $debut_section);
147
			$sectionTxt = substr($page, $debut_section, $fin_section - $debut_section);
-
 
148
			
150
		} else {
149
		} else {
151
			$sectionTxt = '';
150
			$sectionTxt = '';
152
		}	
151
		}
153
 
152
 
154
		return $sectionTxt;
153
		return $sectionTxt;
155
	}
154
	}
156
	
155
	
157
	public function getSectionParTitre($page, $titre) {
156
	public function getSectionParTitre($page, $titre) {
158
		$section = '';
157
		$section = '';
159
		$reg_exp = '/((=[=]+)[ ]*'.preg_quote(trim($titre), '/').'[ ]*=[=]+)[.]*/i';
158
		$reg_exp = '/((=[=]+)[ ]*'.preg_quote(trim($titre), '/').'[ ]*=[=]+)[.]*/i';
160
		$match = preg_split($reg_exp, $page, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
159
		$match = preg_split($reg_exp, $page, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
161
		
-
 
162
		if(count($match) > 3) {
160
		if (count($match) > 3) {
163
			$section = explode(trim($match[2]), $match[3], 2);
161
			$section = explode(trim($match[2]), $match[3], 2);
164
			$section = $match[1].$section[0];
162
			$section = $match[1].$section[0];
165
		} elseif(count($match) == 2) {
163
		} elseif (count($match) == 3) {
166
			$section = explode(trim($match[1]), $match[2], 2);
164
			$section = explode(trim($match[1]), $match[2], 2);
167
			$section = $match[0].$section[0];
165
			$section = $match[0].$section[0];
168
		} else {
166
		} else {
169
			$section = "";
167
			$section = "";
170
		}
168
		}
171
		
169
		
172
		return $section;
170
		return $section;
173
	}
171
	}
174
	
172
	
175
	private function formaterRetour($page) {
173
	private function formaterRetour($page) {
176
 
174
 
177
		$mime = null;
175
		$mime = null;
178
		$texte = '';
176
		$texte = '';
179
		
177
		
180
		switch($this->retour) {
178
		switch ($this->retour) {
181
			case self::MIME_HTML:
179
			case self::MIME_HTML:
182
				$texte = $this->wiki->Format($page["body"], "wakka");
180
				$texte = $this->wiki->Format($page["body"], "wakka");
183
				$mime = self::MIME_HTML;
181
				$mime = self::MIME_HTML;
184
				break;
182
				break;
185
			default:
183
			default:
186
				$texte = $page["body"];
184
				$texte = $page["body"];
187
				$mime = self::MIME_TEXT;
185
				$mime = self::MIME_TEXT;
188
		}
186
		}
189
		
187
		
190
		$url = $this->wiki->Href("", $this->pageNom);
188
		$url = $this->wiki->Href("", $this->pageNom);
191
		
189
		
192
		$retour = array('id' => $this->pageNom,
190
		$retour = array('id' => $this->pageNom,
193
				'titre' => $this->pageNom,
191
				'titre' => $this->pageNom,
194
				'mime' => $mime,
192
				'mime' => $mime,
195
				'texte' => $texte,
193
				'texte' => $texte,
196
				'href' => $url);
194
				'href' => $url);
197
		
195
		
198
		return $retour;
196
		return $retour;
199
	}
197
	}
200
	
198
	
201
	public function ajouter($ressources, $requeteDonnees) {
199
	public function ajouter($ressources, $requeteDonnees) {
202
		return $this->modifier($ressources, $requeteDonnees);
200
		return $this->modifier($ressources, $requeteDonnees);
203
	}
201
	}
204
	
202
	
205
	public function modifier($ressources, $requeteDonnees) {
203
	public function modifier($ressources, $requeteDonnees) {
206
			
204
 
-
 
205
		$requeteDonnees['pageTag'] = $ressources[0];
207
		$this->verifierParametresEcriture($requeteDonnees);
206
		$this->verifierParametresEcriture($requeteDonnees);
208
		$this->analyserParametresEcriture($requeteDonnees);
207
		$this->analyserParametresEcriture($requeteDonnees);
209
		
-
 
210
		$this->wiki = Registre::get('wikiApi');
208
		$this->wiki = Registre::get('wikiApi');
211
		$this->wiki->setPageCourante($this->pageNom);
209
		$this->wiki->setPageCourante($this->pageNom);
212
		
210
		
213
		$texte = $requeteDonnees['texte'];
211
		$texte = $requeteDonnees['pageContenu'];
214
		$page = $this->consulterPage($this->pageNom);
212
		$page = $this->consulterPage($this->pageNom);
215
				
213
				
216
		if($page != null) {
214
		if ($page != null) {
217
			$corps = ($this->section != null) ? $this->remplacerSection($this->section, $texte, $page['body']) : $texte;	
215
			$corps = ($this->section != null) ? $this->remplacerSection($this->section, $texte, $page['body']) : $texte;	
218
		} else {
216
		} else {
219
			$corps = $texte;
217
			$corps = $texte;
220
		}	
218
		}	
-
 
219
		
221
		$ecriture = $this->ecrirePage($this->pageNom, $corps);	
220
		$ecriture = $this->ecrirePage($this->pageNom, $corps);	
-
 
221
		
222
		if($ecriture) {
222
		if ($ecriture) {
223
			$this->envoyerCreationEffectuee();
223
			$this->envoyerCreationEffectuee();
224
		} else {
224
		} else {
225
			$message = 'Impossible de créer ou modifier la page';
225
			$message = 'Impossible de créer ou modifier la page';
226
			$code = RestServeur::HTTP_CODE_ERREUR;
226
			$code = RestServeur::HTTP_CODE_ERREUR;
227
			throw new Exception($message, $code);
227
			throw new Exception($message, $code);
228
		}
228
		}
229
		
229
		
230
		return $ecriture;
230
		return $ecriture;
231
	}
231
	}
232
	
232
	
233
	private function remplacerSection($titre_ou_numero_section, $section_remplacement, $corps) {
233
	private function remplacerSection($titre_ou_numero_section, $section_remplacement, $corps) {
234
		$section_page_originale = $this->decouperPageSection($corps, $titre_ou_numero_section);
234
		$section_page_originale = $this->decouperPageSection($corps, $titre_ou_numero_section);
235
		$contenu = str_replace($section_page_originale, $texte, $corps);
235
		$contenu = str_replace($section_page_originale, $section_remplacement, $corps);
236
		
236
		
237
		return $contenu;
237
		return $contenu;
238
	}
238
	}
239
	
239
	
240
	private function ecrirePage($nom_page, $contenu) {
240
	private function ecrirePage($nom_page, $contenu) {
241
		
241
		
242
		$texte_encode = $this->convertirTexteAppliVersEncodageWiki($contenu);
242
		$texte_encode = $this->convertirTexteAppliVersEncodageWiki($contenu);
243
		$ecriture = $this->wiki->SavePage($nom_page, $texte_encode, "", true);
243
		$ecriture = $this->wiki->SavePage($nom_page, $texte_encode, "", true);
244
		
244
		
245
		return $ecriture;
245
		return $ecriture;
246
	}
246
	}
247
	
247
	
248
	private function analyserParametresEcriture($parametres) {
248
	private function analyserParametresEcriture($parametres) {
249
		$this->pageNom = $parametres['wiki'];
249
		$this->pageNom = $parametres['pageTag'];
250
		$this->section = isset($parametres['section']) ? $parametres['section'] : null;
250
		$this->section = isset($parametres['pageSection']) ? $parametres['pageSection'] : null;
251
	}
251
	}
252
	
252
	
253
	private function verifierParametresEcriture($parametres) {
253
	private function verifierParametresEcriture($parametres) {
254
			
254
			
255
		$erreurs = array();
255
		$erreurs = array();
256
		
256
		
257
		if(!isset($parametres['texte'])) {
257
		if(!isset($parametres['pageContenu'])) {
258
			$message = "Le paramètre texte est obligatoire";
258
			$message = "Le paramètre pageContenu est obligatoire";
259
			$erreurs[] = $message;
259
			$erreurs[] = $message;
260
		}
260
		}
261
		
261
		
262
		if(!isset($parametres['wiki']) || trim($parametres['wiki']) == '') {
262
		if(!isset($parametres['pageTag']) || trim($parametres['pageTag']) == '') {
263
			$message = "Le paramètre wiki est obligatoire";
263
			$message = "Le paramètre pageTag est obligatoire";
264
			$erreurs[] = $message;
264
			$erreurs[] = $message;
265
		}
265
		}
266
		
266
		
267
		if (count($erreurs) > 0) {
267
		if (count($erreurs) > 0) {
268
			$message = implode('<br />', $erreurs);
268
			$message = implode('<br />', $erreurs);
269
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
269
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
270
			throw new Exception($message, $code);
270
			throw new Exception($message, $code);
271
		}
271
		}
272
	}
272
	}
273
	
273
	
274
	private function convertirTexteWikiVersEncodageAppli($texte) {
274
	private function convertirTexteWikiVersEncodageAppli($texte) {
275
		if(Config::get('encodage_appli') != Config::get('encodage_wiki')) {
275
		if(Config::get('encodage_appli') != Config::get('encodage_wiki')) {
276
			$texte = mb_convert_encoding($texte,Config::get('encodage_appli'),Config::get('encodage_wiki'));
276
			$texte = mb_convert_encoding($texte,Config::get('encodage_appli'),Config::get('encodage_wiki'));
277
		}
277
		}
278
		return $texte;
278
		return $texte;
279
	}
279
	}
280
	
280
	
281
	private function convertirTexteAppliVersEncodageWiki($texte) {
281
	private function convertirTexteAppliVersEncodageWiki($texte) {
282
		if(Config::get('encodage_appli') != Config::get('encodage_wiki')) {
282
		if(Config::get('encodage_appli') != Config::get('encodage_wiki')) {
283
			$texte = mb_convert_encoding($texte,Config::get('encodage_wiki'),Config::get('encodage_appli'));
283
			$texte = mb_convert_encoding($texte,Config::get('encodage_wiki'),Config::get('encodage_appli'));
284
		}
284
		}
285
		return $texte;
285
		return $texte;
286
	}
286
	}
287
}	
287
}	
288
?>
288
?>