Skip to content

Commit

Permalink
Fixed pagination in query page (lines were too long, distorting table),
Browse files Browse the repository at this point in the history
and added results caching to improve query time when browsing pages of the
same query. Wiki document strips (x.x.x.x) ip field from author field now.
  • Loading branch information
mchampan committed Jul 14, 2006
1 parent 44c55d4 commit 91ad955
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 45 deletions.
4 changes: 3 additions & 1 deletion search/documents/wiki_document.php
Expand Up @@ -21,7 +21,9 @@ public function __construct(&$page, $wiki_id, $course_id, $group_id) {
// generic information; required
$doc->id = $page->id;
$doc->title = $page->pagename;
$doc->author = $page->author;

//remove '(ip.ip.ip.ip)' from wiki author field
$doc->author = preg_replace('/\(.*?\)/', '', $page->author);
$doc->contents = $page->content;
$doc->url = wiki_make_link($wiki_id, $page->pagename, $page->version);

Expand Down
182 changes: 138 additions & 44 deletions search/query.php
Expand Up @@ -23,7 +23,7 @@
* */

require_once('../config.php');
require_once("$CFG->dirroot/search/lib.php");
require_once("$CFG->dirroot/search/lib.php");

//check for php5, but don't die yet (see line 27)
if ($check = search_check_php5()) {
Expand All @@ -46,7 +46,91 @@
//print $e;
$no_index = true;
} //catch
} //if
} //if


//Result document class that contains all the display information we need
class ResultDocument {
public $url,
$title,
$score,
$doctype,
$author;
} //ResultDocument

//generates an HTML string of links to result pages
function page_numbers($query, $hits, $page=1, $results_per_page=20) {
//total result pages
$pages = ceil($hits/$results_per_page);

$ret = "<div align='center'>";

//Back is disabled if we're on page 1
if ($page > 1) {
$ret .= "<a href='query.php?query_string=$query&page=".($page-1)."'>< Back</a>&nbsp;";
} else {
$ret .= "< Back&nbsp;";
} //else

//don't <a href> the current page
for ($i = 1; $i <= $pages; $i++) {
if ($page == $i) {
$ret .= "[$i]&nbsp;";
} else {
$ret .= "<a href='query.php?query_string=$query&page=$i'>$i</a>&nbsp;";
} //else
} //for

//Next disabled if we're on the last page
if ($page < $pages) {
$ret .= "<a href='query.php?query_string=$query&page=".($page+1)."'>Next ></a>&nbsp;";
} else {
$ret .= "Next >&nbsp;";
} //else

$ret .= "</div>";

//shorten really long page lists, to stop table distorting width-ways
if (strlen($ret) > 70) {
$start = 4;
$end = $page - 5;
$ret = preg_replace("/<a\D+\d+\D+>$start<\/a>.*?<a\D+\d+\D+>$end<\/a>/", '...', $ret);

$start = $page + 5;
$end = $pages - 3;
$ret = preg_replace("/<a\D+\d+\D+>$start<\/a>.*?<a\D+\d+\D+>$end<\/a>/", '...', $ret);
} //if

return $ret;
} //page_numbers

//calculates whether a user is allowed to see this result
function can_display(&$user, $course_id, $group_id) {
return true;
} //can_display

//caches the results of the last query, deletes the previous one also
function cache($id=false, &$object=false) {
//see if there was a previous query
$last_term = (isset($_SESSION['search_last_term'])) ? $_SESSION['search_last_term'] : false;

//if this query is different from the last, clear out the last one
if ($id != false and $last_term != $id) {
unset($_SESSION[$last_term]);
session_unregister($last_term);
} //if

//store the new query if id and object are passed in
if ($object and $id) {
$_SESSION['search_last_term'] = $id;
$_SESSION[$id] = $object;
return true;
//otherwise return the stored results
} else if ($id and isset($_SESSION[$id])) {
return $_SESSION[$id];
} //else
} //cache


if (!$site = get_site()) {
redirect("index.php");
Expand Down Expand Up @@ -82,19 +166,19 @@

<div align="center">
<?php
echo 'Searching: ';

if ($no_index) {
print "0";
} else {
print $index->count();
} //else

print ' documents.';

if ($no_index and isadmin()) {
print "<br><br>Admin: There appears to be no index, click <a href='indexersplash.php'>here</a> to create one.";
} //if
echo 'Searching: ';
if ($no_index) {
print "0";
} else {
print $index->count();
} //else
print ' documents.';
if ($no_index and isadmin()) {
print "<br><br>Admin: There appears to be no index, click <a href='indexersplash.php'>here</a> to create one.";
} //if
?>
</div>

Expand All @@ -105,15 +189,46 @@
print_simple_box_start('center', '50%', 'white', 10);

search_stopwatch();
$hits = $index->find(strtolower($query_string));
$hit_count = count($hits);

//if the cache is empty
if (!($hits = cache($query_string))) {
$resultdocs = array();
$resultdoc = new ResultDocument;

//generate a new result-set
$hits = $index->find(strtolower($query_string));

foreach ($hits as $hit) {
//check permissions on each result
if (can_display($USER, $hit->course_id, $hit->group_id)) {
$resultdoc->url = $hit->url;
$resultdoc->title = $hit->title;
$resultdoc->score = $hit->score;
$resultdoc->doctype = $hit->doctype;
$resultdoc->author = $hit->author;

//and store it if it passes the test
$resultdocs[] = clone($resultdoc);
} //if
} //foreach

//cache the results so we don't have to compute this on every page-load
cache($query_string, $resultdocs);

//print "Using new results.";
} else {
//There was something in the cache, so we're using that to save time
//print "Using cached results.";
} //else

$hit_count = count($hits);

print "<br>";

print $hit_count." results returned for '".stripslashes($query_string)."'.";
print "<br>";

if ($hit_count > 0) {
if ($hit_count > 0) {
if ($hit_count < $results_per_page) {
$page_number = 1;
} else if ($page_number > ceil($hit_count/$results_per_page)) {
Expand All @@ -122,6 +237,8 @@

$start = ($page_number - 1)*$results_per_page;
$end = $start + $results_per_page;

$page_links = page_numbers($query_string, $hit_count, $page_number, $results_per_page);

print "<ol>";

Expand All @@ -131,39 +248,16 @@
} //if

$listing = $hits[$i];

print "<li value='".($i+1)."'><a href='".$listing->url."'>$listing->title</a><br>\n"
."<em>".search_shorten_url($listing->url, 70)."</em><br>\n"
."Type: ".$listing->doctype.", score: ".round($listing->score, 3).", author: ".$listing->author."<br>\n"
."<br></li>\n";
} //for

print "</ol>";
} //if

print "<div align='center'>";

if ($page_number > 1) {
print "<a href='query.php?query_string=$query_string&page=".($page_number-1)."'>< Back</a>&nbsp;";
} else {
print "< Back&nbsp;";
} //else

for ($i = 1; $i <= ceil($hit_count/$results_per_page); $i++) {
if ($page_number == $i) {
print "[$i]&nbsp;";
} else {
print "<a href='query.php?query_string=$query_string&page=$i'>$i</a>&nbsp;";
} //else
} //for

if ($page_number < ceil($hit_count/$results_per_page)) {
print "<a href='query.php?query_string=$query_string&page=".($page_number+1)."'>Next ></a>&nbsp;";
} else {
print "Next >&nbsp;";
} //else

print "</div>";
print $page_links;
} //if

print_simple_box_end();
} //if
Expand Down

0 comments on commit 91ad955

Please sign in to comment.