fixes MDL-14775
[moodle.git] / search / delete.php
CommitLineData
d9e1bf24 1<?php
2f338ab5 2/**
3* Global Search Engine for Moodle
3319ef85 4*
5* @package search
6* @category core
7* @subpackage search_engine
8* @author Michael Champanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
9* @date 2008/03/31
10* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
2f338ab5 11*
12* Asynchronous index cleaner
13*
14* Major chages in this review is passing the xxxx_db_names return to
15* multiple arity to handle multiple document types modules
16*/
17
3319ef85 18/**
19* includes and requires
20*/
2f338ab5 21require_once('../config.php');
22require_once("$CFG->dirroot/search/lib.php");
23
2f338ab5 24
3319ef85 25 require_login();
26
27 if (empty($CFG->enableglobalsearch)) {
28 error(get_string('globalsearchdisabled', 'search'));
29 }
30
31 if (!isadmin()) {
32 error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
33 } //if
34
3319ef85 35 require_once("$CFG->dirroot/search/indexlib.php");
2f338ab5 36
3319ef85 37 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
38 $dbcontrol = new IndexDBControl();
39 $deletion_count = 0;
40 $startcleantime = time();
41
42 mtrace('Starting clean-up of removed records...');
43 mtrace('Index size before: '.$CFG->search_index_size."\n");
44
45 if ($mods = get_records_select('modules')) {
46 $mods = array_merge($mods, search_get_additional_modules());
2f338ab5 47
3319ef85 48 foreach ($mods as $mod) {
49 //build function names
50 $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
51 $delete_function = $mod->name.'_delete';
52 $db_names_function = $mod->name.'_db_names';
53 $deletions = array();
2f338ab5 54
3319ef85 55 if (file_exists($class_file)) {
56 require_once($class_file);
57
58 //if both required functions exist
59 if (function_exists($delete_function) and function_exists($db_names_function)) {
60 mtrace("Checking $mod->name module for deletions.");
61 $valuesArray = $db_names_function();
62 if ($valuesArray){
63 foreach($valuesArray as $values){
64 $where = (isset($values[5])) ? 'WHERE '.$values[5] : '';
65 $itemtypes = ($values[4] != '*' && $values[4] != 'any') ? " itemtype = '{$values[4]}' AND " : '' ;
66 $query = "
67 SELECT
68 id,
69 {$values[0]}
70 FROM
71 {$CFG->prefix}{$values[1]}
72 $where
73 ";
74 $docIds = get_records_sql($query);
75 $docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
76
77 $table = SEARCH_DATABASE_TABLE;
78 $query = "
79 SELECT
80 id,
81 docid
82 FROM
83 {$CFG->prefix}{$table}
84 WHERE
85 doctype = '{$mod->name}' AND
86 $itemtypes
87 docid not in ('{$docIdList}')
88 ";
89 $records = get_records_sql($query);
90
91 // build an array of all the deleted records
92 if (is_array($records)) {
93 foreach($records as $record) {
94 $deletions[] = $delete_function($record->docid, $values[4]);
95 }
2f338ab5 96 }
97 }
2f338ab5 98
3319ef85 99 foreach ($deletions as $delete) {
100 // find the specific document in the index, using it's docid and doctype as keys
101 $doc = $index->find("+docid:{$delete->id} +doctype:$mod->name +itemtype:{$delete->itemtype}");
2f338ab5 102
3319ef85 103 // get the record, should only be one
104 foreach ($doc as $thisdoc) {
105 ++$deletion_count;
106 mtrace(" Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
107
108 //remove it from index and database table
109 $dbcontrol->delDocument($thisdoc);
110 $index->delete($thisdoc->id);
111 }
2f338ab5 112 }
113 }
3319ef85 114 else{
115 mtrace("No types to delete.\n");
116 }
117 mtrace("Finished $mod->name.\n");
2f338ab5 118 }
2f338ab5 119 }
120 }
121 }
3319ef85 122
123/// commit changes
2f338ab5 124
3319ef85 125 $index->commit();
126
127/// update index date and index size
2f338ab5 128
3319ef85 129 set_config("search_indexer_cleanup_date", $startcleantime);
130 set_config("search_index_size", (int)$CFG->search_index_size - (int)$deletion_count);
131
132 mtrace("Finished $deletion_count removals.");
133 mtrace('Index size after: '.$index->count());
d9e1bf24 134
135?>