MDL-15942 - separate data escaped for database entry from unescaped data
[moodle-linuxchix.git] / search / delete.php
blobe4579c60e8919baece76b04d96b374366ee071c1
1 <?php
2 /**
3 * Global Search Engine for Moodle
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
12 * Asynchronous index cleaner
14 * Major chages in this review is passing the xxxx_db_names return to
15 * multiple arity to handle multiple document types modules
18 /**
19 * includes and requires
21 require_once('../config.php');
22 require_once("$CFG->dirroot/search/lib.php");
25 require_login();
27 if (empty($CFG->enableglobalsearch)) {
28 error(get_string('globalsearchdisabled', 'search'));
31 if (!isadmin()) {
32 error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
33 } //if
35 //check for php5 (lib.php)
36 if (!search_check_php5()) {
37 $phpversion = phpversion();
38 mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version ".phpversion().")");
39 exit(0);
42 require_once("$CFG->dirroot/search/indexlib.php");
44 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
45 $dbcontrol = new IndexDBControl();
46 $deletion_count = 0;
47 $startcleantime = time();
49 mtrace('Starting clean-up of removed records...');
50 mtrace('Index size before: '.$CFG->search_index_size."\n");
52 if ($mods = get_records_select('modules')) {
53 $mods = array_merge($mods, search_get_additional_modules());
55 foreach ($mods as $mod) {
56 //build function names
57 $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
58 $delete_function = $mod->name.'_delete';
59 $db_names_function = $mod->name.'_db_names';
60 $deletions = array();
62 if (file_exists($class_file)) {
63 require_once($class_file);
65 //if both required functions exist
66 if (function_exists($delete_function) and function_exists($db_names_function)) {
67 mtrace("Checking $mod->name module for deletions.");
68 $valuesArray = $db_names_function();
69 if ($valuesArray){
70 foreach($valuesArray as $values){
71 $where = (isset($values[5])) ? 'WHERE '.$values[5] : '';
72 $itemtypes = ($values[4] != '*' && $values[4] != 'any') ? " itemtype = '{$values[4]}' AND " : '' ;
73 $query = "
74 SELECT
75 id,
76 {$values[0]}
77 FROM
78 {$CFG->prefix}{$values[1]}
79 $where
81 $docIds = get_records_sql($query);
82 $docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
84 $table = SEARCH_DATABASE_TABLE;
85 $query = "
86 SELECT
87 id,
88 docid
89 FROM
90 {$CFG->prefix}{$table}
91 WHERE
92 doctype = '{$mod->name}' AND
93 $itemtypes
94 docid not in ('{$docIdList}')
96 $records = get_records_sql($query);
98 // build an array of all the deleted records
99 if (is_array($records)) {
100 foreach($records as $record) {
101 $deletions[] = $delete_function($record->docid, $values[4]);
106 foreach ($deletions as $delete) {
107 // find the specific document in the index, using it's docid and doctype as keys
108 $doc = $index->find("+docid:{$delete->id} +doctype:$mod->name +itemtype:{$delete->itemtype}");
110 // get the record, should only be one
111 foreach ($doc as $thisdoc) {
112 ++$deletion_count;
113 mtrace(" Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
115 //remove it from index and database table
116 $dbcontrol->delDocument($thisdoc);
117 $index->delete($thisdoc->id);
121 else{
122 mtrace("No types to delete.\n");
124 mtrace("Finished $mod->name.\n");
130 /// commit changes
132 $index->commit();
134 /// update index date and index size
136 set_config("search_indexer_cleanup_date", $startcleantime);
137 set_config("search_index_size", (int)$CFG->search_index_size - (int)$deletion_count);
139 mtrace("Finished $deletion_count removals.");
140 mtrace('Index size after: '.$index->count());