]> git.wh0rd.org Git - tt-rss.git/commitdiff
remove debug_enabled global
authorAndrew Dolgov <fox@madoka.volgo-balt.ru>
Fri, 19 Apr 2013 09:42:40 +0000 (13:42 +0400)
committerAndrew Dolgov <fox@madoka.volgo-balt.ru>
Fri, 19 Apr 2013 09:42:40 +0000 (13:42 +0400)
include/functions.php
include/rssfuncs.php

index 004f4db2f1bb77c0ee0ec5f3c9efd5e9feddff96..88484cadd6a925ff6284934a77fa7993a8d43568 100644 (file)
        $utc_tz = new DateTimeZone('UTC');
        $schema_version = false;
 
-       global $_debug_enabled;
-       $_debug_enabled = true;
-
        /**
         * Print a timestamped debug message.
         *
         * @param string $msg The debug message.
         * @return void
         */
-       function _debug($msg) {
-               global $_debug_enabled;
-
-               if (!$_debug_enabled) return;
+       function _debug($msg, $show = true) {
 
                $ts = strftime("%H:%M:%S", time());
                if (function_exists('posix_getpid')) {
                        $ts = "$ts/" . posix_getpid();
                }
 
-               if (!(defined('QUIET') && QUIET)) {
+               if ($show && !(defined('QUIET') && QUIET)) {
                        print "[$ts] $msg\n";
                }
 
 
        } // function _debug
 
-       function _debug_enable($enabled) {
-               global $_debug_enabled;
-
-               $old = $_debug_enabled;
-               $_debug_enabled = $enabled;
-
-               return $old;
-       }
-
        /**
         * Purge a feed old posts.
         *
index 2f9ba4260cca9c79ac18e38add03902733155cb3..6ccf520af11efc2a6022c18ec008af1f347b415d 100644 (file)
 
                $debug_enabled = defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug'];
 
-               $prev_debug_enabled = _debug_enable($debug_enabled);
-
-               _debug("start");
+               _debug("start", $debug_enabled);
 
                $result = db_query("SELECT id,update_interval,auth_login,
                        feed_url,auth_pass,cache_images,last_updated,
                        FROM ttrss_feeds WHERE id = '$feed'");
 
                if (db_num_rows($result) == 0) {
-                       _debug("feed $feed NOT FOUND/SKIPPED");
+                       _debug("feed $feed NOT FOUND/SKIPPED", $debug_enabled);
                        return false;
                }
 
                $cache_age = ($no_cache || is_null($last_updated) || $last_updated == '1970-01-01 00:00:00') ?
                        30 : get_feed_update_interval($feed) * 60;
 
-               _debug("cache filename: $cache_filename exists: " . file_exists($cache_filename));
-               _debug("cache age: $cache_age; no cache: $no_cache");
+               _debug("cache filename: $cache_filename exists: " . file_exists($cache_filename), $debug_enabled);
+               _debug("cache age: $cache_age; no cache: $no_cache", $debug_enabled);
 
                $cached_feed_data_hash = false;
 
                        !$auth_login && !$auth_pass &&
                        filemtime($cache_filename) > time() - $cache_age) {
 
-                               _debug("using local cache.");
+                               _debug("using local cache.", $debug_enabled);
 
                                if ($cache_timestamp > $last_updated_timestamp) {
                                        @$rss_data = file_get_contents($cache_filename);
                                                @$rss = unserialize($rss_data);
                                        }
                                } else if (!$force_refetch) {
-                                       _debug("local cache valid and older than last_updated, nothing to do.");
+                                       _debug("local cache valid and older than last_updated, nothing to do.", $debug_enabled);
                                        return;
                                }
                }
                if (!$rss) {
 
                        if (!$feed_data) {
-                               _debug("fetching [$fetch_url] (ts: $cache_timestamp/$last_updated_timestamp)");
+                               _debug("fetching [$fetch_url] (ts: $cache_timestamp/$last_updated_timestamp)", $debug_enabled);
 
                                $feed_data = fetch_file_contents($fetch_url, false,
                                        $auth_login, $auth_pass, false,
 
                                $feed_data = trim($feed_data);
 
-                               _debug("fetch done.");
+                               _debug("fetch done.", $debug_enabled);
 
-                               $error = verify_feed_xml($feed_data);
+                               if ($feed_data) {
+                                       $error = verify_feed_xml($feed_data);
 
-                               if ($error) {
-                                       _debug("error verifying XML, code: " . $error->code);
+                                       if ($error) {
+                                               _debug("error verifying XML, code: " . $error->code, $debug_enabled);
 
-                                       if ($error->code == 26) {
-                                               _debug("got error 26, trying to decode entities...");
+                                               if ($error->code == 26) {
+                                                       _debug("got error 26, trying to decode entities...", $debug_enabled);
 
-                                               $feed_data = html_entity_decode($feed_data, ENT_COMPAT, 'UTF-8');
+                                                       $feed_data = html_entity_decode($feed_data, ENT_COMPAT, 'UTF-8');
 
-                                               $error = verify_feed_xml($feed_data);
+                                                       $error = verify_feed_xml($feed_data);
 
-                                               if ($error) $feed_data = '';
+                                                       if ($error) $feed_data = '';
+                                               }
                                        }
                                }
                        }
                                global $fetch_last_error;
                                global $fetch_last_error_code;
 
-                               _debug("unable to fetch: $fetch_last_error [$fetch_last_error_code]");
+                               _debug("unable to fetch: $fetch_last_error [$fetch_last_error_code]", $debug_enabled);
 
                                $error_escaped = '';
 
                                if ($fetch_last_error_code != 304) {
                                        $error_escaped = db_escape_string($fetch_last_error);
                                } else {
-                                       _debug("source claims data not modified, nothing to do.");
+                                       _debug("source claims data not modified, nothing to do.", $debug_enabled);
                                }
 
                                db_query(
                }
 
                $pluginhost = new PluginHost();
-               $pluginhost->set_debug($debug_enabled);
+               $pluginhost->set_debug($debug_enabled, $debug_enabled);
                $user_plugins = get_pref("_ENABLED_PLUGINS", $owner_uid);
 
                $pluginhost->load(PLUGINS, PluginHost::KIND_ALL);
                                $new_rss_hash = sha1($rss_data);
 
                                if ($new_rss_hash != $rss_hash) {
-                                       _debug("saving $cache_filename");
+                                       _debug("saving $cache_filename", $debug_enabled);
                                        @file_put_contents($cache_filename, serialize($rss));
                                }
                        }
                        // We use local pluginhost here because we need to load different per-user feed plugins
                        $pluginhost->run_hooks(PluginHost::HOOK_FEED_PARSED, "hook_feed_parsed", $rss);
 
-                       _debug("processing feed data...");
+                       _debug("processing feed data...", $debug_enabled);
 
 //                     db_query("BEGIN");
 
                        $site_url = db_escape_string(mb_substr(rewrite_relative_url($fetch_url, $rss->get_link()), 0, 245));
 
                        if ($favicon_needs_check || $force_refetch) {
-                               _debug("checking favicon...");
+                               _debug("checking favicon...", $debug_enabled);
 
                                check_feed_favicon($site_url, $feed, $link);
                                $favicon_file = ICONS_DIR . "/$feed.ico";
 
                                $feed_title = db_escape_string($rss->get_title());
 
-                               _debug("registering title: $feed_title");
+                               _debug("registering title: $feed_title", $debug_enabled);
 
                                db_query("UPDATE ttrss_feeds SET
                                        title = '$feed_title' WHERE id = '$feed'");
                                        site_url = '$site_url' WHERE id = '$feed'");
                        }
 
-                       _debug("loading filters & labels...");
+                       _debug("loading filters & labels...", $debug_enabled);
 
                        $filters = load_filters($feed, $owner_uid);
                        $labels = get_all_labels($owner_uid);
 
-                       _debug("" . count($filters) . " filters loaded.");
+                       _debug("" . count($filters) . " filters loaded.", $debug_enabled);
 
                        $items = $rss->get_items();
 
                        if (!is_array($items)) {
-                               _debug("no articles found.");
+                               _debug("no articles found.", $debug_enabled);
 
                                db_query("UPDATE ttrss_feeds
                                        SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
 
                        if ($pubsub_state != 2 && PUBSUBHUBBUB_ENABLED) {
 
-                               _debug("checking for PUSH hub...");
+                               _debug("checking for PUSH hub...", $debug_enabled);
 
                                $feed_hub_url = false;
 
                                        }
                                }
 
-                               _debug("feed hub url: $feed_hub_url");
+                               _debug("feed hub url: $feed_hub_url", $debug_enabled);
 
                                if ($feed_hub_url && function_exists('curl_init') &&
                                        !ini_get("open_basedir")) {
 
                                        $rc = $s->subscribe($fetch_url);
 
-                                       _debug("feed hub url found, subscribe request sent.");
+                                       _debug("feed hub url found, subscribe request sent.", $debug_enabled);
 
                                        db_query("UPDATE ttrss_feeds SET pubsub_state = 1
                                                WHERE id = '$feed'");
                                }
                        }
 
-                       _debug("processing articles...");
+                       _debug("processing articles...", $debug_enabled);
 
                        foreach ($items as $item) {
                                if ($_REQUEST['xdebug'] == 3) {
 
                                $entry_guid_hashed = db_escape_string('SHA1:' . sha1($entry_guid));
 
-                               _debug("guid $entry_guid / $entry_guid_hashed");
+                               _debug("guid $entry_guid / $entry_guid_hashed", $debug_enabled);
 
                                $entry_timestamp = "";
 
 
                                $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
 
-                               _debug("date $entry_timestamp [$entry_timestamp_fmt]");
+                               _debug("date $entry_timestamp [$entry_timestamp_fmt]", $debug_enabled);
 
                                $entry_title = html_entity_decode($item->get_title(), ENT_COMPAT, 'UTF-8');
 
                                $entry_link = rewrite_relative_url($site_url, $item->get_link());
 
-                               _debug("title $entry_title");
-                               _debug("link $entry_link");
+                               _debug("title $entry_title", $debug_enabled);
+                               _debug("link $entry_link", $debug_enabled);
 
                                if (!$entry_title) $entry_title = date("Y-m-d H:i:s", $entry_timestamp);;
 
                                        $num_comments = 0;
                                }
 
-                               _debug("author $entry_author");
-                               _debug("num_comments: $num_comments");
-                               _debug("looking for tags [1]...");
+                               _debug("author $entry_author", $debug_enabled);
+                               _debug("num_comments: $num_comments", $debug_enabled);
+                               _debug("looking for tags [1]...", $debug_enabled);
 
                                // parse <category> entries into tags
 
                                        }
                                }
 
-                               _debug("category tags:");
-                               _debug("looking for tags [2]...");
+                               _debug("category tags:", $debug_enabled);
+                               _debug("looking for tags [2]...", $debug_enabled);
 
                                $entry_tags = array_unique($additional_tags);
 
                                for ($i = 0; $i < count($entry_tags); $i++)
                                        $entry_tags[$i] = mb_strtolower($entry_tags[$i], 'utf-8');
 
-                               _debug("done collecting data.");
+                               _debug("done collecting data.", $debug_enabled);
 
                                // TODO: less memory-hungry implementation
 
-                               _debug("applying plugin filters..");
+                               _debug("applying plugin filters..", $debug_enabled);
 
                                // FIXME not sure if owner_uid is a good idea here, we may have a base entry without user entry (?)
                                $result = db_query("SELECT plugin_data,title,content,link,tag_cache,author FROM ttrss_entries, ttrss_user_entries
                                $entry_content = $article["content"]; // escaped below
 
 
-                               _debug("plugin data: $entry_plugin_data");
+                               _debug("plugin data: $entry_plugin_data", $debug_enabled);
 
                                if ($cache_images && is_writable(CACHE_DIR . '/images'))
                                        cache_images($entry_content, $site_url, $debug_enabled);
 
                                if (db_num_rows($result) == 0) {
 
-                                       _debug("base guid [$entry_guid] not found");
+                                       _debug("base guid [$entry_guid] not found", $debug_enabled);
 
                                        // base post entry does not exist, create it
 
 
                                if (db_num_rows($result) == 1) {
 
-                                       _debug("base guid found, checking for user record");
+                                       _debug("base guid found, checking for user record", $debug_enabled);
 
                                        // this will be used below in update handler
                                        $orig_content_hash = db_fetch_result($result, 0, "content_hash");
 
                                        /* $stored_guid = db_fetch_result($result, 0, "guid");
                                        if ($stored_guid != $entry_guid_hashed) {
-                                               if ($debug_enabled) _debug("upgrading compat guid to hashed one");
+                                               if ($debug_enabled) _debug("upgrading compat guid to hashed one", $debug_enabled);
 
                                                db_query("UPDATE ttrss_entries SET guid = '$entry_guid_hashed' WHERE
                                                        id = '$ref_id'");
                                                $entry_tags);
 
                                        if ($debug_enabled) {
-                                               _debug("article filters: ");
+                                               _debug("article filters: ", $debug_enabled);
                                                if (count($article_filters) != 0) {
                                                        print_r($article_filters);
                                                }
 
                                        $score = calculate_article_score($article_filters);
 
-                                       _debug("initial score: $score");
+                                       _debug("initial score: $score", $debug_enabled);
 
                                        $query = "SELECT ref_id, int_id FROM ttrss_user_entries WHERE
                                                        ref_id = '$ref_id' AND owner_uid = '$owner_uid'
                                        // okay it doesn't exist - create user entry
                                        if (db_num_rows($result) == 0) {
 
-                                               _debug("user record not found, creating...");
+                                               _debug("user record not found, creating...", $debug_enabled);
 
                                                if ($score >= -500 && !find_article_filter($article_filters, 'catchup')) {
                                                        $unread = 'true';
 
                                                        $ngram_similar = db_fetch_result($result, 0, "similar");
 
-                                                       _debug("N-gram similar results: $ngram_similar");
+                                                       _debug("N-gram similar results: $ngram_similar", $debug_enabled);
 
                                                        if ($ngram_similar > 0) {
                                                                $unread = 'false';
                                                        $entry_int_id = db_fetch_result($result, 0, "int_id");
                                                }
                                        } else {
-                                               _debug("user record FOUND");
+                                               _debug("user record FOUND", $debug_enabled);
 
                                                $entry_ref_id = db_fetch_result($result, 0, "ref_id");
                                                $entry_int_id = db_fetch_result($result, 0, "int_id");
                                        }
 
-                                       _debug("RID: $entry_ref_id, IID: $entry_int_id");
+                                       _debug("RID: $entry_ref_id, IID: $entry_int_id", $debug_enabled);
 
                                        $post_needs_update = false;
                                        $update_insignificant = false;
                                        if ($post_needs_update) {
 
                                                if (defined('DAEMON_EXTENDED_DEBUG')) {
-                                                       _debug("post $entry_guid_hashed needs update...");
+                                                       _debug("post $entry_guid_hashed needs update...", $debug_enabled);
                                                }
 
 //                                             print "<!-- post $orig_title needs update : $post_needs_update -->";
 
                                db_query("COMMIT");
 
-                               _debug("assigning labels...");
+                               _debug("assigning labels...", $debug_enabled);
 
                                assign_article_to_label_filters($entry_ref_id, $article_filters,
                                        $owner_uid, $article_labels);
 
-                               _debug("looking for enclosures...");
+                               _debug("looking for enclosures...", $debug_enabled);
 
                                // enclosures
 
                                }
 
                                if ($debug_enabled) {
-                                       _debug("article enclosures:");
+                                       _debug("article enclosures:", $debug_enabled);
                                        print_r($enclosures);
                                }
 
                                $filtered_tags = array_unique($filtered_tags);
 
                                if ($debug_enabled) {
-                                       _debug("filtered article tags:");
+                                       _debug("filtered article tags:", $debug_enabled);
                                        print_r($filtered_tags);
                                }
 
                                }
 
                                if (get_pref("AUTO_ASSIGN_LABELS", $owner_uid, false)) {
-                                       _debug("auto-assigning labels...");
+                                       _debug("auto-assigning labels...", $debug_enabled);
 
                                        foreach ($labels as $label) {
                                                $caption = preg_quote($label["caption"]);
                                        }
                                }
 
-                               _debug("article processed");
+                               _debug("article processed", $debug_enabled);
                        }
 
                        if (!$last_updated) {
-                               _debug("new feed, catching it up...");
+                               _debug("new feed, catching it up...", $debug_enabled);
                                catchup_feed($feed, false, $owner_uid);
                        }
 
-                       _debug("purging feed...");
+                       _debug("purging feed...", $debug_enabled);
 
                        purge_feed($feed, 0, $debug_enabled);
 
 
                        $error_msg = db_escape_string(mb_substr($rss->error(), 0, 245));
 
-                       _debug("error fetching feed: $error_msg");
+                       _debug("error fetching feed: $error_msg", $debug_enabled);
 
                        db_query(
                                "UPDATE ttrss_feeds SET last_error = '$error_msg',
 
                unset($rss);
 
-               _debug("done");
-
-               _debug_enable($prev_debug_enabled);
+               _debug("done", $debug_enabled);
        }
 
        function cache_images($html, $site_url, $debug) {