Automatic installer lang files (20110214)
[moodle.git] / search / delete.php
blob605dcfd959b27afdddd7ce73f402e1111cad77bd
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 * @version prepared for 2.0
11 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
13 * Asynchronous index cleaner
15 * Major chages in this review is passing the xxxx_db_names return to
16 * multiple arity to handle multiple document types modules
19 /**
20 * includes and requires
22 require_once('../config.php');
24 if (!defined('MOODLE_INTERNAL')) {
25 die('Direct access to this script is forbidden.'); /// It must be included from the cron script
28 global $DB;
30 /// makes inclusions of the Zend Engine more reliable
31 ini_set('include_path', $CFG->dirroot.DIRECTORY_SEPARATOR.'search'.PATH_SEPARATOR.ini_get('include_path'));
33 require_once($CFG->dirroot.'/search/lib.php');
34 require_once($CFG->dirroot.'/search/indexlib.php');
36 /// checks global search activation
38 // require_login();
40 if (empty($CFG->enableglobalsearch)) {
41 print_error('globalsearchdisabled', 'search');
45 Obsolete with the MOODLE INTERNAL check
46 if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
47 print_error('beadmin', 'search', get_login_url());
51 try {
52 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
53 } catch(LuceneException $e) {
54 mtrace("Could not construct a valid index. Maybe the first indexation was never made, or files might be corrupted. Run complete indexation again.");
55 return;
57 $dbcontrol = new IndexDBControl();
58 $deletion_count = 0;
59 $startcleantime = time();
61 mtrace('Starting clean-up of removed records...');
62 mtrace('Index size before: '.$CFG->search_index_size."\n");
64 /// check all modules
65 if ($mods = search_collect_searchables(false, true)){
67 foreach ($mods as $mod) {
68 //build function names
69 $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
70 $delete_function = $mod->name.'_delete';
71 $db_names_function = $mod->name.'_db_names';
72 $deletions = array();
74 if (file_exists($class_file)) {
75 require_once($class_file);
77 //if both required functions exist
78 if (function_exists($delete_function) and function_exists($db_names_function)) {
79 mtrace("Checking $mod->name module for deletions.");
80 $valuesArray = $db_names_function();
81 if ($valuesArray){
82 foreach($valuesArray as $values){
83 $where = (!empty($values[5])) ? 'WHERE '.$values[5] : '';
84 $itemtypes = ($values[4] != '*' && $values[4] != 'any') ? " itemtype = '{$values[4]}' AND " : '' ;
85 $query = "
86 SELECT
87 id,
88 {$values[0]}
89 FROM
90 {{$values[1]}}
91 $where
93 $docIds = $DB->get_records_sql($query, array());
95 if (!empty($docIds)){
96 $table = SEARCH_DATABASE_TABLE;
97 list($usql, $params) = $DB->get_in_or_equal(array_keys($docIds), SQL_PARAMS_QM, 'param0000', false); // negative IN
98 $query = "
99 SELECT
100 id,
101 docid
102 FROM
103 {{$table}}
104 WHERE
105 doctype = '{$mod->name}' AND
106 $itemtypes
107 docid $usql
109 $records = $DB->get_records_sql($query, $params);
110 } else {
111 $records = array();
114 // build an array of all the deleted records
115 foreach($records as $record) {
116 $deletions[] = $delete_function($record->docid, $values[4]);
120 foreach ($deletions as $delete) {
121 // find the specific document in the index, using it's docid and doctype as keys
122 // change from default text only search to include numerals for this search.
123 Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive());
124 $doc = $index->find("+docid:{$delete->id} +doctype:$mod->name +itemtype:{$delete->itemtype}");
126 // get the record, should only be one
127 foreach ($doc as $thisdoc) {
128 ++$deletion_count;
129 mtrace(" Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
131 //remove it from index and database table
132 $dbcontrol->delDocument($thisdoc);
133 $index->delete($thisdoc->id);
137 else{
138 mtrace("No types to delete.\n");
140 mtrace("Finished $mod->name.\n");
146 /// commit changes
148 $index->commit();
150 /// update index date and index size
152 set_config('search_indexer_cleanup_date', $startcleantime);
153 set_config('search_index_size', (int)$CFG->search_index_size - (int)$deletion_count);
155 mtrace("Finished $deletion_count removals.");
156 mtrace('Index size after: '.$index->count());