global search MDL-25009 indexing failures in each module are now handled so that...
[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
964a5e92 10 * @version prepared for 2.0
67afeedd 11 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
12 *
13 * Index asynchronous updator
14 *
15 * Major chages in this review is passing the xxxx_db_names return to
16 * multiple arity to handle multiple document types modules
17 */
964a5e92 18
67afeedd 19 /**
20 * includes and requires
21 */
22 require_once('../config.php');
2f338ab5 23
964a5e92 24 if (!defined('MOODLE_INTERNAL')) {
25 die('Direct access to this script is forbidden.'); /// It must be included from the cron script
26 }
27
28 global $DB;
2f338ab5 29
964a5e92 30/// makes inclusions of the Zend Engine more reliable
d8f209e8 31 ini_set('include_path', $CFG->dirroot.DIRECTORY_SEPARATOR.'search'.PATH_SEPARATOR.ini_get('include_path'));
2f338ab5 32
964a5e92 33 require_once($CFG->dirroot.'/search/lib.php');
34 require_once($CFG->dirroot.'/search/indexlib.php');
32487831 35
964a5e92 36/// checks global search activation
37
38 // require_login();
39
3319ef85 40 if (empty($CFG->enableglobalsearch)) {
32487831 41 print_error('globalsearchdisabled', 'search');
3319ef85 42 }
964a5e92 43
44 /*
45 Obsolete with the MOODLE INTERNAL check
4f0c2d00 46 if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
93f66983 47 print_error('beadmin', 'search', get_login_url());
32487831 48 }
964a5e92 49 */
32487831 50
67afeedd 51 try {
52 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
53 } catch(LuceneException $e) {
54 mtrace("Could not construct a valid index. Maybe the first indexation was never made, or files might be corrupted. Run complete indexation again.");
55 return;
56 }
3319ef85 57 $dbcontrol = new IndexDBControl();
58 $update_count = 0;
8dc68e3e 59 $mainstartupdatedate = time();
2f338ab5 60
3319ef85 61/// indexing changed resources
964a5e92 62
3319ef85 63 mtrace("Starting index update (updates)...\n");
964a5e92 64
65 if ($mods = search_collect_searchables(false, true)){
66
3319ef85 67 foreach ($mods as $mod) {
8dc68e3e
AB
68 $indexdate = 0;
69 $indexdatestring = 'search_indexer_update_date_'.$mod->name;
70 $startupdatedate = time();
71 if (isset($CFG->$indexdatestring)) {
72 $indexdate = $CFG->$indexdatestring;
73 }
74
3319ef85 75 $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
76 $get_document_function = $mod->name.'_single_document';
77 $delete_function = $mod->name.'_delete';
78 $db_names_function = $mod->name.'_db_names';
79 $updates = array();
964a5e92 80
3319ef85 81 if (file_exists($class_file)) {
82 require_once($class_file);
964a5e92 83
3319ef85 84 //if both required functions exist
85 if (function_exists($delete_function) and function_exists($db_names_function) and function_exists($get_document_function)) {
86 mtrace("Checking $mod->name module for updates.");
87 $valuesArray = $db_names_function();
88 if ($valuesArray){
89 foreach($valuesArray as $values){
8dc68e3e 90 $where = (isset($values[5]) and $values[5]!='') ? 'AND ('.$values[5].')' : '';
3319ef85 91 $itemtypes = ($values[4] != '*' && $values[4] != 'any') ? " AND itemtype = '{$values[4]}' " : '' ;
964a5e92 92
3319ef85 93 //TODO: check 'in' syntax with other RDBMS' (add and update.php as well)
94 $table = SEARCH_DATABASE_TABLE;
95 $query = "
964a5e92 96 SELECT
3319ef85 97 docid,
98 itemtype
964a5e92 99 FROM
100 {{$table}}
3319ef85 101 WHERE
c6307ef2 102 doctype = ?
3319ef85 103 $itemtypes
104 ";
8dc68e3e 105 $docIds = $DB->get_records_sql_menu($query, array($mod->name));
964a5e92 106 if (!empty($docIds)){
107 list($usql, $params) = $DB->get_in_or_equal(array_keys($docIds));
108 $query = "
109 SELECT
110 id,
111 $values[0] as docid
112 FROM
113 {{$values[1]}}
114 WHERE
115 $values[3] > $indexdate AND
116 id $usql
3319ef85 117 $where
964a5e92 118 ";
119 $records = $DB->get_records_sql($query, $params);
120 } else {
121 $records = array();
32487831 122 }
32487831 123
964a5e92 124 foreach($records as $record) {
125 $updates[] = $delete_function($record->docid, $docIds[$record->docid]);
126 }
127 }
8dc68e3e 128
3319ef85 129 foreach ($updates as $update) {
130 ++$update_count;
7ee2741b
AB
131 $added_doc = false;
132
133 //get old document for deletion later
8dc68e3e
AB
134 // change from default text only search to include numerals for this search.
135 Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive());
3319ef85 136 $doc = $index->find("+docid:{$update->id} +doctype:{$mod->name} +itemtype:{$update->itemtype}");
964a5e92 137
7ee2741b
AB
138 try {
139 //add new modified document back into index
140 $add = $get_document_function($update->id, $update->itemtype);
141
142 //object to insert into db
143 $dbid = $dbcontrol->addDocument($add);
144
145 //synchronise db with index
146 $add->addField(Zend_Search_Lucene_Field::Keyword('dbid', $dbid));
147 mtrace(" Add: $add->title (database id = $add->dbid, moodle instance id = $add->docid)");
148 $index->addDocument($add);
149 $added_doc = true;
150 }
151
152 catch (dml_write_exception $e) {
153 mtrace(" Add: FAILED adding '$add->title' , moodle instance id = $add->docid , Error: $e->error ");
154 mtrace($e);
155 $added_doc = false;
156 }
8dc68e3e 157
7ee2741b
AB
158 if ($added_doc) {
159 // ok we've successfully added the new document so far
160 // delete single previous old document
161 try {
162 //get the record, should only be one
163 foreach ($doc as $thisdoc) {
164 mtrace(" Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
165 $dbcontrol->delDocument($thisdoc);
166 $index->delete($thisdoc->id);
167 }
168 }
169
170 catch (dml_write_exception $e) {
171 mtrace(" Delete: FAILED deleting '$thisdoc->title' , moodle instance id = $thisdoc->docid , Error: $e->error ");
172 mtrace($e);
173 }
174 }
964a5e92 175 }
67afeedd 176 }
177 else{
3319ef85 178 mtrace("No types to update.\n");
179 }
8dc68e3e
AB
180 //commit changes
181 $index->commit();
182
183 //update index date
184 set_config($indexdatestring, $startupdatedate);
185
3319ef85 186 mtrace("Finished $mod->name.\n");
964a5e92 187 }
188 }
189 }
190 }
191
3319ef85 192 //commit changes
193 $index->commit();
8dc68e3e 194
3319ef85 195 //update index date
8dc68e3e
AB
196 set_config('search_indexer_update_date', $mainstartupdatedate);
197
3319ef85 198 mtrace("Finished $update_count updates");
6f1b1da1 199
964a5e92 200?>