Browse Source

implement search results pagination

sqliteway
ghost 2 years ago
parent
commit
4ea01bf8b4
  1. 1
      config/app.php.txt
  2. 4
      library/sqlite.php
  3. 10
      public/search.php

1
config/app.php.txt

@ -7,6 +7,7 @@ error_reporting(E_ALL);
// Website // Website
define('WEBSITE_DOMAIN', (issue($_SERVER['HTTP_HOST']) ? 'http://' . $_SERVER['HTTP_HOST'] : '')); define('WEBSITE_DOMAIN', (issue($_SERVER['HTTP_HOST']) ? 'http://' . $_SERVER['HTTP_HOST'] : ''));
define('WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT', 100);
// Database // Database
define('DB_NAME', 'database.sqlite'); define('DB_NAME', 'database.sqlite');

4
library/sqlite.php

@ -150,9 +150,9 @@ class SQLite {
return $query->rowCount(); 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]); $query->execute([$q]);

10
public/search.php

@ -18,6 +18,7 @@ $placeholder = Filter::plural($totalPages, [sprintf(_('Over %s page or enter the
// Filter request data // Filter request data
$q = !empty($_GET['q']) ? Filter::url($_GET['q']) : ''; $q = !empty($_GET['q']) ? Filter::url($_GET['q']) : '';
$p = !empty($_GET['p']) ? (int) $_GET['p'] : 1;
// Crawl request // Crawl request
if (filter_var($q, FILTER_VALIDATE_URL) && preg_match(CRAWL_URL_REGEXP, $q)) { 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 // Search request
if (!empty($q)) { 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 . '"'); $resultsTotal = $db->searchPagesTotal('"' . $q . '"');
} else { } else {
@ -42,7 +43,7 @@ if (!empty($q)) {
<!DOCTYPE html> <!DOCTYPE html>
<html lang="<?php echo _('en-US'); ?>"> <html lang="<?php echo _('en-US'); ?>">
<head> <head>
<title><?php echo sprintf(_('%s - YGGo!'), htmlentities($q)) ?></title> <title><?php echo sprintf(_('%s - #%s - YGGo!'), htmlentities($q), $p) ?></title>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="<?php echo _('Javascript-less Open Source Web Search Engine') ?>" /> <meta name="description" content="<?php echo _('Javascript-less Open Source Web Search Engine') ?>" />
@ -189,6 +190,11 @@ if (!empty($q)) {
<a href="<?php echo $result->url ?>"><?php echo $result->url ?></a> <a href="<?php echo $result->url ?>"><?php echo $result->url ?></a>
</div> </div>
<?php } ?> <?php } ?>
<?php if ($p * WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT < $resultsTotal) { ?>
<div>
<a href="<?php echo WEBSITE_DOMAIN; ?>/search.php?q=<?php echo urlencode(htmlentities($q)) ?>&p=<?php echo $p + 1 ?>"><?php echo _('Next page') ?></a>
</div>
<?php } ?>
<?php } else { ?> <?php } else { ?>
<div style="text-align:center"> <div style="text-align:center">
<span><?php echo sprintf(_('Total found: %s'), $resultsTotal) ?></span> <span><?php echo sprintf(_('Total found: %s'), $resultsTotal) ?></span>

Loading…
Cancel
Save