2016-08-07 00:06:14 +02:00
|
|
|
<?php
|
2016-08-07 12:51:09 +02:00
|
|
|
|
2016-08-07 13:26:30 +02:00
|
|
|
define('WIKIPEDIA_SUBJECT_TFA', 0); // Today's featured article
|
2016-08-07 12:51:09 +02:00
|
|
|
define('WIKIPEDIA_SUBJECT_DYK', 1); // Did you know...
|
|
|
|
|
2016-09-10 19:11:09 +02:00
|
|
|
class WikipediaBridge extends BridgeAbstract {
|
2016-08-30 11:23:55 +02:00
|
|
|
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';
|
2016-08-07 00:06:14 +02:00
|
|
|
|
2016-08-30 11:23:55 +02:00
|
|
|
const PARAMETERS = array( array(
|
2017-02-11 16:16:56 +01:00
|
|
|
'language' => array(
|
|
|
|
'name' => 'Language',
|
|
|
|
'type' => 'list',
|
|
|
|
'title' => 'Select your language',
|
|
|
|
'exampleValue' => 'English',
|
|
|
|
'values' => array(
|
|
|
|
'English' => 'en',
|
|
|
|
'Dutch' => 'nl',
|
|
|
|
'Esperanto' => 'eo',
|
|
|
|
'French' => 'fr',
|
|
|
|
'German' => 'de',
|
2016-08-27 21:03:26 +02:00
|
|
|
)
|
|
|
|
),
|
2017-02-11 16:16:56 +01:00
|
|
|
'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'
|
2016-08-27 21:03:26 +02:00
|
|
|
)
|
|
|
|
),
|
2017-02-11 16:16:56 +01:00
|
|
|
'fullarticle' => array(
|
|
|
|
'name' => 'Load full article',
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'title' => 'Activate to always load the full article'
|
2016-08-27 21:03:26 +02:00
|
|
|
)
|
|
|
|
));
|
2016-08-07 00:06:14 +02:00
|
|
|
|
2016-08-27 21:03:26 +02:00
|
|
|
public function getURI(){
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!is_null($this->getInput('language'))) {
|
2017-02-14 22:36:33 +01:00
|
|
|
return 'https://'
|
|
|
|
. strtolower($this->getInput('language'))
|
|
|
|
. '.wikipedia.org';
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getURI();
|
2017-02-11 16:16:56 +01:00
|
|
|
}
|
2016-08-27 18:39:57 +02:00
|
|
|
|
2016-08-27 21:03:26 +02:00
|
|
|
public function getName(){
|
2017-07-29 19:28:00 +02:00
|
|
|
switch($this->getInput('subject')) {
|
2016-08-07 12:51:09 +02:00
|
|
|
case 'tfa':
|
|
|
|
$subject = WIKIPEDIA_SUBJECT_TFA;
|
|
|
|
break;
|
|
|
|
case 'dyk':
|
|
|
|
$subject = WIKIPEDIA_SUBJECT_DYK;
|
|
|
|
break;
|
2017-02-14 22:20:55 +01:00
|
|
|
default: return parent::getName();
|
2016-08-07 12:51:09 +02:00
|
|
|
}
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
switch($subject) {
|
2016-08-07 12:51:09 +02:00
|
|
|
case WIKIPEDIA_SUBJECT_TFA:
|
2017-02-11 16:16:56 +01:00
|
|
|
$name = 'Today\'s featured article from '
|
|
|
|
. strtolower($this->getInput('language'))
|
|
|
|
. '.wikipedia.org';
|
2016-08-07 12:51:09 +02:00
|
|
|
break;
|
|
|
|
case WIKIPEDIA_SUBJECT_DYK:
|
2017-02-11 16:16:56 +01:00
|
|
|
$name = 'Did you know? - articles from '
|
|
|
|
. strtolower($this->getInput('language'))
|
|
|
|
. '.wikipedia.org';
|
2016-08-07 12:51:09 +02:00
|
|
|
break;
|
|
|
|
default:
|
2017-02-11 16:16:56 +01:00
|
|
|
$name = 'Articles from '
|
|
|
|
. strtolower($this->getInput('language'))
|
|
|
|
. '.wikipedia.org';
|
2016-08-07 12:51:09 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-02-11 16:16:56 +01:00
|
|
|
return $name;
|
|
|
|
}
|
2016-08-27 18:39:57 +02:00
|
|
|
|
2016-08-25 01:24:53 +02:00
|
|
|
public function collectData(){
|
2016-08-22 01:25:56 +02:00
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
switch($this->getInput('subject')) {
|
2016-08-07 12:51:09 +02:00
|
|
|
case 'tfa':
|
|
|
|
$subject = WIKIPEDIA_SUBJECT_TFA;
|
|
|
|
break;
|
|
|
|
case 'dyk':
|
|
|
|
$subject = WIKIPEDIA_SUBJECT_DYK;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$subject = WIKIPEDIA_SUBJECT_TFA;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-08-28 01:25:33 +02:00
|
|
|
$fullArticle = $this->getInput('fullarticle');
|
2016-08-07 00:06:14 +02:00
|
|
|
|
|
|
|
// This will automatically send us to the correct main page in any language (try it!)
|
2016-09-25 23:22:33 +02:00
|
|
|
$html = getSimpleHTMLDOM($this->getURI() . '/wiki');
|
2016-08-07 00:06:14 +02:00
|
|
|
|
|
|
|
if(!$html)
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('Could not load site: ' . $this->getURI() . '!');
|
2016-08-07 00:06:14 +02:00
|
|
|
|
2016-08-22 01:25:56 +02:00
|
|
|
/*
|
2016-08-07 00:06:14 +02:00
|
|
|
* 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
|
2017-02-11 16:16:56 +01:00
|
|
|
* with your desired language code, where the language code is upper case! (en -> getContentsEN).
|
2016-08-07 00:06:14 +02:00
|
|
|
*/
|
2017-02-11 16:16:56 +01:00
|
|
|
$function = 'getContents' . ucfirst(strtolower($this->getInput('language')));
|
2016-08-07 00:06:14 +02:00
|
|
|
|
|
|
|
if(!method_exists($this, $function))
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('A function to get the contents for your language is missing (\'' . $function . '\')!');
|
2016-08-22 01:25:56 +02:00
|
|
|
|
2016-08-07 00:06:14 +02:00
|
|
|
/*
|
|
|
|
* The method takes care of creating all items.
|
|
|
|
*/
|
2016-08-07 12:51:09 +02:00
|
|
|
$this->$function($html, $subject, $fullArticle);
|
2016-08-07 00:06:14 +02:00
|
|
|
}
|
|
|
|
|
2016-08-07 12:51:09 +02:00
|
|
|
/**
|
|
|
|
* Replaces all relative URIs with absolute ones
|
|
|
|
* @param $element A simplehtmldom element
|
|
|
|
* @return The $element->innertext with all URIs replaced
|
|
|
|
*/
|
2017-02-11 16:16:56 +01:00
|
|
|
private function replaceUriInHtmlElement($element){
|
2016-08-27 18:39:57 +02:00
|
|
|
return str_replace('href="/', 'href="' . $this->getURI() . '/', $element->innertext);
|
2016-08-07 12:51:09 +02:00
|
|
|
}
|
|
|
|
|
2016-08-07 00:06:14 +02:00
|
|
|
/*
|
2017-02-11 16:16:56 +01:00
|
|
|
* 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)
|
2016-08-07 00:06:14 +02:00
|
|
|
*/
|
2017-02-14 17:28:07 +01:00
|
|
|
private function addTodaysFeaturedArticleGeneric($element,
|
|
|
|
$fullArticle,
|
|
|
|
$anchorText = '...',
|
|
|
|
$anchorFallbackIndex = 0){
|
2016-08-07 00:06:14 +02:00
|
|
|
// Clean the bottom of the featured article
|
2016-08-28 11:22:37 +02:00
|
|
|
if ($element->find('div', -1))
|
|
|
|
$element->find('div', -1)->outertext = '';
|
2016-08-07 00:06:14 +02:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
// The title and URI of the article can be found in an anchor containing
|
|
|
|
// the string '...' in most wikis ('full article ...')
|
2016-08-28 11:22:37 +02:00
|
|
|
$target = $element->find('p/a', $anchorFallbackIndex);
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($element->find('//a') as $anchor) {
|
|
|
|
if(strpos($anchor->innertext, $anchorText) !== false) {
|
2016-08-07 11:26:45 +02:00
|
|
|
$target = $anchor;
|
|
|
|
break;
|
2016-08-22 01:25:56 +02:00
|
|
|
}
|
2016-08-07 11:26:45 +02:00
|
|
|
}
|
|
|
|
|
2016-08-22 18:55:59 +02:00
|
|
|
$item = array();
|
2016-08-27 18:39:57 +02:00
|
|
|
$item['uri'] = $this->getURI() . $target->href;
|
2016-08-22 18:55:59 +02:00
|
|
|
$item['title'] = $target->title;
|
2016-08-07 00:06:14 +02:00
|
|
|
|
|
|
|
if(!$fullArticle)
|
2017-02-11 16:16:56 +01:00
|
|
|
$item['content'] = strip_tags($this->replaceUriInHtmlElement($element), '<a><p><br><img>');
|
2016-08-22 01:25:56 +02:00
|
|
|
else
|
2017-02-11 16:16:56 +01:00
|
|
|
$item['content'] = $this->loadFullArticle($item['uri']);
|
2016-08-07 00:06:14 +02:00
|
|
|
|
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
|
2016-08-07 12:51:09 +02:00
|
|
|
/*
|
|
|
|
* Adds a new item to $items using a generic operation (should work for most (all?) wikis)
|
|
|
|
*/
|
2017-02-11 16:16:56 +01:00
|
|
|
private function addDidYouKnowGeneric($element, $fullArticle){
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($element->find('ul', 0)->find('li') as $entry) {
|
2016-08-22 18:55:59 +02:00
|
|
|
$item = array();
|
2016-08-22 01:25:56 +02:00
|
|
|
|
2016-08-07 13:02:10 +02:00
|
|
|
// We can only use the first anchor, there is no way of finding the 'correct' one if there are multiple
|
2016-08-27 18:39:57 +02:00
|
|
|
$item['uri'] = $this->getURI() . $entry->find('a', 0)->href;
|
2016-08-22 18:55:59 +02:00
|
|
|
$item['title'] = strip_tags($entry->innertext);
|
2016-08-07 12:51:09 +02:00
|
|
|
|
|
|
|
if(!$fullArticle)
|
2017-02-11 16:16:56 +01:00
|
|
|
$item['content'] = $this->replaceUriInHtmlElement($entry);
|
2016-08-22 01:25:56 +02:00
|
|
|
else
|
2017-02-11 16:16:56 +01:00
|
|
|
$item['content'] = $this->loadFullArticle($item['uri']);
|
2016-08-07 12:51:09 +02:00
|
|
|
|
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-07 00:06:14 +02:00
|
|
|
/**
|
|
|
|
* Loads the full article from a given URI
|
|
|
|
*/
|
2017-02-11 16:16:56 +01:00
|
|
|
private function loadFullArticle($uri){
|
2016-09-25 23:22:33 +02:00
|
|
|
$content_html = getSimpleHTMLDOMCached($uri);
|
2016-08-22 01:25:56 +02:00
|
|
|
|
2016-08-07 00:06:14 +02:00
|
|
|
if(!$content_html)
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('Could not load site: ' . $uri . '!');
|
2016-08-22 01:25:56 +02:00
|
|
|
|
2016-08-07 00:06:14 +02:00
|
|
|
$content = $content_html->find('#mw-content-text', 0);
|
|
|
|
|
|
|
|
if(!$content)
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('Could not find content in page: ' . $uri . '!');
|
2016-08-22 01:25:56 +02:00
|
|
|
|
2016-08-07 00:06:14 +02:00
|
|
|
// Let's remove a couple of things from the article
|
2016-08-07 12:51:09 +02:00
|
|
|
$table = $content->find('#toc', 0); // Table of contents
|
|
|
|
if(!$table === false)
|
|
|
|
$table->outertext = '';
|
2016-08-07 00:06:14 +02:00
|
|
|
|
|
|
|
foreach($content->find('ol.references') as $reference) // References
|
|
|
|
$reference->outertext = '';
|
|
|
|
|
2016-08-27 18:39:57 +02:00
|
|
|
return str_replace('href="/', 'href="' . $this->getURI() . '/', $content->innertext);
|
2016-08-07 00:06:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation for de.wikipedia.org
|
|
|
|
*/
|
2017-02-11 16:16:56 +01:00
|
|
|
private function getContentsDe($html, $subject, $fullArticle){
|
2017-07-29 19:28:00 +02:00
|
|
|
switch($subject) {
|
2016-08-22 01:25:56 +02:00
|
|
|
case WIKIPEDIA_SUBJECT_TFA:
|
2016-08-07 12:51:09 +02:00
|
|
|
$element = $html->find('div[id=mf-tfa]', 0);
|
2017-02-11 16:16:56 +01:00
|
|
|
$this->addTodaysFeaturedArticleGeneric($element, $fullArticle);
|
2016-08-07 12:51:09 +02:00
|
|
|
break;
|
|
|
|
case WIKIPEDIA_SUBJECT_DYK:
|
|
|
|
$element = $html->find('div[id=mf-dyk]', 0);
|
2017-02-11 16:16:56 +01:00
|
|
|
$this->addDidYouKnowGeneric($element, $fullArticle);
|
2016-08-07 12:51:09 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-08-07 00:06:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation for fr.wikipedia.org
|
|
|
|
*/
|
2017-02-11 16:16:56 +01:00
|
|
|
private function getContentsFr($html, $subject, $fullArticle){
|
2017-07-29 19:28:00 +02:00
|
|
|
switch($subject) {
|
2016-08-22 01:25:56 +02:00
|
|
|
case WIKIPEDIA_SUBJECT_TFA:
|
2017-04-28 19:15:23 +02:00
|
|
|
$element = $html->find('div[class=accueil_2017_cadre]', 0);
|
2017-02-11 16:16:56 +01:00
|
|
|
$this->addTodaysFeaturedArticleGeneric($element, $fullArticle, 'Lire la suite');
|
2016-08-07 12:51:09 +02:00
|
|
|
break;
|
|
|
|
case WIKIPEDIA_SUBJECT_DYK:
|
2017-04-28 19:15:23 +02:00
|
|
|
$element = $html->find('div[class=accueil_2017_cadre]', 2);
|
2017-02-11 16:16:56 +01:00
|
|
|
$this->addDidYouKnowGeneric($element, $fullArticle);
|
2016-08-07 12:51:09 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-08-07 00:06:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation for en.wikipedia.org
|
|
|
|
*/
|
2017-02-11 16:16:56 +01:00
|
|
|
private function getContentsEn($html, $subject, $fullArticle){
|
2017-07-29 19:28:00 +02:00
|
|
|
switch($subject) {
|
2016-08-22 01:25:56 +02:00
|
|
|
case WIKIPEDIA_SUBJECT_TFA:
|
2016-08-07 12:51:09 +02:00
|
|
|
$element = $html->find('div[id=mp-tfa]', 0);
|
2017-02-11 16:16:56 +01:00
|
|
|
$this->addTodaysFeaturedArticleGeneric($element, $fullArticle);
|
2016-08-07 12:51:09 +02:00
|
|
|
break;
|
|
|
|
case WIKIPEDIA_SUBJECT_DYK:
|
|
|
|
$element = $html->find('div[id=mp-dyk]', 0);
|
2017-02-11 16:16:56 +01:00
|
|
|
$this->addDidYouKnowGeneric($element, $fullArticle);
|
2016-08-07 12:51:09 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-08-07 00:06:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation for eo.wikipedia.org
|
|
|
|
*/
|
2017-02-11 16:16:56 +01:00
|
|
|
private function getContentsEo($html, $subject, $fullArticle){
|
2017-07-29 19:28:00 +02:00
|
|
|
switch($subject) {
|
2016-08-22 01:25:56 +02:00
|
|
|
case WIKIPEDIA_SUBJECT_TFA:
|
2016-08-07 12:51:09 +02:00
|
|
|
$element = $html->find('div[id=mf-artikolo-de-la-semajno]', 0);
|
2017-02-11 16:16:56 +01:00
|
|
|
$this->addTodaysFeaturedArticleGeneric($element, $fullArticle);
|
2016-08-07 12:51:09 +02:00
|
|
|
break;
|
|
|
|
case WIKIPEDIA_SUBJECT_DYK:
|
|
|
|
$element = $html->find('div[id=mw-content-text]', 0)->find('table', 4)->find('td', 4);
|
2017-02-11 16:16:56 +01:00
|
|
|
$this->addDidYouKnowGeneric($element, $fullArticle);
|
2016-08-07 12:51:09 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-08-07 00:06:14 +02:00
|
|
|
}
|
2016-08-28 11:22:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation for nl.wikipedia.org
|
|
|
|
*/
|
2017-02-11 16:16:56 +01:00
|
|
|
private function getContentsNl($html, $subject, $fullArticle){
|
2017-07-29 19:28:00 +02:00
|
|
|
switch($subject) {
|
2016-08-28 11:22:37 +02:00
|
|
|
case WIKIPEDIA_SUBJECT_TFA:
|
|
|
|
$element = $html->find('div[id=mf-uitgelicht]', 0);
|
2017-02-11 16:16:56 +01:00
|
|
|
$this->addTodaysFeaturedArticleGeneric($element, $fullArticle, 'Lees meer');
|
2016-08-28 11:22:37 +02:00
|
|
|
break;
|
|
|
|
case WIKIPEDIA_SUBJECT_DYK:
|
|
|
|
$element = $html->find('div[id=mw-content-text]', 0)->find('table', 4)->find('td', 2);
|
2017-02-11 16:16:56 +01:00
|
|
|
$this->addDidYouKnowGeneric($element, $fullArticle);
|
2016-08-28 11:22:37 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-08-07 00:06:14 +02:00
|
|
|
}
|