Subversion Repositories Applications.wikini

Rev

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

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