From e6c886bf66928d4bd496672f12b79b547747677b Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Fri, 5 May 2017 18:10:07 +0300 Subject: [PATCH] wrap rssfuncs into rssutils class --- classes/api.php | 7 +- classes/feeds.php | 10 +- classes/pref/feeds.php | 14 +- classes/pref/filters.php | 4 +- classes/rpc.php | 4 +- include/rssfuncs.php => classes/rssutils.php | 193 ++++++++++--------- plugins/auto_assign_labels/init.php | 3 +- update.php | 13 +- update_daemon2.php | 1 - 9 files changed, 117 insertions(+), 132 deletions(-) rename include/rssfuncs.php => classes/rssutils.php (90%) diff --git a/classes/api.php b/classes/api.php index 1fc84884..220d3798 100644 --- a/classes/api.php +++ b/classes/api.php @@ -412,12 +412,10 @@ class API extends Handler { } function updateFeed() { - require_once "include/rssfuncs.php"; - $feed_id = (int) $this->dbh->escape_string($_REQUEST["feed_id"]); if (!ini_get("open_basedir")) { - update_rss_feed($feed_id); + RSSUtils::update_rss_feed($feed_id); } $this->wrap(self::STATUS_OK, array("status" => "OK")); @@ -678,8 +676,7 @@ class API extends Handler { $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images")); if (!$cache_images && time() - $last_updated > 120) { - include "rssfuncs.php"; - update_rss_feed($feed_id, true); + RSSUtils::update_rss_feed($feed_id, true); } else { db_query("UPDATE ttrss_feeds SET last_updated = '1970-01-01', last_update_started = '1970-01-01' WHERE id = '$feed_id'"); diff --git a/classes/feeds.php b/classes/feeds.php index 6c85bbf3..809a8ff9 100755 --- a/classes/feeds.php +++ b/classes/feeds.php @@ -202,8 +202,7 @@ class Feeds extends Handler_Protected { $cache_images = sql_bool_to_bool($this->dbh->fetch_result($result, 0, "cache_images")); if (!$cache_images && time() - $last_updated > 120) { - include "rssfuncs.php"; - update_rss_feed($feed, true); + RSSUtils::update_rss_feed($feed, true); } else { $this->dbh->query("UPDATE ttrss_feeds SET last_updated = '1970-01-01', last_update_started = '1970-01-01' WHERE id = '$feed'"); @@ -1222,8 +1221,7 @@ class Feeds extends Handler_Protected {
@@ -1491,8 +1489,6 @@ class Feeds extends Handler_Protected { global $fetch_last_error; global $fetch_last_error_content; - require_once "include/rssfuncs.php"; - $url = fix_url($url); if (!$url || !validate_feed_url($url)) return array("code" => 2); @@ -1550,7 +1546,7 @@ class Feeds extends Handler_Protected { $feed_id = db_fetch_result($result, 0, "id"); if ($feed_id) { - set_basic_feed_info($feed_id); + RSSUtils::set_basic_feed_info($feed_id); } return array("code" => 1, "feed_id" => (int) $feed_id); diff --git a/classes/pref/feeds.php b/classes/pref/feeds.php index f1eea93f..78e0f2f2 100755 --- a/classes/pref/feeds.php +++ b/classes/pref/feeds.php @@ -1024,9 +1024,7 @@ class Pref_Feeds extends Handler_Protected { WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]); if ($reset_basic_info) { - require_once "rssfuncs.php"; - - set_basic_feed_info($feed_id); + RSSUtils::set_basic_feed_info($feed_id); } PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_SAVE_FEED, @@ -1147,8 +1145,6 @@ class Pref_Feeds extends Handler_Protected { } function rescore() { - require_once "rssfuncs.php"; - $ids = explode(",", $this->dbh->escape_string($_REQUEST["ids"])); foreach ($ids as $id) { @@ -1170,11 +1166,11 @@ class Pref_Feeds extends Handler_Protected { $tags = Article::get_article_tags($line["ref_id"]); - $article_filters = get_article_filters($filters, $line['title'], + $article_filters = RSSUtils::get_article_filters($filters, $line['title'], $line['content'], $line['link'], strtotime($line['updated']), $line['author'], $tags); - $new_score = calculate_article_score($article_filters); + $new_score = RSSUtils::calculate_article_score($article_filters); if (!$scores[$new_score]) $scores[$new_score] = array(); @@ -1227,11 +1223,11 @@ class Pref_Feeds extends Handler_Protected { $tags = Article::get_article_tags($line["ref_id"]); - $article_filters = get_article_filters($filters, $line['title'], + $article_filters = RSSUtils::get_article_filters($filters, $line['title'], $line['content'], $line['link'], strtotime($line['updated']), $line['author'], $tags); - $new_score = calculate_article_score($article_filters); + $new_score = RSSUtils::calculate_article_score($article_filters); if (!$scores[$new_score]) $scores[$new_score] = array(); diff --git a/classes/pref/filters.php b/classes/pref/filters.php index e4943a5a..e5abf462 100755 --- a/classes/pref/filters.php +++ b/classes/pref/filters.php @@ -44,8 +44,6 @@ class Pref_Filters extends Handler_Protected { } function testFilterDo() { - require_once "include/rssfuncs.php"; - $offset = (int) db_escape_string($_REQUEST["offset"]); $limit = (int) db_escape_string($_REQUEST["limit"]); @@ -129,7 +127,7 @@ class Pref_Filters extends Handler_Protected { while ($line = db_fetch_assoc($result)) { - $rc = get_article_filters(array($filter), $line['title'], $line['content'], $line['link'], + $rc = RSSUtils::get_article_filters(array($filter), $line['title'], $line['content'], $line['link'], $line['author'], explode(",", $line['tag_cache'])); if (count($rc) > 0) { diff --git a/classes/rpc.php b/classes/rpc.php index a31452be..70ee423a 100755 --- a/classes/rpc.php +++ b/classes/rpc.php @@ -538,8 +538,6 @@ class RPC extends Handler_Protected { $feed_id = -1; - require_once "rssfuncs.php"; - $num_updated = 0; $tstart = time(); @@ -548,7 +546,7 @@ class RPC extends Handler_Protected { $feed_id = $line["id"]; if (time() - $tstart < ini_get("max_execution_time") * 0.7) { - update_rss_feed($feed_id, true); + RSSUtils::update_rss_feed($feed_id, true); ++$num_updated; } else { break; diff --git a/include/rssfuncs.php b/classes/rssutils.php similarity index 90% rename from include/rssfuncs.php rename to classes/rssutils.php index 33b6ff6e..84d6941d 100644 --- a/include/rssfuncs.php +++ b/classes/rssutils.php @@ -1,12 +1,11 @@ $v) { @@ -22,7 +21,7 @@ return sha1(implode(",", $pluginhost->get_plugin_names()) . $tmp); } - function update_feedbrowser_cache() { + static function update_feedbrowser_cache() { $result = db_query("SELECT feed_url, site_url, title, COUNT(id) AS subscribers FROM ttrss_feeds WHERE feed_url NOT IN (SELECT feed_url FROM ttrss_feeds @@ -62,7 +61,7 @@ } - function update_daemon_common($limit = DAEMON_FEED_LIMIT, $debug = true) { + static function update_daemon_common($limit = DAEMON_FEED_LIMIT, $debug = true) { // Process all other feeds using last_updated and interval parameters $schema_version = get_schema_version(); @@ -196,7 +195,7 @@ array_push($batch_owners, $tline["owner_uid"]); $fstarted = microtime(true); - update_rss_feed($tline["id"], true, false); + RSSUtils::update_rss_feed($tline["id"], true, false); _debug_suppress(false); _debug(sprintf(" %.4f (sec)", microtime(true) - $fstarted)); @@ -214,7 +213,7 @@ foreach ($batch_owners as $owner_uid) { _debug("Running housekeeping tasks for user $owner_uid..."); - housekeeping_user($owner_uid); + RSSUtils::housekeeping_user($owner_uid); } // Send feed digests by email if needed. @@ -225,7 +224,7 @@ } // this is used when subscribing - function set_basic_feed_info($feed) { + static function set_basic_feed_info($feed) { $feed = db_escape_string($feed); @@ -286,9 +285,9 @@ } /** - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - function update_rss_feed($feed, $no_cache = false) { + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + static function update_rss_feed($feed, $no_cache = false) { $debug_enabled = defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']; @@ -310,7 +309,7 @@ // this is not optimal currently as it fetches stuff separately TODO: optimize if ($title == "[Unknown]") { _debug("setting basic feed info for $feed..."); - set_basic_feed_info($feed); + RSSUtils::set_basic_feed_info($feed); } $result = db_query("SELECT id,update_interval,auth_login, @@ -494,22 +493,22 @@ _debug("checking favicon...", $debug_enabled); - check_feed_favicon($site_url, $feed); + RSSUtils::check_feed_favicon($site_url, $feed); $favicon_modified_new = @filemtime($favicon_file); if ($favicon_modified_new > $favicon_modified) $favicon_avg_color = ''; if (file_exists($favicon_file) && function_exists("imagecreatefromstring") && $favicon_avg_color == '') { - require_once "colors.php"; + require_once "colors.php"; - db_query("UPDATE ttrss_feeds SET favicon_avg_color = 'fail' WHERE + db_query("UPDATE ttrss_feeds SET favicon_avg_color = 'fail' WHERE id = '$feed'"); - $favicon_color = db_escape_string( - calculate_avg_color($favicon_file)); + $favicon_color = db_escape_string( + calculate_avg_color($favicon_file)); - $favicon_colorstring = ",favicon_avg_color = '".$favicon_color."'"; + $favicon_colorstring = ",favicon_avg_color = '".$favicon_color."'"; } else if ($favicon_avg_color == 'fail') { _debug("floicon failed on this file, not trying to recalculate avg color", $debug_enabled); } @@ -601,7 +600,7 @@ $entry_guid = $item->get_id(); if (!$entry_guid) $entry_guid = $item->get_link(); - if (!$entry_guid) $entry_guid = make_guid_from_title($item->get_title()); + if (!$entry_guid) $entry_guid = RSSUtils::make_guid_from_title($item->get_title()); if (!$entry_guid) continue; $entry_guid = "$owner_uid,$entry_guid"; @@ -710,10 +709,10 @@ "fetch_url" => $fetch_url, "site_url" => $site_url, "cache_images" => $cache_images) - ); + ); $entry_plugin_data = ""; - $entry_current_hash = calculate_article_hash($article, $pluginhost); + $entry_current_hash = RSSUtils::calculate_article_hash($article, $pluginhost); _debug("article hash: $entry_current_hash [stored=$entry_stored_hash]", $debug_enabled); @@ -771,7 +770,7 @@ $matched_rules = array(); - $article_filters = get_article_filters($filters, $article["title"], + $article_filters = RSSUtils::get_article_filters($filters, $article["title"], $article["content"], $article["link"], $article["author"], $article["tags"], $matched_rules); @@ -789,7 +788,7 @@ } } - $plugin_filter_names = find_article_filters($article_filters, "plugin"); + $plugin_filter_names = RSSUtils::find_article_filters($article_filters, "plugin"); $plugin_filter_actions = $pluginhost->get_filter_actions(); if (count($plugin_filter_names) > 0) { @@ -839,7 +838,7 @@ _debug("force catchup: $entry_force_catchup"); if ($cache_images && is_writable(CACHE_DIR . '/images')) - cache_media($entry_content, $site_url, $debug_enabled); + RSSUtils::cache_media($entry_content, $site_url, $debug_enabled); $entry_content = db_escape_string($entry_content, false); @@ -911,12 +910,12 @@ id = '$ref_id'"); } */ - if (find_article_filter($article_filters, "filter")) { + if (RSSUtils::find_article_filter($article_filters, "filter")) { //db_query("COMMIT"); // close transaction in progress continue; } - $score = calculate_article_score($article_filters) + $entry_score_modifier; + $score = RSSUtils::calculate_article_score($article_filters) + $entry_score_modifier; _debug("initial score: $score [including plugin modifier: $entry_score_modifier]", $debug_enabled); @@ -934,7 +933,7 @@ _debug("user record not found, creating...", $debug_enabled); - if ($score >= -500 && !find_article_filter($article_filters, 'catchup') && !$entry_force_catchup) { + if ($score >= -500 && !RSSUtils::find_article_filter($article_filters, 'catchup') && !$entry_force_catchup) { $unread = 'true'; $last_read_qpart = 'NULL'; } else { @@ -942,13 +941,13 @@ $last_read_qpart = 'NOW()'; } - if (find_article_filter($article_filters, 'mark') || $score > 1000) { + if (RSSUtils::find_article_filter($article_filters, 'mark') || $score > 1000) { $marked = 'true'; } else { $marked = 'false'; } - if (find_article_filter($article_filters, 'publish')) { + if (RSSUtils::find_article_filter($article_filters, 'publish')) { $published = 'true'; } else { $published = 'false'; @@ -994,7 +993,7 @@ _debug("RID: $entry_ref_id, IID: $entry_int_id", $debug_enabled); if (DB_TYPE == "pgsql") { - $tsvector_combined = db_escape_string(mb_substr($entry_title . ' ' . strip_tags(str_replace('<', ' <', $entry_content)), + $tsvector_combined = db_escape_string(mb_substr($entry_title . ' ' . strip_tags(str_replace('<', ' <', $entry_content)), 0, 1000000)); $tsvector_qpart = "tsvector_combined = to_tsvector('$feed_language', '$tsvector_combined'),"; @@ -1037,7 +1036,7 @@ _debug("assigning labels [filters]...", $debug_enabled); - assign_article_to_label_filters($entry_ref_id, $article_filters, + RSSUtils::assign_article_to_label_filters($entry_ref_id, $article_filters, $owner_uid, $article_labels); _debug("looking for enclosures...", $debug_enabled); @@ -1058,7 +1057,7 @@ } if ($cache_images && is_writable(CACHE_DIR . '/images')) - cache_enclosures($enclosures, $site_url, $debug_enabled); + RSSUtils::cache_enclosures($enclosures, $site_url, $debug_enabled); if ($debug_enabled) { _debug("article enclosures:", $debug_enabled); @@ -1145,12 +1144,12 @@ WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND owner_uid = '$owner_uid' LIMIT 1"); - if ($result && db_num_rows($result) == 0) { + if ($result && db_num_rows($result) == 0) { - db_query("INSERT INTO ttrss_tags + db_query("INSERT INTO ttrss_tags (owner_uid,tag_name,post_int_id) VALUES ('$owner_uid','$tag', '$entry_int_id')"); - } + } array_push($tags_to_cache, $tag); } @@ -1205,7 +1204,7 @@ return true; } - function cache_enclosures($enclosures, $site_url, $debug) { + static function cache_enclosures($enclosures, $site_url, $debug) { foreach ($enclosures as $enc) { if (preg_match("/(image|audio|video)/", $enc[1])) { @@ -1229,7 +1228,7 @@ } } - function cache_media($html, $site_url, $debug) { + static function cache_media($html, $site_url, $debug) { libxml_use_internal_errors(true); $charset_hack = ' @@ -1263,7 +1262,7 @@ } } - function expire_error_log($debug) { + static function expire_error_log($debug) { if ($debug) _debug("Removing old error log entries..."); if (DB_TYPE == "pgsql") { @@ -1276,7 +1275,7 @@ } - function expire_lock_files($debug) { + static function expire_lock_files($debug) { //if ($debug) _debug("Removing old lock files..."); $num_deleted = 0; @@ -1297,7 +1296,7 @@ if ($debug) _debug("Removed $num_deleted old lock files."); } - function expire_cached_files($debug) { + static function expire_cached_files($debug) { foreach (array("simplepie", "images", "export", "upload") as $dir) { $cache_dir = CACHE_DIR . "/$dir"; @@ -1324,13 +1323,13 @@ } /** - * Source: http://www.php.net/manual/en/function.parse-url.php#104527 - * Returns the url query as associative array - * - * @param string query - * @return array params - */ - function convertUrlQuery($query) { + * Source: http://www.php.net/manual/en/function.parse-url.php#104527 + * Returns the url query as associative array + * + * @param string query + * @return array params + */ + static function convertUrlQuery($query) { $queryParts = explode('&', $query); $params = array(); @@ -1343,7 +1342,7 @@ return $params; } - function get_article_filters($filters, $title, $content, $link, $author, $tags, &$matched_rules = false) { + static function get_article_filters($filters, $title, $content, $link, $author, $tags, &$matched_rules = false) { $matches = array(); foreach ($filters as $filter) { @@ -1360,35 +1359,35 @@ continue; switch ($rule["type"]) { - case "title": - $match = @preg_match("/$reg_exp/iu", $title); - break; - case "content": - // we don't need to deal with multiline regexps - $content = preg_replace("/[\r\n\t]/", "", $content); + case "title": + $match = @preg_match("/$reg_exp/iu", $title); + break; + case "content": + // we don't need to deal with multiline regexps + $content = preg_replace("/[\r\n\t]/", "", $content); - $match = @preg_match("/$reg_exp/iu", $content); - break; - case "both": - // we don't need to deal with multiline regexps - $content = preg_replace("/[\r\n\t]/", "", $content); + $match = @preg_match("/$reg_exp/iu", $content); + break; + case "both": + // we don't need to deal with multiline regexps + $content = preg_replace("/[\r\n\t]/", "", $content); - $match = (@preg_match("/$reg_exp/iu", $title) || @preg_match("/$reg_exp/iu", $content)); - break; - case "link": - $match = @preg_match("/$reg_exp/iu", $link); - break; - case "author": - $match = @preg_match("/$reg_exp/iu", $author); - break; - case "tag": - foreach ($tags as $tag) { - if (@preg_match("/$reg_exp/iu", $tag)) { - $match = true; - break; + $match = (@preg_match("/$reg_exp/iu", $title) || @preg_match("/$reg_exp/iu", $content)); + break; + case "link": + $match = @preg_match("/$reg_exp/iu", $link); + break; + case "author": + $match = @preg_match("/$reg_exp/iu", $author); + break; + case "tag": + foreach ($tags as $tag) { + if (@preg_match("/$reg_exp/iu", $tag)) { + $match = true; + break; + } } - } - break; + break; } if ($rule_inverse) $match = !$match; @@ -1423,7 +1422,7 @@ return $matches; } - function find_article_filter($filters, $filter_name) { + static function find_article_filter($filters, $filter_name) { foreach ($filters as $f) { if ($f["type"] == $filter_name) { return $f; @@ -1432,7 +1431,7 @@ return false; } - function find_article_filters($filters, $filter_name) { + static function find_article_filters($filters, $filter_name) { $results = array(); foreach ($filters as $f) { @@ -1443,7 +1442,7 @@ return $results; } - function calculate_article_score($filters) { + static function calculate_article_score($filters) { $score = 0; foreach ($filters as $f) { @@ -1454,7 +1453,7 @@ return $score; } - function labels_contains_caption($labels, $caption) { + static function labels_contains_caption($labels, $caption) { foreach ($labels as $label) { if ($label[1] == $caption) { return true; @@ -1464,22 +1463,22 @@ return false; } - function assign_article_to_label_filters($id, $filters, $owner_uid, $article_labels) { + static function assign_article_to_label_filters($id, $filters, $owner_uid, $article_labels) { foreach ($filters as $f) { if ($f["type"] == "label") { - if (!labels_contains_caption($article_labels, $f["param"])) { + if (!RSSUtils::labels_contains_caption($article_labels, $f["param"])) { Labels::add_article($id, $f["param"], $owner_uid); } } } } - function make_guid_from_title($title) { + static function make_guid_from_title($title) { return preg_replace("/[ \"\',.:;]/", "-", mb_strtolower(strip_tags($title), 'utf-8')); } - function cleanup_counters_cache($debug) { + static function cleanup_counters_cache($debug) { $result = db_query("DELETE FROM ttrss_counters_cache WHERE feed_id > 0 AND (SELECT COUNT(id) FROM ttrss_feeds WHERE @@ -1497,7 +1496,7 @@ if ($debug) _debug("Removed $frows (feeds) $crows (cats) orphaned counter cache entries."); } - function housekeeping_user($owner_uid) { + static function housekeeping_user($owner_uid) { $tmph = new PluginHost(); load_user_plugins($owner_uid, $tmph); @@ -1505,16 +1504,16 @@ $tmph->run_hooks(PluginHost::HOOK_HOUSE_KEEPING, "hook_house_keeping", ""); } - function housekeeping_common($debug) { - expire_cached_files($debug); - expire_lock_files($debug); - expire_error_log($debug); + static function housekeeping_common($debug) { + RSSUtils::expire_cached_files($debug); + RSSUtils::expire_lock_files($debug); + RSSUtils::expire_error_log($debug); - $count = update_feedbrowser_cache(); + $count = RSSUtils::update_feedbrowser_cache(); _debug("Feedbrowser updated, $count feeds processed."); Article::purge_orphans( true); - cleanup_counters_cache($debug); + RSSUtils::cleanup_counters_cache($debug); //$rc = cleanup_tags( 14, 50000); //_debug("Cleaned $rc cached tags."); @@ -1522,8 +1521,8 @@ 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"; + static function check_feed_favicon($site_url, $feed) { + # print "FAVICON [$site_url]: $favicon_url\n"; $icon_file = ICONS_DIR . "/$feed.ico"; @@ -1572,3 +1571,7 @@ return $icon_file; } } + + + +} \ No newline at end of file diff --git a/plugins/auto_assign_labels/init.php b/plugins/auto_assign_labels/init.php index 1209a02a..911adb6b 100644 --- a/plugins/auto_assign_labels/init.php +++ b/plugins/auto_assign_labels/init.php @@ -40,8 +40,7 @@ class Auto_Assign_Labels extends Plugin { if ($caption && preg_match("/\b$caption\b/i", "$tags_str " . strip_tags($article["content"]) . " " . $article["title"])) { - # defined in rssfuncs.php - if (!labels_contains_caption($article["labels"], $caption)) { + if (!RSSUtils::labels_contains_caption($article["labels"], $caption)) { array_push($article["labels"], $label); } } diff --git a/update.php b/update.php index 2245ea02..fe2b3c57 100755 --- a/update.php +++ b/update.php @@ -9,7 +9,6 @@ require_once "autoload.php"; require_once "functions.php"; - require_once "rssfuncs.php"; require_once "config.php"; require_once "sanity_check.php"; require_once "db.php"; @@ -165,14 +164,14 @@ } if (isset($options["feeds"])) { - update_daemon_common(); - housekeeping_common(true); + RSSUtils::update_daemon_common(); + RSSUtils::housekeeping_common(true); PluginHost::getInstance()->run_hooks(PluginHost::HOOK_UPDATE_TASK, "hook_update_task", $op); } if (isset($options["feedbrowser"])) { - $count = update_feedbrowser_cache(); + $count = RSSUtils::update_feedbrowser_cache(); print "Finished, $count feeds processed.\n"; } @@ -192,10 +191,10 @@ _debug("warning: unable to create stampfile\n"); } - update_daemon_common(isset($options["pidlock"]) ? 50 : DAEMON_FEED_LIMIT); + RSSUtils::update_daemon_common(isset($options["pidlock"]) ? 50 : DAEMON_FEED_LIMIT); if (!isset($options["pidlock"]) || $options["task"] == 0) - housekeeping_common(true); + RSSUtils::housekeeping_common(true); PluginHost::getInstance()->run_hooks(PluginHost::HOOK_UPDATE_TASK, "hook_update_task", $op); } @@ -401,7 +400,7 @@ $_REQUEST['xdebug'] = 1; - $rc = update_rss_feed($feed) != false ? 0 : 1; + $rc = RSSUtils::update_rss_feed($feed) != false ? 0 : 1; exit($rc); } diff --git a/update_daemon2.php b/update_daemon2.php index 783217ce..1e6edb33 100755 --- a/update_daemon2.php +++ b/update_daemon2.php @@ -12,7 +12,6 @@ require_once "autoload.php"; require_once "functions.php"; require_once "config.php"; - require_once "rssfuncs.php"; // defaults define_default('PURGE_INTERVAL', 3600); // seconds -- 2.39.2