$statement_end for Oracle changed to "/" because standard ;
[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...');
26 mtrace('Index size before: '.$index->count()."\n");
d9e1bf24 27
28 if ($mods = get_records_select('modules')) {
29 foreach ($mods as $mod) {
30 $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
6f1b1da1 31 $delete_function = $mod->name.'_delete';
32 $db_names_function = $mod->name.'_db_names';
33 $deletions = array();
d9e1bf24 34
35 if (file_exists($class_file)) {
6f1b1da1 36 require_once($class_file);
37
38 if (function_exists($delete_function) and function_exists($db_names_function)) {
39 mtrace("Checking $mod->name module for deletions.");
40 $values = $db_names_function();
41
42 $sql = "select id, docid from ".SEARCH_DATABASE_TABLE."
43 where doctype like '$mod->name'
44 and docid not in
45 (select ".$values[0]." from ".$values[1].")";
46
47 $records = get_records_sql($sql);
48
49 if (is_array($records)) {
50 foreach($records as $record) {
51 $deletions[] = $delete_function($record->docid);
52 } //foreach
53 } //if
54
55 foreach ($deletions as $delete) {
56 $doc = $index->find("+docid:$delete +doctype:$mod->name");
57
58 //get the record, should only be one
59 foreach ($doc as $thisdoc) {
60 ++$deletion_count;
61 mtrace(" Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
62
63 $dbcontrol->delDocument($thisdoc);
64 $index->delete($thisdoc->id);
65 } //foreach
66 } //foreach
67
68 mtrace("Finished $mod->name.\n");
69 } //if
70 } //if
d9e1bf24 71 } //foreach
72 } //if
6f1b1da1 73
74 //commit changes
75 $index->commit();
76
77 //update index date
78 set_config("search_indexer_run_date", time());
d9e1bf24 79
6f1b1da1 80 mtrace("Finished $deletion_count removals.");
81 mtrace('Index size after: '.$index->count().'</pre>');
d9e1bf24 82
83?>