MDL-12234, fixing unicode issues with global search
[moodle.git] / search / update.php
CommitLineData
6f1b1da1 1<?php
2f338ab5 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
7*
8* Index asynchronous updator
9*
10* Major chages in this review is passing the xxxx_db_names return to
11* multiple arity to handle multiple document types modules
12*/
13
14require_once('../config.php');
15require_once("$CFG->dirroot/search/lib.php");
16
17require_login();
18
19if (empty($CFG->enableglobalsearch)) {
20 error(get_string('globalsearchdisabled', 'search'));
21}
22
23if (!isadmin()) {
24 error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
25}
26
27//check for php5 (lib.php)
28if (!search_check_php5()) {
6f1b1da1 29 $phpversion = phpversion();
30 mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version $phpversion)");
31 exit(0);
2f338ab5 32}
33
34require_once("$CFG->dirroot/search/indexlib.php");
35
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();
41
001f3652 42mtrace("Starting index update (updates)...\n");
2f338ab5 43
44if ($mods = get_records_select('modules')) {
45 $mods = array_merge($mods, search_get_additional_modules());
46
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();
53
54 if (file_exists($class_file)) {
55 require_once($class_file);
56
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){
63
64 $where = (isset($values[5])) ? 'AND ('.$values[5].')' : '';
65 $itemtypes = ($values[4] != '*') ? " AND itemtype = '{$values[4]}' " : '' ;
66
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
78 ";
79 $docIds = get_records_sql_menu($query);
80 $docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
81
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
92 ";
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]);
97 }
98 }
99 }
100
101 foreach ($updates as $update) {
102 ++$update_count;
103
104 //delete old document
105 $doc = $index->find("+docid:{$update->id} +doctype:{$mod->name} +itemtype:{$update->itemtype}");
106
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);
112 }
113
114 //add new modified document back into index
115 $add = $get_document_function($update->id, $update->itemtype);
116
117 //object to insert into db
118 $dbid = $dbcontrol->addDocument($add);
119
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);
124 }
125 }
126 else{
127 mtrace("No types to update.\n");
128 }
129 mtrace("Finished $mod->name.\n");
130 }
131 }
132 }
133}
134
135//commit changes
136$index->commit();
137
138//update index date
139set_config("search_indexer_update_date", $startupdatedate);
140
001f3652 141mtrace("Finished $update_count updates");
6f1b1da1 142
143?>