From 42d69a95f3276a2d6089ca68f635c4e2f6aa7a23 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Tue, 21 Oct 2014 16:45:36 +0200 Subject: convert tabs indention to indention with 4 spaces because of mixing of both variants in code and better readability on github and websites because you cant set the indention width there and 8 spaces will be used for a tab --- utility/faviconfetcher.php | 230 ++++++++++++++++++++-------------------- utility/opmlexporter.php | 120 ++++++++++----------- utility/simplepieapifactory.php | 36 +++---- utility/updater.php | 40 +++---- 4 files changed, 213 insertions(+), 213 deletions(-) (limited to 'utility') diff --git a/utility/faviconfetcher.php b/utility/faviconfetcher.php index 850a7f576..b837db91c 100644 --- a/utility/faviconfetcher.php +++ b/utility/faviconfetcher.php @@ -18,16 +18,16 @@ use \ZendXml\Security; class FaviconFetcher { - private $apiFactory; + private $apiFactory; - /** - * Inject a factory to build a simplepie file object. This is needed because - * the file object contains logic in its constructor which makes it - * impossible to inject and test - */ - public function __construct(SimplePieAPIFactory $apiFactory) { - $this->apiFactory = $apiFactory; - } + /** + * Inject a factory to build a simplepie file object. This is needed because + * the file object contains logic in its constructor which makes it + * impossible to inject and test + */ + public function __construct(SimplePieAPIFactory $apiFactory) { + $this->apiFactory = $apiFactory; + } /** @@ -36,129 +36,129 @@ class FaviconFetcher { * @param string|null $url the url where to fetch it from * @return null|string */ - public function fetch($url) { - try { - $url = $this->buildURL($url); - } catch (NoValidUrlException $e) { - return null; - } - - $faviconUrl = $this->extractFromPage($url); - - // check the url for a valid image - if($faviconUrl && $this->isImage($faviconUrl)) { - return $faviconUrl; - } elseif ($url) { - $base = new \Net_URL2($url); - $faviconUrl = (string) $base->resolve('/favicon.ico'); - - if($this->isImage($faviconUrl)) { - return $faviconUrl; - } - } - - return null; - } - - /** - * Get the attribute if its a DOMElement, otherwise return null - */ - private function getAttribute($item, $name) { - // used to fix scrutinizer errors - if ($item instanceof \DOMElement) { - return $item->getAttribute($name); - } else { - return null; - } - } - - - /** - * Tries to get a favicon from a page - * @param string $url the url to the page - * @return string the full url to the page - */ - protected function extractFromPage($url) { - if(!$url) { - return null; - } - - $file = $this->getFile($url); + public function fetch($url) { + try { + $url = $this->buildURL($url); + } catch (NoValidUrlException $e) { + return null; + } + + $faviconUrl = $this->extractFromPage($url); + + // check the url for a valid image + if($faviconUrl && $this->isImage($faviconUrl)) { + return $faviconUrl; + } elseif ($url) { + $base = new \Net_URL2($url); + $faviconUrl = (string) $base->resolve('/favicon.ico'); + + if($this->isImage($faviconUrl)) { + return $faviconUrl; + } + } + + return null; + } + + /** + * Get the attribute if its a DOMElement, otherwise return null + */ + private function getAttribute($item, $name) { + // used to fix scrutinizer errors + if ($item instanceof \DOMElement) { + return $item->getAttribute($name); + } else { + return null; + } + } + + + /** + * Tries to get a favicon from a page + * @param string $url the url to the page + * @return string the full url to the page + */ + protected function extractFromPage($url) { + if(!$url) { + return null; + } + + $file = $this->getFile($url); /** @noinspection PhpUndefinedFieldInspection */ if($file->body !== '') { - $dom = new \DOMDocument(); + $dom = new \DOMDocument(); - Security::scan($file->body, $dom, function ($xml, $dom) { - return @$dom->loadHTML($xml, LIBXML_NONET); - }); + Security::scan($file->body, $dom, function ($xml, $dom) { + return @$dom->loadHTML($xml, LIBXML_NONET); + }); - if($dom) { - $xpath = new \DOMXpath($dom); - $elements = $xpath->query("//link[contains(@rel, 'icon')]"); + if($dom) { + $xpath = new \DOMXpath($dom); + $elements = $xpath->query("//link[contains(@rel, 'icon')]"); - if ($elements->length > 0) { + if ($elements->length > 0) { /** @noinspection PhpUndefinedMethodInspection */ $iconPath = $this->getAttribute($elements->item(0), 'href'); $base = new \Net_URL2($url); - $absPath = (string) $base->resolve($iconPath, $url); - return $absPath; - } - } - } + $absPath = (string) $base->resolve($iconPath, $url); + return $absPath; + } + } + } return null; - } + } - private function getFile($url) { - return $this->apiFactory->getFile( - $url, 10, 5, null, 'Mozilla/5.0 AppleWebKit' - ); - } + private function getFile($url) { + return $this->apiFactory->getFile( + $url, 10, 5, null, 'Mozilla/5.0 AppleWebKit' + ); + } - /** - * Test if the file is an image - * @param string $url the url to the file - * @return bool true if image - */ - protected function isImage($url) { - // check for empty urls - if(!$url) { - return false; - } + /** + * Test if the file is an image + * @param string $url the url to the file + * @return bool true if image + */ + protected function isImage($url) { + // check for empty urls + if(!$url) { + return false; + } - $file = $this->getFile($url); + $file = $this->getFile($url); /** @noinspection PhpParamsInspection */ $sniffer = new \SimplePie_Content_Type_Sniffer($file); - return $sniffer->image() !== false; - } - - - /** - * Get HTTP or HTTPS addresses from an incomplete URL - * @param string $url the url that should be built - * @return string a string containing the http or https address - * @throws NoValidUrlException when no valid url can be returned - */ - protected function buildURL($url) { - // trim the right / from the url - $url = trim($url); - $url = rtrim($url, '/'); - - // check for http:// or https:// and validate URL - if (strpos($url, 'http://') === 0 || strpos($url, 'https://') === 0) { - if (filter_var($url, FILTER_VALIDATE_URL)) { - return $url; - } - } elseif (filter_var("http://" . $url, FILTER_VALIDATE_URL)) { - // maybe $url was something like www.example.com - return 'http://' . $url; - } - - // no valid URL was passed in or could be built from $url - throw new NoValidUrlException(); - } + return $sniffer->image() !== false; + } + + + /** + * Get HTTP or HTTPS addresses from an incomplete URL + * @param string $url the url that should be built + * @return string a string containing the http or https address + * @throws NoValidUrlException when no valid url can be returned + */ + protected function buildURL($url) { + // trim the right / from the url + $url = trim($url); + $url = rtrim($url, '/'); + + // check for http:// or https:// and validate URL + if (strpos($url, 'http://') === 0 || strpos($url, 'https://') === 0) { + if (filter_var($url, FILTER_VALIDATE_URL)) { + return $url; + } + } elseif (filter_var("http://" . $url, FILTER_VALIDATE_URL)) { + // maybe $url was something like www.example.com + return 'http://' . $url; + } + + // no valid URL was passed in or could be built from $url + throw new NoValidUrlException(); + } } diff --git a/utility/opmlexporter.php b/utility/opmlexporter.php index 0efaba9b8..d786c0c6f 100644 --- a/utility/opmlexporter.php +++ b/utility/opmlexporter.php @@ -25,66 +25,66 @@ class OPMLExporter { * @param \OCA\News\Db\Feed[] $feeds * @return \DomDocument the document */ - public function build($folders, $feeds){ - $document = new \DomDocument('1.0', 'UTF-8'); - $document->formatOutput = true; - - $root = $document->createElement('opml'); - $root->setAttribute('version', '2.0'); - - // head - $head = $document->createElement('head'); - - $title = $document->createElement('title', 'Subscriptions'); - $head->appendChild($title); - - $root->appendChild($head); - - // body - $body = $document->createElement('body'); - - // feeds with folders - foreach($folders as $folder) { - $folderOutline = $document->createElement('outline'); - $folderOutline->setAttribute('title', $folder->getName()); - $folderOutline->setAttribute('text', $folder->getName()); - - // feeds in folders - foreach ($feeds as $feed) { - if ($feed->getFolderId() === $folder->getId()) { - $feedOutline = $this->createFeedOutline($feed, $document); - $folderOutline->appendChild($feedOutline); - } - } - - $body->appendChild($folderOutline); - } - - // feeds without folders - foreach ($feeds as $feed) { - if ($feed->getFolderId() === 0) { - $feedOutline = $this->createFeedOutline($feed, $document); - $body->appendChild($feedOutline); - } - } - - $root->appendChild($body); - - $document->appendChild($root); - - return $document; - } - - - protected function createFeedOutline($feed, $document) { - $feedOutline = $document->createElement('outline'); - $feedOutline->setAttribute('title', $feed->getTitle()); - $feedOutline->setAttribute('text', $feed->getTitle()); - $feedOutline->setAttribute('type', 'rss'); - $feedOutline->setAttribute('xmlUrl', $feed->getUrl()); - $feedOutline->setAttribute('htmlUrl', $feed->getLink()); - return $feedOutline; - } + public function build($folders, $feeds){ + $document = new \DomDocument('1.0', 'UTF-8'); + $document->formatOutput = true; + + $root = $document->createElement('opml'); + $root->setAttribute('version', '2.0'); + + // head + $head = $document->createElement('head'); + + $title = $document->createElement('title', 'Subscriptions'); + $head->appendChild($title); + + $root->appendChild($head); + + // body + $body = $document->createElement('body'); + + // feeds with folders + foreach($folders as $folder) { + $folderOutline = $document->createElement('outline'); + $folderOutline->setAttribute('title', $folder->getName()); + $folderOutline->setAttribute('text', $folder->getName()); + + // feeds in folders + foreach ($feeds as $feed) { + if ($feed->getFolderId() === $folder->getId()) { + $feedOutline = $this->createFeedOutline($feed, $document); + $folderOutline->appendChild($feedOutline); + } + } + + $body->appendChild($folderOutline); + } + + // feeds without folders + foreach ($feeds as $feed) { + if ($feed->getFolderId() === 0) { + $feedOutline = $this->createFeedOutline($feed, $document); + $body->appendChild($feedOutline); + } + } + + $root->appendChild($body); + + $document->appendChild($root); + + return $document; + } + + + protected function createFeedOutline($feed, $document) { + $feedOutline = $document->createElement('outline'); + $feedOutline->setAttribute('title', $feed->getTitle()); + $feedOutline->setAttribute('text', $feed->getTitle()); + $feedOutline->setAttribute('type', 'rss'); + $feedOutline->setAttribute('xmlUrl', $feed->getUrl()); + $feedOutline->setAttribute('htmlUrl', $feed->getLink()); + return $feedOutline; + } } diff --git a/utility/simplepieapifactory.php b/utility/simplepieapifactory.php index 0e9326ba8..92cbe18f3 100644 --- a/utility/simplepieapifactory.php +++ b/utility/simplepieapifactory.php @@ -30,24 +30,24 @@ class SimplePieAPIFactory { * @param bool $force_fsockopen * @return \SimplePie_File a new object */ - public function getFile($url, $timeout=10, $redirects=5, $headers=null, - $useragent=null, $force_fsockopen=false) { - - return new \SimplePie_File($url, $timeout, $redirects, $headers, - $useragent, $force_fsockopen); - } - - - /** - * Returns a new instance of a SimplePie_Core() object. This is needed - * because the class relies on external dependencies which are not passed - * in via the constructor and thus making it nearly impossible to unit test - * code that uses this class - * @return \SimplePie_Core instance - */ - public function getCore() { - return new \SimplePie(); - } + public function getFile($url, $timeout=10, $redirects=5, $headers=null, + $useragent=null, $force_fsockopen=false) { + + return new \SimplePie_File($url, $timeout, $redirects, $headers, + $useragent, $force_fsockopen); + } + + + /** + * Returns a new instance of a SimplePie_Core() object. This is needed + * because the class relies on external dependencies which are not passed + * in via the constructor and thus making it nearly impossible to unit test + * code that uses this class + * @return \SimplePie_Core instance + */ + public function getCore() { + return new \SimplePie(); + } } \ No newline at end of file diff --git a/utility/updater.php b/utility/updater.php index 6c4ceae9f..a6f8dc28a 100644 --- a/utility/updater.php +++ b/utility/updater.php @@ -22,33 +22,33 @@ use \OCA\News\Service\ItemService; class Updater { - private $folderService; - private $feedService; - private $itemService; + private $folderService; + private $feedService; + private $itemService; - public function __construct(FolderService $folderService, - FeedService $feedService, - ItemService $itemService) { - $this->folderService = $folderService; - $this->feedService = $feedService; - $this->itemService = $itemService; - } + public function __construct(FolderService $folderService, + FeedService $feedService, + ItemService $itemService) { + $this->folderService = $folderService; + $this->feedService = $feedService; + $this->itemService = $itemService; + } - public function beforeUpdate() { - $this->folderService->purgeDeleted(); - $this->feedService->purgeDeleted(); - } + public function beforeUpdate() { + $this->folderService->purgeDeleted(); + $this->feedService->purgeDeleted(); + } - public function update() { - $this->feedService->updateAll(); - } + public function update() { + $this->feedService->updateAll(); + } - public function afterUpdate() { - $this->itemService->autoPurgeOld(); - } + public function afterUpdate() { + $this->itemService->autoPurgeOld(); + } } \ No newline at end of file -- cgit v1.2.3