From 6e7805628ea435f800594edb7d5e8cc52224060b Mon Sep 17 00:00:00 2001 From: mchampan Date: Mon, 14 Aug 2006 02:29:43 +0000 Subject: [PATCH] Advanced search page added. --- search/README.txt | 27 ++++++++ search/lib.php | 4 +- search/query.php | 155 ++++++++++++++++++++++++++++++++++++++++---- search/querylib.php | 8 +-- 4 files changed, 177 insertions(+), 17 deletions(-) diff --git a/search/README.txt b/search/README.txt index 249c8f8b5e421..0737770d36228 100644 --- a/search/README.txt +++ b/search/README.txt @@ -1,3 +1,30 @@ +2006/08/14 +---------- +First revision of the advanced search page completed. Functional, +but needs a date search field still. + +2006/08/02 +---------- +Added resource search type, and the ability to specify custom 'virtual' +models to search - allowing for non-module specific information to be +indexed. Specify the extra search types to use in lib.php. + +2006/07/28 +---------- +Added delete logic to documents; the moodle database log is checked +and any found delete events are used to remove the referenced documents +from the database table and search index. + +Added database table name constant to lib.php, must change files using +the static table name. + +Changed documents to use 'docid' instead of 'id' to reference the moodle +instance id, since Zend Search adds it's own internal 'id' field. Noticed +this whilst working on deletions. + +Added some additional fields to the permissions checking method, must still +implement it though. + 2006/07/25 ---------- Query logic moved into the SearchQuery class in querylib.php. Should be able diff --git a/search/lib.php b/search/lib.php index 27c7c26f7176a..a5641c27859cb 100644 --- a/search/lib.php +++ b/search/lib.php @@ -11,7 +11,7 @@ define('SEARCH_DATABASE_TABLE', 'search_documents'); //document types that can be searched - define('SEARCH_TYPE_NONE', 'none'); + //define('SEARCH_TYPE_NONE', 'none'); define('SEARCH_TYPE_WIKI', 'wiki'); define('SEARCH_TYPE_FORUM', 'forum'); define('SEARCH_TYPE_GLOSSARY', 'glossary'); @@ -27,6 +27,8 @@ function search_get_document_types($prefix='SEARCH_TYPE') { } //if } //foreach + sort($ret); + return $ret; } //search_get_document_types diff --git a/search/query.php b/search/query.php index d33512e8e670f..82ea337a50193 100644 --- a/search/query.php +++ b/search/query.php @@ -21,7 +21,7 @@ * All articles written by Helen Foster * * */ - + require_once('../config.php'); require_once("$CFG->dirroot/search/lib.php"); @@ -29,8 +29,61 @@ if ($check = search_check_php5()) { require_once("$CFG->dirroot/search/querylib.php"); - $query_string = optional_param('query_string', '', PARAM_CLEAN); - $page_number = optional_param('page', 1, PARAM_INT); + $advanced = (optional_param('a', '0', PARAM_INT) == '1') ? true : false; + $pages = (optional_param('p', '0', PARAM_INT) == '1') ? true : false; + $query_string = optional_param('query_string', '', PARAM_CLEAN); + + if ($pages && isset($_SESSION['search_advanced_query'])) { + $adv = unserialize($_SESSION['search_advanced_query']); + } else if ($advanced) { + unset($_SESSION['search_advanced_query']); + session_unregister('search_advanced_query'); + + $adv->mustappear = trim(optional_param('mustappear', '', PARAM_CLEAN), $chars); + $adv->notappear = trim(optional_param('notappear', '', PARAM_CLEAN), $chars); + $adv->canappear = trim(optional_param('canappear', '', PARAM_CLEAN), $chars); + $adv->module = optional_param('module', '', PARAM_CLEAN); + $adv->title = trim(optional_param('title', '', PARAM_CLEAN), $chars); + $adv->author = trim(optional_param('author', '', PARAM_CLEAN), $chars); + } //else + + if ($advanced) { + $chars = ' \t\n\r\0\x0B,;'; + $query_string = ''; + + $module_types = array_merge(array('All'), array_values(search_get_document_types())); + $adv->module = in_array($adv->module, $module_types) ? $adv->module : 'All'; + + if (strlen(trim($adv->mustappear)) > 0) { + $query_string = ' +'.implode(' +', preg_split("/[\s,;]+/", $adv->mustappear)); + } //if + + if (strlen(trim($adv->notappear)) > 0) { + $query_string .= ' -'.implode(' -', preg_split("/[\s,;]+/", $adv->notappear)); + } //if + + if (strlen(trim($adv->canappear)) > 0) { + $query_string .= ' '.implode(' ', preg_split("/[\s,;]+/", $adv->canappear)); + } //if + + if ($adv->module != 'All') { + $query_string .= ' +doctype:'.$adv->module; + } //if + + if (strlen(trim($adv->title)) > 0) { + $query_string .= ' +title:'.implode(' +title:', preg_split("/[\s,;]+/", $adv->title)); + } //if + + if (strlen(trim($adv->author)) > 0) { + $query_string .= ' +author:'.implode(' +author:', preg_split("/[\s,;]+/", $adv->author)); + } //if + + if (!empty($query_string)) { + $_SESSION['search_advanced_query'] = serialize($adv); + } //if + } //if + + $page_number = optional_param('page', 1, PARAM_INT); if ($page_number < 1) { $page_number = 1; @@ -60,19 +113,94 @@ print_heading($strquery); print_simple_box_start('center', '', '', 20); + + $vars = get_object_vars($adv); + + foreach ($vars as $key => $value) { + $adv->$key = stripslashes(htmlentities($value)); + } //foreach ?>
- -    Advanced search - Statistics + + +     + Advanced search + Statistics + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
These words must appear:
These words must not appear:
These words help improve rank:
Which modules to search?: + +
Words in title:
Author name:

+ + + + + +
Normal search  Statistics
+
+

-
'; print 'Searching: '; if ($sq->is_valid_index()) { @@ -86,10 +214,9 @@ if (!$sq->is_valid_index() and isadmin()) { print "

Admin: There appears to be no index, click here to create one."; } //if -?> -
-'; + print_simple_box_end(); if ($sq->is_valid()) { @@ -103,9 +230,13 @@ print $hit_count." results returned for '".stripslashes($query_string)."'."; print "
"; - if ($hit_count > 0) { - $page_links = $sq->page_numbers(); + if ($hit_count > 0) { + $page_links = $sq->page_numbers(); $hits = $sq->results(); + + if ($advanced) { + $page_links = preg_replace("/query_string=[^&]+/", 'a=1&p=1', $page_links); + } //if print "
    "; diff --git a/search/querylib.php b/search/querylib.php index 5f9ec9e654cb6..83705cec8fc34 100644 --- a/search/querylib.php +++ b/search/querylib.php @@ -170,7 +170,7 @@ private function get_all_results() { foreach ($hits as $hit) { //check permissions on each result - if ($this->can_display($USER, $hit->course_id, $hit->group_id)) { + if ($this->can_display($USER, $hit->id, $hit->doctype, $hit->course_id, $hit->group_id)) { $resultdoc->number = $i; $resultdoc->url = $hit->url; $resultdoc->title = $hit->title; @@ -212,7 +212,7 @@ private function get_results() { public function page_numbers() { $pages = $this->total_pages(); - $query = $this->term; + $query = htmlentities($this->term); $page = $this->pagenumber; $next = "Next"; $back = "Back"; @@ -254,12 +254,12 @@ public function page_numbers() { $end = $pages - 3; $ret = preg_replace("/$start<\/a>.*?$end<\/a>/", '...', $ret); } //if - + return $ret; } //page_numbers //can the user see this result? - private function can_display($user, $course_id, $group_id) { + private function can_display(&$user, $this_id, $doctype, $course_id, $group_id) { //this function should return true/false depending on //whether or not a user can see this resource //..