-
Notifications
You must be signed in to change notification settings - Fork 4
Changes on google search client #18
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,16 +27,16 @@ | |
| * @see http://www.google.com/cse/docs/resultsxml.html | ||
| */ | ||
| class GoogleSearchClient extends Object { | ||
| protected $conn= NULL; | ||
| protected $gsaURL= NULL; | ||
| protected $unmarshaller= NULL; | ||
|
|
||
| /** | ||
| * Constructor | ||
| * | ||
| * @param var conn either a HttpConnection or a string | ||
| */ | ||
| public function __construct($conn) { | ||
| $this->conn= $conn instanceof HttpConnection ? $conn : new HttpConnection($conn); | ||
| public function __construct($gsaURL) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change makes it harder to test, because we can now no longer inject a
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about a protected method to return a new connection instance, e.g.: <?php
// ...
/**
* Returns a new connection for a given URL. Override for testing purposes,
* for example.
*
* @param string url
* @return peer.http.HttpConnection
*/
protected function connectionTo($url) {
return new HttpConnection($url);
}
// ...
?>We can then test by using a <?php
$fixture= newinstance('com.google.custom.GoogleSearchClient', array($url), '{
protected function connectionTo($url) {
return new DummyHttpConnection($url);
}
}');
?> |
||
| $this->gsaURL= $gsaURL; | ||
| $this->unmarshaller= new Unmarshaller(); | ||
| } | ||
|
|
||
|
|
@@ -49,6 +49,7 @@ public function __construct($conn) { | |
| * @see http://www.google.com/cse/docs/resultsxml.html#WebSearch_Query_Parameter_Definitions | ||
| */ | ||
| public function searchFor(GoogleSearchQuery $query, $params= array()) { | ||
| $conn= new HttpConnection($this->gsaURL . '/search'); | ||
|
|
||
| // Build query parameter list | ||
| $params['output']= 'xml_no_dtd'; | ||
|
|
@@ -59,17 +60,37 @@ public function searchFor(GoogleSearchQuery $query, $params= array()) { | |
| ($s= $query->getStart()) && $params['start']= $s; | ||
|
|
||
| // Retrieve result as XML | ||
| $r= $this->conn->get($params); | ||
| $r= $conn->get($params); | ||
| if (HttpConstants::STATUS_OK !== $r->statusCode()) { | ||
| throw new IOException('Non-OK response code '.$r->statusCode().': '.$r->message()); | ||
| } | ||
|
|
||
| // Unmarshal result | ||
| return $this->unmarshaller->unmarshalFrom( | ||
| new StreamInputSource($r->getInputStream(), $this->conn->toString()), | ||
| new StreamInputSource($r->getInputStream(), $conn->toString()), | ||
| 'com.google.search.custom.types.Response' | ||
| ); | ||
| } | ||
|
|
||
| public function getCluster(GoogleSearchQuery $query, $params= array()) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add some APIdocs here - and think about including a link to the documentation (e.g. https://developers.google.com/search-appliance/documentation/52/QuickStart/quick_start_se#dynamicresclust - maybe use the newest version, but you get the picture), as I'm not sure everyone knows what search "clusters" are. The term "cluster" usually refers to a load-balanced group of 2..n machines configured in the same way and serving the same purpose, not "group[ing] search results by topic".
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well , partly true - cluster is in textmining/datamining clustering has a different meaning. But you're definitly right, that there have to be a link to the google documentation |
||
| $conn= new HttpConnection($this->gsaURL . '/cluster'); | ||
|
|
||
| // Build query parameter list | ||
| $params['output']= 'xml_no_dtd'; | ||
| $params['coutput'] = 'xml'; | ||
| $params['q']= $query->getTerm(); | ||
| // Retrieve result as XML | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing newline before comment according to our coding standards: "The line above a comment has to be an empty line." |
||
| $r= $conn->get($params); | ||
| if (HttpConstants::STATUS_OK !== $r->statusCode()) { | ||
| throw new IOException('Non-OK response code '.$r->statusCode().': '.$r->message()); | ||
| } | ||
| // Unmarshal result | ||
| return $this->unmarshaller->unmarshalFrom( | ||
| new StreamInputSource($r->getInputStream(), $conn->toString()), | ||
| 'com.google.search.custom.types.ClusterSearchResponse' | ||
| ); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Creates a string representation of this object | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <?php | ||
| /* This class is part of the XP framework | ||
| * | ||
| * $Id$ | ||
| */ | ||
|
|
||
| $package= 'com.google.search.custom.types'; | ||
|
|
||
| /** | ||
| * Search result | ||
| * | ||
| */ | ||
| class com�google�search�custom�types�ClusterSearchResponse extends Object { | ||
| protected $time= 0.0; | ||
| protected $clusterProposals=array(); | ||
| /** | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +Whitespace |
||
| * Set result set | ||
| * | ||
| * @param com.google.search.custom.types.ResultSet res | ||
| */ | ||
| #[@xmlmapping(element= '/toplevel/Response/cluster/gcluster/label/@data')] | ||
| public function setResultSet($proposal) { | ||
| $this->clusterProposals[] = $proposal; | ||
| } | ||
|
|
||
| /** | ||
| * Returns result set | ||
| * | ||
| * @return com.google.search.custom.types.ResultSet | ||
| */ | ||
| public function getClusterProposals() { | ||
| return $this->clusterProposals; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a string representation of this result set | ||
| * | ||
| * @return string | ||
| */ | ||
| public function toString() { | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As @iigorr states - please make this return something meaningful
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems the |
||
| } | ||
| ?> | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The API doc is wrong here, since the constructor only accepts an URL string now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the hint :-)