diff options
author | Dries Buytaert <dries@buytaert.net> | 2009-08-29 21:05:16 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2009-08-29 21:05:16 +0000 |
commit | 44063e1cf19ad80f1a7e72b2c7e70a305297043b (patch) | |
tree | b067173596c69a197986f39c62c8e3c9c16d0574 /modules/node/node.module | |
parent | d4d43f11eacc7f566d612a19c176a8c4ac867df6 (diff) | |
download | brdo-44063e1cf19ad80f1a7e72b2c7e70a305297043b.tar.gz brdo-44063e1cf19ad80f1a7e72b2c7e70a305297043b.tar.bz2 |
- Patch #505214 by pwolanin: make the search module a slightly better framework.
Diffstat (limited to 'modules/node/node.module')
-rw-r--r-- | modules/node/node.module | 216 |
1 files changed, 119 insertions, 97 deletions
diff --git a/modules/node/node.module b/modules/node/node.module index ac5d3efaa..9a4efdfff 100644 --- a/modules/node/node.module +++ b/modules/node/node.module @@ -1249,110 +1249,132 @@ function _node_rankings(SelectQueryExtender $query) { } /** - * Implement hook_search(). + * Implement hook_search_info(). */ -function node_search($op = 'search', $keys = NULL) { - switch ($op) { - case 'name': - return t('Content'); - - case 'reset': - db_update('search_dataset') - ->fields(array('reindex' => REQUEST_TIME)) - ->condition('type', 'node') - ->execute(); - return; - - case 'status': - $total = db_query('SELECT COUNT(*) FROM {node} WHERE status = 1')->fetchField(); - $remaining = db_query("SELECT COUNT(*) FROM {node} n LEFT JOIN {search_dataset} d ON d.type = 'node' AND d.sid = n.nid WHERE n.status = 1 AND d.sid IS NULL OR d.reindex <> 0")->fetchField(); - return array('remaining' => $remaining, 'total' => $total); - - case 'admin': - $form = array(); - // Output form for defining rank factor weights. - $form['content_ranking'] = array( - '#type' => 'fieldset', - '#title' => t('Content ranking'), - ); - $form['content_ranking']['#theme'] = 'node_search_admin'; - $form['content_ranking']['info'] = array( - '#value' => '<em>' . t('The following numbers control which properties the content search should favor when ordering the results. Higher numbers mean more influence, zero means the property is ignored. Changing these numbers does not require the search index to be rebuilt. Changes take effect immediately.') . '</em>' - ); +function node_search_info() { + return array( + 'title' => 'Content', + 'path' => 'node', + ); +} - // Note: reversed to reflect that higher number = higher ranking. - $options = drupal_map_assoc(range(0, 10)); - foreach (module_invoke_all('ranking') as $var => $values) { - $form['content_ranking']['factors']['node_rank_' . $var] = array( - '#title' => $values['title'], - '#type' => 'select', - '#options' => $options, - '#default_value' => variable_get('node_rank_' . $var, 0), - ); - } - return $form; +/** + * Implement hook_search_access(). + */ +function node_search_access() { + return user_access('access content'); +} - case 'search': - // Build matching conditions - $query = db_select('search_index', 'i')->extend('SearchQuery')->extend('PagerDefault'); - $query->join('node', 'n', 'n.nid = i.sid'); - $query - ->condition('n.status', 1) - ->addTag('node_access') - ->searchExpression($keys, 'node'); - - // Insert special keywords. - $query->setOption('type', 'n.type'); - $query->setOption('language', 'n.language'); - if ($query->setOption('term', 'tn.nid')) { - $query->join('taxonomy_term_node', 'tn', 'n.vid = tn.vid'); - } - // Only continue if the first pass query matches. - if (!$query->executeFirstPass()) { - return array(); - } +/** + * Implement hook_search_reset(). + */ +function node_search_reset() { + db_update('search_dataset') + ->fields(array('reindex' => REQUEST_TIME)) + ->condition('type', 'node') + ->execute(); +} - // Add the ranking expressions. - _node_rankings($query); +/** + * Implement hook_search_status(). + */ +function node_search_status() { + $total = db_query('SELECT COUNT(*) FROM {node} WHERE status = 1')->fetchField(); + $remaining = db_query("SELECT COUNT(*) FROM {node} n LEFT JOIN {search_dataset} d ON d.type = 'node' AND d.sid = n.nid WHERE n.status = 1 AND d.sid IS NULL OR d.reindex <> 0")->fetchField(); + return array('remaining' => $remaining, 'total' => $total); +} - // Add a count query. - $inner_query = clone $query; - $count_query = db_select($inner_query->fields('i', array('sid'))); - $count_query->addExpression('COUNT(*)'); - $query->setCountQuery($count_query); - $find = $query - ->limit(10) - ->execute(); +/** + * Implement hook_search_admin(). + */ +function node_search_admin() { + $form = array(); + // Output form for defining rank factor weights. + $form['content_ranking'] = array( + '#type' => 'fieldset', + '#title' => t('Content ranking'), + ); + $form['content_ranking']['#theme'] = 'node_search_admin'; + $form['content_ranking']['info'] = array( + '#value' => '<em>' . t('The following numbers control which properties the content search should favor when ordering the results. Higher numbers mean more influence, zero means the property is ignored. Changing these numbers does not require the search index to be rebuilt. Changes take effect immediately.') . '</em>' + ); + + // Note: reversed to reflect that higher number = higher ranking. + $options = drupal_map_assoc(range(0, 10)); + foreach (module_invoke_all('ranking') as $var => $values) { + $form['content_ranking']['factors']['node_rank_' . $var] = array( + '#title' => $values['title'], + '#type' => 'select', + '#options' => $options, + '#default_value' => variable_get('node_rank_' . $var, 0), + ); + } + return $form; +} - // Load results. - $results = array(); - foreach ($find as $item) { - // Render the node. - $node = node_load($item->sid); - $node = node_build_content($node, 'search_result'); - $node->rendered = drupal_render($node->content); - - // Fetch comments for snippet. - $node->rendered .= ' ' . module_invoke('comment', 'node_update_index', $node); - // Fetch terms for snippet. - $node->rendered .= ' ' . module_invoke('taxonomy', 'node_update_index', $node); - - $extra = module_invoke_all('node_search_result', $node); - - $results[] = array( - 'link' => url('node/' . $item->sid, array('absolute' => TRUE)), - 'type' => check_plain(node_type_get_name($node)), - 'title' => $node->title, - 'user' => theme('username', $node), - 'date' => $node->changed, - 'node' => $node, - 'extra' => $extra, - 'score' => $item->calculated_score, - 'snippet' => search_excerpt($keys, $node->rendered), - ); - } - return $results; +/** + * Implement hook_search_execute(). + */ +function node_search_execute($keys = NULL) { + // Build matching conditions + $query = db_select('search_index', 'i')->extend('SearchQuery')->extend('PagerDefault'); + $query->join('node', 'n', 'n.nid = i.sid'); + $query + ->condition('n.status', 1) + ->addTag('node_access') + ->searchExpression($keys, 'node'); + + // Insert special keywords. + $query->setOption('type', 'n.type'); + $query->setOption('language', 'n.language'); + if ($query->setOption('term', 'tn.nid')) { + $query->join('taxonomy_term_node', 'tn', 'n.vid = tn.vid'); + } + // Only continue if the first pass query matches. + if (!$query->executeFirstPass()) { + return array(); + } + + // Add the ranking expressions. + _node_rankings($query); + + // Add a count query. + $inner_query = clone $query; + $count_query = db_select($inner_query->fields('i', array('sid'))); + $count_query->addExpression('COUNT(*)'); + $query->setCountQuery($count_query); + $find = $query + ->limit(10) + ->execute(); + + // Load results. + $results = array(); + foreach ($find as $item) { + // Render the node. + $node = node_load($item->sid); + $node = node_build_content($node, 'search_result'); + $node->rendered = drupal_render($node->content); + + // Fetch comments for snippet. + $node->rendered .= ' ' . module_invoke('comment', 'node_update_index', $node); + // Fetch terms for snippet. + $node->rendered .= ' ' . module_invoke('taxonomy', 'node_update_index', $node); + + $extra = module_invoke_all('node_search_result', $node); + + $results[] = array( + 'link' => url('node/' . $item->sid, array('absolute' => TRUE)), + 'type' => check_plain(node_type_get_name($node)), + 'title' => $node->title, + 'user' => theme('username', $node), + 'date' => $node->changed, + 'node' => $node, + 'extra' => $extra, + 'score' => $item->calculated_score, + 'snippet' => search_excerpt($keys, $node->rendered), + ); } + return $results; } /** |