Skip to content

Commit

Permalink
Addresses the tracker MDL - 18638 and MDL 18474
Browse files Browse the repository at this point in the history
  • Loading branch information
diml committed Mar 23, 2009
1 parent b8fdbda commit 964a5e9
Show file tree
Hide file tree
Showing 3 changed files with 310 additions and 280 deletions.
112 changes: 63 additions & 49 deletions search/delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,46 @@
* @subpackage search_engine
* @author Michael Champanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
* @date 2008/03/31
* @version prepared for 2.0
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*
* Asynchronous index cleaner
*
* Major chages in this review is passing the xxxx_db_names return to
* multiple arity to handle multiple document types modules
*/

/**
* includes and requires
*/
require_once('../config.php');
require_once("$CFG->dirroot/search/lib.php");
require_once("$CFG->dirroot/search/indexlib.php");

/// makes inclusions of the Zend Engine more reliable
$separator = (array_key_exists('WINDIR', $_SERVER)) ? ';' : ':' ;
ini_set('include_path', $CFG->dirroot.'/search'.$separator.ini_get('include_path'));require_login();
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from the cron script
}

global $DB;

/// makes inclusions of the Zend Engine more reliable
ini_set('include_path', $CFG->dirroot.PATH_SEPARATOR.'search'.PATH_SEPARATOR.ini_get('include_path'));

require_once($CFG->dirroot.'/search/lib.php');
require_once($CFG->dirroot.'/search/indexlib.php');

/// checks global search activation

// require_login();

if (empty($CFG->enableglobalsearch)) {
print_error('globalsearchdisabled', 'search');
}

/*
Obsolete with the MOODLE INTERNAL check
if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
print_error('beadmin', 'search', get_login_url());
} //if
}
*/

try {
$index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
Expand All @@ -44,74 +57,75 @@
$dbcontrol = new IndexDBControl();
$deletion_count = 0;
$startcleantime = time();

mtrace('Starting clean-up of removed records...');
mtrace('Index size before: '.$CFG->search_index_size."\n");

/// check all modules
if ($mods = get_records_select('modules')) {
$mods = array_merge($mods, search_get_additional_modules());

if ($mods = search_collect_searchables(false, true)){

foreach ($mods as $mod) {
//build function names
$class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
$delete_function = $mod->name.'_delete';
$db_names_function = $mod->name.'_db_names';
$deletions = array();

if (file_exists($class_file)) {
require_once($class_file);

//if both required functions exist
if (function_exists($delete_function) and function_exists($db_names_function)) {
mtrace("Checking $mod->name module for deletions.");
$valuesArray = $db_names_function();
if ($valuesArray){
foreach($valuesArray as $values){
$where = (isset($values[5])) ? 'WHERE '.$values[5] : '';
$where = (!empty($values[5])) ? 'WHERE '.$values[5] : '';
$itemtypes = ($values[4] != '*' && $values[4] != 'any') ? " itemtype = '{$values[4]}' AND " : '' ;
$query = "
SELECT
SELECT
id,
{$values[0]}
FROM
{" . $values[1] . "}
$where
FROM
{{$values[1]}}
$where
";
$docIds = get_records_sql($query);
$docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;

$table = SEARCH_DATABASE_TABLE;
$query = "
SELECT
id,
docid
FROM
{" . $table . "}
WHERE
doctype = '{$mod->name}' AND
$itemtypes
docid not in ('{$docIdList}')
";
$records = get_records_sql($query);

$docIds = $DB->get_records_sql($query, array());

if (!empty($docIds)){
$table = SEARCH_DATABASE_TABLE;
list($usql, $params) = $DB->get_in_or_equal(array_keys($docIds), SQL_PARAMS_QM, 'param0000', false); // negative IN
$query = "
SELECT
id,
docid
FROM
{{$table}}
WHERE
doctype = '{$mod->name}' AND
$itemtypes
docid $usql
";
$records = $DB->get_records_sql($query, $params);
} else {
$records = array();
}

// build an array of all the deleted records
if (is_array($records)) {
foreach($records as $record) {
$deletions[] = $delete_function($record->docid, $values[4]);
}
foreach($records as $record) {
$deletions[] = $delete_function($record->docid, $values[4]);
}
}

foreach ($deletions as $delete) {
// find the specific document in the index, using it's docid and doctype as keys
$doc = $index->find("+docid:{$delete->id} +doctype:$mod->name +itemtype:{$delete->itemtype}");

// get the record, should only be one
foreach ($doc as $thisdoc) {
++$deletion_count;
mtrace(" Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");

//remove it from index and database table
$dbcontrol->delDocument($thisdoc);
$index->delete($thisdoc->id);
Expand All @@ -126,17 +140,17 @@
}
}
}

/// commit changes

$index->commit();

/// update index date and index size

set_config("search_indexer_cleanup_date", $startcleantime);
set_config("search_index_size", (int)$CFG->search_index_size - (int)$deletion_count);

set_config('search_indexer_cleanup_date', $startcleantime);
set_config('search_index_size', (int)$CFG->search_index_size - (int)$deletion_count);
mtrace("Finished $deletion_count removals.");
mtrace('Index size after: '.$index->count());

?>
?>
Loading

0 comments on commit 964a5e9

Please sign in to comment.