From d22b78c8b597b55c8f2e9859f7d626efcef322ce Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michael=20Gro=C3=9Fe?= <grosse@cosmocode.de>
Date: Mon, 26 Mar 2018 11:38:33 +0200
Subject: [PATCH] feat(search): use dedicated url-parameter for search query

This way we do not loose the context of the current page. Further, the
new id generated from the query before wasn't really that useful (see
issue #1124 ). And we can still generate a meaningful link "create a
page for your query", if that is considered useful.

Further redirects exist for the old urls, so bookmarked searches etc.
should mostly keep working.
---
 doku.php              |  2 +-
 inc/Action/Search.php | 15 ++++++++++++---
 inc/Ui/Search.php     | 10 +++++-----
 inc/template.php      |  4 ++--
 4 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/doku.php b/doku.php
index d02a43200..eb7b7d472 100644
--- a/doku.php
+++ b/doku.php
@@ -35,7 +35,7 @@ require_once(DOKU_INC.'inc/init.php');
 
 //import variables
 $INPUT->set('id', str_replace("\xC2\xAD", '', $INPUT->str('id'))); //soft-hyphen
-$QUERY          = trim($INPUT->str('id'));
+$QUERY          = trim($INPUT->str('q'));
 $ID             = getID();
 
 $REV   = $INPUT->int('rev');
diff --git a/inc/Action/Search.php b/inc/Action/Search.php
index f848d8752..4cc3017fa 100644
--- a/inc/Action/Search.php
+++ b/inc/Action/Search.php
@@ -25,13 +25,22 @@ class Search extends AbstractAction {
      */
     public function checkPermissions() {
         parent::checkPermissions();
-        global $QUERY;
-        $s = cleanID($QUERY);
-        if($s === '') throw new ActionAbort();
     }
 
     public function preProcess()
     {
+        global $QUERY, $ID, $conf, $INPUT;
+        $s = cleanID($QUERY);
+
+        if ($ID !== $conf['start'] && $s === '') {
+            parse_str($INPUT->server->str('QUERY_STRING'), $urlParts);
+            $urlParts['q'] = $urlParts['id'];
+            $urlParts['id'] = $conf['start'];
+            $url = DOKU_URL . DOKU_SCRIPT . '?' . http_build_query($urlParts, null, '&');
+            send_redirect($url);
+        }
+
+        if ($s === '') throw new ActionAbort();
         $this->adjustGlobalQuery();
     }
 
diff --git a/inc/Ui/Search.php b/inc/Ui/Search.php
index 2d7ae6b3b..b568a632f 100644
--- a/inc/Ui/Search.php
+++ b/inc/Ui/Search.php
@@ -91,7 +91,7 @@ class Search extends Ui
 
         $searchForm = (new Form())->attrs(['method' => 'get'])->addClass('search-results-form');
         $searchForm->setHiddenField('do', 'search');
-        $searchForm->setHiddenField('from', $ID);
+        $searchForm->setHiddenField('id', $ID);
         $searchForm->setHiddenField('searchPageForm', '1');
         if ($INPUT->has('after')) {
             $searchForm->setHiddenField('after', $INPUT->str('after'));
@@ -100,7 +100,7 @@ class Search extends Ui
             $searchForm->setHiddenField('before', $INPUT->str('before'));
         }
         $searchForm->addFieldsetOpen()->addClass('search-results-form__fieldset');
-        $searchForm->addTextInput('id')->val($query)->useInput(false);
+        $searchForm->addTextInput('q')->val($query)->useInput(false);
         $searchForm->addButton('', $lang['btn_search'])->attr('type', 'submit');
 
         if ($this->isSearchAssistanceAvailable($this->parsedQuery)) {
@@ -222,7 +222,7 @@ class Search extends Ui
         $after = null,
         $before = null
     ) {
-        global $INPUT;
+        global $INPUT, $ID;
         if (null === $and) {
             $and = $this->parsedQuery['and'];
         }
@@ -252,7 +252,7 @@ class Search extends Ui
             $ns,
             $notns
         );
-        $hrefAttributes = ['do' => 'search', 'searchPageForm' => '1'];
+        $hrefAttributes = ['do' => 'search', 'searchPageForm' => '1', 'q' => $newQuery];
         if ($after) {
             $hrefAttributes['after'] = $after;
         }
@@ -261,7 +261,7 @@ class Search extends Ui
         }
         $searchForm->addTagOpen('a')
             ->attrs([
-                'href' => wl($newQuery, $hrefAttributes, false, '&')
+                'href' => wl($ID, $hrefAttributes, false, '&')
             ])
         ;
         $searchForm->addHTML($label);
diff --git a/inc/template.php b/inc/template.php
index 20b0831a9..9a5f611b5 100644
--- a/inc/template.php
+++ b/inc/template.php
@@ -670,8 +670,8 @@ function tpl_searchform($ajax = true, $autocomplete = true) {
     ]);
     $searchForm->addTagOpen('div')->addClass('no');
     $searchForm->setHiddenField('do', 'search');
-    $searchForm->setHiddenField('from', $ID);
-    $searchForm->addTextInput('id')
+    $searchForm->setHiddenField('id', $ID);
+    $searchForm->addTextInput('q')
         ->addClass('edit')
         ->attrs([
             'title' => '[F]',
-- 
GitLab