1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 13:12:41 +00:00

Allow timezone in TT-RSS search queries

Does not quite work yet
This commit is contained in:
J. King 2022-04-25 22:28:16 -04:00
parent e65069885b
commit 17832ac63e
3 changed files with 17 additions and 16 deletions

View file

@ -1520,7 +1520,8 @@ class API extends \JKingWeb\Arsse\REST\AbstractHandler {
} }
// handle the search string, if any // handle the search string, if any
if (isset($data['search'])) { if (isset($data['search'])) {
$c = Search::parse($data['search'], $c); $tz = Arsse::$db->userPropertiesGet(Arsse::$user->id, false)['tz'] ?? "UTC";
$c = Search::parse($data['search'], $tz, $c);
if (!$c) { if (!$c) {
// the search string inherently returns an empty result, either directly or interacting with other input // the search string inherently returns an empty result, either directly or interacting with other input
return new ResultEmpty; return new ResultEmpty;

View file

@ -32,7 +32,7 @@ class Search {
"" => "searchTerms", "" => "searchTerms",
]; ];
public static function parse(string $search, Context $context = null): ?Context { public static function parse(string $search, string $tz, Context $context = null): ?Context {
// normalize the input // normalize the input
$search = strtolower(trim(preg_replace("<\s+>", " ", $search))); $search = strtolower(trim(preg_replace("<\s+>", " ", $search)));
// set initial state // set initial state
@ -88,7 +88,7 @@ class Search {
continue 3; continue 3;
case '"': case '"':
if (($pos + 1 == $stop) || $search[$pos + 1] === " ") { if (($pos + 1 == $stop) || $search[$pos + 1] === " ") {
$context = self::processToken($context, $buffer, $tag, $flag_negative, false); $context = self::processToken($context, $buffer, $tag, $flag_negative);
$state = self::STATE_BEFORE_TOKEN; $state = self::STATE_BEFORE_TOKEN;
$flag_negative = false; $flag_negative = false;
$buffer = $tag = ""; $buffer = $tag = "";
@ -135,7 +135,7 @@ class Search {
while ($pos < $stop && $search[$pos] !== " ") { while ($pos < $stop && $search[$pos] !== " ") {
$buffer .= $search[$pos++]; $buffer .= $search[$pos++];
} }
$context = self::processToken($context, $buffer, $tag, $flag_negative, true); $context = self::processToken($context, $buffer, $tag, $flag_negative, $tz);
$state = self::STATE_BEFORE_TOKEN; $state = self::STATE_BEFORE_TOKEN;
$flag_negative = false; $flag_negative = false;
$buffer = $tag = ""; $buffer = $tag = "";
@ -145,7 +145,7 @@ class Search {
case "": case "":
case '"': case '"':
if (($pos + 1 >= $stop) || $search[$pos + 1] === " ") { if (($pos + 1 >= $stop) || $search[$pos + 1] === " ") {
$context = self::processToken($context, $buffer, $tag, $flag_negative, true); $context = self::processToken($context, $buffer, $tag, $flag_negative, $tz);
$state = self::STATE_BEFORE_TOKEN; $state = self::STATE_BEFORE_TOKEN;
$flag_negative = false; $flag_negative = false;
$buffer = $tag = ""; $buffer = $tag = "";
@ -178,7 +178,7 @@ class Search {
if (!strlen($tag)) { if (!strlen($tag)) {
$buffer = ":".$buffer; $buffer = ":".$buffer;
} }
$context = self::processToken($context, $buffer, $tag, $flag_negative, false); $context = self::processToken($context, $buffer, $tag, $flag_negative);
$state = self::STATE_BEFORE_TOKEN; $state = self::STATE_BEFORE_TOKEN;
$flag_negative = false; $flag_negative = false;
$buffer = $tag = ""; $buffer = $tag = "";
@ -191,7 +191,7 @@ class Search {
if (!strlen($tag)) { if (!strlen($tag)) {
$buffer = ":".$buffer; $buffer = ":".$buffer;
} }
$context = self::processToken($context, $buffer, $tag, $flag_negative, false); $context = self::processToken($context, $buffer, $tag, $flag_negative);
$state = self::STATE_BEFORE_TOKEN; $state = self::STATE_BEFORE_TOKEN;
$flag_negative = false; $flag_negative = false;
$buffer = $tag = ""; $buffer = $tag = "";
@ -221,7 +221,7 @@ class Search {
switch ($char) { switch ($char) {
case "": case "":
case " ": case " ":
$context = self::processToken($context, $buffer, $tag, $flag_negative, false); $context = self::processToken($context, $buffer, $tag, $flag_negative);
$state = self::STATE_BEFORE_TOKEN; $state = self::STATE_BEFORE_TOKEN;
$flag_negative = false; $flag_negative = false;
$buffer = $tag = ""; $buffer = $tag = "";
@ -241,7 +241,7 @@ class Search {
case "": case "":
case '"': case '"':
if (($pos + 1 >= $stop) || $search[$pos + 1] === " ") { if (($pos + 1 >= $stop) || $search[$pos + 1] === " ") {
$context = self::processToken($context, $buffer, $tag, $flag_negative, false); $context = self::processToken($context, $buffer, $tag, $flag_negative);
$state = self::STATE_BEFORE_TOKEN; $state = self::STATE_BEFORE_TOKEN;
$flag_negative = false; $flag_negative = false;
$buffer = $tag = ""; $buffer = $tag = "";
@ -282,7 +282,7 @@ class Search {
return $context; return $context;
} }
protected static function processToken(Context $c, string $value, string $tag, bool $neg, bool $date): Context { protected static function processToken(Context $c, string $value, string $tag, bool $neg, string $tz = null): Context {
if (!strlen($value) && !strlen($tag)) { if (!strlen($value) && !strlen($tag)) {
return $c; return $c;
} elseif (!strlen($value)) { } elseif (!strlen($value)) {
@ -290,8 +290,8 @@ class Search {
$value = "$tag:"; $value = "$tag:";
$tag = ""; $tag = "";
} }
if ($date) { if ($tz !== null) {
return self::setDate($value, $c, $neg); return self::setDate($value, $c, $neg, $tz);
} elseif (isset(self::FIELDS_BOOLEAN[$tag])) { } elseif (isset(self::FIELDS_BOOLEAN[$tag])) {
return self::setBoolean($tag, $value, $c, $neg); return self::setBoolean($tag, $value, $c, $neg);
} else { } else {
@ -309,15 +309,15 @@ class Search {
return $c->$type(array_merge($c->$type ?? [], [$value])); return $c->$type(array_merge($c->$type ?? [], [$value]));
} }
protected static function setDate(string $value, Context $c, bool $neg): Context { protected static function setDate(string $value, Context $c, bool $neg, string $tz): Context {
$spec = Date::normalize($value); $spec = Date::normalize($value);
// TTRSS treats invalid dates as the start of the Unix epoch; we ignore them instead // TTRSS treats invalid dates as the start of the Unix epoch; we ignore them instead
if (!$spec) { if (!$spec) {
return $c; return $c;
} }
$day = $spec->format("Y-m-d"); $day = $spec->format("Y-m-d");
$start = $day."T00:00:00+00:00"; $start = $day."T00:00:00 $tz";
$end = $day."T23:59:59+00:00"; $end = $day."T23:59:59 $tz";
// if a date is already set, the same date is a no-op; anything else is a contradiction // if a date is already set, the same date is a no-op; anything else is a contradiction
$cc = $neg ? $c->not : $c; $cc = $neg ? $c->not : $c;
if ($cc->modifiedRange()) { if ($cc->modifiedRange()) {

View file

@ -119,7 +119,7 @@ class TestSearch extends \JKingWeb\Arsse\Test\AbstractTest {
/** @dataProvider provideSearchStrings */ /** @dataProvider provideSearchStrings */
public function testApplySearchToContext(string $search, $exp): void { public function testApplySearchToContext(string $search, $exp): void {
$act = Search::parse($search); $act = Search::parse($search, "UTC");
$this->assertEquals($exp, $act); $this->assertEquals($exp, $act);
} }
} }