From 24775724d14520b8007cf7f4cc0c1d8901980494 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sat, 29 Sep 2018 13:27:17 +0100
Subject: [PATCH] Bugfix: Correctly calculate the end offset of search context
 snippets

---
 Changelog.md               | 1 +
 build/index.php            | 9 ++++-----
 module_index.json          | 2 +-
 modules/feature-search.php | 3 +--
 4 files changed, 7 insertions(+), 8 deletions(-)

diff --git a/Changelog.md b/Changelog.md
index c307dfb..9fcf894 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -32,6 +32,7 @@ This file holds the changelog for Pepperminty Wiki. This is the master list of t
  - The search redirector will now check both the specified page name and the page name in Title Case
  - Improve help text description of image captions displayed alongside images
  - Fixed the page history page - it should now display all page revisions in valid HTML
+ - Fixed another bug in the search context generator
 
 ## Changed
  - Password hashing has been overhauled! A totally new-and-different system is being used now, so you'll need to rehash all your passwords.
diff --git a/build/index.php b/build/index.php
index 9a04b3a..7ccbf8d 100644
--- a/build/index.php
+++ b/build/index.php
@@ -407,7 +407,7 @@ if($settings->sessionprefix == "auto")
 /////////////////////////////////////////////////////////////////////////////
 /** The version of Pepperminty Wiki currently running. */
 $version = "v0.17-dev";
-$commit = "39c8f48019599cc00e919d5c699e0c01c7278ef4";
+$commit = "284b4049460887b0b40b2f73e7db9158e5bc1ef0";
 /// Environment ///
 /** Holds information about the current request environment. */
 $env = new stdClass();
@@ -1478,7 +1478,7 @@ class page_renderer
 		{all-pages-datalist}";
 	/**
 	 * A specially minified content template that doesn't include the navbar and
-	 * other elements not suiltable for printing.
+	 * other elements not suitable for printing.
 	 * @var string
 	 * @package core
 	 */
@@ -4550,7 +4550,7 @@ class search
 		}
 		foreach($newindex as $nterm => $entry)
 		{
-			if(!isset($oldindex[$nterm]) or // If this world is new
+			if(!isset($oldindex[$nterm]) or // If this word is new
 			   $newindex[$nterm] !== $oldindex[$nterm]) // If this word has changed
 				$changed[$nterm] = $newindex[$nterm];
 		}
@@ -4783,7 +4783,6 @@ class search
 			// todo remove items if the rank is below a threshold
 		}
 		
-		// todo sort by rank here
 		uasort($matching_pages, function($a, $b) {
 			if($a["rank"] == $b["rank"]) return 0;
 			return ($a["rank"] < $b["rank"]) ? +1 : -1;
@@ -4836,7 +4835,7 @@ class search
 		for($i = 0; $i < $matches_count; $i++) {
 			$next_context = [
 				"from" => max(0, $matches[$i][1] - $settings->search_characters_context),
-				"to" => min($sourceLength, $matches[$i][1] + count($matches[0]) + $settings->search_characters_context)
+				"to" => min($sourceLength, $matches[$i][1] + mb_strlen($matches[$i][0]) + $settings->search_characters_context)
 			];
 			
 			if(end($contexts) !== false && end($contexts)["to"] > $next_context["from"]) {
diff --git a/module_index.json b/module_index.json
index 3739a1e..227989d 100755
--- a/module_index.json
+++ b/module_index.json
@@ -104,7 +104,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds proper search functionality to Pepperminty Wiki using an inverted index to provide a full text search engine. If pages don't show up, then you might have hit a stop word. If not, try requesting the `invindex-rebuild` action to rebuild the inverted index from scratch.",
         "id": "feature-search",
-        "lastupdate": 1530443634,
+        "lastupdate": 1538223927,
         "optional": false
     },
     {
diff --git a/modules/feature-search.php b/modules/feature-search.php
index 944c1d9..6d4aea5 100644
--- a/modules/feature-search.php
+++ b/modules/feature-search.php
@@ -904,7 +904,6 @@ class search
 			// todo remove items if the rank is below a threshold
 		}
 		
-		// todo sort by rank here
 		uasort($matching_pages, function($a, $b) {
 			if($a["rank"] == $b["rank"]) return 0;
 			return ($a["rank"] < $b["rank"]) ? +1 : -1;
@@ -957,7 +956,7 @@ class search
 		for($i = 0; $i < $matches_count; $i++) {
 			$next_context = [
 				"from" => max(0, $matches[$i][1] - $settings->search_characters_context),
-				"to" => min($sourceLength, $matches[$i][1] + count($matches[0]) + $settings->search_characters_context)
+				"to" => min($sourceLength, $matches[$i][1] + mb_strlen($matches[$i][0]) + $settings->search_characters_context)
 			];
 			
 			if(end($contexts) !== false && end($contexts)["to"] > $next_context["from"]) {
