course-format-topic MDL-24680 Fixed XHTML bug that was destroying page layout.
[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     * @version prepared for 2.0
11     * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
12     *
13     * Asynchronous index cleaner
14     *
15     * Major chages in this review is passing the xxxx_db_names return to
16     * multiple arity to handle multiple document types modules
17     */
18     
19     /**
20     * includes and requires
21     */
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
26     }
27     
28     global $DB;
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');        
35     
36 /// checks global search activation
38     // require_login();
40     if (empty($CFG->enableglobalsearch)) {
41         print_error('globalsearchdisabled', 'search');
42     }
44     /*
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());
48     }
49     */
51     try {
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.");
55         return;
56     }
57     $dbcontrol = new IndexDBControl();
58     $deletion_count = 0;
59     $startcleantime = time();
60     
61     mtrace('Starting clean-up of removed records...');
62     mtrace('Index size before: '.$CFG->search_index_size."\n");
63     
64 /// check all modules
65     if ($mods = search_collect_searchables(false, true)){
66         
67         foreach ($mods as $mod) {
68             //build function names
69             $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
70             $delete_function = $mod->name.'_delete';
71             $db_names_function = $mod->name.'_db_names';
72             $deletions = array();
73             
74             if (file_exists($class_file)) {
75                 require_once($class_file);
76                 
77                 //if both required functions exist
78                 if (function_exists($delete_function) and function_exists($db_names_function)) {
79                     mtrace("Checking $mod->name module for deletions.");
80                     $valuesArray = $db_names_function();
81                     if ($valuesArray){
82                         foreach($valuesArray as $values){
83                            $where = (!empty($values[5])) ? 'WHERE '.$values[5] : '';
84                            $itemtypes = ($values[4] != '*' && $values[4] != 'any') ? " itemtype = '{$values[4]}' AND " : '' ;
85                            $query = "
86                                 SELECT 
87                                     id,
88                                     {$values[0]}
89                                 FROM 
90                                     {{$values[1]}}
91                                 $where
92                             ";
93                             $docIds = $DB->get_records_sql($query, array());
94                             
95                             if (!empty($docIds)){
96                                 $table = SEARCH_DATABASE_TABLE;
97                                 list($usql, $params) = $DB->get_in_or_equal(array_keys($docIds), SQL_PARAMS_QM, 'param0000', false); // negative IN
98                                 $query = "
99                                     SELECT 
100                                         id, 
101                                         docid 
102                                     FROM 
103                                         {{$table}}
104                                     WHERE 
105                                         doctype = '{$mod->name}' AND 
106                                         $itemtypes
107                                         docid $usql
108                                 ";
109                                 $records = $DB->get_records_sql($query, $params);
110                             } else {
111                                 $records = array();
112                             }
113                             
114                             // build an array of all the deleted records
115                             foreach($records as $record) {
116                                 $deletions[] = $delete_function($record->docid, $values[4]);
117                             }
118                         }
119                         
120                         foreach ($deletions as $delete) {
121                             // find the specific document in the index, using it's docid and doctype as keys
122                             $doc = $index->find("+docid:{$delete->id} +doctype:$mod->name +itemtype:{$delete->itemtype}");
123                             
124                             // get the record, should only be one
125                             foreach ($doc as $thisdoc) {
126                                 ++$deletion_count;
127                                 mtrace("  Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
128                                 
129                                 //remove it from index and database table
130                                 $dbcontrol->delDocument($thisdoc);
131                                 $index->delete($thisdoc->id);
132                             }
133                         }
134                     }
135                     else{
136                         mtrace("No types to delete.\n");
137                     }
138                     mtrace("Finished $mod->name.\n");
139                 }
140             }
141         }
142     }
143     
144 /// commit changes
146     $index->commit();
147     
148 /// update index date and index size
150     set_config('search_indexer_cleanup_date', $startcleantime);
151     set_config('search_index_size', (int)$CFG->search_index_size - (int)$deletion_count);
152     
153     mtrace("Finished $deletion_count removals.");
154     mtrace('Index size after: '.$index->count());
156 ?>