$print_exec_time = false;
- if ((!$op || $op == "rpc" || $op == "rss" || $op == "digestSend" ||
+ if ((!$op || $op == "rpc" || $op == "rss" || $op == "view" || $op == "digestSend" ||
$op == "globalUpdateFeeds") && !$_REQUEST["noxml"]) {
header("Content-Type: application/xml; charset=utf-8");
} else {
}
- if ($op == "view") {
+ function outputArticleXML($link, $id, $feed_id, $mark_as_read = true) {
- $id = db_escape_string($_GET["id"]);
- $feed_id = db_escape_string($_GET["feed"]);
+ print "<article id='$id'><![CDATA[";
$result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
$rtl_class = "";
}
- $result = db_query($link, "UPDATE ttrss_user_entries
- SET unread = false,last_read = NOW()
- WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
+ if ($mark_as_read) {
+ $result = db_query($link, "UPDATE ttrss_user_entries
+ SET unread = false,last_read = NOW()
+ WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
+ }
$result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
SUBSTRING(updated,1,16) as updated,
print "</div>";
}
+
+ print "]]></article>";
+
+ }
+
+ if ($op == "view") {
+
+ $id = db_escape_string($_GET["id"]);
+ $feed_id = db_escape_string($_GET["feed"]);
+ $cids = split(",", db_escape_string($_GET["cids"]));
+ $mode = db_escape_string($_GET["mode"]);
+
+ print "<reply>";
+
+ // in prefetch mode we only output requested cids, main article
+ // just gets marked as read (it already exists in client cache)
+
+ if ($mode != "prefetch") {
+ outputArticleXML($link, $id, $feed_id);
+ } else {
+ catchupArticleById($link, $id, 0);
+ }
+
+ foreach ($cids as $cid) {
+ if ($cid) {
+ outputArticleXML($link, $cid, $feed_id, false);
+ }
+ }
+
+ print "</reply>";
}
if ($op == "viewfeed") {
exception_error("logoutUser", e);
}
}
+
+// this only searches loaded headlines list, not in CDM
+function getRelativePostIds(id) {
+
+ debug("getRelativePostIds: " + id);
+
+ var ids = new Array();
+ var container = document.getElementById("headlinesList");
+
+ if (container) {
+ var rows = container.rows;
+
+ for (var i = 0; i < rows.length; i++) {
+ var r_id = rows[i].id.replace("RROW-", "");
+
+ if (r_id == id) {
+ if (i > 0) ids.push(rows[i-1].id.replace("RROW-", ""));
+ if (i > 1) ids.push(rows[i-2].id.replace("RROW-", ""));
+ if (i > 2) ids.push(rows[i-3].id.replace("RROW-", ""));
+
+ if (i < rows.length) ids.push(rows[i+1].id.replace("RROW-", ""));
+ if (i < rows.length-1) ids.push(rows[i+2].id.replace("RROW-", ""));
+ if (i < rows.length-2) ids.push(rows[i+3].id.replace("RROW-", ""));
+
+ return ids;
+ }
+ }
+ }
+
+ return false;
+}
var _cdm_wd_timeout = false;
var _cdm_wd_vishist = new Array();
+var article_cache = new Array();
+
function catchup_callback() {
if (xmlhttp_rpc.readyState == 4) {
try {
}
}
-function article_callback() {
- if (xmlhttp.readyState == 4) {
- debug("article_callback");
+function render_article(article) {
+ try {
var f = document.getElementById("content-frame");
try {
f.scrollTop = 0;
} catch (e) { };
- f.innerHTML = xmlhttp.responseText;
+
+ f.innerHTML = article;
+
+ } catch (e) {
+ exception_error("render_article", e);
+ }
+}
+
+function article_callback() {
+ if (xmlhttp.readyState == 4) {
+ debug("article_callback");
+
+ try {
+ if (xmlhttp.responseXML) {
+ var reply = xmlhttp.responseXML.firstChild.firstChild;
+
+ var articles = xmlhttp.responseXML.getElementsByTagName("article");
+
+ for (var i = 0; i < articles.length; i++) {
+ var a_id = articles[i].getAttribute("id");
+
+ debug("found id: " + a_id);
+
+ if (a_id == active_post_id) {
+ debug("active article, rendering...");
+ render_article(articles[i].firstChild.nodeValue);
+ }
+
+ cache_inject(a_id, articles[i].firstChild.nodeValue);
+ }
+
+ } else {
+ debug("article_callback: returned no XML object");
+ }
+ } catch (e) {
+ exception_error("article_callback", e);
+ }
var date = new Date();
last_article_view = date.getTime() / 1000;
active_real_feed_id = feed_id;
- if (!skip_history) {
+ var cached_article = cache_find(id);
+
+ debug("cache check result: " + (cached_article != false));
+
+/* if (!skip_history) {
history_push("ARTICLE:" + id + ":" + feed_id);
- }
+ } */
enableHotkeys();
xmlhttp.abort();
}
- if (xmlhttp_ready(xmlhttp)) {
+ if (cached_article || xmlhttp_ready(xmlhttp)) {
cleanSelected("headlinesList");
var crow = document.getElementById("RROW-" + active_post_id);
+
+ var article_is_unread = crow.className.match("Unread");
+ debug("article is unread: " + article_is_unread);
+
crow.className = crow.className.replace("Unread", "");
var upd_img_pic = document.getElementById("FUPDPIC-" + active_post_id);
selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
markHeadline(active_post_id);
+ var neighbor_ids = getRelativePostIds(active_post_id);
+
+ /* only request uncached articles */
+
+ var cids_to_request = Array();
+
+ for (var i = 0; i < neighbor_ids.length; i++) {
+ if (!cache_check(neighbor_ids[i])) {
+ cids_to_request.push(neighbor_ids[i]);
+ }
+ }
+
+ debug("additional ids: " + cids_to_request.toString());
+
var date = new Date();
var timestamp = Math.round(date.getTime() / 1000);
query = query + "&ts=" + timestamp;
- notify_progress("Loading, please wait...");
+ query = query + "&cids=" + cids_to_request.toString();
+
+ if (!cached_article) {
+
+ notify_progress("Loading, please wait...");
+
+ debug(query);
+
+ xmlhttp.open("GET", query, true);
+ xmlhttp.onreadystatechange=article_callback;
+ xmlhttp.send(null);
+ } else if (cached_article && article_is_unread) {
+
+ query = query + "&mode=prefetch";
+
+ debug(query);
+
+ xmlhttp.open("GET", query, true);
+ xmlhttp.onreadystatechange=article_callback;
+ xmlhttp.send(null);
+
+ render_article(cached_article);
+
+ } else if (cached_article) {
+
+ render_article(cached_article);
+
+ }
+
+ cache_expire();
- xmlhttp.open("GET", query, true);
- xmlhttp.onreadystatechange=article_callback;
- xmlhttp.send(null);
} else {
debug("xmlhttp busy (@view)");
printLockingError();
}
}
+
+
+function cache_inject(id, article) {
+ if (!article_cache[id]) {
+ debug("cache_article: miss: " + id);
+
+ var cache_obj = new Array();
+
+ var d = new Date();
+ cache_obj["entered"] = d.getTime() / 1000;
+ cache_obj["data"] = article;
+
+ article_cache[id] = cache_obj;
+
+ } else {
+ debug("cache_article: hit: " + id);
+ }
+}
+
+function cache_find(id) {
+ if (typeof article_cache[id] != 'undefined') {
+ return article_cache[id]["data"];
+ } else {
+ return false;
+ }
+}
+
+function cache_check(id) {
+ return typeof article_cache[id] != 'undefined';
+}
+
+function cache_expire() {
+ /* TODO */
+}