From a24e6ea32325b48b252ade937060925f6d0c9308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20Mazi=C3=A8re?= Date: Sat, 27 Aug 2016 18:39:57 +0200 Subject: [PATCH] [WikipediaBridge] implement and use getURI() and getName() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Pierre Mazière --- bridges/WikipediaBridge.php | 64 ++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/bridges/WikipediaBridge.php b/bridges/WikipediaBridge.php index 7a284037..55394f74 100644 --- a/bridges/WikipediaBridge.php +++ b/bridges/WikipediaBridge.php @@ -44,8 +44,42 @@ class WikipediaBridge extends BridgeAbstract{ ); } + public function getURI(){ + $params=$this->parameters[$this->queriedContext]; + return 'https://' . strtolower($params['language']['value']) . '.wikipedia.org'; + } + + public function getName(){ + $params=$this->parameters[$this->queriedContext]; + $subject = WIKIPEDIA_SUBJECT_TFA; + switch($params['subject']['value']){ + case 'tfa': + $subject = WIKIPEDIA_SUBJECT_TFA; + break; + case 'dyk': + $subject = WIKIPEDIA_SUBJECT_DYK; + break; + default: + $subject = WIKIPEDIA_SUBJECT_TFA; + break; + } + + switch($subject){ + case WIKIPEDIA_SUBJECT_TFA: + $name = 'Today\'s featured article from ' . strtolower($params['language']['value']) . '.wikipedia.org'; + break; + case WIKIPEDIA_SUBJECT_DYK: + $name = 'Did you know? - articles from ' . strtolower($params['language']['value']) . '.wikipedia.org'; + break; + default: + $name = 'Articles from ' . strtolower($params['language']['value']) . '.wikipedia.org'; + break; + } + return $name; + } + public function collectData(){ - $params=$this->parameters[$this->queriedContext]; + $params=$this->parameters[$this->queriedContext]; if(!isset($params['language']['value'])) $this->returnClientError('You must specify a valid language via \'&language=\'!'); @@ -72,27 +106,11 @@ class WikipediaBridge extends BridgeAbstract{ if(isset($params['fullarticle']['value'])) $fullArticle = $params['fullarticle']['value']; - // We store the correct URI as URI of this bridge (so it can be used later!) - $this->uri = 'https://' . strtolower($params['language']['value']) . '.wikipedia.org'; - - // While we at it let's also update the name for the feed - switch($subject){ - case WIKIPEDIA_SUBJECT_TFA: - $this->name = 'Today\'s featured article from ' . strtolower($params['language']['value']) . '.wikipedia.org'; - break; - case WIKIPEDIA_SUBJECT_DYK: - $this->name = 'Did you know? - articles from ' . strtolower($params['language']['value']) . '.wikipedia.org'; - break; - default: - $this->name = 'Articles from ' . strtolower($params['language']['value']) . '.wikipedia.org'; - break; - } - // This will automatically send us to the correct main page in any language (try it!) - $html = $this->getSimpleHTMLDOM($this->uri . '/wiki'); + $html = $this->getSimpleHTMLDOM($this->getURI() . '/wiki'); if(!$html) - $this->returnServerError('Could not load site: ' . $this->uri . '!'); + $this->returnServerError('Could not load site: ' . $this->getURI() . '!'); /* * Now read content depending on the language (make sure to create one function per language!) @@ -130,7 +148,7 @@ class WikipediaBridge extends BridgeAbstract{ * @return The $element->innertext with all URIs replaced */ private function ReplaceURIInHTMLElement($element){ - return str_replace('href="/', 'href="' . $this->uri . '/', $element->innertext); + return str_replace('href="/', 'href="' . $this->getURI() . '/', $element->innertext); } /* @@ -153,7 +171,7 @@ class WikipediaBridge extends BridgeAbstract{ } $item = array(); - $item['uri'] = $this->uri . $target->href; + $item['uri'] = $this->getURI() . $target->href; $item['title'] = $target->title; if(!$fullArticle) @@ -172,7 +190,7 @@ class WikipediaBridge extends BridgeAbstract{ $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->uri . $entry->find('a', 0)->href; + $item['uri'] = $this->getURI() . $entry->find('a', 0)->href; $item['title'] = strip_tags($entry->innertext); if(!$fullArticle) @@ -206,7 +224,7 @@ class WikipediaBridge extends BridgeAbstract{ foreach($content->find('ol.references') as $reference) // References $reference->outertext = ''; - return str_replace('href="/', 'href="' . $this->uri . '/', $content->innertext); + return str_replace('href="/', 'href="' . $this->getURI() . '/', $content->innertext); } /**