diff options
author | Karl Vollmer <karl.vollmer@dal.ca> | 2011-11-28 11:22:47 -0400 |
---|---|---|
committer | Karl Vollmer <karl.vollmer@dal.ca> | 2011-11-28 11:22:47 -0400 |
commit | 2c06cb5440e7075721872538e661850932eaa55b (patch) | |
tree | 74e7ed06a126c0c9cb0c08af09f5256861b37844 | |
parent | e862098f66d7959663715b7b7002cf087f0fe3cd (diff) | |
download | ampache-2c06cb5440e7075721872538e661850932eaa55b.tar.gz ampache-2c06cb5440e7075721872538e661850932eaa55b.tar.bz2 ampache-2c06cb5440e7075721872538e661850932eaa55b.zip |
Switched API to exceptions rather then trigger_error in most instances, more work on the
remote catalog indexing.
-rw-r--r-- | lib/class/api.class.php | 2 | ||||
-rw-r--r-- | lib/class/catalog.class.php | 27 | ||||
-rw-r--r-- | modules/ampacheapi/AmpacheApi.lib.php | 62 |
3 files changed, 67 insertions, 24 deletions
diff --git a/lib/class/api.class.php b/lib/class/api.class.php index 54fa0a94..7d1c8840 100644 --- a/lib/class/api.class.php +++ b/lib/class/api.class.php @@ -210,7 +210,7 @@ class Api { $db_results = Dba::read($sql); $playlist = Dba::fetch_assoc($db_results); - $sql = "SELECT COUNT(`id`) AS `catalog` FROM `catalog`"; + $sql = "SELECT COUNT(`id`) AS `catalog` FROM `catalog` WHERE `catalog_type`='local'"; $db_results = Dba::read($sql); $catalog = Dba::fetch_assoc($db_results); diff --git a/lib/class/catalog.class.php b/lib/class/catalog.class.php index 84471c33..de13c910 100644 --- a/lib/class/catalog.class.php +++ b/lib/class/catalog.class.php @@ -1290,11 +1290,19 @@ class Catalog extends database_object { */ public function get_remote_catalog($type=0) { - $remote_handle = new AmpacheApi(array('username'=>'','password'=>'','server'=>'')); + try { + $remote_handle = new AmpacheApi(array('username'=>$this->remote_username,'password'=>$this->remote_password,'server'=>$this->path,'debug'=>true)); + } catch (Exception $e) { + Error::add('general',$e->getMessage()); + Error::display('general'); + flush(); + return false; + } - if ($remote_handle->state() != 'READY') { + if ($remote_handle->state() != 'CONNECTED') { debug_event('APICLIENT','Error Unable to make API client ready','1'); Error::add('general',_('Error Connecting to Remote Server')); + Error::display('general'); return false; } @@ -1302,7 +1310,7 @@ class Catalog extends database_object { $remote_catalog_info = $remote_handle->info(); // Tell em what we've found johnny! - printf(_('%u remote catalogs found (%u songs)'),$remote_catalog_info['catalogs'],$remote_catalog_info['songs']); + printf(_('%u remote catalog(s) found (%u songs)'),$remote_catalog_info['catalogs'],$remote_catalog_info['songs']); flush(); // Hardcoded for now @@ -1313,9 +1321,16 @@ class Catalog extends database_object { while ($total > $current) { $start = $current; $current += $step; - $remote_handle->parse_response($remote_handle->send_command('songs',array('offset'=>$start,'limit'=>$step))); - $songs = $remote_handle->get_response(); - } + // It uses exceptions so lets try this + try { + $remote_handle->parse_response($remote_handle->send_command('songs',array('offset'=>$start,'limit'=>$step))); + $songs = $remote_handle->get_response(); + } catch (Exception $e) { + Error::add('general',$e->getMessage()); + Error::display('general'); + flush(); + } + } // end while echo "<p>" . _('Completed updating remote catalog(s)') . ".</p><hr />\n"; flush(); diff --git a/modules/ampacheapi/AmpacheApi.lib.php b/modules/ampacheapi/AmpacheApi.lib.php index 5f34170d..25eac8fe 100644 --- a/modules/ampacheapi/AmpacheApi.lib.php +++ b/modules/ampacheapi/AmpacheApi.lib.php @@ -37,7 +37,7 @@ class AmpacheApi { // Constructed variables private $api_url; - private $api_state; + private $api_state='UNCONFIGURED'; private $api_auth; // XML Parser variables @@ -50,12 +50,14 @@ class AmpacheApi { protected $XML_skiptags = array('root'); protected $XML_parenttags = array('artist','album','song','tag','video','playlist','result', 'auth','version','update','add','clean','songs', - 'artists','albums','tags','videos','api','playlists'); + 'artists','albums','tags','videos','api','playlists','catalogs'); // Library static version information protected $LIB_version = '350001'; private $API_version = ''; + private $DEBUG=false; + /** * Constructor * This takes an array of input, if enough information is provided then it will @@ -64,6 +66,11 @@ class AmpacheApi { */ public function __construct($config=array()) { + // See if we are setting debug first + if ($config['debug']) { + $this->debug($config['debug']); + } + // If we got something, then configure! if (is_array($config) AND count($config)) { $this->configure($config); @@ -82,21 +89,32 @@ class AmpacheApi { */ public function connect() { + if ($this->debug) { echo "CONNECT:: Using $this->username / $this->password\n"; } + // Setup the handshake + $results = array(); $timestamp = time(); $key = hash('sha256',$this->password); $passphrase = hash('sha256',$timestamp . $key); $options = array('timestamp'=>$timestamp,'auth'=>$passphrase,'version'=>$this->LIB_version,'user'=>$this->username); - + $response = $this->send_command('handshake',$options); $this->parse_response($response); // We want the first response - $results = array_shift($this->get_response()); + $data = $this->get_response(); + foreach ($data as $value) { + $results = array_merge($results,$value); + } - $this->api_auth = $results['auth']; + if (!$results['auth']) { + $this->set_state('error'); + return false; + } + $this->api_auth = $results['auth']; + $this->set_state('connected'); // Define when we pulled this, it is not wine, it does // not get better with age $this->handshake_time = time(); @@ -112,6 +130,8 @@ class AmpacheApi { */ public function configure($config=array()) { + if ($this->debug) { echo "CONFIGURE :: Checking Passed config options\n"; } + if (!is_array($config)) { trigger_error('AmpacheApi::configure received a non-array value'); return false; @@ -175,9 +195,8 @@ class AmpacheApi { */ public function info() { - if ($this->state() != 'READY') { - trigger_error('AmpacheApi::info API in non-ready state, unable to return info'); - return false; + if ($this->state() != 'CONNECTED') { + throw new Exception('AmpacheApi::info API in non-ready state, unable to return info'); } return $this->handshake; @@ -190,24 +209,24 @@ class AmpacheApi { * host, and returns a nice clean keyed array */ public function send_command($command,$options=array()) { + + if ($this->debug) { echo "SEND COMMAND:: $command"; print_r($options,1); echo "\n"; } - if ($this->state() != 'READY') { - trigger_error('AmpacheApi::send_command API in non-ready state, unable to send'); - return false; + if ($this->state() != 'READY' AND $this->state() != 'CONNECTED') { + throw new Exception('AmpacheApi::send_command API in non-ready state, unable to send'); } if (!trim($command)) { - trigger_error('AmpacheApi::send_command no command specified'); - return false; + throw new Exception('AmpacheApi::send_command no command specified'); } if (!$this->validate_command($command)) { - trigger_error('AmpacheApi::send_command Invalid/Unknown command ' . $command . ' issued'); - return false; + throw new Exception('AmpacheApi::send_command Invalid/Unknown command ' . $command . ' issued'); } $url = $this->api_url . '?action=' . urlencode($command); foreach ($options as $key=>$value) { if (!trim($key)) { + // Non fatal don't need to except it trigger_error('AmpacheApi::send_command unable to append empty variable to command'); continue; } @@ -251,8 +270,7 @@ class AmpacheApi { $this->XML_create_parser(); if (!xml_parse($this->XML_parser,$response)) { - trigger_error('AmpacheApi::parse_response was unable to parse XML document'); - return false; + throw new Exception('AmpacheApi::parse_response was unable to parse XML document'); } xml_parser_free($this->XML_parser); @@ -270,6 +288,16 @@ class AmpacheApi { } // get_response + /** + * debug + * set debug to true? + */ + private function debug($value) { + + $this->debug = intval($value); + + } // debug + /////////////////////////// XML PARSER FUNCTIONS ///////////////////////////// /** |