[geeklog-cvs] geeklog: Added callback function to search API (feature request ...
geeklog-cvs at lists.geeklog.net
geeklog-cvs at lists.geeklog.net
Sun Oct 25 19:17:40 EDT 2009
changeset 7391:0bee991937cc
url: http://project.geeklog.net/cgi-bin/hgwebdir.cgi/geeklog/rev/0bee991937cc
user: Sami Barakat
date: Sun Oct 25 22:01:38 2009 +0000
description:
Added callback function to search API (feature request #0000985)
diffstat:
system/classes/listfactory.class.php | 87 +++++++++++++++++++++++++---
system/classes/search.class.php | 79 +++++++++++++------------
system/classes/searchcriteria.class.php | 93 ++++++++++++++++++++-----------
3 files changed, 178 insertions(+), 81 deletions(-)
diffs (truncated from 459 to 300 lines):
diff -r 2b5aac1d58f8 -r 0bee991937cc system/classes/listfactory.class.php
--- a/system/classes/listfactory.class.php Tue Oct 20 22:53:07 2009 +0100
+++ b/system/classes/listfactory.class.php Sun Oct 25 22:01:38 2009 +0000
@@ -206,7 +206,13 @@
if ($name === ROW_NUMBER) {
$sort = false;
}
- $this->_fields[] = array('title' => $title, 'name' => $name, 'display' => $display, 'sort' => $sort, 'format' => $format);
+ $this->_fields[] = array(
+ 'title' => $title,
+ 'name' => $name,
+ 'display' => $display,
+ 'sort' => $sort,
+ 'format' => $format
+ );
}
/**
@@ -221,7 +227,26 @@
*/
function setQuery( $title, $name, $sql, $rank )
{
- $this->_query_arr[] = array('title' => $title, 'name' => $name, 'sql' => $sql, 'rank' => $rank);
+ $this->_query_arr[] = array(
+ 'type' => 'sql',
+ 'title' => $title,
+ 'name' => $name,
+ 'sql' => $sql,
+ 'rank' => $rank
+ );
+ $this->_total_rank += $rank;
+ }
+
+ function setCallback( $title, $name, $function, $rank, $total )
+ {
+ $this->_query_arr[] = array(
+ 'type' => 'callback',
+ 'title' => $title,
+ 'name' => $name,
+ 'func' => $function,
+ 'rank' => $rank,
+ 'total' => $total
+ );
$this->_total_rank += $rank;
}
@@ -282,21 +307,25 @@
* @return int Total number of rows
*
*/
- function _numRows( $sql )
+ function _getTotal( $param )
{
- if (is_array($sql))
- {
+ if ($param['type'] == 'callback') {
+ return $param['total'];
+ }
+ else {
+ $sql = $param['sql'];
+ }
+
+ if (is_array($sql)) {
$sql['mysql'] = preg_replace('/SELECT.*FROM/is', 'SELECT COUNT(*) FROM', $sql['mysql']);
$sql['mssql'] = preg_replace('/SELECT.*FROM/is', 'SELECT COUNT(*) FROM', $sql['mssql']);
}
- else
- {
+ else {
$sql = preg_replace('/SELECT.*FROM/is', 'SELECT COUNT(*) FROM', $sql);
}
$result = DB_query($sql);
$num_rows = DB_numRows($result);
- if ($num_rows <= 1)
- {
+ if ($num_rows <= 1) {
$B = DB_fetchArray($result, true);
$num_rows = $B[0];
}
@@ -397,7 +426,7 @@
$limits = array();
for ($i = 0; $i < count($this->_query_arr); $i++)
{
- $limits[$i]['total'] = $this->_numRows($this->_query_arr[$i]['sql']);
+ $limits[$i]['total'] = $this->_getTotal($this->_query_arr[$i]);
$limits[$i]['pp'] = round(($this->_query_arr[$i]['rank'] / $this->_total_rank) * $num_query_results);
$this->_total_found += $limits[$i]['total'];
$pp_total += $limits[$i]['pp'];
@@ -415,6 +444,44 @@
if ($limits[$i]['limit'] <= 0) {
continue;
}
+
+ // This is a callback function
+ if ($this->_query_arr[$i]['type'] == 'callback')
+ {
+ if (is_callable($this->_query_arr[$i]['func']))
+ {
+ $callback_rows = call_user_func_array($this->_query_arr[$i]['func'], array($limits[$i]['offset'], $limits[$i]['limit']));
+
+ foreach ($callback_rows as $row)
+ {
+ $col = array();
+ $col[SQL_TITLE] = $this->_query_arr[$i]['title'];
+ $col[SQL_NAME] = $this->_query_arr[$i]['name'];
+
+ foreach ($this->_fields as $field)
+ {
+ if (!is_numeric($field['name']) && $field['name'][0] != '_') {
+ if (empty($row[ $field['name'] ])) {
+ $col[ $field['name'] ] = 'LF_NULL';
+ } else {
+ $col[ $field['name'] ] = $row[ $field['name'] ];
+ }
+ }
+ }
+
+ // Need to call the format function before and after
+ // sorting the results.
+ if (is_callable($this->_function)) {
+ $col = call_user_func_array($this->_function, array(true, $col));
+ }
+
+ $rows_arr[] = $col;
+ }
+ }
+ continue;
+ }
+
+ // This is an SQL query, so execute it and format the results
$limit_sql = " LIMIT {$limits[$i]['offset']},{$limits[$i]['limit']}";
if (is_array($this->_query_arr[$i]['sql']))
diff -r 2b5aac1d58f8 -r 0bee991937cc system/classes/search.class.php
--- a/system/classes/search.class.php Tue Oct 20 22:53:07 2009 +0100
+++ b/system/classes/search.class.php Sun Oct 25 22:01:38 2009 +0000
@@ -2,7 +2,7 @@
/* Reminder: always indent with 4 spaces (no tabs). */
// +---------------------------------------------------------------------------+
-// | Geeklog 1.6 |
+// | Geeklog 1.6.1 |
// +---------------------------------------------------------------------------+
// | search.class.php |
// | |
@@ -342,8 +342,6 @@
/**
* Performs search on all stories
*
- * @author Tony Bibbs, tony AT geeklog DOT net
- * @author Sami Barakat, s.m.barakat AT gmail DOT com
* @access private
* @return object plugin object
*
@@ -425,7 +423,6 @@
* in this function to allow legacy support to plugins using
* the old API calls defined versions prior to Geeklog 1.5.1
*
- * @author Sami Barakat, s.m.barakat AT gmail DOT com
* @access public
* @return string HTML output for search results
*
@@ -460,7 +457,7 @@
$url = "{$this->_searchURL}&type={$this->_type}&mode=";
$obj = new ListFactory($url.'search', $_CONF['search_limits'], $_CONF['num_search_results']);
- $obj->setRowFunction(array($this, 'searchFormatCallBack'));
+ $obj->setRowFunction(array($this, 'searchFormatCallback'));
$obj->setField('ID', 'id', false);
$obj->setField('URL', 'url', false);
@@ -520,51 +517,62 @@
{
if (is_a($result, 'SearchCriteria'))
{
+ $debug_info = $result->getName() . " using APIv2";
+
if ($this->_type != 'all' && $this->_type != $result->getName())
{
if ($this->_verbose) {
- COM_errorLog($result->getName() . " using APIv2. Skipped as type is not " . $this->_type);
+ $new_api++;
+ COM_errorLog("$debug_info. Skipped as type is not " . $this->_type);
}
continue;
}
- $debug_info = $result->getName() . " using APIv2 with ";
-
- if ($_CONF['search_use_fulltext'] == true && $result->getFTSQL() != '')
- {
- $debug_info .= "FULLTEXT. ";
- $sql = $result->getFTSQL();
- }
- else
- {
- $debug_info .= "LIKE. ";
- $sql = $result->getSQL();
- }
-
- $sql = $this->_convertsql($sql);
-
- $debug_info .= "SQL = " . print_r($sql,1);
- if ($this->_verbose) {
- COM_errorLog($debug_info);
- }
-
$api_results = $result->getResults();
if (!empty($api_results)) {
$obj->addResultArray($api_results);
}
- $obj->setQuery($result->getLabel(), $result->getName(), $sql, $result->getRank());
+ $api_callback_func = $result->getCallback();
+ if (!empty($api_callback_func))
+ {
+ $debug_info .= " with Callback Function " . $api_callback_func;
+ $obj->setCallback($result->getLabel(), $result->getName(), $api_callback_func, $result->getRank(), $result->getTotal());
+ }
+ else
+ {
+ if ($_CONF['search_use_fulltext'] == true && $result->getFTSQL() != '')
+ {
+ $debug_info .= " with SQL FULLTEXT. ";
+ $sql = $result->getFTSQL();
+ }
+ else
+ {
+ $debug_info .= " with SQL LIKE. ";
+ $sql = $result->getSQL();
+ }
+
+ $sql = $this->_convertsql($sql);
+ $debug_info .= "SQL = " . print_r($sql,1);
+ $obj->setQuery($result->getLabel(), $result->getName(), $sql, $result->getRank());
+ }
+
$this->_url_rewrite[ $result->getName() ] = $result->UrlRewriteEnable();
$this->_append_query[ $result->getName() ] = $result->AppendQueryEnable();
- $new_api++;
+
+ if ($this->_verbose) {
+ $new_api++;
+ COM_errorLog($debug_info);
+ }
}
else if (is_a($result, 'Plugin') && $result->num_searchresults != 0)
{
// Some backwards compatibility
- $debug_info = $result->plugin_name . " using APIv1 with backwards compatibility.";
- $debug_info .= " Count: " . $result->num_searchresults;
- $debug_info .= " Headings: " . implode(",", $result->searchheading);
if ($this->_verbose) {
+ $old_api++;
+ $debug_info = $result->plugin_name . " using APIv1 with backwards compatibility.";
+ $debug_info .= " Count: " . $result->num_searchresults;
+ $debug_info .= " Headings: " . implode(",", $result->searchheading);
COM_errorLog($debug_info);
}
@@ -617,7 +625,6 @@
$obj->addResult($api_results);
}
- $old_api++;
}
}
@@ -673,19 +680,18 @@
}
/**
- * CallBack function for the ListFactory class
+ * Callback function for the ListFactory class
*
* This function gets called by the ListFactory class and formats
* each row accordingly for example pulling usernames from the
* users table and displaying a link to their profile.
*
- * @author Sami Barakat, s.m.barakat AT gmail DOT com
* @access public
* @param array $row An array of plain data to format
* @return array A reformatted version of the input array
*
*/
- function searchFormatCallBack( $preSort, $row )
+ function searchFormatCallback( $preSort, $row )
{
global $_CONF, $LANG09;
@@ -756,7 +762,6 @@
* version depending where the text was cut. Works on a
* word basis, so long words wont get cut.
*
- * @author Sami Barakat, s.m.barakat AT gmail DOT com
* @access private
* @param string $keyword The word to centre around
* @param string $text The complete text string
@@ -879,7 +884,6 @@
* number of similar heading names. Used for backwards
More information about the geeklog-cvs
mailing list