MDL-11719 increased size of idnumber in user table - needed for some enrolment plugin...
[moodle.git] / search / delete.php
CommitLineData
d9e1bf24 1<?php
67afeedd 2 /**
3 * Global Search Engine for Moodle
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
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 */
3319ef85 17
67afeedd 18 /**
19 * includes and requires
20 */
21 require_once('../config.php');
22 require_once("$CFG->dirroot/search/lib.php");
23 require_once("$CFG->dirroot/search/indexlib.php");
24
25/// makes inclusions of the Zend Engine more reliable
26 $separator = (array_key_exists('WINDIR', $_SERVER)) ? ';' : ':' ;
6b51c209 27 ini_set('include_path', $CFG->dirroot.'/search'.$separator.ini_get('include_path'));require_login();
67afeedd 28
29/// checks global search activation
3319ef85 30 if (empty($CFG->enableglobalsearch)) {
31 error(get_string('globalsearchdisabled', 'search'));
32 }
33
34 if (!isadmin()) {
35 error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
36 } //if
37
67afeedd 38/// check for php5 (lib.php)
39 if (!search_check_php5()) {
40 $phpversion = phpversion();
41 mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version ".phpversion().")");
42 exit(0);
43 }
2f338ab5 44
67afeedd 45 try {
46 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
47 } catch(LuceneException $e) {
48 mtrace("Could not construct a valid index. Maybe the first indexation was never made, or files might be corrupted. Run complete indexation again.");
49 return;
50 }
3319ef85 51 $dbcontrol = new IndexDBControl();
52 $deletion_count = 0;
53 $startcleantime = time();
54
55 mtrace('Starting clean-up of removed records...');
56 mtrace('Index size before: '.$CFG->search_index_size."\n");
57
67afeedd 58/// check all modules
3319ef85 59 if ($mods = get_records_select('modules')) {
60 $mods = array_merge($mods, search_get_additional_modules());
2f338ab5 61
3319ef85 62 foreach ($mods as $mod) {
63 //build function names
64 $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
65 $delete_function = $mod->name.'_delete';
66 $db_names_function = $mod->name.'_db_names';
67 $deletions = array();
2f338ab5 68
3319ef85 69 if (file_exists($class_file)) {
70 require_once($class_file);
71
72 //if both required functions exist
73 if (function_exists($delete_function) and function_exists($db_names_function)) {
74 mtrace("Checking $mod->name module for deletions.");
75 $valuesArray = $db_names_function();
76 if ($valuesArray){
77 foreach($valuesArray as $values){
78 $where = (isset($values[5])) ? 'WHERE '.$values[5] : '';
79 $itemtypes = ($values[4] != '*' && $values[4] != 'any') ? " itemtype = '{$values[4]}' AND " : '' ;
80 $query = "
81 SELECT
82 id,
83 {$values[0]}
84 FROM
85 {$CFG->prefix}{$values[1]}
86 $where
87 ";
88 $docIds = get_records_sql($query);
89 $docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
90
91 $table = SEARCH_DATABASE_TABLE;
92 $query = "
93 SELECT
94 id,
95 docid
96 FROM
97 {$CFG->prefix}{$table}
98 WHERE
99 doctype = '{$mod->name}' AND
100 $itemtypes
101 docid not in ('{$docIdList}')
102 ";
103 $records = get_records_sql($query);
104
105 // build an array of all the deleted records
106 if (is_array($records)) {
107 foreach($records as $record) {
108 $deletions[] = $delete_function($record->docid, $values[4]);
109 }
2f338ab5 110 }
111 }
2f338ab5 112
3319ef85 113 foreach ($deletions as $delete) {
114 // find the specific document in the index, using it's docid and doctype as keys
115 $doc = $index->find("+docid:{$delete->id} +doctype:$mod->name +itemtype:{$delete->itemtype}");
2f338ab5 116
3319ef85 117 // get the record, should only be one
118 foreach ($doc as $thisdoc) {
119 ++$deletion_count;
120 mtrace(" Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
121
122 //remove it from index and database table
123 $dbcontrol->delDocument($thisdoc);
124 $index->delete($thisdoc->id);
125 }
2f338ab5 126 }
127 }
3319ef85 128 else{
129 mtrace("No types to delete.\n");
130 }
131 mtrace("Finished $mod->name.\n");
2f338ab5 132 }
2f338ab5 133 }
134 }
135 }
3319ef85 136
137/// commit changes
2f338ab5 138
3319ef85 139 $index->commit();
140
141/// update index date and index size
2f338ab5 142
3319ef85 143 set_config("search_indexer_cleanup_date", $startcleantime);
144 set_config("search_index_size", (int)$CFG->search_index_size - (int)$deletion_count);
145
146 mtrace("Finished $deletion_count removals.");
147 mtrace('Index size after: '.$index->count());
d9e1bf24 148
149?>