Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / search / delete.php
blob3183e7ddb2f627f497532f38b36fcd2730e42262
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
8 * Asynchronous index cleaner
10 * Major chages in this review is passing the xxxx_db_names return to
11 * multiple arity to handle multiple document types modules
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'));
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);
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('<pre>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());
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();
54 if (file_exists($class_file)) {
55 require_once($class_file);
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
73 $docIds = get_records_sql($query);
74 $docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
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}')
88 $records = get_records_sql($query);
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]);
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}");
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)");
107 //remove it from index and database table
108 $dbcontrol->delDocument($thisdoc);
109 $index->delete($thisdoc->id);
113 else{
114 mtrace("No types to delete.\n");
116 mtrace("Finished $mod->name.\n");
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().'</pre>');