MDL-30192 restore - prevent any extra info to be created for deleted users
[moodle.git] / search / add.php
blob8543215a569781d682614828bc5e52338f21bb72
1 <?php
2 /**
3 * Global Search Engine for Moodle
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 * @version prepared for 2.0
11 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
13 * Asynchronous adder for new indexable contents
15 * Major chages in this review is passing the xxxx_db_names return to
16 * multiple arity to handle multiple document types modules
18 * changes are in implementing new $DB access
21 /**
22 * includes and requires
24 require_once('../config.php');
26 if (!defined('MOODLE_INTERNAL')) {
27 die('Direct access to this script is forbidden.'); /// It must be included from the cron script
30 global $DB;
32 /// makes inclusions of the Zend Engine more reliable
33 ini_set('include_path', $CFG->dirroot.DIRECTORY_SEPARATOR.'search'.PATH_SEPARATOR.ini_get('include_path'));
35 require_once($CFG->dirroot.'/search/lib.php');
36 require_once($CFG->dirroot.'/search/indexlib.php');
39 /// checks global search activation
41 // require_login();
43 if (empty($CFG->enableglobalsearch)) {
44 print_error('globalsearchdisabled', 'search');
48 Obsolete with the MOODLE INTERNAL check
49 if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
50 print_error('beadmin', 'search', get_login_url());
54 /// check index
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;
62 $dbcontrol = new IndexDBControl();
63 $addition_count = 0;
64 $mainstartindextime = time();
66 mtrace('Starting index update (additions)...');
67 mtrace('Index size before: '.$CFG->search_index_size."\n");
69 /// get all modules
70 if ($mods = search_collect_searchables(false, true)){
72 /// append virtual modules onto array
74 foreach ($mods as $mod) {
76 $indexdate = 0;
77 $indexdatestring = 'search_indexer_run_date_'.$mod->name;
78 $startrundate = time();
79 if (isset($CFG->$indexdatestring)) {
80 $indexdate = $CFG->$indexdatestring;
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();
90 if (file_exists($class_file)) {
91 require_once($class_file);
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){
99 $where = (isset($values[5]) and $values[5]!='') ? 'AND ('.$values[5].')' : '';
100 $itemtypes = ($values[4] != '*' && $values[4] != 'any') ? " AND itemtype = '{$values[4]}' " : '' ;
102 //select records in MODULE table, but not in SEARCH_DATABASE_TABLE
103 $table = SEARCH_DATABASE_TABLE;
104 $query = "
105 SELECT
106 docid,
107 itemtype
108 FROM
109 {{$table}}
110 WHERE
111 doctype = ?
112 $itemtypes
114 $docIds = $DB->get_records_sql_menu($query, array($mod->name));
116 if (!empty($docIds)){
117 list($usql, $params) = $DB->get_in_or_equal(array_keys($docIds), SQL_PARAMS_QM, 'param', false); // negative IN
118 $query = "
119 SELECT id,
120 $values[0] as docid
121 FROM
122 {{$values[1]}}
123 WHERE
124 id $usql AND
125 $values[2] > $indexdate
126 $where
128 $records = $DB->get_records_sql($query, $params);
129 } else {
130 $records = array();
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;
144 // foreach document, add it to the index and database table
145 foreach ($additions as $add) {
146 ++$addition_count;
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);
153 // synchronise db with index
154 $add->addField(Zend_Search_Lucene_Field::Keyword('dbid', $dbid));
156 $index->addDocument($add);
158 mtrace(" Add: $add->title (database id = $add->dbid, moodle instance id = $add->docid)");
161 catch (dml_write_exception $e) {
162 mtrace(" Add: FAILED adding '$add->title' , moodle instance id = $add->docid , Error: $e->error ");
163 mtrace($e);
168 else{
169 mtrace("No types to add.\n");
172 //commit changes
173 $index->commit();
175 //update index date
176 set_config($indexdatestring, $startrundate);
178 mtrace("Finished $mod->name.\n");
184 /// commit changes
186 $index->commit();
188 /// update index date and size
190 set_config('search_indexer_run_date', $mainstartindextime);
191 set_config('search_index_size', (int)$CFG->search_index_size + (int)$addition_count);
193 /// print some additional info
195 mtrace("Added $addition_count documents.");
196 mtrace('Index size after: '.$index->count());