Subversion Repositories Applications.wikini

Rev

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

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