Lucene Zend Implementation update (better handle of UTF8)
[moodle.git] / search / delete.php
CommitLineData
d9e1bf24 1<?php
2
3 require_once('../config.php');
4 require_once("$CFG->dirroot/search/lib.php");
eef868d1 5
6f1b1da1 6 require_login();
7
721d74b0 8 if (empty($CFG->enableglobalsearch)) {
9 error('Global searching is not enabled.');
10 }
11
6f1b1da1 12 if (!isadmin()) {
13 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
14 } //if
eef868d1 15
6f1b1da1 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);
eef868d1 21 } //if
22
23 require_once("$CFG->dirroot/search/indexlib.php");
24
6f1b1da1 25 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
26 $dbcontrol = new IndexDBControl();
eef868d1 27 $deletion_count = 0;
28
6f1b1da1 29 mtrace('<pre>Starting clean-up of removed records...');
791a4cec 30 mtrace('Index size before: '.$CFG->search_index_size."\n");
eef868d1 31
d9e1bf24 32 if ($mods = get_records_select('modules')) {
791a4cec 33 $mods = array_merge($mods, search_get_additional_modules());
eef868d1 34
d9e1bf24 35 foreach ($mods as $mod) {
791a4cec 36 //build function names
d9e1bf24 37 $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
6f1b1da1 38 $delete_function = $mod->name.'_delete';
39 $db_names_function = $mod->name.'_db_names';
eef868d1 40 $deletions = array();
41
d9e1bf24 42 if (file_exists($class_file)) {
6f1b1da1 43 require_once($class_file);
eef868d1 44
6f1b1da1 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();
eef868d1 48
791a4cec 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].")";
6f1b1da1 53
eef868d1 54 $records = get_records_sql($sql);
55
791a4cec 56 //build an array of all the deleted records
eef868d1 57 if (is_array($records)) {
6f1b1da1 58 foreach($records as $record) {
59 $deletions[] = $delete_function($record->docid);
60 } //foreach
eef868d1 61 } //if
62
63 foreach ($deletions as $delete) {
791a4cec 64 //find the specific document in the index, using it's docid and doctype as keys
eef868d1 65 $doc = $index->find("+docid:$delete +doctype:$mod->name");
66
6f1b1da1 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)");
eef868d1 71
791a4cec 72 //remove it from index and database table
6f1b1da1 73 $dbcontrol->delDocument($thisdoc);
eef868d1 74 $index->delete($thisdoc->id);
6f1b1da1 75 } //foreach
76 } //foreach
eef868d1 77
6f1b1da1 78 mtrace("Finished $mod->name.\n");
eef868d1 79 } //if
6f1b1da1 80 } //if
d9e1bf24 81 } //foreach
82 } //if
eef868d1 83
6f1b1da1 84 //commit changes
85 $index->commit();
eef868d1 86
791a4cec 87 //update index date and index size
6f1b1da1 88 set_config("search_indexer_run_date", time());
791a4cec 89 set_config("search_index_size", (int)$CFG->search_index_size - (int)$deletion_count);
d9e1bf24 90
6f1b1da1 91 mtrace("Finished $deletion_count removals.");
92 mtrace('Index size after: '.$index->count().'</pre>');
d9e1bf24 93
94?>