MDL-11719 increased size of idnumber in user table - needed for some enrolment plugin...
[moodle.git] / search / update.php
CommitLineData
6f1b1da1 1<?php
67afeedd 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 * Index asynchronous updator
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");
2f338ab5 24
67afeedd 25/// makes inclusions of the Zend Engine more reliable
26 $separator = (array_key_exists('WINDIR', $_SERVER)) ? ';' : ':' ;
6b51c209 27 ini_set('include_path', $CFG->dirroot.'/search'.$separator.ini_get('include_path'));require_login();
2f338ab5 28
3319ef85 29/// checks global search activation
2f338ab5 30
3319ef85 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
67afeedd 41/// check for php5 (lib.php)
42 if (!search_check_php5()) {
43 $phpversion = phpversion();
44 mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version ".phpversion().")");
45 exit(0);
46 }
47
48 try {
49 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
50 } catch(LuceneException $e) {
51 mtrace("Could not construct a valid index. Maybe the first indexation was never made, or files might be corrupted. Run complete indexation again.");
52 return;
53 }
3319ef85 54 $dbcontrol = new IndexDBControl();
55 $update_count = 0;
56 $indexdate = $CFG->search_indexer_update_date;
57 $startupdatedate = time();
2f338ab5 58
3319ef85 59/// indexing changed resources
2f338ab5 60
3319ef85 61 mtrace("Starting index update (updates)...\n");
62
63 if ($mods = get_records_select('modules')) {
64 $mods = array_merge($mods, search_get_additional_modules());
2f338ab5 65
3319ef85 66 foreach ($mods as $mod) {
67 $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
68 $get_document_function = $mod->name.'_single_document';
69 $delete_function = $mod->name.'_delete';
70 $db_names_function = $mod->name.'_db_names';
71 $updates = array();
2f338ab5 72
3319ef85 73 if (file_exists($class_file)) {
74 require_once($class_file);
75
76 //if both required functions exist
77 if (function_exists($delete_function) and function_exists($db_names_function) and function_exists($get_document_function)) {
78 mtrace("Checking $mod->name module for updates.");
79 $valuesArray = $db_names_function();
80 if ($valuesArray){
81 foreach($valuesArray as $values){
2f338ab5 82
3319ef85 83 $where = (isset($values[5])) ? 'AND ('.$values[5].')' : '';
84 $itemtypes = ($values[4] != '*' && $values[4] != 'any') ? " AND itemtype = '{$values[4]}' " : '' ;
85
86 //TODO: check 'in' syntax with other RDBMS' (add and update.php as well)
87 $table = SEARCH_DATABASE_TABLE;
88 $query = "
89 SELECT
90 docid,
91 itemtype
92 FROM
93 {$CFG->prefix}{$table}
94 WHERE
95 doctype = '{$mod->name}'
96 $itemtypes
97 ";
98 $docIds = get_records_sql_menu($query);
99 $docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
100
101 $query = "
102 SELECT
103 id,
104 {$values[0]} as docid
105 FROM
106 {$CFG->prefix}{$values[1]}
107 WHERE
108 {$values[3]} > {$indexdate} AND
109 id IN ('{$docIdList}')
110 $where
111 ";
112 $records = get_records_sql($query);
113 if (is_array($records)) {
114 foreach($records as $record) {
115 $updates[] = $delete_function($record->docid, $docIds[$record->docid]);
116 }
2f338ab5 117 }
3319ef85 118 }
2f338ab5 119
3319ef85 120 foreach ($updates as $update) {
121 ++$update_count;
122
123 //delete old document
124 $doc = $index->find("+docid:{$update->id} +doctype:{$mod->name} +itemtype:{$update->itemtype}");
125
126 //get the record, should only be one
127 foreach ($doc as $thisdoc) {
128 mtrace(" Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
129 $dbcontrol->delDocument($thisdoc);
130 $index->delete($thisdoc->id);
131 }
132
133 //add new modified document back into index
134 $add = $get_document_function($update->id, $update->itemtype);
135
136 //object to insert into db
137 $dbid = $dbcontrol->addDocument($add);
138
139 //synchronise db with index
140 $add->addField(Zend_Search_Lucene_Field::Keyword('dbid', $dbid));
141 mtrace(" Add: $add->title (database id = $add->dbid, moodle instance id = $add->docid)");
142 $index->addDocument($add);
2f338ab5 143 }
67afeedd 144 }
145 else{
3319ef85 146 mtrace("No types to update.\n");
147 }
148 mtrace("Finished $mod->name.\n");
149 }
2f338ab5 150 }
151 }
152 }
3319ef85 153
154 //commit changes
155 $index->commit();
156
157 //update index date
158 set_config("search_indexer_update_date", $startupdatedate);
159
160 mtrace("Finished $update_count updates");
6f1b1da1 161
162?>