MDL-12234, fixing unicode issues with global search
[moodle.git] / search / delete.php
1 <?php
2 /**
3 * Global Search Engine for Moodle
4 * Michael Champanis (mchampan) [cynnical@gmail.com]
5 * review 1.8+ : Valery Fremaux [valery.fremaux@club-internet.fr] 
6 * 2007/08/02
7 *
8 * Asynchronous index cleaner
9 *
10 * Major chages in this review is passing the xxxx_db_names return to
11 * multiple arity to handle multiple document types modules
12 */
14 require_once('../config.php');
15 require_once("$CFG->dirroot/search/lib.php");
17 require_login();
19 if (empty($CFG->enableglobalsearch)) {
20     error(get_string('globalsearchdisabled', 'search'));
21 }
23 if (!isadmin()) {
24     error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
25 } //if
27 //check for php5 (lib.php)
28 if (!search_check_php5()) {
29     $phpversion = phpversion();
30     mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version $phpversion)");
31     exit(0);
32 }
34 require_once("$CFG->dirroot/search/indexlib.php");
36 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
37 $dbcontrol = new IndexDBControl();
38 $deletion_count = 0;
39 $startcleantime = time();
41 mtrace('Starting clean-up of removed records...');
42 mtrace('Index size before: '.$CFG->search_index_size."\n");
44 if ($mods = get_records_select('modules')) {
45     $mods = array_merge($mods, search_get_additional_modules());
46     
47     foreach ($mods as $mod) {
48         //build function names
49         $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
50         $delete_function = $mod->name.'_delete';
51         $db_names_function = $mod->name.'_db_names';
52         $deletions = array();
53         
54         if (file_exists($class_file)) {
55             require_once($class_file);
56             
57             //if both required functions exist
58             if (function_exists($delete_function) and function_exists($db_names_function)) {
59                 mtrace("Checking $mod->name module for deletions.");
60                 $valuesArray = $db_names_function();
61                 if ($valuesArray){
62                     foreach($valuesArray as $values){
63                        $where = (isset($values[5])) ? 'WHERE '.$values[5] : '';
64                        $itemtypes = ($values[4] != '*') ? " itemtype = '{$values[4]}' AND " : '' ;
65                        $query = "
66                             SELECT 
67                                 id,
68                                 {$values[0]}
69                             FROM 
70                                 {$CFG->prefix}{$values[1]}
71                                 $where
72                         ";
73                         $docIds = get_records_sql($query);
74                         $docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
75                         
76                         $table = SEARCH_DATABASE_TABLE;
77                         $query = "
78                             SELECT 
79                                 id, 
80                                 docid 
81                             FROM 
82                                 {$CFG->prefix}{$table}
83                             WHERE 
84                                 doctype = '{$mod->name}' AND 
85                                 $itemtypes
86                                 docid not in ('{$docIdList}')
87                         ";
88                         $records = get_records_sql($query);
89                         
90                         // build an array of all the deleted records
91                         if (is_array($records)) {
92                             foreach($records as $record) {
93                                 $deletions[] = $delete_function($record->docid, $values[4]);
94                             }
95                         }
96                     }
97                     
98                     foreach ($deletions as $delete) {
99                         // find the specific document in the index, using it's docid and doctype as keys
100                         $doc = $index->find("+docid:{$delete->id} +doctype:$mod->name +itemtype:{$delete->itemtype}");
101                         
102                         // get the record, should only be one
103                         foreach ($doc as $thisdoc) {
104                             ++$deletion_count;
105                             mtrace("  Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
106                             
107                             //remove it from index and database table
108                             $dbcontrol->delDocument($thisdoc);
109                             $index->delete($thisdoc->id);
110                         }
111                     }
112                 }
113                 else{
114                     mtrace("No types to delete.\n");
115                 }
116                 mtrace("Finished $mod->name.\n");
117             }
118         }
119     }
122 //commit changes
123 $index->commit();
125 //update index date and index size
126 set_config("search_indexer_cleanup_date", $startcleantime);
127 set_config("search_index_size", (int)$CFG->search_index_size - (int)$deletion_count);
129 mtrace("Finished $deletion_count removals.");
130 mtrace('Index size after: '.$index->count());
132 ?>