Permalink
Browse files

Better Paging Support

  • Loading branch information...
1 parent 7606c3c commit 37db221b3609090557b055163c6f94e28e10dbb3 @GautamGupta GautamGupta committed Oct 23, 2011
Showing with 127 additions and 26 deletions.
  1. +1 −1 README.md
  2. +43 −11 dd-includes/class.service.php
  3. +53 −4 dd-includes/functions.core.php
  4. +11 −3 dd-includes/service.gstocks.php
  5. +11 −3 dd-includes/service.gweather.php
  6. +8 −4 request.php
View
@@ -11,7 +11,7 @@ An online version of this API exists on http://api.datumdroid.com/
Installation
------------
-Just put the files in a directory on an Apache server with PHP5.2+ (requires JSON).
+Just put the files in a directory on an Apache server with PHP5.2+ (requires JSON and SimpleXML).
Check `dd-config-sample.php` and `request.php` for possible configurations.
Must edits: `DD_URI` and `$dd_api_keys`.
@@ -159,23 +159,55 @@ function geocode( $lat, $long, $radius, $units = 'mi' ) {
}
/**
+ * Paging debug function
+ *
+ * @return int
+ */
+ function _results() {
+ $this->max = 5;
+
+ $arr = array( 1, 2, 3, 4, 5, 6 );
+
+ array_splice( $arr, 0, ( $this->page - 1 ) * $this->rpp );
+
+ return $arr;
+ }
+
+ /**
* Get the required number of results
- * @param string $url Required. API URL to request
- * @param string $postargs Optional. Urlencoded query string to append to the $url
+ *
+ * @param bool $reset_query Reset query?
+ * @return array Array of results
*/
function search( $reset_query = false ) {
- // For the first page
- $results = $this->results( $reset_query );
+ $to_fetch = $this->page * $this->rpp;
+ $results = array();
+
+ do {
+ $new_results = (array) $this->results( $reset_query );
+
+ // No new results
+ if ( empty( $new_results ) )
+ break;
+
+ // Append the results
+ $results = array_merge( $results, $new_results );
+
+ // No more results after this present
+ if ( count( $new_results ) < $this->max )
+ break;
+
+ // Increment page
+ $this->page++;
+ } while ( $to_fetch > count( $results ) );
- // No results
- if ( empty( $results ) )
- return array( "no results found" );
+ // Filter, slice and return
+ $results = array_filter( (array) $results );
- // No more results or no more requested
- //if ( count( $results ) <= min( $this->max, $this->rpp ) )
- // return $results;
+ if ( $to_fetch < count( $results ) )
+ $results = array_slice( $results, 0, $to_fetch );
- return $results;
+ return empty( $results ) ? array( "no results found" ) : $results;
}
/**
@@ -25,13 +25,14 @@ function dd_get_query() {
/**
* Returns the per page option
*
+ * @param string $service The service requested. If empty, the all are returned
* @global int $dd_per_page Per page option
* @return int Per page Option
*/
-function dd_get_per_page() {
+function dd_get_per_page( $service = '' ) {
global $dd_per_page;
- return $dd_per_page;
+ return ( empty( $_REQUEST[$service] ) || ( $_REQUEST[$service] == 1 && isset( $dd_per_page ) ) ) ? $dd_per_page : $_REQUEST[$service];
}
/**
@@ -74,10 +75,58 @@ function dd_get_api_key( $service = '' ) {
return !empty( $dd_api_keys[$service] ) ? $dd_api_keys[$service] : '';
}
-/** Loaders *******************************************************************/
+/** Loaders & setters *********************************************************/
+
+/**
+ * Gets the current service
+ *
+ * @param string $service Service name
+ * @global string $dd_current_service Current service
+ * @return string Current service
+ */
+function dd_get_service( $service = '' ) {
+ global $dd_current_service, $dd_services;
+
+ if ( array_key_exists( $service, $dd_services ) )
+ return $service;
+ else
+ return $dd_current_service;
+}
+
+/**
+ * Set the current service global to the sent parameter
+ *
+ * @param string $service Service name
+ * @global string $dd_current_service Current service
+ * @global array $dd_services Registered services
+ */
+function dd_set_service( $service = '' ) {
+ global $dd_current_service, $dd_services;
+
+ if ( array_key_exists( $service, $dd_services ) )
+ $dd_current_service = $service;
+ else
+ dd_reset_service();
+}
+
+/**
+ * Reset the current service global to empty string
+ *
+ * @global string $dd_current_service Current service
+ */
+function dd_reset_service() {
+ global $dd_current_service;
+ $dd_current_service = '';
+}
+
+/**
+ * Load the class file for a service
+ *
+ * @param string $service Service name
+ */
function dd_load_service( $service = '' ) {
- require_once( DD_DIR_INC . 'service.' . $service . '.php' );
+ require_once( DD_DIR_INC . 'service.' . dd_get_service( $service ) . '.php' );
}
/** Formatting/misc ***********************************************************/
@@ -22,17 +22,25 @@ class DD_Service_Google_Stocks extends DD_Search_Service {
/**
* The maximum number of results that this service can return
+ *
+ * 2 for this service so that it fails the received results >= max results => more results|next page test
+ *
* @var int
*/
- var $max = '1';
+ var $max = '2';
/**
* Function to prepare data for return to client
* @access private
* @param string $data
*/
function objectify( $data ) {
- $obj = simplexml_load_string( $data )->finance;
+ $obj = simplexml_load_string( '<?xml version="1.0"?><xml_api_reply version="1"><finance module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><symbol data="GOOG"/><pretty_symbol data="GOOG"/><symbol_lookup_url data="/finance?client=ig&amp;q=GOOG"/><company data="Google Inc."/><exchange data="Nasdaq"/><exchange_timezone data="ET"/><exchange_utc_offset data="+05:00"/><exchange_closing data="960"/><divisor data="2"/><currency data="USD"/><last data="590.49"/><high data="592.75"/><low data="586.70"/><volume data="3394233"/><avg_volume data="3408"/><market_cap data="190661.99"/><open data="589.51"/><y_close data="583.67"/><change data="+6.82"/><perc_change data="1.17"/><delay data="0"/><trade_timestamp data="21 Oct 2011"/><trade_date_utc data="20111021"/><trade_time_utc data="200008"/><current_date_utc data="20111023"/><current_time_utc data="103305"/><symbol_url data="/finance?client=ig&amp;q=GOOG"/><chart_url data="/finance/chart?q=NASDAQ:GOOG&amp;tlf=12"/><disclaimer_url data="/help/stock_disclaimer.html"/><ecn_url data=""/><isld_last data=""/><isld_trade_date_utc data=""/><isld_trade_time_utc data=""/><brut_last data=""/><brut_trade_date_utc data=""/><brut_trade_time_utc data=""/><daylight_savings data="true"/></finance></xml_api_reply>' );
+
+ if ( ! $obj || ! $obj->finance->company['data'] )
+ return array();
+ else
+ $obj = $obj->finance;
$conditions = array();
@@ -43,7 +51,7 @@ function objectify( $data ) {
$conditions[$condition] = in_array( $condition, array( 'symbol_lookup_url', 'symbol_url', 'chart_url', 'disclaimer_url' ) ) ? 'http://google.com' . $conditions[$condition] : $conditions[$condition];
}
- return (object) $conditions;
+ return array( (object) $conditions );
}
/**
@@ -21,17 +21,25 @@ class DD_Service_Google_Weather extends DD_Search_Service {
/**
* The maximum number of results that this service can return
+ *
+ * 2 for this service so that it fails the received results >= max results => more results|next page test
+ *
* @var int
*/
- var $max = '1';
+ var $max = '2';
/**
* Function to prepare data for return to client
* @access private
* @param string $data
*/
function objectify( $data ) {
- $obj = simplexml_load_string( $data )->weather->current_conditions;
+ $obj = simplexml_load_string( $data );
+
+ if ( ! $obj )
+ return array();
+ else
+ $obj = $obj->weather->current_conditions;
$conditions = array();
@@ -42,7 +50,7 @@ function objectify( $data ) {
$conditions[$condition] = 'icon' == $condition ? 'http://google.com' . $conditions[$condition] : $conditions[$condition];
}
- return (object) $conditions;
+ return array( (object) $conditions );
}
/**
View
@@ -104,6 +104,9 @@
if ( ! isset( $dd_api_keys ) )
$dd_api_keys = array();
+// Current service
+$dd_current_service = '';
+
// Results array, which would be outputted as json encoded later
$dd_results = array();
@@ -122,7 +125,7 @@
dd_output( array( 'responseDetails' => "no services found", 'responseStatus' => 400 ) );
// Are we just returning supported services?
-if ( !empty( $_REQUEST['supported_services'] ) )
+if ( ! empty( $_REQUEST['supported_services'] ) )
dd_output( $dd_services );
// Check for search keywords
@@ -134,13 +137,14 @@
foreach ( (array) $dd_services as $service => $service_name ) {
if ( ( !empty( $_REQUEST['all'] ) && $_REQUEST['all'] == 1 ) || isset( $_REQUEST[$service] ) ) {
// Require the search results fetcher file
- dd_load_service( $service );
+ dd_set_service ( $service );
+ dd_load_service( dd_get_service() );
$service_class = 'DD_Service_' . $service_name;
- $search = new $service_class( ( empty( $_REQUEST[$service] ) || ( $_REQUEST[$service] == 1 && isset( $_REQUEST['per_page'] ) ) ) ? array() : array( 'per_page' => $_REQUEST[$service] ) );
+ $search = new $service_class();
- $dd_results[$service] = $search->search();
+ $dd_results[dd_get_service()] = $search->search();
}
}

0 comments on commit 37db221

Please sign in to comment.