]> git.wh0rd.org - tt-rss.git/blobdiff - functions.php
move update daemon code to common function, reorganize backend.php (patch from landure)
[tt-rss.git] / functions.php
index a5da7b0bba3ad0b26dcc5cee2d46559d72b13b62..f9771879080d61ef883e9c54db8e0ce25d1d86ae 100644 (file)
        define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION . ')');
        define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
 
-       if (ENABLE_SIMPLEPIE) {
-               require_once "simplepie/simplepie.inc";
-       } else {
-               require_once "magpierss/rss_fetch.inc";
-               require_once 'magpierss/rss_utils.inc';
-       }
-
+       require_once "simplepie/simplepie.inc";
+       require_once "magpierss/rss_fetch.inc";
+       require_once 'magpierss/rss_utils.inc';
+
+       /**
+        * Print a timestamped debug message.
+        * 
+        * @param string $msg The debug message.
+        * @return void
+        */
        function _debug($msg) {
                $ts = strftime("%H:%M:%S", time());
+               $ts = "$ts/" . posix_getpid();
                print "[$ts] $msg\n";
-       }
+       } // function _debug
 
        function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
 
        function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
 
                if (!$_GET["daemon"] && !$ignore_daemon) {
-                       return;                 
+                       return false;
                }
 
                if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
                        _debug("update_rss_feed: start");
                }
 
-               $result = db_query($link, "SELECT id,update_interval,auth_login,auth_pass,cache_images
-                       FROM ttrss_feeds WHERE id = '$feed'");
+               if (!$ignore_daemon) {
+
+                       if (DB_TYPE == "pgsql") {
+                                       $updstart_thresh_qpart = "(ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '120 seconds')";
+                               } else {
+                                       $updstart_thresh_qpart = "(ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 120 SECOND))";
+                               }                       
+       
+                       $result = db_query($link, "SELECT id,update_interval,auth_login,
+                               auth_pass,cache_images,update_method
+                               FROM ttrss_feeds WHERE id = '$feed' AND $updstart_thresh_qpart");
+
+               } else {
+
+                       $result = db_query($link, "SELECT id,update_interval,auth_login,
+                               auth_pass,cache_images,update_method
+                               FROM ttrss_feeds WHERE id = '$feed'");
+
+               }
 
                if (db_num_rows($result) == 0) {
                        if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
-                               _debug("update_rss_feed: feed $feed [$feed_url] NOT FOUND");
+                               _debug("update_rss_feed: feed $feed [$feed_url] NOT FOUND/SKIPPED");
                        }               
-                       return;
+                       return false;
                }
 
+               $update_method = db_fetch_result($result, 0, "update_method");
+
+               db_query($link, "UPDATE ttrss_feeds SET last_update_started = NOW()
+                       WHERE id = '$feed'");
+
                $auth_login = db_fetch_result($result, 0, "auth_login");
                $auth_pass = db_fetch_result($result, 0, "auth_pass");
 
-               if (!ENABLE_SIMPLEPIE) {
+               if (ALLOW_SELECT_UPDATE_METHOD) {
+                       if (ENABLE_SIMPLEPIE) {
+                               $use_simplepie = $update_method != 1;
+                       } else {
+                               $use_simplepie = $update_method == 2;
+                       }
+               } else {
+                       $use_simplepie = ENABLE_SIMPLEPIE;
+               }
+
+               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                       _debug("use simplepie: $use_simplepie (feed setting: $update_method)\n");
+               }
+
+               if (!$use_simplepie) {
                        $auth_login = urlencode($auth_login);
                        $auth_pass = urlencode($auth_pass);
                }
                        error_reporting(0);
                }
 
-               if (!ENABLE_SIMPLEPIE) {
+               if (!$use_simplepie) {
                        $rss = fetch_rss($fetch_url);
                } else {
                        if (!is_dir(SIMPLEPIE_CACHE_DIR)) {
 
                        $rss = new SimplePie();
                        $rss->set_useragent(SIMPLEPIE_USERAGENT . MAGPIE_USER_AGENT_EXT);
-//                     $rss->set_timeout(MAGPIE_FETCH_TIME_OUT);
+#                      $rss->set_timeout(10);
                        $rss->set_feed_url($fetch_url);
                        $rss->set_output_encoding('UTF-8');
 
 
                $feed = db_escape_string($feed);
 
-               if (ENABLE_SIMPLEPIE) {
+               if ($use_simplepie) {
                        $fetch_ok = !$rss->error();
                } else {
                        $fetch_ok = !!$rss;
 
                        $owner_uid = db_fetch_result($result, 0, "owner_uid");
 
-                       if (ENABLE_SIMPLEPIE) {
+                       if ($use_simplepie) {
                                $site_url = $rss->get_link();
                        } else {
                                $site_url = $rss->channel["link"];
 
                        if (!$registered_title || $registered_title == "[Unknown]") {
 
-                               if (ENABLE_SIMPLEPIE) {
+                               if ($use_simplepie) {
                                        $feed_title = db_escape_string($rss->get_title());
                                } else {
                                        $feed_title = db_escape_string($rss->channel["title"]);
                        }
 
                        // weird, weird Magpie
-                       if (!ENABLE_SIMPLEPIE) {
+                       if (!$use_simplepie) {
                                if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
                        }
 
 
 //                     print "I: " . $rss->channel["image"]["url"];
 
-                       if (!ENABLE_SIMPLEPIE) {
+                       if (!$use_simplepie) {
                                $icon_url = $rss->image["url"];
                        } else {
                                $icon_url = $rss->get_image_url();
                                array_push($filters[$line["name"]], $filter);
                        }
 
-                       if (ENABLE_SIMPLEPIE) {
+                       if ($use_simplepie) {
                                $iterator = $rss->get_items();
                        } else {
                                $iterator = $rss->items;
 
                                }
 
-                               if (ENABLE_SIMPLEPIE) {
+                               if ($use_simplepie) {
                                        $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());
 
                                $entry_timestamp = "";
 
-                               if (ENABLE_SIMPLEPIE) {
+                               if ($use_simplepie) {
                                        $entry_timestamp = strtotime($item->get_date());
                                } else {
                                        $rss_2_date = $item['pubdate'];
 
                                $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
 
-                               if (ENABLE_SIMPLEPIE) {
+                               if ($use_simplepie) {
                                        $entry_title = $item->get_title();
                                } else {
                                        $entry_title = trim(strip_tags($item["title"]));
                                }
 
-                               if (ENABLE_SIMPLEPIE) {
+                               if ($use_simplepie) {
                                        $entry_link = $item->get_link();
                                } else {
                                        // strange Magpie workaround
 
                                $entry_link = strip_tags($entry_link);
 
-                               if (ENABLE_SIMPLEPIE) {
+                               if ($use_simplepie) {
                                        $entry_content = $item->get_description();
                                } else {
                                        $entry_content = $item["content:escaped"];
 
                                $entry_content_unescaped = $entry_content;
 
-                               if (ENABLE_SIMPLEPIE) {
+                               if ($use_simplepie) {
                                        $entry_comments = strip_tags($item->data["comments"]);
                                        if ($item->get_author()) {
                                                $entry_author_item = $item->get_author();
                                $entry_comments = mb_substr(db_escape_string($entry_comments), 0, 250);
                                $entry_author = mb_substr($entry_author, 0, 250);
 
-                               if (ENABLE_SIMPLEPIE) {
+                               if ($use_simplepie) {
                                        $num_comments = 0; #FIXME#
                                } else {
                                        $num_comments = db_escape_string($item["slash"]["comments"]);
 
                                // parse <category> entries into tags
 
-                               if (ENABLE_SIMPLEPIE) {
+                               if ($use_simplepie) {
 
                                        $additional_tags = array();
                                        $additional_tags_src = $item->get_categories();
 
                                $enclosures = array();
 
-                               if (ENABLE_SIMPLEPIE) {
+                               if ($use_simplepie) {
                                        $encs = $item->get_enclosures();
 
                                        if (is_array($encs)) {
 
                } else {
 
-                       if (ENABLE_SIMPLEPIE) {
+                       if ($use_simplepie) {
                                $error_msg = mb_substr($rss->error(), 0, 250);
                        } else {
                                $error_msg = mb_substr(magpie_error(), 0, 250);
                                        last_updated = NOW() WHERE id = '$feed'");
                }
 
-               if (ENABLE_SIMPLEPIE) {
+               if ($use_simplepie) {
                        unset($rss);
                }
 
                print "<param key=\"icons_location\" value=\"" . 
                        ICONS_URL . "\"/>";
 
+               print "<param key=\"hide_read_shows_special\" value=\"" . 
+                       (int) get_pref($link, "HIDE_READ_SHOWS_SPECIAL") . "\"/>";
+
                print "</init-params>";
        }
 
                return $res;
        }
 
+       /**
+        * Send by mail a digest of last articles.
+        * 
+        * @param mixed $link The database connection.
+        * @param integer $limit The maximum number of articles by digest.
+        * @return boolean Return false if digests are not enabled.
+        */
        function send_headlines_digests($link, $limit = 100) {
 
                if (!DIGEST_ENABLE) return false;
                                        <li class=\"vsep\">&nbsp;</li>
                                        <li class=\"top\">".__('Mark as read')."<ul>
                                                <li onclick=\"$catchup_sel_link\">".__('Selection')."</li>
-                                               <!-- <li onclick=\"$catchup_page_link\">".__('This page')."</li> -->
+                                               <!-- <li onclick=\"$catchup_page_link\">".__('This page')."</li> -->";
+
+                               if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
+
+                               print "
                                                <li><span class=\"insensitive\">--------</span></li>
                                                <li onclick=\"catchupRelativeToArticle(0)\">".__("Above active article")."</li>
                                                <li onclick=\"catchupRelativeToArticle(1)\">".__("Below active article")."</li>
-                                               <li><span class=\"insensitive\">--------</span></li>
+                                               <li><span class=\"insensitive\">--------</span></li>";
+                               }
+
+                               print "
                                                <li onclick=\"$catchup_feed_link\">".__('Entire feed')."</li></ul></li>
                                        ";
 
                                }
        
                                printFeedEntry($feed_id, $class, $feed, $unread, 
-                                       ICONS_DIR."/$feed_id.ico", $link, $rtl_content, 
+                                       ICONS_URL."/$feed_id.ico", $link, $rtl_content, 
                                        $last_updated, $line["last_error"]);
        
                                ++$lnum;
                return $url_path;
        }
 
+       /**
+        * Purge a feed contents, marked articles excepted.
+        * 
+        * @param mixed $link The database connection.
+        * @param integer $id The id of the feed to purge.
+        * @return void
+        */
        function clear_feed_articles($link, $id) {
                $result = db_query($link, "DELETE FROM ttrss_user_entries
                        WHERE feed_id = '$id' AND marked = false AND owner_uid = " . $_SESSION["uid"]);
 
                $result = db_query($link, "DELETE FROM ttrss_entries WHERE 
                        (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
-       }
-
-        function add_feed_url() {
+       } // function clear_feed_articles
+
+       /**
+        * Compute the Mozilla Firefox feed adding URL from server HOST and REQUEST_URI.
+        *
+        * @return string The Mozilla Firefox feed adding URL.
+        */
+       function add_feed_url() {
                $url_path = 'http://' . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
                $url_path .= "?op=pref-feeds&quiet=1&subop=add&feed_url=%s";
                return $url_path;
-        }
-
+       } // function add_feed_url
+
+       /**
+        * Encrypt a password in SHA1.
+        * 
+        * @param string $pass The password to encrypt.
+        * @param string $login A optionnal login.
+        * @return string The encrypted password.
+        */
        function encrypt_password($pass, $login = '') {
                if ($login) {
                        return "SHA1X:" . sha1("$login:$pass");
                } else {
                        return "SHA1:" . sha1($pass);
                }
-       }
+       } // function encrypt_password
+
+       /**
+        * Update a feed batch.
+        * Used by daemons to update n feeds by run.
+        * Only update feed needing a update, and not being processed
+        * by another process.
+        * 
+        * @param mixed $link Database link
+        * @param integer $limit Maximum number of feeds in update batch. Default to DAEMON_FEED_LIMIT.
+        * @param boolean $from_http Set to true if you call this function from http to disable cli specific code.
+        * @param boolean $debug Set to false to disable debug output. Default to true.
+        * @return void
+        */
+       function update_daemon_common($link, $limit = DAEMON_FEED_LIMIT, $from_http = false, $debug = true) {
+               // Process all other feeds using last_updated and interval parameters
+
+               // Test if the user has loggued in recently. If not, it does not update its feeds.
+               if (DAEMON_UPDATE_LOGIN_LIMIT > 0) {
+                       if (DB_TYPE == "pgsql") {
+                               $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT." days'";
+                       } else {
+                               $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT." DAY)";
+                       }                       
+               } else {
+                       $login_thresh_qpart = "";
+               }
+
+               // Test if the feed need a update (update interval exceded).
+               if (DB_TYPE == "pgsql") {
+                       $update_limit_qpart = "AND ((
+                                       ttrss_feeds.update_interval = 0
+                                       AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL)
+                               ) OR (
+                                       ttrss_feeds.update_interval > 0
+                                       AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL)
+                               ))";
+               } else {
+                       $update_limit_qpart = "AND ((
+                                       ttrss_feeds.update_interval = 0
+                                       AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE)
+                               ) OR (
+                                       ttrss_feeds.update_interval > 0
+                                       AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE)
+                               ))";
+               }
+
+               // Test if feed is currently being updated by another process.
+               if (DB_TYPE == "pgsql") {
+                       $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '120 seconds')";
+               } else {
+                       $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 120 SECOND))";
+               }
+
+               // Test if there is a limit to number of updated feeds
+               $query_limit = "";
+               if($limit) $query_limit = sprintf("LIMIT %d", $limit);
+
+               // We search for feed needing update.
+               $result = db_query($link, "SELECT ttrss_feeds.feed_url,ttrss_feeds.id, ttrss_feeds.owner_uid,
+                               SUBSTRING(ttrss_feeds.last_updated,1,19) AS last_updated,
+                               ttrss_feeds.update_interval 
+                       FROM 
+                               ttrss_feeds, ttrss_users, ttrss_user_prefs
+                       WHERE
+                               ttrss_feeds.owner_uid = ttrss_users.id
+                               AND ttrss_users.id = ttrss_user_prefs.owner_uid
+                               AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL'
+                               $login_thresh_qpart $update_limit_qpart
+                                $updstart_thresh_qpart
+                       ORDER BY ttrss_feeds.last_updated ASC $query_limit");
+
+               $user_prefs_cache = array();
+
+               if($debug) _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result)));
+
+               // Here is a little cache magic in order to minimize risk of double feed updates.
+               $feeds_to_update = array();
+               while ($line = db_fetch_assoc($result)) {
+                       $feeds_to_update[$line['id']] = $line;
+               }
+
+               // We update the feed last update started date before anything else.
+               // There is no lag due to feed contents downloads
+               // It prevent an other process to update the same feed.
+               $feed_ids = array_keys($feeds_to_update);
+               if($feed_ids) {
+                       db_query($link, sprintf("UPDATE ttrss_feeds SET last_update_started = NOW()
+                               WHERE id IN (%s)", implode(',', $feed_ids)));
+               }
+
+               // For each feed, we call the feed update function.
+               while ($line = array_pop($feeds_to_update)) {
+
+                       if($debug) _debug("Feed: " . $line["feed_url"] . ", " . $line["last_updated"]);
+
+                       // We setup a alarm to alert if the feed take more than 300s to update.
+                       // => HANG alarm.
+                       if(!$from_http) pcntl_alarm(300);
+                       update_rss_feed($link, $line["feed_url"], $line["id"], true);
+                       // Cancel the alarm (the update went well)
+                       if(!$from_http) pcntl_alarm(0);
+
+                       sleep(1); // prevent flood (FIXME make this an option?)
+               }
+
+       // Send feed digests by email if needed.
+       if (DAEMON_SENDS_DIGESTS) send_headlines_digests($link);
+
+       } // function update_daemon_common
 
 ?>