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