searpl/index.php

88 lines
2.2 KiB
PHP
Raw Normal View History

2020-12-14 21:59:16 +00:00
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
?>
<!DOCTYPE HTML>
<html lang="en">
<link rel="stylesheet" type="text/css" href="assets/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="a search engine">
<title>searpl</title>
<div class='wrapper'>
<h1>searpl</h1>
<div class='box search-container'>
<form action="./">
<input type="text" placeholder="Search.." name="q" value="<?php if (isset($_GET['q'])) {echo htmlspecialchars($_GET['q']); } ?>">
<button type="submit"><i class="icon-search"></i></button>
2020-12-14 21:59:16 +00:00
</form>
</div>
<?php
2020-12-16 16:36:46 +00:00
$db = new PDO("sqlite:db.sqlite");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
2020-12-14 21:59:16 +00:00
if (isset($_GET['q']) && preg_replace('/\s+/', '', $_GET['q']) != '') {
$sql = "SELECT title,url,snippet(indexed,2,'<b>','</b>','...',15) as snippet FROM indexed WHERE indexed MATCH ? ORDER BY bm25(indexed,2,2,1)";
$params = [$_GET['q']];
2020-12-14 21:59:16 +00:00
$stmt = $db->prepare($sql);
2020-12-14 22:50:06 +00:00
set_error_handler(function ($_,$msg) {echo '<div class="box">'.substr($msg,65).'. you may want to view <a href="https://www.sqlite.org/fts5.html#full_text_query_syntax">this documentation</a> on writing valid queries.</div>';}, E_WARNING);
$stmt->execute($params);
restore_error_handler();
2020-12-14 22:50:06 +00:00
$results = false;
while ($row = $stmt->fetch()) {
2020-12-14 21:59:16 +00:00
$results = true;
?>
<div class='box'>
<a href="<?php echo $row['url']; ?>"><?php echo $row['title']; ?></a>
2020-12-14 21:59:16 +00:00
<br>
<small><?php echo $row['url']; ?></small>
2020-12-14 22:50:06 +00:00
<br>
<?php
echo $row['snippet'];
2020-12-14 21:59:16 +00:00
?>
</div>
<?php
}
if (!$results)
echo '<div class="box">No results.</div>';
2020-12-16 16:36:46 +00:00
} else {
?>
<div class='box'>
<h2>welcome to searpl</h2>
i am a simple, <a href='https://github.com/xfnw/searpl'>open source</a> search
2020-12-16 16:36:46 +00:00
engine that can find stuff :3
</div>
<div class='box'>
queries use <a href='https://www.sqlite.org/fts5.html#full_text_query_syntax'>FTS syntax</a>.
2020-12-16 16:36:46 +00:00
</div>
<div class='box'>
i have
<strong>
<?php
echo $db->query('SELECT rowid FROM indexed ORDER BY rowid DESC LIMIT 1')->fetchColumn();
2020-12-16 16:36:46 +00:00
?>
</strong> pages indexed, using <strong>
<?php
echo round(filesize('db.sqlite')/1024/1024);
?>
</strong>mb of storage
</div>
<?php
2020-12-14 21:59:16 +00:00
}
?>
</div>