diff options
author | Bernhard Posselt <nukeawhale@gmail.com> | 2013-08-28 19:19:28 +0200 |
---|---|---|
committer | Bernhard Posselt <nukeawhale@gmail.com> | 2013-08-28 19:19:28 +0200 |
commit | 2f67340e551b12dce8824381c3291bb2137857cb (patch) | |
tree | 2342cb35b5b337e083bb2fdc866a3d10d92f99c7 /utility/articleenhancer/enhancer.php | |
parent | a9eb72911b6f022da645dc08cf8c0f4b1702d1e1 (diff) |
Possible backwards incompatible change by using the link provided by simplepie instead of the user for the url hash. This prevents duplication of the feed when adding a slightly different feed url which points to the same feed and allows a speedup from O(n) to O(1) for article enhanchers
Diffstat (limited to 'utility/articleenhancer/enhancer.php')
-rw-r--r-- | utility/articleenhancer/enhancer.php | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/utility/articleenhancer/enhancer.php b/utility/articleenhancer/enhancer.php index 059904f63..d7d96f6a9 100644 --- a/utility/articleenhancer/enhancer.php +++ b/utility/articleenhancer/enhancer.php @@ -28,23 +28,36 @@ namespace OCA\News\Utility\ArticleEnhancer; class Enhancer { - private $enhancers; + private $enhancers = array(); - public function __construct(){ - $this->enhancers = array(); + public function registerEnhancer($feedUrl, ArticleEnhancer $enhancer){ + $feedUrl = $this->removeTrailingSlash($feedUrl); + + // create hashkeys for all supported protocols for quick access + $this->enhancers[$feedUrl] = $enhancer; + $this->enhancers['https://' . $feedUrl] = $enhancer; + $this->enhancers['http://' . $feedUrl] = $enhancer; + $this->enhancers['https://www.' . $feedUrl] = $enhancer; + $this->enhancers['http://www.' . $feedUrl] = $enhancer; } - public function registerEnhancer(ArticleEnhancer $enhancer){ - array_push($this->enhancers, $enhancer); + public function enhance($item, $feedUrl){ + $feedUrl = $this->removeTrailingSlash($feedUrl); + + if(array_key_exists($feedUrl, $this->enhancers)) { + return $this->enhancers[$feedUrl]->enhance($item); + } else { + return $item; + } } - public function enhance($item){ - foreach($this->enhancers as $enhancer){ - if($enhancer->canHandle($item)){ - return $enhancer->enhance($item); - } + private function removeTrailingSlash($url) { + if($url[strlen($url)-1] === '/') { + return substr($url, 0, -1); + } else { + return $url; } } |