Automatic installer.php lang files by installer_builder (20080314)
[moodle.git] / search / update.php
blob8282a62bcdf0e48ce130178d6ec8c589e069b467
1 <?php
3 require_once('../config.php');
4 require_once("$CFG->dirroot/search/lib.php");
6 require_login();
8 if (empty($CFG->enableglobalsearch)) {
9 error('Global searching is not enabled.');
12 if (!isadmin()) {
13 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
14 } //if
16 //check for php5 (lib.php)
17 if (!search_check_php5()) {
18 $phpversion = phpversion();
19 mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version $phpversion)");
20 exit(0);
21 } //if
23 require_once("$CFG->dirroot/search/indexlib.php");
25 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
26 $dbcontrol = new IndexDBControl();
27 $update_count = 0;
29 $indexdate = $CFG->search_indexer_run_date;
31 mtrace("<pre>Starting index update (updates)...\n");
33 if ($mods = get_records_select('modules')) {
34 $mods = array_merge($mods, search_get_additional_modules());
36 foreach ($mods as $mod) {
37 $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
38 $get_document_function = $mod->name.'_single_document';
39 $delete_function = $mod->name.'_delete';
40 $db_names_function = $mod->name.'_db_names';
41 $updates = array();
43 if (file_exists($class_file)) {
44 require_once($class_file);
46 if (function_exists($delete_function) and function_exists($db_names_function) and function_exists($get_document_function)) {
47 mtrace("Checking $mod->name module for updates.");
48 $values = $db_names_function();
50 //TODO: check 'in' syntax with other RDBMS' (add and update.php as well)
51 $sql = "select id, ".$values[0]." as docid from ".$values[1].
52 " where ".$values[3]." > $indexdate".
53 " and id in (select docid from ".SEARCH_DATABASE_TABLE.")";
55 $records = get_records_sql($sql);
57 if (is_array($records)) {
58 foreach($records as $record) {
59 $updates[] = $delete_function($record->docid);
60 } //foreach
61 } //if
63 foreach ($updates as $update) {
64 ++$update_count;
66 //delete old document
67 $doc = $index->find("+docid:$update +doctype:$mod->name");
69 //get the record, should only be one
70 foreach ($doc as $thisdoc) {
71 mtrace(" Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
73 $dbcontrol->delDocument($thisdoc);
74 $index->delete($thisdoc->id);
75 } //foreach
77 //add new modified document back into index
78 $add = $get_document_function($update);
80 //object to insert into db
81 $dbid = $dbcontrol->addDocument($add);
83 //synchronise db with index
84 $add->addField(Zend_Search_Lucene_Field::Keyword('dbid', $dbid));
86 mtrace(" Add: $add->title (database id = $add->dbid, moodle instance id = $add->docid)");
88 $index->addDocument($add);
89 } //foreach
91 mtrace("Finished $mod->name.\n");
92 } //if
93 } //if
94 } //foreach
95 } //if
97 //commit changes
98 $index->commit();
100 //update index date
101 set_config("search_indexer_run_date", time());
103 mtrace("Finished $update_count updates.</pre>");