]> git.wh0rd.org Git - tt-rss.git/commitdiff
counters for linked feeds, misc SQL optimizations
authorAndrew Dolgov <fox@madoka.spb.ru>
Thu, 29 Dec 2005 06:19:27 +0000 (07:19 +0100)
committerAndrew Dolgov <fox@madoka.spb.ru>
Thu, 29 Dec 2005 06:19:27 +0000 (07:19 +0100)
backend.php
functions.php

index f159b9a8f9f24fb5095992f2bd0cde3336cd935e..122a6745c8c35fd47c6f58e47b9b87517aa1c1bf 100644 (file)
                }
        }
 
-       function getFeedCounter($link, $id) {
+/*     function getFeedCounter($link, $id) {
        
                $result = db_query($link, "SELECT 
                                count(id) as count,last_error
                        $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) {
 
 
                $old_counters = $_SESSION["fctr_last_value"];
 
-               $result = db_query($link, "SELECT id,last_error,
+               $result = db_query($link, "SELECT id,last_error,parent_feed,
                        (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"]);
+                       FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
+                               AND parent_feed IS NULL");
 
                $fctrs_modified = false;
 
                
                        $id = $line["id"];
                        $count = $line["count"];
-                       $last_error = htmlspecialchars($line["last_error"]);    
-
+                       $last_error = htmlspecialchars($line["last_error"]);
+       
                        $has_img = is_file(ICONS_DIR . "/$id.ico");
 
+                       $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 = '$id' 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"];
+                               }
+                       }
+
                        if (!$smart_mode || $old_counters[$id] != $count) {
                                $old_counters[$id] = $count;
                                $fctrs_modified = true;
                                $order_by_qpart = "title";
                        }
 
-                       $result = db_query($link, "SELECT *,
-                               (SELECT count(id) FROM ttrss_entries,ttrss_user_entries
+                       $result = db_query($link, "SELECT ttrss_feeds.*,
+                               (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
                                        owner_uid = '$owner_uid') AS total,
-                               (SELECT count(id) FROM ttrss_entries,ttrss_user_entries
+                               (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
                                        WHERE feed_id = ttrss_feeds.id AND unread = true
                                                AND ttrss_user_entries.ref_id = ttrss_entries.id
                                                AND owner_uid = '$owner_uid') as unread,
-                               (SELECT title FROM ttrss_feed_categories 
-                                       WHERE id = cat_id) AS category,
                                cat_id,last_error,
-                               (SELECT collapsed FROM ttrss_feed_categories
-                                       WHERE id = cat_id) AS collapsed
-                               FROM ttrss_feeds WHERE 
-                                       owner_uid = '$owner_uid' AND parent_feed IS NULL
-                               ORDER BY $order_by_qpart");                     
-       
+                               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)                          
+                               WHERE 
+                                       ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
+                               ORDER BY $order_by_qpart"); 
+
                        $actid = $_GET["actid"];
        
                        /* real feeds */
                                $total = $line["total"];
                                $unread = $line["unread"];
 
+                               $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"];
+                                       }
+                               }
+
                                $cat_id = $line["cat_id"];
 
                                $tmp_category = $line["category"];
                }
 
                $result = db_query($link, "SELECT 
-                               id,title,feed_url,substring(last_updated,1,16) as last_updated,
-                               update_interval,purge_interval,cat_id,
-                               parent_feed AS parent_feed_id,
-                               (SELECT title FROM ttrss_feed_categories 
-                                       WHERE id = cat_id) AS category,
-                               (SELECT title FROM ttrss_feeds 
-                                       WHERE id = parent_feed_id) AS parent_title                              
+                               F1.id,
+                               F1.title,
+                               F1.feed_url,
+                               substring(F1.last_updated,1,16) AS last_updated,
+                               F1.parent_feed,
+                               F1.update_interval,
+                               F1.purge_interval,
+                               F1.cat_id,
+                               F2.title AS parent_title,
+                               C1.title AS category                            
                        FROM 
-                               ttrss_feeds 
+                               ttrss_feeds AS F1 
+                               LEFT JOIN ttrss_feeds AS F2
+                                       ON (F1.parent_feed = F2.id)
+                               LEFT JOIN ttrss_feed_categories AS C1
+                                       ON (F1.cat_id = C1.id)
                        WHERE 
-                               $search_qpart owner_uid = '".$_SESSION["uid"]."'                        
+                               $search_qpart F1.owner_uid = '".$_SESSION["uid"]."'                     
                        ORDER by category,$feeds_sort,title");
 
                if (db_num_rows($result) != 0) {
index aececd8d9ad8116d6cbfebecc1039fb37cc2d4cf..11eb267966e0ca16b51e152d1158606c25e991b1 100644 (file)
                        $filters = array();
 
                        $result = db_query($link, "SELECT reg_exp,
-                               (SELECT name FROM ttrss_filter_types
-                                       WHERE id = filter_type) as name,
-                               (SELECT name FROM ttrss_filter_actions
-                                       WHERE id = action_id) as action
-                               FROM ttrss_filters WHERE 
-                               owner_uid = $owner_uid AND
+                               ttrss_filter_types.name AS name,
+                               ttrss_filter_actions.name AS action
+                               FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE                                        
+                                       owner_uid = $owner_uid AND
+                                       ttrss_filter_types.id = filter_type AND
+                                       ttrss_filter_actions.id = action_id AND
                                (feed_id IS NULL OR feed_id = '$feed')");
 
                        while ($line = db_fetch_assoc($result)) {