diff options
Diffstat (limited to 'FedoraClient.php')
-rw-r--r-- | FedoraClient.php | 57 |
1 files changed, 28 insertions, 29 deletions
diff --git a/FedoraClient.php b/FedoraClient.php index da62e4f..891b7f3 100644 --- a/FedoraClient.php +++ b/FedoraClient.php @@ -26,13 +26,17 @@ define ('FEDORACLIENT_VERSION','0.1'); if (!function_exists('curl_version')) { die("curl extention required\n"); } +require_once('Cache/Lite.php'); abstract class FedoraClient { private $url; private $agent; - protected $debug = 0; + private $debug = 0; + protected $cache; function __construct ($url, array $options) { + $this->cache = new Cache_Lite(array('memoryCaching'=>true, 'automaticSerialization'=>true)); + $this->url = $url; if (isset($options['agent']) && !empty($options['agent'])) { $this->agent = $options['agent']; @@ -42,11 +46,14 @@ abstract class FedoraClient { if (isset($options['debug']) && intval($options['debug'])>0) { $this->debug = intval($options['debug']); } - if ($this->debug >= 2) { - echo "[debug] ".__CLASS__."::".__FUNCTION__.": url='$url', agent='".$this->agent."'\n"; - } + $this->logDebug(3,__CLASS__."::".__FUNCTION__.": url='$url', agent='".$this->agent."'"); } + function logDebug($level, $msg) { + if ($this->debug>=$level) { + echo "[debug][$level] $msg\n"; + } + } function sendRequest($method, array $options=array()) { $curl = curl_init(); # And join to make our url. @@ -66,10 +73,8 @@ abstract class FedoraClient { # Set standard headers curl_setopt($curl, CURLOPT_HTTPHEADER, array('User-agent: '.$this->agent, 'Accept: application/json')); - if ($this->debug >= 1) { - echo "[debug] ".__CLASS__."::".__FUNCTION__.": call '$url'\n"; - } # run the request + $this->logDebug(1,__CLASS__."::".__FUNCTION__.": call '$url'"); curl_exec($curl); @@ -79,30 +84,23 @@ abstract class FedoraClient { # We need to accept both until all apps are updated to return 401. $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($http_status==401 || $http_status==403) { - if ($this->debug >= 1) { - echo "[debug] ".__CLASS__."::".__FUNCTION__.": http_status '$http_status' Authentication failed logging in\n"; - } + $this->logDebug(1,__CLASS__."::".__FUNCTION__.": http_status '$http_status' Authentication failed logging in"); return array(); } else if ($http_status>=400) { - if ($this->debug >= 1) { - echo "[debug] ".__CLASS__."::".__FUNCTION__.": http_status '$http_status' Unknown HTTP Server Response\n"; - } + $this->logDebug(1,__CLASS__."::".__FUNCTION__.": http_status '$http_status' Unknown HTTP Server Response"); return array(); - } else if ($this->debug >= 2) { - echo "[debug] ".__CLASS__."::".__FUNCTION__.": http_status '$http_status'\n"; + } else { + $this->logDebug(2,__CLASS__."::".__FUNCTION__.": http_status '$http_status'"); } - if ($this->debug >= 1) { - echo "[debug] ".__CLASS__."::".__FUNCTION__.": close connexion\n"; - } + $this->logDebug(1,__CLASS__."::".__FUNCTION__.": close connexion"); + curl_close($curl); return json_decode($this->data, true); } function receive($curl, $data) { - if ($this->debug >= 9) { - echo "[debug] ".__CLASS__."::".__FUNCTION__.": $data\n"; - } + $this->logDebug(9,__CLASS__."::".__FUNCTION__.": $data"); $this->data .= $data; return strlen($data); } @@ -111,23 +109,24 @@ abstract class FedoraClient { class FedoraPkgdb extends FedoraClient { - private $branches = array(); function __construct (array $options=array()) { parent::__construct('https://admin.fedoraproject.org/pkgdb/', $options); - if ($this->debug>=2) { - echo "[debug] ".__CLASS__."::".__FUNCTION__."\n"; - } + $this->logDebug(3,__CLASS__."::".__FUNCTION__); } function getBranches($refresh=false) { - if (count($this->branches) && !$refresh) { - return $this->branches; + $rep = $this->cache->get(__FUNCTION__,__CLASS__); + if ($rep) { + $this->logDebug(2,__CLASS__."::".__FUNCTION__." get from cache"); + } else { + $rep =$this->sendRequest('collections'); + $this->cache->save($rep,__FUNCTION__,__CLASS__); + $this->logDebug(2,__CLASS__."::".__FUNCTION__." save to cache"); } - $rep =$this->sendRequest('collections'); + $this->branches = array(); if (isset($rep['collections'])) { - $this->branches = array(); foreach ($rep['collections'] as $coll) if (isset($coll[0]['branchname'])) { $this->branches[$coll[0]['branchname']] = $coll[0]; } |