From 1e33b7c2ec7bb5c12fd04b96ab86a39cd6e9ecdc Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Tue, 2 Aug 2016 13:25:49 +0200 Subject: [PATCH 1/4] Make CADExtractContent a member function This fixes error "Using $this when not in object context" Nested functions are not part of the object and therefore don't have access to the object instance $this! --- bridges/CADBridge.php | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/bridges/CADBridge.php b/bridges/CADBridge.php index 7857282..6fac253 100644 --- a/bridges/CADBridge.php +++ b/bridges/CADBridge.php @@ -7,21 +7,10 @@ class CADBridge extends BridgeAbstract{ $this->name = "CAD Bridge"; $this->uri = "http://www.cad-comic.com/"; $this->description = "Returns the newest articles."; - $this->update = "2015-04-03"; + $this->update = "2016-08-02"; } - - public function collectData(array $param){ - - function CADUrl($string) { - $html2 = explode("\"", $string); - $string = $html2[1]; - if (substr($string,0,4) != 'http') - return 'notanurl'; - return $string; - } - - function CADExtractContent($url) { + function CADExtractContent($url) { $html3 = $this->file_get_html($url); $htmlpart = explode("/", $url); if ($htmlpart[3] == 'cad') @@ -36,6 +25,18 @@ class CADBridge extends BridgeAbstract{ return ''; } + public function collectData(array $param){ + + function CADUrl($string) { + $html2 = explode("\"", $string); + $string = $html2[1]; + if (substr($string,0,4) != 'http') + return 'notanurl'; + return $string; + } + + + $html = $this->file_get_html('http://cdn2.cad-comic.com/rss.xml') or $this->returnError('Could not request CAD.', 404); $limit = 0; foreach($html->find('item') as $element) { @@ -45,7 +46,7 @@ class CADBridge extends BridgeAbstract{ $item->uri = CADUrl($element->find('description', 0)->innertext); if ($item->uri != 'notanurl') { $item->timestamp = strtotime($element->find('pubDate', 0)->plaintext); - $item->content = CADExtractContent($item->uri); + $item->content = $this->CADExtractContent($item->uri); $this->items[] = $item; $limit++; } From 01bdda5e19c8b45d0e0e9655dc77082c5e8185ca Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Tue, 2 Aug 2016 13:37:18 +0200 Subject: [PATCH 2/4] Fix indentation and remove unnecessary lines --- bridges/CADBridge.php | 87 ++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 46 deletions(-) diff --git a/bridges/CADBridge.php b/bridges/CADBridge.php index 6fac253..bdb7ab3 100644 --- a/bridges/CADBridge.php +++ b/bridges/CADBridge.php @@ -1,70 +1,65 @@ maintainer = "nyutag"; + $this->name = "CAD Bridge"; + $this->uri = "http://www.cad-comic.com/"; + $this->description = "Returns the newest articles."; + $this->update = "2016-08-02"; + } - public function loadMetadatas() { - - $this->maintainer = "nyutag"; - $this->name = "CAD Bridge"; - $this->uri = "http://www.cad-comic.com/"; - $this->description = "Returns the newest articles."; - $this->update = "2016-08-02"; - - } - function CADExtractContent($url) { + function CADExtractContent($url) { $html3 = $this->file_get_html($url); $htmlpart = explode("/", $url); if ($htmlpart[3] == 'cad') - preg_match_all("/http:\/\/cdn2\.cad-comic\.com\/comics\/cad-\S*png/", $html3, $url2); + preg_match_all("/http:\/\/cdn2\.cad-comic\.com\/comics\/cad-\S*png/", $html3, $url2); if ($htmlpart[3] == 'sillies') - preg_match_all("/http:\/\/cdn2\.cad-comic\.com\/comics\/sillies-\S*gif/", $html3, $url2); + preg_match_all("/http:\/\/cdn2\.cad-comic\.com\/comics\/sillies-\S*gif/", $html3, $url2); $img = implode ($url2[0]); $html3->clear(); unset ($html3); if ($img == '') - return 'Daily comic not realease yet'; + return 'Daily comic not realease yet'; return ''; - } - - public function collectData(array $param){ + } + public function collectData(array $param){ function CADUrl($string) { - $html2 = explode("\"", $string); - $string = $html2[1]; - if (substr($string,0,4) != 'http') - return 'notanurl'; - return $string; + $html2 = explode("\"", $string); + $string = $html2[1]; + if (substr($string,0,4) != 'http') + return 'notanurl'; + return $string; } - - $html = $this->file_get_html('http://cdn2.cad-comic.com/rss.xml') or $this->returnError('Could not request CAD.', 404); $limit = 0; + foreach($html->find('item') as $element) { - if($limit < 5) { - $item = new \Item(); - $item->title = $element->find('title', 0)->innertext; - $item->uri = CADUrl($element->find('description', 0)->innertext); - if ($item->uri != 'notanurl') { - $item->timestamp = strtotime($element->find('pubDate', 0)->plaintext); - $item->content = $this->CADExtractContent($item->uri); - $this->items[] = $item; - $limit++; - } - } + if($limit < 5) { + $item = new \Item(); + $item->title = $element->find('title', 0)->innertext; + $item->uri = CADUrl($element->find('description', 0)->innertext); + if ($item->uri != 'notanurl') { + $item->timestamp = strtotime($element->find('pubDate', 0)->plaintext); + $item->content = $this->CADExtractContent($item->uri); + $this->items[] = $item; + $limit++; + } + } } - - } + } - public function getName(){ - return 'CAD Bridge'; - } + public function getName(){ + return 'CAD Bridge'; + } - public function getURI(){ - return 'http://www.cad-comic.com/'; - } + public function getURI(){ + return 'http://www.cad-comic.com/'; + } - public function getCacheDuration(){ - return 3600*2; // 2 hours -// return 0; - } + public function getCacheDuration(){ + return 3600*2; // 2 hours + } } +?> \ No newline at end of file From d0c932c149811474608569e551ac458bdbdc7b0c Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Tue, 2 Aug 2016 14:07:40 +0200 Subject: [PATCH 3/4] Handle failing requests Disabling https support results in a failed request which is now handled as if there is no data at all. --- bridges/CADBridge.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bridges/CADBridge.php b/bridges/CADBridge.php index bdb7ab3..3405d26 100644 --- a/bridges/CADBridge.php +++ b/bridges/CADBridge.php @@ -10,6 +10,11 @@ class CADBridge extends BridgeAbstract{ function CADExtractContent($url) { $html3 = $this->file_get_html($url); + + // The request might fail due to missing https support or wrong URL + if($html3 == false) + return 'Daily comic not released yet'; + $htmlpart = explode("/", $url); if ($htmlpart[3] == 'cad') preg_match_all("/http:\/\/cdn2\.cad-comic\.com\/comics\/cad-\S*png/", $html3, $url2); @@ -19,7 +24,7 @@ class CADBridge extends BridgeAbstract{ $html3->clear(); unset ($html3); if ($img == '') - return 'Daily comic not realease yet'; + return 'Daily comic not released yet'; return ''; } From a32044893afbd30b14b794bda0bedbd7e0b2b109 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Tue, 2 Aug 2016 14:13:22 +0200 Subject: [PATCH 4/4] Replace if-statements with switch statement The previous implementation would fail if $htmlpart[3] was neither 'cad' nor 'sillies'. Now the default text will be returned. --- bridges/CADBridge.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/bridges/CADBridge.php b/bridges/CADBridge.php index 3405d26..90b338c 100644 --- a/bridges/CADBridge.php +++ b/bridges/CADBridge.php @@ -16,10 +16,18 @@ class CADBridge extends BridgeAbstract{ return 'Daily comic not released yet'; $htmlpart = explode("/", $url); - if ($htmlpart[3] == 'cad') - preg_match_all("/http:\/\/cdn2\.cad-comic\.com\/comics\/cad-\S*png/", $html3, $url2); - if ($htmlpart[3] == 'sillies') - preg_match_all("/http:\/\/cdn2\.cad-comic\.com\/comics\/sillies-\S*gif/", $html3, $url2); + + switch ($htmlpart[3]){ + case 'cad': + preg_match_all("/http:\/\/cdn2\.cad-comic\.com\/comics\/cad-\S*png/", $html3, $url2); + break; + case 'sillies': + preg_match_all("/http:\/\/cdn2\.cad-comic\.com\/comics\/sillies-\S*gif/", $html3, $url2); + break; + default: + return 'Daily comic not released yet'; + } + $img = implode ($url2[0]); $html3->clear(); unset ($html3);