]> git.wh0rd.org - tt-rss.git/blobdiff - functions.php
purge feeds after updating
[tt-rss.git] / functions.php
index 473356886cde2baf82c849f8095ffad5ccfe1a0c..565c5d1032cca34d497f90db239f89f432e7494e 100644 (file)
 
                if (!$owner_uid) return;
 
-               $purge_unread = get_pref($link, "PURGE_UNREAD_ARTICLES",
-                       $owner_uid, false);
+               if (FORCE_ARTICLE_PURGE == 0) {
+                       $purge_unread = get_pref($link, "PURGE_UNREAD_ARTICLES",
+                               $owner_uid, false);
+               } else {
+                       $purge_unread = true;
+                       $purge_interval = FORCE_ARTICLE_PURGE;
+               }
 
                if (!$purge_unread) $query_limit = " unread = false AND ";
 
 
                }
 
+               ccache_update($link, $feed_id, $owner_uid);
+
                if ($debug) {
                        _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles");
                }
 //                             print "Feed $feed_id: purge interval = $purge_interval\n";
                        }
 
-                       if ($purge_interval > 0) {
+                       if ($purge_interval > 0 || FORCE_ARTICLE_PURGE) {
                                purge_feed($link, $feed_id, $purge_interval, $do_output);
                        }
                }       
                } else {
 
                        $result = db_query($link, "SELECT id,update_interval,auth_login,
-                               auth_pass,cache_images,update_method
+                               auth_pass,cache_images,update_method,hidden
                                FROM ttrss_feeds WHERE id = '$feed'");
 
                }
                        return false;
                }
 
+               $hidden = sql_bool_to_bool(db_fetch_result($result, 0, "hidden"));
                $update_method = db_fetch_result($result, 0, "update_method");
 
                db_query($link, "UPDATE ttrss_feeds SET last_update_started = NOW()
 
                                db_query($link, "COMMIT");
 
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                                       _debug("update_rss_feed: assigning labels...");
+                               }
+
+                               assign_article_to_labels($link, $entry_ref_id, $article_filters,
+                                       $owner_uid);
+
                                if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
                                        _debug("update_rss_feed: looking for enclosures...");
                                }
                                }
                        } 
 
+                       if (!$hidden) {
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                                       _debug("update_rss_feed: updating counters cache...");
+                               }
+
+                               // disabled, purge_feed() does that...
+                               //ccache_update($link, $feed, $owner_uid);
+                       }
+
+                       purge_feed($link, $feed, 0, true);
+
                        db_query($link, "UPDATE ttrss_feeds 
                                SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
 
                return $score;
        }
 
+       function assign_article_to_labels($link, $id, $filters, $owner_uid) {
+               foreach ($filters as $f) {
+                       if ($f[0] == "label") {
+                               label_add_article($link, $id, $f[1], $owner_uid);
+                       };
+               }
+       }
 
        function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
                $rtl_content = false, $last_updated = false, $last_error = false) {
 
        function initialize_user($link, $uid) {
 
-               db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) 
-                       values ('$uid','unread = true', 'Unread articles')");
+/*             db_query($link, "INSERT INTO ttrss_labels2 (owner_uid, caption)
+                       VALUES ('$uid', 'All Articles')");
 
-               db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) 
-                       values ('$uid','last_read is null and unread = false', 'Updated articles')");
+               db_query($link, "INSERT INTO ttrss_filters 
+                       (owner_uid, feed_id, filter_type, reg_exp, enabled, 
+                               action_id, action_param, filter_param) 
+                       VALUES ('$uid', NULL, 1, '.', true, 7, 'All Articles', 'before')"); */
 
                db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
                        values ('$uid', 'Tiny Tiny RSS: New Releases',
-                       'http://tt-rss.spb.ru/releases.rss')");
+                       'http://tt-rss.org/releases.rss')");
 
                db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
                        values ('$uid', 'Tiny Tiny RSS: Forum',
-                       'http://tt-rss.spb.ru/forum/rss.php')");
+                       'http://tt-rss.org/forum/rss.php')");
        }
 
        function logout_user() {
                                }
                        }
 
-//                     print session_id();
-//                     print_r($_SESSION);
-
                        if (!$_SESSION["uid"] || !validate_session($link)) {
                                render_login_form($link, $mobile);
                                exit;
                                                catchupArticlesById($link, $affected_ids, 0);
                                        }
 
-                               } else if ($feed < -10) { // label
+                                       if ($feed == -4) {
+                                               db_query($link, "UPDATE ttrss_user_entries 
+                                                       SET unread = false,last_read = NOW()
+                                                       WHERE owner_uid = ".$_SESSION["uid"]);
+                                       }
 
-                                       // TODO make this more efficient
+                               } else if ($feed < -10) { // label
 
                                        $label_id = -$feed - 11;
 
-                                       $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
-                                               WHERE id = '$label_id'");                                       
-
-                                       if ($tmp_result) {
-                                               $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
-
-                                               db_query($link, "BEGIN");
-
-                                               $tmp2_result = db_query($link,
-                                                       "SELECT 
-                                                               int_id 
-                                                       FROM 
-                                                               ttrss_user_entries,ttrss_entries,ttrss_feeds
-                                                       WHERE
-                                                               ref_id = ttrss_entries.id AND 
-                                                               ttrss_user_entries.feed_id = ttrss_feeds.id AND
-                                                               $sql_exp AND
-                                                               ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
-
-                                               while ($tmp_line = db_fetch_assoc($tmp2_result)) {
-                                                       db_query($link, "UPDATE 
-                                                               ttrss_user_entries 
-                                                       SET 
-                                                               unread = false, last_read = NOW()
-                                                       WHERE
-                                                               int_id = " . $tmp_line["int_id"]);
-                                               }
-                                                               
-                                               db_query($link, "COMMIT");
+                                       db_query($link, "UPDATE ttrss_user_entries, ttrss_user_labels2 
+                                               SET unread = false WHERE label_id = '$label_id' AND unread = true
+                                                       AND owner_uid = '".$_SESSION["uid"]."' AND ref_id = article_id");
 
-/*                                             db_query($link, "UPDATE ttrss_user_entries,ttrss_entries 
-                                                       SET unread = false,last_read = NOW()
-                                                       WHERE $sql_exp
-                                                       AND ref_id = id
-                                                       AND owner_uid = ".$_SESSION["uid"]); */
-                                       }
                                }
+
+                               ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
+
                        } else { // tag
                                db_query($link, "BEGIN");
 
 
        function getAllCounters($link, $omode = "flc", $active_feed = false) {
 
-               /* getting all counters is a resource intensive operation, so we
-                * rate limit it a little bit */
-
-
-
-               if (get_pref($link, "SYNC_COUNTERS") || 
-                               time() - $_SESSION["get_all_counters_stamp"] > 5) {
-
-                       if (!$omode) $omode = "flc";
+               if (!$omode) $omode = "flc";
        
-                       getGlobalCounters($link);
+               getGlobalCounters($link);
        
-                       if (strchr($omode, "l")) getLabelCounters($link);
-                       if (strchr($omode, "f")) getFeedCounters($link, SMART_RPC_COUNTERS, $active_feed);
-                       if (strchr($omode, "t")) getTagCounters($link);
-                       if (strchr($omode, "c")) {                      
-                               if (get_pref($link, 'ENABLE_FEED_CATS')) {
-                                       getCategoryCounters($link);
-                               }
+               if (strchr($omode, "l")) getLabelCounters($link);
+               if (strchr($omode, "f")) getFeedCounters($link, SMART_RPC_COUNTERS, $active_feed);
+               if (strchr($omode, "t")) getTagCounters($link);
+               if (strchr($omode, "c")) {                      
+                       if (get_pref($link, 'ENABLE_FEED_CATS')) {
+                               getCategoryCounters($link);
                        }
-
-                       $_SESSION["get_all_counters_stamp"] = time();
                }
-
        }       
 
        function getCategoryCounters($link) {
                # two special categories are -1 and -2 (all virtuals; all labels)
 
-               $ctr = getCategoryUnread($link, -1);
+/*             $ctr = getCategoryUnread($link, -1);
 
-               print "<counter type=\"category\" id=\"-1\" counter=\"$ctr\"/>";
+               print "<counter type=\"category\" id=\"-1\" counter=\"$ctr\"/>"; */
 
-               $ctr = getCategoryUnread($link, -2);
+               $ctr = getCategoryUnread($link, -2); 
 
                print "<counter type=\"category\" id=\"-2\" counter=\"$ctr\"/>";
 
                $age_qpart = getMaxAgeSubquery();
 
-               $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id) 
-                               FROM ttrss_user_entries, ttrss_entries WHERE feed_id = ttrss_feeds.id 
-                                       AND id = ref_id AND $age_qpart 
-                                       AND unread = true)) AS unread FROM ttrss_feeds 
-                       WHERE 
-                               hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
+               $result = db_query($link, "SELECT id AS cat_id, value AS unread 
+                       FROM ttrss_feed_categories, ttrss_cat_counters_cache 
+                       WHERE ttrss_cat_counters_cache.feed_id = id AND 
+                       ttrss_feed_categories.owner_uid = " . $_SESSION["uid"]);
 
                while ($line = db_fetch_assoc($result)) {
                        $line["cat_id"] = sprintf("%d", $line["cat_id"]);
+
                        print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
                                $line["unread"]."\"/>";
                }
+
+               /* Special case: NULL category doesn't actually exist in the DB */
+
+               print "<counter type=\"category\" id=\"0\" counter=\"".
+                       ccache_find($link, 0, $_SESSION["uid"], true)."\"/>";
+
        }
 
-       function getCategoryUnread($link, $cat) {
+       function getCategoryUnread($link, $cat, $owner_uid = false) {
+
+               if (!$owner_uid) $owner_uid = $_SESSION["uid"];
 
                if ($cat >= 0) {
 
 
                        $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query 
                                        AND hidden = false
-                                       AND owner_uid = " . $_SESSION["uid"]);
+                                       AND owner_uid = " . $owner_uid);
        
                        $cat_feeds = array();
                        while ($line = db_fetch_assoc($result)) {
                        $result = db_query($link, "SELECT COUNT(int_id) AS unread 
                                FROM ttrss_user_entries,ttrss_entries 
                                WHERE   unread = true AND ($match_part) AND id = ref_id 
-                               AND $age_qpart AND owner_uid = " . $_SESSION["uid"]);
+                               AND $age_qpart AND owner_uid = " . $owner_uid);
        
                        $unread = 0;
        
                        return getFeedUnread($link, -1) + getFeedUnread($link, -2) + getFeedUnread($link, -3);
                } else if ($cat == -2) {
 
-                       $rv = getLabelCounters($link, false, true);
-                       $ctr = 0;
+                       $result = db_query($link, "
+                               SELECT COUNT(unread) AS unread FROM 
+                                       ttrss_user_entries, ttrss_labels2, ttrss_user_labels2, ttrss_feeds 
+                               WHERE label_id = ttrss_labels2.id AND article_id = ref_id AND 
+                                       ttrss_labels2.owner_uid = '$owner_uid'
+                                       AND unread = true AND hidden = false AND feed_id = ttrss_feeds.id
+                                       AND ttrss_user_entries.owner_uid = '$owner_uid'");
 
-                       foreach (array_keys($rv) as $k) {
-                               if ($k < -10) {
-                                       $ctr += $rv[$k]["counter"];
-                               }
-                       }
+                       $unread = db_fetch_result($result, 0, "unread");
 
-                       return $ctr;
-               }
+                       return $unread;
+
+               } 
        }
 
        function getMaxAgeSubquery($days = COUNTERS_MAX_AGE) {
        }
 
        function getFeedUnread($link, $feed, $is_cat = false) {
-               return getFeedArticles($link, $feed, $is_cat, true);
+               return getFeedArticles($link, $feed, $is_cat, true, $_SESSION["uid"]);
        }
 
-       function getFeedArticles($link, $feed, $is_cat = false, $unread_only = false) {
+       function getLabelUnread($link, $label_id, $owner_uid = false) {
+               if (!$owner_uid) $owner_uid = $_SESSION["uid"];
+
+               $result = db_query($link, "
+                       SELECT COUNT(unread) AS unread FROM 
+                               ttrss_user_entries, ttrss_labels2, ttrss_user_labels2, ttrss_feeds 
+                       WHERE label_id = ttrss_labels2.id AND article_id = ref_id AND 
+                               ttrss_labels2.owner_uid = '$owner_uid' AND ttrss_labels2.id = '$label_id'
+                               AND unread = true AND hidden = false AND feed_id = ttrss_feeds.id
+                               AND ttrss_user_entries.owner_uid = '$owner_uid'");
+
+               if (db_num_rows($result) != 0) {
+                       return db_fetch_result($result, 0, "unread");
+               } else {
+                       return 0;
+               }
+       }
+
+       function getFeedArticles($link, $feed, $is_cat = false, $unread_only = false,
+               $owner_uid = false) {
+
                $n_feed = sprintf("%d", $feed);
 
+               if (!$owner_uid) $owner_uid = $_SESSION["uid"];
+
                if ($unread_only) {
                        $unread_qpart = "unread = true";
                } else {
                $age_qpart = getMaxAgeSubquery();
 
                if ($is_cat) {
-                       return getCategoryUnread($link, $n_feed);               
+                       return getCategoryUnread($link, $n_feed, $owner_uid);           
                } else if ($n_feed == -1) {
                        $match_part = "marked = true";
                } else if ($n_feed == -2) {
                } else if ($n_feed == -3) {
                        $match_part = "unread = true";
 
-                       $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
+                       $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid);
 
                        if (DB_TYPE == "pgsql") {
                                $match_part .= " AND updated > NOW() - INTERVAL '$intl hour' "; 
                        } else {
                                $match_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
                        }
-
+               } else if ($n_feed == -4) {
+                       $match_part = "true";
                } else if ($n_feed > 0) {
 
                        $result = db_query($link, "SELECT id FROM ttrss_feeds 
                                        WHERE parent_feed = '$n_feed'
                                        AND hidden = false
-                                       AND owner_uid = " . $_SESSION["uid"]);
+                                       AND owner_uid = " . $owner_uid);
 
                        if (db_num_rows($result) > 0) {
 
                                        ttrss_user_entries.ref_id = ttrss_entries.id AND
                                        $age_qpart AND
                                        ($match_part) AND
-                                       owner_uid = " . $_SESSION["uid"]);
+                                       owner_uid = " . $owner_uid);
 
                                $unread = 0;
 
 
                        $label_id = -$feed - 11;
 
-                       $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
-                               id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
+                       return getLabelUnread($link, $label_id, $owner_uid);
 
-                       $match_part = db_fetch_result($result, 0, "sql_exp");
                }
 
                if ($match_part) {
                                ttrss_user_entries.ref_id = ttrss_entries.id AND 
                                ttrss_feeds.hidden = false AND
                                $age_qpart AND
-                               $unread_qpart AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
+                               $unread_qpart AND ($match_part) AND ttrss_user_entries.owner_uid = " . $owner_uid);
                                
                } else {
                
                                FROM ttrss_tags,ttrss_user_entries,ttrss_entries 
                                WHERE tag_name = '$feed' AND post_int_id = int_id AND ref_id = ttrss_entries.id 
                                AND $unread_qpart AND $age_qpart AND
-                                       ttrss_tags.owner_uid = " . $_SESSION["uid"]);
+                                       ttrss_tags.owner_uid = " . $owner_uid);
                }
                
                $unread = db_fetch_result($result, 0, "unread");
                return $unread;
        }
 
-       /* FIXME this needs reworking */
-
        function getGlobalUnread($link, $user_id = false) {
 
                if (!$user_id) {
                        $user_id = $_SESSION["uid"];
                }
 
-               $age_qpart = getMaxAgeSubquery();
+               $result = db_query($link, "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"); 
 
-               $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
-                       WHERE unread = true AND 
-                       ttrss_user_entries.feed_id = ttrss_feeds.id AND
-                       ttrss_user_entries.ref_id = ttrss_entries.id AND 
-                       hidden = false AND
-                       $age_qpart AND
-                       ttrss_user_entries.owner_uid = '$user_id'");
-               $c_id = db_fetch_result($result, 0, "c_id");
                return $c_id;
        }
 
 
                $tctrs_modified = false;
 
-/*             $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
-                       FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
-                       ttrss_user_entries.ref_id = ttrss_entries.id AND 
-                       ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
-                       post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name 
-               UNION
-                       select tag_name,0 as count FROM ttrss_tags
-                       WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
-
                $age_qpart = getMaxAgeSubquery();
 
                $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) 
                }
 
                $ret_arr = array();
-               
-               $old_counters = $_SESSION["lctr_last_value"];
-               $lctrs_modified = false;
-
-               $count = getFeedUnread($link, -1);
 
-               if (!$ret_mode) {
+               for ($i = -1; $i >= -4; $i--) {
 
-                       if (get_pref($link, 'EXTENDED_FEEDLIST')) {
-                               $xmsg_part = "xmsg=\"(" . getFeedArticles($link, $id) . " total)\"";
-                       } else {
-                               $xmsg_part = "";
-                       }
-
-                       print "<counter type=\"label\" id=\"-1\" counter=\"$count\" $xmsg_part/>";
-               } else {
-                       $ret_arr["-1"]["counter"] = $count;
-                       $ret_arr["-1"]["description"] = __("Starred articles");
-               }
-
-               $count = getFeedUnread($link, -2);
-
-               if (!$ret_mode) {
+                       $count = getFeedUnread($link, $i);
+       
+                       if (!$ret_mode) {
+       
+                               if (get_pref($link, 'EXTENDED_FEEDLIST')) {
+                                       $xmsg_part = "xmsg=\"(" . getFeedArticles($link, $i) . " total)\"";
+                               } else {
+                                       $xmsg_part = "";
+                               }
 
-                       if (get_pref($link, 'EXTENDED_FEEDLIST')) {
-                               $xmsg_part = "xmsg=\"(" . getFeedArticles($link, $id) . " total)\"";
+                               print "<counter type=\"label\" id=\"$i\" counter=\"$count\" $xmsg_part/>";
                        } else {
-                               $xmsg_part = "";
+                               $ret_arr[$i]["counter"] = $count;
+                               $ret_arr[$i]["description"] = getFeedTitle($link, $i);
                        }
-
-                       print "<counter type=\"label\" id=\"-2\" counter=\"$count\" $xmsg_part/>";
-               } else {
-                       $ret_arr["-2"]["counter"] = $count;
-                       $ret_arr["-2"]["description"] = __("Published articles");
+       
                }
 
-               $count = getFeedUnread($link, -3);
-
-               if (!$ret_mode) {
-
-                       if (get_pref($link, 'EXTENDED_FEEDLIST')) {
-                               $xmsg_part = "xmsg=\"(" . getFeedArticles($link, $id) . " total)\"";
-                       } else {
-                               $xmsg_part = "";
-                       }
+               $old_counters = $_SESSION["lctr_last_value"];
+               $lctrs_modified = false;
 
-                       print "<counter type=\"label\" id=\"-3\" counter=\"$count\" $xmsg_part/>";
-               } else {
-                       $ret_arr["-3"]["counter"] = $count;
-                       $ret_arr["-3"]["description"] = __("Fresh articles");
-               }
 
+                       $owner_uid = $_SESSION["uid"];
 
-               $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
-                       ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
+                       $result = db_query($link,
+                               "SELECT ttrss_labels2.id, caption, COUNT(unread) AS unread FROM ttrss_labels2 
+                                       LEFT JOIN ttrss_user_labels2 ON (label_id = ttrss_labels2.id) 
+                                               LEFT JOIN ttrss_user_entries ON (ref_id = article_id AND
+                                                       unread = true AND
+                                                       ttrss_user_entries.owner_uid = '$owner_uid')
+                                               LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = feed_id AND hidden = false)
+                                               WHERE ttrss_labels2.owner_uid = '$owner_uid'
+                                       GROUP BY ttrss_labels2.id, caption");
+               
+                       while ($line = db_fetch_assoc($result)) {
        
-               while ($line = db_fetch_assoc($result)) {
-
-                       $id = -$line["id"] - 11;
-
-                       $label_name = $line["description"];
-
-                       error_reporting (0);
-
-                       $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
-                               WHERE (" . $line["sql_exp"] . ") AND unread = true AND 
-                               ttrss_feeds.hidden = false AND
-                               $age_qpart AND
-                               ttrss_user_entries.feed_id = ttrss_feeds.id AND
-                               ttrss_user_entries.ref_id = ttrss_entries.id AND 
-                               ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
-
-                       $count = db_fetch_result($tmp_result, 0, "count");
-
-                       if (!$smart_mode || $old_counters[$id] != $count) {     
-                               $old_counters[$id] = $count;
-                               $lctrs_modified = true;
-                               if (!$ret_mode) {
-
-                                       if (get_pref($link, 'EXTENDED_FEEDLIST')) {
-                                               $xmsg_part = "xmsg=\"(" . getFeedArticles($link, $id) . " total)\"";
+                               $id = -$line["id"] - 11;
+       
+                               $label_name = $line["caption"];
+                               $count = $line["unread"];
+               
+                               if (!$smart_mode || $old_counters[$id] != $count) {     
+                                       $old_counters[$id] = $count;
+                                       $lctrs_modified = true;
+                                       if (!$ret_mode) {
+       
+                                               if (get_pref($link, 'EXTENDED_FEEDLIST')) {
+                                                       $xmsg_part = "xmsg=\"(" . getFeedArticles($link, $id) . " total)\"";
+                                               } else {
+                                                       $xmsg_part = "";
+                                               }
+       
+                                               print "<counter type=\"label\" id=\"$id\" counter=\"$count\" $xmsg_part/>";
                                        } else {
-                                               $xmsg_part = "";
+                                               $ret_arr[$id]["counter"] = $count;
+                                               $ret_arr[$id]["description"] = $label_name;
                                        }
-
-                                       print "<counter type=\"label\" id=\"$id\" counter=\"$count\" $xmsg_part/>";
-                               } else {
-                                       $ret_arr[$id]["counter"] = $count;
-                                       $ret_arr[$id]["description"] = $label_name;
                                }
+       
+                               error_reporting (DEFAULT_ERROR_LEVEL);
                        }
 
-                       error_reporting (DEFAULT_ERROR_LEVEL);
-               }
-
                if ($smart_mode && $lctrs_modified) {
                        $_SESSION["lctr_last_value"] = $old_counters;
                }
                return $ret_arr;
        }
 
-/*     function getFeedCounter($link, $id) {
-       
-               $result = db_query($link, "SELECT 
-                               count(id) as count,last_error
-                       FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
-                       WHERE feed_id = '$id' AND unread = true
-                       AND ttrss_user_entries.feed_id = ttrss_feeds.id
-                       AND ttrss_user_entries.ref_id = ttrss_entries.id");
-       
-                       $count = db_fetch_result($result, 0, "count");
-                       $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
-                       
-                       print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";           
-       } */
-
        function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS, $active_feed = false) {
 
                $age_qpart = getMaxAgeSubquery();
 
                $old_counters = $_SESSION["fctr_last_value"];
 
-/*             $result = db_query($link, "SELECT id,last_error,parent_feed,
-                       ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated,
-                       (SELECT count(id) 
-                               FROM ttrss_entries,ttrss_user_entries 
-                               WHERE feed_id = ttrss_feeds.id AND 
-                                       ttrss_user_entries.ref_id = ttrss_entries.id
-                               AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
-                       FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
-                       AND parent_feed IS NULL"); */
-
                $query = "SELECT ttrss_feeds.id,
                                ttrss_feeds.title,
                                ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated, 
-                               last_error, 
-                               COUNT(ttrss_entries.id) AS count 
-                       FROM ttrss_feeds 
-                               LEFT JOIN ttrss_user_entries ON (ttrss_user_entries.feed_id = ttrss_feeds.id 
-                                       AND ttrss_user_entries.owner_uid = ttrss_feeds.owner_uid 
-                                       AND ttrss_user_entries.unread = true) 
-                               LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id AND
-                                       $age_qpart) 
+                               last_error, value AS count
+                       FROM ttrss_feeds, ttrss_counters_cache
                        WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."  
                                AND parent_feed IS NULL 
-                       GROUP BY ttrss_feeds.id, ttrss_feeds.title, ttrss_feeds.last_updated, last_error";
+                               AND ttrss_counters_cache.feed_id = id";
 
                $result = db_query($link, $query);
                $fctrs_modified = false;
                        $has_img = feed_has_icon($id);
 
                        $tmp_result = db_query($link,
-                               "SELECT ttrss_feeds.id,COUNT(unread) AS unread
-                               FROM ttrss_feeds LEFT JOIN ttrss_user_entries 
-                                       ON (ttrss_feeds.id = ttrss_user_entries.feed_id) 
-                               LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id) 
-                               WHERE parent_feed = '$id' AND $age_qpart AND unread = true GROUP BY ttrss_feeds.id");
-                       
-                       if (db_num_rows($tmp_result) > 0) {                             
-                               while ($l = db_fetch_assoc($tmp_result)) {
-                                       $count += $l["unread"];
-                               }
-                       }
+                               "SELECT SUM(value) AS unread FROM ttrss_feeds, ttrss_counters_cache 
+                                       WHERE parent_feed = '$id' AND feed_id = id");
+
+                       $count += db_fetch_result($tmp_result, 0, "unread");
 
                        if (!$smart_mode || $old_counters[$id] != $count) {
                                $old_counters[$id] = $count;
                        return __("Published articles");
                } else if ($id == -3) {
                        return __("Fresh articles");
+               } else if ($id == -4) {
+                       return __("All articles");
                } else if ($id < -10) {
                        $label_id = -$id - 11;
-                       $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
+                       $result = db_query($link, "SELECT caption FROM ttrss_labels2 WHERE id = '$label_id'");
                        if (db_num_rows($result) == 1) {
-                               return db_fetch_result($result, 0, "description");
+                               return db_fetch_result($result, 0, "caption");
                        } else {
                                return "Unknown label ($label_id)";
                        }
                print "<param key=\"bw_limit\" value=\"".
                        (int) $_SESSION["bw_limit"]."\"/>";
 
-               print "<param key=\"sync_counters\" value=\"" . 
-                       (int) get_pref($link, "SYNC_COUNTERS") . "\"/>";
+//             print "<param key=\"sync_counters\" value=\"" . 
+//                     (int) get_pref($link, "SYNC_COUNTERS") . "\"/>";
+
+               print "<param key=\"sync_counters\" value=\"1\"/>";
 
                print "</init-params>";
        }
                        if ($view_mode == "unread") {
                                $view_query_part = " unread = true AND ";
                        }
-       
+
+                       if ($view_mode == "updated") {
+                               $view_query_part = " (last_read is null and unread = false) AND ";
+                       }
+
                        if ($limit > 0) {
                                $limit_query_part = "LIMIT " . $limit;
                        } 
                        } else if ($feed == -1) { // starred virtual feed
                                $query_strategy_part = "marked = true";
                                $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
-                       } else if ($feed == -2) { // published virtual feed
-                               $query_strategy_part = "published = true";
-                               $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
+                       } else if ($feed == -2) { // published virtual feed OR labels category
+
+                               if (!$cat_view) {
+                                       $query_strategy_part = "published = true";
+                                       $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
+                               } else {
+                                       $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
+
+                                       $ext_tables_part = ",ttrss_labels2,ttrss_user_labels2";
+       
+                                       $query_strategy_part = "ttrss_labels2.id = ttrss_user_labels2.label_id AND
+                                               ttrss_user_labels2.article_id = ref_id";
+
+                               }
+
                        } else if ($feed == -3) { // fresh virtual feed
                                $query_strategy_part = "unread = true";
 
                                        $query_strategy_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
                                }
 
+                               $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
+                       } else if ($feed == -4) { // all articles virtual feed
+                               $query_strategy_part = "true";
                                $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
                        } else if ($feed <= -10) { // labels
                                $label_id = -$feed - 11;
-       
-                               $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
-                                       WHERE id = '$label_id'");
-                       
-                               $query_strategy_part = "(" . db_fetch_result($tmp_result, 0, "sql_exp") . ")";
 
-                               if (!$query_strategy_part) {
-                                       return false;
-                               }
+                               $query_strategy_part = "label_id = '$label_id' AND
+                                       ttrss_labels2.id = ttrss_user_labels2.label_id AND
+                                       ttrss_user_labels2.article_id = ref_id";
 
                                $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
+                               $ext_tables_part = ",ttrss_labels2,ttrss_user_labels2";
                        } else {
                                $query_strategy_part = "id > 0"; // dumb
                        }
                                $feed_title = __("Starred articles");
                                if ($search) {  $feed_title = __("Searched for") . " $search ($feed_title)"; }
                        } else if ($feed == -2) {
-                               $feed_title = __("Published articles");
-                               if ($search) {  $feed_title = __("Searched for") . " $search ($feed_title)"; }
+                               if (!$cat_view) {
+                                       $feed_title = __("Published articles");
+                                       if ($search) {  $feed_title = __("Searched for") . " $search ($feed_title)"; }
+                               } else {
+                                       $feed_title = __("Labels");
+                               }
                        } else if ($feed == -3) {
                                $feed_title = __("Fresh articles");
                                if ($search) {  $feed_title = __("Searched for") . " $search ($feed_title)"; }
+                       } else if ($feed == -4) {
+                               $feed_title = __("All articles");
+                               if ($search) {  $feed_title = __("Searched for") . " $search ($feed_title)"; }
                        } else if ($feed < -10) {
                                $label_id = -$feed - 11;
-                               $result = db_query($link, "SELECT description FROM ttrss_labels
+                               $result = db_query($link, "SELECT caption FROM ttrss_labels2
                                        WHERE id = '$label_id'");
-                               $feed_title = db_fetch_result($result, 0, "description");
+                               $feed_title = db_fetch_result($result, 0, "caption");
 
                                if ($search) {
                                        $feed_title = __("Searched for") . " $search ($feed_title)";
 } */
                                }
 
-                               $query = "SELECT 
+                               $query = "SELECT DISTINCT 
                                                guid,
                                                ttrss_entries.id,ttrss_entries.title,
                                                updated,
                                                ".SUBSTRING_FOR_DATE."(updated,1,19) as updated_noms,
                                                author,score
                                        FROM
-                                               ttrss_entries,ttrss_user_entries,ttrss_feeds
+                                               ttrss_entries,ttrss_user_entries,ttrss_feeds$ext_tables_part
                                        WHERE
                                        $group_limit_part
                                        ttrss_feeds.hidden = false AND 
        }
 
        function check_for_update($link, $brief_fmt = true) {
-               $releases_feed = "http://tt-rss.spb.ru/releases.rss";
+               $releases_feed = "http://tt-rss.org/releases.rss";
 
                if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) {
                        return;
                                } else {
                                        return "New version of Tiny-Tiny RSS ($latest_version) is available:
                                                <div class='milestoneDetails'>$content</div>
-                                               Visit <a target=\"_blank\" href=\"http://tt-rss.spb.ru/\">official site</a> for
+                                               Visit <a target=\"_blank\" href=\"http://tt-rss.org/\">official site</a> for
                                                download and update information.";      
                                }
 
                        unread = NOT unread,last_read = NOW()
                        WHERE ($ids_qpart) AND owner_uid = $owner_uid");
                }
+
+               /* update ccache */
+
+               $result = db_query($link, "SELECT DISTINCT feed_id FROM ttrss_user_entries
+                       WHERE ($ids_qpart) AND owner_uid = $owner_uid");
+
+               while ($line = db_fetch_assoc($result)) {
+                       ccache_update($link, $line["feed_id"], $owner_uid);
+               }
        }
 
        function catchupArticleById($link, $id, $cmode) {
 
                        }
 
-                       if (!$dashboard_menu) {
-
                                if (strpos($_SESSION["client.userAgent"], "MSIE") === false) {
 
                                        print "<td class=\"headlineActions$rtl_cpart\">
                                                        <li onclick=\"$tog_unread_link\">&nbsp;&nbsp;".__('Unread')."</li>
                                                        <li onclick=\"$tog_marked_link\">&nbsp;&nbsp;".__('Starred')."</li>
                                                        <li onclick=\"$tog_published_link\">&nbsp;&nbsp;".__('Published')."</li>
-                                                       <li><span class=\"insensitive\">--------</span></li>
+                                                       <!-- <li><span class=\"insensitive\">--------</span></li> -->
                                                        <li><span class=\"insensitive\">".__('Mark as read:')."</span></li>
                                                        <li onclick=\"$catchup_sel_link\">&nbsp;&nbsp;".__('Selection')."</li>";
 
 
                                print "<li onclick=\"$catchup_feed_link\">&nbsp;&nbsp;".__('Entire feed')."</li>";
 
-                               print "<li><span class=\"insensitive\">--------</span></li>";
-                               print "<li><span class=\"insensitive\">".__('Other actions:')."</span></li>";
-               
+                               //print "<li><span class=\"insensitive\">--------</span></li>";
+                               print "<li><span class=\"insensitive\">".__('Assign label:')."</span></li>";
 
-                               if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
-                                       print "
-                                               <li onclick=\"javascript:labelFromSearch('$search', '$search_mode',
-                                                       '$match_on', '$feed_id', '$is_cat');\">&nbsp;&nbsp;
-                                                       ".__('Search to label')."</li>";
-                               } else {
-                                       print "<li><span class=\"insensitive\">&nbsp;&nbsp;".__('Search to label')."</li>";
+                               $result = db_query($link, "SELECT id, caption FROM ttrss_labels2 WHERE
+                                       owner_uid = '".$_SESSION["uid"]."' ORDER BY caption");
+
+                               while ($line = db_fetch_assoc($result)) {
+
+                                       $label_id = $line["id"];
+                                       $label_caption = $line["caption"];
+
+                                       if ($feed_id < -10 && $feed_id == -11-$label_id) {
+                                               print "<li onclick=\"javascript:selectionRemoveLabel($label_id)\">
+                                                       &nbsp;&nbsp;$label_caption ".__('(remove)')."</li>";
+                                       } else {                                        
+                                               print "<li onclick=\"javascript:selectionAssignLabel($label_id)\">
+                                                       &nbsp;&nbsp;$label_caption</li>";
+                                       }
+                       }
 
-                               }
-                               
                                print   "</ul></li></ul>";
                                print "</td>"; 
        
                                                        __('Mark as read:')."
                                                                <a href=\"#\" onclick=\"$catchup_page_link\">".__('Page')."</a>,
                                                                <a href=\"#\" onclick=\"$catchup_feed_link\">".__('Feed')."</a>";
-       
-                                       if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
-       
-                                               print "&nbsp;&nbsp;
-                                                               <a href=\"javascript:labelFromSearch('$search', '$search_mode',
-                                                                       '$match_on', '$feed_id', '$is_cat');\">
-                                                               ".__('Convert to label')."</a>";
-                                       }
-       
+
                                        print "</td>";  
        
                                }
-                       } else { // dashboard menu actions
-
-                               // not implemented
-                               print "</td>";
-                       }
 
                        print "<td class=\"headlineTitle$rtl_cpart\">";
 
        function printCategoryHeader($link, $cat_id, $hidden = false, $can_browse = true) {
 
                        $tmp_category = getCategoryTitle($link, $cat_id);
-                       $cat_unread = getCategoryUnread($link, $cat_id);
+
+                       if ($cat_id > 0) {
+                               $cat_unread = ccache_find($link, $cat_id, $_SESSION["uid"], true);
+                       } else if ($cat_id == 0 || $cat_id == -2) {
+                               $cat_unread = getCategoryUnread($link, $cat_id);
+                       }
 
                        if ($hidden) {
                                $holder_style = "display:none;";
                                $inner_title_class = "catTitleNL";
                        }
 
-                       if ($cat_id > 0) {
-                               $cat_class = "feedCat";
-                       } else {
-                               $cat_class = "virtCat";
-                       }
+                       $cat_class = "feedCat";
 
                        print "<li class=\"$cat_class\" id=\"FCAT-$cat_id\">
                                <img onclick=\"toggleCollapseCat($cat_id)\" class=\"catCollapse\"
                        printCategoryHeader($link, -1, $cat_hidden, false);
                }
 
-               if (defined('_ENABLE_DASHBOARD')) {
-                       printFeedEntry(-4, "virt", __("Dashboard"), 0, 
-                               "images/tag.png", $link);
-               }
-
                $num_starred = getFeedUnread($link, -1);
                $num_published = getFeedUnread($link, -2);
                $num_fresh = getFeedUnread($link, -3);
+               $num_total = getFeedUnread($link, -4);
+
+               $class = "virt";
+
+               if ($num_total > 0) $class .= "Unread";
+
+               printFeedEntry(-4, $class, __("All articles"), $num_total, 
+                       "images/tag.png", $link);
 
                $class = "virt";
 
 
                if (!$tags) {
 
-                       if (GLOBAL_ENABLE_LABELS && get_pref($link, 'ENABLE_LABELS')) {
-       
-                               $result = db_query($link, "SELECT id,sql_exp,description FROM
-                                       ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
+
+                               $result = db_query($link, "SELECT id,caption FROM                                       
+                                       ttrss_labels2 WHERE owner_uid = '$owner_uid' ORDER by caption");
                
                                if (db_num_rows($result) > 0) {
                                        if (get_pref($link, 'ENABLE_FEED_CATS')) {
                                                        $cat_hidden = false;
                                                }
 
-                                               printCategoryHeader($link, -2, $cat_hidden, false);
+                                               printCategoryHeader($link, -2, $cat_hidden, true);
 
                                        } else {
                                                print "<li><hr></li>";
                
                                while ($line = db_fetch_assoc($result)) {
        
-                                       error_reporting (0);
-
                                        $label_id = -$line['id'] - 11;
                                        $count = getFeedUnread($link, $label_id);
 
                                        if ($count > 0) {
                                                $class .= "Unread";
                                        }
-                                       
-                                       error_reporting (DEFAULT_ERROR_LEVEL);
        
                                        printFeedEntry($label_id, 
-                                               $class, $line["description"], 
+                                               $class, $line["caption"], 
                                                $count, "images/label.png", $link);
                
                                }
                                        if (get_pref($link, 'ENABLE_FEED_CATS')) {
                                                print "</ul>";
                                        }
-                               }
+                               } 
 
-                       }
 
                        if (!get_pref($link, 'ENABLE_FEED_CATS')) {
                                print "<li><hr></li>";
 
                        $query = "SELECT ttrss_feeds.*,
                                ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated_noms,
-                               (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
-                                       WHERE feed_id = ttrss_feeds.id AND unread = true
-                                               AND $age_qpart
-                                               AND ttrss_user_entries.ref_id = ttrss_entries.id
-                                               AND owner_uid = '$owner_uid') as unread,
                                cat_id,last_error,
                                ttrss_feed_categories.title AS category,
-                               ttrss_feed_categories.collapsed 
-                               FROM ttrss_feeds LEFT JOIN ttrss_feed_categories 
-                                       ON (ttrss_feed_categories.id = cat_id)                          
+                               ttrss_feed_categories.collapsed,
+                               value AS unread 
+                               FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
+                                       ON (ttrss_feed_categories.id = cat_id)                  
+                               LEFT JOIN ttrss_counters_cache 
+                                       ON
+                                               (ttrss_feeds.id = feed_id)
                                WHERE 
                                        ttrss_feeds.hidden = false AND
                                        ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
                                if (!$feed) $feed = "[Untitled]";
 
                                $feed_id = $line["id"];   
+                               $unread = $line["unread"];
        
                                $subop = $_GET["subop"];
-                               
-                               $unread = $line["unread"];
 
                                if (get_pref($link, 'HEADLINES_SMART_DATE')) {
                                        $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
                                }
 
                                $tmp_result = db_query($link,
-                                       "SELECT id,COUNT(unread) AS unread
-                                       FROM ttrss_feeds LEFT JOIN ttrss_user_entries 
-                                               ON (ttrss_feeds.id = ttrss_user_entries.feed_id) 
-                                       WHERE parent_feed = '$feed_id' AND unread = true 
-                                       GROUP BY ttrss_feeds.id");
-                       
-                               if (db_num_rows($tmp_result) > 0) {                             
-                                       while ($l = db_fetch_assoc($tmp_result)) {
-                                               $unread += $l["unread"];
-                                       }
-                               }
+                                       "SELECT SUM(value) AS unread FROM ttrss_feeds, ttrss_counters_cache 
+                                               WHERE parent_feed = '$feed_id' AND feed_id = id");
 
+                               $unread += db_fetch_result($tmp_result, 0, "unread");
+       
                                $cat_id = $line["cat_id"];
 
                                $tmp_category = $line["category"];
                        $result = db_query($link, "UPDATE ttrss_user_entries 
                                SET unread = false,last_read = NOW() 
                                WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
+
+                       ccache_update($link, $feed_id, $_SESSION["uid"]);
                }
 
                $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
                                $feed_icon = "&nbsp;";
                        }
 
-/*                     if ($line["comments"] && $line["link"] != $line["comments"]) {
-                               $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
-                       } else {
-                               $entry_comments = "";
-                       } */
-
                        $num_comments = $line["num_comments"];
                        $entry_comments = "";
 
                                print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
                        }
 
-/*                     $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
-                               ttrss_tags WHERE post_int_id = " . $line["int_id"] . "
-                               ORDER BY tag_name"); */
-
                        $tags = get_article_tags($link, $id);
        
                        $tags_str = "";
                        print "</div>";
 
                        print "<div class=\"postIcon\">" . $feed_icon . "</div>";
+
                        print "<div class=\"postContent\">";
-                       
+
                        #print "<div id=\"allEntryTags\">".__('Tags:')." $f_tags_str</div>";
 
                        $article_content = sanitize_rss($link, $line["content"]);
                                $id = $line["id"];
                                $feed_id = $line["feed_id"];
 
+                               $labels = get_article_labels($link, $id);
+                               $labels_str = "<span id=\"HLLCTR-$id\">";
+
+                               foreach ($labels as $l) {
+                                       $labels_str .= "<span 
+                                               class='hlLabelRef'>".
+                                               $l[1]."</span>";
+                               }
+
+                               $labels_str .= "</span>";
+
                                if (count($topmost_article_ids) < 5) {
                                        array_push($topmost_article_ids, $id);
                                }
 
                                $score_pic = get_score_pic($score);
 
-                               $score_title = __("(Click to change)");
+/*                             $score_title = __("(Click to change)");
+                               $score_pic = "<img class='hlScorePic' src=\"images/$score_pic\" 
+                                       onclick=\"adjustArticleScore($id, $score)\" title=\"$score $score_title\">"; */
 
                                $score_pic = "<img class='hlScorePic' src=\"images/$score_pic\" 
-                                       onclick=\"adjustArticleScore($id, $score)\" title=\"$score $score_title\">";
+                                       title=\"$score\">";
 
                                if ($score > 500) {
                                        $hlc_suffix = "H";
 
                                        print "</a>";
 
+                                       print $labels_str;
+
 #                                                      <a href=\"javascript:viewfeed($feed_id, '', false)\">".
 #                                                      $line["feed_title"]."</a>       
 
                                                        (".__('Show article').")</a>";
                                        } */
 
+                                       print $labels_str;
 
                                        if (!get_pref($link, 'VFEED_GROUP_BY_FEED')) {
                                                if ($line["feed_title"]) {      
                                print "</table>";
                        }
 
-//                     print_headline_subtoolbar($link, 
-//                             "javascript:catchupPage()", "Mark page as read", true, $rtl_content);
-
-
                } else {
                        $message = "";
 
                                case "unread":
                                        $message = __("No unread articles found to display.");
                                        break;
+                               case "updated":
+                                       $message = __("No updated articles found to display.");
+                                       break;
                                case "marked":
                                        $message = __("No starred articles found to display.");
                                        break;
                                default:
-                                       $message = __("No articles found to display.");
+                                       if ($feed < -10) {
+                                               $message = __("No articles found to display. You can assign articles to labels manually (see the Actions menu above) or use a filter.");
+                                       } else {
+                                               $message = __("No articles found to display.");
+                                       }
                        }
 
                        if (!$offset) print "<div class='whiteBox'>$message</div>";
 
        function printTagCloud($link) {
 
-               /* get first ref_id to count from */
-
-               /*
-
-               $query = "";
-
-               if (DB_TYPE == "pgsql") {
-                       $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries 
-                               WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
-                               AND date_entered > NOW() - INTERVAL '30 days'";
-               } else {
-                       $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries 
-                               WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]." 
-                               AND date_entered > DATE_SUB(NOW(), INTERVAL 30 DAY)";
-               }
-
-               $result = db_query($link, $query);
-               $first_id = db_fetch_result($result, 0, "id"); */
-
-               //AND post_int_id >= '$first_id'
                $query = "SELECT tag_name, COUNT(post_int_id) AS count 
                        FROM ttrss_tags WHERE owner_uid = ".$_SESSION["uid"]." 
                        GROUP BY tag_name ORDER BY count DESC LIMIT 50";
 
                $result = db_query($link, "DELETE FROM ttrss_entries WHERE 
                        (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
+
+               ccache_update($link, $id, $_SESSION['uid']);
        } // function clear_feed_articles
 
        /**
 
        } // function update_daemon_common
 
-       function generate_dashboard_feed($link) {
-
-               print "<div id=\"headlinesContainer\">";
-
-               print_headline_subtoolbar($link, "", "Dashboard", 
-                       false, false, -4, false, false, false,
-                       false, 0, 0, true, true, "tag.png");
-
-               print "<div id=\"headlinesInnerContainer\" class=\"dashboard\">";
-               print "<div>There is <b>666</b> unread articles in <b>666</b> feeds.</div>";
-               print "</div>";
-
-               print "</div>";
-
-               print "]]></headlines>";
-               print "<headlines-count value=\"0\"/>";
-               print "<headlines-unread value=\"0\"/>";
-               print "<disable-cache value=\"1\"/>";
-
-               print "<articles>";
-               print "</articles>";
-       }
-
        function sanitize_article_content($text) {
                # we don't support CDATA sections in articles, they break our own escaping
                $text = preg_replace("/\[\[CDATA/", "", $text);
                print "</table>";
        }
 
-       function print_label_dlg_common_examples() {
-
-               print __("Match ") . " ";
-
-/*             print "<select name=\"label_andor\">";
-               print "<option value=\"and\">AND</option>";
-               print "<option value=\"or\">OR</option>";
-               print "</select>"; */
-
-               print "<select name=\"label_fields\" onchange=\"labelFieldsCheck(this)\">";
-               print "<option value=\"unread\">".__("Unread articles")."</option>";
-               print "<option value=\"updated\">".__("Updated articles")."</option>";
-               print "<option value=\"kw_title\">".__("Title contains")."</option>";
-               print "<option value=\"kw_content\">".__("Content contains")."</option>";
-               print "<option value=\"scoreE\">".__("Score equals")."</option>";
-               print "<option value=\"scoreG\">".__("Score is greater than")."</option>";
-               print "<option value=\"scoreL\">".__("Score is less than")."</option>";
-               print "<option value=\"newerH\">".__("Articles newer than X hours")."</option>";
-               print "<option value=\"newerD\">".__("Articles newer than X days")."</option>";
-
-               print "</select>";
-
-               print "<input style=\"display : none\" name=\"label_fields_param\"
-                       size=\"10\">";
-
-               print " <input type=\"submit\" 
-                       onclick=\"return addLabelExample()\"
-                       value=\"".__("Add")."\">";
-       }
-
        function feed_has_icon($id) {
                return is_file(ICONS_DIR . "/$id.ico") && filesize(ICONS_DIR . "/$id.ico") > 0;
        }
                        FROM ttrss_feeds WHERE (SELECT COUNT(id) = 0 FROM ttrss_feeds AS tf 
                                WHERE tf.feed_url = ttrss_feeds.feed_url 
                                AND (private IS true OR feed_url LIKE '%:%@%/%')) 
-                               GROUP BY feed_url ORDER BY subscribers DESC");
+                               GROUP BY feed_url ORDER BY subscribers DESC LIMIT 200");
        
                db_query($link, "BEGIN");
        
                return $count;
 
        }
+
+       function ccache_zero($link, $feed_id, $owner_uid) {
+               db_query($link, "UPDATE ttrss_counters_cache SET
+                       value = 0, updated = NOW() WHERE
+                       feed_id = '$feed_id' AND owner_uid = '$owner_uid'");
+       }
+
+       function ccache_zero_all($link, $owner_uid) {
+               db_query($link, "UPDATE ttrss_counters_cache SET
+                       value = 0 WHERE owner_uid = '$owner_uid'");
+
+               db_query($link, "UPDATE ttrss_cat_counters_cache SET
+                       value = 0 WHERE owner_uid = '$owner_uid'");
+       }
+
+       function ccache_update_all($link, $owner_uid) {
+
+               if (get_pref($link, 'ENABLE_FEED_CATS', $owner_uid)) {
+
+                       $result = db_query($link, "SELECT feed_id FROM ttrss_cat_counters_cache
+                               WHERE feed_id > 0 AND owner_uid = '$owner_uid'");
+
+                       while ($line = db_fetch_assoc($result)) {
+                               ccache_update($link, $line["feed_id"], $owner_uid, true);
+                       }
+
+                       /* We have to manually include category 0 */
+
+                       ccache_update($link, 0, $owner_uid, true);
+
+               } else {
+                       $result = db_query($link, "SELECT feed_id FROM ttrss_counters_cache
+                               WHERE feed_id > 0 AND owner_uid = '$owner_uid'");
+
+                       while ($line = db_fetch_assoc($result)) {
+                               print ccache_update($link, $line["feed_id"], $owner_uid);
+
+                       }
+
+               }
+       }
+
+       function ccache_find($link, $feed_id, $owner_uid, $is_cat = false, 
+               $no_update = false) {
+
+               if (!$is_cat) {
+                       $table = "ttrss_counters_cache";
+               } else {
+                       $table = "ttrss_cat_counters_cache";
+               }
+
+               if (DB_TYPE == "pgsql") {
+                       $date_qpart = "updated > NOW() - INTERVAL '15 minutes'";
+               } else if (DB_TYPE == "mysql") {
+                       $date_qpart = "updated > DATE_SUB(NOW(), INTERVAL 15 MINUTE)";
+               }
+
+               $result = db_query($link, "SELECT value FROM $table
+                       WHERE owner_uid = '$owner_uid' AND feed_id = '$feed_id' 
+                       LIMIT 1");
+
+               if (db_num_rows($result) == 1) {
+                       return db_fetch_result($result, 0, "value");
+               } else {
+                       if ($no_update) {
+                               return -1;
+                       } else {
+                               return ccache_update($link, $feed_id, $owner_uid, $is_cat);
+                       }
+               }
+
+       }
+
+       function ccache_update($link, $feed_id, $owner_uid, $is_cat = false, 
+               $update_pcat = true) {
+
+               $prev_unread = ccache_find($link, $feed_id, $owner_uid, $is_cat, true);
+
+               /* When updating a label, all we need to do is recalculate feed counters
+                * because labels are not cached */
+
+               if ($feed_id < 0) {
+                       ccache_update_all($link, $owner_uid);
+                       return;
+               }
+
+               if (!$is_cat) {
+                       $table = "ttrss_counters_cache";
+               } else {
+                       $table = "ttrss_cat_counters_cache";
+               }
+
+               if ($is_cat && $feed_id >= 0) {
+                       if ($feed_id != 0) {
+                               $cat_qpart = "cat_id = '$feed_id'";
+                       } else {
+                               $cat_qpart = "cat_id IS NULL";
+                       }
+
+                       /* Recalculate counters for child feeds */
+
+                       $result = db_query($link, "SELECT id FROM ttrss_feeds
+                                               WHERE owner_uid = '$owner_uid' AND $cat_qpart");
+
+                       while ($line = db_fetch_assoc($result)) {
+                               ccache_update($link, $line["id"], $owner_uid, false, false);
+                       }
+
+                       $result = db_query($link, "SELECT SUM(value) AS sv 
+                               FROM ttrss_counters_cache, ttrss_feeds 
+                               WHERE id = feed_id AND $cat_qpart AND 
+                               ttrss_feeds.owner_uid = '$owner_uid'");
+
+                       $unread = (int) db_fetch_result($result, 0, "sv");
+
+               } else {
+                       $unread = (int) getFeedArticles($link, $feed_id, $is_cat, true, $owner_uid);
+               }
+
+               $result = db_query($link, "SELECT feed_id FROM $table
+                       WHERE owner_uid = '$owner_uid' AND feed_id = '$feed_id' LIMIT 1");
+
+               if (db_num_rows($result) == 1) {
+                       db_query($link, "UPDATE $table SET
+                               value = '$unread', updated = NOW() WHERE
+                               feed_id = '$feed_id' AND owner_uid = '$owner_uid'");
+
+               } else {
+                       db_query($link, "INSERT INTO $table
+                               (feed_id, value, owner_uid, updated) 
+                               VALUES 
+                               ($feed_id, $unread, $owner_uid, NOW())");
+               }
+
+               if ($feed_id > 0 && $prev_unread != $unread) {
+
+                       if (!$is_cat) {
+
+                               /* Update parent category */
+
+                               if ($update_pcat) {
+
+                                       $result = db_query($link, "SELECT cat_id FROM ttrss_feeds
+                                               WHERE owner_uid = '$owner_uid' AND id = '$feed_id'");
+
+                                       $cat_id = (int) db_fetch_result($result, 0, "cat_id");
+
+                                       ccache_update($link, $cat_id, $owner_uid, true);
+
+                               }
+                       }
+               } else if ($feed_id < 0) {
+                       ccache_update_all($link, $owner_uid);
+               }
+
+               return $unread;
+       }
+
+       function label_find_id($link, $label, $owner_uid) {
+               $result = db_query($link, 
+                       "SELECT id FROM ttrss_labels2 WHERE caption = '$label' 
+                               AND owner_uid = '$owner_uid' LIMIT 1");
+
+               if (db_num_rows($result) == 1) {
+                       return db_fetch_result($result, 0, "id");
+               } else {
+                       return 0;
+               }
+       }
+
+       function get_article_labels($link, $id) {
+               $result = db_query($link, 
+                       "SELECT DISTINCT label_id,caption 
+                               FROM ttrss_labels2, ttrss_user_labels2 
+                       WHERE id = label_id 
+                               AND article_id = '$id' 
+                               AND owner_uid = ".$_SESSION["uid"] . "
+                       ORDER BY caption");
+
+               $rv = array();
+
+               while ($line = db_fetch_assoc($result)) {
+                       $rk = array($line["label_id"], $line["caption"]);
+                       array_push($rv, $rk);
+               }
+
+               return $rv;
+       }
+
+
+       function label_find_caption($link, $label, $owner_uid) {
+               $result = db_query($link, 
+                       "SELECT caption FROM ttrss_labels2 WHERE id = '$label' 
+                               AND owner_uid = '$owner_uid' LIMIT 1");
+
+               if (db_num_rows($result) == 1) {
+                       return db_fetch_result($result, 0, "caption");
+               } else {
+                       return "";
+               }
+       }
+
+       function label_remove_article($link, $id, $label, $owner_uid) {
+
+               $label_id = label_find_id($link, $label, $owner_uid);
+
+               if (!$label_id) return;
+
+               $result = db_query($link, 
+                       "DELETE FROM ttrss_user_labels2
+                       WHERE 
+                               label_id = '$label_id' AND
+                               article_id = '$id'");
+       }
+
+       function label_add_article($link, $id, $label, $owner_uid) {
+
+               $label_id = label_find_id($link, $label, $owner_uid);
+
+               if (!$label_id) return;
+
+               $result = db_query($link, 
+                       "SELECT 
+                               article_id FROM ttrss_labels2, ttrss_user_labels2
+                       WHERE 
+                               label_id = id AND 
+                               label_id = '$label_id' AND
+                               article_id = '$id' AND owner_uid = '$owner_uid'
+                       LIMIT 1");
+
+               if (db_num_rows($result) == 0) {
+                       db_query($link, "INSERT INTO ttrss_user_labels2 
+                               (label_id, article_id) VALUES ('$label_id', '$id')");
+               }
+       }
+
+       function label_remove($link, $id, $owner_uid) {
+
+               db_query($link, "BEGIN");
+
+               $result = db_query($link, "SELECT caption FROM ttrss_labels2
+                       WHERE id = '$id'");
+
+               $caption = db_fetch_result($result, 0, "caption");
+
+               $result = db_query($link, "DELETE FROM ttrss_labels2 WHERE id = '$id'
+                       AND owner_uid = " . $_SESSION["uid"]);
+
+               if (db_affected_rows($link, $result) != 0 && $caption) {
+
+                       /* Disable filters that reference label being removed */
+       
+                       db_query($link, "UPDATE ttrss_filters SET
+                               enabled = false WHERE action_param = '$caption'
+                                       AND action_id = 7
+                                       AND owner_uid = " . $_SESSION["uid"]);
+                       }
+
+               db_query($link, "COMMIT");
+       }
 ?>