Automatic installer.php lang files by installer_builder (20080623)
[moodle.git] / search / delete.php
blob8b86305e12901129ff5b466241f34fe1d0743367
1 <?php
3 require_once('../config.php');
4 require_once("$CFG->dirroot/search/lib.php");
6 require_login();
8 if (empty($CFG->enableglobalsearch)) {
9 error('Global searching is not enabled.');
12 if (!isadmin()) {
13 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
14 } //if
16 //check for php5 (lib.php)
17 if (!search_check_php5()) {
18 $phpversion = phpversion();
19 mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version $phpversion)");
20 exit(0);
21 } //if
23 require_once("$CFG->dirroot/search/indexlib.php");
25 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
26 $dbcontrol = new IndexDBControl();
27 $deletion_count = 0;
29 mtrace('<pre>Starting clean-up of removed records...');
30 mtrace('Index size before: '.$CFG->search_index_size."\n");
32 if ($mods = get_records_select('modules')) {
33 $mods = array_merge($mods, search_get_additional_modules());
35 foreach ($mods as $mod) {
36 //build function names
37 $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
38 $delete_function = $mod->name.'_delete';
39 $db_names_function = $mod->name.'_db_names';
40 $deletions = array();
42 if (file_exists($class_file)) {
43 require_once($class_file);
45 if (function_exists($delete_function) and function_exists($db_names_function)) {
46 mtrace("Checking $mod->name module for deletions.");
47 $values = $db_names_function();
49 $sql = "select id, docid from ".SEARCH_DATABASE_TABLE.
50 " where doctype like '$mod->name'".
51 " and docid not in".
52 " (select ".$values[0]." from ".$values[1].")";
54 $records = get_records_sql($sql);
56 //build an array of all the deleted records
57 if (is_array($records)) {
58 foreach($records as $record) {
59 $deletions[] = $delete_function($record->docid);
60 } //foreach
61 } //if
63 foreach ($deletions as $delete) {
64 //find the specific document in the index, using it's docid and doctype as keys
65 $doc = $index->find("+docid:$delete +doctype:$mod->name");
67 //get the record, should only be one
68 foreach ($doc as $thisdoc) {
69 ++$deletion_count;
70 mtrace(" Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
72 //remove it from index and database table
73 $dbcontrol->delDocument($thisdoc);
74 $index->delete($thisdoc->id);
75 } //foreach
76 } //foreach
78 mtrace("Finished $mod->name.\n");
79 } //if
80 } //if
81 } //foreach
82 } //if
84 //commit changes
85 $index->commit();
87 //update index date and index size
88 set_config("search_indexer_run_date", time());
89 set_config("search_index_size", (int)$CFG->search_index_size - (int)$deletion_count);
91 mtrace("Finished $deletion_count removals.");
92 mtrace('Index size after: '.$index->count().'</pre>');