search: dedicated page with query form
authorMischa POSLAWSKY <perl@shiar.org>
Tue, 23 Oct 2018 00:05:29 +0000 (02:05 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Fri, 26 Oct 2018 19:02:59 +0000 (21:02 +0200)
search.html [new file with mode: 0644]
search.php

diff --git a/search.html b/search.html
new file mode 100644 (file)
index 0000000..2b24635
--- /dev/null
@@ -0,0 +1,8 @@
+<h2>Zoeken</h2>
+
+<form action="">
+       <input type="search" name="q" value="" />
+       <input type="submit" value="Zoek" />
+</form>
+
+[[search limit=100 verbose=1 /]]
index 5bd03bb3d464f882e8e6b908157fe5ab4b843672..2f093c9d8dacf517747298a11d686be5a8895873 100644 (file)
@@ -1,8 +1,11 @@
 <?php
-$limit = 10;
+$limit = @$Place['limit'] ?: 10;
 $path = ' '.escapeshellarg('*.html');
+$query = @$_REQUEST['q'] ?: $Args ?: $Page;
 
-$cmd = "git grep -li -- ".escapeshellarg($Page).$path;
+if (!trim($query, '/')) return;
+
+$cmd = "git grep -li -- ".escapeshellarg($query).$path;
 exec($cmd, $results);
 if (count($results) > $limit) {
        shuffle($results);
@@ -13,9 +16,9 @@ $cmd = "git ls-files -- $path";
 exec($cmd, $ls);
 if ($ls) {
        # order files by similarity to query
-       $ls = array_combine($ls, array_map(function ($row) use ($Page) {
+       $ls = array_combine($ls, array_map(function ($row) use ($query) {
                $row = preg_replace('{(?:^|/)index\.html$}', '', $row);
-               return similar_text($row, $Page) - strlen($row) / 8;
+               return similar_text($row, $query) - strlen($row) / 8;
        }, $ls));
        arsort($ls);
 
@@ -29,6 +32,13 @@ elseif (!$results) {
        $results = ['index.html'];
 }
 
+if (isset($Place['verbose'])) {
+       printf("<p>%s gevonden voor <q>%s</q>:</p>\n",
+               $results ? count($results).' resultaten' : 'Niets',
+               htmlspecialchars($query)
+       );
+}
+
 require_once('nieuws.inc.php');
 print '<ul>';
 foreach ($results as $result) {