summaryrefslogtreecommitdiff
path: root/bridges/WikipediaBridge.php
blob: 7ca763fcc68a887f6e6c47d5a102fb8c1fbc2026 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php

define('WIKIPEDIA_SUBJECT_TFA', 0); // Today's featured article
define('WIKIPEDIA_SUBJECT_DYK', 1); // Did you know...

class WikipediaBridge extends BridgeAbstract {
	const MAINTAINER = 'logmanoriginal';
	const NAME = 'Wikipedia bridge for many languages';
	const URI = 'https://www.wikipedia.org/';
	const DESCRIPTION = 'Returns articles for a language of your choice';

	const PARAMETERS = array( array(
		'language' => array(
			'name' => 'Language',
			'type' => 'list',
			'title' => 'Select your language',
			'exampleValue' => 'English',
			'values' => array(
				'English' => 'en',
				'Dutch' => 'nl',
				'Esperanto' => 'eo',
				'French' => 'fr',
				'German' => 'de',
			)
		),
		'subject' => array(
			'name' => 'Subject',
			'type' => 'list',
			'title' => 'What subject are you interested in?',
			'exampleValue' => 'Today\'s featured article',
			'values' => array(
				'Today\'s featured article' => 'tfa',
				'Did you know…' => 'dyk'
			)
		),
		'fullarticle' => array(
			'name' => 'Load full article',
			'type' => 'checkbox',
			'title' => 'Activate to always load the full article'
		)
	));

	public function getURI(){
		if(!is_null($this->getInput('language'))) {
			return 'https://'
			. strtolower($this->getInput('language'))
			. '.wikipedia.org';
		}

		return parent::getURI();
	}

	public function getName(){
		switch($this->getInput('subject')) {
			case 'tfa':
				$subject = WIKIPEDIA_SUBJECT_TFA;
				break;
			case 'dyk':
				$subject = WIKIPEDIA_SUBJECT_DYK;
				break;
			default: return parent::getName();
		}

		switch($subject) {
			case WIKIPEDIA_SUBJECT_TFA:
				$name = 'Today\'s featured article from '
				. strtolower($this->getInput('language'))
				. '.wikipedia.org';
				break;
			case WIKIPEDIA_SUBJECT_DYK:
				$name = 'Did you know? - articles from '
				. strtolower($this->getInput('language'))
				. '.wikipedia.org';
				break;
			default:
				$name = 'Articles from '
				. strtolower($this->getInput('language'))
				. '.wikipedia.org';
				break;
		}
		return $name;
	}

	public function collectData(){

		switch($this->getInput('subject')) {
			case 'tfa':
				$subject = WIKIPEDIA_SUBJECT_TFA;
				break;
			case 'dyk':
				$subject = WIKIPEDIA_SUBJECT_DYK;
				break;
			default:
				$subject = WIKIPEDIA_SUBJECT_TFA;
				break;
		}

		$fullArticle = $this->getInput('fullarticle');

		// This will automatically send us to the correct main page in any language (try it!)
		$html = getSimpleHTMLDOM($this->getURI() . '/wiki');

		if(!$html)
			returnServerError('Could not load site: ' . $this->getURI() . '!');

		/*
		* Now read content depending on the language (make sure to create one function per language!)
		* We build the function name automatically, just make sure you create a private function ending
		* with your desired language code, where the language code is upper case! (en -> getContentsEN).
		*/
		$function = 'getContents' . ucfirst(strtolower($this->getInput('language')));

		if(!method_exists($this, $function))
			returnServerError('A function to get the contents for your language is missing (\'' . $function . '\')!');

		/*
		* The method takes care of creating all items.
		*/
		$this->$function($html, $subject, $fullArticle);
	}

	/**
	* Replaces all relative URIs with absolute ones
	* @param $element A simplehtmldom element
	* @return The $element->innertext with all URIs replaced
	*/
	private function replaceUriInHtmlElement($element){
		return str_replace('href="/', 'href="' . $this->getURI() . '/', $element->innertext);
	}

	/*
	* Adds a new item to $items using a generic operation (should work for most
	* (all?) wikis) $anchorText can be specified if the wiki in question doesn't
	* use '...' (like Dutch, French and Italian) $anchorFallbackIndex can be
	* used to specify a different fallback link than the first
	* (e.g., -1 for the last)
	*/
	private function addTodaysFeaturedArticleGeneric($element,
	$fullArticle,
	$anchorText = '...',
	$anchorFallbackIndex = 0){
		// Clean the bottom of the featured article
		if ($element->find('div', -1))
			$element->find('div', -1)->outertext = '';

		// The title and URI of the article can be found in an anchor containing
		// the string '...' in most wikis ('full article ...')
		$target = $element->find('p/a', $anchorFallbackIndex);
		foreach($element->find('//a') as $anchor) {
			if(strpos($anchor->innertext, $anchorText) !== false) {
				$target = $anchor;
				break;
			}
		}

		$item = array();
		$item['uri'] = $this->getURI() . $target->href;
		$item['title'] = $target->title;

		if(!$fullArticle)
			$item['content'] = strip_tags($this->replaceUriInHtmlElement($element), '<a><p><br><img>');
		else
			$item['content'] = $this->loadFullArticle($item['uri']);

		$this->items[] = $item;
	}

	/*
	* Adds a new item to $items using a generic operation (should work for most (all?) wikis)
	*/
	private function addDidYouKnowGeneric($element, $fullArticle){
		foreach($element->find('ul', 0)->find('li') as $entry) {
			$item = array();

			// We can only use the first anchor, there is no way of finding the 'correct' one if there are multiple
			$item['uri'] = $this->getURI() . $entry->find('a', 0)->href;
			$item['title'] = strip_tags($entry->innertext);

			if(!$fullArticle)
				$item['content'] = $this->replaceUriInHtmlElement($entry);
			else
				$item['content'] = $this->loadFullArticle($item['uri']);

			$this->items[] = $item;
		}
	}

	/**
	* Loads the full article from a given URI
	*/
	private function loadFullArticle($uri){
		$content_html = getSimpleHTMLDOMCached($uri);

		if(!$content_html)
			returnServerError('Could not load site: ' . $uri . '!');

		$content = $content_html->find('#mw-content-text', 0);

		if(!$content)
			returnServerError('Could not find content in page: ' . $uri . '!');

		// Let's remove a couple of things from the article
		$table = $content->find('#toc', 0); // Table of contents
		if(!$table === false)
			$table->outertext = '';

		foreach($content->find('ol.references') as $reference) // References
			$reference->outertext = '';

		return str_replace('href="/', 'href="' . $this->getURI() . '/', $content->innertext);
	}

	/**
	* Implementation for de.wikipedia.org
	*/
	private function getContentsDe($html, $subject, $fullArticle){
		switch($subject) {
			case WIKIPEDIA_SUBJECT_TFA:
				$element = $html->find('div[id=mf-tfa]', 0);
				$this->addTodaysFeaturedArticleGeneric($element, $fullArticle);
				break;
			case WIKIPEDIA_SUBJECT_DYK:
				$element = $html->find('div[id=mf-dyk]', 0);
				$this->addDidYouKnowGeneric($element, $fullArticle);
				break;
			default:
				break;
		}
	}

	/**
	* Implementation for fr.wikipedia.org
	*/
	private function getContentsFr($html, $subject, $fullArticle){
		switch($subject) {
			case WIKIPEDIA_SUBJECT_TFA:
				$element = $html->find('div[class=accueil_2017_cadre]', 0);
				$this->addTodaysFeaturedArticleGeneric($element, $fullArticle, 'Lire la suite');
				break;
			case WIKIPEDIA_SUBJECT_DYK:
				$element = $html->find('div[class=accueil_2017_cadre]', 2);
				$this->addDidYouKnowGeneric($element, $fullArticle);
				break;
			default:
				break;
		}
	}

	/**
	* Implementation for en.wikipedia.org
	*/
	private function getContentsEn($html, $subject, $fullArticle){
		switch($subject) {
			case WIKIPEDIA_SUBJECT_TFA:
				$element = $html->find('div[id=mp-tfa]', 0);
				$this->addTodaysFeaturedArticleGeneric($element, $fullArticle);
				break;
			case WIKIPEDIA_SUBJECT_DYK:
				$element = $html->find('div[id=mp-dyk]', 0);
				$this->addDidYouKnowGeneric($element, $fullArticle);
				break;
			default:
				break;
		}
	}

	/**
	* Implementation for eo.wikipedia.org
	*/
	private function getContentsEo($html, $subject, $fullArticle){
		switch($subject) {
			case WIKIPEDIA_SUBJECT_TFA:
				$element = $html->find('div[id=mf-artikolo-de-la-semajno]', 0);
				$this->addTodaysFeaturedArticleGeneric($element, $fullArticle);
				break;
			case WIKIPEDIA_SUBJECT_DYK:
				$element = $html->find('div[id=mw-content-text]', 0)->find('table', 4)->find('td', 4);
				$this->addDidYouKnowGeneric($element, $fullArticle);
				break;
			default:
				break;
		}
	}

	/**
	* Implementation for nl.wikipedia.org
	*/
	private function getContentsNl($html, $subject, $fullArticle){
		switch($subject) {
			case WIKIPEDIA_SUBJECT_TFA:
				$element = $html->find('div[id=mf-uitgelicht]', 0);
				$this->addTodaysFeaturedArticleGeneric($element, $fullArticle, 'Lees meer');
				break;
			case WIKIPEDIA_SUBJECT_DYK:
				$element = $html->find('div[id=mw-content-text]', 0)->find('table', 4)->find('td', 2);
				$this->addDidYouKnowGeneric($element, $fullArticle);
				break;
			default:
				break;
		}
	}
}