Subversion Repositories Applications.wikini

Rev

Rev 30 | Rev 33 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 30 Rev 31
Line 1... Line 1...
1
<?php
1
<?php
2
// declare(encoding='UTF-8');
2
// declare(encoding='UTF-8');
3
/**
3
/**
4
 * Classe d'exemple de service web du projet eFlore
-
 
5
 * Source des données : {NOM_DU_PROJET} {ADRESSE_WEB_DONNEES_DU_PROJET}
-
 
6
 * Paramètres du service :
-
 
7
 *  - param1 : explication de l'utilisation du param1
-
 
8
 *  - param2 : explication de l'utilisation du param2
4
 * Web service de consultation d'un page wiki
9
 * Exemple :
-
 
10
 * http://localhost/{CODE_DU_PROJET}/services/0.1/Exemple?param1=val1&param2=val2
-
 
11
 *
5
 *
12
 * @category	php 5.2
6
 * @category	php 5.2
13
 * @package		lion1906
7
 * @package		wapi
-
 
8
 * @author		Aurélien Peronnet < aurelien@tela-botanica.org>
14
 * @author		{PRENOM} {NOM}<{PRENOM}@tela-botanica.org>
9
 * @author		Jean-Pascal Milcent < jpm@tela-botanica.org>
15
 * @copyright	Copyright (c) 2011, Tela Botanica (accueil@tela-botanica.org)
10
 * @copyright	Copyright (c) 2011, Tela Botanica (accueil@tela-botanica.org)
16
 * @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
17
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
12
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
18
 * @version		$Id$
13
 * @version		$Id$
19
 */
14
 */
20
class Pages extends Service {
15
class Pages extends Service {
Line -... Line 16...
-
 
16
	
21
	
17
	private $wiki = null;
-
 
18
	private $pageNom = null;
-
 
19
	private $section = null;
22
	private $pageNom = null;
20
	
-
 
21
	private $retour = 'txt';
Line 23... Line 22...
23
	private $retour = 'html';
22
	private $formats_retour = array('txt','html');
-
 
23
	
24
	
24
	public function consulter($ressources, $parametres) {
25
	public function consulter($ressources, $parametres) {
25
		header('Content-type: text/plain');
26
		$verifOk = $this->verifierParametres($parametres);
-
 
27
		if ($verifOk) {
26
		$verifOk = $this->verifierParametres($parametres);
28
			// Débuter ici le code du service
-
 
29
			$this->pageNom = $ressources[0];
-
 
30
			$wiki = Registre::get('wikiApi');
27
		if ($verifOk) {
31
			$wiki->setPageCourrante($this->pageNom);
-
 
32
			$page = $wiki->LoadPage($ressources[0]);
28
			$this->pageNom = $ressources[0];
33
			if ($this->retour == 'html') {
-
 
34
				$retour = $wiki->Format($page["body"], "wakka");
-
 
35
			} else {
-
 
36
				$retour = $page["body"];
-
 
37
			}
29
			$page = $this->consulterPage($ressources[0]);
38
			return $retour;
30
			return $this->formaterRetour($page);
-
 
31
		} else {
-
 
32
			RestServeur::envoyerEnteteStatutHttp(RestServeur::HTTP_CODE_MAUVAISE_REQUETE);		
39
		} else {
33
		}
-
 
34
	}
-
 
35
	
-
 
36
	private function definirValeurParDefautDesParametres() {
-
 
37
		if (isset($this->parametres['retour']) == false) {
-
 
38
			$this->parametres['retour'] = self::MIME_JSON;
-
 
39
		}
40
			RestServeur::envoyerEnteteStatutHttp(RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
40
		if (isset($this->parametres['txt_format']) == false) {
41
			
41
			$this->parametres['txt_format'] = 'txt';
Line 42... Line 42...
42
		}
42
		}
43
	}
43
	}
44
	
-
 
45
	private function verifierParametres($parametres) {
44
	
46
		$ok = true;
45
	private function verifierParametres($parametres) {
47
		extract($parametres);
46
		$ok = true;
48
		if (isset($retour) ) {
47
		if (isset($parametres['txt_format'])) {
49
			if (!preg_match('/^(wiki|html)$/', $retour)) {
48
			if(!in_array($parametres['txt_format'], $this->formats_retour)) {
50
				$message = "La valeur du paramètre 'retour' peut seulement prendre les valeurs : wiki et html.";
49
				$message = "La valeur du paramètre 'txt.format' peut seulement prendre les valeurs : txt et html.";
51
				$this->ajouterMessage($message);
50
				$this->ajouterMessage($message);
52
				$ok = false;
51
				$ok = false;
53
			} else {
52
			} else {
-
 
53
				$this->retour = $parametres['txt_format'];
-
 
54
			}
-
 
55
		}
-
 
56
		
-
 
57
		if(isset($parametres['txt_section_position'])) {
-
 
58
			$this->section = $parametres['txt_section_position'];
-
 
59
		}
-
 
60
		
-
 
61
		if(isset($parametres['txt_section_titre'])) {
54
				$this->retour = $retour;
62
			$this->section = $parametres['txt_section_titre'];
55
			}
63
		}
-
 
64
		
-
 
65
		return $ok;
-
 
66
	}
-
 
67
	
-
 
68
	private function consulterPage($page) {
-
 
69
		$this->wiki = Registre::get('wikiApi');
-
 
70
		$this->wiki->setPageCourante($this->pageNom);
-
 
71
		$page = $this->wiki->LoadPage($page);
-
 
72
		
-
 
73
		// attention les wikis sont en ISO !
-
 
74
		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'));
-
 
76
		}
-
 
77
	
-
 
78
		if($this->section != null) {
-
 
79
			$page["body"] = $this->découperPageSection($page["body"], $this->section);
-
 
80
		}
-
 
81
	
-
 
82
		return $page;
-
 
83
	}
-
 
84
	
-
 
85
	private function découperPageSection($contenu_page, $section) {
-
 
86
	
-
 
87
		$section_retour = '';
-
 
88
	
-
 
89
		if(is_numeric($section)) {
-
 
90
			$section_retour =  $this->getSectionParNumero($contenu_page, $section);
-
 
91
		} else {
-
 
92
			$section_retour =  $this->getSectionParTitre($contenu_page, $section);
-
 
93
		}
-
 
94
	
-
 
95
		return $section_retour;
-
 
96
	}
-
 
97
	
-
 
98
	public function getSectionParNumero($page, $num) {
-
 
99
		preg_match_all('/(=[=]+[ ]*)(.[.^=]*)+[ ]*=[=]+[.]*/i', $page, $sections, PREG_OFFSET_CAPTURE);
-
 
100
		$sectionTxt = '';
-
 
101
		$debut_section = 0;
-
 
102
		$lg_page = strlen($page);
-
 
103
		$fin_section = $lg_page;
-
 
104
		
-
 
105
		if($num <= count($sections[1]) && $num > 0) {	
-
 
106
						
-
 
107
			$debut_section = $sections[1][$num - 1][1];
-
 
108
			$separateur = trim($sections[1][$num - 1][0]);
-
 
109
			$separateur_trouve = false;
-
 
110
						
-
 
111
			for($i = $num; $i < count($sections[1]); $i++) {
-
 
112
				$fin_section = $sections[1][$i][1];
-
 
113
				if($separateur == trim($sections[1][$i][0])) {
-
 
114
					$separateur_trouve = true;
-
 
115
					break;
-
 
116
				}
-
 
117
			}
-
 
118
			
-
 
119
			$fin_section = $separateur_trouve ? $fin_section : $lg_page;
-
 
120
			$sectionTxt = substr($page, $debut_section, $fin_section - $debut_section);
-
 
121
		} else {
-
 
122
			$sectionTxt = '';
-
 
123
		}	
-
 
124
 
-
 
125
		return $sectionTxt;
-
 
126
	}
-
 
127
	
-
 
128
	public function getSectionParTitre($page, $titre) {
-
 
129
		$section = '';
-
 
130
		$reg_exp = '/((=[=]+)[ ]*'.preg_quote(trim($titre), '/').'[ ]*=[=]+)[.]*/i';
-
 
131
		$match = preg_split($reg_exp, $page, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
-
 
132
		
-
 
133
		if(count($match) > 3) {
-
 
134
			$section = explode(trim($match[2]), $match[3], 2);
-
 
135
			$section = $match[1].' '.$section[0];
-
 
136
		} elseif(count($match) == 2) {
-
 
137
			$section = explode(trim($match[1]), $match[2], 2);
-
 
138
			$section = $match[0].' '.$section[0];
-
 
139
		} else {
-
 
140
			$section = "";
-
 
141
		}
-
 
142
		
-
 
143
		return $section;
-
 
144
	}
-
 
145
	
-
 
146
	private function formaterRetour($page) {
-
 
147
 
-
 
148
		switch($this->retour) {
-
 
149
			case 'html':
-
 
150
				$retour = $this->wiki->Format($page["body"], "wakka");
-
 
151
				break;
-
 
152
			default:
-
 
153
				$retour = $page["body"];
-
 
154
		}
-
 
155
		return $retour;
-
 
156
	}
-
 
157
	
56
		}
158
	private function formaterRetourHtml($retour) {
57
		return $ok;
159
	
58
	}
160
	}