]> git.wh0rd.org Git - tt-rss.git/commitdiff
split some more functions from functions.php
authorAndrew Dolgov <fox@madoka.volgo-balt.ru>
Tue, 22 Jan 2013 18:32:17 +0000 (22:32 +0400)
committerAndrew Dolgov <fox@madoka.volgo-balt.ru>
Tue, 22 Jan 2013 18:32:17 +0000 (22:32 +0400)
classes/article.php
classes/feeds.php
classes/pref/feeds.php
classes/rpc.php
include/ccache.php [new file with mode: 0644]
include/functions.php
include/labels.php [new file with mode: 0644]
include/rssfuncs.php

index 16619c9ad02ec2cc1ad28c12657a99f63e59deca..0cb7073b510c220870e6602f57ae8e43ad211157 100644 (file)
@@ -52,7 +52,7 @@ class Article extends Handler_Protected {
                        return;\r
                }\r
 \r
-               catchupArticleById($this->link, $id, 0);\r
+               $this->catchupArticleById($this->link, $id, 0);\r
 \r
                if (!$_SESSION["bw_limit"]) {\r
                        foreach ($cids as $cid) {\r
@@ -63,7 +63,27 @@ class Article extends Handler_Protected {
                }\r
 \r
                print json_encode($articles);\r
+       }\r
+\r
+       private function catchupArticleById($link, $id, $cmode) {\r
+\r
+               if ($cmode == 0) {\r
+                       db_query($link, "UPDATE ttrss_user_entries SET\r
+                       unread = false,last_read = NOW()\r
+                       WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);\r
+               } else if ($cmode == 1) {\r
+                       db_query($link, "UPDATE ttrss_user_entries SET\r
+                       unread = true\r
+                       WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);\r
+               } else {\r
+                       db_query($link, "UPDATE ttrss_user_entries SET\r
+                       unread = NOT unread,last_read = NOW()\r
+                       WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);\r
+               }\r
 \r
+               $feed_id = getArticleFeed($link, $id);\r
+               ccache_update($link, $feed_id, $_SESSION["uid"]);\r
        }\r
 \r
+\r
 }\r
index d10f1fdfac28b3f8a05da03ea2966886eda78858..3d34a3b2c0ddd862c40570009287b20f311f800e 100644 (file)
@@ -807,7 +807,7 @@ class Feeds extends Handler_Protected {
                 * when there's nothing to load - e.g. no stuff in fresh feed */\r
 \r
                if ($feed == -5) {\r
-                       print json_encode(generate_dashboard_feed($this->link));\r
+                       print json_encode($this->generate_dashboard_feed($this->link));\r
                        return;\r
                }\r
 \r
@@ -826,7 +826,7 @@ class Feeds extends Handler_Protected {
                }\r
 \r
                if ($result && db_num_rows($result) == 0) {\r
-                       print json_encode(generate_error_feed($this->link, __("Feed not found.")));\r
+                       print json_encode($this->generate_error_feed($this->link, __("Feed not found.")));\r
                        return;\r
                }\r
 \r
@@ -929,5 +929,63 @@ class Feeds extends Handler_Protected {
                print json_encode($reply);\r
 \r
        }\r
+\r
+       private function generate_dashboard_feed($link) {\r
+               $reply = array();\r
+\r
+               $reply['headlines']['id'] = -5;\r
+               $reply['headlines']['is_cat'] = false;\r
+\r
+               $reply['headlines']['toolbar'] = '';\r
+               $reply['headlines']['content'] = "<div class='whiteBox'>".__('No feed selected.');\r
+\r
+               $reply['headlines']['content'] .= "<p class=\"small\"><span class=\"insensitive\">";\r
+\r
+               $result = db_query($link, "SELECT ".SUBSTRING_FOR_DATE."(MAX(last_updated), 1, 19) AS last_updated FROM ttrss_feeds\r
+                       WHERE owner_uid = " . $_SESSION['uid']);\r
+\r
+               $last_updated = db_fetch_result($result, 0, "last_updated");\r
+               $last_updated = make_local_datetime($link, $last_updated, false);\r
+\r
+               $reply['headlines']['content'] .= sprintf(__("Feeds last updated at %s"), $last_updated);\r
+\r
+               $result = db_query($link, "SELECT COUNT(id) AS num_errors\r
+                       FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ".$_SESSION["uid"]);\r
+\r
+               $num_errors = db_fetch_result($result, 0, "num_errors");\r
+\r
+               if ($num_errors > 0) {\r
+                       $reply['headlines']['content'] .= "<br/>";\r
+                       $reply['headlines']['content'] .= "<a class=\"insensitive\" href=\"#\" onclick=\"showFeedsWithErrors()\">".\r
+                               __('Some feeds have update errors (click for details)')."</a>";\r
+               }\r
+               $reply['headlines']['content'] .= "</span></p>";\r
+\r
+               $reply['headlines-info'] = array("count" => 0,\r
+                       "vgroup_last_feed" => '',\r
+                       "unread" => 0,\r
+                       "disable_cache" => true);\r
+\r
+               return $reply;\r
+       }\r
+\r
+       private function generate_error_feed($link, $error) {\r
+               $reply = array();\r
+\r
+               $reply['headlines']['id'] = -6;\r
+               $reply['headlines']['is_cat'] = false;\r
+\r
+               $reply['headlines']['toolbar'] = '';\r
+               $reply['headlines']['content'] = "<div class='whiteBox'>". $error . "</div>";\r
+\r
+               $reply['headlines-info'] = array("count" => 0,\r
+                       "vgroup_last_feed" => '',\r
+                       "unread" => 0,\r
+                       "disable_cache" => true);\r
+\r
+               return $reply;\r
+       }\r
+\r
+\r
 }\r
 ?>\r
index f11c746bfb54d71c8ee9fd4517589a4a07328588..e9b09829de40cd1e21d8ee8b70621e9808576bea 100644 (file)
@@ -1070,7 +1070,7 @@ class Pref_Feeds extends Handler_Protected {
 
        function clear() {
                $id = db_escape_string($_REQUEST["id"]);
-               clear_feed_articles($this->link, $id);
+               $this->clear_feed_articles($this->link, $id);
        }
 
        function rescore() {
@@ -1677,5 +1677,29 @@ class Pref_Feeds extends Handler_Protected {
                print "</div>";
        }
 
+       /**
+        * 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
+        */
+       private function clear_feed_articles($link, $id) {
+
+               if ($id != 0) {
+                       $result = db_query($link, "DELETE FROM ttrss_user_entries
+                       WHERE feed_id = '$id' AND marked = false AND owner_uid = " . $_SESSION["uid"]);
+               } else {
+                       $result = db_query($link, "DELETE FROM ttrss_user_entries
+                       WHERE feed_id IS NULL 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");
+
+               ccache_update($link, $id, $_SESSION['uid']);
+       } // function clear_feed_articles
+
+
 }
 ?>
index b4afc6023ae6906b87f7f71004ed75ae47955176..0e6fa3379e8514f9319d66bcb8327ebe57244afb 100644 (file)
@@ -169,12 +169,44 @@ class RPC extends Handler_Protected {
                $ids = explode(",", db_escape_string($_REQUEST["ids"]));
 
                foreach ($ids as $id) {
-                       archive_article($this->link, $id, $_SESSION["uid"]);
+                       $this->archive_article($this->link, $id, $_SESSION["uid"]);
                }
 
                print json_encode(array("message" => "UPDATE_COUNTERS"));
        }
 
+       private function archive_article($link, $id, $owner_uid) {
+               db_query($link, "BEGIN");
+
+               $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
+                       WHERE ref_id = '$id' AND owner_uid = $owner_uid");
+
+               if (db_num_rows($result) != 0) {
+
+                       /* prepare the archived table */
+
+                       $feed_id = (int) db_fetch_result($result, 0, "feed_id");
+
+                       if ($feed_id) {
+                               $result = db_query($link, "SELECT id FROM ttrss_archived_feeds
+                                       WHERE id = '$feed_id'");
+
+                               if (db_num_rows($result) == 0) {
+                                       db_query($link, "INSERT INTO ttrss_archived_feeds
+                                               (id, owner_uid, title, feed_url, site_url)
+                                       SELECT id, owner_uid, title, feed_url, site_url from ttrss_feeds
+                                       WHERE id = '$feed_id'");
+                               }
+
+                               db_query($link, "UPDATE ttrss_user_entries
+                                       SET orig_feed_id = feed_id, feed_id = NULL
+                                       WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
+                       }
+               }
+
+               db_query($link, "COMMIT");
+       }
+
        function publ() {
                $pub = $_REQUEST["pub"];
                $id = db_escape_string($_REQUEST["id"]);
@@ -241,7 +273,7 @@ class RPC extends Handler_Protected {
                $ids = explode(",", db_escape_string($_REQUEST["ids"]));
                $cmode = sprintf("%d", $_REQUEST["cmode"]);
 
-               markArticlesById($this->link, $ids, $cmode);
+               $this->markArticlesById($this->link, $ids, $cmode);
 
                print json_encode(array("message" => "UPDATE_COUNTERS"));
        }
@@ -250,7 +282,7 @@ class RPC extends Handler_Protected {
                $ids = explode(",", db_escape_string($_REQUEST["ids"]));
                $cmode = sprintf("%d", $_REQUEST["cmode"]);
 
-               publishArticlesById($this->link, $ids, $cmode);
+               $this->publishArticlesById($this->link, $ids, $cmode);
 
                print json_encode(array("message" => "UPDATE_COUNTERS"));
        }
@@ -335,7 +367,7 @@ class RPC extends Handler_Protected {
        }
 
        function regenOPMLKey() {
-               update_feed_access_key($this->link, 'OPML:Publish',
+               $this->update_feed_access_key($this->link, 'OPML:Publish',
                false, $_SESSION["uid"]);
 
                $new_link = opml_publish_url($this->link);
@@ -544,7 +576,7 @@ class RPC extends Handler_Protected {
                $feed_id = db_escape_string($_REQUEST['id']);
                $is_cat = db_escape_string($_REQUEST['is_cat']) == "true";
 
-               $new_key = update_feed_access_key($this->link, $feed_id, $is_cat);
+               $new_key = $this->update_feed_access_key($this->link, $feed_id, $is_cat);
 
                print json_encode(array("link" => $new_key));
        }
@@ -716,5 +748,88 @@ class RPC extends Handler_Protected {
 
        }
 
+       function update_feed_access_key($link, $feed_id, $is_cat, $owner_uid = false) {
+               if (!$owner_uid) $owner_uid = $_SESSION["uid"];
+
+               $sql_is_cat = bool_to_sql_bool($is_cat);
+
+               $result = db_query($link, "SELECT access_key FROM ttrss_access_keys
+                       WHERE feed_id = '$feed_id'      AND is_cat = $sql_is_cat
+                       AND owner_uid = " . $owner_uid);
+
+               if (db_num_rows($result) == 1) {
+                       $key = db_escape_string(sha1(uniqid(rand(), true)));
+
+                       db_query($link, "UPDATE ttrss_access_keys SET access_key = '$key'
+                               WHERE feed_id = '$feed_id' AND is_cat = $sql_is_cat
+                               AND owner_uid = " . $owner_uid);
+
+                       return $key;
+
+               } else {
+                       return get_feed_access_key($link, $feed_id, $is_cat, $owner_uid);
+               }
+       }
+
+       private function markArticlesById($link, $ids, $cmode) {
+
+               $tmp_ids = array();
+
+               foreach ($ids as $id) {
+                       array_push($tmp_ids, "ref_id = '$id'");
+               }
+
+               $ids_qpart = join(" OR ", $tmp_ids);
+
+               if ($cmode == 0) {
+                       db_query($link, "UPDATE ttrss_user_entries SET
+                       marked = false,last_read = NOW()
+                       WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
+               } else if ($cmode == 1) {
+                       db_query($link, "UPDATE ttrss_user_entries SET
+                       marked = true
+                       WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
+               } else {
+                       db_query($link, "UPDATE ttrss_user_entries SET
+                       marked = NOT marked,last_read = NOW()
+                       WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
+               }
+       }
+
+       private function publishArticlesById($link, $ids, $cmode) {
+
+               $tmp_ids = array();
+
+               foreach ($ids as $id) {
+                       array_push($tmp_ids, "ref_id = '$id'");
+               }
+
+               $ids_qpart = join(" OR ", $tmp_ids);
+
+               if ($cmode == 0) {
+                       db_query($link, "UPDATE ttrss_user_entries SET
+                       published = false,last_read = NOW()
+                       WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
+               } else if ($cmode == 1) {
+                       db_query($link, "UPDATE ttrss_user_entries SET
+                       published = true,last_read = NOW()
+                       WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
+               } else {
+                       db_query($link, "UPDATE ttrss_user_entries SET
+                       published = NOT published,last_read = NOW()
+                       WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
+               }
+
+               if (PUBSUBHUBBUB_HUB) {
+                       $rss_link = get_self_url_prefix() .
+                               "/public.php?op=rss&id=-2&key=" .
+                               get_feed_access_key($link, -2, false);
+
+                       $p = new Publisher(PUBSUBHUBBUB_HUB);
+
+                       $pubsub_result = $p->publish_update($rss_link);
+               }
+       }
+
 }
 ?>
diff --git a/include/ccache.php b/include/ccache.php
new file mode 100644 (file)
index 0000000..d036100
--- /dev/null
@@ -0,0 +1,222 @@
+<?php
+       /* 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_remove($link, $feed_id, $owner_uid, $is_cat = false) {
+
+               if (!$is_cat) {
+                       $table = "ttrss_counters_cache";
+               } else {
+                       $table = "ttrss_cat_counters_cache";
+               }
+
+               db_query($link, "DELETE FROM $table WHERE
+                       feed_id = '$feed_id' AND 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_numeric($feed_id)) return;
+
+               if (!$is_cat) {
+                       $table = "ttrss_counters_cache";
+                       if ($feed_id > 0) {
+                               $tmp_result = db_query($link, "SELECT owner_uid FROM ttrss_feeds
+                                       WHERE id = '$feed_id'");
+                               $owner_uid = db_fetch_result($tmp_result, 0, "owner_uid");
+                       }
+               } 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) {
+
+               if (!is_numeric($feed_id)) return;
+
+               if (!$is_cat && $feed_id > 0) {
+                       $tmp_result = db_query($link, "SELECT owner_uid FROM ttrss_feeds
+                               WHERE id = '$feed_id'");
+                       $owner_uid = db_fetch_result($tmp_result, 0, "owner_uid");
+               }
+
+               $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);
+               }
+
+               db_query($link, "BEGIN");
+
+               $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())");
+               }
+
+               db_query($link, "COMMIT");
+
+               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 ccache_cleanup($link, $owner_uid) {
+
+               if (DB_TYPE == "pgsql") {
+                       db_query($link, "DELETE FROM ttrss_counters_cache AS c1 WHERE
+                               (SELECT count(*) FROM ttrss_counters_cache AS c2
+                                       WHERE c1.feed_id = c2.feed_id AND c2.owner_uid = c1.owner_uid) > 1
+                                       AND owner_uid = '$owner_uid'");
+
+                       db_query($link, "DELETE FROM ttrss_cat_counters_cache AS c1 WHERE
+                               (SELECT count(*) FROM ttrss_cat_counters_cache AS c2
+                                       WHERE c1.feed_id = c2.feed_id AND c2.owner_uid = c1.owner_uid) > 1
+                                       AND owner_uid = '$owner_uid'");
+               } else {
+                       db_query($link, "DELETE c1 FROM
+                                       ttrss_counters_cache AS c1,
+                                       ttrss_counters_cache AS c2
+                               WHERE
+                                       c1.owner_uid = '$owner_uid' AND
+                                       c1.owner_uid = c2.owner_uid AND
+                                       c1.feed_id = c2.feed_id");
+
+                       db_query($link, "DELETE c1 FROM
+                                       ttrss_cat_counters_cache AS c1,
+                                       ttrss_cat_counters_cache AS c2
+                               WHERE
+                                       c1.owner_uid = '$owner_uid' AND
+                                       c1.owner_uid = c2.owner_uid AND
+                                       c1.feed_id = c2.feed_id");
+
+               }
+       } */
+?>
index 0f8f1ccb8938e7824dd7de604e6172228bd7d8ed..5ff0dd49f2ba8cd3bf57621d52da869af304f6a0 100644 (file)
@@ -63,6 +63,7 @@
        require_once "lib/accept-to-gettext.php";
        require_once "lib/gettext/gettext.inc";
 
+
        function startup_gettext() {
 
                # Get locale from Accept-Language header
 
        require_once 'db-prefs.php';
        require_once 'version.php';
+       require_once 'ccache.php';
+       require_once 'labels.php';
 
        ini_set('user_agent', SELF_USER_AGENT);
 
                return false;
        }
 
-       function markArticlesById($link, $ids, $cmode) {
-
-               $tmp_ids = array();
-
-               foreach ($ids as $id) {
-                       array_push($tmp_ids, "ref_id = '$id'");
-               }
-
-               $ids_qpart = join(" OR ", $tmp_ids);
-
-               if ($cmode == 0) {
-                       db_query($link, "UPDATE ttrss_user_entries SET
-                       marked = false,last_read = NOW()
-                       WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
-               } else if ($cmode == 1) {
-                       db_query($link, "UPDATE ttrss_user_entries SET
-                       marked = true
-                       WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
-               } else {
-                       db_query($link, "UPDATE ttrss_user_entries SET
-                       marked = NOT marked,last_read = NOW()
-                       WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
-               }
-       }
-
-       function publishArticlesById($link, $ids, $cmode) {
-
-               $tmp_ids = array();
-
-               foreach ($ids as $id) {
-                       array_push($tmp_ids, "ref_id = '$id'");
-               }
-
-               $ids_qpart = join(" OR ", $tmp_ids);
-
-               if ($cmode == 0) {
-                       db_query($link, "UPDATE ttrss_user_entries SET
-                       published = false,last_read = NOW()
-                       WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
-               } else if ($cmode == 1) {
-                       db_query($link, "UPDATE ttrss_user_entries SET
-                       published = true,last_read = NOW()
-                       WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
-               } else {
-                       db_query($link, "UPDATE ttrss_user_entries SET
-                       published = NOT published,last_read = NOW()
-                       WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
-               }
-
-               if (PUBSUBHUBBUB_HUB) {
-                       $rss_link = get_self_url_prefix() .
-                               "/public.php?op=rss&id=-2&key=" .
-                               get_feed_access_key($link, -2, false);
-
-                       $p = new Publisher(PUBSUBHUBBUB_HUB);
-
-                       $pubsub_result = $p->publish_update($rss_link);
-               }
-       }
-
        function catchupArticlesById($link, $ids, $cmode, $owner_uid = false) {
 
                if (!$owner_uid) $owner_uid = $_SESSION["uid"];
                }
        }
 
-       function catchupArticleById($link, $id, $cmode) {
-
-               if ($cmode == 0) {
-                       db_query($link, "UPDATE ttrss_user_entries SET
-                       unread = false,last_read = NOW()
-                       WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
-               } else if ($cmode == 1) {
-                       db_query($link, "UPDATE ttrss_user_entries SET
-                       unread = true
-                       WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
-               } else {
-                       db_query($link, "UPDATE ttrss_user_entries SET
-                       unread = NOT unread,last_read = NOW()
-                       WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
-               }
-
-               $feed_id = getArticleFeed($link, $id);
-               ccache_update($link, $feed_id, $_SESSION["uid"]);
-       }
-
-       function make_guid_from_title($title) {
-               return preg_replace("/[ \"\',.:;]/", "-",
-                       mb_strtolower(strip_tags($title), 'utf-8'));
-       }
-
        function get_article_tags($link, $id, $owner_uid = 0, $tag_cache = false) {
 
                $a_id = db_escape_string($id);
                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) {
-
-               if ($id != 0) {
-                       $result = db_query($link, "DELETE FROM ttrss_user_entries
-                       WHERE feed_id = '$id' AND marked = false AND owner_uid = " . $_SESSION["uid"]);
-               } else {
-                       $result = db_query($link, "DELETE FROM ttrss_user_entries
-                       WHERE feed_id IS NULL 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");
-
-               ccache_update($link, $id, $_SESSION['uid']);
-       } // function clear_feed_articles
-
        /**
         * Compute the Mozilla Firefox feed adding URL from server HOST and REQUEST_URI.
         *
                }
        }
 
-       /* 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_remove($link, $feed_id, $owner_uid, $is_cat = false) {
-
-               if (!$is_cat) {
-                       $table = "ttrss_counters_cache";
-               } else {
-                       $table = "ttrss_cat_counters_cache";
-               }
-
-               db_query($link, "DELETE FROM $table WHERE
-                       feed_id = '$feed_id' AND 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_numeric($feed_id)) return;
-
-               if (!$is_cat) {
-                       $table = "ttrss_counters_cache";
-                       if ($feed_id > 0) {
-                               $tmp_result = db_query($link, "SELECT owner_uid FROM ttrss_feeds
-                                       WHERE id = '$feed_id'");
-                               $owner_uid = db_fetch_result($tmp_result, 0, "owner_uid");
-                       }
-               } 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) {
-
-               if (!is_numeric($feed_id)) return;
-
-               if (!$is_cat && $feed_id > 0) {
-                       $tmp_result = db_query($link, "SELECT owner_uid FROM ttrss_feeds
-                               WHERE id = '$feed_id'");
-                       $owner_uid = db_fetch_result($tmp_result, 0, "owner_uid");
-               }
-
-               $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);
-               }
-
-               db_query($link, "BEGIN");
-
-               $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())");
-               }
-
-               db_query($link, "COMMIT");
-
-               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 ccache_cleanup($link, $owner_uid) {
-
-               if (DB_TYPE == "pgsql") {
-                       db_query($link, "DELETE FROM ttrss_counters_cache AS c1 WHERE
-                               (SELECT count(*) FROM ttrss_counters_cache AS c2
-                                       WHERE c1.feed_id = c2.feed_id AND c2.owner_uid = c1.owner_uid) > 1
-                                       AND owner_uid = '$owner_uid'");
-
-                       db_query($link, "DELETE FROM ttrss_cat_counters_cache AS c1 WHERE
-                               (SELECT count(*) FROM ttrss_cat_counters_cache AS c2
-                                       WHERE c1.feed_id = c2.feed_id AND c2.owner_uid = c1.owner_uid) > 1
-                                       AND owner_uid = '$owner_uid'");
-               } else {
-                       db_query($link, "DELETE c1 FROM
-                                       ttrss_counters_cache AS c1,
-                                       ttrss_counters_cache AS c2
-                               WHERE
-                                       c1.owner_uid = '$owner_uid' AND
-                                       c1.owner_uid = c2.owner_uid AND
-                                       c1.feed_id = c2.feed_id");
-
-                       db_query($link, "DELETE c1 FROM
-                                       ttrss_cat_counters_cache AS c1,
-                                       ttrss_cat_counters_cache AS c2
-                               WHERE
-                                       c1.owner_uid = '$owner_uid' AND
-                                       c1.owner_uid = c2.owner_uid AND
-                                       c1.feed_id = c2.feed_id");
-
-               }
-       } */
-
-       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, $owner_uid = false) {
-               $rv = array();
-
-               if (!$owner_uid) $owner_uid = $_SESSION["uid"];
-
-               $result = db_query($link, "SELECT label_cache FROM
-                       ttrss_user_entries WHERE ref_id = '$id' AND owner_uid = " .
-                       $owner_uid);
-
-               if (db_num_rows($result) > 0) {
-                       $label_cache = db_fetch_result($result, 0, "label_cache");
-
-                       if ($label_cache) {
-                               $label_cache = json_decode($label_cache, true);
-
-                               if ($label_cache["no-labels"] == 1)
-                                       return $rv;
-                               else
-                                       return $label_cache;
-                       }
-               }
-
-               $result = db_query($link,
-                       "SELECT DISTINCT label_id,caption,fg_color,bg_color
-                               FROM ttrss_labels2, ttrss_user_labels2
-                       WHERE id = label_id
-                               AND article_id = '$id'
-                               AND owner_uid = ". $owner_uid . "
-                       ORDER BY caption");
-
-               while ($line = db_fetch_assoc($result)) {
-                       $rk = array($line["label_id"], $line["caption"], $line["fg_color"],
-                               $line["bg_color"]);
-                       array_push($rv, $rk);
-               }
-
-               if (count($rv) > 0)
-                       label_update_cache($link, $owner_uid, $id, $rv);
-               else
-                       label_update_cache($link, $owner_uid, $id, array("no-labels" => 1));
-
-               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 get_all_labels($link, $owner_uid) {
-               $rv = array();
-
-               $result = db_query($link, "SELECT fg_color, bg_color, caption FROM ttrss_labels2 WHERE owner_uid = " . $owner_uid);
-
-               while ($line = db_fetch_assoc($result)) {
-                       array_push($rv, $line);
-               }
-
-               return $rv;
-       }
-
-       function label_update_cache($link, $owner_uid, $id, $labels = false, $force = false) {
-
-               if ($force)
-                       label_clear_cache($link, $id);
-
-               if (!$labels)
-                       $labels = get_article_labels($link, $id);
-
-               $labels = db_escape_string(json_encode($labels));
-
-               db_query($link, "UPDATE ttrss_user_entries SET
-                       label_cache = '$labels' WHERE ref_id = '$id' AND  owner_uid = '$owner_uid'");
-
-       }
-
-       function label_clear_cache($link, $id) {
-
-               db_query($link, "UPDATE ttrss_user_entries SET
-                       label_cache = '' WHERE ref_id = '$id'");
-
-       }
-
-       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'");
-
-               label_clear_cache($link, $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')");
-               }
-
-               label_clear_cache($link, $id);
-
-       }
-
-       function label_remove($link, $id, $owner_uid) {
-               if (!$owner_uid) $owner_uid = $_SESSION["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 = " . $owner_uid);
-
-               if (db_affected_rows($link, $result) != 0 && $caption) {
-
-                       /* Remove access key for the label */
-
-                       $ext_id = -11 - $id;
-
-                       db_query($link, "DELETE FROM ttrss_access_keys WHERE
-                               feed_id = '$ext_id' AND owner_uid = $owner_uid");
-
-                       /* 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 = " . $owner_uid);
-
-                       /* Remove cached data */
-
-                       db_query($link, "UPDATE ttrss_user_entries SET label_cache = ''
-                               WHERE label_cache LIKE '%$caption%' AND owner_uid = " . $owner_uid);
-
-               }
-
-               db_query($link, "COMMIT");
-       }
-
-       function label_create($link, $caption, $fg_color = '', $bg_color = '', $owner_uid) {
-
-               if (!$owner_uid) $owner_uid = $_SESSION['uid'];
-
-               db_query($link, "BEGIN");
-
-               $result = false;
-
-               $result = db_query($link, "SELECT id FROM ttrss_labels2
-                       WHERE caption = '$caption' AND owner_uid = $owner_uid");
-
-               if (db_num_rows($result) == 0) {
-                       $result = db_query($link,
-                               "INSERT INTO ttrss_labels2 (caption,owner_uid,fg_color,bg_color)
-                                       VALUES ('$caption', '$owner_uid', '$fg_color', '$bg_color')");
-
-                       $result = db_affected_rows($link, $result) != 0;
-               }
-
-               db_query($link, "COMMIT");
-
-               return $result;
-       }
-
        function format_tags_string($tags, $id) {
 
                $tags_str = "";
                ccache_remove($link, $id, $owner_uid, true);
        }
 
-       function archive_article($link, $id, $owner_uid) {
-               db_query($link, "BEGIN");
-
-               $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
-                       WHERE ref_id = '$id' AND owner_uid = $owner_uid");
-
-               if (db_num_rows($result) != 0) {
-
-                       /* prepare the archived table */
-
-                       $feed_id = (int) db_fetch_result($result, 0, "feed_id");
-
-                       if ($feed_id) {
-                               $result = db_query($link, "SELECT id FROM ttrss_archived_feeds
-                                       WHERE id = '$feed_id'");
-
-                               if (db_num_rows($result) == 0) {
-                                       db_query($link, "INSERT INTO ttrss_archived_feeds
-                                               (id, owner_uid, title, feed_url, site_url)
-                                       SELECT id, owner_uid, title, feed_url, site_url from ttrss_feeds
-                                       WHERE id = '$feed_id'");
-                               }
-
-                               db_query($link, "UPDATE ttrss_user_entries
-                                       SET orig_feed_id = feed_id, feed_id = NULL
-                                       WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
-                       }
-               }
-
-               db_query($link, "COMMIT");
-       }
-
        function getArticleFeed($link, $id) {
                $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
                        WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
                return $rv;
        }
 
-       function generate_error_feed($link, $error) {
-               $reply = array();
-
-               $reply['headlines']['id'] = -6;
-               $reply['headlines']['is_cat'] = false;
-
-               $reply['headlines']['toolbar'] = '';
-               $reply['headlines']['content'] = "<div class='whiteBox'>". $error . "</div>";
-
-               $reply['headlines-info'] = array("count" => 0,
-                       "vgroup_last_feed" => '',
-                       "unread" => 0,
-                       "disable_cache" => true);
-
-               return $reply;
-       }
-
-
-       function generate_dashboard_feed($link) {
-               $reply = array();
-
-               $reply['headlines']['id'] = -5;
-               $reply['headlines']['is_cat'] = false;
-
-               $reply['headlines']['toolbar'] = '';
-               $reply['headlines']['content'] = "<div class='whiteBox'>".__('No feed selected.');
-
-               $reply['headlines']['content'] .= "<p class=\"small\"><span class=\"insensitive\">";
-
-               $result = db_query($link, "SELECT ".SUBSTRING_FOR_DATE."(MAX(last_updated), 1, 19) AS last_updated FROM ttrss_feeds
-                       WHERE owner_uid = " . $_SESSION['uid']);
-
-               $last_updated = db_fetch_result($result, 0, "last_updated");
-               $last_updated = make_local_datetime($link, $last_updated, false);
-
-               $reply['headlines']['content'] .= sprintf(__("Feeds last updated at %s"), $last_updated);
-
-               $result = db_query($link, "SELECT COUNT(id) AS num_errors
-                       FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ".$_SESSION["uid"]);
-
-               $num_errors = db_fetch_result($result, 0, "num_errors");
-
-               if ($num_errors > 0) {
-                       $reply['headlines']['content'] .= "<br/>";
-                       $reply['headlines']['content'] .= "<a class=\"insensitive\" href=\"#\" onclick=\"showFeedsWithErrors()\">".
-                               __('Some feeds have update errors (click for details)')."</a>";
-               }
-               $reply['headlines']['content'] .= "</span></p>";
-
-               $reply['headlines-info'] = array("count" => 0,
-                       "vgroup_last_feed" => '',
-                       "unread" => 0,
-                       "disable_cache" => true);
-
-               return $reply;
-       }
-
        function save_email_address($link, $email) {
                // FIXME: implement persistent storage of emails
 
                        array_push($_SESSION['stored_emails'], $email);
        }
 
-       function update_feed_access_key($link, $feed_id, $is_cat, $owner_uid = false) {
-               if (!$owner_uid) $owner_uid = $_SESSION["uid"];
-
-               $sql_is_cat = bool_to_sql_bool($is_cat);
-
-               $result = db_query($link, "SELECT access_key FROM ttrss_access_keys
-                       WHERE feed_id = '$feed_id'      AND is_cat = $sql_is_cat
-                       AND owner_uid = " . $owner_uid);
-
-               if (db_num_rows($result) == 1) {
-                       $key = db_escape_string(sha1(uniqid(rand(), true)));
-
-                       db_query($link, "UPDATE ttrss_access_keys SET access_key = '$key'
-                               WHERE feed_id = '$feed_id' AND is_cat = $sql_is_cat
-                               AND owner_uid = " . $owner_uid);
-
-                       return $key;
-
-               } else {
-                       return get_feed_access_key($link, $feed_id, $is_cat, $owner_uid);
-               }
-       }
 
        function get_feed_access_key($link, $feed_id, $is_cat, $owner_uid = false) {
 
diff --git a/include/labels.php b/include/labels.php
new file mode 100644 (file)
index 0000000..da7e3f9
--- /dev/null
@@ -0,0 +1,207 @@
+<?php
+       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, $owner_uid = false) {
+               $rv = array();
+
+               if (!$owner_uid) $owner_uid = $_SESSION["uid"];
+
+               $result = db_query($link, "SELECT label_cache FROM
+                       ttrss_user_entries WHERE ref_id = '$id' AND owner_uid = " .
+                       $owner_uid);
+
+               if (db_num_rows($result) > 0) {
+                       $label_cache = db_fetch_result($result, 0, "label_cache");
+
+                       if ($label_cache) {
+                               $label_cache = json_decode($label_cache, true);
+
+                               if ($label_cache["no-labels"] == 1)
+                                       return $rv;
+                               else
+                                       return $label_cache;
+                       }
+               }
+
+               $result = db_query($link,
+                       "SELECT DISTINCT label_id,caption,fg_color,bg_color
+                               FROM ttrss_labels2, ttrss_user_labels2
+                       WHERE id = label_id
+                               AND article_id = '$id'
+                               AND owner_uid = ". $owner_uid . "
+                       ORDER BY caption");
+
+               while ($line = db_fetch_assoc($result)) {
+                       $rk = array($line["label_id"], $line["caption"], $line["fg_color"],
+                               $line["bg_color"]);
+                       array_push($rv, $rk);
+               }
+
+               if (count($rv) > 0)
+                       label_update_cache($link, $owner_uid, $id, $rv);
+               else
+                       label_update_cache($link, $owner_uid, $id, array("no-labels" => 1));
+
+               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 get_all_labels($link, $owner_uid) {
+               $rv = array();
+
+               $result = db_query($link, "SELECT fg_color, bg_color, caption FROM ttrss_labels2 WHERE owner_uid = " . $owner_uid);
+
+               while ($line = db_fetch_assoc($result)) {
+                       array_push($rv, $line);
+               }
+
+               return $rv;
+       }
+
+       function label_update_cache($link, $owner_uid, $id, $labels = false, $force = false) {
+
+               if ($force)
+                       label_clear_cache($link, $id);
+
+               if (!$labels)
+                       $labels = get_article_labels($link, $id);
+
+               $labels = db_escape_string(json_encode($labels));
+
+               db_query($link, "UPDATE ttrss_user_entries SET
+                       label_cache = '$labels' WHERE ref_id = '$id' AND  owner_uid = '$owner_uid'");
+
+       }
+
+       function label_clear_cache($link, $id) {
+
+               db_query($link, "UPDATE ttrss_user_entries SET
+                       label_cache = '' WHERE ref_id = '$id'");
+
+       }
+
+       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'");
+
+               label_clear_cache($link, $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')");
+               }
+
+               label_clear_cache($link, $id);
+
+       }
+
+       function label_remove($link, $id, $owner_uid) {
+               if (!$owner_uid) $owner_uid = $_SESSION["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 = " . $owner_uid);
+
+               if (db_affected_rows($link, $result) != 0 && $caption) {
+
+                       /* Remove access key for the label */
+
+                       $ext_id = -11 - $id;
+
+                       db_query($link, "DELETE FROM ttrss_access_keys WHERE
+                               feed_id = '$ext_id' AND owner_uid = $owner_uid");
+
+                       /* 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 = " . $owner_uid);
+
+                       /* Remove cached data */
+
+                       db_query($link, "UPDATE ttrss_user_entries SET label_cache = ''
+                               WHERE label_cache LIKE '%$caption%' AND owner_uid = " . $owner_uid);
+
+               }
+
+               db_query($link, "COMMIT");
+       }
+
+       function label_create($link, $caption, $fg_color = '', $bg_color = '', $owner_uid) {
+
+               if (!$owner_uid) $owner_uid = $_SESSION['uid'];
+
+               db_query($link, "BEGIN");
+
+               $result = false;
+
+               $result = db_query($link, "SELECT id FROM ttrss_labels2
+                       WHERE caption = '$caption' AND owner_uid = $owner_uid");
+
+               if (db_num_rows($result) == 0) {
+                       $result = db_query($link,
+                               "INSERT INTO ttrss_labels2 (caption,owner_uid,fg_color,bg_color)
+                                       VALUES ('$caption', '$owner_uid', '$fg_color', '$bg_color')");
+
+                       $result = db_affected_rows($link, $result) != 0;
+               }
+
+               db_query($link, "COMMIT");
+
+               return $result;
+       }
+
+?>
index b20c7958ca9ae608cfeefa60164da4d3bf9bd06e..8500df0df718626c5e511fd5308d38600f96ba2a 100644 (file)
 
                return "";
        }
+
+       function make_guid_from_title($title) {
+               return preg_replace("/[ \"\',.:;]/", "-",
+                       mb_strtolower(strip_tags($title), 'utf-8'));
+       }
+
+
 ?>