3 * Global Search Engine for Moodle
4 * Michael Champanis (mchampan) [cynnical@gmail.com]
5 * review 1.8+ : Valery Fremaux [valery.fremaux@club-internet.fr]
8 * Asynchronous adder for new indexable contents
10 * Major chages in this review is passing the xxxx_db_names return to
11 * multiple arity to handle multiple document types modules
14 require_once('../config.php');
15 require_once("$CFG->dirroot/search/lib.php");
19 if (empty($CFG->enableglobalsearch
)) {
20 error(get_string('globalsearchdisabled', 'search'));
24 error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
27 //check for php5 (lib.php)
28 if (!search_check_php5()) {
29 $phpversion = phpversion();
30 mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version $phpversion)");
34 require_once("$CFG->dirroot/search/indexlib.php");
36 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH
);
37 $dbcontrol = new IndexDBControl();
39 $startindextime = time();
41 $indexdate = $CFG->search_indexer_run_date
;
43 mtrace('<pre>Starting index update (additions)...');
44 mtrace('Index size before: '.$CFG->search_index_size
."\n");
47 if ($mods = get_records_select('modules')) {
49 //append virtual modules onto array
50 $mods = array_merge($mods, search_get_additional_modules());
51 foreach ($mods as $mod) {
52 //build include file and function names
53 $class_file = $CFG->dirroot
.'/search/documents/'.$mod->name
.'_document.php';
54 $db_names_function = $mod->name
.'_db_names';
55 $get_document_function = $mod->name
.'_single_document';
56 $get_newrecords_function = $mod->name
.'_new_records';
59 if (file_exists($class_file)) {
60 require_once($class_file);
62 //if both required functions exist
63 if (function_exists($db_names_function) and function_exists($get_document_function)) {
64 mtrace("Checking $mod->name module for additions.");
65 $valuesArray = $db_names_function();
67 foreach($valuesArray as $values){
68 $where = (isset($values[5])) ?
'AND ('.$values[5].')' : '';
69 $itemtypes = ($values[4] != '*') ?
" AND itemtype = '{$values[4]}' " : '' ;
71 //select records in MODULE table, but not in SEARCH_DATABASE_TABLE
72 $table = SEARCH_DATABASE_TABLE
;
78 {$CFG->prefix}{$table}
80 doctype = '{$mod->name}'
83 $docIds = get_records_sql_menu($query);
84 $docIdList = ($docIds) ?
implode("','", array_keys($docIds)) : '' ;
90 {$CFG->prefix}{$values[1]}
92 id NOT IN ('{$docIdList}') and
93 {$values[2]} > {$indexdate}
96 $records = get_records_sql($query);
98 // foreach record, build a module specific search document using the get_document function
99 if (is_array($records)) {
100 foreach($records as $record) {
101 $add = $get_document_function($record->docid
, $values[4]);
102 // some documents may not be indexable
109 // foreach document, add it to the index and database table
110 foreach ($additions as $add) {
113 // object to insert into db
114 $dbid = $dbcontrol->addDocument($add);
116 // synchronise db with index
117 $add->addField(Zend_Search_Lucene_Field
::Keyword('dbid', $dbid));
119 mtrace(" Add: $add->title (database id = $add->dbid, moodle instance id = $add->docid)");
121 $index->addDocument($add);
125 mtrace("No types to add.\n");
127 mtrace("Finished $mod->name.\n");
136 // update index date and size
137 set_config("search_indexer_run_date", $startindextime);
138 set_config("search_index_size", (int)$CFG->search_index_size +
(int)$addition_count);
140 // print some additional info
141 mtrace("Added $addition_count documents.");
142 mtrace('Index size after: '.$index->count().'</pre>');