public function __construct($path = SEARCH_INDEX_PATH) { global $CFG, $db; $this->path = $path; //test to see if there is a valid index on disk, at the specified path try { $test_index = new Zend_Search_Lucene($this->path, false); $validindex = true; } catch (Exception $e) { $validindex = false; } //retrieve file system info about the index if it is valid if ($validindex) { $this->size = display_size(get_directory_size($this->path)); $index_dir = get_directory_list($this->path, '', false, false); $this->filecount = count($index_dir); $this->indexcount = $test_index->count(); } else { $this->size = 0; $this->filecount = 0; $this->indexcount = 0; } $db_exists = false; //for now //get all the current tables in moodle $admin_tables = $db->MetaTables(); //TODO: use new IndexDBControl class for database checks? //check if our search table exists if (in_array($CFG->prefix . SEARCH_DATABASE_TABLE, $admin_tables)) { //retrieve database information if it does $db_exists = true; //total documents $this->dbcount = count_records(SEARCH_DATABASE_TABLE); //individual document types // $types = search_get_document_types(); $types = search_collect_searchables(true, false); sort($types); foreach ($types as $type) { $c = count_records(SEARCH_DATABASE_TABLE, 'doctype', $type); $this->types[$type] = (int) $c; } } else { $this->dbcount = 0; $this->types = array(); } //check if the busy flag is set if (isset($CFG->search_indexer_busy) && $CFG->search_indexer_busy == '1') { $this->complete = false; } else { $this->complete = true; } //get the last run date for the indexer if ($this->valid() && $CFG->search_indexer_run_date) { $this->time = $CFG->search_indexer_run_date; } else { $this->time = 0; } }
print_error('beadmin', 'search', get_login_url()); } */ try { $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH); } catch (LuceneException $e) { mtrace("Could not construct a valid index. Maybe the first indexation was never made, or files might be corrupted. Run complete indexation again."); return; } $dbcontrol = new IndexDBControl(); $deletion_count = 0; $startcleantime = time(); mtrace('Starting clean-up of removed records...'); mtrace('Index size before: ' . $CFG->search_index_size . "\n"); /// check all modules if ($mods = search_collect_searchables(false, true)) { foreach ($mods as $mod) { //build function names $class_file = $CFG->dirroot . '/search/documents/' . $mod->name . '_document.php'; $delete_function = $mod->name . '_delete'; $db_names_function = $mod->name . '_db_names'; $deletions = array(); if (file_exists($class_file)) { require_once $class_file; //if both required functions exist if (function_exists($delete_function) and function_exists($db_names_function)) { mtrace("Checking {$mod->name} module for deletions."); $valuesArray = $db_names_function(); if ($valuesArray) { foreach ($valuesArray as $values) { $where = !empty($values[5]) ? 'WHERE ' . $values[5] : '';
print $hit_count . ' ' . get_string('resultsreturnedfor', 'search') . " '" . s($query_string) . "'."; print "<br />"; if ($hit_count > 0) { $page_links = $sq->page_numbers(); $hits = $sq->results(); if ($advanced) { // if in advanced mode, search options are saved in the session, so // we can remove the query string var from the page links, and replace // it with a=1 (Advanced = on) instead $page_links = preg_replace("/query_string=[^&]+/", 'a=1', $page_links); } print "<ol>"; $typestr = get_string('type', 'search'); $scorestr = get_string('score', 'search'); $authorstr = get_string('author', 'search'); $searchables = search_collect_searchables(false, false); foreach ($hits as $listing) { if ($listing->doctype == 'user') { // A special handle for users $icon = print_user_picture($listing->userid, 0, true, 0, true, false); } else { $iconpath = $CFG->modpixpath . '/' . $listing->doctype . '/icon.gif'; $icon = "<img align=\"top\" src=\"" . $iconpath . "\" class=\"activityicon\" alt=\"\"/>"; } $coursename = get_field('course', 'fullname', 'id', $listing->courseid); $courseword = mb_convert_case(get_string('course', 'moodle'), MB_CASE_LOWER, 'UTF-8'); $course = $listing->doctype != 'user' ? '<strong> (' . $courseword . ': \'' . $coursename . '\')</strong>' : ''; $title_post_processing_function = $listing->doctype . '_link_post_processing'; $searchable_instance = $searchables[$listing->doctype]; if ($searchable_instance->location == 'internal') { require_once "{$CFG->dirroot}/search/documents/{$listing->doctype}_document.php";
/** * do the effective collection of results * @param boolean $all * @uses USER */ private function process_results($all = false) { global $USER; // unneeded since changing the default Zend Lexer // $term = mb_convert_case($this->term, MB_CASE_LOWER, 'UTF-8'); $term = $this->term; $page = optional_param('page', 1, PARAM_INT); //experimental - return more results // $strip_arr = array('author:', 'title:', '+', '-', 'doctype:'); // $stripped_term = str_replace($strip_arr, '', $term); // $search_string = $term." title:".$stripped_term." author:".$stripped_term; $search_string = $term; $hits = $this->index->find($search_string); //-- $hitcount = count($hits); $this->total_results = $hitcount; if ($hitcount == 0) { return array(); } $resultdoc = new SearchResult(); $resultdocs = array(); $searchables = search_collect_searchables(false, false); $realindex = 0; /** if (!$all) { if ($finalresults < $this->results_per_page) { $this->pagenumber = 1; } elseif ($this->pagenumber > $totalpages) { $this->pagenumber = $totalpages; } $start = ($this->pagenumber - 1) * $this->results_per_page; $end = $start + $this->results_per_page; if ($end > $finalresults) { $end = $finalresults; } } else { $start = 0; $end = $finalresults; } */ for ($i = 0; $i < min($hitcount, $page * $this->results_per_page); $i++) { $hit = $hits[$i]; //check permissions on each result if ($this->can_display($USER, $hit->docid, $hit->doctype, $hit->course_id, $hit->group_id, $hit->path, $hit->itemtype, $hit->context_id, $searchables)) { if ($i >= ($page - 1) * $this->results_per_page) { $resultdoc->number = $realindex; $resultdoc->url = $hit->url; $resultdoc->title = $hit->title; $resultdoc->score = $hit->score; $resultdoc->doctype = $hit->doctype; $resultdoc->author = $hit->author; $resultdoc->courseid = $hit->course_id; //and store it $resultdocs[] = clone $resultdoc; } $realindex++; } else { // lowers total_results one unit $this->total_results--; } } $totalpages = ceil($this->total_results / $this->results_per_page); return $resultdocs; }
} else { mtrace("Using {$index_path} as data directory."); } Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive()); $index = new Zend_Search_Lucene($index_path, true); /// New regeneration mtrace('Deleting old index entries.'); $DB->delete_records(SEARCH_DATABASE_TABLE); /// begin timer search_stopwatch(); mtrace("Starting activity modules\n"); //the presence of the required search functions - // * mod_iterator // * mod_get_content_for_index //are the sole basis for including a module in the index at the moment. $searchables = search_collect_searchables(); /// start indexation if ($searchables) { foreach ($searchables as $mod) { //mark last update times for mods to now. $indexdatestring = 'search_indexer_update_date_' . $mod->name; set_config($indexdatestring, time()); $indexdatestring = 'search_indexer_run_date_' . $mod->name; set_config($indexdatestring, time()); mtrace("starting indexing {$mod->name}\n"); $key = 'search_in_' . $mod->name; if (isset($CFG->{$key}) && !$CFG->{$key}) { mtrace("module {$key} has been administratively disabled. Skipping...\n"); continue; } if ($mod->location == 'internal') {
if (preg_match("/\\b{$type}\\b/i", $defaultfiletypes)) { continue; } //header $propname = 'block_search_' . $type . '_to_text'; $settings->add(new admin_setting_heading($propname, get_string('handlingfor', 'block_search') . ' ' . $utype, '')); //word_to_text_cmd $propname = 'block_search_' . $type . '_to_text_cmd'; $settings->add(new admin_setting_configtext($propname, get_string('configtypetotxtcmd', 'block_search'), get_string('cmdtoconverttotextfor', 'block_search', $type), '', PARAM_PATH, 60)); //word_to_text_env $propname = 'block_search_' . $type . '_to_text_env'; $settings->add(new admin_setting_configtext($propname, get_string('configtypetotxtenv', 'block_search'), get_string('envforcmdtotextfor', 'block_search', $type), '', PARAM_PATH, 60)); } } require_once $CFG->dirroot . '/search/lib.php'; $searchnames = search_collect_searchables(true, false); list($searchable_list, $params) = $DB->get_in_or_equal($searchnames); //header $propname = 'block_search_' . $type . '_to_text'; $settings->add(new admin_setting_heading($propname, get_string('searchdiscovery', 'block_search'), '')); $found_searchable_modules = 0; if ($modules = $DB->get_records_select('modules', "name {$searchable_list}", $params, 'name', 'id,name')) { foreach ($modules as $module) { $keyname = 'search_in_' . $module->name; $settings->add(new admin_setting_configcheckbox($keyname, get_string('modulename', $module->name), get_string('enableindexinginmodule', 'block_search', $module->name), 1, 1, 0)); $found_searchable_modules = 1; } } if (!$found_searchable_modules) { //header $propname = 'block_search_nosearchablemodules';