From 759e5132a1311a1b56787b97e82ddad4b5348aa1 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 23 Sep 2012 13:38:58 +0400 Subject: [PATCH] subscribe_to_feed: stop fetching URL multiple times while subscribing, various other speedups --- classes/handler/public.php | 2 +- classes/rpc.php | 6 ----- include/functions.php | 46 +++++++++++++------------------------- js/functions.js | 19 +++++++++++++++- 4 files changed, 34 insertions(+), 39 deletions(-) diff --git a/classes/handler/public.php b/classes/handler/public.php index b9972df4f..7cb465594 100644 --- a/classes/handler/public.php +++ b/classes/handler/public.php @@ -553,7 +553,7 @@ class Handler_Public extends Handler { break; case 4: print_notice(__("Multiple feed URLs found.")); - $feed_urls = get_feeds_from_html($feed_url); + $feed_urls = $rc["feeds"]; break; case 5: print_error(T_sprintf("Could not subscribe to %s.
Can't download the Feed URL.", $feed_url)); diff --git a/classes/rpc.php b/classes/rpc.php index c4d0cd53c..f0d1d3d86 100644 --- a/classes/rpc.php +++ b/classes/rpc.php @@ -195,12 +195,6 @@ class RPC extends Handler_Protected { print json_encode(array("result" => $rc)); } - function extractfeedurls() { - $urls = get_feeds_from_html($_REQUEST['url']); - - print json_encode(array("urls" => $urls)); - } - function togglepref() { $key = db_escape_string($_REQUEST["key"]); set_pref($this->link, $key, !get_pref($this->link, $key)); diff --git a/include/functions.php b/include/functions.php index ba7cb75a8..cca02837e 100644 --- a/include/functions.php +++ b/include/functions.php @@ -1810,15 +1810,19 @@ $update_method = 0; - if (!fetch_file_contents($url, false, $auth_login, $auth_pass)) - return array("code" => 5, "message" => $fetch_last_error); + $contents = @fetch_file_contents($url, false, $auth_login, $auth_pass); + + if (!$contents) { + return array("code" => 5, "message" => $fetch_last_error); + } + + if (is_html($contents)) { + $feedUrls = get_feeds_from_html($url, $contents); - if (url_is_html($url, $auth_login, $auth_pass)) { - $feedUrls = get_feeds_from_html($url, $auth_login, $auth_pass); if (count($feedUrls) == 0) { return array("code" => 3); } else if (count($feedUrls) > 1) { - return array("code" => 4); + return array("code" => 4, "feeds" => $feedUrls); } //use feed url as new URL $url = key($feedUrls); @@ -4758,22 +4762,13 @@ return false; } - /** - * Extracts RSS/Atom feed URLs from the given HTML URL. - * - * @param string $url HTML page URL - * - * @return array Array of feeds. Key is the full URL, value the title - */ - function get_feeds_from_html($url, $login = false, $pass = false) + function get_feeds_from_html($url, $content) { $url = fix_url($url); $baseUrl = substr($url, 0, strrpos($url, '/') + 1); libxml_use_internal_errors(true); - $content = @fetch_file_contents($url, false, $login, $pass); - $doc = new DOMDocument(); $doc->loadHTML($content); $xpath = new DOMXPath($doc); @@ -4794,23 +4789,12 @@ return $feedUrls; } - /** - * Checks if the content behind the given URL is a HTML file - * - * @param string $url URL to check - * - * @return boolean True if the URL contains HTML content - */ + function is_html($content) { + return preg_match("/') === false - && stripos($content, ' 0) + select.removeOption(0); + + var count = 0; + for (var feedUrl in feeds) { + select.addOption({value: feedUrl, label: feeds[feedUrl]}); + count++; + } + + Effect.Appear('feedDlg_feedsContainer', {duration : 0.5}); + break; case 5: alert(__("Couldn't download the specified URL: %s").