]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Added super-basic support for searching comments.
authorbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Sun, 8 Nov 2009 00:09:09 +0000 (00:09 +0000)
committerbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Sun, 8 Nov 2009 00:09:09 +0000 (00:09 +0000)
git-svn-id: http://code.elgg.org/elgg/trunk@3636 36083f99-b078-4883-b0ff-0f9b5a30f544

mod/search/search_hooks.php
mod/search/start.php
mod/search/views/default/search/comments/listing.php [new file with mode: 0644]
mod/search/views/default/search/listing.php
mod/search/views/default/search/tags/listing.php

index abe85d9625583e85c2d093aaeac2dfe4d3e6670f..89e7ecce085c11c0495ac3cdf40b5bc0cdd2e3dd 100644 (file)
@@ -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, '<strong class="searchMatch">', '</strong>');
+               $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;
+}
+
index 47405450a19dbc5e61bedd7f004d702fb6cceecd..ad9604cb698ba0724937eb0ad744bbb8e93361f3 100644 (file)
@@ -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 (file)
index 0000000..ba9b7ea
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+/**
+ * Elgg comments search listing
+ *
+ * @package Elgg
+ * @subpackage Core
+ * @author Curverider Ltd
+ * @link http://elgg.org/
+ */
+
+foreach ($vars['entities'] as $entity) {
+       if ($owner = $entity->getOwnerEntity()) {
+               $owner_icon = $owner->getIcon('tiny');
+               $icon = "<img src=\"$owner_icon\" />";
+       } else {
+               $icon = '';
+       }
+       $title = "Comment on " . elgg_echo('item:' . $entity->getType() . ':' . $entity->getSubtype());
+       $description = $entity->getVolatileData('search_matched_comment');
+       $url = $entity->getURL();
+       $title = "<a href=\"$url\">$title</a>";
+       $tc = $entity->getVolatileData('search_matched_comment_time_created');;
+       $time = friendly_time($tc);
+
+       echo <<<___END
+<span class="searchListing">
+       <h3 class="searchTitle">$title</h3>
+       <span class="searchDetails">
+               <span class="searchDescription">$description</span><br />
+               $icon $time - $more</a>
+       </span>
+</span>
+___END;
+}
\ No newline at end of file
index 270e33267c95370d2801502c45d7d198ffcb0b13..23a6a44e8497b676c46221c21f63089e478d7d76 100644 (file)
@@ -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']
        )
 ));
index a5a33c4a44699ade966a626e56a5b8157ffaa95c..9b229b3496f1281373ee7b20b06f11d5767ccb51 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * Elgg search listing
+ * Elgg tag search listing
  *
  * @package Elgg
  * @subpackage Core