|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00028 require_once 'Zend/Rest/Client.php'; 00029 00033 require_once 'Zend/Json/Decoder.php'; 00034 00038 require_once 'Zend/Service/Delicious/SimplePost.php'; 00039 00043 require_once 'Zend/Service/Delicious/Post.php'; 00044 00048 require_once 'Zend/Service/Delicious/PostList.php'; 00049 00050 00060 class Zend_Service_Delicious 00061 { 00062 const API_URI = 'https://api.del.icio.us'; 00063 00064 const PATH_UPDATE = '/v1/posts/update'; 00065 const PATH_TAGS = '/v1/tags/get'; 00066 const PATH_TAG_RENAME = '/v1/tags/rename'; 00067 const PATH_BUNDLES = '/v1/tags/bundles/all'; 00068 const PATH_BUNDLE_DELETE = '/v1/tags/bundles/delete'; 00069 const PATH_BUNDLE_ADD = '/v1/tags/bundles/set'; 00070 const PATH_DATES = '/v1/posts/dates'; 00071 const PATH_POST_DELETE = '/v1/posts/delete'; 00072 const PATH_POSTS_GET = '/v1/posts/get'; 00073 const PATH_POSTS_ALL = '/v1/posts/all'; 00074 const PATH_POSTS_ADD = '/v1/posts/add'; 00075 const PATH_POSTS_RECENT = '/v1/posts/recent'; 00076 00077 const JSON_URI = 'http://del.icio.us'; 00078 const JSON_POSTS = '/feeds/json/%s/%s'; 00079 const JSON_TAGS = '/feeds/json/tags/%s'; 00080 const JSON_NETWORK = '/feeds/json/network/%s'; 00081 const JSON_FANS = '/feeds/json/fans/%s'; 00082 const JSON_URL = '/feeds/json/url/data'; 00083 00089 protected $_rest; 00090 00096 protected $_authUname; 00097 00103 protected $_authPass; 00104 00110 protected static $_lastRequestTime = 0; 00111 00119 public function __construct($uname = null, $pass = null) 00120 { 00121 $this->_rest = new Zend_Rest_Client(); 00122 $this->_rest->getHttpClient()->setConfig(array('ssltransport' => 'ssl')); 00123 $this->setAuth($uname, $pass); 00124 } 00125 00133 public function setAuth($uname, $pass) 00134 { 00135 $this->_authUname = $uname; 00136 $this->_authPass = $pass; 00137 00138 return $this; 00139 } 00140 00147 public function getLastUpdate() 00148 { 00149 $response = $this->makeRequest(self::PATH_UPDATE); 00150 00151 $rootNode = $response->documentElement; 00152 if ($rootNode && $rootNode->nodeName == 'update') { 00156 return new Zend_Date(strtotime($rootNode->getAttribute('time'))); 00157 } else { 00161 require_once 'Zend/Service/Delicious/Exception.php'; 00162 throw new Zend_Service_Delicious_Exception('del.icio.us web service has returned something odd!'); 00163 } 00164 } 00165 00171 public function getTags() 00172 { 00173 $response = $this->makeRequest(self::PATH_TAGS); 00174 00175 return self::_xmlResponseToArray($response, 'tags', 'tag', 'tag', 'count'); 00176 } 00177 00185 public function renameTag($old, $new) 00186 { 00187 $response = $this->makeRequest(self::PATH_TAG_RENAME, array('old' => $old, 'new' => $new)); 00188 00189 self::_evalXmlResult($response); 00190 00191 return $this; 00192 } 00193 00199 public function getBundles() 00200 { 00201 $response = $this->makeRequest(self::PATH_BUNDLES); 00202 00203 $bundles = self::_xmlResponseToArray($response, 'bundles', 'bundle', 'name', 'tags'); 00204 foreach ($bundles as &$tags) { 00205 $tags = explode(' ', $tags); 00206 } 00207 return $bundles; 00208 } 00209 00217 public function addBundle($bundle, array $tags) 00218 { 00219 $tags = implode(' ', (array) $tags); 00220 $response = $this->makeRequest(self::PATH_BUNDLE_ADD, array('bundle' => $bundle, 'tags' => $tags)); 00221 00222 self::_evalXmlResult($response); 00223 00224 return $this; 00225 } 00226 00233 public function deleteBundle($bundle) 00234 { 00235 $response = $this->makeRequest(self::PATH_BUNDLE_DELETE, array('bundle' => $bundle)); 00236 00237 self::_evalXmlResult($response); 00238 00239 return $this; 00240 } 00241 00248 public function deletePost($url) 00249 { 00250 $response = $this->makeRequest(self::PATH_POST_DELETE, array('url' => $url)); 00251 00252 self::_evalXmlResult($response); 00253 00254 return $this; 00255 } 00256 00265 public function getDates($tag = null) 00266 { 00267 $parms = array(); 00268 if ($tag) { 00269 $parms['tag'] = $tag; 00270 } 00271 00272 $response = $this->makeRequest(self::PATH_DATES, $parms); 00273 00274 return self::_xmlResponseToArray($response, 'dates', 'date', 'date', 'count'); 00275 } 00276 00288 public function getPosts($tag = null, Zend_Date $dt = null, $url = null) 00289 { 00290 $parms = array(); 00291 if ($tag) { 00292 $parms['tag'] = $tag; 00293 } 00294 if ($url) { 00295 $parms['url'] = $url; 00296 } 00297 if ($dt) { 00298 $parms['dt'] = $dt->get('Y-m-d\TH:i:s\Z'); 00299 } 00300 00301 $response = $this->makeRequest(self::PATH_POSTS_GET, $parms); 00302 00303 return $this->_parseXmlPostList($response); 00304 } 00305 00312 public function getAllPosts($tag = null) 00313 { 00314 $parms = array(); 00315 if ($tag) { 00316 $parms['tag'] = $tag; 00317 } 00318 00319 $response = $this->makeRequest(self::PATH_POSTS_ALL, $parms); 00320 00321 return $this->_parseXmlPostList($response); 00322 } 00323 00331 public function getRecentPosts($tag = null, $count = 15) 00332 { 00333 $parms = array(); 00334 if ($tag) { 00335 $parms['tag'] = $tag; 00336 } 00337 if ($count) { 00338 $parms['count'] = $count; 00339 } 00340 00341 $response = $this->makeRequest(self::PATH_POSTS_RECENT, $parms); 00342 00343 return $this->_parseXmlPostList($response); 00344 } 00345 00351 public function createNewPost($title, $url) 00352 { 00353 return new Zend_Service_Delicious_Post($this, array('title' => $title, 'url' => $url)); 00354 } 00355 00364 public function getUserPosts($user, $count = null, $tag = null) 00365 { 00366 $parms = array(); 00367 if ($count) { 00368 $parms['count'] = $count; 00369 } 00370 00371 $path = sprintf(self::JSON_POSTS, $user, $tag); 00372 $res = $this->makeRequest($path, $parms, 'json'); 00373 00374 return new Zend_Service_Delicious_PostList($this, $res); 00375 } 00376 00388 public function getUserTags($user, $atleast = null, $count = null, $sort = 'alpha') 00389 { 00390 $parms = array(); 00391 if ($atleast) { 00392 $parms['atleast'] = $atleast; 00393 } 00394 if ($count) { 00395 $parms['count'] = $count; 00396 } 00397 if ($sort) { 00398 $parms['sort'] = $sort; 00399 } 00400 00401 $path = sprintf(self::JSON_TAGS, $user); 00402 00403 return $this->makeRequest($path, $parms, 'json'); 00404 } 00405 00412 public function getUserNetwork($user) 00413 { 00414 $path = sprintf(self::JSON_NETWORK, $user); 00415 return $this->makeRequest($path, array(), 'json'); 00416 } 00417 00424 public function getUserFans($user) 00425 { 00426 $path = sprintf(self::JSON_FANS, $user); 00427 return $this->makeRequest($path, array(), 'json'); 00428 } 00429 00444 public function getUrlDetails($url) 00445 { 00446 $parms = array('hash' => md5($url)); 00447 00448 $res = $this->makeRequest(self::JSON_URL, $parms, 'json'); 00449 00450 if(isset($res[0])) { 00451 return $res[0]; 00452 } else { 00453 return null; 00454 } 00455 } 00456 00466 public function makeRequest($path, array $parms = array(), $type = 'xml') 00467 { 00468 // if previous request was made less then 1 sec ago 00469 // wait until we can make a new request 00470 $timeDiff = microtime(true) - self::$_lastRequestTime; 00471 if ($timeDiff < 1) { 00472 usleep((1 - $timeDiff) * 1000000); 00473 } 00474 00475 $this->_rest->getHttpClient()->setAuth($this->_authUname, $this->_authPass); 00476 00477 switch ($type) { 00478 case 'xml': 00479 $this->_rest->setUri(self::API_URI); 00480 break; 00481 case 'json': 00482 $parms['raw'] = true; 00483 $this->_rest->setUri(self::JSON_URI); 00484 break; 00485 default: 00489 require_once 'Zend/Service/Delicious/Exception.php'; 00490 throw new Zend_Service_Delicious_Exception('Unknown request type'); 00491 } 00492 00493 self::$_lastRequestTime = microtime(true); 00494 $response = $this->_rest->restGet($path, $parms); 00495 00496 if (!$response->isSuccessful()) { 00500 require_once 'Zend/Service/Delicious/Exception.php'; 00501 throw new Zend_Service_Delicious_Exception("Http client reported an error: '{$response->getMessage()}'"); 00502 } 00503 00504 $responseBody = $response->getBody(); 00505 00506 switch ($type) { 00507 case 'xml': 00508 $dom = new DOMDocument() ; 00509 00510 if (!@$dom->loadXML($responseBody)) { 00514 require_once 'Zend/Service/Delicious/Exception.php'; 00515 throw new Zend_Service_Delicious_Exception('XML Error'); 00516 } 00517 00518 return $dom; 00519 case 'json': 00520 return Zend_Json_Decoder::decode($responseBody); 00521 } 00522 } 00523 00535 private static function _xmlResponseToArray(DOMDocument $response, $root, $child, $attKey, $attValue) 00536 { 00537 $rootNode = $response->documentElement; 00538 $arrOut = array(); 00539 00540 if ($rootNode->nodeName == $root) { 00541 $childNodes = $rootNode->childNodes; 00542 00543 for ($i = 0; $i < $childNodes->length; $i++) { 00544 $currentNode = $childNodes->item($i); 00545 if ($currentNode->nodeName == $child) { 00546 $arrOut[$currentNode->getAttribute($attKey)] = $currentNode->getAttribute($attValue); 00547 } 00548 } 00549 } else { 00553 require_once 'Zend/Service/Delicious/Exception.php'; 00554 throw new Zend_Service_Delicious_Exception('del.icio.us web service has returned something odd!'); 00555 } 00556 00557 return $arrOut; 00558 } 00559 00567 private function _parseXmlPostList(DOMDocument $response) 00568 { 00569 $rootNode = $response->documentElement; 00570 00571 if ($rootNode->nodeName == 'posts') { 00572 return new Zend_Service_Delicious_PostList($this, $rootNode->childNodes); 00573 } else { 00577 require_once 'Zend/Service/Delicious/Exception.php'; 00578 throw new Zend_Service_Delicious_Exception('del.icio.us web service has returned something odd!'); 00579 } 00580 } 00581 00589 private static function _evalXmlResult(DOMDocument $response) 00590 { 00591 $rootNode = $response->documentElement; 00592 00593 if ($rootNode && $rootNode->nodeName == 'result') { 00594 00595 if ($rootNode->hasAttribute('code')) { 00596 $strResponse = $rootNode->getAttribute('code'); 00597 } else { 00598 $strResponse = $rootNode->nodeValue; 00599 } 00600 00601 if ($strResponse != 'done' && $strResponse != 'ok') { 00605 require_once 'Zend/Service/Delicious/Exception.php'; 00606 throw new Zend_Service_Delicious_Exception("del.icio.us web service: '{$strResponse}'"); 00607 } 00608 } else { 00612 require_once 'Zend/Service/Delicious/Exception.php'; 00613 throw new Zend_Service_Delicious_Exception('del.icio.us web service has returned something odd!'); 00614 } 00615 } 00616 }