glFusion Wiki

Site Tools


glfusion:development:pluginapi:search

Search Functions

Tying your plugin into the glFusion search API is rather easy. Easy, that is, if you know how to search your plugin's data already. The glFusion search API provides a way for you to return the SQL query back to glFusion which will be executed in the core and the results included in its search page.

For the data in your plugin to be searched by glFusion's search functions, there are two functions that you need to implement in your plugin's function.inc:

plugin_getsearchtypes_<plugin name>

You will probably want to add a new type in the Type drop down on search.php. This function prints the option tags needed. Make sure that the value tag is <plugin name>

Arguments

This function takes no parameters and returns an associative array of the search type.

Code Example

Naturally all occurrences of links would be replaced with the name of your plugin and the LANGUAGE varible $LANG_LINKS is just an example. Your Plugin needs to use a unique variable name so replace $LANG_LINKS.

The return array is of the format array['searchtype'] = 'Search Description', where searchtype is returned to your plugin search routine and Description is displayed in the Search Page Drop Down box.

You can have multiple search types and descriptions.

function plugin_searchtypes_links()
{
    global $LANG_LINKS;
    $tmp['searchtype']= $LANG_LINKS['searchtype'];
    return $tmp;
}

plugin_dopluginsearch_<plugin name>

This section will outline the method used to search a plugin's data.

Input Parameters

The parameters that are passed to this function are as follows:

  • $query – a string containing the search term.
  • $datestart – starting date to begin search.
  • $dateend – ending date to end search.
  • $topic – topic item is assigned to.
  • $type – no longer used, deprecated.
  • $author – the user ID of the author.
  • $keyType – search key type: 'all', 'phrase', 'any'.
  • $page – no longer used, deprecated.
  • $perpage – no longer used, deprecated.

Depending on your plugin, some of these criteria may be meaningless and thus ignored. The function should then perform two basic operations: #Build search SQL string using the input parameters. #Return the query along with some plugin information.

Returned Values

The search API requires the following information to be returned by the plugin_dopluginsearch_{plugin_name}() function:

Plugin Name

This is a name to display to the user, (i.e. 'My Plugin' singular preferred). It could also be a name used locally to identify the plugin, (i.e. 'myplugin').

A Search Rank

This is an integer between 1 and 5 based on how many result to extract from the query. As all the results have been combined into a single list, the core needs to prioritize important plugins over non-important ones. The higher the ranking the more results will be displayed to the user. A rank of 1 will show fewer results, where as a rank of 5 shows the most amount of results from the plugin. The rank is optional, if it is not provided it will default to 3.

Here is an example of glFusion setup with three plugins, the different rank values will allow varying results from each plugin. The results page is set to display a total of 50 results.

  plugin      rank    results
  ---------------------------
  stories     5       23
  forums      4       18
  comments    2       9

Standard SQL Query

This is a string containing a single query, OR an array of two queries for both MSSQL and MySQL DBMS.

All SQL queries returned should be without the LIMIT and ORDER BY clauses. The SQL query must have the following column names, and look like:

SELECT id, title, description, date, user, hits, url FROM ... WHERE ...

If a column does not exist in the table then another value should be substituted, for example: '0' AS hits, …

The url is where to take the user when they have clicked a result. It should start with a single slash if the target is within the current domain. Otherwise the result with be printed, as is, in the href attribute of the link. For example this URL belongs to the glFusion site:

CONCAT('/staticpages/index.php?page=', sp.sp_id) AS url

However the Links plugin needs to direct users to external sites when clicked, so providing the URL from the database will suffice.

Full-Text SQL Query (optional)

An array of two queries for both MSSQL and MySQL DBMS using the Full-Text search method. This search method will take advantage of Full-Text indexes which will reduce search times. This should only be returned if the columns being searched have been properly indexed. The Full-Text SQL Query will only be executed if Full-Text searches have been enabled by the admin during the installation or upgrading process, otherwise the core will always fall back to the Standard SQL Query.

The SearchCriteria() Class

The process of returning the values is done by initializing a SearchCriteria() object, setting the parameters for the search then returning the object. Here is an example:

$search = new SearchCriteria('myplugin', 'My Plugin');
$search->setSQL($sql);
$search->setFTSQL($ftsql);
$search->setRank(4);

This indicates that the 'myplugin' plugin supports Full-Text searches and will have a ranking of 4.

buildSearchSQL() Function

As a lot of the plugins will be processing the same or similar SQL queries the buildSearchSQL() function has been provided to simplify things. This function will take four parameters then build the searching part of the SQL query. It will also return the Full-Text query strings should they be required. The parameters are:

  • $keyType – search key type: 'all', 'phrase', 'any'.
  • $query – the query string.
  • $columns – the column names to search.
  • $sql – the sql query to append to. (optional)

And an example of how it can be put to use:

$sql = 'SELECT ... FROM ... WHERE ... ';
$columns = array('title','bodytext');
list($sql,$ftsql) = buildSearchSQL('any', 'my glfusion', $columns, $sql);
 
// $sql => SELECT ... FROM ... WHERE ... AND ((title LIKE '%my%' OR bodytext LIKE '%my%') OR  (title LIKE '%glfusion%' OR bodytext LIKE '%glfusion%'))
// $ftsql[mysql] => SELECT ... FROM ... WHERE ... AND MATCH(title,bodytext) AGAINST ('my glfusion' IN BOOLEAN MODE)
// $ftsql[mssql] => SELECT ... FROM ... WHERE ... AND CONTAINS((title,bodytext), '"my" OR "glfusion"')

A proper example of its use is at the bottom of this section.

Enabling URL Rewrite

If the plugin requires the URL to be passed through the COM_buildUrl() function then it should set the setURLRewrite() function to true. For example:

$search = new SearchCriteria('myplugin', 'My Plugin');
$search->setSQL($sql);
$search->setURLRewrite(true);

Returning Multiple Objects

In some cases a plugin may be required to search across two or more tables that have no relation with one another. To accommodate this they may return an array of SearchCriteria() objects, each with a different SQL query. To allow the user to differentiate between the results, an array of names can be passed. Each name will be a separate subgroup and will be appended to one another using the separator from the configuration. The following example shows how this works:

// Search the main table
//   These results will be labeled 'My Plugin > Main'
$search_main = new SearchCriteria('myplugin', array('My Plugin', 'Main'));
$plugin_main->setSQL($sql_main);
$plugin_main->setFTSQL($ftsql_main);
$plugin_main->setRank(4);
 
// Search a sub table
//   These results will be labelled 'My Plugin > Sub'
$search_sub = new SearchCriteria('myplugin', array('My Plugin', 'Sub'));
$search_sub->setSQL($sql_sub);
$search_sub->setFTSQL($ftsql_sub);
$search_sub->setRank(4);
 
// Return both objects
return array($search_main, $search_sub);
Note: The plugin identifier needs to stay the same across all objects, in this case 'myplugin'.

Code Example

This example searches the Stories, and puts to use everything discussed here so its a good example of how to implement the API in your plugin.

function plugin_dopluginsearch_searchStories($query, $datestart, $dateend, $topic, $type, $author, $keyType, $page, $perpage)
{
    global $_TABLES, $_DB_dbms, $LANG09;
 
    // Make sure the query is SQL safe
    $query = trim(addslashes($query));
 
    // Build the first part of the query
    $sql = "SELECT
                s.sid AS id,
                s.title AS title,
                s.introtext AS description,
                UNIX_TIMESTAMP(s.date) AS date,
                s.uid AS uid,
                s.hits AS hits,
                CONCAT('/article.php?story=',s.sid) AS url ";
    $sql .= "FROM {$_TABLES['stories']} AS s, {$_TABLES['users']} AS u ";
    $sql .= "WHERE (draft_flag = 0) AND (date <= NOW()) AND (u.uid = s.uid) ";
    $sql .= COM_getPermSQL('AND') . COM_getTopicSQL('AND') . COM_getLangSQL('sid', 'AND') . ' ';
 
    // If we are searching by date add that too
    if (!empty($datestart) && !empty($dateend)) {
        $delim = substr($datestart, 4, 1);
        if (!empty($delim)) {
            $DS = explode($delim, $datestart);
            $DE = explode($delim, $dateend);
            $startdate = mktime(0,0,0,$DS[1],$DS[2],$DS[0]);
            $enddate = mktime(23,59,59,$DE[1],$DE[2],$DE[0]);
            $sql .= "AND (UNIX_TIMESTAMP(date) BETWEEN '$startdate' AND '$enddate') ";
        }
    }
    if (!empty($topic)) {
        $sql .= "AND (s.tid = '$topic') ";
    }
    if (!empty($author)) {
        $sql .= "AND (s.uid = '$author') ";
    }
 
    // Create a SearchCriteria instance with the name of the plugin
    $search = new SearchCriteria('stories', $LANG09[65]);
 
    // These are the columns in the table that need searching
    $columns = array('introtext','bodytext','title');
 
    // Get back the completed SQL query
    list($sql,$ftsql) = $search->buildSearchSQL($keyType, $query, $columns, $sql);
 
    // Set the Std. SQL Query
    $search->setSQL($sql);
 
    // Set the Full-Text Query, remember the columns _MUST_ be indexed first. If they are not then don't set this.
    $search->setFTSQL($ftsql);
 
    // Finally set a high ranking, enable URLRewrite and return the object.
    $search->setRank(5);
    $search->setURLRewrite(true);
    return $search;
}
glfusion/development/pluginapi/search.txt · Last modified: 2017/04/12 21:13 (external edit)

Page Tools