Skip to content

Commit

Permalink
Half fixed search context generation for #30.
Browse files Browse the repository at this point in the history
The problem: The matches were being sorted in the wrong direction.
Next up we need to improve the context generation, as there are several contexts that are being generated without the keywork in question in them.
  • Loading branch information
sbrl committed Aug 19, 2016
1 parent 70f9c39 commit c501791
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ A Wiki in a box

Pepperminty Wiki is a complete wiki contained in a single file, inspired by @am2064's [Minty Wiki](https://github.com/am2064/Minty-Wiki). It's open source too (under MPL-2.0), so contributions are welcome!

**Latest Version:** v0.12 (stable) v0.13-dev (development)
**Latest Version:** v0.12 (stable) v0.12.1-dev (development)

## Screenshots
![Main Page Example](https://i.imgur.com/8TokPXw.png)
Expand Down
20 changes: 12 additions & 8 deletions build/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@
///////////////////////////////////////////////////////////////////////////////////////////////
/////////////// Do not edit below this line unless you know what you are doing! ///////////////
///////////////////////////////////////////////////////////////////////////////////////////////
$version = "v0.12";
$version = "v0.12.1-dev";
/// Environment ///
$env = new stdClass();
$env->action = $settings->defaultaction;
Expand Down Expand Up @@ -1025,7 +1025,7 @@ class page_renderer
<footer>
<p>{footer-message}</p>
<p>Powered by Pepperminty Wiki v0.12, which was built by <a href='//starbeamrainbowlabs.com/'>Starbeamrainbowlabs</a>. Send bugs to 'bugs at starbeamrainbowlabs dot com' or <a href='//github.com/sbrl/Pepperminty-Wiki' title='Github Issue Tracker'>open an issue</a>.</p>
<p>Powered by Pepperminty Wiki v0.12.1-dev, which was built by <a href='//starbeamrainbowlabs.com/'>Starbeamrainbowlabs</a>. Send bugs to 'bugs at starbeamrainbowlabs dot com' or <a href='//github.com/sbrl/Pepperminty-Wiki' title='Github Issue Tracker'>open an issue</a>.</p>
<p>Your local friendly administrators are {admins-name-list}.</p>
<p>This wiki is managed by <a href='mailto:{admin-details-email}'>{admin-details-name}</a>.</p>
</footer>
Expand All @@ -1037,7 +1037,7 @@ class page_renderer
<p><em>From {sitename}, which is managed by {admin-details-name}.</em></p>
<p>{footer-message}</p>
<p><em>Timed at {generation-date}</em></p>
<p><em>Powered by Pepperminty Wiki v0.12.</em></p>
<p><em>Powered by Pepperminty Wiki v0.12.1-dev.</em></p>
</footer>";

// An array of functions that have been registered to process the
Expand Down Expand Up @@ -1092,7 +1092,7 @@ public static function render($title, $content, $body_template = false)
"{body}" => $body_template,

"{sitename}" => $logo_html,
"v0.12" => $version,
"v0.12.1-dev" => $version,
"{favicon-url}" => $settings->favicon,
"{header-html}" => self::get_header_html(),

Expand Down Expand Up @@ -2210,6 +2210,7 @@ function render_recent_change($rchange)
$link = "?page=" . rawurlencode($result["pagename"]);
$pagesource = file_get_contents($env->storage_prefix . $result["pagename"] . ".md");
$context = search::extract_context($_GET["query"], $pagesource);
//echo("Generated search context for " . $result["pagename"] . ": $context\n");
$context = search::highlight_context($_GET["query"], $context);
/*if(strlen($context) == 0)
{
Expand Down Expand Up @@ -2340,7 +2341,7 @@ public static function rebuild_invindex()

self::merge_into_invindex($invindex, ids::getid($pagename), $index);
}
error_log("Saving inverted index to $paths->searchindex");

self::save_invindex($paths->searchindex, $invindex);
}

Expand Down Expand Up @@ -2557,18 +2558,18 @@ public static function extract_context($query, $source)
}
}

// Sort the matches by offset
usort($matches, function($a, $b) {
if($a[1] == $b[1]) return 0;
return ($a[1] < $b[1]) ? +1 : -1;
return ($a[1] > $b[1]) ? +1 : -1;
});

$contexts = [];
$basepos = 0;
$matches_count = count($matches);
while($basepos < $matches_count)
{
// Store the next match along - all others will be relative to that
// one
// Store the next match along - all others will be relative to that one
$group = [$matches[$basepos]];

// Start scanning at the next one along - we always store the first match
Expand All @@ -2595,6 +2596,9 @@ public static function extract_context($query, $source)
$context_start = $group[0][1] - $settings->search_characters_context;
$context_end = $group[count($group) - 1][1] + $settings->search_characters_context;

//echo("Got context. Start: $context_start, End: $context_end\n");
//echo("Group:"); var_dump($group);

$context = substr($source, $context_start, $context_end - $context_start);

// Strip the markdown from the context - it's most likely going to
Expand Down
2 changes: 1 addition & 1 deletion module_index.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,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": 1471597850,
"lastupdate": 1471604218,
"optional": false
},
{
Expand Down
12 changes: 8 additions & 4 deletions modules/feature-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
$link = "?page=" . rawurlencode($result["pagename"]);
$pagesource = file_get_contents($env->storage_prefix . $result["pagename"] . ".md");
$context = search::extract_context($_GET["query"], $pagesource);
//echo("Generated search context for " . $result["pagename"] . ": $context\n");
$context = search::highlight_context($_GET["query"], $context);
/*if(strlen($context) == 0)
{
Expand Down Expand Up @@ -267,7 +268,7 @@ public static function rebuild_invindex()

self::merge_into_invindex($invindex, ids::getid($pagename), $index);
}
error_log("Saving inverted index to $paths->searchindex");

self::save_invindex($paths->searchindex, $invindex);
}

Expand Down Expand Up @@ -484,18 +485,18 @@ public static function extract_context($query, $source)
}
}

// Sort the matches by offset
usort($matches, function($a, $b) {
if($a[1] == $b[1]) return 0;
return ($a[1] < $b[1]) ? +1 : -1;
return ($a[1] > $b[1]) ? +1 : -1;
});

$contexts = [];
$basepos = 0;
$matches_count = count($matches);
while($basepos < $matches_count)
{
// Store the next match along - all others will be relative to that
// one
// Store the next match along - all others will be relative to that one
$group = [$matches[$basepos]];

// Start scanning at the next one along - we always store the first match
Expand All @@ -522,6 +523,9 @@ public static function extract_context($query, $source)
$context_start = $group[0][1] - $settings->search_characters_context;
$context_end = $group[count($group) - 1][1] + $settings->search_characters_context;

//echo("Got context. Start: $context_start, End: $context_end\n");
//echo("Group:"); var_dump($group);

$context = substr($source, $context_start, $context_end - $context_start);

// Strip the markdown from the context - it's most likely going to
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.12
v0.12.1-dev

0 comments on commit c501791

Please sign in to comment.