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 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
12 * Asynchronous adder for new indexable contents
14 * Major chages in this review is passing the xxxx_db_names return to
15 * multiple arity to handle multiple document types modules
19 * includes and requires
21 require_once('../config.php');
22 require_once("$CFG->dirroot/search/lib.php");
24 /// checks global search activation
28 if (empty($CFG->enableglobalsearch
)) {
29 error(get_string('globalsearchdisabled', 'search'));
33 error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
36 /// check for php5 (lib.php)
38 if (!search_check_php5()) {
39 $phpversion = phpversion();
40 mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version ".phpversion().")");
44 require_once("$CFG->dirroot/search/indexlib.php");
46 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH
);
47 $dbcontrol = new IndexDBControl();
49 $startindextime = time();
51 $indexdate = $CFG->search_indexer_run_date
;
53 mtrace('Starting index update (additions)...');
54 mtrace('Index size before: '.$CFG->search_index_size
."\n");
57 if ($mods = get_records_select('modules')) {
59 /// append virtual modules onto array
61 $mods = array_merge($mods, search_get_additional_modules());
62 foreach ($mods as $mod) {
63 //build include file and function names
64 $class_file = $CFG->dirroot
.'/search/documents/'.$mod->name
.'_document.php';
65 $db_names_function = $mod->name
.'_db_names';
66 $get_document_function = $mod->name
.'_single_document';
67 $get_newrecords_function = $mod->name
.'_new_records';
70 if (file_exists($class_file)) {
71 require_once($class_file);
73 //if both required functions exist
74 if (function_exists($db_names_function) and function_exists($get_document_function)) {
75 mtrace("Checking $mod->name module for additions.");
76 $valuesArray = $db_names_function();
78 foreach($valuesArray as $values){
79 $where = (isset($values[5])) ?
'AND ('.$values[5].')' : '';
80 $itemtypes = ($values[4] != '*' && $values[4] != 'any') ?
" AND itemtype = '{$values[4]}' " : '' ;
82 //select records in MODULE table, but not in SEARCH_DATABASE_TABLE
83 $table = SEARCH_DATABASE_TABLE
;
89 {$CFG->prefix}{$table}
91 doctype = '{$mod->name}'
94 $docIds = get_records_sql_menu($query);
95 $docIdList = ($docIds) ?
implode("','", array_keys($docIds)) : '' ;
101 {$CFG->prefix}{$values[1]}
103 id NOT IN ('{$docIdList}') and
104 {$values[2]} > {$indexdate}
107 $records = get_records_sql($query);
109 // foreach record, build a module specific search document using the get_document function
110 if (is_array($records)) {
111 foreach($records as $record) {
112 $add = $get_document_function($record->docid
, $values[4]);
113 // some documents may not be indexable
120 // foreach document, add it to the index and database table
121 foreach ($additions as $add) {
124 // object to insert into db
125 $dbid = $dbcontrol->addDocument($add);
127 // synchronise db with index
128 $add->addField(Zend_Search_Lucene_Field
::Keyword('dbid', $dbid));
130 mtrace(" Add: $add->title (database id = $add->dbid, moodle instance id = $add->docid)");
132 $index->addDocument($add);
136 mtrace("No types to add.\n");
138 mtrace("Finished $mod->name.\n");
148 /// update index date and size
150 set_config("search_indexer_run_date", $startindextime);
151 set_config("search_index_size", (int)$CFG->search_index_size +
(int)$addition_count);
153 /// print some additional info
155 mtrace("Added $addition_count documents.");
156 mtrace('Index size after: '.$index->count());