From a230bf88a9ce4589eeaf2d00226eafb78b4de01c Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 4 May 2017 15:00:21 +0300 Subject: [PATCH] move to Article: + static function purge_orphans($do_output = false) { move to Feeds + static function getGlobalUnread($user_id = false) { + static function getCategoryTitle($cat_id) { + static function getLabelUnread($label_id, $owner_uid = false) { --- classes/api.php | 4 +- classes/article.php | 13 ++++ classes/feeds.php | 50 +++++++++++++++- classes/handler/public.php | 2 +- classes/pref/feeds.php | 2 +- classes/pref/filters.php | 4 +- classes/rpc.php | 4 +- include/functions.php | 118 ++----------------------------------- include/functions2.php | 4 +- include/rssfuncs.php | 54 ++++++++++++++++- 10 files changed, 128 insertions(+), 127 deletions(-) diff --git a/classes/api.php b/classes/api.php index ad3ccea09..2dee790b3 100644 --- a/classes/api.php +++ b/classes/api.php @@ -101,7 +101,7 @@ class API extends Handler { if ($feed_id) { $this->wrap(self::STATUS_OK, array("unread" => getFeedUnread($feed_id, $is_cat))); } else { - $this->wrap(self::STATUS_OK, array("unread" => getGlobalUnread())); + $this->wrap(self::STATUS_OK, array("unread" => Feeds::getGlobalUnread())); } } @@ -170,7 +170,7 @@ class API extends Handler { if ($unread || !$unread_only) { array_push($cats, array("id" => $cat_id, - "title" => getCategoryTitle($cat_id), + "title" => Feeds::getCategoryTitle($cat_id), "unread" => $unread)); } } diff --git a/classes/article.php b/classes/article.php index e0c8d3e6d..64c04e140 100644 --- a/classes/article.php +++ b/classes/article.php @@ -864,4 +864,17 @@ class Article extends Handler_Protected { return $rv; } + static function purge_orphans($do_output = false) { + + // purge orphaned posts in main content table + $result = db_query("DELETE FROM ttrss_entries WHERE + NOT EXISTS (SELECT ref_id FROM ttrss_user_entries WHERE ref_id = id)"); + + if ($do_output) { + $rows = db_affected_rows($result); + _debug("Purged $rows orphaned posts."); + } + } + + } diff --git a/classes/feeds.php b/classes/feeds.php index 2b99a2823..8f57f059e 100755 --- a/classes/feeds.php +++ b/classes/feeds.php @@ -1445,7 +1445,7 @@ class Feeds extends Handler_Protected { $label_id = feed_to_label_id($feed); - return getLabelUnread($label_id, $owner_uid); + return Feeds::getLabelUnread($label_id, $owner_uid); } @@ -1602,7 +1602,7 @@ class Feeds extends Handler_Protected { static function getFeedTitle($id, $cat = false) { if ($cat) { - return getCategoryTitle($id); + return Feeds::getCategoryTitle($id); } else if ($id == -1) { return __("Starred articles"); } else if ($id == -2) { @@ -1707,5 +1707,51 @@ class Feeds extends Handler_Protected { return $unread; } + static function getGlobalUnread($user_id = false) { + + if (!$user_id) { + $user_id = $_SESSION["uid"]; + } + + $result = db_query("SELECT SUM(value) AS c_id FROM ttrss_counters_cache + WHERE owner_uid = '$user_id' AND feed_id > 0"); + + $c_id = db_fetch_result($result, 0, "c_id"); + + return $c_id; + } + + static function getCategoryTitle($cat_id) { + + if ($cat_id == -1) { + return __("Special"); + } else if ($cat_id == -2) { + return __("Labels"); + } else { + + $result = db_query("SELECT title FROM ttrss_feed_categories WHERE + id = '$cat_id'"); + + if (db_num_rows($result) == 1) { + return db_fetch_result($result, 0, "title"); + } else { + return __("Uncategorized"); + } + } + } + + static function getLabelUnread($label_id, $owner_uid = false) { + if (!$owner_uid) $owner_uid = $_SESSION["uid"]; + + $result = db_query("SELECT COUNT(ref_id) AS unread FROM ttrss_user_entries, ttrss_user_labels2 + WHERE owner_uid = '$owner_uid' AND unread = true AND label_id = '$label_id' AND article_id = ref_id"); + + if (db_num_rows($result) != 0) { + return db_fetch_result($result, 0, "unread"); + } else { + return 0; + } + } + } diff --git a/classes/handler/public.php b/classes/handler/public.php index 89d481ec5..0f1ae21eb 100644 --- a/classes/handler/public.php +++ b/classes/handler/public.php @@ -287,7 +287,7 @@ class Handler_Public extends Handler { if ($this->dbh->num_rows($result) == 1) { $uid = $this->dbh->fetch_result($result, 0, "id"); - print getGlobalUnread($uid); + print Feeds::getGlobalUnread($uid); if ($fresh) { print ";"; diff --git a/classes/pref/feeds.php b/classes/pref/feeds.php index 4dbc07a9f..f3df0297d 100755 --- a/classes/pref/feeds.php +++ b/classes/pref/feeds.php @@ -1542,7 +1542,7 @@ class Pref_Feeds extends Handler_Protected { $obj['id'] = 'CAT:' . $cat_id; $obj['items'] = array(); - $obj['name'] = getCategoryTitle($cat_id); + $obj['name'] = Feeds::getCategoryTitle($cat_id); $obj['type'] = 'category'; $obj['unread'] = (int) $cat_unread; $obj['bare_id'] = $cat_id; diff --git a/classes/pref/filters.php b/classes/pref/filters.php index 924588899..e4943a5a5 100755 --- a/classes/pref/filters.php +++ b/classes/pref/filters.php @@ -224,7 +224,7 @@ class Pref_Filters extends Handler_Protected { while ($line = $this->dbh->fetch_assoc($result)) { $where = sql_bool_to_bool($line["cat_filter"]) ? - getCategoryTitle($line["cat_id"]) : + Feeds::getCategoryTitle($line["cat_id"]) : ($line["feed_id"] ? Feeds::getFeedTitle($line["feed_id"]) : __("All feeds")); @@ -497,7 +497,7 @@ class Pref_Filters extends Handler_Protected { if (strpos($feed_id, "CAT:") === 0) { $feed_id = (int) substr($feed_id, 4); - $feed = getCategoryTitle($feed_id); + $feed = Feeds::getCategoryTitle($feed_id); } else { $feed_id = (int) $feed_id; diff --git a/classes/rpc.php b/classes/rpc.php index f82f1ef32..949a6177a 100755 --- a/classes/rpc.php +++ b/classes/rpc.php @@ -151,7 +151,7 @@ class RPC extends Handler_Protected { $this->dbh->query("DELETE FROM ttrss_user_entries WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]); - purge_orphans(); + Article::purge_orphans(); print json_encode(array("message" => "UPDATE_COUNTERS")); } @@ -556,7 +556,7 @@ class RPC extends Handler_Protected { } // Purge orphans and cleanup tags - purge_orphans(); + Article::purge_orphans(); //cleanup_tags(14, 50000); if ($num_updated > 0) { diff --git a/include/functions.php b/include/functions.php index 17c3d8c4b..d5a75843f 100644 --- a/include/functions.php +++ b/include/functions.php @@ -305,19 +305,7 @@ } } - function purge_orphans($do_output = false) { - - // purge orphaned posts in main content table - $result = db_query("DELETE FROM ttrss_entries WHERE - NOT EXISTS (SELECT ref_id FROM ttrss_user_entries WHERE ref_id = id)"); - - if ($do_output) { - $rows = db_affected_rows($result); - _debug("Purged $rows orphaned posts."); - } - } - - function get_feed_update_interval($feed_id) { + /*function get_feed_update_interval($feed_id) { $result = db_query("SELECT owner_uid, update_interval FROM ttrss_feeds WHERE id = '$feed_id'"); @@ -334,7 +322,7 @@ } else { return -1; } - } + }*/ // TODO: multiple-argument way is deprecated, first parameter is a hash now function fetch_file_contents($options /* previously: 0: $url , 1: $type = false, 2: $login = false, 3: $pass = false, @@ -578,57 +566,6 @@ return $favicon_url; } // function get_favicon_url - function check_feed_favicon($site_url, $feed) { -# print "FAVICON [$site_url]: $favicon_url\n"; - - $icon_file = ICONS_DIR . "/$feed.ico"; - - if (!file_exists($icon_file)) { - $favicon_url = get_favicon_url($site_url); - - if ($favicon_url) { - // Limiting to "image" type misses those served with text/plain - $contents = fetch_file_contents($favicon_url); // , "image"); - - if ($contents) { - // Crude image type matching. - // Patterns gleaned from the file(1) source code. - if (preg_match('/^\x00\x00\x01\x00/', $contents)) { - // 0 string \000\000\001\000 MS Windows icon resource - //error_log("check_feed_favicon: favicon_url=$favicon_url isa MS Windows icon resource"); - } - elseif (preg_match('/^GIF8/', $contents)) { - // 0 string GIF8 GIF image data - //error_log("check_feed_favicon: favicon_url=$favicon_url isa GIF image"); - } - elseif (preg_match('/^\x89PNG\x0d\x0a\x1a\x0a/', $contents)) { - // 0 string \x89PNG\x0d\x0a\x1a\x0a PNG image data - //error_log("check_feed_favicon: favicon_url=$favicon_url isa PNG image"); - } - elseif (preg_match('/^\xff\xd8/', $contents)) { - // 0 beshort 0xffd8 JPEG image data - //error_log("check_feed_favicon: favicon_url=$favicon_url isa JPG image"); - } - else { - //error_log("check_feed_favicon: favicon_url=$favicon_url isa UNKNOWN type"); - $contents = ""; - } - } - - if ($contents) { - $fp = @fopen($icon_file, "w"); - - if ($fp) { - fwrite($fp, $contents); - fclose($fp); - chmod($icon_file, 0644); - } - } - } - return $icon_file; - } - } - function initialize_user_prefs($uid, $profile = false) { $uid = db_escape_string($uid); @@ -1105,26 +1042,6 @@ return $data; } - function getCategoryTitle($cat_id) { - - if ($cat_id == -1) { - return __("Special"); - } else if ($cat_id == -2) { - return __("Labels"); - } else { - - $result = db_query("SELECT title FROM ttrss_feed_categories WHERE - id = '$cat_id'"); - - if (db_num_rows($result) == 1) { - return db_fetch_result($result, 0, "title"); - } else { - return __("Uncategorized"); - } - } - } - - function getCategoryCounters() { $ret_arr = array(); @@ -1147,7 +1064,7 @@ $line["cat_id"] = (int) $line["cat_id"]; if ($line["num_children"] > 0) { - $child_counter = getCategoryChildrenUnread($line["cat_id"], $_SESSION["uid"]); + $child_counter = Feeds::getCategoryChildrenUnread($line["cat_id"], $_SESSION["uid"]); } else { $child_counter = 0; } @@ -1172,38 +1089,11 @@ return Feeds::getFeedArticles($feed, $is_cat, true, $_SESSION["uid"]); } - function getLabelUnread($label_id, $owner_uid = false) { - if (!$owner_uid) $owner_uid = $_SESSION["uid"]; - - $result = db_query("SELECT COUNT(ref_id) AS unread FROM ttrss_user_entries, ttrss_user_labels2 - WHERE owner_uid = '$owner_uid' AND unread = true AND label_id = '$label_id' AND article_id = ref_id"); - - if (db_num_rows($result) != 0) { - return db_fetch_result($result, 0, "unread"); - } else { - return 0; - } - } - - function getGlobalUnread($user_id = false) { - - if (!$user_id) { - $user_id = $_SESSION["uid"]; - } - - $result = db_query("SELECT SUM(value) AS c_id FROM ttrss_counters_cache - WHERE owner_uid = '$user_id' AND feed_id > 0"); - - $c_id = db_fetch_result($result, 0, "c_id"); - - return $c_id; - } - function getGlobalCounters($global_unread = -1) { $ret_arr = array(); if ($global_unread == -1) { - $global_unread = getGlobalUnread(); + $global_unread = Feeds::getGlobalUnread(); } $cv = array("id" => "global-unread", diff --git a/include/functions2.php b/include/functions2.php index 7ebf4a0cf..a47e9c463 100644 --- a/include/functions2.php +++ b/include/functions2.php @@ -506,7 +506,7 @@ $unread = getFeedUnread($feed, $cat_view); if ($cat_view && $feed > 0 && $include_children) - $unread += getCategoryChildrenUnread($feed); + $unread += Feeds::getCategoryChildrenUnread($feed); if ($unread > 0) { $view_query_part = " unread = true AND "; @@ -670,7 +670,7 @@ $feed_title = T_sprintf("Search results: %s", $search); } else { if ($cat_view) { - $feed_title = getCategoryTitle($feed); + $feed_title = Feeds::getCategoryTitle($feed); } else { if (is_numeric($feed) && $feed > 0) { $result = db_query("SELECT title,site_url,last_error,last_updated diff --git a/include/rssfuncs.php b/include/rssfuncs.php index 4fdd2843f..bb8127547 100644 --- a/include/rssfuncs.php +++ b/include/rssfuncs.php @@ -4,6 +4,8 @@ define_default('DAEMON_SLEEP_INTERVAL', 120); define_default('_MIN_CACHE_FILE_SIZE', 1024); + // TODO: this needs to be removed from global namespace into classes/RSS.php or something + function calculate_article_hash($article, $pluginhost) { $tmp = ""; @@ -1513,7 +1515,7 @@ $count = update_feedbrowser_cache(); _debug("Feedbrowser updated, $count feeds processed."); - purge_orphans( true); + Article::purge_orphans( true); cleanup_counters_cache($debug); //$rc = cleanup_tags( 14, 50000); @@ -1522,3 +1524,53 @@ PluginHost::getInstance()->run_hooks(PluginHost::HOOK_HOUSE_KEEPING, "hook_house_keeping", ""); } + function check_feed_favicon($site_url, $feed) { + # print "FAVICON [$site_url]: $favicon_url\n"; + + $icon_file = ICONS_DIR . "/$feed.ico"; + + if (!file_exists($icon_file)) { + $favicon_url = get_favicon_url($site_url); + + if ($favicon_url) { + // Limiting to "image" type misses those served with text/plain + $contents = fetch_file_contents($favicon_url); // , "image"); + + if ($contents) { + // Crude image type matching. + // Patterns gleaned from the file(1) source code. + if (preg_match('/^\x00\x00\x01\x00/', $contents)) { + // 0 string \000\000\001\000 MS Windows icon resource + //error_log("check_feed_favicon: favicon_url=$favicon_url isa MS Windows icon resource"); + } + elseif (preg_match('/^GIF8/', $contents)) { + // 0 string GIF8 GIF image data + //error_log("check_feed_favicon: favicon_url=$favicon_url isa GIF image"); + } + elseif (preg_match('/^\x89PNG\x0d\x0a\x1a\x0a/', $contents)) { + // 0 string \x89PNG\x0d\x0a\x1a\x0a PNG image data + //error_log("check_feed_favicon: favicon_url=$favicon_url isa PNG image"); + } + elseif (preg_match('/^\xff\xd8/', $contents)) { + // 0 beshort 0xffd8 JPEG image data + //error_log("check_feed_favicon: favicon_url=$favicon_url isa JPG image"); + } + else { + //error_log("check_feed_favicon: favicon_url=$favicon_url isa UNKNOWN type"); + $contents = ""; + } + } + + if ($contents) { + $fp = @fopen($icon_file, "w"); + + if ($fp) { + fwrite($fp, $contents); + fclose($fp); + chmod($icon_file, 0644); + } + } + } + return $icon_file; + } + }