Subversion Repositories Applications.wikini

Rev

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

Rev 37 Rev 38
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
			
38
			if ($page == null) {
38
			if ($page == null) {
39
				$message = 'La page demandée n\'existe pas';
39
				$message = 'La page demandée n\'existe pas';
40
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
40
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
41
				throw new Exception($message, $code);
41
				throw new Exception($message, $code);
42
			}
42
			}
43
			$retour = $this->formaterRetour($page);
43
			$retour = $this->formaterRetour($page);
44
			
44
			
45
			$this->envoyerContenuJson($retour);
45
			$this->envoyerContenuJson($retour);
46
		} catch (Exception $e) {
46
		} catch (Exception $e) {
47
			$this->envoyerErreur($e);
47
			$this->envoyerErreur($e);
48
		}
48
		}
49
	}
49
	}
50
	
50
	
51
	private function definirValeurParDefautDesParametres() {
51
	private function definirValeurParDefautDesParametres() {
52
		if (isset($this->parametres['txt_format']) == false) {
52
		if (isset($this->parametres['txt_format']) == false) {
53
			$this->parametres['txt_format'] = 'text/plain';
53
			$this->parametres['txt_format'] = 'text/plain';
54
		}
54
		}
55
	}
55
	}
56
	
56
	
57
	private function verifierParametres($parametres) {
57
	private function verifierParametres($parametres) {
58
		$erreurs = array();
58
		$erreurs = array();
59
		
59
		
60
		if (isset($parametres['txt_format'])) {
60
		if (isset($parametres['txt_format'])) {
61
			if(!in_array($parametres['txt_format'], $this->formats_retour)) {
61
			if(!in_array($parametres['txt_format'], $this->formats_retour)) {
62
				$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.";
63
				$erreurs[] = $message;
63
				$erreurs[] = $message;
64
			}
64
			}
65
		}
65
		}
66
		
66
		
67
		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'])) {
68
			$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";
69
			$erreurs[] = $message;
69
			$erreurs[] = $message;
70
		}
70
		}
71
		
71
		
72
		if (isset($parametres['txt_section_titre']) && trim($parametres['txt_section_titre']) == '') {
72
		if (isset($parametres['txt_section_titre']) && trim($parametres['txt_section_titre']) == '') {
73
			$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";
74
			$erreurs[] = $message;
74
			$erreurs[] = $message;
75
		}
75
		}
76
				
76
				
77
		if (count($erreurs) > 0) {
77
		if (count($erreurs) > 0) {
78
			$message = implode('<br />', $erreurs);
78
			$message = implode('<br />', $erreurs);
79
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
79
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
80
			throw new Exception($message, $code);
80
			throw new Exception($message, $code);
81
		}
81
		}
82
	}
82
	}
83
	
83
	
84
	private function analyserParametres($ressources, $parametres) {	
84
	private function analyserParametres($ressources, $parametres) {	
85
		$this->pageNom = $ressources[0];
85
		$this->pageNom = $ressources[0];
86
		if (isset($parametres['txt_section_titre'])) {
86
		if (isset($parametres['txt_section_titre'])) {
87
			$this->section = $parametres['txt_section_titre'];
87
			$this->section = $parametres['txt_section_titre'];
88
		}
88
		}
89
		if (isset($parametres['txt_section_position'])) {
89
		if (isset($parametres['txt_section_position'])) {
90
			$this->section = $parametres['txt_section_position'];
90
			$this->section = $parametres['txt_section_position'];
91
		}
91
		}
92
		if (isset($parametres['txt_format'])) {
92
		if (isset($parametres['txt_format'])) {
93
			$this->retour = $parametres['txt_format'];
93
			$this->retour = $parametres['txt_format'];
94
		}
94
		}
95
	}
95
	}
96
	
96
	
97
	private function consulterPage($page, $section = null) {
97
	private function consulterPage($page, $section = null) {
98
		
98
		
99
		$this->wiki = Registre::get('wikiApi');
99
		$this->wiki = Registre::get('wikiApi');
100
		$this->wiki->setPageCourante($this->pageNom);
100
		$this->wiki->setPageCourante($this->pageNom);
101
		$page = $this->wiki->LoadPage($page);
101
		$page = $this->wiki->LoadPage($page);
102
				
102
				
103
		if ($page != null) {
103
		if ($page != null) {
104
			// attention les wikis sont souvent en ISO !
104
			// attention les wikis sont souvent en ISO !
105
			$page["body"] = $this->convertirTexteWikiVersEncodageAppli($page['body']);
105
			$page["body"] = $this->convertirTexteWikiVersEncodageAppli($page['body']);
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
		if (is_numeric($section)) {
117
		if (is_numeric($section)) {
118
			$section_retour =  $this->getSectionParNumero($contenu_page, $section);
118
			$section_retour =  $this->getSectionParNumero($contenu_page, $section);
119
		} else {
119
		} else {
120
			$section_retour =  $this->getSectionParTitre($contenu_page, $section);
120
			$section_retour =  $this->getSectionParTitre($contenu_page, $section);
121
		}
121
		}
122
		return $section_retour;
122
		return $section_retour;
123
	}
123
	}
124
	
124
	
125
	public function getSectionParNumero($page, $num) {
125
	public function getSectionParNumero($page, $num) {
126
		preg_match_all('/(=[=]+[ ]*)(.[.^=]*)+[ ]*=[=]+[.]*/i', $page, $sections, PREG_OFFSET_CAPTURE);
126
		preg_match_all('/(=[=]+[ ]*)(.[.^=]*)+[ ]*=[=]+[.]*/i', $page, $sections, PREG_OFFSET_CAPTURE);
127
		$sectionTxt = '';
127
		$sectionTxt = '';
128
		$debut_section = 0;
128
		$debut_section = 0;
129
		$lg_page = strlen($page);
129
		$lg_page = strlen($page);
130
		$fin_section = $lg_page;
130
		$fin_section = $lg_page;
131
		
131
		
132
		if ($num <= count($sections[1]) && $num > 0) {	
132
		if ($num <= count($sections[1]) && $num > 0) {	
133
						
133
						
134
			$debut_section = $sections[1][$num - 1][1];
134
			$debut_section = $sections[1][$num - 1][1];
135
			$separateur = trim($sections[1][$num - 1][0]);
135
			$separateur = trim($sections[1][$num - 1][0]);
136
			$separateur_trouve = false;
136
			$separateur_trouve = false;
137
						
137
						
138
			for ($i = $num; $i < count($sections[1]); $i++) {
138
			for ($i = $num; $i < count($sections[1]); $i++) {
139
				$fin_section = $sections[1][$i][1];
139
				$fin_section = $sections[1][$i][1];
140
				if($separateur == trim($sections[1][$i][0])) {
140
				if($separateur == trim($sections[1][$i][0])) {
141
					$separateur_trouve = true;
141
					$separateur_trouve = true;
142
					break;
142
					break;
143
				}
143
				}
144
			}
144
			}
145
			
145
			
146
			$fin_section = $separateur_trouve ? $fin_section : $lg_page;
146
			$fin_section = $separateur_trouve ? $fin_section : $lg_page;
147
			$sectionTxt = substr($page, $debut_section, $fin_section - $debut_section);
147
			$sectionTxt = substr($page, $debut_section, $fin_section - $debut_section);
148
			
148
			
149
		} else {
149
		} else {
150
			$sectionTxt = '';
150
			$sectionTxt = '';
151
		}
151
		}
152
 
152
 
153
		return $sectionTxt;
153
		return $sectionTxt;
154
	}
154
	}
155
	
155
	
156
	public function getSectionParTitre($page, $titre) {
156
	public function getSectionParTitre($page, $titre) {
157
		$section = '';
157
		$section = '';
158
		$reg_exp = '/((=[=]+)[ ]*'.preg_quote(trim($titre), '/').'[ ]*=[=]+)[.]*/i';
158
		$reg_exp = '/((=[=]+)[ ]*'.preg_quote(trim($titre), '/').'[ ]*=[=]+)[.]*/i';
159
		$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);
160
		if (count($match) > 3) {
160
		if (count($match) > 3) {
161
			$section = explode(trim($match[2]), $match[3], 2);
161
			$section = explode(trim($match[2]), $match[3], 2);
162
			$section = $match[1].$section[0];
162
			$section = $match[1].$section[0];
163
		} elseif (count($match) == 3) {
163
		} elseif (count($match) == 3) {
164
			$section = explode(trim($match[1]), $match[2], 2);
164
			$section = explode(trim($match[1]), $match[2], 2);
165
			$section = $match[0].$section[0];
165
			$section = $match[0].$section[0];
166
		} else {
166
		} else {
167
			$section = "";
167
			$section = "";
168
		}
168
		}
169
		
169
		
170
		return $section;
170
		return $section;
171
	}
171
	}
172
	
172
	
173
	private function formaterRetour($page) {
173
	private function formaterRetour($page) {
174
 
174
 
175
		$mime = null;
175
		$mime = null;
176
		$texte = '';
176
		$texte = '';
177
		
177
		
178
		switch ($this->retour) {
178
		switch ($this->retour) {
179
			case self::MIME_HTML:
179
			case self::MIME_HTML:
180
				$texte = $this->wiki->Format($page["body"], "wakka");
180
				$texte = $this->wiki->Format($page["body"], "wakka");
181
				$mime = self::MIME_HTML;
181
				$mime = self::MIME_HTML;
182
				break;
182
				break;
183
			default:
183
			default:
184
				$texte = $page["body"];
184
				$texte = $page["body"];
185
				$mime = self::MIME_TEXT;
185
				$mime = self::MIME_TEXT;
186
		}
186
		}
187
		
187
		
188
		$url = $this->wiki->Href("", $this->pageNom);
188
		$url = $this->wiki->Href("", $this->pageNom);
189
		
189
		
190
		$retour = array('id' => $this->pageNom,
190
		$retour = array('id' => $this->pageNom,
191
				'titre' => $this->pageNom,
191
				'titre' => $this->pageNom,
192
				'mime' => $mime,
192
				'mime' => $mime,
193
				'texte' => $texte,
193
				'texte' => $texte,
194
				'href' => $url);
194
				'href' => $url);
195
		
195
		
196
		return $retour;
196
		return $retour;
197
	}
197
	}
198
	
198
	
199
	public function ajouter($ressources, $requeteDonnees) {
199
	public function ajouter($ressources, $requeteDonnees) {
200
		return $this->modifier($ressources, $requeteDonnees);
200
		return $this->modifier($ressources, $requeteDonnees);
201
	}
201
	}
202
	
202
	
203
	public function modifier($ressources, $requeteDonnees) {
203
	public function modifier($ressources, $requeteDonnees) {
204
 
204
 
205
		$requeteDonnees['pageTag'] = $ressources[0];
205
		$requeteDonnees['pageTag'] = $ressources[0];
206
		$this->verifierParametresEcriture($requeteDonnees);
206
		$this->verifierParametresEcriture($requeteDonnees);
207
		$this->analyserParametresEcriture($requeteDonnees);
207
		$this->analyserParametresEcriture($requeteDonnees);
208
		$this->wiki = Registre::get('wikiApi');
208
		$this->wiki = Registre::get('wikiApi');
209
		$this->wiki->setPageCourante($this->pageNom);
209
		$this->wiki->setPageCourante($this->pageNom);
210
		
210
		
211
		$texte = $requeteDonnees['pageContenu'];
211
		$texte = $requeteDonnees['pageContenu'];
212
		$page = $this->consulterPage($this->pageNom);
212
		$page = $this->consulterPage($this->pageNom);
213
				
213
				
214
		if ($page != null) {
214
		if ($page != null) {
215
			$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;	
216
		} else {
216
		} else {
217
			$corps = $texte;
217
			$corps = $texte;
218
		}	
218
		}	
219
		
219
		
220
		$ecriture = $this->ecrirePage($this->pageNom, $corps);	
220
		$ecriture = $this->ecrirePage($this->pageNom, $corps);	
221
		
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
		
-
 
235
		//TODO: empecher l'insertion de section de niveau supérieur (avec plus de =)
-
 
236
		// en les réduisant pour que le niveau de la section soit toujours inférieur à celui
-
 
237
		// de la section éditée ? voir avec le PO !	
-
 
238
		
-
 
239
		//TODO: insérer un espace ou un saut de ligne à la fin du texte si celui-ci n'en contient pas 
-
 
240
		// à la fin, ceci pour éviter que le formatage saute lors de l'édition du wiki
234
		$section_page_originale = $this->decouperPageSection($corps, $titre_ou_numero_section);
241
		$section_page_originale = $this->decouperPageSection($corps, $titre_ou_numero_section);
235
		$contenu = str_replace($section_page_originale, $section_remplacement, $corps);
242
		$contenu = str_replace($section_page_originale, $section_remplacement, $corps);
236
		
243
			
237
		return $contenu;
244
		return $contenu;
238
	}
245
	}
239
	
246
	
240
	private function ecrirePage($nom_page, $contenu) {
247
	private function ecrirePage($nom_page, $contenu) {
241
		
248
		
242
		$texte_encode = $this->convertirTexteAppliVersEncodageWiki($contenu);
249
		$texte_encode = $this->convertirTexteAppliVersEncodageWiki($contenu);
243
		$ecriture = $this->wiki->SavePage($nom_page, $texte_encode, "", true);
250
		$ecriture = $this->wiki->SavePage($nom_page, $texte_encode, "", true);
244
		
251
		
245
		return $ecriture;
252
		return $ecriture;
246
	}
253
	}
247
	
254
	
248
	private function analyserParametresEcriture($parametres) {
255
	private function analyserParametresEcriture($parametres) {
249
		$this->pageNom = $parametres['pageTag'];
256
		$this->pageNom = $parametres['pageTag'];
250
		$this->section = isset($parametres['pageSection']) ? $parametres['pageSection'] : null;
257
		$this->section = isset($parametres['pageSection']) ? $parametres['pageSection'] : null;
251
	}
258
	}
252
	
259
	
253
	private function verifierParametresEcriture($parametres) {
260
	private function verifierParametresEcriture($parametres) {
254
			
261
			
255
		$erreurs = array();
262
		$erreurs = array();
256
		
263
		
257
		if(!isset($parametres['pageContenu'])) {
264
		if(!isset($parametres['pageContenu'])) {
258
			$message = "Le paramètre pageContenu est obligatoire";
265
			$message = "Le paramètre pageContenu est obligatoire";
259
			$erreurs[] = $message;
266
			$erreurs[] = $message;
260
		}
267
		}
261
		
268
		
262
		if(!isset($parametres['pageTag']) || trim($parametres['pageTag']) == '') {
269
		if(!isset($parametres['pageTag']) || trim($parametres['pageTag']) == '') {
263
			$message = "Le paramètre pageTag est obligatoire";
270
			$message = "Le paramètre pageTag est obligatoire";
264
			$erreurs[] = $message;
271
			$erreurs[] = $message;
265
		}
272
		}
266
		
273
		
267
		if (count($erreurs) > 0) {
274
		if (count($erreurs) > 0) {
268
			$message = implode('<br />', $erreurs);
275
			$message = implode('<br />', $erreurs);
269
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
276
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
270
			throw new Exception($message, $code);
277
			throw new Exception($message, $code);
271
		}
278
		}
272
	}
279
	}
273
	
280
	
274
	private function convertirTexteWikiVersEncodageAppli($texte) {
281
	private function convertirTexteWikiVersEncodageAppli($texte) {
275
		if(Config::get('encodage_appli') != Config::get('encodage_wiki')) {
282
		if(Config::get('encodage_appli') != Config::get('encodage_wiki')) {
276
			$texte = mb_convert_encoding($texte,Config::get('encodage_appli'),Config::get('encodage_wiki'));
283
			$texte = mb_convert_encoding($texte,Config::get('encodage_appli'),Config::get('encodage_wiki'));
277
		}
284
		}
278
		return $texte;
285
		return $texte;
279
	}
286
	}
280
	
287
	
281
	private function convertirTexteAppliVersEncodageWiki($texte) {
288
	private function convertirTexteAppliVersEncodageWiki($texte) {
282
		if(Config::get('encodage_appli') != Config::get('encodage_wiki')) {
289
		if(Config::get('encodage_appli') != Config::get('encodage_wiki')) {
283
			$texte = mb_convert_encoding($texte,Config::get('encodage_wiki'),Config::get('encodage_appli'));
290
			$texte = mb_convert_encoding($texte,Config::get('encodage_wiki'),Config::get('encodage_appli'));
284
		}
291
		}
285
		return $texte;
292
		return $texte;
286
	}
293
	}
287
}	
294
}	
288
?>
295
?>