MDL-12234, fixing unicode issues with global search
[moodle.git] / search / add.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* 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*/
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$addition_count = 0;
39$startindextime = time();
40
41$indexdate = $CFG->search_indexer_run_date;
42
001f3652 43mtrace('Starting index update (additions)...');
2f338ab5 44mtrace('Index size before: '.$CFG->search_index_size."\n");
45
46//get all modules
47if ($mods = get_records_select('modules')) {
48
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}
132
133// commit changes
134$index->commit();
135
136// update index date and size
137set_config("search_indexer_run_date", $startindextime);
138set_config("search_index_size", (int)$CFG->search_index_size + (int)$addition_count);
139
140// print some additional info
141mtrace("Added $addition_count documents.");
001f3652 142mtrace('Index size after: '.$index->count());
6f1b1da1 143
144?>