From b02f17540195e1c9f933b40b0176a6f01bf1be6d Mon Sep 17 00:00:00 2001 From: brettp Date: Sun, 8 Nov 2009 00:09:09 +0000 Subject: Added super-basic support for searching comments. git-svn-id: http://code.elgg.org/elgg/trunk@3636 36083f99-b078-4883-b0ff-0f9b5a30f544 --- mod/search/search_hooks.php | 78 +++++++++++++++++++++- mod/search/start.php | 33 +++++---- .../views/default/search/comments/listing.php | 34 ++++++++++ mod/search/views/default/search/listing.php | 2 + mod/search/views/default/search/tags/listing.php | 2 +- 5 files changed, 133 insertions(+), 16 deletions(-) create mode 100644 mod/search/views/default/search/comments/listing.php (limited to 'mod') diff --git a/mod/search/search_hooks.php b/mod/search/search_hooks.php index abe85d962..89e7ecce0 100644 --- a/mod/search/search_hooks.php +++ b/mod/search/search_hooks.php @@ -30,7 +30,7 @@ function search_objects_hook($hook, $type, $value, $params) { //@todo allow sorting by recent time $params['order_by'] = NULL; - +var_dump($params); $entities = elgg_get_entities($params); $params['count'] = TRUE; $count = elgg_get_entities($params); @@ -203,3 +203,79 @@ function search_custom_types_tags_hook($hook, $type, $value, $params) { return $value; } + +/** + * Return default results for searches on comments. + * + * @param unknown_type $hook + * @param unknown_type $type + * @param unknown_type $value + * @param unknown_type $params + * @return unknown_type + */ +function search_comments_hook($hook, $type, $value, $params) { + global $CONFIG; + + $query = $params['query']; + $params['annotation_names'] = array('generic_comment', 'group_topic_post'); + + $params['joins'] = array( + "JOIN {$CONFIG->dbprefix}annotations a on e.guid = a.entity_guid", + "JOIN {$CONFIG->dbprefix}metastrings msn on a.name_id = msn.id", + "JOIN {$CONFIG->dbprefix}metastrings msv on a.value_id = msv.id" + ); + + $fields = array('string'); + $search_where = search_get_where_sql('msv', $fields, $params); + + $e_access = get_access_sql_suffix('e'); + $a_access = get_access_sql_suffix('a'); + // @todo this can probably be done through the api.. + $q = "SELECT DISTINCT a.*, msv.string as comment FROM {$CONFIG->dbprefix}annotations a + JOIN {$CONFIG->dbprefix}metastrings msn ON a.name_id = msn.id + JOIN {$CONFIG->dbprefix}metastrings msv ON a.value_id = msv.id + JOIN {$CONFIG->dbprefix}entities e ON a.entity_guid = e.guid + WHERE msn.string IN ('generic_comment', 'group_topic_post') + AND ($search_where) + AND $e_access + AND $a_access + + LIMIT {$params['offset']}, {$params['limit']} + "; + $comments = get_data($q); + + // need to return actual entities + // add the volatile data for why these entities have been returned. + $entities = array(); + foreach ($comments as $comment) { + $tags = implode(',', $entity->tags); + if (!$entity = get_entity($comment->entity_guid)) { + continue; + } + $comment_str = search_get_relevant_substring($comment->comment, $query, '', ''); + $entity->setVolatileData('search_matched_comment', $comment_str); + $entity->setVolatileData('search_matched_comment_owner_guid', $comment->owner_guid); + $entity->setVolatileData('search_matched_comment_time_created', $comment->time_created); + $entities[] = $entity; + } + + return array( + 'entities' => $entities, + 'count' => count($entities), + ); +} + +/** + * Register comments as a custom search type. + * + * @param unknown_type $hook + * @param unknown_type $type + * @param unknown_type $value + * @param unknown_type $params + * @return unknown_type + */ +function search_custom_types_comments_hook($hook, $type, $value, $params) { + $value[] = 'comments'; + return $value; +} + diff --git a/mod/search/start.php b/mod/search/start.php index 47405450a..ad9604cb6 100644 --- a/mod/search/start.php +++ b/mod/search/start.php @@ -19,17 +19,20 @@ function search_init() { // page handler for search actions and results register_page_handler('search','search_page_handler'); - // register some default search hooks - register_plugin_hook('search', 'object', 'search_objects_hook'); - register_plugin_hook('search', 'user', 'search_users_hook'); - - // @todo pull this out into groups - register_plugin_hook('search', 'group', 'search_groups_hook'); - - // tags are a bit different. - // register a custom search type and a hook for that. - register_plugin_hook('search_types', 'get_types', 'search_custom_types_tags_hook'); - register_plugin_hook('search', 'tags', 'search_tags_hook'); +// // register some default search hooks +// register_plugin_hook('search', 'object', 'search_objects_hook'); +// register_plugin_hook('search', 'user', 'search_users_hook'); +// +// // @todo pull this out into groups +// register_plugin_hook('search', 'group', 'search_groups_hook'); +// +// // tags and comments are a bit different. +// // register a search types and a hooks for them. +// register_plugin_hook('search_types', 'get_types', 'search_custom_types_tags_hook'); +// register_plugin_hook('search', 'tags', 'search_tags_hook'); + + register_plugin_hook('search_types', 'get_types', 'search_custom_types_comments_hook'); + register_plugin_hook('search', 'comments', 'search_comments_hook'); // get server min and max allowed chars for ft searching $word_lens = get_data('SELECT @@ft_min_word_len as min, @@ft_max_word_len as max'); @@ -327,7 +330,8 @@ function search_get_where_sql($table, $fields, $params) { $likes[] = "$field LIKE '%$query%'"; } $likes_str = implode(' OR ', $likes); - $where = "($table.guid = e.guid AND ($likes_str))"; + //$where = "($table.guid = e.guid AND ($likes_str))"; + $where = "($likes_str))"; } else { // if using advanced or paired "s, switch into boolean mode if ((isset($params['advanced_search']) && $params['advanced_search']) || substr_count($query, '"') >= 2 ) { @@ -338,11 +342,12 @@ function search_get_where_sql($table, $fields, $params) { // if short query, use query expansion. if (strlen($query) < 6) { - $options .= ' WITH QUERY EXPANSION'; + //$options .= ' WITH QUERY EXPANSION'; } // if query is shorter than the ft_min_word_len switch to literal mode. $fields_str = implode(',', $fields); - $where = "($table.guid = e.guid AND (MATCH ($fields_str) AGAINST ('$query' $options)))"; + //$where = "($table.guid = e.guid AND (MATCH ($fields_str) AGAINST ('$query' $options)))"; + $where = "(MATCH ($fields_str) AGAINST ('$query' $options))"; } return $where; diff --git a/mod/search/views/default/search/comments/listing.php b/mod/search/views/default/search/comments/listing.php new file mode 100644 index 000000000..ba9b7ea6e --- /dev/null +++ b/mod/search/views/default/search/comments/listing.php @@ -0,0 +1,34 @@ +getOwnerEntity()) { + $owner_icon = $owner->getIcon('tiny'); + $icon = ""; + } else { + $icon = ''; + } + $title = "Comment on " . elgg_echo('item:' . $entity->getType() . ':' . $entity->getSubtype()); + $description = $entity->getVolatileData('search_matched_comment'); + $url = $entity->getURL(); + $title = "$title"; + $tc = $entity->getVolatileData('search_matched_comment_time_created');; + $time = friendly_time($tc); + + echo <<<___END + +

$title

+ + $description
+ $icon $time - $more +
+
+___END; +} \ No newline at end of file diff --git a/mod/search/views/default/search/listing.php b/mod/search/views/default/search/listing.php index 270e33267..23a6a44e8 100644 --- a/mod/search/views/default/search/listing.php +++ b/mod/search/views/default/search/listing.php @@ -26,6 +26,8 @@ $query = htmlspecialchars(http_build_query( array( 'q' => $vars['params']['query'], 'type' => $vars['params']['type'], + 'limit' => get_input('limit', 10), + 'offset' => get_input('offset', 0), 'subtype' => $vars['params']['subtype'] ) )); diff --git a/mod/search/views/default/search/tags/listing.php b/mod/search/views/default/search/tags/listing.php index a5a33c4a4..9b229b349 100644 --- a/mod/search/views/default/search/tags/listing.php +++ b/mod/search/views/default/search/tags/listing.php @@ -1,6 +1,6 @@