3 * Global Search Engine for Moodle
7 * @subpackage search_engine
8 * @author Michael Champanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
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
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
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
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());
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.");
62 $dbcontrol = new IndexDBControl();
64 $mainstartindextime = time();
66 mtrace('Starting index update (additions)...');
67 mtrace('Index size before: '.$CFG->search_index_size
."\n");
70 if ($mods = search_collect_searchables(false, true)){
72 /// append virtual modules onto array
74 foreach ($mods as $mod) {
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';
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();
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
;
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
125 $values[2] > $indexdate
128 $records = $DB->get_records_sql($query, $params);
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
144 // foreach document, add it to the index and database table
145 foreach ($additions as $add) {
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.
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 ");
169 mtrace("No types to add.\n");
176 set_config($indexdatestring, $startrundate);
178 mtrace("Finished $mod->name.\n");
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());