From e79e522d78c118c8f1d6d5a1188b44f4558bccb4 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sun, 1 Nov 2015 10:13:35 +0000
Subject: [PATCH] Integrate search into top bar

---
 build/index.php            | 28 ++++++++++++++++++++++++++--
 core.php                   | 16 +++++++++++++++-
 module_index.json          |  2 +-
 modules/feature-search.php | 12 +++++++++++-
 4 files changed, 53 insertions(+), 5 deletions(-)

diff --git a/build/index.php b/build/index.php
index 0f1df79..ae5b4e1 100644
--- a/build/index.php
+++ b/build/index.php
@@ -963,7 +963,7 @@ class page_renderer
 						break;
 					
 					case "search": // Displays a search bar
-						$result .= "<span class='inflexible'><form method='get' action='index.php' style='display: inline;'><input type='search' name='page' list='allpages' placeholder='Type a page name here and hit enter' /></form></span>";
+						$result .= "<span class='inflexible'><form method='get' action='index.php' style='display: inline;'><input type='search' name='page' list='allpages' placeholder='Type a page name here and hit enter' /><input type='hidden' name='search-redirect' value='true' /></form></span>";
 						break;
 					
 					case "divider": // Displays a divider
@@ -1018,6 +1018,20 @@ class page_renderer
 	}
 }
 
+//////////////////////////////////////
+///// Extra consistency measures /////
+//////////////////////////////////////
+if(!isset($pageindex->{$env->page}) and isset($_GET["search-redirect"]))
+{
+	http_response_code(307);
+	$url = "?action=search&query=" . rawurlencode($env->page);
+	header("location: $url");
+	exit(pagerenderer::render("Non existent page - $settings->sitename", "<p>There isn't a page on $settings->sitename with that name. However, you could <a href='$url'>search for this page name</a> in other pages.</p>
+		<p>Alternatively, you could <a href='?action=edit&page=" . rawurlencode($env->page) . "&create=true'>create this page</a>.</p>"));
+}
+//////////////////////////////////////
+//////////////////////////////////////
+
 //////////////////////////
 ///  Module functions  ///
 //////////////////////////
@@ -1358,7 +1372,7 @@ register_module([
 		});
 		
 		add_action("search", function() {
-			global $settings;
+			global $settings, $env, $pageindex;
 			
 			if(!isset($_GET["query"]))
 				exit(page_renderer::render("No Search Terms - Error - $settings->sitename", "<p>You didn't specify any search terms. Try typing some into the box above.</p>"));
@@ -1381,6 +1395,16 @@ register_module([
 			$content .= "	<input type='hidden' name='action' value='search' />\n";
 			$content .= "</form>";
 			
+			$query = $_GET["query"];
+			if(isset($pageindex->$query))
+			{
+				$content .= "<p>There's a page on $settings->sitename called <a href='?page=" . rawurlencode($query) . "'>$query</a>.</p>";
+			}
+			else
+			{
+				$content .= "<p>There isn't a page called $query on $settings->sitename, but you can <a href='?action=edit&page=" . rawurlencode($query) . "'>create it</a>.</p>";
+			}
+			
 			$i = 0; // todo use $_GET["offset"] and $_GET["result-count"] or something
 			foreach($results as $result)
 			{
diff --git a/core.php b/core.php
index 23015e9..15a163f 100644
--- a/core.php
+++ b/core.php
@@ -638,7 +638,7 @@ class page_renderer
 						break;
 					
 					case "search": // Displays a search bar
-						$result .= "<span class='inflexible'><form method='get' action='index.php' style='display: inline;'><input type='search' name='page' list='allpages' placeholder='Type a page name here and hit enter' /></form></span>";
+						$result .= "<span class='inflexible'><form method='get' action='index.php' style='display: inline;'><input type='search' name='page' list='allpages' placeholder='Type a page name here and hit enter' /><input type='hidden' name='search-redirect' value='true' /></form></span>";
 						break;
 					
 					case "divider": // Displays a divider
@@ -693,6 +693,20 @@ class page_renderer
 	}
 }
 
+//////////////////////////////////////
+///// Extra consistency measures /////
+//////////////////////////////////////
+if(!isset($pageindex->{$env->page}) and isset($_GET["search-redirect"]))
+{
+	http_response_code(307);
+	$url = "?action=search&query=" . rawurlencode($env->page);
+	header("location: $url");
+	exit(pagerenderer::render("Non existent page - $settings->sitename", "<p>There isn't a page on $settings->sitename with that name. However, you could <a href='$url'>search for this page name</a> in other pages.</p>
+		<p>Alternatively, you could <a href='?action=edit&page=" . rawurlencode($env->page) . "&create=true'>create this page</a>.</p>"));
+}
+//////////////////////////////////////
+//////////////////////////////////////
+
 //////////////////////////
 ///  Module functions  ///
 //////////////////////////
diff --git a/module_index.json b/module_index.json
index 651c4f6..29cff1b 100644
--- a/module_index.json
+++ b/module_index.json
@@ -50,7 +50,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds proper search functionality to Pepperminty Wiki. Note that this module, at the moment, just contains test code while I figure out how best to write a search engine.",
         "id": "feature-search",
-        "lastupdate": 1446300948,
+        "lastupdate": 1446372585,
         "optional": false
     },
     {
diff --git a/modules/feature-search.php b/modules/feature-search.php
index 46ebb4f..ea1fcd6 100644
--- a/modules/feature-search.php
+++ b/modules/feature-search.php
@@ -28,7 +28,7 @@ register_module([
 		});
 		
 		add_action("search", function() {
-			global $settings;
+			global $settings, $env, $pageindex;
 			
 			if(!isset($_GET["query"]))
 				exit(page_renderer::render("No Search Terms - Error - $settings->sitename", "<p>You didn't specify any search terms. Try typing some into the box above.</p>"));
@@ -51,6 +51,16 @@ register_module([
 			$content .= "	<input type='hidden' name='action' value='search' />\n";
 			$content .= "</form>";
 			
+			$query = $_GET["query"];
+			if(isset($pageindex->$query))
+			{
+				$content .= "<p>There's a page on $settings->sitename called <a href='?page=" . rawurlencode($query) . "'>$query</a>.</p>";
+			}
+			else
+			{
+				$content .= "<p>There isn't a page called $query on $settings->sitename, but you can <a href='?action=edit&page=" . rawurlencode($query) . "'>create it</a>.</p>";
+			}
+			
 			$i = 0; // todo use $_GET["offset"] and $_GET["result-count"] or something
 			foreach($results as $result)
 			{
