Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Service/Yahoo.php
Go to the documentation of this file.
00001 <?php
00002 
00032 class Zend_Service_Yahoo
00033 {
00039     public $appId;
00040 
00046     protected $_rest;
00047 
00048 
00055     public function __construct($appId)
00056     {
00057         $this->appId = (string) $appId;
00061         require_once 'Zend/Rest/Client.php';
00062         $this->_rest = new Zend_Rest_Client('http://search.yahooapis.com');
00063     }
00064 
00065 
00080     public function inlinkDataSearch($query, array $options = array())
00081     {
00082         static $defaultOptions = array('results'     => '50',
00083                                        'start'    => 1);
00084 
00085         $options = $this->_prepareOptions($query, $options, $defaultOptions);
00086         $this->_validateInlinkDataSearch($options);
00087 
00088         $this->_rest->getHttpClient()->resetParameters();
00089         $this->_rest->setUri('http://search.yahooapis.com');
00090         $response = $this->_rest->restGet('/SiteExplorerService/V1/inlinkData', $options);
00091 
00092         if ($response->isError()) {
00096             require_once 'Zend/Service/Exception.php';
00097             throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
00098                                              $response->getStatus());
00099         }
00100 
00101         $dom = new DOMDocument();
00102         $dom->loadXML($response->getBody());
00103 
00104         self::_checkErrors($dom);
00105 
00109         require_once 'Zend/Service/Yahoo/InlinkDataResultSet.php';
00110         return new Zend_Service_Yahoo_InlinkDataResultSet($dom);
00111     }
00112 
00113 
00132     public function imageSearch($query, array $options = array())
00133     {
00134         static $defaultOptions = array('type'       => 'all',
00135                                        'results'    => 10,
00136                                        'start'      => 1,
00137                                        'format'     => 'any',
00138                                        'coloration' => 'any');
00139 
00140         $options = $this->_prepareOptions($query, $options, $defaultOptions);
00141 
00142         $this->_validateImageSearch($options);
00143 
00144         $this->_rest->getHttpClient()->resetParameters();
00145         $this->_rest->setUri('http://search.yahooapis.com');
00146         $response = $this->_rest->restGet('/ImageSearchService/V1/imageSearch', $options);
00147 
00148         if ($response->isError()) {
00152             require_once 'Zend/Service/Exception.php';
00153             throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
00154                                              $response->getStatus());
00155         }
00156 
00157         $dom = new DOMDocument();
00158         $dom->loadXML($response->getBody());
00159 
00160         self::_checkErrors($dom);
00161 
00165         require_once 'Zend/Service/Yahoo/ImageResultSet.php';
00166         return new Zend_Service_Yahoo_ImageResultSet($dom);
00167     }
00168 
00169 
00197     public function localSearch($query, array $options = array())
00198     {
00199         static $defaultOptions = array('results' => 10,
00200                                        'start'   => 1,
00201                                        'sort'    => 'distance',
00202                                        'radius'  => 5);
00203 
00204         $options = $this->_prepareOptions($query, $options, $defaultOptions);
00205 
00206         $this->_validateLocalSearch($options);
00207 
00208         $this->_rest->getHttpClient()->resetParameters();
00209         $this->_rest->setUri('http://local.yahooapis.com');
00210         $response = $this->_rest->restGet('/LocalSearchService/V1/localSearch', $options);
00211 
00212         if ($response->isError()) {
00216             require_once 'Zend/Service/Exception.php';
00217             throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
00218                                              $response->getStatus());
00219         }
00220 
00221         $dom = new DOMDocument();
00222         $dom->loadXML($response->getBody());
00223 
00224         self::_checkErrors($dom);
00225 
00229         require_once 'Zend/Service/Yahoo/LocalResultSet.php';
00230         return new Zend_Service_Yahoo_LocalResultSet($dom);
00231     }
00232 
00233 
00252     public function newsSearch($query, array $options = array())
00253     {
00254         static $defaultOptions = array('type'     => 'all',
00255                                        'start'    => 1,
00256                                        'sort'     => 'rank');
00257 
00258         $options = $this->_prepareOptions($query, $options, $defaultOptions);
00259 
00260         $this->_validateNewsSearch($options);
00261 
00262         $this->_rest->getHttpClient()->resetParameters();
00263         $this->_rest->setUri('http://search.yahooapis.com');
00264         $response = $this->_rest->restGet('/NewsSearchService/V1/newsSearch', $options);
00265 
00266         if ($response->isError()) {
00270             require_once 'Zend/Service/Exception.php';
00271             throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
00272                                              $response->getStatus());
00273         }
00274 
00275         $dom = new DOMDocument();
00276         $dom->loadXML($response->getBody());
00277 
00278         self::_checkErrors($dom);
00279 
00283         require_once 'Zend/Service/Yahoo/NewsResultSet.php';
00284         return new Zend_Service_Yahoo_NewsResultSet($dom);
00285     }
00286 
00287 
00301     public function pageDataSearch($query, array $options = array())
00302     {
00303         static $defaultOptions = array('results'     => '50',
00304                                        'start'    => 1);
00305 
00306         $options = $this->_prepareOptions($query, $options, $defaultOptions);
00307         $this->_validatePageDataSearch($options);
00308 
00309         $this->_rest->getHttpClient()->resetParameters();
00310         $this->_rest->setUri('http://search.yahooapis.com');
00311         $response = $this->_rest->restGet('/SiteExplorerService/V1/pageData', $options);
00312 
00313         if ($response->isError()) {
00317             require_once 'Zend/Service/Exception.php';
00318             throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
00319                                              $response->getStatus());
00320         }
00321 
00322         $dom = new DOMDocument();
00323         $dom->loadXML($response->getBody());
00324 
00325         self::_checkErrors($dom);
00326 
00330         require_once 'Zend/Service/Yahoo/PageDataResultSet.php';
00331         return new Zend_Service_Yahoo_PageDataResultSet($dom);
00332     }
00333 
00334 
00352     public function videoSearch($query, array $options = array())
00353     {
00354         static $defaultOptions = array('type'       => 'all',
00355                                        'results'    => 10,
00356                                        'start'      => 1,
00357                                        'format'     => 'any');
00358 
00359         $options = $this->_prepareOptions($query, $options, $defaultOptions);
00360 
00361         $this->_validateVideoSearch($options);
00362 
00363         $this->_rest->getHttpClient()->resetParameters();
00364         $this->_rest->setUri('http://search.yahooapis.com');
00365         $response = $this->_rest->restGet('/VideoSearchService/V1/videoSearch', $options);
00366 
00367         if ($response->isError()) {
00371             require_once 'Zend/Service/Exception.php';
00372             throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
00373                                              $response->getStatus());
00374         }
00375 
00376         $dom = new DOMDocument();
00377         $dom->loadXML($response->getBody());
00378 
00379         self::_checkErrors($dom);
00380 
00384         require_once 'Zend/Service/Yahoo/VideoResultSet.php';
00385         return new Zend_Service_Yahoo_VideoResultSet($dom);
00386     }
00387 
00388 
00410     public function webSearch($query, array $options = array())
00411     {
00412         static $defaultOptions = array('type'     => 'all',
00413                                        'start'    => 1,
00414                                        'results'  => 10,
00415                                        'format'   => 'any');
00416 
00417         $options = $this->_prepareOptions($query, $options, $defaultOptions);
00418         $this->_validateWebSearch($options);
00419 
00420         $this->_rest->getHttpClient()->resetParameters();
00421         $this->_rest->setUri('http://search.yahooapis.com');
00422         $response = $this->_rest->restGet('/WebSearchService/V1/webSearch', $options);
00423 
00424         if ($response->isError()) {
00428             require_once 'Zend/Service/Exception.php';
00429             throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
00430                                              $response->getStatus());
00431         }
00432 
00433         $dom = new DOMDocument();
00434         $dom->loadXML($response->getBody());
00435 
00436         self::_checkErrors($dom);
00437 
00441         require_once 'Zend/Service/Yahoo/WebResultSet.php';
00442         return new Zend_Service_Yahoo_WebResultSet($dom);
00443     }
00444 
00445 
00451     public function getRestClient()
00452     {
00453         return $this->_rest;
00454     }
00455 
00456 
00464     protected function _validateInlinkDataSearch(array $options)
00465     {
00466         $validOptions = array('appid', 'query', 'results', 'start', 'entire_site', 'omit_inlinks');
00467 
00468         $this->_compareOptions($options, $validOptions);
00469 
00473         require_once 'Zend/Validate/Between.php';
00474         $between = new Zend_Validate_Between(1, 100, true);
00475 
00476         if (isset($options['results']) && !$between->setMin(1)->setMax(100)->isValid($options['results'])) {
00480             require_once 'Zend/Service/Exception.php';
00481             throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
00482         }
00483 
00484         if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
00488             require_once 'Zend/Service/Exception.php';
00489             throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
00490         }
00491 
00492         if (isset($options['omit_inlinks'])) {
00493             $this->_validateInArray('omit_inlinks', $options['omit_inlinks'], array('none', 'domain', 'subdomain'));
00494         }
00495     }
00496 
00497 
00505     protected function _validateImageSearch(array $options)
00506     {
00507         $validOptions = array('appid', 'query', 'type', 'results', 'start', 'format', 'coloration', 'adult_ok');
00508 
00509         $this->_compareOptions($options, $validOptions);
00510 
00511         if (isset($options['type'])) {
00512             switch($options['type']) {
00513                 case 'all':
00514                 case 'any':
00515                 case 'phrase':
00516                     break;
00517                 default:
00521                     require_once 'Zend/Service/Exception.php';
00522                     throw new Zend_Service_Exception("Invalid value for option 'type': '{$options['type']}'");
00523             }
00524         }
00525 
00529         require_once 'Zend/Validate/Between.php';
00530         $between = new Zend_Validate_Between(1, 50, true);
00531 
00532         if (isset($options['results']) && !$between->setMin(1)->setMax(50)->isValid($options['results'])) {
00536             require_once 'Zend/Service/Exception.php';
00537             throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
00538         }
00539 
00540         if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
00544             require_once 'Zend/Service/Exception.php';
00545             throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
00546         }
00547 
00548         if (isset($options['format'])) {
00549             switch ($options['format']) {
00550                 case 'any':
00551                 case 'bmp':
00552                 case 'gif':
00553                 case 'jpeg':
00554                 case 'png':
00555                     break;
00556                 default:
00560                     require_once 'Zend/Service/Exception.php';
00561                     throw new Zend_Service_Exception("Invalid value for option 'format': {$options['format']}");
00562             }
00563         }
00564 
00565         if (isset($options['coloration'])) {
00566             switch ($options['coloration']) {
00567                 case 'any':
00568                 case 'color':
00569                 case 'bw':
00570                     break;
00571                 default:
00575                     require_once 'Zend/Service/Exception.php';
00576                     throw new Zend_Service_Exception("Invalid value for option 'coloration': "
00577                                                    . "{$options['coloration']}");
00578             }
00579         }
00580     }
00581 
00582 
00590     protected function _validateLocalSearch(array $options)
00591     {
00592         $validOptions = array('appid', 'query', 'results', 'start', 'sort', 'radius', 'street',
00593                               'city', 'state', 'zip', 'location', 'latitude', 'longitude');
00594 
00595         $this->_compareOptions($options, $validOptions);
00596 
00600         require_once 'Zend/Validate/Between.php';
00601         $between = new Zend_Validate_Between(1, 20, true);
00602 
00603         if (isset($options['results']) && !$between->setMin(1)->setMax(20)->isValid($options['results'])) {
00607             require_once 'Zend/Service/Exception.php';
00608             throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
00609         }
00610 
00611         if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
00615             require_once 'Zend/Service/Exception.php';
00616             throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
00617         }
00618 
00619         if (isset($options['longitude']) && !$between->setMin(-90)->setMax(90)->isValid($options['longitude'])) {
00623             require_once 'Zend/Service/Exception.php';
00624             throw new Zend_Service_Exception("Invalid value for option 'longitude': {$options['longitude']}");
00625         }
00626 
00627         if (isset($options['latitude']) && !$between->setMin(-180)->setMax(180)->isValid($options['latitude'])) {
00631             require_once 'Zend/Service/Exception.php';
00632             throw new Zend_Service_Exception("Invalid value for option 'latitude': {$options['latitude']}");
00633         }
00634 
00635         if (isset($options['zip']) && !preg_match('/(^\d{5}$)|(^\d{5}-\d{4}$)/', $options['zip'])) {
00639             require_once 'Zend/Service/Exception.php';
00640             throw new Zend_Service_Exception("Invalid value for option 'zip': {$options['zip']}");
00641         }
00642 
00643         $hasLocation = false;
00644         $locationFields = array('street', 'city', 'state', 'zip', 'location');
00645         foreach ($locationFields as $field) {
00646             if (isset($options[$field]) && $options[$field] != '') {
00647                 $hasLocation = true;
00648                 break;
00649             }
00650         }
00651 
00652         if (!$hasLocation && (!isset($options['latitude']) || !isset($options['longitude']))) {
00656             require_once 'Zend/Service/Exception.php';
00657             throw new Zend_Service_Exception('Location data are required but missing');
00658         }
00659 
00660         if (!in_array($options['sort'], array('relevance', 'title', 'distance', 'rating'))) {
00664             require_once 'Zend/Service/Exception.php';
00665             throw new Zend_Service_Exception("Invalid value for option 'sort': {$options['sort']}");
00666         }
00667     }
00668 
00669 
00677     protected function _validateNewsSearch(array $options)
00678     {
00679         $validOptions = array('appid', 'query', 'results', 'start', 'sort', 'language', 'type', 'site');
00680 
00681         $this->_compareOptions($options, $validOptions);
00682 
00686         require_once 'Zend/Validate/Between.php';
00687         $between = new Zend_Validate_Between(1, 50, true);
00688 
00689         if (isset($options['results']) && !$between->setMin(1)->setMax(50)->isValid($options['results'])) {
00693             require_once 'Zend/Service/Exception.php';
00694             throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
00695         }
00696 
00697         if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
00701             require_once 'Zend/Service/Exception.php';
00702             throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
00703         }
00704 
00705         if (isset($options['language'])) {
00706             $this->_validateLanguage($options['language']);
00707         }
00708 
00709         $this->_validateInArray('sort', $options['sort'], array('rank', 'date'));
00710         $this->_validateInArray('type', $options['type'], array('all', 'any', 'phrase'));
00711     }
00712 
00713 
00721     protected function _validatePageDataSearch(array $options)
00722     {
00723         $validOptions = array('appid', 'query', 'results', 'start', 'domain_only');
00724 
00725         $this->_compareOptions($options, $validOptions);
00726 
00730         require_once 'Zend/Validate/Between.php';
00731         $between = new Zend_Validate_Between(1, 100, true);
00732 
00733         if (isset($options['results']) && !$between->setMin(1)->setMax(100)->isValid($options['results'])) {
00737             require_once 'Zend/Service/Exception.php';
00738             throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
00739         }
00740 
00741         if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
00745             require_once 'Zend/Service/Exception.php';
00746             throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
00747         }
00748     }
00749 
00750 
00758     protected function _validateVideoSearch(array $options)
00759     {
00760         $validOptions = array('appid', 'query', 'type', 'results', 'start', 'format', 'adult_ok');
00761 
00762         $this->_compareOptions($options, $validOptions);
00763 
00764         if (isset($options['type'])) {
00765             $this->_validateInArray('type', $options['type'], array('all', 'any', 'phrase'));
00766         }
00767 
00771         require_once 'Zend/Validate/Between.php';
00772         $between = new Zend_Validate_Between(1, 50, true);
00773 
00774         if (isset($options['results']) && !$between->setMin(1)->setMax(50)->isValid($options['results'])) {
00778             require_once 'Zend/Service/Exception.php';
00779             throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
00780         }
00781 
00782         if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
00786             require_once 'Zend/Service/Exception.php';
00787             throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
00788         }
00789 
00790         if (isset($options['format'])) {
00791             $this->_validateInArray('format', $options['format'], array('any', 'avi', 'flash', 'mpeg', 'msmedia', 'quicktime', 'realmedia'));
00792         }
00793     }
00794 
00795 
00803     protected function _validateWebSearch(array $options)
00804     {
00805         $validOptions = array('appid', 'query', 'results', 'start', 'language', 'type', 'format', 'adult_ok',
00806                               'similar_ok', 'country', 'site', 'subscription', 'license', 'region');
00807 
00808         $this->_compareOptions($options, $validOptions);
00809 
00813         require_once 'Zend/Validate/Between.php';
00814         $between = new Zend_Validate_Between(1, 100, true);
00815 
00816         if (isset($options['results']) && !$between->setMin(1)->setMax(100)->isValid($options['results'])) {
00820             require_once 'Zend/Service/Exception.php';
00821             throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
00822         }
00823 
00824         if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
00828             require_once 'Zend/Service/Exception.php';
00829             throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
00830         }
00831 
00832         if (isset($options['language'])) {
00833             $this->_validateLanguage($options['language']);
00834         }
00835 
00836         $this->_validateInArray('type', $options['type'], array('all', 'any', 'phrase'));
00837         $this->_validateInArray('format', $options['format'], array('any', 'html', 'msword', 'pdf', 'ppt', 'rss',
00838                                                                     'txt', 'xls'));
00839         if (isset($options['license'])) {
00840             $this->_validateInArray('license', $options['license'], array('any', 'cc_any', 'cc_commercial',
00841                                                                       'cc_modifiable'));
00842         }
00843 
00844         if (isset($options['region'])){
00845             $this->_validateInArray('region', $options['region'], array('ar', 'au', 'at', 'br', 'ca', 'ct', 'dk', 'fi',
00846                                                                           'fr', 'de', 'in', 'id', 'it', 'my', 'mx',
00847                                                                           'nl', 'no', 'ph', 'ru', 'sg', 'es', 'se',
00848                                                                           'ch', 'th', 'uk', 'us'));
00849         }
00850     }
00851 
00852 
00861     protected function _prepareOptions($query, array $options, array $defaultOptions = array())
00862     {
00863         $options['appid'] = $this->appId;
00864         $options['query'] = (string) $query;
00865 
00866         return array_merge($defaultOptions, $options);
00867     }
00868 
00869 
00877     protected function _validateLanguage($lang)
00878     {
00879         $languages = array('ar', 'bg', 'ca', 'szh', 'tzh', 'hr', 'cs', 'da', 'nl', 'en', 'et', 'fi', 'fr', 'de', 'el',
00880             'he', 'hu', 'is', 'id', 'it', 'ja', 'ko', 'lv', 'lt', 'no', 'fa', 'pl', 'pt', 'ro', 'ru', 'sk', 'sr', 'sl',
00881             'es', 'sv', 'th', 'tr'
00882             );
00883         if (!in_array($lang, $languages)) {
00887             require_once 'Zend/Service/Exception.php';
00888             throw new Zend_Service_Exception("The selected language '$lang' is not supported");
00889         }
00890     }
00891 
00892 
00901     protected function _compareOptions(array $options, array $validOptions)
00902     {
00903         $difference = array_diff(array_keys($options), $validOptions);
00904         if ($difference) {
00908             require_once 'Zend/Service/Exception.php';
00909             throw new Zend_Service_Exception('The following parameters are invalid: ' . join(', ', $difference));
00910         }
00911     }
00912 
00913 
00923     protected function _validateInArray($name, $value, array $array)
00924     {
00925         if (!in_array($value, $array)) {
00929             require_once 'Zend/Service/Exception.php';
00930             throw new Zend_Service_Exception("Invalid value for option '$name': $value");
00931         }
00932     }
00933 
00934 
00942     protected static function _checkErrors(DOMDocument $dom)
00943     {
00944         $xpath = new DOMXPath($dom);
00945         $xpath->registerNamespace('yapi', 'urn:yahoo:api');
00946 
00947         if ($xpath->query('//yapi:Error')->length >= 1) {
00948             $message = $xpath->query('//yapi:Error/yapi:Message/text()')->item(0)->data;
00952             require_once 'Zend/Service/Exception.php';
00953             throw new Zend_Service_Exception($message);
00954         }
00955     }
00956 }
 All Data Structures Namespaces Files Functions Variables Enumerations