Commit b6eee48c authored by Dries's avatar Dries

- Patch #12232 by Steven: more search improvements:

   + When a comment is posted, a node needs to be re-indexed. Luckily, we can use node_comment_statistics for this easily.
   + When a node is deleted, it should be deleted from the search index as well.
   + The search wipe didn't properly remove links to nodes from the index.
   + Section url was faulty in _help.
   + Minor code rearrangement.
parent d13943e2
......@@ -541,6 +541,7 @@ CREATE TABLE search_index (
fromtype varchar(16) default NULL,
score int(10) unsigned default NULL,
KEY sid (sid),
KEY fromsid (fromsid),
KEY word (word)
) TYPE=MyISAM;
......
......@@ -1940,6 +1940,7 @@ function update_110() {
fromtype varchar(16) default NULL,
score int(10) unsigned default NULL,
KEY sid (sid),
KEY fromsid (fromsid),
KEY word (word)
) TYPE=MyISAM");
......
......@@ -1409,6 +1409,9 @@ function node_delete($edit) {
// Clear the cache so an anonymous poster can see the node being deleted.
cache_clear_all();
// Remove this node from the search index
search_wipe($node->nid, 'node');
watchdog('special', t('%type: deleted %title.', array('%type' => '<em>'. t($node->type) .'</em>', '%title' => "<em>$node->title</em>")));
$output = t('The node has been deleted.');
}
......@@ -1515,7 +1518,7 @@ function node_update_index() {
$last = variable_get('node_cron_last', 0);
$limit = (int)variable_get('search_cron_limit', 100);
$result = db_query_range('SELECT nid FROM {node} n WHERE n.status = 1 AND moderate = 0 AND (created > %d OR changed > %d) ORDER BY GREATEST(created, changed) ASC', $last, $last, 0, $limit);
$result = db_query_range('SELECT n.nid FROM {node} n LEFT JOIN {node_comment_statistics} c ON n.nid = c.nid WHERE n.status = 1 AND n.moderate = 0 AND (n.created > %d OR n.changed > %d OR c.last_comment_timestamp > %d) ORDER BY GREATEST(n.created, n.changed, c.last_comment_timestamp) ASC', $last, $last, $last, 0, $limit);
while ($node = db_fetch_object($result)) {
$node = node_load(array('nid' => $node->nid));
......
......@@ -1409,6 +1409,9 @@ function node_delete($edit) {
// Clear the cache so an anonymous poster can see the node being deleted.
cache_clear_all();
// Remove this node from the search index
search_wipe($node->nid, 'node');
watchdog('special', t('%type: deleted %title.', array('%type' => '<em>'. t($node->type) .'</em>', '%title' => "<em>$node->title</em>")));
$output = t('The node has been deleted.');
}
......@@ -1515,7 +1518,7 @@ function node_update_index() {
$last = variable_get('node_cron_last', 0);
$limit = (int)variable_get('search_cron_limit', 100);
$result = db_query_range('SELECT nid FROM {node} n WHERE n.status = 1 AND moderate = 0 AND (created > %d OR changed > %d) ORDER BY GREATEST(created, changed) ASC', $last, $last, 0, $limit);
$result = db_query_range('SELECT n.nid FROM {node} n LEFT JOIN {node_comment_statistics} c ON n.nid = c.nid WHERE n.status = 1 AND n.moderate = 0 AND (n.created > %d OR n.changed > %d OR c.last_comment_timestamp > %d) ORDER BY GREATEST(n.created, n.changed, c.last_comment_timestamp) ASC', $last, $last, $last, 0, $limit);
while ($node = db_fetch_object($result)) {
$node = node_load(array('nid' => $node->nid));
......
......@@ -47,8 +47,10 @@ function search_help($section = 'admin/help#search') {
switch ($section) {
case 'admin/modules#description':
return t('Enables site-wide keyword searching.');
case 'admin/search':
return t('<p>The search engine works by maintaining an index of words in your site\'s content. You can adjust the settings below to tweak the indexing behaviour. Note that indexing requires cron to be set up correctly.</p><p>Changes to these settings will only apply to content that is indexed after the change. If you want them to apply to everything, you need to wipe the index with the button below.</p>');
case 'admin/settings/search':
return t('
<p>The search engine works by maintaining an index of the words in your site\'s content. You can adjust the settings below to tweak the indexing behaviour. Note that the search requires cron to be set up correctly.</p>
<p>Changes to these settings will only apply to content that is indexed after the change. If you want them to apply to everything, you need to wipe the index with the button below.</p>');
case 'search#noresults':
return t('<p><ul>
<li>Check if your spelling is correct.</li>
......@@ -97,6 +99,7 @@ function search_menu($may_cache) {
'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
$items[] = array('path' => 'admin/settings/search', 'title' => t('search'),
'callback' => 'search_admin',
'type' => MENU_NORMAL_ITEM,
'access' => user_access('administer site configuration'));
}
......@@ -112,6 +115,8 @@ function search_admin() {
}
if ($_POST['op'] == t('Wipe index')) {
search_wipe();
drupal_set_message(t('The search index has been wiped.'));
drupal_goto('admin/settings/search');
}
// Indexing settings:
......@@ -133,15 +138,25 @@ function search_admin() {
}
/**
* Wipe the search index.
* Wipes a part of or the entire search index.
*
* @param $sid
* (optional) The SID of the item to wipe. If specified, $type must be passed
* too.
* @param $type
* (optional) The type of item to wipe.
*
*/
function search_wipe() {
db_query('DELETE FROM {search_index}');
db_query('DELETE FROM {search_total}');
module_invoke_all('search', 'reset');
drupal_set_message(t('The search index has been wiped.'));
drupal_goto('admin/settings/search');
function search_wipe($type = NULL, $sid = NULL) {
if ($type == NULL && $sid == NULL) {
db_query('DELETE FROM {search_index}');
db_query('DELETE FROM {search_total}');
module_invoke_all('search', 'reset');
}
else {
db_query("DELETE FROM {search_index} WHERE sid = %d AND type = '%s'", $sid, $type);
db_query("DELETE FROM {search_index} WHERE fromsid = %d AND fromtype = '%s'", $sid, $type);
}
}
/**
......@@ -356,7 +371,7 @@ function search_index($sid, $type, $text) {
$tag = !$tag;
}
db_query("DELETE FROM {search_index} WHERE sid = %d AND type = '%s'", $sid, $type);
search_wipe($sid, $type);
// Insert results into search index
foreach ($results[0] as $word => $score) {
......
......@@ -47,8 +47,10 @@ function search_help($section = 'admin/help#search') {
switch ($section) {
case 'admin/modules#description':
return t('Enables site-wide keyword searching.');
case 'admin/search':
return t('<p>The search engine works by maintaining an index of words in your site\'s content. You can adjust the settings below to tweak the indexing behaviour. Note that indexing requires cron to be set up correctly.</p><p>Changes to these settings will only apply to content that is indexed after the change. If you want them to apply to everything, you need to wipe the index with the button below.</p>');
case 'admin/settings/search':
return t('
<p>The search engine works by maintaining an index of the words in your site\'s content. You can adjust the settings below to tweak the indexing behaviour. Note that the search requires cron to be set up correctly.</p>
<p>Changes to these settings will only apply to content that is indexed after the change. If you want them to apply to everything, you need to wipe the index with the button below.</p>');
case 'search#noresults':
return t('<p><ul>
<li>Check if your spelling is correct.</li>
......@@ -97,6 +99,7 @@ function search_menu($may_cache) {
'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
$items[] = array('path' => 'admin/settings/search', 'title' => t('search'),
'callback' => 'search_admin',
'type' => MENU_NORMAL_ITEM,
'access' => user_access('administer site configuration'));
}
......@@ -112,6 +115,8 @@ function search_admin() {
}
if ($_POST['op'] == t('Wipe index')) {
search_wipe();
drupal_set_message(t('The search index has been wiped.'));
drupal_goto('admin/settings/search');
}
// Indexing settings:
......@@ -133,15 +138,25 @@ function search_admin() {
}
/**
* Wipe the search index.
* Wipes a part of or the entire search index.
*
* @param $sid
* (optional) The SID of the item to wipe. If specified, $type must be passed
* too.
* @param $type
* (optional) The type of item to wipe.
*
*/
function search_wipe() {
db_query('DELETE FROM {search_index}');
db_query('DELETE FROM {search_total}');
module_invoke_all('search', 'reset');
drupal_set_message(t('The search index has been wiped.'));
drupal_goto('admin/settings/search');
function search_wipe($type = NULL, $sid = NULL) {
if ($type == NULL && $sid == NULL) {
db_query('DELETE FROM {search_index}');
db_query('DELETE FROM {search_total}');
module_invoke_all('search', 'reset');
}
else {
db_query("DELETE FROM {search_index} WHERE sid = %d AND type = '%s'", $sid, $type);
db_query("DELETE FROM {search_index} WHERE fromsid = %d AND fromtype = '%s'", $sid, $type);
}
}
/**
......@@ -356,7 +371,7 @@ function search_index($sid, $type, $text) {
$tag = !$tag;
}
db_query("DELETE FROM {search_index} WHERE sid = %d AND type = '%s'", $sid, $type);
search_wipe($sid, $type);
// Insert results into search index
foreach ($results[0] as $word => $score) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment