From 1ed6704e1945fb93457db67ed66986cf522607d9 Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Fri, 31 May 2024 15:44:44 -0500 Subject: [PATCH] Added sqlite compatible date criteria to numeric score. Fixed missing semicolon that would cause the word "day" to appear on the "Popular by Day" screen after the current date. Fixed month browsing issue when viewing "Popular by Month" on the 31st of a month, which happened to be today. --- ext/numeric_score/main.php | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/ext/numeric_score/main.php b/ext/numeric_score/main.php index 52b44226..27c294e2 100644 --- a/ext/numeric_score/main.php +++ b/ext/numeric_score/main.php @@ -199,18 +199,36 @@ class NumericScore extends Extension $totaldate = $year."/".$month."/".$day; - $sql = "SELECT id FROM images WHERE EXTRACT(YEAR FROM posted) = :year"; + if($database->get_driver_id() === DatabaseDriverID::SQLITE) { + $sql = "SELECT id FROM images WHERE strftime('%Y', posted) = cast(:year as text)"; + $month = str_pad(strval($month), 2, "0", STR_PAD_LEFT); + $day = str_pad(strval($day), 2, "0", STR_PAD_LEFT); + } else { + $sql = "SELECT id FROM images WHERE EXTRACT(YEAR FROM posted) = :year"; + } $args = ["limit" => $config->get_int(IndexConfig::IMAGES), "year" => $year]; if ($event->page_matches("popular_by_day")) { - $sql .= " AND EXTRACT(MONTH FROM posted) = :month AND EXTRACT(DAY FROM posted) = :day"; + if($database->get_driver_id() === DatabaseDriverID::SQLITE) { + $sql .= " AND strftime('%m', posted) = cast(:month as text) AND strftime('%d', posted) = cast(:day as text)"; + } else { + $sql .= " AND EXTRACT(MONTH FROM posted) = :month AND EXTRACT(DAY FROM posted) = :day"; + } $args = array_merge($args, ["month" => $month, "day" => $day]); - $current = date("F jS, Y", \Safe\strtotime($totaldate)). + $current = date("F jS, Y", \Safe\strtotime($totaldate)); $name = "day"; $fmt = "\\y\\e\\a\\r\\=Y\\&\\m\\o\\n\\t\\h\\=m\\&\\d\\a\\y\\=d"; } elseif ($event->page_matches("popular_by_month")) { - $sql .= " AND EXTRACT(MONTH FROM posted) = :month"; + if($database->get_driver_id() === DatabaseDriverID::SQLITE) { + $sql .= " AND strftime('%m', posted) = cast(:month as text)"; + } else { + $sql .= " AND EXTRACT(MONTH FROM posted) = :month"; + } $args = array_merge($args, ["month" => $month]); + // PHP's -1 month and +1 month functionality break when modifying dates that are on the 31st of the month. + // See Example #3 on https://www.php.net/manual/en/datetime.modify.php + // To get around this, set the day to 1 when doing month work. + $totaldate = $year."/".$month."/01"; $current = date("F Y", \Safe\strtotime($totaldate)); $name = "month"; $fmt = "\\y\\e\\a\\r\\=Y\\&\\m\\o\\n\\t\\h\\=m"; @@ -225,7 +243,6 @@ class NumericScore extends Extension $sql .= " AND NOT numeric_score=0 ORDER BY numeric_score DESC LIMIT :limit OFFSET 0"; //filter images by score != 0 + date > limit to max images on one page > order from highest to lowest score - $ids = $database->get_col($sql, $args); $images = Search::get_images($ids); $this->theme->view_popular($images, $totaldate, $current, $name, $fmt);