2006-08-16 13:34:07 +00:00
|
|
|
<?php
|
2008-05-10 14:11:10 +00:00
|
|
|
/**
|
|
|
|
* Global Search Engine for Moodle
|
|
|
|
*
|
|
|
|
* @package search
|
|
|
|
* @category core
|
|
|
|
* @subpackage search_engine
|
|
|
|
* @author Michael Champanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
|
|
|
|
* @date 2008/03/31
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
|
*
|
|
|
|
* Index asynchronous updator
|
|
|
|
*
|
|
|
|
* Major chages in this review is passing the xxxx_db_names return to
|
|
|
|
* multiple arity to handle multiple document types modules
|
|
|
|
*/
|
2008-07-21 08:28:26 +00:00
|
|
|
|
2008-05-10 14:11:10 +00:00
|
|
|
/**
|
|
|
|
* includes and requires
|
|
|
|
*/
|
|
|
|
require_once('../config.php');
|
|
|
|
require_once("$CFG->dirroot/search/lib.php");
|
|
|
|
require_once("$CFG->dirroot/search/indexlib.php");
|
2007-07-09 21:12:16 +00:00
|
|
|
|
2008-07-21 08:28:26 +00:00
|
|
|
/// makes inclusions of the Zend Engine more reliable
|
|
|
|
$separator = (array_key_exists('WINDIR', $_SERVER)) ? ';' : ':' ;
|
2008-05-10 18:32:07 +00:00
|
|
|
ini_set('include_path', $CFG->dirroot.'/search'.$separator.ini_get('include_path'));require_login();
|
2007-07-09 21:12:16 +00:00
|
|
|
|
2008-03-31 22:21:42 +00:00
|
|
|
/// checks global search activation
|
2007-07-09 21:12:16 +00:00
|
|
|
|
2008-03-31 22:21:42 +00:00
|
|
|
require_login();
|
2008-07-21 08:28:26 +00:00
|
|
|
|
2008-03-31 22:21:42 +00:00
|
|
|
if (empty($CFG->enableglobalsearch)) {
|
2008-07-21 08:28:26 +00:00
|
|
|
print_error('globalsearchdisabled', 'search');
|
2008-03-31 22:21:42 +00:00
|
|
|
}
|
2008-07-21 08:28:26 +00:00
|
|
|
|
2008-05-25 10:08:05 +00:00
|
|
|
if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
|
2008-07-21 08:28:26 +00:00
|
|
|
print_error('beadmin', 'search', "$CFG->wwwroot/login/index.php");
|
|
|
|
}
|
|
|
|
|
2008-05-10 14:11:10 +00:00
|
|
|
try {
|
|
|
|
$index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
|
|
|
|
} catch(LuceneException $e) {
|
|
|
|
mtrace("Could not construct a valid index. Maybe the first indexation was never made, or files might be corrupted. Run complete indexation again.");
|
|
|
|
return;
|
|
|
|
}
|
2008-03-31 22:21:42 +00:00
|
|
|
$dbcontrol = new IndexDBControl();
|
|
|
|
$update_count = 0;
|
|
|
|
$indexdate = $CFG->search_indexer_update_date;
|
|
|
|
$startupdatedate = time();
|
2007-07-09 21:12:16 +00:00
|
|
|
|
2008-03-31 22:21:42 +00:00
|
|
|
/// indexing changed resources
|
2008-07-21 08:28:26 +00:00
|
|
|
|
2008-03-31 22:21:42 +00:00
|
|
|
mtrace("Starting index update (updates)...\n");
|
2008-07-21 08:28:26 +00:00
|
|
|
|
2008-03-31 22:21:42 +00:00
|
|
|
if ($mods = get_records_select('modules')) {
|
|
|
|
$mods = array_merge($mods, search_get_additional_modules());
|
2008-07-21 08:28:26 +00:00
|
|
|
|
2008-03-31 22:21:42 +00:00
|
|
|
foreach ($mods as $mod) {
|
|
|
|
$class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
|
|
|
|
$get_document_function = $mod->name.'_single_document';
|
|
|
|
$delete_function = $mod->name.'_delete';
|
|
|
|
$db_names_function = $mod->name.'_db_names';
|
|
|
|
$updates = array();
|
2008-07-21 08:28:26 +00:00
|
|
|
|
2008-03-31 22:21:42 +00:00
|
|
|
if (file_exists($class_file)) {
|
|
|
|
require_once($class_file);
|
2008-07-21 08:28:26 +00:00
|
|
|
|
2008-03-31 22:21:42 +00:00
|
|
|
//if both required functions exist
|
|
|
|
if (function_exists($delete_function) and function_exists($db_names_function) and function_exists($get_document_function)) {
|
|
|
|
mtrace("Checking $mod->name module for updates.");
|
|
|
|
$valuesArray = $db_names_function();
|
|
|
|
if ($valuesArray){
|
|
|
|
foreach($valuesArray as $values){
|
2008-07-21 08:28:26 +00:00
|
|
|
|
2008-03-31 22:21:42 +00:00
|
|
|
$where = (isset($values[5])) ? 'AND ('.$values[5].')' : '';
|
|
|
|
$itemtypes = ($values[4] != '*' && $values[4] != 'any') ? " AND itemtype = '{$values[4]}' " : '' ;
|
2008-07-21 08:28:26 +00:00
|
|
|
|
2008-03-31 22:21:42 +00:00
|
|
|
//TODO: check 'in' syntax with other RDBMS' (add and update.php as well)
|
|
|
|
$table = SEARCH_DATABASE_TABLE;
|
|
|
|
$query = "
|
2008-07-21 08:28:26 +00:00
|
|
|
SELECT
|
2008-03-31 22:21:42 +00:00
|
|
|
docid,
|
|
|
|
itemtype
|
2008-07-21 08:28:26 +00:00
|
|
|
FROM
|
2008-03-31 22:21:42 +00:00
|
|
|
{$CFG->prefix}{$table}
|
|
|
|
WHERE
|
2008-06-01 16:08:16 +00:00
|
|
|
doctype = ?
|
2008-03-31 22:21:42 +00:00
|
|
|
$itemtypes
|
|
|
|
";
|
2008-06-01 16:08:16 +00:00
|
|
|
$docIds = $DB->get_records_sql_menu($query, array($mod->name));
|
2008-03-31 22:21:42 +00:00
|
|
|
$docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
|
2008-07-21 08:28:26 +00:00
|
|
|
|
2008-03-31 22:21:42 +00:00
|
|
|
$query = "
|
2008-07-21 08:28:26 +00:00
|
|
|
SELECT
|
|
|
|
id,
|
2008-03-31 22:21:42 +00:00
|
|
|
{$values[0]} as docid
|
2008-07-21 08:28:26 +00:00
|
|
|
FROM
|
|
|
|
{$CFG->prefix}{$values[1]}
|
|
|
|
WHERE
|
|
|
|
{$values[3]} > {$indexdate} AND
|
2008-03-31 22:21:42 +00:00
|
|
|
id IN ('{$docIdList}')
|
|
|
|
$where
|
|
|
|
";
|
|
|
|
$records = get_records_sql($query);
|
|
|
|
if (is_array($records)) {
|
|
|
|
foreach($records as $record) {
|
|
|
|
$updates[] = $delete_function($record->docid, $docIds[$record->docid]);
|
2008-07-21 08:28:26 +00:00
|
|
|
}
|
|
|
|
}
|
2008-03-31 22:21:42 +00:00
|
|
|
}
|
2008-07-21 08:28:26 +00:00
|
|
|
|
2008-03-31 22:21:42 +00:00
|
|
|
foreach ($updates as $update) {
|
|
|
|
++$update_count;
|
2008-07-21 08:28:26 +00:00
|
|
|
|
2008-03-31 22:21:42 +00:00
|
|
|
//delete old document
|
|
|
|
$doc = $index->find("+docid:{$update->id} +doctype:{$mod->name} +itemtype:{$update->itemtype}");
|
2008-07-21 08:28:26 +00:00
|
|
|
|
2008-03-31 22:21:42 +00:00
|
|
|
//get the record, should only be one
|
|
|
|
foreach ($doc as $thisdoc) {
|
|
|
|
mtrace(" Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
|
|
|
|
$dbcontrol->delDocument($thisdoc);
|
|
|
|
$index->delete($thisdoc->id);
|
2008-07-21 08:28:26 +00:00
|
|
|
}
|
|
|
|
|
2008-03-31 22:21:42 +00:00
|
|
|
//add new modified document back into index
|
|
|
|
$add = $get_document_function($update->id, $update->itemtype);
|
2008-07-21 08:28:26 +00:00
|
|
|
|
2008-03-31 22:21:42 +00:00
|
|
|
//object to insert into db
|
|
|
|
$dbid = $dbcontrol->addDocument($add);
|
2008-07-21 08:28:26 +00:00
|
|
|
|
2008-03-31 22:21:42 +00:00
|
|
|
//synchronise db with index
|
|
|
|
$add->addField(Zend_Search_Lucene_Field::Keyword('dbid', $dbid));
|
|
|
|
mtrace(" Add: $add->title (database id = $add->dbid, moodle instance id = $add->docid)");
|
|
|
|
$index->addDocument($add);
|
2008-07-21 08:28:26 +00:00
|
|
|
}
|
2008-05-10 14:11:10 +00:00
|
|
|
}
|
|
|
|
else{
|
2008-03-31 22:21:42 +00:00
|
|
|
mtrace("No types to update.\n");
|
|
|
|
}
|
|
|
|
mtrace("Finished $mod->name.\n");
|
2008-07-21 08:28:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-31 22:21:42 +00:00
|
|
|
//commit changes
|
|
|
|
$index->commit();
|
2008-07-21 08:28:26 +00:00
|
|
|
|
2008-03-31 22:21:42 +00:00
|
|
|
//update index date
|
|
|
|
set_config("search_indexer_update_date", $startupdatedate);
|
2008-07-21 08:28:26 +00:00
|
|
|
|
2008-03-31 22:21:42 +00:00
|
|
|
mtrace("Finished $update_count updates");
|
2006-08-16 13:34:07 +00:00
|
|
|
|
2008-07-21 08:28:26 +00:00
|
|
|
?>
|