Commit 17d1527e authored by Dries's avatar Dries

- Patch #146466 by douggreen, Steven et al: Remove temporary table usage from search module

parent f68f5254
......@@ -567,7 +567,7 @@ function drupal_redirect_form($form, $redirect = NULL) {
function _form_validate($elements, &$form_state, $form_id = NULL) {
// Also used in the installer, pre-database setup.
$t = get_t();
// Recurse through all children.
foreach (element_children($elements) as $key) {
if (isset($elements[$key]) && $elements[$key]) {
......@@ -1974,7 +1974,7 @@ function theme_file($element) {
function theme_form_element($element, $value) {
// This is also used in the installer, pre-database setup.
$t = get_t();
$output = '<div class="form-item"';
if (!empty($element['#id'])) {
$output .= ' id="'. $element['#id'] .'-wrapper"';
......
......@@ -10,7 +10,7 @@ Drupal.behaviors.teaser = function(context) {
if (/KDE/.test(navigator.vendor)) {
return;
}
$('textarea.teaser:not(.teaser-processed)', context).each(function() {
var teaser = $(this).addClass('teaser-processed');
......
......@@ -1017,15 +1017,12 @@ function node_search($op = 'search', $keys = NULL) {
return t('Content');
case 'reset':
variable_del('node_cron_last');
variable_del('node_cron_last_nid');
db_query("UPDATE {search_dataset} SET reindex = %d AND type = 'node'", time());
return;
case 'status':
$last = variable_get('node_cron_last', 0);
$last_nid = variable_get('node_cron_last_nid', 0);
$total = db_result(db_query('SELECT COUNT(*) FROM {node} WHERE status = 1'));
$remaining = db_result(db_query('SELECT COUNT(*) FROM {node} n LEFT JOIN {node_comment_statistics} c ON n.nid = c.nid WHERE n.status = 1 AND ((GREATEST(n.created, n.changed, c.last_comment_timestamp) = %d AND n.nid > %d ) OR (n.created > %d OR n.changed > %d OR c.last_comment_timestamp > %d))', $last, $last_nid, $last, $last, $last));
$remaining = db_result(db_query("SELECT COUNT(*) FROM {node} n LEFT JOIN {search_dataset} d ON d.type = 'node' AND d.sid = n.nid WHERE d.sid IS NULL OR d.reindex <> 0"));
return array('remaining' => $remaining, 'total' => $total);
case 'admin':
......@@ -1097,7 +1094,7 @@ function node_search($op = 'search', $keys = NULL) {
$ranking[] = '%d * POW(2, (GREATEST(n.created, n.changed, c.last_comment_timestamp) - %d) * 6.43e-8)';
$arguments2[] = $weight;
$arguments2[] = (int)variable_get('node_cron_last', 0);
$join2 .= ' INNER JOIN {node} n ON n.nid = i.sid LEFT JOIN {node_comment_statistics} c ON c.nid = i.sid';
$join2 .= ' LEFT JOIN {node_comment_statistics} c ON c.nid = i.sid';
$stats_join = TRUE;
$total += $weight;
}
......@@ -1546,60 +1543,50 @@ function node_page_view($node, $cid = NULL) {
return node_show($node, $cid);
}
/**
* shutdown function to make sure we always mark the last node processed.
*/
function node_update_shutdown() {
global $last_change, $last_nid;
if ($last_change && $last_nid) {
variable_set('node_cron_last', $last_change);
variable_set('node_cron_last_nid', $last_nid);
}
}
/**
* Implementation of hook_update_index().
*/
function node_update_index() {
global $last_change, $last_nid;
register_shutdown_function('node_update_shutdown');
$last = variable_get('node_cron_last', 0);
$last_nid = variable_get('node_cron_last_nid', 0);
$limit = (int)variable_get('search_cron_limit', 100);
// Store the maximum possible comments per thread (used for ranking by reply count)
variable_set('node_cron_comments_scale', 1.0 / max(1, db_result(db_query('SELECT MAX(comment_count) FROM {node_comment_statistics}'))));
variable_set('node_cron_views_scale', 1.0 / max(1, db_result(db_query('SELECT MAX(totalcount) FROM {node_counter}'))));
$result = db_query_range('SELECT GREATEST(IF(c.last_comment_timestamp IS NULL, 0, c.last_comment_timestamp), n.changed) as last_change, n.nid FROM {node} n LEFT JOIN {node_comment_statistics} c ON n.nid = c.nid WHERE n.status = 1 AND ((GREATEST(n.changed, c.last_comment_timestamp) = %d AND n.nid > %d) OR (n.changed > %d OR c.last_comment_timestamp > %d)) ORDER BY GREATEST(n.changed, c.last_comment_timestamp) ASC, n.nid ASC', $last, $last_nid, $last, $last, 0, $limit);
$result = db_query_range("SELECT n.nid FROM {node} n LEFT JOIN {search_dataset} d ON d.type = 'node' AND d.sid = n.nid WHERE d.sid IS NULL OR d.reindex <> 0 ORDER BY d.reindex ASC, n.nid ASC", 0, $limit);
while ($node = db_fetch_object($result)) {
$last_change = $node->last_change;
$last_nid = $node->nid;
$node = node_load($node->nid);
_node_index_node($node);
}
}
// Build the node body.
$node->build_mode = NODE_BUILD_SEARCH_INDEX;
$node = node_build_content($node, FALSE, FALSE);
$node->body = drupal_render($node->content);
/**
* Index a single node.
*
* @param $node
* The node to index.
*/
function _node_index_node($node) {
$node = node_load($node->nid);
// Allow modules to modify the fully-built node.
node_invoke_nodeapi($node, 'alter');
// save the changed time of the most recent indexed node, for the search results half-life calculation
variable_set('node_cron_last', $node->changed);
$text = '<h1>'. check_plain($node->title) .'</h1>'. $node->body;
// Build the node body.
$node->build_mode = NODE_BUILD_SEARCH_INDEX;
$node = node_build_content($node, FALSE, FALSE);
$node->body = drupal_render($node->content);
// Fetch extra data normally not visible
$extra = node_invoke_nodeapi($node, 'update index');
foreach ($extra as $t) {
$text .= $t;
}
$text = '<h1>'. check_plain($node->title) .'</h1>'. $node->body;
// Update index
search_index($node->nid, 'node', $text);
// Fetch extra data normally not visible
$extra = node_invoke_nodeapi($node, 'update index');
foreach ($extra as $t) {
$text .= $t;
}
// Update index
search_index($node->nid, 'node', $text);
}
/**
......
......@@ -47,6 +47,13 @@ function search_schema() {
'size' => 'big',
'description' => t('List of space-separated words from the item.'),
),
'reindex' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => t('Set to force node reindexing.'),
),
),
'indexes' => array('sid_type' => array('sid', 'type')),
);
......@@ -74,19 +81,6 @@ function search_schema() {
'not null' => FALSE,
'description' => t('The {search_dataset}.type of the searchable item to which the word belongs.'),
),
'fromsid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => t('The {search_dataset}.sid of the referring link to this item.'),
),
'fromtype' => array(
'type' => 'varchar',
'length' => 16,
'not null' => FALSE,
'description' => t('The {search_dataset}.type of the referring link to this item.'),
),
'score' => array(
'type' => 'float',
'not null' => FALSE,
......@@ -94,10 +88,10 @@ function search_schema() {
),
),
'indexes' => array(
'from_sid_type' => array('fromsid', 'fromtype'),
'sid_type' => array('sid', 'type'),
'word' => array('word')
),
'unique keys' => array('word_sid_type' => array('word', 'sid', 'type')),
);
$schema['search_total'] = array(
......@@ -119,6 +113,40 @@ function search_schema() {
'primary key' => array('word'),
);
$schema['search_node_links'] = array(
'fields' => array(
'sid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => t('The {search_dataset}.sid of the searchable item to which the word belongs.'),
),
'type' => array(
'type' => 'varchar',
'length' => 16,
'not null' => TRUE,
'default' => '',
'description' => t('The {search_dataset}.sid of the searchable item to which the word belongs.'),
),
'nid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => t('The {node}.nid that this item links to.'),
),
'caption' => array(
'type' => 'text',
'size' => 'big',
'not null' => FALSE,
'description' => t('The text used to link to the {node}.nid.'),
),
),
'primary key' => array('sid', 'type', 'nid'),
'indexes' => array('nid' => array('nid')),
);
return $schema;
}
This diff is collapsed.
......@@ -4466,6 +4466,64 @@ function system_update_6035() {
return $ret;
}
/**
* Change the search index and for reindexing.
*/
function system_update_6036() {
$ret = array();
if (db_table_exists('search_index')) {
if ($GLOBALS['db_type'] == 'mysql') {
// Create the search_dataset.reindex column.
db_add_field($ret, 'search_dataset', 'reindex', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
// Drop the search_index.from fields which are no longer used.
db_drop_index($ret, 'search_index', 'from_sid_type');
db_drop_field($ret, 'search_index', 'fromsid');
db_drop_field($ret, 'search_index', 'fromtype');
// Drop the search_dataset.sid_type index, so that it can be made unique.
db_drop_index($ret, 'search_dataset', 'sid_type');
// Create the search_node_links Table.
$search_node_links_schema = array(
'fields' => array(
'sid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
'type' => array('type' => 'varchar', 'length' => 16, 'not null' => TRUE, 'default' => ''),
'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
'caption' => array('type' => 'text', 'size' => 'big', 'not null' => FALSE),
),
'primary key' => array('sid', 'type', 'nid'),
'indexes' => array('nid' => array('nid')),
);
db_create_table($ret, 'search_node_links', $search_node_links_schema);
// with the change to search_dataset.reindex, the search queue is handled differently,
// and this is no longer needed
variable_del('node_cron_last');
// Everything needs to be reindexed.
$ret[] = update_sql("UPDATE {search_dataset} SET reindex = 1");
// Add a unique index for the search_index.
// Since it's possible that some existing sites have duplicates,
// create the index using the IGNORE keyword, which ignores duplicate errors.
// However, pgsql doesn't support it
$ret[] = update_sql("ALTER IGNORE TABLE {search_index} ADD UNIQUE KEY sid_word_type (sid, word, type)");
$ret[] = update_sql("ALTER IGNORE TABLE {search_dataset} ADD UNIQUE KEY sid_type (sid, type)");
}
else {
// Drop the existing tables
db_query('DROP TABLE {search_dataset}');
db_query('DROP TABLE {search_index}');
db_query('DROP TABLE {search_total}');
// Create the new tables and do a full re-index
search_install();
}
}
return $ret;
}
/**
* @} End of "defgroup updates-5.x-to-6.x"
* The next series of updates should start at 7000.
......
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