Subversion Repositories Applications.wikini

Rev

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

Rev 31 Rev 33
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('txt','html');
-
 
23
	private $format_texte;
-
 
24
	
-
 
25
	const MIME_JSON = 'application/json';
-
 
26
	const MIME_HTML = 'text/html';
-
 
27
	const MIME_TEXT = 'text/plain';
23
	
28
	
-
 
29
	public function consulter($ressources, $parametres) {
-
 
30
		
24
	public function consulter($ressources, $parametres) {
31
		try {
25
		header('Content-type: text/plain');
32
			$this->definirValeurParDefautDesParametres();
26
		$verifOk = $this->verifierParametres($parametres);
-
 
27
		if ($verifOk) {
33
			$this->verifierParametres($parametres);
-
 
34
			$this->analyserParametres($ressources, $parametres);
28
			$this->pageNom = $ressources[0];
35
			
29
			$page = $this->consulterPage($ressources[0]);
36
			$page = $this->consulterPage($this->pageNom);
-
 
37
			$retour = $this->formaterRetour($page);
-
 
38
			
30
			return $this->formaterRetour($page);
39
			$this->envoyerContenuJson($retour);
31
		} else {
40
		} catch (Exception $e) {
32
			RestServeur::envoyerEnteteStatutHttp(RestServeur::HTTP_CODE_MAUVAISE_REQUETE);		
41
			$this->envoyerErreur($e);
33
		}
42
		}
34
	}
43
	}
35
	
44
	
36
	private function definirValeurParDefautDesParametres() {
45
	private function definirValeurParDefautDesParametres() {
37
		if (isset($this->parametres['retour']) == false) {
-
 
38
			$this->parametres['retour'] = self::MIME_JSON;
-
 
39
		}
-
 
40
		if (isset($this->parametres['txt_format']) == false) {
46
		if (isset($this->parametres['txt_format']) == false) {
41
			$this->parametres['txt_format'] = 'txt';
47
			$this->parametres['txt_format'] = 'txt';
42
		}
48
		}
43
	}
49
	}
44
	
50
	
45
	private function verifierParametres($parametres) {
51
	private function verifierParametres($parametres) {
46
		$ok = true;
52
		$erreurs = array();
-
 
53
		
47
		if (isset($parametres['txt_format'])) {
54
		if (isset($parametres['txt_format'])) {
48
			if(!in_array($parametres['txt_format'], $this->formats_retour)) {
55
			if(!in_array($parametres['txt_format'], $this->formats_retour)) {
49
				$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 : txt et html.";
50
				$this->ajouterMessage($message);
57
				$erreurs[] = $message;
51
				$ok = false;
-
 
52
			} else {
-
 
53
				$this->retour = $parametres['txt_format'];
-
 
54
			}
58
			}
55
		}
59
		}
56
		
60
		
57
		if(isset($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";
58
			$this->section = $parametres['txt_section_position'];
63
			$erreurs[] = $message;
-
 
64
		}
-
 
65
		
-
 
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";
-
 
68
			$erreurs[] = $message;
-
 
69
		}
-
 
70
				
-
 
71
		if (count($erreurs) > 0) {
-
 
72
			$message = implode('<br />', $erreurs);
-
 
73
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
-
 
74
			throw new Exception($message, $code);
-
 
75
		}
-
 
76
	}
-
 
77
	
59
		}
78
	private function analyserParametres($ressources, $parametres) {	
60
		
79
		$this->pageNom = $ressources[0];
61
		if(isset($parametres['txt_section_titre'])) {
80
		if(isset($parametres['txt_section_titre'])) {
62
			$this->section = $parametres['txt_section_titre'];
81
			$this->section = $parametres['txt_section_titre'];
63
		}
82
		}
-
 
83
		if(isset($parametres['txt_section_position'])) {
-
 
84
			$this->section = $parametres['txt_section_position'];
64
		
85
		}
-
 
86
		if (isset($parametres['txt_format'])) {
65
		return $ok;
87
			$this->retour = $parametres['txt_format'];
-
 
88
		}
66
	}
89
	}
67
	
90
	
68
	private function consulterPage($page) {
91
	private function consulterPage($page) {
69
		$this->wiki = Registre::get('wikiApi');
92
		$this->wiki = Registre::get('wikiApi');
70
		$this->wiki->setPageCourante($this->pageNom);
93
		$this->wiki->setPageCourante($this->pageNom);
71
		$page = $this->wiki->LoadPage($page);
94
		$page = $this->wiki->LoadPage($page);
72
		
95
		
73
		// attention les wikis sont en ISO !
96
		// attention les wikis sont en ISO !
74
		if(Config::get('encodage_appli') != Config::get('encodage_wiki')) {
97
		if(Config::get('encodage_appli') != Config::get('encodage_wiki')) {
75
			$page["body"] = mb_convert_encoding($page['body'],Config::get('encodage_appli'),Config::get('encodage_wiki'));
98
			$page["body"] = mb_convert_encoding($page['body'],Config::get('encodage_appli'),Config::get('encodage_wiki'));
76
		}
99
		}
77
	
100
	
78
		if($this->section != null) {
101
		if($this->section != null) {
79
			$page["body"] = $this->découperPageSection($page["body"], $this->section);
102
			$page["body"] = $this->découperPageSection($page["body"], $this->section);
80
		}
103
		}
81
	
104
	
82
		return $page;
105
		return $page;
83
	}
106
	}
84
	
107
	
85
	private function découperPageSection($contenu_page, $section) {
108
	private function découperPageSection($contenu_page, $section) {
86
	
109
	
87
		$section_retour = '';
110
		$section_retour = '';
88
	
111
	
89
		if(is_numeric($section)) {
112
		if(is_numeric($section)) {
90
			$section_retour =  $this->getSectionParNumero($contenu_page, $section);
113
			$section_retour =  $this->getSectionParNumero($contenu_page, $section);
91
		} else {
114
		} else {
92
			$section_retour =  $this->getSectionParTitre($contenu_page, $section);
115
			$section_retour =  $this->getSectionParTitre($contenu_page, $section);
93
		}
116
		}
94
	
117
	
95
		return $section_retour;
118
		return $section_retour;
96
	}
119
	}
97
	
120
	
98
	public function getSectionParNumero($page, $num) {
121
	public function getSectionParNumero($page, $num) {
99
		preg_match_all('/(=[=]+[ ]*)(.[.^=]*)+[ ]*=[=]+[.]*/i', $page, $sections, PREG_OFFSET_CAPTURE);
122
		preg_match_all('/(=[=]+[ ]*)(.[.^=]*)+[ ]*=[=]+[.]*/i', $page, $sections, PREG_OFFSET_CAPTURE);
100
		$sectionTxt = '';
123
		$sectionTxt = '';
101
		$debut_section = 0;
124
		$debut_section = 0;
102
		$lg_page = strlen($page);
125
		$lg_page = strlen($page);
103
		$fin_section = $lg_page;
126
		$fin_section = $lg_page;
104
		
127
		
105
		if($num <= count($sections[1]) && $num > 0) {	
128
		if($num <= count($sections[1]) && $num > 0) {	
106
						
129
						
107
			$debut_section = $sections[1][$num - 1][1];
130
			$debut_section = $sections[1][$num - 1][1];
108
			$separateur = trim($sections[1][$num - 1][0]);
131
			$separateur = trim($sections[1][$num - 1][0]);
109
			$separateur_trouve = false;
132
			$separateur_trouve = false;
110
						
133
						
111
			for($i = $num; $i < count($sections[1]); $i++) {
134
			for($i = $num; $i < count($sections[1]); $i++) {
112
				$fin_section = $sections[1][$i][1];
135
				$fin_section = $sections[1][$i][1];
113
				if($separateur == trim($sections[1][$i][0])) {
136
				if($separateur == trim($sections[1][$i][0])) {
114
					$separateur_trouve = true;
137
					$separateur_trouve = true;
115
					break;
138
					break;
116
				}
139
				}
117
			}
140
			}
118
			
141
			
119
			$fin_section = $separateur_trouve ? $fin_section : $lg_page;
142
			$fin_section = $separateur_trouve ? $fin_section : $lg_page;
120
			$sectionTxt = substr($page, $debut_section, $fin_section - $debut_section);
143
			$sectionTxt = substr($page, $debut_section, $fin_section - $debut_section);
121
		} else {
144
		} else {
122
			$sectionTxt = '';
145
			$sectionTxt = '';
123
		}	
146
		}	
124
 
147
 
125
		return $sectionTxt;
148
		return $sectionTxt;
126
	}
149
	}
127
	
150
	
128
	public function getSectionParTitre($page, $titre) {
151
	public function getSectionParTitre($page, $titre) {
129
		$section = '';
152
		$section = '';
130
		$reg_exp = '/((=[=]+)[ ]*'.preg_quote(trim($titre), '/').'[ ]*=[=]+)[.]*/i';
153
		$reg_exp = '/((=[=]+)[ ]*'.preg_quote(trim($titre), '/').'[ ]*=[=]+)[.]*/i';
131
		$match = preg_split($reg_exp, $page, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
154
		$match = preg_split($reg_exp, $page, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
132
		
155
		
133
		if(count($match) > 3) {
156
		if(count($match) > 3) {
134
			$section = explode(trim($match[2]), $match[3], 2);
157
			$section = explode(trim($match[2]), $match[3], 2);
135
			$section = $match[1].' '.$section[0];
158
			$section = $match[1].' '.$section[0];
136
		} elseif(count($match) == 2) {
159
		} elseif(count($match) == 2) {
137
			$section = explode(trim($match[1]), $match[2], 2);
160
			$section = explode(trim($match[1]), $match[2], 2);
138
			$section = $match[0].' '.$section[0];
161
			$section = $match[0].' '.$section[0];
139
		} else {
162
		} else {
140
			$section = "";
163
			$section = "";
141
		}
164
		}
142
		
165
		
143
		return $section;
166
		return $section;
144
	}
167
	}
145
	
168
	
146
	private function formaterRetour($page) {
169
	private function formaterRetour($page) {
-
 
170
 
-
 
171
		$mime = null;
-
 
172
		$texte = '';
147
 
173
		
148
		switch($this->retour) {
174
		switch($this->retour) {
149
			case 'html':
175
			case 'html':
-
 
176
				$texte = $this->wiki->Format($page["body"], "wakka");
150
				$retour = $this->wiki->Format($page["body"], "wakka");
177
				$mime = self::MIME_HTML;
151
				break;
178
				break;
152
			default:
179
			default:
-
 
180
				$texte = $page["body"];
153
				$retour = $page["body"];
181
				$mime = self::MIME_TEXT;
-
 
182
		}
-
 
183
		
-
 
184
		$retour = array('id' => $this->pageNom,
-
 
185
				'titre' => $this->pageNom,
-
 
186
				'mime' => $mime,
-
 
187
				'texte' => $texte,
-
 
188
				'href' => '');
154
		}
189
		
155
		return $retour;
190
		return $retour;
156
	}
-
 
157
	
-
 
158
	private function formaterRetourHtml($retour) {
-
 
159
	
-
 
160
	}
191
	}
161
}	
192
}	
162
?>
193
?>