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 index cleaner
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");
27 if (empty($CFG->enableglobalsearch
)) {
28 error(get_string('globalsearchdisabled', 'search'));
32 error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
35 //check for php5 (lib.php)
36 if (!search_check_php5()) {
37 $phpversion = phpversion();
38 mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version ".phpversion().")");
42 require_once("$CFG->dirroot/search/indexlib.php");
44 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH
);
45 $dbcontrol = new IndexDBControl();
47 $startcleantime = time();
49 mtrace('Starting clean-up of removed records...');
50 mtrace('Index size before: '.$CFG->search_index_size
."\n");
52 if ($mods = get_records_select('modules')) {
53 $mods = array_merge($mods, search_get_additional_modules());
55 foreach ($mods as $mod) {
56 //build function names
57 $class_file = $CFG->dirroot
.'/search/documents/'.$mod->name
.'_document.php';
58 $delete_function = $mod->name
.'_delete';
59 $db_names_function = $mod->name
.'_db_names';
62 if (file_exists($class_file)) {
63 require_once($class_file);
65 //if both required functions exist
66 if (function_exists($delete_function) and function_exists($db_names_function)) {
67 mtrace("Checking $mod->name module for deletions.");
68 $valuesArray = $db_names_function();
70 foreach($valuesArray as $values){
71 $where = (isset($values[5])) ?
'WHERE '.$values[5] : '';
72 $itemtypes = ($values[4] != '*' && $values[4] != 'any') ?
" itemtype = '{$values[4]}' AND " : '' ;
78 {$CFG->prefix}{$values[1]}
81 $docIds = get_records_sql($query);
82 $docIdList = ($docIds) ?
implode("','", array_keys($docIds)) : '' ;
84 $table = SEARCH_DATABASE_TABLE
;
90 {$CFG->prefix}{$table}
92 doctype = '{$mod->name}' AND
94 docid not in ('{$docIdList}')
96 $records = get_records_sql($query);
98 // build an array of all the deleted records
99 if (is_array($records)) {
100 foreach($records as $record) {
101 $deletions[] = $delete_function($record->docid
, $values[4]);
106 foreach ($deletions as $delete) {
107 // find the specific document in the index, using it's docid and doctype as keys
108 $doc = $index->find("+docid:{$delete->id} +doctype:$mod->name +itemtype:{$delete->itemtype}");
110 // get the record, should only be one
111 foreach ($doc as $thisdoc) {
113 mtrace(" Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
115 //remove it from index and database table
116 $dbcontrol->delDocument($thisdoc);
117 $index->delete($thisdoc->id
);
122 mtrace("No types to delete.\n");
124 mtrace("Finished $mod->name.\n");
134 /// update index date and index size
136 set_config("search_indexer_cleanup_date", $startcleantime);
137 set_config("search_index_size", (int)$CFG->search_index_size
- (int)$deletion_count);
139 mtrace("Finished $deletion_count removals.");
140 mtrace('Index size after: '.$index->count());