3 * Global Search Engine for Moodle
7 * @subpackage search_engine
8 * @author Michael Champanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
10 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
14 * includes and requires
16 require_once("{$CFG->dirroot}/search/Zend/Search/Lucene.php");
18 define('DEFAULT_POPUP_SETTINGS', "\"menubar=0,location=0,scrollbars,resizable,width=600,height=450\"");
21 * a class that represents a single result record of the search engine
35 * split this into Cache class and extend to SearchCache?
41 // foresees other caching locations
42 public function __construct($mode = 'session') {
43 $accepted_modes = array('session');
45 if (in_array($mode, $accepted_modes)) {
48 $this->mode
= 'session';
55 * returns the search cache status
58 public function can_cache() {
66 public function cache($id = false, $object = false) {
67 //see if there was a previous query
68 $last_term = $this->fetch('search_last_term');
70 //if this query is different from the last, clear out the last one
71 if ($id != false && $last_term != $id) {
72 $this->clear($last_term);
75 //store the new query if id and object are passed in
77 $this->store('search_last_term', $id);
78 $this->store($id, $object);
80 //otherwise return the stored results
81 } else if ($id && $this->exists($id)) {
82 return $this->fetch($id);
87 * do key exist in cache ?
88 * @param id the object key
91 private function exists($id) {
92 switch ($this->mode
) {
94 return isset($_SESSION[$id]);
99 * clears a cached object in cache
100 * @param the object key to clear
103 private function clear($id) {
104 switch ($this->mode
) {
106 unset($_SESSION[$id]);
107 session_unregister($id);
113 * fetches a cached object
114 * @param id the object identifier
115 * @return the object cached
117 private function fetch($id) {
118 switch ($this->mode
) {
120 return ($this->exists($id)) ?
unserialize($_SESSION[$id]) : false;
125 * put an object in cache
126 * @param id the key for that object
127 * @param object the object to cache as a serialized value
130 private function store($id, $object) {
131 switch ($this->mode
) {
133 $_SESSION[$id] = serialize($object);
140 * Represents a single query with results
155 * constructor records query parameters
158 public function __construct($term = '', $page = 1, $results_per_page = 10, $cache = false) {
162 $this->pagenumber
= $page;
163 $this->cache
= $cache;
164 $this->validquery
= true;
165 $this->validindex
= true;
166 $this->results_per_page
= $results_per_page;
168 $index_path = SEARCH_INDEX_PATH
;
171 $this->index
= new Zend_Search_Lucene($index_path, false);
172 } catch(Exception
$e) {
173 $this->validindex
= false;
177 if (empty($this->term
)) {
178 $this->validquery
= false;
180 $this->set_query($this->term
);
185 * determines state of query object depending on query entry and
186 * tries to lauch search if all is OK
187 * @return void (this is only a state changing trigger).
189 public function set_query($term = '') {
194 if (empty($this->term
)) {
195 $this->validquery
= false;
197 $this->validquery
= true;
200 if ($this->validquery
and $this->validindex
) {
201 $this->results
= $this->get_results();
203 $this->results
= array();
208 * accessor to the result table.
209 * @return an array of result records
211 public function results() {
212 return $this->results
;
216 * do the effective collection of results
217 * @param boolean $all
220 private function process_results($all=false) {
223 // $term = mb_convert_case($this->term, MB_CASE_LOWER, 'UTF-8');
225 $page = optional_param('page', 1, PARAM_INT
);
227 //experimental - return more results
228 // $strip_arr = array('author:', 'title:', '+', '-', 'doctype:');
229 // $stripped_term = str_replace($strip_arr, '', $term);
231 // $search_string = $term." title:".$stripped_term." author:".$stripped_term;
232 $search_string = $term;
233 $hits = $this->index
->find($search_string);
236 $hitcount = count($hits);
237 $this->total_results
= $hitcount;
239 if ($hitcount == 0) return array();
241 $resultdoc = new SearchResult();
242 $resultdocs = array();
243 $searchables = search_collect_searchables(false, false);
249 if ($finalresults < $this->results_per_page) {
250 $this->pagenumber = 1;
251 } elseif ($this->pagenumber > $totalpages) {
252 $this->pagenumber = $totalpages;
255 $start = ($this->pagenumber - 1) * $this->results_per_page;
256 $end = $start + $this->results_per_page;
258 if ($end > $finalresults) {
259 $end = $finalresults;
263 $end = $finalresults;
266 for ($i = 0; $i < min($hitcount, ($page) * $this->results_per_page
); $i++
) {
269 //check permissions on each result
270 if ($this->can_display($USER, $hit->docid
, $hit->doctype
, $hit->course_id
, $hit->group_id
, $hit->path
, $hit->itemtype
, $hit->context_id
, $searchables )) {
271 if ($i >= ($page - 1) * $this->results_per_page
){
272 $resultdoc->number
= $realindex;
273 $resultdoc->url
= $hit->url
;
274 $resultdoc->title
= $hit->title
;
275 $resultdoc->score
= $hit->score
;
276 $resultdoc->doctype
= $hit->doctype
;
277 $resultdoc->author
= $hit->author
;
278 $resultdoc->courseid
= $hit->course_id
;
279 $resultdoc->userid
= $hit->user_id
;
282 $resultdocs[] = clone($resultdoc);
286 // lowers total_results one unit
287 $this->total_results
--;
291 $totalpages = ceil($this->total_results
/$this->results_per_page
);
298 * get results of a search query using a caching strategy if available
299 * @return the result documents as an array of search objects
301 private function get_results() {
302 $cache = new SearchCache();
304 if ($this->cache
&& $cache->can_cache()) {
305 if (!($resultdocs = $cache->cache($this->term
))) {
306 $resultdocs = $this->process_results();
307 //cache the results so we don't have to compute this on every page-load
308 $cache->cache($this->term
, $resultdocs);
309 //print "Using new results.";
311 //There was something in the cache, so we're using that to save time
312 //print "Using cached results.";
316 // print "Caching disabled!";
317 $resultdocs = $this->process_results();
323 * constructs the results paging links on results.
324 * @return string the results paging links
326 public function page_numbers() {
327 $pages = $this->total_pages();
328 // $query = htmlentities($this->term);
329 // http://moodle.org/mod/forum/discuss.php?d=115788
330 $query = htmlentities($this->term
,ENT_NOQUOTES
,'utf-8');
331 $page = $this->pagenumber
;
332 $next = get_string('next', 'search');
333 $back = get_string('back', 'search');
335 $ret = "<div align='center' id='search_page_links'>";
337 //Back is disabled if we're on page 1
339 $ret .= "<a href='query.php?query_string={$query}&page=".($page-1)."'>< {$back}</a> ";
341 $ret .= "< {$back} ";
344 //don't <a href> the current page
345 for ($i = 1; $i <= $pages; $i++
) {
347 $ret .= "($i) ";
349 $ret .= "<a href='query.php?query_string={$query}&page={$i}'>{$i}</a> ";
353 //Next disabled if we're on the last page
354 if ($page < $pages) {
355 $ret .= "<a href='query.php?query_string={$query}&page=".($page+
1)."'>{$next} ></a> ";
357 $ret .= "{$next} > ";
362 //shorten really long page lists, to stop table distorting width-ways
363 if (strlen($ret) > 70) {
366 $ret = preg_replace("/<a\D+\d+\D+>$start<\/a>.*?<a\D+\d+\D+>$end<\/a>/", '...', $ret);
370 $ret = preg_replace("/<a\D+\d+\D+>$start<\/a>.*?<a\D+\d+\D+>$end<\/a>/", '...', $ret);
377 * can the user see this result ?
378 * @param user a reference upon the user to be checked for access
379 * @param this_id the item identifier
380 * @param doctype the search document type. MAtches the module or block or
381 * extra search source definition
382 * @param course_id the course reference of the searched result
383 * @param group_id the group identity attached to the found resource
384 * @param path the path that routes to the local lib.php of the searched
385 * surrounding object fot that document
386 * @param item_type a subclassing information for complex module data models
388 * // TODO reorder parameters more consistently
390 private function can_display(&$user, $this_id, $doctype, $course_id, $group_id, $path, $item_type, $context_id, &$searchables) {
394 * course related checks
396 // admins can see everything, anyway.
397 if (has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM
))){
401 // first check course compatibility against user : enrolled users to that course can see.
402 $myCourses = get_my_courses($user->id
);
403 $unenroled = !in_array($course_id, array_keys($myCourses));
405 // if guests are allowed, logged guest can see
406 $isallowedguest = (isguest()) ?
get_field('course', 'guest', 'id', $course_id) : false ;
408 if ($unenroled && !$isallowedguest){
412 // if user is enrolled or is allowed user and course is hidden, can he see it ?
413 $visibility = get_field('course', 'visible', 'id', $course_id);
414 if ($visibility <= 0){
415 if (!has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE
, $course_id))){
421 * prerecorded capabilities
423 // get context caching information and tries to discard unwanted records here
429 // then give back indexing data to the module for local check
430 $searchable_instance = $searchables[$doctype];
431 if ($searchable_instance->location
== 'internal'){
432 include_once "{$CFG->dirroot}/search/documents/{$doctype}_document.php";
434 include_once "{$CFG->dirroot}/{$searchable_instance->location}/$doctype/search_document.php";
436 $access_check_function = "{$doctype}_check_text_access";
438 if (function_exists($access_check_function)){
439 $modulecheck = $access_check_function($path, $item_type, $this_id, $user, $group_id, $context_id);
440 // echo "module said $modulecheck for item $doctype/$item_type/$this_id";
441 return($modulecheck);
450 public function count() {
451 return $this->total_results
;
457 public function is_valid() {
458 return ($this->validquery
and $this->validindex
);
464 public function is_valid_query() {
465 return $this->validquery
;
471 public function is_valid_index() {
472 return $this->validindex
;
478 public function total_pages() {
479 return ceil($this->count()/$this->results_per_page
);
485 public function get_pagenumber() {
486 return $this->pagenumber
;
492 public function get_results_per_page() {
493 return $this->results_per_page
;
499 public function __destruct(){