Should be $COURSE not $course
[moodle-linuxchix.git] / search / update.php
blobb7ebe4abc5be199ff0727987f0765a0cb10a1244
1 <?php
2 /**
3 * Global Search Engine for Moodle
4 * Michael Champanis (mchampan) [cynnical@gmail.com]
5 * review 1.8+ : Valery Fremaux [valery.fremaux@club-internet.fr]
6 * 2007/08/02
8 * Index asynchronous updator
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");
17 require_login();
19 if (empty($CFG->enableglobalsearch)) {
20 error(get_string('globalsearchdisabled', 'search'));
23 if (!isadmin()) {
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)");
31 exit(0);
34 require_once("$CFG->dirroot/search/indexlib.php");
36 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
37 $dbcontrol = new IndexDBControl();
38 $update_count = 0;
39 $indexdate = $CFG->search_indexer_update_date;
40 $startupdatedate = time();
42 mtrace("<pre>Starting index update (updates)...\n");
44 if ($mods = get_records_select('modules')) {
45 $mods = array_merge($mods, search_get_additional_modules());
47 foreach ($mods as $mod) {
48 $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
49 $get_document_function = $mod->name.'_single_document';
50 $delete_function = $mod->name.'_delete';
51 $db_names_function = $mod->name.'_db_names';
52 $updates = array();
54 if (file_exists($class_file)) {
55 require_once($class_file);
57 //if both required functions exist
58 if (function_exists($delete_function) and function_exists($db_names_function) and function_exists($get_document_function)) {
59 mtrace("Checking $mod->name module for updates.");
60 $valuesArray = $db_names_function();
61 if ($valuesArray){
62 foreach($valuesArray as $values){
64 $where = (isset($values[5])) ? 'AND ('.$values[5].')' : '';
65 $itemtypes = ($values[4] != '*') ? " AND itemtype = '{$values[4]}' " : '' ;
67 //TODO: check 'in' syntax with other RDBMS' (add and update.php as well)
68 $table = SEARCH_DATABASE_TABLE;
69 $query = "
70 SELECT
71 docid,
72 itemtype
73 FROM
74 {$CFG->prefix}{$table}
75 WHERE
76 doctype = '{$mod->name}'
77 $itemtypes
79 $docIds = get_records_sql_menu($query);
80 $docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
82 $query = "
83 SELECT
84 id,
85 {$values[0]} as docid
86 FROM
87 {$CFG->prefix}{$values[1]}
88 WHERE
89 {$values[3]} > {$indexdate} AND
90 id IN ('{$docIdList}')
91 $where
93 $records = get_records_sql($query);
94 if (is_array($records)) {
95 foreach($records as $record) {
96 $updates[] = $delete_function($record->docid, $docIds[$record->docid]);
101 foreach ($updates as $update) {
102 ++$update_count;
104 //delete old document
105 $doc = $index->find("+docid:{$update->id} +doctype:{$mod->name} +itemtype:{$update->itemtype}");
107 //get the record, should only be one
108 foreach ($doc as $thisdoc) {
109 mtrace(" Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
110 $dbcontrol->delDocument($thisdoc);
111 $index->delete($thisdoc->id);
114 //add new modified document back into index
115 $add = $get_document_function($update->id, $update->itemtype);
117 //object to insert into db
118 $dbid = $dbcontrol->addDocument($add);
120 //synchronise db with index
121 $add->addField(Zend_Search_Lucene_Field::Keyword('dbid', $dbid));
122 mtrace(" Add: $add->title (database id = $add->dbid, moodle instance id = $add->docid)");
123 $index->addDocument($add);
126 else{
127 mtrace("No types to update.\n");
129 mtrace("Finished $mod->name.\n");
135 //commit changes
136 $index->commit();
138 //update index date
139 set_config("search_indexer_update_date", $startupdatedate);
141 mtrace("Finished $update_count updates.</pre>");