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
12 * Index asynchronous updator
14 * Major chages in this review is passing the xxxx_db_names return to
15 * multiple arity to handle multiple document types modules
19 * includes and requires
21 require_once('../config.php');
22 require_once("$CFG->dirroot/search/lib.php");
24 /// checks global search activation
28 if (empty($CFG->enableglobalsearch
)) {
29 error(get_string('globalsearchdisabled', 'search'));
33 error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
36 /// check for php5 (lib.php)
38 if (!search_check_php5()) {
39 $phpversion = phpversion();
40 mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version ".phpversion().")");
44 require_once("$CFG->dirroot/search/indexlib.php");
46 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH
);
47 $dbcontrol = new IndexDBControl();
49 $indexdate = $CFG->search_indexer_update_date
;
50 $startupdatedate = time();
52 /// indexing changed resources
54 mtrace("Starting index update (updates)...\n");
56 if ($mods = get_records_select('modules')) {
57 $mods = array_merge($mods, search_get_additional_modules());
59 foreach ($mods as $mod) {
60 $class_file = $CFG->dirroot
.'/search/documents/'.$mod->name
.'_document.php';
61 $get_document_function = $mod->name
.'_single_document';
62 $delete_function = $mod->name
.'_delete';
63 $db_names_function = $mod->name
.'_db_names';
66 if (file_exists($class_file)) {
67 require_once($class_file);
69 //if both required functions exist
70 if (function_exists($delete_function) and function_exists($db_names_function) and function_exists($get_document_function)) {
71 mtrace("Checking $mod->name module for updates.");
72 $valuesArray = $db_names_function();
74 foreach($valuesArray as $values){
76 $where = (isset($values[5])) ?
'AND ('.$values[5].')' : '';
77 $itemtypes = ($values[4] != '*' && $values[4] != 'any') ?
" AND itemtype = '{$values[4]}' " : '' ;
79 //TODO: check 'in' syntax with other RDBMS' (add and update.php as well)
80 $table = SEARCH_DATABASE_TABLE
;
86 {$CFG->prefix}{$table}
88 doctype = '{$mod->name}'
91 $docIds = get_records_sql_menu($query);
92 $docIdList = ($docIds) ?
implode("','", array_keys($docIds)) : '' ;
99 {$CFG->prefix}{$values[1]}
101 {$values[3]} > {$indexdate} AND
102 id IN ('{$docIdList}')
105 $records = get_records_sql($query);
106 if (is_array($records)) {
107 foreach($records as $record) {
108 $updates[] = $delete_function($record->docid
, $docIds[$record->docid
]);
113 foreach ($updates as $update) {
116 //delete old document
117 $doc = $index->find("+docid:{$update->id} +doctype:{$mod->name} +itemtype:{$update->itemtype}");
119 //get the record, should only be one
120 foreach ($doc as $thisdoc) {
121 mtrace(" Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
122 $dbcontrol->delDocument($thisdoc);
123 $index->delete($thisdoc->id
);
126 //add new modified document back into index
127 $add = $get_document_function($update->id
, $update->itemtype
);
129 //object to insert into db
130 $dbid = $dbcontrol->addDocument($add);
132 //synchronise db with index
133 $add->addField(Zend_Search_Lucene_Field
::Keyword('dbid', $dbid));
134 mtrace(" Add: $add->title (database id = $add->dbid, moodle instance id = $add->docid)");
135 $index->addDocument($add);
139 mtrace("No types to update.\n");
141 mtrace("Finished $mod->name.\n");
151 set_config("search_indexer_update_date", $startupdatedate);
153 mtrace("Finished $update_count updates");