MDL-11719 increased size of idnumber in user table - needed for some enrolment plugin...
[moodle.git] / search / delete.php
1 <?php
2     /**
3     * Global Search Engine for Moodle
4     *
5     * @package search
6     * @category core
7     * @subpackage search_engine
8     * @author Michael Champanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
9     * @date 2008/03/31
10     * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11     *
12     * Asynchronous index cleaner
13     *
14     * Major chages in this review is passing the xxxx_db_names return to
15     * multiple arity to handle multiple document types modules
16     */
17     
18     /**
19     * includes and requires
20     */
21     require_once('../config.php');
22     require_once("$CFG->dirroot/search/lib.php");
23     require_once("$CFG->dirroot/search/indexlib.php");    
24     
25 /// makes inclusions of the Zend Engine more reliable                               
26     $separator = (array_key_exists('WINDIR', $_SERVER)) ? ';' : ':' ;                   
27     ini_set('include_path', $CFG->dirroot.'/search'.$separator.ini_get('include_path'));require_login();
28     
29 /// checks global search activation
30     if (empty($CFG->enableglobalsearch)) {
31         error(get_string('globalsearchdisabled', 'search'));
32     }
33     
34     if (!isadmin()) {
35         error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
36     } //if
37     
38 /// check for php5 (lib.php)
39     if (!search_check_php5()) {
40         $phpversion = phpversion();
41         mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version ".phpversion().")");
42         exit(0);
43     }
44     
45     try {
46         $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
47     } catch(LuceneException $e) {
48         mtrace("Could not construct a valid index. Maybe the first indexation was never made, or files might be corrupted. Run complete indexation again.");
49         return;
50     }
51     $dbcontrol = new IndexDBControl();
52     $deletion_count = 0;
53     $startcleantime = time();
54     
55     mtrace('Starting clean-up of removed records...');
56     mtrace('Index size before: '.$CFG->search_index_size."\n");
57     
58 /// check all modules
59     if ($mods = get_records_select('modules')) {
60         $mods = array_merge($mods, search_get_additional_modules());
61         
62         foreach ($mods as $mod) {
63             //build function names
64             $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
65             $delete_function = $mod->name.'_delete';
66             $db_names_function = $mod->name.'_db_names';
67             $deletions = array();
68             
69             if (file_exists($class_file)) {
70                 require_once($class_file);
71                 
72                 //if both required functions exist
73                 if (function_exists($delete_function) and function_exists($db_names_function)) {
74                     mtrace("Checking $mod->name module for deletions.");
75                     $valuesArray = $db_names_function();
76                     if ($valuesArray){
77                         foreach($valuesArray as $values){
78                            $where = (isset($values[5])) ? 'WHERE '.$values[5] : '';
79                            $itemtypes = ($values[4] != '*' && $values[4] != 'any') ? " itemtype = '{$values[4]}' AND " : '' ;
80                            $query = "
81                                 SELECT 
82                                     id,
83                                     {$values[0]}
84                                 FROM 
85                                     {$CFG->prefix}{$values[1]}
86                                     $where
87                             ";
88                             $docIds = get_records_sql($query);
89                             $docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
90                             
91                             $table = SEARCH_DATABASE_TABLE;
92                             $query = "
93                                 SELECT 
94                                     id, 
95                                     docid 
96                                 FROM 
97                                     {$CFG->prefix}{$table}
98                                 WHERE 
99                                     doctype = '{$mod->name}' AND 
100                                     $itemtypes
101                                     docid not in ('{$docIdList}')
102                             ";
103                             $records = get_records_sql($query);
104                             
105                             // build an array of all the deleted records
106                             if (is_array($records)) {
107                                 foreach($records as $record) {
108                                     $deletions[] = $delete_function($record->docid, $values[4]);
109                                 }
110                             }
111                         }
112                         
113                         foreach ($deletions as $delete) {
114                             // find the specific document in the index, using it's docid and doctype as keys
115                             $doc = $index->find("+docid:{$delete->id} +doctype:$mod->name +itemtype:{$delete->itemtype}");
116                             
117                             // get the record, should only be one
118                             foreach ($doc as $thisdoc) {
119                                 ++$deletion_count;
120                                 mtrace("  Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
121                                 
122                                 //remove it from index and database table
123                                 $dbcontrol->delDocument($thisdoc);
124                                 $index->delete($thisdoc->id);
125                             }
126                         }
127                     }
128                     else{
129                         mtrace("No types to delete.\n");
130                     }
131                     mtrace("Finished $mod->name.\n");
132                 }
133             }
134         }
135     }
136     
137 /// commit changes
139     $index->commit();
140     
141 /// update index date and index size
143     set_config("search_indexer_cleanup_date", $startcleantime);
144     set_config("search_index_size", (int)$CFG->search_index_size - (int)$deletion_count);
145     
146     mtrace("Finished $deletion_count removals.");
147     mtrace('Index size after: '.$index->count());
149 ?>