MDL-11719 increased size of idnumber in user table - needed for some enrolment plugin...
[moodle.git] / search / add.php
1 <?php
2     /**
3     * Global Search Engine for Moodle
4     *
5     * @package search
6     * @category core
7     * @subpackage search_engine
8     * @author Michael Champanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
9     * @date 2008/03/31
10     * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11     *
12     * Asynchronous adder for new indexable contents
13     *
14     * Major chages in this review is passing the xxxx_db_names return to
15     * multiple arity to handle multiple document types modules
16     */
17     
18     /**
19     * includes and requires 
20     */
21     require_once('../config.php');
22     require_once("$CFG->dirroot/search/lib.php");
23     require_once("$CFG->dirroot/search/indexlib.php");
24     
25 /// makes inclusions of the Zend Engine more reliable
26     $separator = (array_key_exists('WINDIR', $_SERVER)) ? ';' : ':' ;
27     ini_set('include_path', $CFG->dirroot.'/search'.$separator.ini_get('include_path'));
29 /// checks global search activation
30     
31     require_login();
32     
33     if (empty($CFG->enableglobalsearch)) {
34         error(get_string('globalsearchdisabled', 'search'));
35     }
36     
37     if (!isadmin()) {
38         error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
39     } 
40     
41 /// check for php5 (lib.php)
43     if (!search_check_php5()) {
44         $phpversion = phpversion();
45         mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version ".phpversion().")");
46         exit(0);
47     } 
48     
49     try {
50         $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
51     } catch(LuceneException $e) {
52         mtrace("Could not construct a valid index. Maybe the first indexation was never made, or files might be corrupted. Run complete indexation again.");
53         return;
54     }
55     $dbcontrol = new IndexDBControl();
56     $addition_count = 0;
57     $startindextime = time();
58     
59     $indexdate = $CFG->search_indexer_run_date;
60     
61     mtrace('Starting index update (additions)...');
62     mtrace('Index size before: '.$CFG->search_index_size."\n");
63     
64 /// get all modules
65     if ($mods = get_records_select('modules')) {
66     
67 /// append virtual modules onto array
69     $mods = array_merge($mods, search_get_additional_modules());
70         foreach ($mods as $mod) {
71             //build include file and function names
72             $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
73             $db_names_function = $mod->name.'_db_names';
74             $get_document_function = $mod->name.'_single_document';
75             $get_newrecords_function = $mod->name.'_new_records';
76             $additions = array();
77             
78             if (file_exists($class_file)) {
79                 require_once($class_file);
80                 
81                 //if both required functions exist
82                 if (function_exists($db_names_function) and function_exists($get_document_function)) {
83                     mtrace("Checking $mod->name module for additions.");
84                     $valuesArray = $db_names_function();
85                     if ($valuesArray){
86                         foreach($valuesArray as $values){
87                             $where = (isset($values[5])) ? 'AND ('.$values[5].')' : '';
88                             $itemtypes = ($values[4] != '*' && $values[4] != 'any') ? " AND itemtype = '{$values[4]}' " : '' ;
89                             
90                             //select records in MODULE table, but not in SEARCH_DATABASE_TABLE
91                             $table = SEARCH_DATABASE_TABLE;
92                             $query = "
93                                 SELECT 
94                                     docid,
95                                     itemtype 
96                                 FROM 
97                                     {$CFG->prefix}{$table}
98                                 WHERE 
99                                     doctype = '{$mod->name}'
100                                     $itemtypes
101                             ";
102                             $docIds = get_records_sql_menu($query);
103                             $docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
104                             
105                             $query =  "
106                                 SELECT id, 
107                                     {$values[0]} as docid 
108                                 FROM 
109                                     {$CFG->prefix}{$values[1]} 
110                                 WHERE 
111                                     id NOT IN ('{$docIdList}') and 
112                                     {$values[2]} > {$indexdate}
113                                     $where
114                             ";
115                             $records = get_records_sql($query);
116                             
117                             // foreach record, build a module specific search document using the get_document function
118                             if (is_array($records)) {
119                                 foreach($records as $record) {
120                                     $add = $get_document_function($record->docid, $values[4]);
121                                     // some documents may not be indexable
122                                     if ($add)
123                                         $additions[] = $add;
124                                 } 
125                             } 
126                         } 
127                         
128                         // foreach document, add it to the index and database table
129                         foreach ($additions as $add) {
130                             ++$addition_count;
131                             
132                             // object to insert into db
133                             $dbid = $dbcontrol->addDocument($add);
134                             
135                             // synchronise db with index
136                             $add->addField(Zend_Search_Lucene_Field::Keyword('dbid', $dbid));
137                             
138                             mtrace("  Add: $add->title (database id = $add->dbid, moodle instance id = $add->docid)");
139                             
140                             $index->addDocument($add);
141                         } 
142                     }
143                     else{
144                         mtrace("No types to add.\n");
145                     }
146                     mtrace("Finished $mod->name.\n");
147                 } 
148             } 
149         } 
150     } 
151     
152 /// commit changes
154     $index->commit();
155     
156 /// update index date and size
158     set_config("search_indexer_run_date", $startindextime);
159     set_config("search_index_size", (int)$CFG->search_index_size + (int)$addition_count);
160     
161 /// print some additional info
163     mtrace("Added $addition_count documents.");
164     mtrace('Index size after: '.$index->count());
166 ?>