From 87d7e8507a4a41c4e0d7a4f2d54fe48f3a6f72cb Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 22 Jan 2013 22:32:17 +0400 Subject: split some more functions from functions.php --- classes/article.php | 22 ++++++++- classes/feeds.php | 62 +++++++++++++++++++++++- classes/pref/feeds.php | 26 +++++++++- classes/rpc.php | 125 +++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 226 insertions(+), 9 deletions(-) (limited to 'classes') diff --git a/classes/article.php b/classes/article.php index 16619c9ad..0cb7073b5 100644 --- a/classes/article.php +++ b/classes/article.php @@ -52,7 +52,7 @@ class Article extends Handler_Protected { return; } - catchupArticleById($this->link, $id, 0); + $this->catchupArticleById($this->link, $id, 0); if (!$_SESSION["bw_limit"]) { foreach ($cids as $cid) { @@ -63,7 +63,27 @@ class Article extends Handler_Protected { } print json_encode($articles); + } + + private 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"]); } + } diff --git a/classes/feeds.php b/classes/feeds.php index d10f1fdfa..3d34a3b2c 100644 --- a/classes/feeds.php +++ b/classes/feeds.php @@ -807,7 +807,7 @@ class Feeds extends Handler_Protected { * when there's nothing to load - e.g. no stuff in fresh feed */ if ($feed == -5) { - print json_encode(generate_dashboard_feed($this->link)); + print json_encode($this->generate_dashboard_feed($this->link)); return; } @@ -826,7 +826,7 @@ class Feeds extends Handler_Protected { } if ($result && db_num_rows($result) == 0) { - print json_encode(generate_error_feed($this->link, __("Feed not found."))); + print json_encode($this->generate_error_feed($this->link, __("Feed not found."))); return; } @@ -929,5 +929,63 @@ class Feeds extends Handler_Protected { print json_encode($reply); } + + private function generate_dashboard_feed($link) { + $reply = array(); + + $reply['headlines']['id'] = -5; + $reply['headlines']['is_cat'] = false; + + $reply['headlines']['toolbar'] = ''; + $reply['headlines']['content'] = "
".__('No feed selected.'); + + $reply['headlines']['content'] .= "

"; + + $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'] .= "
"; + $reply['headlines']['content'] .= "". + __('Some feeds have update errors (click for details)').""; + } + $reply['headlines']['content'] .= "

"; + + $reply['headlines-info'] = array("count" => 0, + "vgroup_last_feed" => '', + "unread" => 0, + "disable_cache" => true); + + return $reply; + } + + private function generate_error_feed($link, $error) { + $reply = array(); + + $reply['headlines']['id'] = -6; + $reply['headlines']['is_cat'] = false; + + $reply['headlines']['toolbar'] = ''; + $reply['headlines']['content'] = "
". $error . "
"; + + $reply['headlines-info'] = array("count" => 0, + "vgroup_last_feed" => '', + "unread" => 0, + "disable_cache" => true); + + return $reply; + } + + } ?> diff --git a/classes/pref/feeds.php b/classes/pref/feeds.php index f11c746bf..e9b09829d 100644 --- a/classes/pref/feeds.php +++ b/classes/pref/feeds.php @@ -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 "
"; } + /** + * 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 + + } ?> diff --git a/classes/rpc.php b/classes/rpc.php index b4afc6023..0e6fa3379 100644 --- a/classes/rpc.php +++ b/classes/rpc.php @@ -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); + } + } + } ?> -- cgit v1.2.3-54-g00ecf