From 19e47ad60bc390b52cfe1545a6d401545e6568a5 Mon Sep 17 00:00:00 2001
From: Andrew Dolgov <noreply@fakecake.org>
Date: Sat, 21 Nov 2015 22:20:00 +0300
Subject: [PATCH] queryFeedHeadlines: don't check first_id when sorting by
 oldest first

---
 classes/api.php        | 10 +++++++---
 classes/feeds.php      | 10 +++++++---
 include/functions2.php |  3 ++-
 3 files changed, 16 insertions(+), 7 deletions(-)
 mode change 100644 => 100755 classes/api.php
 mode change 100644 => 100755 classes/feeds.php

diff --git a/classes/api.php b/classes/api.php
old mode 100644
new mode 100755
index 08c1846d2..dfe9ef6cb
--- a/classes/api.php
+++ b/classes/api.php
@@ -210,6 +210,8 @@ class API extends Handler {
 
 			$_SESSION['hasSandbox'] = $has_sandbox;
 
+			$skip_first_id_check = false;
+
 			$override_order = false;
 			switch ($_REQUEST["order_by"]) {
 				case "title":
@@ -217,6 +219,7 @@ class API extends Handler {
 					break;
 				case "date_reverse":
 					$override_order = "score DESC, date_entered, updated";
+					$skip_first_id_check = true;
 					break;
 				case "feed_dates":
 					$override_order = "updated DESC";
@@ -230,7 +233,7 @@ class API extends Handler {
 			list($headlines, $headlines_header) = $this->api_get_headlines($feed_id, $limit, $offset,
 				$filter, $is_cat, $show_excerpt, $show_content, $view_mode, $override_order,
 				$include_attachments, $since_id, $search,
-				$include_nested, $sanitize_content, $force_update, $excerpt_length, $check_first_id);
+				$include_nested, $sanitize_content, $force_update, $excerpt_length, $check_first_id, $skip_first_id_check);
 
 			if ($include_header) {
 				$this->wrap(self::STATUS_OK, array($headlines_header, $headlines));
@@ -644,7 +647,7 @@ class API extends Handler {
 				$filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order,
 				$include_attachments, $since_id,
 				$search = "", $include_nested = false, $sanitize_content = true,
-				$force_update = false, $excerpt_length = 100, $check_first_id = false) {
+				$force_update = false, $excerpt_length = 100, $check_first_id = false, $skip_first_id_check = false) {
 
 			if ($force_update && $feed_id > 0 && is_numeric($feed_id)) {
 				// Update the feed if required with some basic flood control
@@ -686,7 +689,8 @@ class API extends Handler {
 				"offset" => $offset,
 				"since_id" => $since_id,
 				"include_children" => $include_nested,
-				"check_first_id" => $check_first_id
+				"check_first_id" => $check_first_id,
+				"skip_first_id_check" => $skip_first_id_check
 			);
 
 			$qfh_ret = queryFeedHeadlines($params);
diff --git a/classes/feeds.php b/classes/feeds.php
old mode 100644
new mode 100755
index c3cb72da8..07a18741d
--- a/classes/feeds.php
+++ b/classes/feeds.php
@@ -148,7 +148,8 @@ class Feeds extends Handler_Protected {
 
 	private function format_headlines_list($feed, $method, $view_mode, $limit, $cat_view,
 					$next_unread_feed, $offset, $vgr_last_feed = false,
-					$override_order = false, $include_children = false, $check_first_id = false) {
+					$override_order = false, $include_children = false, $check_first_id = false,
+					$skip_first_id_check = false) {
 
 		$disable_cache = false;
 
@@ -252,7 +253,8 @@ class Feeds extends Handler_Protected {
 				"override_order" => $override_order,
 				"offset" => $offset,
 				"include_children" => $include_children,
-				"check_first_id" => $check_first_id
+				"check_first_id" => $check_first_id,
+				"skip_first_id_check" => $skip_first_id_check
 			);
 
 			$qfh_ret = queryFeedHeadlines($params);
@@ -903,6 +905,7 @@ class Feeds extends Handler_Protected {
 		$reply['headlines'] = array();
 
 		$override_order = false;
+		$skip_first_id_check = false;
 
 		switch ($order_by) {
 		case "title":
@@ -910,6 +913,7 @@ class Feeds extends Handler_Protected {
 			break;
 		case "date_reverse":
 			$override_order = "score DESC, date_entered, updated";
+			$skip_first_id_check = true;
 			break;
 		case "feed_dates":
 			$override_order = "updated DESC";
@@ -920,7 +924,7 @@ class Feeds extends Handler_Protected {
 
 		$ret = $this->format_headlines_list($feed, $method,
 			$view_mode, $limit, $cat_view, $next_unread_feed, $offset,
-			$vgroup_last_feed, $override_order, true, $check_first_id);
+			$vgroup_last_feed, $override_order, true, $check_first_id, $skip_first_id_check);
 
 		//$topmost_article_ids = $ret[0];
 		$headlines_count = $ret[1];
diff --git a/include/functions2.php b/include/functions2.php
index 779688716..0386b52ed 100755
--- a/include/functions2.php
+++ b/include/functions2.php
@@ -465,6 +465,7 @@
 		$override_vfeed = isset($params["override_vfeed"]) ? $params["override_vfeed"] : false;
 		$start_ts = isset($params["start_ts"]) ? $params["start_ts"] : false;
 		$check_first_id = isset($params["check_first_id"]) ? $params["check_first_id"] : false;
+		$skip_first_id_check = isset($params["skip_first_id_check"]) ? $params["skip_first_id_check"] : false;
 
 		$ext_tables_part = "";
 		$query_strategy_part = "";
@@ -731,7 +732,7 @@
 					$sanity_interval_qpart = "date_entered >= DATE_SUB(NOW(), INTERVAL 1 hour) AND";
 				}
 
-				if (!$search) {
+				if (!$search && !$skip_first_id_check) {
 					// if previous topmost article id changed that means our current pagination is no longer valid
 					$query = "SELECT DISTINCT
 							ttrss_feeds.title,
-- 
GitLab