MDL-8682 - the last commit caused a regression that stopped it working.
[moodle.git] / search / delete.php
CommitLineData
d9e1bf24 1<?php
2f338ab5 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
7*
8* Asynchronous index cleaner
9*
10* Major chages in this review is passing the xxxx_db_names return to
11* multiple arity to handle multiple document types modules
12*/
13
14require_once('../config.php');
15require_once("$CFG->dirroot/search/lib.php");
16
17require_login();
18
19if (empty($CFG->enableglobalsearch)) {
20 error(get_string('globalsearchdisabled', 'search'));
21}
22
23if (!isadmin()) {
24 error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
25} //if
26
27//check for php5 (lib.php)
28if (!search_check_php5()) {
6f1b1da1 29 $phpversion = phpversion();
30 mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version $phpversion)");
31 exit(0);
2f338ab5 32}
33
34require_once("$CFG->dirroot/search/indexlib.php");
35
36$index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
37$dbcontrol = new IndexDBControl();
38$deletion_count = 0;
39$startcleantime = time();
40
001f3652 41mtrace('Starting clean-up of removed records...');
2f338ab5 42mtrace('Index size before: '.$CFG->search_index_size."\n");
43
44if ($mods = get_records_select('modules')) {
45 $mods = array_merge($mods, search_get_additional_modules());
46
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();
53
54 if (file_exists($class_file)) {
55 require_once($class_file);
56
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
72 ";
73 $docIds = get_records_sql($query);
74 $docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
75
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}')
87 ";
88 $records = get_records_sql($query);
89
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]);
94 }
95 }
96 }
97
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}");
101
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)");
106
107 //remove it from index and database table
108 $dbcontrol->delDocument($thisdoc);
109 $index->delete($thisdoc->id);
110 }
111 }
112 }
113 else{
114 mtrace("No types to delete.\n");
115 }
116 mtrace("Finished $mod->name.\n");
117 }
118 }
119 }
120}
121
122//commit changes
123$index->commit();
124
125//update index date and index size
126set_config("search_indexer_cleanup_date", $startcleantime);
127set_config("search_index_size", (int)$CFG->search_index_size - (int)$deletion_count);
128
129mtrace("Finished $deletion_count removals.");
001f3652 130mtrace('Index size after: '.$index->count());
d9e1bf24 131
132?>