From 4ea01bf8b4301e2f8a72ab537d12fdacd4cd423b Mon Sep 17 00:00:00 2001 From: ghost Date: Sun, 2 Apr 2023 23:36:35 +0300 Subject: [PATCH] implement search results pagination --- config/app.php.txt | 1 + library/sqlite.php | 4 ++-- public/search.php | 10 ++++++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/config/app.php.txt b/config/app.php.txt index 5a0afe1..27612e7 100644 --- a/config/app.php.txt +++ b/config/app.php.txt @@ -7,6 +7,7 @@ error_reporting(E_ALL); // Website define('WEBSITE_DOMAIN', (issue($_SERVER['HTTP_HOST']) ? 'http://' . $_SERVER['HTTP_HOST'] : '')); +define('WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT', 100); // Database define('DB_NAME', 'database.sqlite'); diff --git a/library/sqlite.php b/library/sqlite.php index 6ace4ab..8878986 100644 --- a/library/sqlite.php +++ b/library/sqlite.php @@ -150,9 +150,9 @@ class SQLite { return $query->rowCount(); } - public function searchPages(string $q) { + public function searchPages(string $q, int $start = 0, int $limit = 100) { - $query = $this->_db->prepare('SELECT `title`, `description`, `url` FROM `ftsPage` WHERE `data` MATCH ? ORDER BY `rank`'); + $query = $this->_db->prepare('SELECT `title`, `description`, `url` FROM `ftsPage` WHERE `data` MATCH ? ORDER BY `rank` LIMIT ' . (int) $start . ',' . (int) $limit); $query->execute([$q]); diff --git a/public/search.php b/public/search.php index 2dbcdee..16e25e3 100644 --- a/public/search.php +++ b/public/search.php @@ -18,6 +18,7 @@ $placeholder = Filter::plural($totalPages, [sprintf(_('Over %s page or enter the // Filter request data $q = !empty($_GET['q']) ? Filter::url($_GET['q']) : ''; +$p = !empty($_GET['p']) ? (int) $_GET['p'] : 1; // Crawl request if (filter_var($q, FILTER_VALIDATE_URL) && preg_match(CRAWL_URL_REGEXP, $q)) { @@ -28,7 +29,7 @@ if (filter_var($q, FILTER_VALIDATE_URL) && preg_match(CRAWL_URL_REGEXP, $q)) { // Search request if (!empty($q)) { - $results = $db->searchPages('"' . $q . '"'); + $results = $db->searchPages('"' . $q . '"', $p * WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT - WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT, WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT); $resultsTotal = $db->searchPagesTotal('"' . $q . '"'); } else { @@ -42,7 +43,7 @@ if (!empty($q)) { - <?php echo sprintf(_('%s - YGGo!'), htmlentities($q)) ?> + <?php echo sprintf(_('%s - #%s - YGGo!'), htmlentities($q), $p) ?> @@ -189,6 +190,11 @@ if (!empty($q)) { url ?> + +
+ +
+