Commit 8daed9cb authored by Dries's avatar Dries

- Patch #12232 by Steven/UnConed: search module improvements.

1) Clean up the text analyser: make it handle UTF-8 and all sorts of characters. The word splitter now does intelligent splitting into words and supports all Unicode characters. It has smart handling of acronyms, URLs, dates, ...

2) It now indexes the filtered output, which means it can take advantage of HTML tags. Meaningful tags (headers, strong, em, ...) are analysed and used to boost certain words scores. This has the side-effect of allowing the indexing of PHP nodes.

3) Link analyser for node links. The HTML analyser also checks for links. If they point to a node on the current site (handles path aliases) then the link's words are counted as part of the target node. This helps bring out commonly linked FAQs and answers to the top of the results.

4) Index comments along with the node. This means that the search can make a difference between a single node/comment about 'X' and a whole thread about 'X'. It also makes the search results much shorter and more relevant (before this patch, comments were even shown first).

5) We now keep track of total counts as well as a per item count for a word. This allows us to divide the word score by the total before adding up the scores for different words, and automatically makes noisewords have less influence than rare words. This dramatically improves the relevancy of multiword searches. This also makes the disadvantage of now using OR searching instead of AND searching less problematic.

6) Includes support for text preprocessors through a hook. This is required to index Chinese and Japanese, because these languages do not use spaces between words. An external utility can be used to split these into words through a simple wrapper module. Other uses could be spell checking (although it would have no UI).

7) Indexing is now regulated: only a certain amount of items will be indexed per cron run. This prevents PHP from running out of memory or timing out. This also makes the reindexing required for this patch automatic. I also added an index coverage estimate to the search admin screen.

8) Code cleanup! Moved all the search stuff from common.inc into search.module, rewired some hooks and simplified the functions used. The search form and results now also use valid XHTML and form_ functions. The search admin was moved from search/configure to admin/search for consistency.

9) Improved search output: we also show much more info per item: date, author, node type, amount of comments and a cool dynamic excerpt à la Google. The search form is now much more simpler and the help is only displayed as tips when no search results are found.

10) By moving all search logic to SQL, I was able to add a pager to the search results. This improves usability and performance dramatically.
parent 83dc5f9b
Drupal x.x.x, xxxx-xx-xx
------------------------
- search:
* added UTF-8 support to make it work with all languages.
* improved search indexing.
* imrpoved search output.
Drupal 4.5.0, 2004-10-18
------------------------
- navigation:
......
......@@ -534,10 +534,22 @@ CREATE TABLE role (
CREATE TABLE search_index (
word varchar(50) NOT NULL default '',
lno int(10) unsigned NOT NULL default '0',
sid int(10) unsigned NOT NULL default '0',
type varchar(16) default NULL,
fromsid int(10) unsigned NOT NULL default '0',
fromtype varchar(16) default NULL,
score int(10) unsigned default NULL,
KEY sid (sid),
KEY word (word)
) TYPE=MyISAM;
--
-- Table structure for table 'search_total'
--
CREATE TABLE search_total (
word varchar(50) NOT NULL default '',
count int(10) unsigned default NULL,
KEY lno (lno),
KEY word (word)
) TYPE=MyISAM;
......
......@@ -85,7 +85,8 @@
"2004-09-15" => "update_106",
"2004-09-17" => "update_107",
"2004-10-16" => "update_108",
"2004-10-18" => "update_109"
"2004-10-18" => "update_109",
"2004-10-28: first update since Drupal 4.5.0 release" => "update_110"
);
function update_32() {
......@@ -1921,6 +1922,34 @@ function update_109() {
return $ret;
}
function update_110() {
$ret = array();
// TODO: needs PGSQL version
if ($GLOBALS['db_type'] == 'mysql') {
$ret[] = update_sql('DROP TABLE {search_index}');
$ret[] = update_sql("CREATE TABLE {search_index} (
word varchar(50) NOT NULL default '',
sid int(10) unsigned NOT NULL default '0',
type varchar(16) default NULL,
fromsid int(10) unsigned NOT NULL default '0',
fromtype varchar(16) default NULL,
score int(10) unsigned default NULL,
KEY sid (sid),
KEY word (word)
) TYPE=MyISAM");
$ret[] = update_sql("CREATE TABLE {search_total} (
word varchar(50) NOT NULL default '',
count int(10) unsigned default NULL,
KEY word (word)
) TYPE=MyISAM");
$ret[] = update_sql("DELETE FROM {variable} WHERE name = 'node_cron_last'");
}
return $ret;
}
function update_sql($sql) {
$edit = $_POST["edit"];
......
......@@ -668,134 +668,6 @@ function valid_input_data($data) {
* @} End of "defgroup validation".
*/
/**
* @defgroup search Search interface
* @{
* The Drupal search interface manages a global search mechanism.
*
* Modules may plug into this system to provide searches of different types of
* data. Most of the system is handled by search.module, so this must be enabled
* for all of the search features to work.
*/
/**
* Format a single result entry of a search query.
*
* Modules may implement hook_search_item() in order to override this default
* function to display search results.
*
* @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".
* @param $type
* The type of item found, such as "user" or "comment".
*/
function search_item($item, $type) {
if (module_hook($type, 'search_item')) {
$output = module_invoke($type, 'search_item', $item);
}
else {
$output = ' <dt class="title"><a href="'. $item['link'] .'">'. $item['title'] .'</a></dt>';
$output .= ' <dd class="small">' . t($type) . ($item['user'] ? ' - '. $item['user'] : '') .''. ($item['date'] ? ' - '. format_date($item['date'], 'small') : '') .'</dd>';
}
return $output;
}
/**
* Render a generic search form.
*
* This form must be usable not only within "http://example.com/search", but also
* as a simple search box (without "Restrict search to", help text, etc.), in the
* theme's header, and so forth. This means we must provide options to
* conditionally render certain parts of this form.
*
* @param $action
* Form action. Defaults to "search".
* @param $keys
* The search string entered by the user, containing keywords for the search.
* @param $options
* Whether to render the optional form fields and text ("Restrict search
* to", help text, etc.).
* @return
* An HTML string containing the search form.
*/
function search_form($action = '', $keys = '', $options = FALSE) {
$edit = $_POST['edit'];
if (!$action) {
$action = url('search');
}
$output = ' <div class="search-form"><br /><input type="text" class="form-text" size="50" value="'. check_form($keys) .'" name="keys" />';
$output .= ' <input type="submit" class="form-submit" value="'. t('Search') ."\" />\n";
if ($options) {
$output .= '<br />';
$output .= t('Restrict search to') .': ';
foreach (module_list() as $name) {
if (module_hook($name, 'search')) {
$output .= ' <input type="checkbox" name="edit[type]['. $name .']" '. ($edit['type'][$name] ? ' checked="checked"' : '') .' /> '. t($name);
}
}
}
$output .= '</div>';
return form($output, 'post', $action);
}
/**
* Perform a global search on the given keys, and return the formatted results.
*/
function search_data($keys = NULL) {
$edit = $_POST['edit'];
$output = '';
if (isset($keys)) {
foreach (module_list() as $name) {
if (module_hook($name, 'search') && (!$edit['type'] || $edit['type'][$name])) {
list($title, $results) = module_invoke($name, 'search', $keys);
if ($results) {
$output .= '<h2>'. $title .'</h2>';
$output .= '<dl class="search-results">';
foreach ($results as $entry) {
$output .= search_item($entry, $name);
}
$output .= '</dl>';
}
}
}
}
return $output;
}
/**
* Display a search form for a particular type of data.
*
* @param $type
* The type of content to search within.
* @param $action
* Form action. Defaults to "search".
* @param $keys
* The search string entered by the user, containing keywords for the search.
* @param $options
* Whether to render the optional form fields and text ("Restrict search
* to", help text, etc.).
* @return
* An HTML string containing the search form and results.
*/
function search_type($type, $action = '', $keys = '', $options = FALSE) {
$_POST['edit']['type'][$type] = 'on';
return search_form($action, $keys, $options) . '<br />'. search_data($keys);
}
/**
* @} End of "defgroup search".
*/
function check_form($text) {
return drupal_specialchars($text, ENT_QUOTES);
}
......@@ -1840,7 +1712,7 @@ function truncate_utf8($string, $len) {
/**
* Encodes MIME/HTTP header values that contain non US-ASCII characters.
*
* For example, mime_header_encode('tést.txt') returns "=?UTF-8?B?dMOpc3QudHh0?=".
* For example, mime_header_encode('tést.txt') returns "=?UTF-8?B?dMOpc3QudHh0?=".
*
* See http://www.rfc-editor.org/rfc/rfc2047.txt for more information.
*
......@@ -1862,6 +1734,59 @@ function mime_header_encode($string, $charset = 'UTF-8') {
return $string;
}
/**
* Decode all HTML entities (including numerical ones) to regular UTF-8 bytes.
*/
function decode_entities($text) {
static $table;
// We store named entities in a table for quick processing.
if (!isset($table)) {
// Get all named HTML entities.
$table = array_flip(get_html_translation_table(HTML_ENTITIES, $special));
// PHP gives us Windows-1252/ISO-8859-1 data, we need UTF-8.
$table = array_map('utf8_encode', $table);
}
$text = strtr($text, $table);
// Any remaining entities are numerical. Use a regexp to replace them.
return preg_replace('/&#(x?)([A-Za-z0-9]+);/e', '_decode_entities("$1", "$2")', $text);
}
/**
* Helper function for decode_entities
*/
function _decode_entities($hex, $codepoint) {
if ($hex != '') {
$codepoint = base_convert($codepoint, 16, 10);
}
if ($codepoint < 0x80) {
return chr($codepoint);
}
else if ($codepoint < 0x800) {
return chr(0xC0 | ($codepoint >> 6))
. chr(0x80 | ($codepoint & 0x3F));
}
else if ($codepoint < 0x10000) {
return chr(0xE0 | ( $codepoint >> 12))
. chr(0x80 | (($codepoint >> 6) & 0x3F))
. chr(0x80 | ( $codepoint & 0x3F));
}
else if ($codepoint < 0x200000) {
return chr(0xF0 | ( $codepoint >> 18))
. chr(0x80 | (($codepoint >> 12) & 0x3F))
. chr(0x80 | (($codepoint >> 6) & 0x3F))
. chr(0x80 | ( $codepoint & 0x3F));
}
}
/**
* Count the amount of characters in a UTF-8 string. This is less than or
* equal to the byte count.
*/
function string_length(&$text) {
return strlen(preg_replace("/[\x80-\xBF]/", '', $text));
}
/**
* Evaluate a string of PHP code.
*
......
......@@ -383,6 +383,31 @@ img.screenshot {
display: block;
margin: 2px;
}
.search-form {
margin-bottom: 1em;
}
.search-form p {
margin-top: 0;
margin-bottom: 0.2em;
padding-top: 0;
padding-bottom: 0;
}
.search-form input {
margin-top: 0;
margin-bottom: 0;
}
.search-results p {
margin-top: 0;
}
.search-results dt {
font-size: 1.1em;
}
.search-results dd {
margin-bottom: 1em;
}
.search-results .search-info {
font-size: 0.85em;
}
#tracker td.replies {
text-align: center;
}
......
......@@ -74,8 +74,6 @@ function comment_help($section = "admin/help#comment") {
return t("Use these forms to setup the name and minimum \"cut off\" score to help your users hide comments they don't want to see. These thresholds appear in the user's comment control panel. Click \"edit threshold\" to modify the values of an already existing configuration. To delete a setting, \"edit\" it first, and then choose \"delete threshold\".");
case 'admin/comment/configure/votes':
return t("Create and control the possible comment moderation votes here. \"Weight\" lets you set the order of the drop down menu. Click \"edit\" to edit a current vote weight. To delete a name/weight combination go to the \"edit\" area. To delete a setting, \"edit\" it first, and then choose \"delete vote\".");
case 'admin/comment/search':
return t("Enter a simple pattern ('*' may be used as a wildcard match) to search for a comment. For example, one may search for 'br' and Drupal might return 'bread brakers', 'our daily bread' and 'brenda'.");
case 'admin/modules#description':
return t('Enables user to comment on published content.');
}
......@@ -101,10 +99,6 @@ function comment_menu($may_cache) {
'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
$items[] = array('path' => 'admin/comment/configure', 'title' => t('configure'),
'callback' => 'comment_configure', 'access' => $access, 'type' => MENU_LOCAL_TASK);
if (module_exist('search')) {
$items[] = array('path' => 'admin/comment/search', 'title' => t('search'),
'callback' => 'comment_search', 'access' => $access, 'type' => MENU_LOCAL_TASK);
}
// Subtabs:
$items[] = array('path' => 'admin/comment/list/new', 'title' => t('new comments'),
......@@ -267,37 +261,6 @@ function comment_nodeapi(&$node, $op, $arg = 0) {
}
}
/**
* Implementation of hook_search().
*
* This search function uses search.module's built-in content index by
* calling do_search(). The "nid" identifier in the select is used to
* present search results in the context of their associated node.
*
* This function doubles as a menu callback for the administrative comment search.
*/
function comment_search($keys = NULL) {
if (!$keys) {
// if there are no keys, we've been called from our menu callback,
// so we hook into the search.module to pass the $keys back to us.
print theme('page', search_type('comment', url('admin/comment/search'), $_POST['keys']));
}
else if ($keys) {
$find = do_search(array('keys' => $keys, 'type' => 'comment', 'select' => 'SELECT s.lno AS lno, c.nid AS nid, c.subject AS title, c.timestamp AS created, u.uid AS uid, u.name AS name, s.count AS count FROM {search_index} s, {comments} c INNER JOIN {users} u ON c.uid = u.uid '. node_access_join_sql('c') .' WHERE '. node_access_where_sql() ." AND s.lno = c.cid AND s.type = 'comment' AND c.status = 0 AND s.word like '%'"));
return array(t('Matching comments ranked in order of relevance'), $find);
}
}
/**
* Implementation of hook_update_index().
*
* The SQL statement returned checks for the last time the index was updated
* so as not to cause redundant work for the indexer.
*/
function comment_update_index() {
return array('last_update' => 'comment_cron_last', 'node_type' => 'comment', 'select' => 'SELECT c.cid as lno, c.subject as text1, c.comment as text2 FROM {comments} c WHERE c.status = 0 AND timestamp > '. variable_get('comment_cron_last', 1));
}
/**
* Implementation of hook_user().
*
......@@ -1250,7 +1213,7 @@ function comment_threshold_settings($fid = 0) {
}
/**
*** misc functions: helpers, privates, history, search
*** misc functions: helpers, privates, history
**/
......
......@@ -74,8 +74,6 @@ function comment_help($section = "admin/help#comment") {
return t("Use these forms to setup the name and minimum \"cut off\" score to help your users hide comments they don't want to see. These thresholds appear in the user's comment control panel. Click \"edit threshold\" to modify the values of an already existing configuration. To delete a setting, \"edit\" it first, and then choose \"delete threshold\".");
case 'admin/comment/configure/votes':
return t("Create and control the possible comment moderation votes here. \"Weight\" lets you set the order of the drop down menu. Click \"edit\" to edit a current vote weight. To delete a name/weight combination go to the \"edit\" area. To delete a setting, \"edit\" it first, and then choose \"delete vote\".");
case 'admin/comment/search':
return t("Enter a simple pattern ('*' may be used as a wildcard match) to search for a comment. For example, one may search for 'br' and Drupal might return 'bread brakers', 'our daily bread' and 'brenda'.");
case 'admin/modules#description':
return t('Enables user to comment on published content.');
}
......@@ -101,10 +99,6 @@ function comment_menu($may_cache) {
'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
$items[] = array('path' => 'admin/comment/configure', 'title' => t('configure'),
'callback' => 'comment_configure', 'access' => $access, 'type' => MENU_LOCAL_TASK);
if (module_exist('search')) {
$items[] = array('path' => 'admin/comment/search', 'title' => t('search'),
'callback' => 'comment_search', 'access' => $access, 'type' => MENU_LOCAL_TASK);
}
// Subtabs:
$items[] = array('path' => 'admin/comment/list/new', 'title' => t('new comments'),
......@@ -267,37 +261,6 @@ function comment_nodeapi(&$node, $op, $arg = 0) {
}
}
/**
* Implementation of hook_search().
*
* This search function uses search.module's built-in content index by
* calling do_search(). The "nid" identifier in the select is used to
* present search results in the context of their associated node.
*
* This function doubles as a menu callback for the administrative comment search.
*/
function comment_search($keys = NULL) {
if (!$keys) {
// if there are no keys, we've been called from our menu callback,
// so we hook into the search.module to pass the $keys back to us.
print theme('page', search_type('comment', url('admin/comment/search'), $_POST['keys']));
}
else if ($keys) {
$find = do_search(array('keys' => $keys, 'type' => 'comment', 'select' => 'SELECT s.lno AS lno, c.nid AS nid, c.subject AS title, c.timestamp AS created, u.uid AS uid, u.name AS name, s.count AS count FROM {search_index} s, {comments} c INNER JOIN {users} u ON c.uid = u.uid '. node_access_join_sql('c') .' WHERE '. node_access_where_sql() ." AND s.lno = c.cid AND s.type = 'comment' AND c.status = 0 AND s.word like '%'"));
return array(t('Matching comments ranked in order of relevance'), $find);
}
}
/**
* Implementation of hook_update_index().
*
* The SQL statement returned checks for the last time the index was updated
* so as not to cause redundant work for the indexer.
*/
function comment_update_index() {
return array('last_update' => 'comment_cron_last', 'node_type' => 'comment', 'select' => 'SELECT c.cid as lno, c.subject as text1, c.comment as text2 FROM {comments} c WHERE c.status = 0 AND timestamp > '. variable_get('comment_cron_last', 1));
}
/**
* Implementation of hook_user().
*
......@@ -1250,7 +1213,7 @@ function comment_threshold_settings($fid = 0) {
}
/**
*** misc functions: helpers, privates, history, search
*** misc functions: helpers, privates, history
**/
......
......@@ -553,29 +553,27 @@ function node_perm() {
/**
* Implementation of hook_search().
*
* Return the results of performing a search using the indexed search
* for this particular type of node.
*
* Pass an array to the 'do_search' function which dictates what it
* will search through, and what it will search for
*
* "keys"'s value is the keywords entered by the user
*
* "type"'s value is used to identify the node type in the search
* index.
*
* "select"'s value is used to relate the data from the specific nodes
* table to the data that the search_index table has in it, and the the
* do_search function will rank it.
*
* The select must always provide the following fields: lno, title,
* created, uid, name, and count.
*/
function node_search($keys) {
$find = do_search(array('keys' => $keys, 'type' => 'node', 'select' => "SELECT DISTINCT s.lno as lno, n.title as title, n.created as created, u.uid as uid, u.name as name, s.count as count FROM {search_index} s, {node} n ". node_access_join_sql() ." INNER JOIN {users} u ON n.uid = u.uid WHERE s.lno = n.nid AND s.type = 'node' AND s.word like '%' AND n.status = 1 AND ". node_access_where_sql()));
return array(t('Matching nodes ranked in order of relevance'), $find);
function node_search($op = 'search', $keys = null) {
switch ($op) {
case 'name':
return t('content');
case 'search':
$find = do_search($keys, 'node', 'INNER JOIN {node} n ON n.nid = i.sid '. node_access_join_sql() .' INNER JOIN {users} u ON n.uid = u.uid', 'n.status = 1 AND '. node_access_where_sql());
$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));
$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'),
'snippet' => search_excerpt($keys, check_output($node->body, $node->format)));
}
return $results;
}
}
/**
......@@ -947,7 +945,8 @@ function node_admin() {
// Compile a list of the administrative links:
switch ($op) {
case 'search':
$output = search_type('node', url('admin/node/search'), $_POST['keys']);
case t('Search'):
$output = search_form(url('admin/node/search'), $_POST['edit']['keys'], 'node') . search_data($_POST['edit']['keys'], 'node');
break;
case 'delete':
$output = node_delete(array('nid' => arg(3)));
......@@ -1508,24 +1507,42 @@ function node_page() {
/**
* Implementation of hook_update_index().
*
* Returns an array of values to dictate how to update the search index
* for this particular type of node.
*
* "last_update"'s value is used with variable_set to set the
* last time this node type had an index update run.
*
* "node_type"'s value is used to identify the node type in the search
* index.
*
* "select"'s value is used to select the node id and text fields from
* the table we are indexing. In this case, we also check against the
* last run date for the nodes update.
*/
function node_update_index() {
return array('last_update' => 'node_cron_last',
'node_type' => 'node',
'select' => "SELECT n.nid as lno, n.title as text1, n.body as text2 FROM {node} n WHERE n.status = 1 AND moderate = 0 and (created > " . variable_get('node_cron_last', 1) . " or changed > " . variable_get('node_cron_last', 1) . ")");
$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);
while ($node = db_fetch_object($result)) {
$node = node_load(array('nid' => $node->nid));
// We update this variable per node in case cron times out, or if the node
// cannot be indexed (PHP nodes which call drupal_goto, for example).
// In rare cases this can mean a node is only partially indexed, but the
// chances of this happening are very small.
variable_set('node_cron_last', max($node->changed, $node->created));
if (node_hook($node, 'view')) {
node_invoke($node, 'view', false, true);
}
else {
$node = node_prepare($node, false);
}
$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);
}
}
// Update index
search_index($node->nid, 'node', $text);
}
}
/**
......
......@@ -553,29 +553,27 @@ function node_perm() {
/**
* Implementation of hook_search().
*
* Return the results of performing a search using the indexed search
* for this particular type of node.
*
* Pass an array to the 'do_search' function which dictates what it
* will search through, and what it will search for
*
* "keys"'s value is the keywords entered by the user
*
* "type"'s value is used to identify the node type in the search
* index.
*
* "select"'s value is used to relate the data from the specific nodes
* table to the data that the search_index table has in it, and the the
* do_search function will rank it.
*
* The select must always provide the following fields: lno, title,
* created, uid, name, and count.
*/
function node_search($keys) {
$find = do_search(array('keys' => $keys, 'type' => 'node', 'select' => "SELECT DISTINCT s.lno as lno, n.title as title, n.created as created, u.uid as uid, u.name as name, s.count as count FROM {search_index} s, {node} n ". node_access_join_sql() ." INNER JOIN {users} u ON n.uid = u.uid WHERE s.lno = n.nid AND s.type = 'node' AND s.word like '%' AND n.status = 1 AND ". node_access_where_sql()));
return array(t('Matching nodes ranked in order of relevance'), $find);
function node_search($op = 'search', $keys = null) {
switch ($op) {
case 'name':
return t('content');
case 'search':
$find = do_search($keys, 'node', 'INNER JOIN {node} n ON n.nid = i.sid '. node_access_join_sql() .' INNER JOIN {users} u ON n.uid = u.uid', 'n.status = 1 AND '. node_access_where_sql());
$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));
$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'),
'snippet' => search_excerpt($keys, check_output($node->body, $node->format)));
}
return $results;
}
}
/**
......@@ -947,7 +945,8 @@ function node_admin() {
// Compile a list of the administrative links:
switch ($op) {
case 'search':
$output = search_type('node', url('admin/node/search'), $_POST['keys']);
case t('Search'):
$output = search_form(url('admin/node/search'), $_POST['edit']['keys'], 'node') . search_data($_POST['edit']['keys'], 'node');
break;
case 'delete':
$output = node_delete(array('nid' => arg(3)));
......@@ -1508,24 +1507,42 @@ function node_page() {
/**
* Implementation of hook_update_index().
*
* Returns an array of values to dictate how to update the search index
* for this particular type of node.
*
* "last_update"'s value is used with variable_set to set the
* last time this node type had an index update run.
*
* "node_type"'s value is used to identify the node type in the search
* index.
*
* "select"'s value is used to select the node id and text fields from
* the table we are indexing. In this case, we also check against the
* last run date for the nodes update.
*/
function node_update_index() {
return array('last_update' => 'node_cron_last',
'node_type' => 'node',
'select' => "SELECT n.nid as lno, n.title as text1, n.body as text2 FROM {node} n WHERE n.status = 1 AND moderate = 0 and (created > " . variable_get('node_cron_last', 1) . " or changed > " . variable_get('node_cron_last', 1) . ")");
$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);
while ($node = db_fetch_object($result)) {
$node = node_load(array('nid' => $node->nid));
// We update this variable per node in case cron times out, or if the node
// cannot be indexed (PHP nodes which call drupal_goto, for example).
// In rare cases this can mean a node is only partially indexed, but the
// chances of this happening are very small.
variable_set('node_cron_last', max($node->changed, $node->created));
if (node_hook($node, 'view')) {
node_invoke($node, 'view', false, true);
}
else {
$node = node_prepare($node, false);
}
$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);
}
}
// Update index
search_index($node->nid, 'node', $text);
}
}
/**
......
This diff is collapsed.
This diff is collapsed.
......@@ -431,17 +431,20 @@ function user_file_download($file) {
/**
* Implementation of hook_search().
*/
function user_search($keys) {
$find = array();
// Replace wildcards with MySQL/PostgreSQL wildcards.
$keys = str_replace('*', '%', $keys);
$result = db_query_range("SELECT * FROM {users} WHERE LOWER(name) LIKE '%%%s%%'", strtolower($keys), 0, 20);
while ($account = db_fetch_object($result)) {
$find[] = array('title' => $account->name, 'link' => url("user/$account->uid/view"), 'user' => $account->name);
function user_search($op = 'search', $keys = null) {
switch ($op) {
case 'name':
return t('users');
case 'search':
$find = array();
// Replace wildcards with MySQL/PostgreSQL wildcards.
$keys = str_replace('*', '%', $keys);
$result = db_query_range("SELECT * FROM {users} WHERE LOWER(name) LIKE '%%%s%%'", strtolower($keys), 0, 20);
while ($account = db_fetch_object($result)) {
$find[] = array('title' => $account->name, 'link' => url("user/$account->uid/view"));
}
return $find;
}
return array(t('Matching users'), $find);
}
/**
......@@ -1595,7 +1598,8 @@ function user_admin() {
switch ($op) {
case 'search':
$output = search_type('user', url('admin/user/search'), $_POST['keys']);
case t('Search'):
$output = search_form(url('admin/user/search'), $_POST['edit']['keys'], 'user') . search_data($_POST['edit']['keys'], 'user');
break;
case t('Create account'):