* @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);
$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);
}
}
}
-
+
/// 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());
-?>
+?>
\ No newline at end of file
* @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
*
* The query page - accepts a user-entered query string and returns results.
* All articles written by Helen Foster
*
*/
-
+
/**
* includes and requires
*/
require_once('../config.php');
- require_once("$CFG->dirroot/search/lib.php");
-
+ require_once($CFG->dirroot.'/search/lib.php');
if ($CFG->forcelogin) {
require_login();
}
-
+
if (empty($CFG->enableglobalsearch)) {
print_error('globalsearchdisabled', 'search');
}
-
+
$adv = new Object();
-
+
/// check for php5, but don't die yet (see line 52)
- require_once("{$CFG->dirroot}/search/querylib.php");
-
- $page_number = optional_param('page', -1, PARAM_INT);
- $pages = ($page_number == -1) ? false : true;
- $advanced = (optional_param('a', '0', PARAM_INT) == '1') ? true : false;
- $query_string = optional_param('query_string', '', PARAM_CLEAN);
-
- /**
- * discard harmfull searches
- */
- if (preg_match("/^[\*\?]+$/", $query_string)){
- $query_string = '';
- $error = get_string('fullwildcardquery','search');
- }
-
-
- if ($pages && isset($_SESSION['search_advanced_query'])) {
- // if both are set, then we are busy browsing through the result pages of an advanced query
- $adv = unserialize($_SESSION['search_advanced_query']);
- } elseif ($advanced) {
- // otherwise we are dealing with a new advanced query
- unset($_SESSION['search_advanced_query']);
- session_unregister('search_advanced_query');
+ require_once($CFG->dirroot.'/search/querylib.php');
- // chars to strip from strings (whitespace)
- $chars = " \t\n\r\0\x0B,-+";
-
- // retrieve advanced query variables
- $adv->mustappear = trim(optional_param('mustappear', '', PARAM_CLEAN), $chars);
- $adv->notappear = trim(optional_param('notappear', '', PARAM_CLEAN), $chars);
- $adv->canappear = trim(optional_param('canappear', '', PARAM_CLEAN), $chars);
- $adv->module = optional_param('module', '', PARAM_CLEAN);
- $adv->title = trim(optional_param('title', '', PARAM_CLEAN), $chars);
- $adv->author = trim(optional_param('author', '', PARAM_CLEAN), $chars);
- }
-
- if ($advanced) {
- //parse the advanced variables into a query string
- //TODO: move out to external query class (QueryParse?)
-
- $query_string = '';
-
- // get all available module types adding third party modules
- $module_types = array_merge(array('all'), array_values(search_get_document_types()));
- $module_types = array_merge($module_types, array_values(search_get_document_types('X_SEARCH_TYPE')));
- $adv->module = in_array($adv->module, $module_types) ? $adv->module : 'all';
-
- // convert '1 2' into '+1 +2' for required words field
- if (strlen(trim($adv->mustappear)) > 0) {
- $query_string = ' +'.implode(' +', preg_split("/[\s,;]+/", $adv->mustappear));
- }
-
- // convert '1 2' into '-1 -2' for not wanted words field
- if (strlen(trim($adv->notappear)) > 0) {
- $query_string .= ' -'.implode(' -', preg_split("/[\s,;]+/", $adv->notappear));
- }
-
- // this field is left untouched, apart from whitespace being stripped
- if (strlen(trim($adv->canappear)) > 0) {
- $query_string .= ' '.implode(' ', preg_split("/[\s,;]+/", $adv->canappear));
- }
-
- // add module restriction
- $doctypestr = get_string('doctype', 'search');
- $titlestr = get_string('title', 'search');
- $authorstr = get_string('author', 'search');
- if ($adv->module != 'all') {
- $query_string .= " +{$doctypestr}:".$adv->module;
- }
+ $page_number = optional_param('page', -1, PARAM_INT);
+ $pages = ($page_number == -1) ? false : true;
+ $advanced = (optional_param('a', '0', PARAM_INT) == '1') ? true : false;
+ $query_string = stripslashes(optional_param('query_string', '', PARAM_CLEAN));
- // create title search string
- if (strlen(trim($adv->title)) > 0) {
- $query_string .= " +{$titlestr}:".implode(" +{$titlestr}:", preg_split("/[\s,;]+/", $adv->title));
- }
+/// discard harmfull searches
- // create author search string
- if (strlen(trim($adv->author)) > 0) {
- $query_string .= " +{$authorstr}:".implode(" +{$authorstr}:", preg_split("/[\s,;]+/", $adv->author));
- }
-
- // save our options if the query is valid
- if (!empty($query_string)) {
- $_SESSION['search_advanced_query'] = serialize($adv);
- }
- }
-
- // normalise page number
- if ($page_number < 1) {
- $page_number = 1;
- }
-
- //run the query against the index ensuring internal coding works in UTF-8
- Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8());
- $sq = new SearchQuery($query_string, $page_number, 10, false);
+ if (!isset($CFG->block_search_utf8dir)){
+ set_config('block_search_utf8dir', 1);
+ }
+
+/// discard harmfull searches
+ if (preg_match("/^[\*\?]+$/", $query_string)){
+ $query_string = '';
+ $error = get_string('fullwildcardquery','search');
+ }
+
+
+ if ($pages && isset($_SESSION['search_advanced_query'])) {
+ // if both are set, then we are busy browsing through the result pages of an advanced query
+ $adv = unserialize($_SESSION['search_advanced_query']);
+ } elseif ($advanced) {
+ // otherwise we are dealing with a new advanced query
+ unset($_SESSION['search_advanced_query']);
+ session_unregister('search_advanced_query');
+
+ // chars to strip from strings (whitespace)
+ $chars = " \t\n\r\0\x0B,-+";
+
+ // retrieve advanced query variables
+ $adv->mustappear = trim(optional_param('mustappear', '', PARAM_CLEAN), $chars);
+ $adv->notappear = trim(optional_param('notappear', '', PARAM_CLEAN), $chars);
+ $adv->canappear = trim(optional_param('canappear', '', PARAM_CLEAN), $chars);
+ $adv->module = optional_param('module', '', PARAM_CLEAN);
+ $adv->title = trim(optional_param('title', '', PARAM_CLEAN), $chars);
+ $adv->author = trim(optional_param('author', '', PARAM_CLEAN), $chars);
+ }
+
+ if ($advanced) {
+ //parse the advanced variables into a query string
+ //TODO: move out to external query class (QueryParse?)
+
+ $query_string = '';
+
+ // get all available module types adding third party modules
+ $module_types = array_merge(array('all'), array_values(search_get_document_types()));
+ $module_types = array_merge($module_types, array_values(search_get_document_types('X_SEARCH_TYPE')));
+ $adv->module = in_array($adv->module, $module_types) ? $adv->module : 'all';
+
+ // convert '1 2' into '+1 +2' for required words field
+ if (strlen(trim($adv->mustappear)) > 0) {
+ $query_string = ' +'.implode(' +', preg_split("/[\s,;]+/", $adv->mustappear));
+ }
+
+ // convert '1 2' into '-1 -2' for not wanted words field
+ if (strlen(trim($adv->notappear)) > 0) {
+ $query_string .= ' -'.implode(' -', preg_split("/[\s,;]+/", $adv->notappear));
+ }
+
+ // this field is left untouched, apart from whitespace being stripped
+ if (strlen(trim($adv->canappear)) > 0) {
+ $query_string .= ' '.implode(' ', preg_split("/[\s,;]+/", $adv->canappear));
+ }
+
+ // add module restriction
+ $doctypestr = get_string('doctype', 'search');
+ $titlestr = get_string('title', 'search');
+ $authorstr = get_string('author', 'search');
+ if ($adv->module != 'all') {
+ $query_string .= " +{$doctypestr}:".$adv->module;
+ }
+
+ // create title search string
+ if (strlen(trim($adv->title)) > 0) {
+ $query_string .= " +{$titlestr}:".implode(" +{$titlestr}:", preg_split("/[\s,;]+/", $adv->title));
+ }
+
+ // create author search string
+ if (strlen(trim($adv->author)) > 0) {
+ $query_string .= " +{$authorstr}:".implode(" +{$authorstr}:", preg_split("/[\s,;]+/", $adv->author));
+ }
+
+ // save our options if the query is valid
+ if (!empty($query_string)) {
+ $_SESSION['search_advanced_query'] = serialize($adv);
+ }
+ }
+
+ // normalise page number
+ if ($page_number < 1) {
+ $page_number = 1;
+ }
+
+ //run the query against the index ensuring internal coding works in UTF-8
+ Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
+ $sq = new SearchQuery($query_string, $page_number, 10, false);
+
if (!$site = get_site()) {
redirect("index.php");
- }
-
+ }
+
$strsearch = get_string('search', 'search');
$strquery = get_string('enteryoursearchquery', 'search');
-
-// if ($CFG->version < 2007032200){ NOT RELIABLE
- if (!function_exists('build_navigation')){
- print_header("$site->shortname: $strsearch: $strquery", "$site->fullname",
- "<a href=\"index.php\">$strsearch</a> -> $strquery");
- } else {
- $navlinks[] = array('name' => $strsearch, 'link' => "index.php", 'type' => 'misc');
- $navlinks[] = array('name' => $strquery, 'link' => null, 'type' => 'misc');
- $navigation = build_navigation($navlinks);
- $site = get_site();
- print_header("$strsearch", "$site->fullname" , $navigation, "", "", true, " ", navmenu($site));
- }
-
+
+ // print the header
+ $navlinks[] = array('name' => $strsearch, 'link' => "index.php", 'type' => 'misc');
+ $navlinks[] = array('name' => $strquery, 'link' => null, 'type' => 'misc');
+ $navigation = build_navigation($navlinks);
+ $site = get_site();
+ print_header("$strsearch", "$site->fullname" , $navigation, '', '', true, ' ', navmenu($site));
+
if (!empty($error)){
notice ($error);
}
-
+
print_box_start();
print_heading($strquery);
-
+
print_box_start();
-
+
$vars = get_object_vars($adv);
-
+
if (isset($vars)) {
foreach ($vars as $key => $value) {
- // htmlentities breaks non-ascii chars
- $adv->key = $value;
- //$adv->$key = htmlentities($value);
- }
+ // htmlentities breaks non-ascii chars ??
+ $adv->key = stripslashes($value);
+ //$adv->$key = stripslashes(htmlentities($value));
+ }
}
?>
<form id="query" method="get" action="query.php">
- <?php
- if (!$advanced) {
+ <?php
+ if (!$advanced) {
?>
- <input type="text" name="query_string" length="50" value="<?php p($query_string) ?>" />
- <input type="submit" value="<?php print_string('search', 'search') ?>" />
+ <input type="text" name="query_string" length="50" value="<?php p($query_string) ?>" />
+ <input type="submit" value="<?php print_string('search', 'search') ?>" />
<a href="query.php?a=1"><?php print_string('advancedsearch', 'search') ?></a> |
<a href="stats.php"><?php print_string('statistics', 'search') ?></a>
- <?php
- }
+ <?php
+ }
else {
print_box_start();
?>
<input type="hidden" name="a" value="<?php print $advanced; ?>"/>
-
+
<table border="0" cellpadding="3" cellspacing="3">
-
+
<tr>
<td width="240"><?php print_string('thesewordsmustappear', 'search') ?>:</td>
<td><input type="text" name="mustappear" length="50" value="<?php print $adv->mustappear; ?>" /></td>
</tr>
-
+
<tr>
<td><?php print_string('thesewordsmustnotappear', 'search') ?>:</td>
<td><input type="text" name="notappear" length="50" value="<?php print $adv->notappear; ?>" /></td>
</tr>
-
+
<tr>
<td><?php print_string('thesewordshelpimproverank', 'search') ?>:</td>
<td><input type="text" name="canappear" length="50" value="<?php print $adv->canappear; ?>" /></td>
</tr>
-
+
<tr>
<td><?php print_string('whichmodulestosearch?', 'search') ?>:</td>
<td>
<select name="module">
- <?php
+ <?php
foreach($module_types as $mod) {
if ($mod == $adv->module) {
if ($mod != 'all'){
else{
print "<option value='$mod' selected=\"selected\">".get_string('all', 'search')."</option>\n";
}
- }
+ }
else {
if ($mod != 'all'){
print "<option value='$mod'>".get_string('modulenameplural', $mod)."</option>\n";
else{
print "<option value='$mod'>".get_string('all', 'search')."</option>\n";
}
- }
- }
+ }
+ }
?>
</select>
</td>
</tr>
-
+
<tr>
<td><?php print_string('wordsintitle', 'search') ?>:</td>
<td><input type="text" name="title" length="50" value="<?php print $adv->title; ?>" /></td>
</tr>
-
+
<tr>
<td><?php print_string('authorname', 'search') ?>:</td>
<td><input type="text" name="author" length="50" value="<?php print $adv->author; ?>" /></td>
</tr>
-
+
<tr>
<td colspan="3" align="center"><br /><input type="submit" value="<?php print_string('search', 'search') ?>" /></td>
</tr>
-
+
<tr>
<td colspan="3" align="center">
<table border="0" cellpadding="0" cellspacing="0">
</table>
<?php
print_box_end();
- }
+ }
?>
</form>
<br/>
<div class="mdl-align">
<?php
print_string('searching', 'search') . ': ';
-
+
if ($sq->is_valid_index()) {
//use cached variable to show up-to-date index size (takes deletions into account)
print $CFG->search_index_size;
- }
+ }
else {
print "0";
- }
-
+ }
+
print ' ';
print_string('documents', 'search');
print '.';
-
+
if (!$sq->is_valid_index() and has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
print '<p>' . get_string('noindexmessage', 'search') . '<a href="indexersplash.php">' . get_string('createanindex', 'search')."</a></p>\n";
- }
-
+ }
+
?>
</div>
<?php
print_box_end();
-
+
/// prints all the results in a box
if ($sq->is_valid()) {
print_box_start();
-
+
search_stopwatch();
$hit_count = $sq->count();
-
+
print "<br />";
-
+
print $hit_count.' '.get_string('resultsreturnedfor', 'search') . " '".s($query_string)."'.";
print "<br />";
-
+
if ($hit_count > 0) {
$page_links = $sq->page_numbers();
$hits = $sq->results();
-
+
if ($advanced) {
// if in advanced mode, search options are saved in the session, so
// we can remove the query string var from the page links, and replace
// it with a=1 (Advanced = on) instead
$page_links = preg_replace("/query_string=[^&]+/", 'a=1', $page_links);
- }
-
+ }
+
print "<ol>";
-
+
$typestr = get_string('type', 'search');
$scorestr = get_string('score', 'search');
$authorstr = get_string('author', 'search');
$searchables = search_collect_searchables(false, false);
- foreach ($hits as $listing) {
-
- $iconpath = $CFG->modpixpath.'/'.$listing->doctype.'/icon.gif';
- $coursename = get_field('course', 'fullname', 'id', $listing->courseid);
+ foreach ($hits as $listing) {
+
+ if ($listing->doctype == 'user'){ // A special handle for users
+ $icon = print_user_picture ($listing->author, 0, true, 0, true, false) ;
+ } else {
+ $iconpath = $CFG->modpixpath.'/'.$listing->doctype.'/icon.gif';
+ $icon = "<img align=\"top\" src=\"".$iconpath."\" class=\"activityicon\" alt=\"\"/>";
+ }
+ $coursename = $DB->get_field('course', 'fullname', array('id' => $listing->courseid));
$courseword = mb_convert_case(get_string('course', 'moodle'), MB_CASE_LOWER, 'UTF-8');
+ $course = ($listing->doctype != 'user') ? '<strong> ('.$courseword.': \''.$coursename.'\')</strong>' : '' ;
+
$title_post_processing_function = $listing->doctype.'_link_post_processing';
$searchable_instance = $searchables[$listing->doctype];
if ($searchable_instance->location == 'internal'){
if (function_exists($title_post_processing_function)) {
$listing->title = $title_post_processing_function($listing->title);
}
-
- echo "<li value='".($listing->number+1)."'><a href='"
+
+ echo "<li value='".($listing->number + 1)."'><a href='"
.str_replace('DEFAULT_POPUP_SETTINGS', DEFAULT_POPUP_SETTINGS ,$listing->url)
- ."'><img align=\"top\" src=\"".$iconpath
- ."\" class=\"activityicon\" alt=\"\"/> $listing->title</a><strong> (".$courseword.": '".$coursename."')</strong><br />\n";
- // print "<li value='".($listing->number+1)."'><a href='".str_replace('DEFAULT_POPUP_SETTINGS', DEFAULT_POPUP_SETTINGS ,$listing->url)."'>$listing->title</a><br />\n"
- // ."<em>".search_shorten_url($listing->url, 70)."</em><br />\n"
- // ."{$typestr}: ".$listing->doctype.", {$scorestr}: ".round($listing->score, 3);
- if (!empty($listing->author)){
+ ."'>$icon $listing->title</a> $course<br />\n";
+ echo "{$typestr}: " . $listing->doctype . ", {$scorestr}: " . round($listing->score, 3);
+ if (!empty($listing->author) && !is_numeric($listing->author)){
echo ", {$authorstr}: ".$listing->author."\n"
."</li>\n";
}
- }
+ }
echo "</ol>";
echo $page_links;
- }
+ }
print_box_end();
?>
<div class="mdl-align">
<?php
print_string('ittook', 'search');
- search_stopwatch();
+ search_stopwatch();
print_string('tofetchtheseresults', 'search');
?>.
</div>
-
+
<?php
}
print_box_end();
print_footer();
-?>
+?>
\ No newline at end of file
* @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
*
* Index asynchronous updator
* 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;
-/// checks global search activation
+/// makes inclusions of the Zend Engine more reliable
+ ini_set('include_path', $CFG->dirroot.PATH_SEPARATOR.'search'.PATH_SEPARATOR.ini_get('include_path'));
- require_login();
+ 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());
}
+ */
try {
$index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
}
$dbcontrol = new IndexDBControl();
$update_count = 0;
- $indexdate = $CFG->search_indexer_update_date;
+ $indexdate = @$CFG->search_indexer_update_date;
$startupdatedate = time();
/// indexing changed resources
-
+
mtrace("Starting index update (updates)...\n");
-
- 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) {
$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();
-
+
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) and function_exists($get_document_function)) {
mtrace("Checking $mod->name module for updates.");
$valuesArray = $db_names_function();
if ($valuesArray){
foreach($valuesArray as $values){
-
- $where = (isset($values[5])) ? 'AND ('.$values[5].')' : '';
+
+ $where = (!empty($values[5])) ? 'AND ('.$values[5].')' : '';
$itemtypes = ($values[4] != '*' && $values[4] != 'any') ? " AND itemtype = '{$values[4]}' " : '' ;
-
+
//TODO: check 'in' syntax with other RDBMS' (add and update.php as well)
$table = SEARCH_DATABASE_TABLE;
$query = "
- SELECT
+ SELECT
docid,
itemtype
- FROM
- {" . $table . "}
+ FROM
+ {{$table}}
WHERE
doctype = ?
$itemtypes
";
- $docIds = $DB->get_records_sql_menu($query, array($mod->name));
- $docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
-
- $query = "
- SELECT
- id,
- {$values[0]} as docid
- FROM
- {" . $values[1] . "}
- WHERE
- {$values[3]} > {$indexdate} AND
- id IN ('{$docIdList}')
+ $docIds = $DB->get_records_sql_menu($query, array($values[1]));
+
+ if (!empty($docIds)){
+ list($usql, $params) = $DB->get_in_or_equal(array_keys($docIds));
+ $query = "
+ SELECT
+ id,
+ $values[0] as docid
+ FROM
+ {{$values[1]}}
+ WHERE
+ $values[3] > $indexdate AND
+ id $usql
$where
- ";
- $records = get_records_sql($query);
- if (is_array($records)) {
- foreach($records as $record) {
- $updates[] = $delete_function($record->docid, $docIds[$record->docid]);
- }
+ ";
+ $records = $DB->get_records_sql($query, $params);
+ } else {
+ $records = array();
}
- }
+ foreach($records as $record) {
+ $updates[] = $delete_function($record->docid, $docIds[$record->docid]);
+ }
+ }
+
foreach ($updates as $update) {
++$update_count;
-
+
//delete old document
$doc = $index->find("+docid:{$update->id} +doctype:{$mod->name} +itemtype:{$update->itemtype}");
-
+
//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);
- }
-
+ }
+
//add new modified document back into index
$add = $get_document_function($update->id, $update->itemtype);
-
+
//object to insert into db
$dbid = $dbcontrol->addDocument($add);
-
+
//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);
- }
+ }
}
else{
mtrace("No types to update.\n");
}
mtrace("Finished $mod->name.\n");
- }
- }
- }
- }
-
+ }
+ }
+ }
+ }
+
//commit changes
$index->commit();
-
+
//update index date
- set_config("search_indexer_update_date", $startupdatedate);
-
+ set_config('search_indexer_update_date', $startupdatedate);
+
mtrace("Finished $update_count updates");
-?>
+?>
\ No newline at end of file