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 * Index asynchronous updator
15 * Major chages in this review is passing the xxxx_db_names return to
16 * multiple arity to handle multiple document types modules
20 * includes and requires
22 require_once('../config.php');
24 if (!defined('MOODLE_INTERNAL')) {
25 die('Direct access to this script is forbidden.'); /// It must be included from the cron script
30 /// makes inclusions of the Zend Engine more reliable
31 ini_set('include_path', $CFG->dirroot
.DIRECTORY_SEPARATOR
.'search'.PATH_SEPARATOR
.ini_get('include_path'));
33 require_once($CFG->dirroot
.'/search/lib.php');
34 require_once($CFG->dirroot
.'/search/indexlib.php');
36 /// checks global search activation
40 if (empty($CFG->enableglobalsearch
)) {
41 print_error('globalsearchdisabled', 'search');
45 Obsolete with the MOODLE INTERNAL check
46 if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
47 print_error('beadmin', 'search', get_login_url());
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.");
57 $dbcontrol = new IndexDBControl();
59 $mainstartupdatedate = time();
61 /// indexing changed resources
63 mtrace("Starting index update (updates)...\n");
65 if ($mods = search_collect_searchables(false, true)){
67 foreach ($mods as $mod) {
69 $indexdatestring = 'search_indexer_update_date_'.$mod->name
;
70 $startupdatedate = time();
71 if (isset($CFG->$indexdatestring)) {
72 $indexdate = $CFG->$indexdatestring;
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';
81 if (file_exists($class_file)) {
82 require_once($class_file);
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();
89 foreach($valuesArray as $values){
90 $where = (isset($values[5]) and $values[5]!='') ?
'AND ('.$values[5].')' : '';
91 $itemtypes = ($values[4] != '*' && $values[4] != 'any') ?
" AND itemtype = '{$values[4]}' " : '' ;
93 //TODO: check 'in' syntax with other RDBMS' (add and update.php as well)
94 $table = SEARCH_DATABASE_TABLE
;
105 $docIds = $DB->get_records_sql_menu($query, array($mod->name
));
106 if (!empty($docIds)){
107 list($usql, $params) = $DB->get_in_or_equal(array_keys($docIds));
115 $values[3] > $indexdate AND
119 $records = $DB->get_records_sql($query, $params);
124 foreach($records as $record) {
125 $updates[] = $delete_function($record->docid
, $docIds[$record->docid
]);
129 foreach ($updates as $update) {
133 //get old document for deletion later
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());
136 $doc = $index->find("+docid:{$update->id} +doctype:{$mod->name} +itemtype:{$update->itemtype}");
139 //add new modified document back into index
140 $add = $get_document_function($update->id
, $update->itemtype
);
142 //object to insert into db
143 $dbid = $dbcontrol->addDocument($add);
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);
152 catch (dml_write_exception
$e) {
153 mtrace(" Add: FAILED adding '$add->title' , moodle instance id = $add->docid , Error: $e->error ");
159 // ok we've successfully added the new document so far
160 // delete single previous old document
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
);
170 catch (dml_write_exception
$e) {
171 mtrace(" Delete: FAILED deleting '$thisdoc->title' , moodle instance id = $thisdoc->docid , Error: $e->error ");
178 mtrace("No types to update.\n");
184 set_config($indexdatestring, $startupdatedate);
186 mtrace("Finished $mod->name.\n");
196 set_config('search_indexer_update_date', $mainstartupdatedate);
198 mtrace("Finished $update_count updates");