Lucene Zend Implementation update (better handle of UTF8)
[moodle.git] / search / update.php
CommitLineData
6f1b1da1 1<?php
2
3 require_once('../config.php');
4 require_once("$CFG->dirroot/search/lib.php");
eef868d1 5
6f1b1da1 6 require_login();
7
721d74b0 8 if (empty($CFG->enableglobalsearch)) {
9 error('Global searching is not enabled.');
10 }
11
6f1b1da1 12 if (!isadmin()) {
13 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
14 } //if
eef868d1 15
6f1b1da1 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);
eef868d1 21 } //if
22
23 require_once("$CFG->dirroot/search/indexlib.php");
24
6f1b1da1 25 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
26 $dbcontrol = new IndexDBControl();
27 $update_count = 0;
eef868d1 28
6f1b1da1 29 $indexdate = $CFG->search_indexer_run_date;
30
eef868d1 31 mtrace("<pre>Starting index update (updates)...\n");
32
6f1b1da1 33 if ($mods = get_records_select('modules')) {
791a4cec 34 $mods = array_merge($mods, search_get_additional_modules());
eef868d1 35
6f1b1da1 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';
eef868d1 41 $updates = array();
42
6f1b1da1 43 if (file_exists($class_file)) {
44 require_once($class_file);
eef868d1 45
6f1b1da1 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();
eef868d1 49
791a4cec 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.")";
eef868d1 54
55 $records = get_records_sql($sql);
56
57 if (is_array($records)) {
6f1b1da1 58 foreach($records as $record) {
59 $updates[] = $delete_function($record->docid);
60 } //foreach
eef868d1 61 } //if
62
6f1b1da1 63 foreach ($updates as $update) {
64 ++$update_count;
eef868d1 65
66 //delete old document
67 $doc = $index->find("+docid:$update +doctype:$mod->name");
68
6f1b1da1 69 //get the record, should only be one
eef868d1 70 foreach ($doc as $thisdoc) {
6f1b1da1 71 mtrace(" Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
eef868d1 72
6f1b1da1 73 $dbcontrol->delDocument($thisdoc);
eef868d1 74 $index->delete($thisdoc->id);
6f1b1da1 75 } //foreach
eef868d1 76
6f1b1da1 77 //add new modified document back into index
78 $add = $get_document_function($update);
eef868d1 79
6f1b1da1 80 //object to insert into db
eef868d1 81 $dbid = $dbcontrol->addDocument($add);
82
6f1b1da1 83 //synchronise db with index
eef868d1 84 $add->addField(Zend_Search_Lucene_Field::Keyword('dbid', $dbid));
85
6f1b1da1 86 mtrace(" Add: $add->title (database id = $add->dbid, moodle instance id = $add->docid)");
eef868d1 87
88 $index->addDocument($add);
6f1b1da1 89 } //foreach
eef868d1 90
6f1b1da1 91 mtrace("Finished $mod->name.\n");
eef868d1 92 } //if
6f1b1da1 93 } //if
94 } //foreach
95 } //if
eef868d1 96
6f1b1da1 97 //commit changes
98 $index->commit();
eef868d1 99
6f1b1da1 100 //update index date
101 set_config("search_indexer_run_date", time());
102
eef868d1 103 mtrace("Finished $update_count updates.</pre>");
6f1b1da1 104
105?>