Просмотр исходного кода

Filter on last user modified (#8093)

Example: `userdate:PT1H` to select only articles modified by user during the last hour
Fix https://github.com/FreshRSS/FreshRSS/issues/4280#issuecomment-3393078024

Useful for instance to bulk mark as unread recently marked articles by error:
1. Click on the toggle button to show the read articles (making sure the toggle for the unread articles is off)
2. Sort by *User modified 9→1*
3. Filter by *user modified date*, for instance to the last 3 hours by typing `userdate:PT3H`
4. Click in the drop-down menu *Mark selection as unread*

P.S.: I have added at the same time a bunch of unit tests for date-related logic
Alexandre Alapetite 5 месяцев назад
Родитель
Сommit
7e72033859

+ 12 - 0
app/Models/Entry.php

@@ -654,6 +654,18 @@ HTML;
 				if ($ok && $filter->getNotMaxPubdate() !== null) {
 					$ok &= $this->date > $filter->getNotMaxPubdate();
 				}
+				if ($ok && $filter->getMinUserdate() !== null) {
+					$ok &= $this->lastUserModified >= $filter->getMinUserdate();
+				}
+				if ($ok && $filter->getNotMinUserdate() !== null) {
+					$ok &= $this->lastUserModified < $filter->getNotMinUserdate();
+				}
+				if ($ok && $filter->getMaxUserdate() !== null) {
+					$ok &= $this->lastUserModified <= $filter->getMaxUserdate();
+				}
+				if ($ok && $filter->getNotMaxUserdate() !== null) {
+					$ok &= $this->lastUserModified > $filter->getNotMaxUserdate();
+				}
 				if ($ok && $filter->getFeedIds() !== null) {
 					$ok &= in_array($this->feedId, $filter->getFeedIds(), true);
 				}

+ 23 - 0
app/Models/EntryDAO.php

@@ -906,6 +906,14 @@ SQL;
 				$sub_search .= 'AND ' . $alias . 'date <= ? ';
 				$values[] = $filter->getMaxPubdate();
 			}
+			if ($filter->getMinUserdate() !== null) {
+				$sub_search .= 'AND ' . $alias . '`lastUserModified` >= ? ';
+				$values[] = $filter->getMinUserdate();
+			}
+			if ($filter->getMaxUserdate() !== null) {
+				$sub_search .= 'AND ' . $alias . '`lastUserModified` <= ? ';
+				$values[] = $filter->getMaxUserdate();
+			}
 
 			//Negation of date intervals must be combined by OR
 			if ($filter->getNotMinDate() !== null || $filter->getNotMaxDate() !== null) {
@@ -938,6 +946,21 @@ SQL;
 				}
 				$sub_search .= ') ';
 			}
+			if ($filter->getNotMinUserdate() !== null || $filter->getNotMaxUserdate() !== null) {
+				$sub_search .= 'AND (';
+				if ($filter->getNotMinUserdate() !== null) {
+					$sub_search .= $alias . '`lastUserModified` < ?';
+					$values[] = $filter->getNotMinUserdate();
+					if ($filter->getNotMaxUserdate()) {
+						$sub_search .= ' OR ';
+					}
+				}
+				if ($filter->getNotMaxUserdate() !== null) {
+					$sub_search .= $alias . '`lastUserModified` > ?';
+					$values[] = $filter->getNotMaxUserdate();
+				}
+				$sub_search .= ') ';
+			}
 
 			if ($filter->getFeedIds() !== null) {
 				$sub_search .= 'AND ' . $alias . 'id_feed IN (';

+ 50 - 0
app/Models/Search.php

@@ -43,6 +43,10 @@ class FreshRSS_Search implements \Stringable {
 	private $min_pubdate = null;
 	/** @var int|false|null */
 	private $max_pubdate = null;
+	/** @var int|false|null */
+	private $min_userdate = null;
+	/** @var int|false|null */
+	private $max_userdate = null;
 	/** @var list<string>|null */
 	private ?array $inurl = null;
 	/** @var list<string>|null */
@@ -86,6 +90,10 @@ class FreshRSS_Search implements \Stringable {
 	private $not_min_pubdate = null;
 	/** @var int|false|null */
 	private $not_max_pubdate = null;
+	/** @var int|false|null */
+	private $not_min_userdate = null;
+	/** @var int|false|null */
+	private $not_max_userdate = null;
 	/** @var list<string>|null */
 	private ?array $not_inurl = null;
 	/** @var list<string>|null */
@@ -115,6 +123,7 @@ class FreshRSS_Search implements \Stringable {
 		$input = $this->parseNotLabelIds($input);
 		$input = $this->parseNotLabelNames($input);
 
+		$input = $this->parseNotUserdateSearch($input);
 		$input = $this->parseNotPubdateSearch($input);
 		$input = $this->parseNotDateSearch($input);
 
@@ -130,6 +139,7 @@ class FreshRSS_Search implements \Stringable {
 		$input = $this->parseLabelIds($input);
 		$input = $this->parseLabelNames($input);
 
+		$input = $this->parseUserdateSearch($input);
 		$input = $this->parsePubdateSearch($input);
 		$input = $this->parseDateSearch($input);
 
@@ -265,6 +275,20 @@ class FreshRSS_Search implements \Stringable {
 		return $this->not_max_pubdate ?: null;
 	}
 
+	public function getMinUserdate(): ?int {
+		return $this->min_userdate ?: null;
+	}
+	public function getNotMinUserdate(): ?int {
+		return $this->not_min_userdate ?: null;
+	}
+
+	public function getMaxUserdate(): ?int {
+		return $this->max_userdate ?: null;
+	}
+	public function getNotMaxUserdate(): ?int {
+		return $this->not_max_userdate ?: null;
+	}
+
 	/** @return list<string>|null */
 	public function getInurl(): ?array {
 		return $this->inurl;
@@ -798,6 +822,32 @@ class FreshRSS_Search implements \Stringable {
 		return $input;
 	}
 
+	/**
+	 * Parse the search string to find userdate keyword and the search related to it.
+	 * The search is the first word following the keyword.
+	 */
+	private function parseUserdateSearch(string $input): string {
+		if (preg_match_all('/\\buserdate:(?P<search>[^\\s]*)/', $input, $matches)) {
+			$input = str_replace($matches[0], '', $input);
+			$dates = self::removeEmptyValues($matches['search']);
+			if (!empty($dates[0])) {
+				[$this->min_userdate, $this->max_userdate] = parseDateInterval($dates[0]);
+			}
+		}
+		return $input;
+	}
+
+	private function parseNotUserdateSearch(string $input): string {
+		if (preg_match_all('/(?<=[\\s(]|^)[!-]userdate:(?P<search>[^\\s]*)/', $input, $matches)) {
+			$input = str_replace($matches[0], '', $input);
+			$dates = self::removeEmptyValues($matches['search']);
+			if (!empty($dates[0])) {
+				[$this->not_min_userdate, $this->not_max_userdate] = parseDateInterval($dates[0]);
+			}
+		}
+		return $input;
+	}
+
 	/**
 	 * Parse the search string to find tags keyword (# followed by a word)
 	 * and the search related to it.

+ 1 - 0
docs/en/users/10_filter.md

@@ -91,6 +91,7 @@ You can use the search field to further refine results:
 	* Date constraints may be combined:
 		* `date:P1Y !date:P1M` (from one year before now until one month before now)
 * by date of publication, using the same format: `pubdate:<date-interval>`
+* by date of user modification, using the same format: `userdate:<date-interval>`
 * by custom label ID `L:12` or multiple label IDs: `L:12,13,14` or with any label: `L:*`
 * by custom label name `label:label`, `label:"my label"` or any label name from a list (*or*): `labels:"my label,my other label"`
 * by several label names (*and*): `label:"my label" label:"my other label"`

+ 1 - 0
docs/fr/users/03_Main_view.md

@@ -250,6 +250,7 @@ Il est possible d’utiliser le champ de recherche pour raffiner les résultats
 	* Les contraintes de date peuvent être combinées :
 		* `date:P1Y !date:P1M` (depuis un an avant maintenant jusqu’à un mois avant maintenant)
 * par date de publication, avec la même syntaxe : `pubdate:<date-interval>`
+* par date de modification par l’utilisateur, avec la même syntaxe : `userdate:<date-interval>`
 * par ID d’étiquette : `L:12` ou de plusieurs étiquettes : `L:12,13,14` ou avec n’importe quelle étiquette : `L:*`
 * par nom d’étiquette : `label:étiquette`, `label:"mon étiquette"` ou d’une étiquette parmi une liste (*ou*) : `labels:"mon étiquette,mon autre étiquette"`
 * par plusieurs noms d’étiquettes (*et*) : `label:"mon étiquette" label:"mon autre étiquette"`

+ 2 - 2
lib/lib_date.php

@@ -74,7 +74,7 @@ function _dateRelative(?string $d1, ?string $d2): ?string {
 	if ($d2 === null) {
 		return $d1 !== null && $d1[0] !== 'P' ? $d1 : null;
 	}
-	if ($d2 !== '' && $d2[0] != 'P' && $d1 !== null && $d1[0] !== 'P') {
+	if ($d2 !== '' && $d2[0] !== 'P' && $d1 !== null && $d1[0] !== 'P') {
 		$y2 = substr($d2, 0, 4);
 		if (strlen($y2) < 4 || !ctype_digit($y2)) {	//Does not start by a year
 			$d2 = _noDelimit($d2);
@@ -91,7 +91,7 @@ function _dateRelative(?string $d1, ?string $d2): ?string {
  */
 function parseDateInterval(string $dateInterval): array {
 	$dateInterval = trim($dateInterval);
-	$dateInterval = str_replace('--', '/', $dateInterval);
+	$dateInterval = str_replace(['--', ' '], ['/', 'T'], $dateInterval);
 	$dateInterval = strtoupper($dateInterval);
 	$min = null;
 	$max = null;

+ 136 - 6
tests/app/Models/SearchTest.php

@@ -167,9 +167,9 @@ final class SearchTest extends \PHPUnit\Framework\TestCase {
 	 */
 	public static function provideDateSearch(): array {
 		return [
-			['date:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', 1172754000, 1210519800],
-			['date:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', 1172754000, 1210519799],
-			['date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', 1172754001, 1210519800],
+			['date:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
+			['date:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:29:59Z')],
+			['date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:01Z'), strtotime('2008-05-11T15:30:00Z')],
 			['date:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1],
 			['date:2007-03-01/', strtotime('2007-03-01'), null],
 			['date:/2008-05-11', null, strtotime('2008-05-12') - 1],
@@ -188,15 +188,32 @@ final class SearchTest extends \PHPUnit\Framework\TestCase {
 	 */
 	public static function providePubdateSearch(): array {
 		return [
-			['pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', 1172754000, 1210519800],
-			['pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', 1172754000, 1210519799],
-			['pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', 1172754001, 1210519800],
+			['pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
+			['pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:29:59Z')],
+			['pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:01Z'), strtotime('2008-05-11T15:30:00Z')],
 			['pubdate:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1],
 			['pubdate:2007-03-01/', strtotime('2007-03-01'), null],
 			['pubdate:/2008-05-11', null, strtotime('2008-05-12') - 1],
 		];
 	}
 
+	#[DataProvider('provideUserdateSearch')]
+	public static function test__construct_whenInputContainsUserdate(string $input, ?int $min_userdate_value, ?int $max_userdate_value): void {
+		$search = new FreshRSS_Search($input);
+		self::assertSame($min_userdate_value, $search->getMinUserdate());
+		self::assertSame($max_userdate_value, $search->getMaxUserdate());
+	}
+
+	/**
+	 * @return list<list<mixed>>
+	 */
+	public static function provideUserdateSearch(): array {
+		return [
+			['userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
+			['userdate:/2008-05-11', null, strtotime('2008-05-12') - 1],
+		];
+	}
+
 	/**
 	 * @param array<string>|null $tags_value
 	 * @param array<string>|null $search_value
@@ -583,6 +600,119 @@ final class SearchTest extends \PHPUnit\Framework\TestCase {
 		];
 	}
 
+	/**
+	 * @param array<string> $values
+	 */
+	#[DataProvider('provideDateOperators')]
+	public function test__date_operators(string $input, string $sql, array $values): void {
+		[$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
+		self::assertSame(trim($sql), trim($filterSearch));
+		self::assertSame($values, $filterValues);
+	}
+
+	/** @return list<list<mixed>> */
+	public static function provideDateOperators(): array {
+		return [
+			// Basic date operator tests
+			[
+				'date:2007-03-01/2008-05-11',
+				'(e.id >= ? AND e.id <= ? )',
+				[strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z') . '000000'],
+			],
+			[
+				'date:2007-03-01/',
+				'(e.id >= ? )',
+				[strtotime('2007-03-01T00:00:00Z') . '000000'],
+			],
+			[
+				'date:/2008-05-11',
+				'(e.id <= ? )',
+				[strtotime('2008-05-11T23:59:59Z') . '000000'],
+			],
+			// Basic pubdate operator tests
+			[
+				'pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
+				'(e.date >= ? AND e.date <= ? )',
+				[strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
+			],
+			[
+				'pubdate:2007-03-01/',
+				'(e.date >= ? )',
+				[strtotime('2007-03-01T00:00:00Z')],
+			],
+			[
+				'pubdate:/2008-05-11',
+				'(e.date <= ? )',
+				[strtotime('2008-05-11T23:59:59Z')],
+			],
+			// Basic userdate operator tests
+			[
+				'userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
+				'(e.`lastUserModified` >= ? AND e.`lastUserModified` <= ? )',
+				[strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
+			],
+			[
+				'userdate:2007-03-01/',
+				'(e.`lastUserModified` >= ? )',
+				[strtotime('2007-03-01T00:00:00Z')],
+			],
+			[
+				'userdate:/2008-05-11',
+				'(e.`lastUserModified` <= ? )',
+				[strtotime('2008-05-11T23:59:59Z')],
+			],
+			// Negative date operator tests
+			[
+				'-date:2007-03-01/2008-05-11',
+				'((e.id < ? OR e.id > ?) )',
+				[strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z') . '000000'],
+			],
+			[
+				'!pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
+				'((e.date < ? OR e.date > ?) )',
+				[strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
+			],
+			[
+				'!userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
+				'((e.`lastUserModified` < ? OR e.`lastUserModified` > ?) )',
+				[strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
+			],
+			// Combined date operators
+			[
+				'date:2007-03-01/ pubdate:/2008-05-11',
+				'(e.id >= ? AND e.date <= ? )',
+				[strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z')],
+			],
+			[
+				'pubdate:2007-03-01/ userdate:/2008-05-11',
+				'(e.date >= ? AND e.`lastUserModified` <= ? )',
+				[strtotime('2007-03-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z')],
+			],
+			[
+				'date:2007-03-01/ userdate:2007-06-01/',
+				'(e.id >= ? AND e.`lastUserModified` >= ? )',
+				[strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2007-06-01T00:00:00Z')],
+			],
+			// Complex combinations with other operators
+			[
+				'intitle:test date:2007-03-01/ pubdate:/2008-05-11',
+				'(e.id >= ? AND e.date <= ? AND e.title LIKE ? )',
+				[strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z'), '%test%'],
+			],
+			[
+				'author:john userdate:2007-03-01/2008-05-11',
+				'(e.`lastUserModified` >= ? AND e.`lastUserModified` <= ? AND e.author LIKE ? )',
+				[strtotime('2007-03-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z'), '%john%'],
+			],
+			// Mixed positive and negative date operators
+			[
+				'date:2007-03-01/ !pubdate:2008-01-01/2008-05-11',
+				'(e.id >= ? AND (e.date < ? OR e.date > ?) )',
+				[strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-01-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z')],
+			],
+		];
+	}
+
 	/**
 	 * @dataProvider provideRegexPostreSQL
 	 * @param array<string> $values

+ 84 - 0
tests/lib/LibDateTest.php

@@ -0,0 +1,84 @@
+<?php
+declare(strict_types=1);
+
+require_once LIB_PATH . '/lib_date.php';
+
+use PHPUnit\Framework\Attributes\DataProvider;
+
+/**
+ * Tests for lib_date.php functions
+ */
+class LibDateTest extends \PHPUnit\Framework\TestCase {
+
+	/**
+	 * Test parseDateInterval function with various ISO 8601 interval formats
+	 *
+	 * @param string $interval
+	 * @param int|null|false $expectedMin
+	 * @param int|null|false $expectedMax
+	 */
+	#[DataProvider('provideDateIntervals')]
+	public function test_parseDateInterval(string $interval, $expectedMin, $expectedMax): void {
+		$result = parseDateInterval($interval);
+		self::assertIsArray($result);
+		self::assertCount(2, $result);
+		self::assertSame($expectedMin, $result[0], "Min timestamp mismatch for interval: $interval");
+		self::assertSame($expectedMax, $result[1], "Max timestamp mismatch for interval: $interval");
+	}
+
+	/** @return list<array{string,int|null|false,int|null|false}> */
+	public static function provideDateIntervals(): array {
+		return [
+			['', null, null], // Empty string
+
+			// Year intervals
+			['2014', strtotime('2014-01-01 00:00:00'), strtotime('2014-12-31 23:59:59')],
+			[' 2015 ', strtotime('2015-01-01 00:00:00'), strtotime('2015-12-31 23:59:59')],	// With whitespace to be trimmed
+
+			// Year-month intervals
+			['2014-03', strtotime('2014-03-01 00:00:00'), strtotime('2014-03-31 23:59:59')],
+			['2016-02', strtotime('2016-02-01 00:00:00'), strtotime('2016-02-29 23:59:59')],	// Leap year
+			['2014-02', strtotime('2014-02-01 00:00:00'), strtotime('2014-02-28 23:59:59')],	// Non-leap year
+			['201404', strtotime('2014-04-01 00:00:00'), strtotime('2014-04-30 23:59:59')],	// Without hyphen
+
+			// Specific dates
+			['2014-03-30', strtotime('2014-03-30 00:00:00'), strtotime('2014-03-30 23:59:59')],
+			['2014-05-30T13', strtotime('2014-05-30 13:00:00'), strtotime('2014-05-30 13:59:59')],
+			['2014-05-30T13:30', strtotime('2014-05-30 13:30:00'), strtotime('2014-05-30 13:30:59')],
+
+			// Date ranges with explicit end dates
+			['2014-02/2014-04', strtotime('2014-02-01 00:00:00'), strtotime('2014-04-30 23:59:59')],
+			['2014-02--2014-04', strtotime('2014-02-01 00:00:00'), strtotime('2014-04-30 23:59:59')],	// Same with -- separator
+			['2014-02/04', strtotime('2014-02-01 00:00:00'), strtotime('2014-04-30 23:59:59')],
+			['2014-02-03/05', strtotime('2014-02-03 00:00:00'), strtotime('2014-02-05 23:59:59')],
+
+			// Time ranges within same day
+			['2014-02-03T22:00/22:15', strtotime('2014-02-03 22:00:00'), strtotime('2014-02-03 22:15:59')],
+			['2014-02-03T22:00/15', strtotime('2014-02-03 22:00:00'), strtotime('2014-02-03 22:15:59')],
+
+			// Open intervals
+			['2014-03/', strtotime('2014-03-01 00:00:00'), null],
+			['/2014-03', null, strtotime('2014-03-31 23:59:59')],
+
+			// Period-based intervals
+			['2014-03/P1W', strtotime('2014-03-01 00:00:00'), strtotime('2014-03-07 23:59:59')],
+			['P1W/2014-05-25T23:59:59', strtotime('2014-05-19 00:00:00'), strtotime('2014-05-25 23:59:59')],
+
+			// Fixed date periods with known anchors
+			['2014-01-01/P1Y', strtotime('2014-01-01 00:00:00'), strtotime('2014-12-31 23:59:59')],
+			['2014-06-15/P6M', strtotime('2014-06-15 00:00:00'), strtotime('2014-12-14 23:59:59')],
+			['2014-03-01/P2W', strtotime('2014-03-01 00:00:00'), strtotime('2014-03-14 23:59:59')],
+			['2014-12-25/P10D', strtotime('2014-12-25 00:00:00'), strtotime('2015-01-03 23:59:59')],
+			['2014-01-01T12:00/PT6H', strtotime('2014-01-01 12:00:00'), strtotime('2014-01-01 17:59:59')],
+			['2014-01-01 12:00/PT6H', strtotime('2014-01-01 12:00:00'), strtotime('2014-01-01 17:59:59')],	// Space instead of T
+			['2014-01-01T12:00/PT6h', strtotime('2014-01-01 12:00:00'), strtotime('2014-01-01 17:59:59')],	// Lowercase h
+			['2014-05-01T10:30/PT90M', strtotime('2014-05-01 10:30:00'), strtotime('2014-05-01 11:59:59')],
+			['2014-07-04T14:15:30/PT45S', strtotime('2014-07-04 14:15:30'), strtotime('2014-07-04 14:16:14')],
+
+			// Reverse periods
+			['P1M/2014-02-28', strtotime('2014-01-28 00:00:01'), strtotime('2014-02-28 00:00:00')],
+			['P3D/2014-01-10T23:59:59', strtotime('2014-01-08 00:00:00'), strtotime('2014-01-10 23:59:59')],
+			['PT12H/2014-06-01T12:00', strtotime('2014-06-01 00:00:01'), strtotime('2014-06-01 12:00:00')],
+		];
+	}
+}