Commit 1dbe0dc2 authored by Dries's avatar Dries

- Patch #14917 by UnConeD/Steven:

1) The different types of search, which used to be radio button options in the search form, are now subtabs of "search" (default "search/node"). This seems better from a UI point of view, but also has another advantage: modules which implement a custom search form (flexinode, project) can add it as a subtab of search. This means that all search forms will be located in the same place, and also without needing an extra api call to search.module.

2) The current code was a bit hackish, as the indexing of comments along with nodes was hardcoded in node.module. Instead, I created a nodeapi operation "update index" which allows modules to add more data for a node that is being indexed. Comments are now indexed using this mechanism and from comment.module, which is a lot cleaner.

3) The search results format was also hardcoded to include "N comments". I replaced this with a nodeapi operation "search result" and moved the comment code to comment.module where it belongs. This op is quite useful, as for example I also modified upload.module to add "N attachments" to a search result if any are present.
parent 828be2ad
......@@ -261,6 +261,16 @@ function comment_nodeapi(&$node, $op, $arg = 0) {
db_query('DELETE FROM {comments} WHERE nid = %d', $node->nid);
db_query('DELETE FROM {node_comment_statistics} WHERE nid = %d', $node->nid);
break;
case 'update index':
$text = '';
$comments = db_query('SELECT subject, comment, format FROM {comments} WHERE nid = %d AND status = 0', $node->nid);
while ($comment = db_fetch_object($comments)) {
$text .= '<h2>'. $comment->subject .'</h2>'. check_output($comment->comment, $comment->format);
}
return $text;
case 'search result':
$comments = db_result(db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = %d', $node->nid));
return format_plural($comments, '1 comment', '%count comments');
}
}
......
......@@ -261,6 +261,16 @@ function comment_nodeapi(&$node, $op, $arg = 0) {
db_query('DELETE FROM {comments} WHERE nid = %d', $node->nid);
db_query('DELETE FROM {node_comment_statistics} WHERE nid = %d', $node->nid);
break;
case 'update index':
$text = '';
$comments = db_query('SELECT subject, comment, format FROM {comments} WHERE nid = %d AND status = 0', $node->nid);
while ($comment = db_fetch_object($comments)) {
$text .= '<h2>'. $comment->subject .'</h2>'. check_output($comment->comment, $comment->format);
}
return $text;
case 'search result':
$comments = db_result(db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = %d', $node->nid));
return format_plural($comments, '1 comment', '%count comments');
}
}
......
......@@ -582,13 +582,13 @@ function node_search($op = 'search', $keys = null) {
$results = array();
foreach ($find as $item) {
$node = node_load(array('nid' => $item));
$comments = db_result(db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = %d', $item));
$extra = node_invoke_nodeapi($node, 'search result');
$results[] = array('link' => url('node/'. $item),
'type' => node_invoke($node, 'node_name'),
'title' => $node->title,
'user' => format_name($node),
'date' => $node->changed,
'extra' => format_plural($comments, '1 comment', '%count comments'),
'extra' => $extra,
'snippet' => search_excerpt($keys, check_output($node->body, $node->format)));
}
return $results;
......@@ -1588,12 +1588,10 @@ function node_update_index() {
$text = '<h1>'. drupal_specialchars($node->title) .'</h1>'. $node->body;
// Fetch comments
if (module_exist('comment')) {
$comments = db_query('SELECT subject, comment, format FROM {comments} WHERE nid = %d AND status = 0', $node->nid);
while ($comment = db_fetch_object($comments)) {
$text .= '<h2>'. $comment->subject .'</h2>'. check_output($comment->comment, $comment->format);
}
// Fetch extra data normally not visible
$extra = node_invoke_nodeapi($node, 'update index');
foreach ($extra as $t) {
$text .= $t;
}
// Update index
......
......@@ -582,13 +582,13 @@ function node_search($op = 'search', $keys = null) {
$results = array();
foreach ($find as $item) {
$node = node_load(array('nid' => $item));
$comments = db_result(db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = %d', $item));
$extra = node_invoke_nodeapi($node, 'search result');
$results[] = array('link' => url('node/'. $item),
'type' => node_invoke($node, 'node_name'),
'title' => $node->title,
'user' => format_name($node),
'date' => $node->changed,
'extra' => format_plural($comments, '1 comment', '%count comments'),
'extra' => $extra,
'snippet' => search_excerpt($keys, check_output($node->body, $node->format)));
}
return $results;
......@@ -1588,12 +1588,10 @@ function node_update_index() {
$text = '<h1>'. drupal_specialchars($node->title) .'</h1>'. $node->body;
// Fetch comments
if (module_exist('comment')) {
$comments = db_query('SELECT subject, comment, format FROM {comments} WHERE nid = %d AND status = 0', $node->nid);
while ($comment = db_fetch_object($comments)) {
$text .= '<h2>'. $comment->subject .'</h2>'. check_output($comment->comment, $comment->format);
}
// Fetch extra data normally not visible
$extra = node_invoke_nodeapi($node, 'update index');
foreach ($extra as $t) {
$text .= $t;
}
// Update index
......
......@@ -74,12 +74,20 @@ function search_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array('path' => 'search', 'title' => t('search'),
$items[] = array('path' => 'search', 'title' => t('search for'),
'callback' => 'search_view',
'access' => user_access('search content'),
'type' => MENU_SUGGESTED_ITEM);
$items[] = array('path' => 'search/search', 'title' => t('search'),
'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
foreach (module_list() as $name) {
if (module_hook($name, 'search')) {
$items[] = array('path' => 'search/'. $name, 'title' => module_invoke($name, 'search', 'name'),
'callback' => 'search_view',
'access' => user_access('search content'),
'type' => $name == 'node' ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK);
}
}
$items[] = array('path' => 'admin/settings/search', 'title' => t('search'),
'callback' => 'search_admin',
'type' => MENU_NORMAL_ITEM,
......@@ -465,7 +473,7 @@ function do_search($keys, $type, $join = '', $where = '1') {
*/
function search_view() {
$keys = isset($_GET['keys']) ? $_GET['keys'] : $_POST['edit']['keys'];
$type = isset($_GET['type']) ? $_GET['type'] : ($_POST['edit']['type'] ? $_POST['edit']['type'] : 'node');
$type = arg(1) ? arg(1) : (isset($_GET['type']) ? $_GET['type'] : ($_POST['edit']['type'] ? $_POST['edit']['type'] : 'node'));
if (user_access('search content')) {
// Only perform search if there is non-whitespace search term:
......@@ -538,6 +546,9 @@ function search_form($action = '', $keys = '', $type = null, $options = FALSE) {
if (!$action) {
$action = url('search');
}
if (!$type) {
$type = 'node';
}
$output = ' <div class="search-form">';
$box = '<div class="container-inline">';
......@@ -545,24 +556,7 @@ function search_form($action = '', $keys = '', $type = null, $options = FALSE) {
$box .= form_submit(t('Search'));;
$box .= '</div>';
$output .= form_item(t('Enter your keywords'), $box);
if ($options) {
$output .= '<div class="container-inline">'. t('Search for') .': ';
if (!isset($edit['type'])) {
$edit['type'] = $type;
}
foreach (module_list() as $name) {
if (module_hook($name, 'search')) {
$output .= form_radio(module_invoke($name, 'search', 'name'), 'type', $name, $edit['type'] == $name);
}
}
$output .= '</div>';
}
else if ($type) {
$output .= form_hidden('type', $type);
}
$output .= form_hidden('type', $type);
$output .= '</div>';
return form($output, 'post', $action);
......@@ -712,7 +706,9 @@ function _search_excerpt_replace($text) {
*
* @param $item
* A single search result as returned by hook_search(). The result should be
* an array with keys "count", "link", "title", "user", "date", and "keywords".
* an array with keys "link", "title", "type", "user", "date", and "snippet".
* Optionally, "extra" can be an array of extra info to show along with the
* result.
* @param $type
* The type of item found, such as "user" or "comment".
*/
......@@ -732,8 +728,8 @@ function theme_search_item($item, $type) {
if ($item['date']) {
$info[] = format_date($item['date'], 'small');
}
if (isset($item['extra'])) {
$info[] = $item['extra'];
if (is_array($item['extra'])) {
$info = array_merge($info, $item['extra']);
}
$output .= ' <dd>'. ($item['snippet'] ? '<p>'. $item['snippet'] . '</p>' : '') . '<p class="search-info">' . implode(' - ', $info) .'</p></dd>';
}
......
......@@ -74,12 +74,20 @@ function search_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array('path' => 'search', 'title' => t('search'),
$items[] = array('path' => 'search', 'title' => t('search for'),
'callback' => 'search_view',
'access' => user_access('search content'),
'type' => MENU_SUGGESTED_ITEM);
$items[] = array('path' => 'search/search', 'title' => t('search'),
'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
foreach (module_list() as $name) {
if (module_hook($name, 'search')) {
$items[] = array('path' => 'search/'. $name, 'title' => module_invoke($name, 'search', 'name'),
'callback' => 'search_view',
'access' => user_access('search content'),
'type' => $name == 'node' ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK);
}
}
$items[] = array('path' => 'admin/settings/search', 'title' => t('search'),
'callback' => 'search_admin',
'type' => MENU_NORMAL_ITEM,
......@@ -465,7 +473,7 @@ function do_search($keys, $type, $join = '', $where = '1') {
*/
function search_view() {
$keys = isset($_GET['keys']) ? $_GET['keys'] : $_POST['edit']['keys'];
$type = isset($_GET['type']) ? $_GET['type'] : ($_POST['edit']['type'] ? $_POST['edit']['type'] : 'node');
$type = arg(1) ? arg(1) : (isset($_GET['type']) ? $_GET['type'] : ($_POST['edit']['type'] ? $_POST['edit']['type'] : 'node'));
if (user_access('search content')) {
// Only perform search if there is non-whitespace search term:
......@@ -538,6 +546,9 @@ function search_form($action = '', $keys = '', $type = null, $options = FALSE) {
if (!$action) {
$action = url('search');
}
if (!$type) {
$type = 'node';
}
$output = ' <div class="search-form">';
$box = '<div class="container-inline">';
......@@ -545,24 +556,7 @@ function search_form($action = '', $keys = '', $type = null, $options = FALSE) {
$box .= form_submit(t('Search'));;
$box .= '</div>';
$output .= form_item(t('Enter your keywords'), $box);
if ($options) {
$output .= '<div class="container-inline">'. t('Search for') .': ';
if (!isset($edit['type'])) {
$edit['type'] = $type;
}
foreach (module_list() as $name) {
if (module_hook($name, 'search')) {
$output .= form_radio(module_invoke($name, 'search', 'name'), 'type', $name, $edit['type'] == $name);
}
}
$output .= '</div>';
}
else if ($type) {
$output .= form_hidden('type', $type);
}
$output .= form_hidden('type', $type);
$output .= '</div>';
return form($output, 'post', $action);
......@@ -712,7 +706,9 @@ function _search_excerpt_replace($text) {
*
* @param $item
* A single search result as returned by hook_search(). The result should be
* an array with keys "count", "link", "title", "user", "date", and "keywords".
* an array with keys "link", "title", "type", "user", "date", and "snippet".
* Optionally, "extra" can be an array of extra info to show along with the
* result.
* @param $type
* The type of item found, such as "user" or "comment".
*/
......@@ -732,8 +728,8 @@ function theme_search_item($item, $type) {
if ($item['date']) {
$info[] = format_date($item['date'], 'small');
}
if (isset($item['extra'])) {
$info[] = $item['extra'];
if (is_array($item['extra'])) {
$info = array_merge($info, $item['extra']);
}
$output .= ' <dd>'. ($item['snippet'] ? '<p>'. $item['snippet'] . '</p>' : '') . '<p class="search-info">' . implode(' - ', $info) .'</p></dd>';
}
......
......@@ -257,6 +257,8 @@ function upload_nodeapi(&$node, $op, $arg) {
case 'delete':
upload_delete($node);
break;
case 'search result':
return $node->files ? format_plural(count($node->files), '1 attachment', '%count attachments') : null;
}
return $output;
......
......@@ -257,6 +257,8 @@ function upload_nodeapi(&$node, $op, $arg) {
case 'delete':
upload_delete($node);
break;
case 'search result':
return $node->files ? format_plural(count($node->files), '1 attachment', '%count attachments') : null;
}
return $output;
......
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