Web Services - Elgg Search

I am trying to create the web services api for the elgg's search feature. I found the file \mod\search\pages\search\index.php and have modified the same as below:

<?php

function site_search($q, $offset = 0,$search_type = 'all', $limit = 20, $entity_type = ELGG_ENTITIES_ANY_VALUE, /* $owner_guid = ELGG_ENTITIES_ANY_VALUE, $container_guid = ELGG_ENTITIES_ANY_VALUE, $friends = ELGG_ENTITIES_ANY_VALUE,*/ $entity_subtype = ELGG_ENTITIES_ANY_VALUE, $sort = 'relevance', $order = 'desc')    {

$query = stripslashes($q);
$display_query = _elgg_get_display_query($query);

// check that we have an actual query
if (empty($query) && $query != "0") {
    $return['status'] = true;
    $return['output'] = elgg_echo('search:no_query');
    return $return;
}

// get limit and offset.  override if on search dashboard, where only 2
// of each most recent entity types will be shown.
$limit = ($search_type == 'all') ? 2 : elgg_get_config('default_limit');
$offset = ($search_type == 'all') ? 0 : $offset;

switch ($sort) {
    case 'relevance':
    case 'created':
    case 'updated':
    case 'action_on':
    case 'alpha':
        break;

    default:
        $sort = 'relevance';
        break;
}

if ($order != 'asc' && $order != 'desc') {
    $order = 'desc';
}

// set up search params
$params = array(
    'query' => $query,
    'offset' => $offset,
    'limit' => $limit,
    'sort' => $sort,
    'order' => $order,
    'search_type' => $search_type,
    'type' => $entity_type,
    'subtype' => $entity_subtype,
//    'tag_type' => $tag_type,
//    'owner_guid' => $owner_guid,
//    'container_guid' => $container_guid,
//    'friends' => $friends
    'pagination' => ($search_type == 'all') ? FALSE : TRUE
);

$types = get_registered_entity_types();
$types = elgg_trigger_plugin_hook('search_types', 'get_queries', $params, $types);

$custom_types = elgg_trigger_plugin_hook('search_types', 'get_types', $params, array());

// start the actual search
$results_html = '';

if ($search_type == 'all' || $search_type == 'entities') {
    // to pass the correct current search type to the views
    $current_params = $params;
    $current_params['search_type'] = 'entities';

    // foreach through types.
    // if a plugin returns FALSE for subtype ignore it.
    // if a plugin returns NULL or '' for subtype, pass to generic type search function.
    // if still NULL or '' or empty(array()) no results found. (== don't show??)
    foreach ($types as $type => $subtypes) {
        if ($search_type != 'all' && $entity_type != $type) {
            continue;
        }

        if (is_array($subtypes) && count($subtypes)) {
            foreach ($subtypes as $subtype) {
                // no need to search if we're not interested in these results
                // @todo when using index table, allow search to get full count.
                if ($search_type != 'all' && $entity_subtype != $subtype) {
                    continue;
                }
                $current_params['subtype'] = $subtype;
                $current_params['type'] = $type;

                $results = elgg_trigger_plugin_hook('search', "$type:$subtype", $current_params, NULL);
                if ($results === FALSE) {
                    // someone is saying not to display these types in searches.
                    continue;
                } elseif (is_array($results) && !count($results)) {
                    // no results, but results searched in hook.
                } elseif (!$results) {
                    // no results and not hooked.  use default type search.
                    // don't change the params here, since it's really a different subtype.
                    // Will be passed to elgg_get_entities().
                    $results = elgg_trigger_plugin_hook('search', $type, $current_params, array());
                }

                if (is_array($results['entities']) && $results['count']) {
                    if ($view = search_get_search_view($current_params, 'list')) {
                        $results_html .= elgg_view($view, array(
                            'results' => $results,
                            'params' => $current_params,
                        ));
                    }
                }
            }
        }

        // pull in default type entities with no subtypes
        $current_params['type'] = $type;
        $current_params['subtype'] = ELGG_ENTITIES_NO_VALUE;

        $results = elgg_trigger_plugin_hook('search', $type, $current_params, array());
        if ($results === FALSE) {
            // someone is saying not to display these types in searches.
            continue;
        }

        if (is_array($results['entities']) && $results['count']) {
            if ($view = search_get_search_view($current_params, 'list')) {
                $results_html .= elgg_view($view, array(
                    'results' => $results,
                    'params' => $current_params,
                ));
            }
        }
    }
}

// call custom searches
if ($search_type != 'entities' || $search_type == 'all') {
    if (is_array($custom_types)) {
        foreach ($custom_types as $type) {
            if ($search_type != 'all' && $search_type != $type) {
                continue;
            }

            $current_params = $params;
            $current_params['search_type'] = $type;

            $results = elgg_trigger_plugin_hook('search', $type, $current_params, array());

            if ($results === FALSE) {
                // someone is saying not to display these types in searches.
                continue;
            }

            if (is_array($results['entities']) && $results['count']) {
                if ($view = search_get_search_view($current_params, 'list')) {
                    $results_html .= elgg_view($view, array(
                        'results' => $results,
                        'params' => $current_params,
                    ));
                }
            }
        }
    }
}

// highlight search terms
if ($search_type == 'tags') {
    $searched_words = array($display_query);
} else {
    $searched_words = search_remove_ignored_words($display_query, 'array');
}

$highlighted_query = search_highlight_words($searched_words, $display_query);
$highlighted_title = elgg_echo('search:results', array("\"$highlighted_query\""));

if (!$results_html) {
    $return['status'] = false;
    $return['output'] = elgg_echo('No Results');
} else {
    $return['status'] = true;
    $return['output'] = $results_html;
}

return $return;

}

elgg_ws_expose_function('search.site',
    "site_search",
    array(
        'q' => array ('type' => 'string','required' => true),
        'offset' => array ('type' => 'int','required' => false),
        'search_type' => array ('type' => 'string','required' => false),
        'limit' => array ('type' => 'int','required' => false),
        'entity_type' => array ('type' => 'string','required' => false),
        'entity_subtype' => array ('type' => 'string','required' => false),
        'sort' => array ('type' => 'string','required' => false),
        'order' => array ('type' => 'string','required' => false),
    ),
    "Search the Site",
    'GET',
    false,
    false);

But when I tried to search a term like "admin", the website shows 3 result but I get the following as the API output.

{
    "status": 0,
    "result": {
      "status": false,
      "output": "No Results"
    },
    "runtime_errors": [
      "DEBUG: 2016-10-12 12:01:52 (CEST): \"Undefined index: metadata_values\" in file  
      C:\\xampp\\htdocs\\www\\ck12\\engine\\lib\\metastrings.php (line 692)",
      "DEBUG: 2016-10-12 12:01:52 (CEST): \"Undefined index: metadata_name_value_pairs\" in file 
      C:\\xampp\\htdocs\\www\\ck12\\engine\\lib\\metastrings.php (line 692)",
      "DEBUG: 2016-10-12 12:01:52 (CEST): \"Undefined index: metadata_name_value_pairs_operator\" in file 
      C:\\xampp\\htdocs\\www\\ck12\\engine\\lib\\metastrings.php (line 693)",
      "DEBUG: 2016-10-12 12:01:52 (CEST): \"Undefined index: metadata_case_sensitive\" in file 
      C:\\xampp\\htdocs\\www\\ck12\\engine\\lib\\metastrings.php (line 693)",
      "DEBUG: 2016-10-12 12:01:52 (CEST): \"Undefined index: order_by_metadata\" in file 
      C:\\xampp\\htdocs\\www\\ck12\\engine\\lib\\metastrings.php (line 694)",
      "DEBUG: 2016-10-12 12:01:52 (CEST): \"Undefined index: metadata_owner_guids\" in file 
      C:\\xampp\\htdocs\\www\\ck12\\engine\\lib\\metastrings.php (line 694)"
   ]
}

Can anyone help me, why can't I see any output in API.

 

  • Anyone? who can help me on this?

    if you think this is the wrong code. Can you help me with the "search" code.

  • 2 days, and still no reply.

    Can anyone help me with this?

  • I can't see anything obviously wrong with it, that said there's a lot there and it's very messy and there's various parts commented out, it looks like it would be easy to miss something small.

    error_log() your variables, find out which search hooks are actually firing with what parameters and compare it to the search page hooks/parameters.  If the same hooks are being triggered with the same params then it should be fine, I'm guessing you've unintentionally changed a parameter somewhere.

  • @Matt, I have managed to extract the search output but the same is returning an Elgg Object with protected parameters. Here's an example of 1 ElggObject:

         ElggUser Object
            (
                [url_override:protected] => 
                [temp_metadata:protected] => Array
                    (
                    )
    
                [temp_annotations:protected] => Array
                    (
                    )
    
                [temp_private_settings:protected] => Array
                    (
                    )
    
                [volatile:protected] => Array
                    (
                        [search_matched_title] => Admin (admin)
                        [search_matched_description] => 
                    )
    
                [orig_attributes:protected] => Array
                    (
                    )
    
                [attributes:protected] => Array
                    (
                        [guid] => 36
                        [type] => user
                        [subtype] => 0
                        [owner_guid] => 0
                        [site_guid] => 1
                        [container_guid] => 0
                        [access_id] => 2
                        [time_created] => 1466102750
                        [time_updated] => 1466102750
                        [last_action] => 1476549893
                        [enabled] => yes
                        [name] => Admin
                        [username] => admin
                        [password] => 
                        [salt] => 
                        [password_hash] => $2y$10$1SCz3uD8e2ztn8PyiTjYOuxV46787qcYYVBhhGM8WQfoUahq7j4tO
                        [email] => ______@yahoo.com
                        [language] => en
                        [banned] => no
                        [admin] => yes
                        [prev_last_action] => 1476549892
                        [last_login] => 1476549425
                        [prev_last_login] => 1476531681
                    )
    
                [valid:protected] => 
            )

     

    How can I convert and access protected data to JSON?

  • Use ElggEntity::toObject() then add whatever other metadata you need

  • Thanks @Ismayil, toObject() worked like a charm.