Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 175 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 175 Rev 176
1
<?php
1
<?php
2
class Wikipedia {
2
class WikipediaBot {
3
	const HTTP_URL_REQUETE_SEPARATEUR = '&';
3
	const HTTP_URL_REQUETE_SEPARATEUR = '&';
4
	private $langue = 'fr';
4
	private $langue = 'fr';
5
	private $url = '';
5
	private $url = '';
6
	private $parametres = array();
6
	private $parametres = array();
7
	private $titre = '';
7
	private $titre = '';
8
	private $txt = '';
8
	private $txt = '';
9
	private $userAgent = 'eFloreBot v0.1';
9
	private $userAgent = 'eFloreBot v0.1';
10
	private $reponse_entetes = null;
10
	private $reponse_entetes = null;
11
 
11
 
12
	public function __construct($langue = null) {
12
	public function __construct($langue = null) {
13
		if (is_string($langue)) {
13
		if (is_string($langue)) {
14
			$this->langue = strtolower($langue);
14
			$this->langue = strtolower($langue);
15
		}
15
		}
16
	}
16
	}
17
 
17
 
18
	public function chargerPage($article) {
18
	public function chargerPage($article) {
19
		$this->initialiserRequete();
19
		$this->initialiserRequete();
20
		$this->url = $this->getBaseApiURL();
20
		$this->url = $this->getBaseApiURL();
21
		$this->parametres = array(
21
		$this->parametres = array(
22
			'action' => 'query',
22
			'action' => 'query',
23
			'prop' => 'revisions',
23
			'prop' => 'revisions',
24
			'titles' => $article,
24
			'titles' => $article,
25
			'rvprop' => 'content',
25
			'rvprop' => 'content',
26
			'redirects' => 1
26
			'redirects' => 1
27
		);
27
		);
28
		$this->resultats = $this->consulterAPI();
28
		$this->resultats = $this->consulterAPI();
29
		$sxGetAID = $this->resultats['query']['pages'];
29
		$sxGetAID = $this->resultats['query']['pages'];
30
		$sxGetAID = array_shift($sxGetAID);
30
		$sxGetAID = array_shift($sxGetAID);
31
		$this->titre = $sxGetAID['title'];
31
		$this->titre = $sxGetAID['title'];
32
		$this->txt = $sxGetAID['revisions'][0]['*'];
32
		$this->txt = $sxGetAID['revisions'][0]['*'];
33
	}
33
	}
34
 
34
 
35
	public function getPageTitre() {
35
	public function getPageTitre() {
36
		return $this->titre;
36
		return $this->titre;
37
	}
37
	}
38
 
38
 
39
	public function getPageTxt() {
39
	public function getPageTxt() {
40
		return $this->txt;
40
		return $this->txt;
41
	}
41
	}
42
 
42
 
43
	public function getTaxobox() {
43
	public function getTaxobox() {
44
		$taxobox = '';
44
		$taxobox = '';
45
		if (preg_match('/([{]{2}Taxobox début.+[{]{2}Taxobox fin[}]{2})/s', $this->txt, $match)) {
45
		if (preg_match('/([{]{2}Taxobox début.+[{]{2}Taxobox fin[}]{2})/s', $this->txt, $match)) {
46
			$taxobox = $match[1];
46
			$taxobox = $match[1];
47
		}
47
		}
48
		return $taxobox;
48
		return $taxobox;
49
	}
49
	}
50
 
50
 
51
	public function extraireTaxobox() {
51
	public function extraireTaxobox() {
52
		$taxobox = $this->getTaxobox();
52
		$taxobox = $this->getTaxobox();
53
		$this->txt = str_replace($taxobox, '', $this->txt);
53
		$this->txt = str_replace($taxobox, '', $this->txt);
54
		return $taxobox;
54
		return $taxobox;
55
	}
55
	}
56
 
56
 
57
	public function getSectionParNumero($num) {
57
	public function getSectionParNumero($num) {
58
		$sections = preg_split('/[=]{2}[^=]+[=]{2}/U', $this->txt);
58
		$sections = preg_split('/[=]{2}[^=]+[=]{2}/U', $this->txt);
59
		Debug::printr($sections);
59
		Debug::printr($sections);
60
		$sectionTxt = isset($sections[$num]) ? $sections[$num] : '';
60
		$sectionTxt = isset($sections[$num]) ? $sections[$num] : '';
61
		return $sectionTxt;
61
		return $sectionTxt;
62
	}
62
	}
63
 
63
 
64
	public function getSectionParTitre($titre) {
64
	public function getSectionParTitre($titre) {
65
		$section = '';
65
		$section = '';
66
		if (preg_match('/[=]{2} '.$titre.' [=]{2}(.*)\n\n/sU', $this->txt, $match)) {
66
		if (preg_match('/[=]{2} '.$titre.' [=]{2}(.*)\n\n/sU', $this->txt, $match)) {
67
			$section = $match[1];
67
			$section = $match[1];
68
		}
68
		}
69
		return $section;
69
		return $section;
70
	}
70
	}
71
 
71
 
72
	public function rendre($wikitxt) {
72
	public function rendre($wikitxt) {
73
		$wikitxt .= '<references />';
73
		$wikitxt .= '<references />';
74
		$this->initialiserRequete();
74
		$this->initialiserRequete();
75
		$this->url = $this->getBaseApiURL();
75
		$this->url = $this->getBaseApiURL();
76
		$this->parametres = array(
76
		$this->parametres = array(
77
					'action' => 'parse',
77
					'action' => 'parse',
78
					'prop' => 'text',
78
					'prop' => 'text',
79
					'text' => $wikitxt
79
					'text' => $wikitxt
80
		);
80
		);
81
		$this->resultats = $this->consulterAPI();
81
		$this->resultats = $this->consulterAPI();
82
		$txt = $this->resultats['parse']['text']['*'];
82
		$txt = $this->resultats['parse']['text']['*'];
83
		$txt = $this->remplacerUrls($txt);
83
		$txt = $this->remplacerUrls($txt);
84
		return $txt;
84
		return $txt;
85
	}
85
	}
86
 
86
 
87
	private function initialiserRequete() {
87
	private function initialiserRequete() {
88
		$this->url = '';
88
		$this->url = '';
89
		$this->parametres = array();
89
		$this->parametres = array();
90
		$this->resultats = array();
90
		$this->resultats = array();
91
	}
91
	}
92
 
92
 
93
	private function getBaseWpURL() {
93
	private function getBaseWpURL() {
94
		$baseURL = "http://{$this->langue}.wikipedia.org";
94
		$baseURL = "http://{$this->langue}.wikipedia.org";
95
		return $baseURL;
95
		return $baseURL;
96
	}
96
	}
97
 
97
 
98
	private function getBaseApiURL() {
98
	private function getBaseApiURL() {
99
		$baseURL = $this->getBaseWpURL().'/w/api.php';
99
		$baseURL = $this->getBaseWpURL().'/w/api.php';
100
		return $baseURL;
100
		return $baseURL;
101
	}
101
	}
102
 
102
 
103
	private function consulterAPI() {
103
	private function consulterAPI() {
104
		$this->parametres['format'] = 'php';
104
		$this->parametres['format'] = 'php';
105
		$resultat = $this->consulterEnPost();
105
		$resultat = $this->consulterEnPost();
106
		$resultat = unserialize($resultat);
106
		$resultat = unserialize($resultat);
107
		if (isset($resultat['error'])) {
107
		if (isset($resultat['error'])) {
108
			throw new Exception($resultat['error']['info'], $resultat['error']['info']);
108
			throw new Exception($resultat['error']['info'], $resultat['error']['info']);
109
		}
109
		}
110
		return $resultat;
110
		return $resultat;
111
	}
111
	}
112
 
112
 
113
	private function consulterEnPost() {
113
	private function consulterEnPost() {
114
		return $this->consulter('POST');
114
		return $this->consulter('POST');
115
	}
115
	}
116
 
116
 
117
	private function consulter($mode) {
117
	private function consulter($mode) {
118
		$entetes = array(
118
		$entetes = array(
119
				'Content-type' => 'application/x-www-form-urlencoded',
119
				'Content-type' => 'application/x-www-form-urlencoded',
120
				'User-Agent' => $this->userAgent);
120
				'User-Agent' => $this->userAgent);
121
		$contexte = stream_context_create(array(
121
		$contexte = stream_context_create(array(
122
				'http' => array(
122
				'http' => array(
123
					'method' => $mode,
123
					'method' => $mode,
124
					'header' => $this->getEnteteChaine($entetes),
124
					'header' => $this->getEnteteChaine($entetes),
125
					'content' => http_build_query($this->parametres, null, self::HTTP_URL_REQUETE_SEPARATEUR))));
125
					'content' => http_build_query($this->parametres, null, self::HTTP_URL_REQUETE_SEPARATEUR))));
126
		$flux = fopen($this->url, 'r', false, $contexte);
126
		$flux = fopen($this->url, 'r', false, $contexte);
127
		if (!$flux) {
127
		if (!$flux) {
128
			$this->reponse_entetes = $http_response_header;
128
			$this->reponse_entetes = $http_response_header;
129
			$e = "L'ouverture de l'url '{$this->url}' par la méthode HTTP '$mode' a échoué!";
129
			$e = "L'ouverture de l'url '{$this->url}' par la méthode HTTP '$mode' a échoué!";
130
			throw new Exception($e);
130
			throw new Exception($e);
131
		}
131
		}
132
		// Informations sur les en-têtes et métadonnées du flux
132
		// Informations sur les en-têtes et métadonnées du flux
133
		$this->reponse_entetes = stream_get_meta_data($flux);
133
		$this->reponse_entetes = stream_get_meta_data($flux);
134
		// Contenu actuel de $url
134
		// Contenu actuel de $url
135
		$contenu = stream_get_contents($flux);
135
		$contenu = stream_get_contents($flux);
136
		fclose($flux);
136
		fclose($flux);
137
		return $contenu;
137
		return $contenu;
138
	}
138
	}
139
 
139
 
140
	private function getEnteteChaine(Array $entetes) {
140
	private function getEnteteChaine(Array $entetes) {
141
		$entetesCleVal = array();
141
		$entetesCleVal = array();
142
		foreach ($entetes as $cle => $valeur) {
142
		foreach ($entetes as $cle => $valeur) {
143
			$entetesCleVal[] = $cle.': '.$valeur;
143
			$entetesCleVal[] = $cle.': '.$valeur;
144
		}
144
		}
145
		return implode("\r\n", $entetesCleVal);
145
		return implode("\r\n", $entetesCleVal);
146
	}
146
	}
147
 
147
 
148
	private function remplacerUrls($txt) {
148
	private function remplacerUrls($txt) {
149
		$remplacements = array(
149
		$remplacements = array(
150
			'href="/wiki/' => 'href="'.$this->getBaseWpURL().'/wiki/',
150
			'href="/wiki/' => 'href="'.$this->getBaseWpURL().'/wiki/',
151
			'href="/w/' => 'href="'.$this->getBaseWpURL().'/w/');
151
			'href="/w/' => 'href="'.$this->getBaseWpURL().'/w/');
152
		$txt = strtr($txt, $remplacements);
152
		$txt = strtr($txt, $remplacements);
153
		return $txt;
153
		return $txt;
154
	}
154
	}
155
}
155
}
156
?>
156
?>