|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00038 class Zend_Service_Technorati 00039 { 00041 const API_URI_BASE = 'http://api.technorati.com'; 00042 00044 const API_PATH_COSMOS = '/cosmos'; 00045 const API_PATH_SEARCH = '/search'; 00046 const API_PATH_TAG = '/tag'; 00047 const API_PATH_DAILYCOUNTS = '/dailycounts'; 00048 const API_PATH_TOPTAGS = '/toptags'; 00049 const API_PATH_BLOGINFO = '/bloginfo'; 00050 const API_PATH_BLOGPOSTTAGS = '/blogposttags'; 00051 const API_PATH_GETINFO = '/getinfo'; 00052 const API_PATH_KEYINFO = '/keyinfo'; 00053 00055 const PARAM_LIMIT_MIN_VALUE = 1; 00056 const PARAM_LIMIT_MAX_VALUE = 100; 00057 const PARAM_DAYS_MIN_VALUE = 1; 00058 const PARAM_DAYS_MAX_VALUE = 180; 00059 const PARAM_START_MIN_VALUE = 1; 00060 00061 00068 protected $_apiKey; 00069 00076 protected $_restClient; 00077 00078 00085 public function __construct($apiKey) 00086 { 00087 iconv_set_encoding('output_encoding', 'UTF-8'); 00088 iconv_set_encoding('input_encoding', 'UTF-8'); 00089 iconv_set_encoding('internal_encoding', 'UTF-8'); 00090 00091 $this->_apiKey = $apiKey; 00092 } 00093 00094 00141 public function cosmos($url, $options = null) 00142 { 00143 static $defaultOptions = array( 'type' => 'link', 00144 'start' => 1, 00145 'limit' => 20, 00146 'current' => 'yes', 00147 'format' => 'xml', 00148 'claim' => 0, 00149 'highlight' => 1, 00150 ); 00151 00152 $options['url'] = $url; 00153 00154 $options = $this->_prepareOptions($options, $defaultOptions); 00155 $this->_validateCosmos($options); 00156 $response = $this->_makeRequest(self::API_PATH_COSMOS, $options); 00157 $dom = $this->_convertResponseAndCheckContent($response); 00158 00162 require_once 'Zend/Service/Technorati/CosmosResultSet.php'; 00163 return new Zend_Service_Technorati_CosmosResultSet($dom, $options); 00164 } 00165 00207 public function search($query, $options = null) 00208 { 00209 static $defaultOptions = array( 'start' => 1, 00210 'limit' => 20, 00211 'format' => 'xml', 00212 'claim' => 0); 00213 00214 $options['query'] = $query; 00215 00216 $options = $this->_prepareOptions($options, $defaultOptions); 00217 $this->_validateSearch($options); 00218 $response = $this->_makeRequest(self::API_PATH_SEARCH, $options); 00219 $dom = $this->_convertResponseAndCheckContent($response); 00220 00224 require_once 'Zend/Service/Technorati/SearchResultSet.php'; 00225 return new Zend_Service_Technorati_SearchResultSet($dom, $options); 00226 } 00227 00254 public function tag($tag, $options = null) 00255 { 00256 static $defaultOptions = array( 'start' => 1, 00257 'limit' => 20, 00258 'format' => 'xml', 00259 'excerptsize' => 100, 00260 'topexcerptsize' => 150); 00261 00262 $options['tag'] = $tag; 00263 00264 $options = $this->_prepareOptions($options, $defaultOptions); 00265 $this->_validateTag($options); 00266 $response = $this->_makeRequest(self::API_PATH_TAG, $options); 00267 $dom = $this->_convertResponseAndCheckContent($response); 00268 00272 require_once 'Zend/Service/Technorati/TagResultSet.php'; 00273 return new Zend_Service_Technorati_TagResultSet($dom, $options); 00274 } 00275 00292 public function dailyCounts($query, $options = null) 00293 { 00294 static $defaultOptions = array( 'days' => 180, 00295 'format' => 'xml' 00296 ); 00297 00298 $options['q'] = $query; 00299 00300 $options = $this->_prepareOptions($options, $defaultOptions); 00301 $this->_validateDailyCounts($options); 00302 $response = $this->_makeRequest(self::API_PATH_DAILYCOUNTS, $options); 00303 $dom = $this->_convertResponseAndCheckContent($response); 00304 00308 require_once 'Zend/Service/Technorati/DailyCountsResultSet.php'; 00309 return new Zend_Service_Technorati_DailyCountsResultSet($dom); 00310 } 00311 00331 public function topTags($options = null) 00332 { 00333 static $defaultOptions = array( 'start' => 1, 00334 'limit' => 20, 00335 'format' => 'xml' 00336 ); 00337 00338 $options = $this->_prepareOptions($options, $defaultOptions); 00339 $this->_validateTopTags($options); 00340 $response = $this->_makeRequest(self::API_PATH_TOPTAGS, $options); 00341 $dom = $this->_convertResponseAndCheckContent($response); 00342 00346 require_once 'Zend/Service/Technorati/TagsResultSet.php'; 00347 return new Zend_Service_Technorati_TagsResultSet($dom); 00348 } 00349 00360 public function blogInfo($url, $options = null) 00361 { 00362 static $defaultOptions = array( 'format' => 'xml' 00363 ); 00364 00365 $options['url'] = $url; 00366 00367 $options = $this->_prepareOptions($options, $defaultOptions); 00368 $this->_validateBlogInfo($options); 00369 $response = $this->_makeRequest(self::API_PATH_BLOGINFO, $options); 00370 $dom = $this->_convertResponseAndCheckContent($response); 00371 00375 require_once 'Zend/Service/Technorati/BlogInfoResult.php'; 00376 return new Zend_Service_Technorati_BlogInfoResult($dom); 00377 } 00378 00401 public function blogPostTags($url, $options = null) 00402 { 00403 static $defaultOptions = array( 'start' => 1, 00404 'limit' => 20, 00405 'format' => 'xml' 00406 ); 00407 00408 $options['url'] = $url; 00409 00410 $options = $this->_prepareOptions($options, $defaultOptions); 00411 $this->_validateBlogPostTags($options); 00412 $response = $this->_makeRequest(self::API_PATH_BLOGPOSTTAGS, $options); 00413 $dom = $this->_convertResponseAndCheckContent($response); 00414 00418 require_once 'Zend/Service/Technorati/TagsResultSet.php'; 00419 return new Zend_Service_Technorati_TagsResultSet($dom); 00420 } 00421 00438 public function getInfo($username, $options = null) 00439 { 00440 static $defaultOptions = array('format' => 'xml'); 00441 00442 $options['username'] = $username; 00443 00444 $options = $this->_prepareOptions($options, $defaultOptions); 00445 $this->_validateGetInfo($options); 00446 $response = $this->_makeRequest(self::API_PATH_GETINFO, $options); 00447 $dom = $this->_convertResponseAndCheckContent($response); 00448 00452 require_once 'Zend/Service/Technorati/GetInfoResult.php'; 00453 return new Zend_Service_Technorati_GetInfoResult($dom); 00454 } 00455 00466 public function keyInfo() 00467 { 00468 static $defaultOptions = array(); 00469 00470 $options = $this->_prepareOptions(array(), $defaultOptions); 00471 // you don't need to validate this request 00472 // because key is the only mandatory element 00473 // and it's already set in #_prepareOptions 00474 $response = $this->_makeRequest(self::API_PATH_KEYINFO, $options); 00475 $dom = $this->_convertResponseAndCheckContent($response); 00476 00480 require_once 'Zend/Service/Technorati/KeyInfoResult.php'; 00481 return new Zend_Service_Technorati_KeyInfoResult($dom, $this->_apiKey); 00482 } 00483 00484 00490 public function getApiKey() 00491 { 00492 return $this->_apiKey; 00493 } 00494 00503 public function getRestClient() 00504 { 00505 if ($this->_restClient === null) { 00509 require_once 'Zend/Rest/Client.php'; 00510 $this->_restClient = new Zend_Rest_Client(self::API_URI_BASE); 00511 } 00512 00513 return $this->_restClient; 00514 } 00515 00528 public function setApiKey($key) 00529 { 00530 $this->_apiKey = $key; 00531 return $this; 00532 } 00533 00534 00543 protected function _validateCosmos(array $options) 00544 { 00545 static $validOptions = array('key', 'url', 00546 'type', 'limit', 'start', 'current', 'claim', 'highlight', 'format'); 00547 00548 // Validate keys in the $options array 00549 $this->_compareOptions($options, $validOptions); 00550 // Validate url (required) 00551 $this->_validateOptionUrl($options); 00552 // Validate limit (optional) 00553 $this->_validateOptionLimit($options); 00554 // Validate start (optional) 00555 $this->_validateOptionStart($options); 00556 // Validate format (optional) 00557 $this->_validateOptionFormat($options); 00558 // Validate type (optional) 00559 $this->_validateInArrayOption('type', $options, array('link', 'weblog')); 00560 // Validate claim (optional) 00561 $this->_validateOptionClaim($options); 00562 // Validate highlight (optional) 00563 $this->_validateIntegerOption('highlight', $options); 00564 // Validate current (optional) 00565 if (isset($options['current'])) { 00566 $tmp = (int) $options['current']; 00567 $options['current'] = $tmp ? 'yes' : 'no'; 00568 } 00569 00570 } 00571 00580 protected function _validateSearch(array $options) 00581 { 00582 static $validOptions = array('key', 'query', 00583 'language', 'authority', 'limit', 'start', 'claim', 'format'); 00584 00585 // Validate keys in the $options array 00586 $this->_compareOptions($options, $validOptions); 00587 // Validate query (required) 00588 $this->_validateMandatoryOption('query', $options); 00589 // Validate authority (optional) 00590 $this->_validateInArrayOption('authority', $options, array('n', 'a1', 'a4', 'a7')); 00591 // Validate limit (optional) 00592 $this->_validateOptionLimit($options); 00593 // Validate start (optional) 00594 $this->_validateOptionStart($options); 00595 // Validate claim (optional) 00596 $this->_validateOptionClaim($options); 00597 // Validate format (optional) 00598 $this->_validateOptionFormat($options); 00599 } 00600 00609 protected function _validateTag(array $options) 00610 { 00611 static $validOptions = array('key', 'tag', 00612 'limit', 'start', 'excerptsize', 'topexcerptsize', 'format'); 00613 00614 // Validate keys in the $options array 00615 $this->_compareOptions($options, $validOptions); 00616 // Validate query (required) 00617 $this->_validateMandatoryOption('tag', $options); 00618 // Validate limit (optional) 00619 $this->_validateOptionLimit($options); 00620 // Validate start (optional) 00621 $this->_validateOptionStart($options); 00622 // Validate excerptsize (optional) 00623 $this->_validateIntegerOption('excerptsize', $options); 00624 // Validate excerptsize (optional) 00625 $this->_validateIntegerOption('topexcerptsize', $options); 00626 // Validate format (optional) 00627 $this->_validateOptionFormat($options); 00628 } 00629 00630 00639 protected function _validateDailyCounts(array $options) 00640 { 00641 static $validOptions = array('key', 'q', 00642 'days', 'format'); 00643 00644 // Validate keys in the $options array 00645 $this->_compareOptions($options, $validOptions); 00646 // Validate q (required) 00647 $this->_validateMandatoryOption('q', $options); 00648 // Validate format (optional) 00649 $this->_validateOptionFormat($options); 00650 // Validate days (optional) 00651 if (isset($options['days'])) { 00652 $options['days'] = (int) $options['days']; 00653 if ($options['days'] < self::PARAM_DAYS_MIN_VALUE || 00654 $options['days'] > self::PARAM_DAYS_MAX_VALUE) { 00658 require_once 'Zend/Service/Technorati/Exception.php'; 00659 throw new Zend_Service_Technorati_Exception( 00660 "Invalid value '" . $options['days'] . "' for 'days' option"); 00661 } 00662 } 00663 } 00664 00673 protected function _validateGetInfo(array $options) 00674 { 00675 static $validOptions = array('key', 'username', 00676 'format'); 00677 00678 // Validate keys in the $options array 00679 $this->_compareOptions($options, $validOptions); 00680 // Validate username (required) 00681 $this->_validateMandatoryOption('username', $options); 00682 // Validate format (optional) 00683 $this->_validateOptionFormat($options); 00684 } 00685 00694 protected function _validateTopTags(array $options) 00695 { 00696 static $validOptions = array('key', 00697 'limit', 'start', 'format'); 00698 00699 // Validate keys in the $options array 00700 $this->_compareOptions($options, $validOptions); 00701 // Validate limit (optional) 00702 $this->_validateOptionLimit($options); 00703 // Validate start (optional) 00704 $this->_validateOptionStart($options); 00705 // Validate format (optional) 00706 $this->_validateOptionFormat($options); 00707 } 00708 00717 protected function _validateBlogInfo(array $options) 00718 { 00719 static $validOptions = array('key', 'url', 00720 'format'); 00721 00722 // Validate keys in the $options array 00723 $this->_compareOptions($options, $validOptions); 00724 // Validate url (required) 00725 $this->_validateOptionUrl($options); 00726 // Validate format (optional) 00727 $this->_validateOptionFormat($options); 00728 } 00729 00738 protected function _validateBlogPostTags(array $options) 00739 { 00740 static $validOptions = array('key', 'url', 00741 'limit', 'start', 'format'); 00742 00743 // Validate keys in the $options array 00744 $this->_compareOptions($options, $validOptions); 00745 // Validate url (required) 00746 $this->_validateOptionUrl($options); 00747 // Validate limit (optional) 00748 $this->_validateOptionLimit($options); 00749 // Validate start (optional) 00750 $this->_validateOptionStart($options); 00751 // Validate format (optional) 00752 $this->_validateOptionFormat($options); 00753 } 00754 00765 protected function _validateInArrayOption($name, $options, array $array) 00766 { 00767 if (isset($options[$name]) && !in_array($options[$name], $array)) { 00771 require_once 'Zend/Service/Technorati/Exception.php'; 00772 throw new Zend_Service_Technorati_Exception( 00773 "Invalid value '{$options[$name]}' for '$name' option"); 00774 } 00775 } 00776 00785 protected function _validateMandatoryOption($name, $options) 00786 { 00787 if (!isset($options[$name]) || !trim($options[$name])) { 00791 require_once 'Zend/Service/Technorati/Exception.php'; 00792 throw new Zend_Service_Technorati_Exception( 00793 "Empty value for '$name' option"); 00794 } 00795 } 00796 00804 protected function _validateIntegerOption($name, $options) 00805 { 00806 if (isset($options[$name])) { 00807 $options[$name] = (int) $options[$name]; 00808 } 00809 } 00810 00821 protected function _makeRequest($path, $options = array()) 00822 { 00823 $restClient = $this->getRestClient(); 00824 $restClient->getHttpClient()->resetParameters(); 00825 $response = $restClient->restGet($path, $options); 00826 self::_checkResponse($response); 00827 return $response; 00828 } 00829 00837 protected function _validateOptionClaim(array $options) 00838 { 00839 $this->_validateIntegerOption('claim', $options); 00840 } 00841 00851 protected function _validateOptionFormat(array $options) 00852 { 00853 if (isset($options['format']) && $options['format'] != 'xml') { 00857 require_once 'Zend/Service/Technorati/Exception.php'; 00858 throw new Zend_Service_Technorati_Exception( 00859 "Invalid value '" . $options['format'] . "' for 'format' option. " . 00860 "Zend_Service_Technorati supports only 'xml'"); 00861 } 00862 } 00863 00874 protected function _validateOptionLimit(array $options) 00875 { 00876 if (!isset($options['limit'])) return; 00877 00878 $options['limit'] = (int) $options['limit']; 00879 if ($options['limit'] < self::PARAM_LIMIT_MIN_VALUE || 00880 $options['limit'] > self::PARAM_LIMIT_MAX_VALUE) { 00884 require_once 'Zend/Service/Technorati/Exception.php'; 00885 throw new Zend_Service_Technorati_Exception( 00886 "Invalid value '" . $options['limit'] . "' for 'limit' option"); 00887 } 00888 } 00889 00899 protected function _validateOptionStart(array $options) 00900 { 00901 if (!isset($options['start'])) return; 00902 00903 $options['start'] = (int) $options['start']; 00904 if ($options['start'] < self::PARAM_START_MIN_VALUE) { 00908 require_once 'Zend/Service/Technorati/Exception.php'; 00909 throw new Zend_Service_Technorati_Exception( 00910 "Invalid value '" . $options['start'] . "' for 'start' option"); 00911 } 00912 } 00913 00924 protected function _validateOptionUrl(array $options) 00925 { 00926 $this->_validateMandatoryOption('url', $options); 00927 } 00928 00938 protected static function _checkErrors(DomDocument $dom) 00939 { 00940 $xpath = new DOMXPath($dom); 00941 00942 $result = $xpath->query("/tapi/document/result/error"); 00943 if ($result->length >= 1) { 00944 $error = $result->item(0)->nodeValue; 00948 require_once 'Zend/Service/Technorati/Exception.php'; 00949 throw new Zend_Service_Technorati_Exception($error); 00950 } 00951 } 00952 00961 protected function _convertResponseAndCheckContent(Zend_Http_Response $response) 00962 { 00963 $dom = new DOMDocument(); 00964 $dom->loadXML($response->getBody()); 00965 self::_checkErrors($dom); 00966 return $dom; 00967 } 00968 00977 protected static function _checkResponse(Zend_Http_Response $response) 00978 { 00979 if ($response->isError()) { 00983 require_once 'Zend/Service/Technorati/Exception.php'; 00984 throw new Zend_Service_Technorati_Exception(sprintf( 00985 'Invalid response status code (HTTP/%s %s %s)', 00986 $response->getVersion(), $response->getStatus(), $response->getMessage())); 00987 } 00988 } 00989 00999 protected function _compareOptions(array $options, array $validOptions) 01000 { 01001 $difference = array_diff(array_keys($options), $validOptions); 01002 if ($difference) { 01006 require_once 'Zend/Service/Technorati/Exception.php'; 01007 throw new Zend_Service_Technorati_Exception( 01008 "The following parameters are invalid: '" . 01009 implode("', '", $difference) . "'"); 01010 } 01011 } 01012 01021 protected function _prepareOptions($options, array $defaultOptions) 01022 { 01023 $options = (array) $options; // force cast to convert null to array() 01024 $options['key'] = $this->_apiKey; 01025 $options = array_merge($defaultOptions, $options); 01026 return $options; 01027 } 01028 }