From db7bf0f7ec486634bb4f87e5597ba9ad783eb5ec Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Tue, 27 Oct 2015 21:10:05 +0000
Subject: [PATCH] begin playing around with indexing

---
 build/index.php            | 68 ++++++++++++++++++++++++++++++++++++++
 module_index.json          |  9 +++++
 modules/feature-search.php | 67 +++++++++++++++++++++++++++++++++++++
 3 files changed, 144 insertions(+)
 create mode 100644 modules/feature-search.php

diff --git a/build/index.php b/build/index.php
index 82fde55..a0ec337 100644
--- a/build/index.php
+++ b/build/index.php
@@ -1249,6 +1249,74 @@ register_module([
 
 
 
+register_module([
+	"name" => "Search",
+	"version" => "0.1",
+	"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",
+	"code" => function() {
+		add_action("index", function() {
+			global $settings, $env;
+			
+			$breakable_chars = "\r\n\t .,\\/!\"£$%^&*[]()+`_~#";
+			
+			header("content-type: text/plain");
+			
+			$source = file_get_contents("$env->page.md");
+			$source_length = strlen($source);
+			
+			$index = [];
+			
+			var_dump($env->page);
+			var_dump($source);
+			echo("source length: $source_length\n");
+			
+			$basepos = 0;
+			$scanpos = $basepos;
+			while($basepos < $source_length)
+			{
+				$word = "";
+				do {
+					// Break if we reach the end of the source text
+					if($scanpos >= $source_length) break;
+					$word .= $source[$scanpos];
+					$scanpos++;
+				} while(strpos($breakable_chars, $source[$scanpos]) === false);
+				
+				// Move the base position up to the scan position (plus one to
+				// skip over the breakable character), saving the old base
+				// position for later
+				$word_start_pos = $basepos;
+				$basepos = $scanpos + 1;
+				// Continue if the word is empty
+				if(strlen($word) === 0) continue;
+				// Normalise the word to be lowercase
+				$word = strtolower($word);
+				
+				var_dump($word);
+				
+				// Initialise the entry in the index if it doesn't exist
+				if(!isset($index[$word]))
+				{
+					$index[$word] = [
+						"freq" => 0,
+						"offsets" => []
+					];
+				}
+				// Update the index entry
+				$index[$word]["freq"]++;
+				$index[$word]["offsets"][] = $word_start_pos;
+			}
+			
+			var_dump($index);
+		});
+	}
+]);
+
+
+
+
 register_module([
 	"name" => "Uploader",
 	"version" => "0.1",
diff --git a/module_index.json b/module_index.json
index ca09ff6..1862e6d 100644
--- a/module_index.json
+++ b/module_index.json
@@ -44,6 +44,15 @@
         "lastupdate": 1445170746,
         "optional": false
     },
+    {
+        "name": "Search",
+        "version": "0.1",
+        "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": 1445980152,
+        "optional": false
+    },
     {
         "name": "Uploader",
         "version": "0.1",
diff --git a/modules/feature-search.php b/modules/feature-search.php
new file mode 100644
index 0000000..1e80fc9
--- /dev/null
+++ b/modules/feature-search.php
@@ -0,0 +1,67 @@
+<?php
+register_module([
+	"name" => "Search",
+	"version" => "0.1",
+	"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",
+	"code" => function() {
+		add_action("index", function() {
+			global $settings, $env;
+			
+			$breakable_chars = "\r\n\t .,\\/!\"£$%^&*[]()+`_~#";
+			
+			header("content-type: text/plain");
+			
+			$source = file_get_contents("$env->page.md");
+			$source_length = strlen($source);
+			
+			$index = [];
+			
+			var_dump($env->page);
+			var_dump($source);
+			echo("source length: $source_length\n");
+			
+			$basepos = 0;
+			$scanpos = $basepos;
+			while($basepos < $source_length)
+			{
+				$word = "";
+				do {
+					// Break if we reach the end of the source text
+					if($scanpos >= $source_length) break;
+					$word .= $source[$scanpos];
+					$scanpos++;
+				} while(strpos($breakable_chars, $source[$scanpos]) === false);
+				
+				// Move the base position up to the scan position (plus one to
+				// skip over the breakable character), saving the old base
+				// position for later
+				$word_start_pos = $basepos;
+				$basepos = $scanpos + 1;
+				// Continue if the word is empty
+				if(strlen($word) === 0) continue;
+				// Normalise the word to be lowercase
+				$word = strtolower($word);
+				
+				var_dump($word);
+				
+				// Initialise the entry in the index if it doesn't exist
+				if(!isset($index[$word]))
+				{
+					$index[$word] = [
+						"freq" => 0,
+						"offsets" => []
+					];
+				}
+				// Update the index entry
+				$index[$word]["freq"]++;
+				$index[$word]["offsets"][] = $word_start_pos;
+			}
+			
+			var_dump($index);
+		});
+	}
+]);
+
+?>
