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