Moodle release 1.9.15
[moodle.git] / search / delete.php
blob2a1c713eed1a2a90b796a3caa61e3600b18813c6
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');
23 if (!defined('MOODLE_INTERNAL')) {
24 die('Direct access to this script is forbidden.'); /// It must be included from the cron script
27 /// makes inclusions of the Zend Engine more reliable
28 ini_set('include_path', $CFG->dirroot.DIRECTORY_SEPARATOR.'search'.PATH_SEPARATOR.ini_get('include_path'));
30 require_once($CFG->dirroot.'/search/lib.php');
31 require_once($CFG->dirroot.'/search/indexlib.php');
33 /// checks global search activation
35 // require_login();
37 if (empty($CFG->enableglobalsearch)) {
38 error(get_string('globalsearchdisabled', 'search'));
42 Obsolete with the MOODLE INTERNAL check
43 if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
44 error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
48 try {
49 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
50 } catch(LuceneException $e) {
51 mtrace("Could not construct a valid index. Maybe the first indexation was never made, or files might be corrupted. Run complete indexation again.");
52 return;
54 $dbcontrol = new IndexDBControl();
55 $deletion_count = 0;
56 $startcleantime = time();
58 mtrace('Starting clean-up of removed records...');
59 mtrace('Index size before: '.$CFG->search_index_size."\n");
61 /// check all modules
62 if ($mods = search_collect_searchables(false, true)){
64 foreach ($mods as $mod) {
65 //build function names
66 $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
67 $delete_function = $mod->name.'_delete';
68 $db_names_function = $mod->name.'_db_names';
69 $deletions = array();
71 if (file_exists($class_file)) {
72 require_once($class_file);
74 //if both required functions exist
75 if (function_exists($delete_function) and function_exists($db_names_function)) {
76 mtrace("Checking $mod->name module for deletions.");
77 $valuesArray = $db_names_function();
78 if ($valuesArray){
79 foreach($valuesArray as $values){
80 $where = (!empty($values[5])) ? 'WHERE '.$values[5] : '';
81 $itemtypes = ($values[4] != '*' && $values[4] != 'any') ? " itemtype = '{$values[4]}' AND " : '' ;
82 $query = "
83 SELECT
84 id,
85 {$values[0]}
86 FROM
87 {$CFG->prefix}{$values[1]}
88 $where
90 $docIds = get_records_sql($query);
91 $docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
93 $table = SEARCH_DATABASE_TABLE;
94 $query = "
95 SELECT
96 id,
97 docid
98 FROM
99 {$CFG->prefix}{$table}
100 WHERE
101 doctype = '{$mod->name}' AND
102 $itemtypes
103 docid not in ('{$docIdList}')
105 $records = get_records_sql($query);
107 // build an array of all the deleted records
108 if (is_array($records)) {
109 foreach($records as $record) {
110 $deletions[] = $delete_function($record->docid, $values[4]);
115 foreach ($deletions as $delete) {
116 // find the specific document in the index, using it's docid and doctype as keys
117 $doc = $index->find("+docid:{$delete->id} +doctype:$mod->name +itemtype:{$delete->itemtype}");
119 // get the record, should only be one
120 foreach ($doc as $thisdoc) {
121 ++$deletion_count;
122 mtrace(" Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
124 //remove it from index and database table
125 $dbcontrol->delDocument($thisdoc);
126 $index->delete($thisdoc->id);
130 else{
131 mtrace("No types to delete.\n");
133 mtrace("Finished $mod->name.\n");
139 /// commit changes
141 $index->commit();
143 /// update index date and index size
145 set_config("search_indexer_cleanup_date", $startcleantime);
146 set_config("search_index_size", (int)$CFG->search_index_size - (int)$deletion_count);
148 mtrace("Finished $deletion_count removals.");
149 mtrace('Index size after: '.$index->count());