MDL-12234, fixing unicode issues with global search
[moodle.git] / search / add.php
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
7 *
8 * Asynchronous adder for new indexable contents
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 */
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'));
21 }
23 if (!isadmin()) {
24     error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
25
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);
32
34 require_once("$CFG->dirroot/search/indexlib.php");
36 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
37 $dbcontrol = new IndexDBControl();
38 $addition_count = 0;
39 $startindextime = time();
41 $indexdate = $CFG->search_indexer_run_date;
43 mtrace('Starting index update (additions)...');
44 mtrace('Index size before: '.$CFG->search_index_size."\n");
46 //get all modules
47 if ($mods = get_records_select('modules')) {
49 //append virtual modules onto array
50 $mods = array_merge($mods, search_get_additional_modules());
51     foreach ($mods as $mod) {
52         //build include file and function names
53         $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
54         $db_names_function = $mod->name.'_db_names';
55         $get_document_function = $mod->name.'_single_document';
56         $get_newrecords_function = $mod->name.'_new_records';
57         $additions = array();
58         
59         if (file_exists($class_file)) {
60             require_once($class_file);
61             
62             //if both required functions exist
63             if (function_exists($db_names_function) and function_exists($get_document_function)) {
64                 mtrace("Checking $mod->name module for additions.");
65                 $valuesArray = $db_names_function();
66                 if ($valuesArray){
67                     foreach($valuesArray as $values){
68                         $where = (isset($values[5])) ? 'AND ('.$values[5].')' : '';
69                         $itemtypes = ($values[4] != '*') ? " AND itemtype = '{$values[4]}' " : '' ;
70                         
71                         //select records in MODULE table, but not in SEARCH_DATABASE_TABLE
72                         $table = SEARCH_DATABASE_TABLE;
73                         $query = "
74                             SELECT 
75                                 docid,
76                                 itemtype 
77                             FROM 
78                                 {$CFG->prefix}{$table}
79                             WHERE 
80                                 doctype = '{$mod->name}'
81                                 $itemtypes
82                         ";
83                         $docIds = get_records_sql_menu($query);
84                         $docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
85                         
86                         $query =  "
87                             SELECT id, 
88                                 {$values[0]} as docid 
89                             FROM 
90                                 {$CFG->prefix}{$values[1]} 
91                             WHERE 
92                                 id NOT IN ('{$docIdList}') and 
93                                 {$values[2]} > {$indexdate}
94                                 $where
95                         ";
96                         $records = get_records_sql($query);
97                         
98                         // foreach record, build a module specific search document using the get_document function
99                         if (is_array($records)) {
100                             foreach($records as $record) {
101                                 $add = $get_document_function($record->docid, $values[4]);
102                                 // some documents may not be indexable
103                                 if ($add)
104                                     $additions[] = $add;
105                             } 
106                         } 
107                     } 
108                     
109                     // foreach document, add it to the index and database table
110                     foreach ($additions as $add) {
111                         ++$addition_count;
112                         
113                         // object to insert into db
114                         $dbid = $dbcontrol->addDocument($add);
115                         
116                         // synchronise db with index
117                         $add->addField(Zend_Search_Lucene_Field::Keyword('dbid', $dbid));
118                         
119                         mtrace("  Add: $add->title (database id = $add->dbid, moodle instance id = $add->docid)");
120                         
121                         $index->addDocument($add);
122                     } 
123                 }
124                 else{
125                     mtrace("No types to add.\n");
126                 }
127                 mtrace("Finished $mod->name.\n");
128             } 
129         } 
130     } 
131
133 // commit changes
134 $index->commit();
136 // update index date and size
137 set_config("search_indexer_run_date", $startindextime);
138 set_config("search_index_size", (int)$CFG->search_index_size + (int)$addition_count);
140 // print some additional info
141 mtrace("Added $addition_count documents.");
142 mtrace('Index size after: '.$index->count());
144 ?>