Changeset 10436
- Timestamp:
- 07/23/08 09:08:20 (5 years ago)
- Files:
-
- plugins/ysfAPIClientPlugin/trunk/lib/ysfAPIClient.class.php (modified) (13 diffs)
- plugins/ysfAPIClientPlugin/trunk/modules/ysfAPI/actions/actions.class.php (modified) (1 diff)
- plugins/ysfAPIClientPlugin/trunk/modules/ysfAPI/templates/indexSuccess.php (deleted)
- plugins/ysfAPIClientPlugin/trunk/modules/ysfAPI/templates/searchSuccess.php (added)
- plugins/ysfAPIClientPlugin/trunk/test (added)
- plugins/ysfAPIClientPlugin/trunk/test/unit (added)
- plugins/ysfAPIClientPlugin/trunk/test/unit/ysfAPIClientTest.php (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/ysfAPIClientPlugin/trunk/lib/ysfAPIClient.class.php
r10373 r10436 22 22 { 23 23 24 protected static $instance = null ;24 protected static $instance = null, $count = 0; 25 25 26 26 protected $context = null; … … 28 28 private $cache = null, 29 29 $batches = array(), 30 $timer = null, 30 31 $requests = array(), 31 32 $responses = array(), … … 80 81 $this->options = $options; 81 82 82 $this->options['debug'] = isset($this->options['debug']) ? (boolean) $this->options['debug'] : false;83 $this->options['servers'] = isset($this->options['servers']) ? $this->options['servers'] : array();84 85 83 $this->loadConfiguration(); 86 84 } … … 97 95 } 98 96 99 if(sfConfig::has('ysf_api_cache')) 100 { 101 $cacheAdapter = sfConfig::get('ysf_api_cache'); 102 $this->cache = new $cacheAdapter['class']($cacheAdapter['param']); 103 } 104 else 105 { 106 $this->cache = new sfNoCache(); 107 } 108 109 $this->options['servers'] = sfToolkit::arrayDeepMerge($this->options['servers'], sfConfig::get('ysf_api_servers', array())); 97 $this->options['enabled'] = sfConfig::get('ysf_api_enabled', true); 98 $this->options['debug'] = sfConfig::get('ysf_api_debug', false); 99 100 $this->options['servers'] = sfToolkit::arrayDeepMerge(isset($this->options['servers']) ? $this->options['servers'] : array(), sfConfig::get('ysf_api_servers', array())); 101 102 $this->options['cache'] = sfConfig::get('ysf_api_cache', array()); 103 $this->cache = isset($this->options['cache']['class']) ? new $this->options['cache']['class'](isset($this->options['cache']['param']) ? $this->options['cache']['param'] : array()) : new sfNoCache(); 110 104 } 111 105 … … 117 111 public function setOptions($options) 118 112 { 119 $options['debug'] = isset($options['debug']) ? (boolean) $options['debug'] : $this->options['debug'];120 121 113 $this->options = $options; 122 114 } … … 157 149 public function execute() 158 150 { 151 if($this->options['debug']) 152 { 153 $this->timer = sfTimerManager::getTimer('API Requests'); 154 } 155 156 $id = time(); // first batch id 159 157 if(is_array($this->options['servers']) && !empty($this->options['servers'])) 160 158 { … … 169 167 170 168 $options[$options['method']]['uri'] = isset($options[$options['method']]['uri']) ? $options[$options['method']]['uri'] : ''; 171 172 169 $options[$options['method']]['parameter'] = isset($options[$options['method']]['parameter']) ? $options[$options['method']]['parameter'] : ''; 173 170 174 171 if(isset($options['method']) && $options['method'] == 'compound') 175 172 { 176 $id = md5($ server . 'compound');173 $id = md5($id.$server); 177 174 $request = $this->encodeCompound($this->requests[$server], $this->options['servers'][$server]); 178 175 … … 180 177 { 181 178 $encodedRequest = $this->encodeRequest($request, $options['format']); 182 183 $this->batches[$id]['request'] = $encodedRequest;184 179 185 180 $curlRequest = $provider->buildCurlRequest($options[$options['method']]['uri'], array($options[$options['method']]['parameter'] => $encodedRequest)); … … 193 188 else // single requests (usually rest or similar) 194 189 { 195 foreach($this->requests[$server] as $id => $ request)190 foreach($this->requests[$server] as $id => $curlRequest) 196 191 { 197 if(!isset($this->responses[$id]) && ($ request != false))192 if(!isset($this->responses[$id]) && ($curlRequest != false)) 198 193 { 199 $this->batches[$id]['handle'] = $ request;194 $this->batches[$id]['handle'] = $curlRequest; 200 195 } 201 196 } … … 209 204 $mh = curl_multi_init(); 210 205 206 if($this->options['debug']) 207 { 208 $this->context->getLogger()->info(sprintf("{ysfAPIClient} Executing batch #%s with %s requests", ++self::$count, count($this->batches)), sfLogger::DEBUG); 209 } 210 211 211 foreach($this->batches as $id => $batch) 212 212 { 213 if($this->options['debug'])214 {215 $this->context->getLogger()->info(sprintf("executing batch id '%s'", $id), sfLogger::DEBUG);216 }217 218 213 curl_multi_add_handle($mh, $batch['handle']); 219 214 } … … 251 246 } 252 247 while($still_running); 248 249 if($this->options['debug']) 250 { 251 $this->timer->addTime(); 252 } 253 253 254 254 foreach($this->options['servers'] as $server => $options) … … 342 342 $serverParameters = $this->options['servers'][$server]; 343 343 344 $id = md5($server . $call . serialize($ options));344 $id = md5($server . $call . serialize($parameters)); 345 345 if(!isset($this->requests[$server][$id])) 346 346 { … … 353 353 if($this->options['debug']) 354 354 { 355 $this->context->getLogger()->info(sprintf("{ysfAPIClient} adding request '%s' with parameters %s", $call, str_replace(array('array (', '0 => ', ",\n )", ",\n)"), '', var_export($options, true)))); 356 $this->context->getLogger()->info(sprintf("{ysfAPIClient} adding request id '%s' for server '%s'", $id, $server)); 355 $this->context->getLogger()->info(sprintf("{ysfAPIClient} adding request '%s' to batch with parameters: %s", $call, str_replace(array('array (', '0 => ', ",\n )", ",\n)", "\n"), '', var_export($parameters, true)))); 357 356 } 358 357 } plugins/ysfAPIClientPlugin/trunk/modules/ysfAPI/actions/actions.class.php
r10373 r10436 25 25 * 26 26 */ 27 public function execute Index($request)27 public function executeSearch($request) 28 28 { 29 29 30 $timer = sfTimerManager::getTimer('API Requests'); 30 // search form 31 $this->form = new sfForm(); 32 $this->form->setWidgets(array('query' => new sfWidgetFormInput(array(), array('class' => 'search-box')))); 33 $this->form->setValidators(array('query' => new sfValidatorString(array('min_length' => 3)))); 34 $this->form->getWidgetSchema()->setNameFormat('search[%s]'); 35 $this->form->getWidgetSchema()->setFormFormatterName('list'); 31 36 32 $api = ysfAPIClient::getInstance(); 37 // search results 38 $this->results = array(); 33 39 34 $this->query = $request->getParameter('query', 'symfony'); 40 if($request->isMethod('post')) 41 { 42 // bind posted form 43 $this->form->bind($request->getParameter('search')); 35 44 36 $ysearch = $api->addRequest('yahoo.search', array('query' => $this->query), array(CURLOPT_USERAGENT => 'my Y! search')); 37 $gsearch = $api->addRequest('google.search', array('query' => $this->query), array(CURLOPT_USERAGENT => 'my G search')); 45 if($this->form->isValid()) 46 { 47 $this->query = $this->form->getValue('query'); 38 48 39 if($api->execute()) 40 { 41 $yJson = $api->getData($ysearch); 42 $gJson = $api->getData($gsearch); 49 $api = ysfAPIClient::getInstance(); 43 50 44 // normalization logic can be moved to provier 45 $this->results = array(); 46 foreach (array_merge($yJson->ysearchresponse->resultset_web, $gJson->responseData->results) as $data) 47 { 48 $result = new stdClass(); 49 $result->title = $data->title; 50 $result->abstract = isset($data->abstract) ? $data->abstract : $data->content; 51 $result->url = $data->url; 51 // parallel 52 $ysearch = $api->addRequest('yahoo.search', array('query' => $this->query), array(CURLOPT_USERAGENT => 'my Y! search')); 53 $gsearch = $api->addRequest('google.search', array('query' => $this->query), array(CURLOPT_USERAGENT => 'my G search')); 52 54 53 array_push($this->results, $result); 55 if($api->execute()) 56 { 57 $yJson = $api->getData($ysearch); 58 $gJson = $api->getData($gsearch); 59 60 if(isset($yJson->ysearchresponse) && isset($gJson->responseData)) 61 { 62 // normalization logic could be moved to each provider 63 foreach (array_merge($yJson->ysearchresponse->resultset_web, $gJson->responseData->results) as $data) 64 { 65 $result = new stdClass(); 66 $result->title = $data->title; 67 $result->abstract = isset($data->abstract) ? $data->abstract : $data->content; 68 $result->url = $data->url; 69 70 array_push($this->results, $result); 71 } 72 } 73 } 54 74 } 75 55 76 } 56 else57 {58 $this->results = array();59 }60 $timer->addTime();61 77 62 78 return sfView::SUCCESS;