Commit 2f2e4336 authored by Bas de Nooijer's avatar Bas de Nooijer

- added support for XML responses to the stream adapter because ping cannot return json

- updated ping response, querytime is not available
- fixed a documentation error
parent 9c2d826b
...@@ -60,11 +60,12 @@ class Solarium_Client_Adapter_Stream extends Solarium_Client_Adapter ...@@ -60,11 +60,12 @@ class Solarium_Client_Adapter_Stream extends Solarium_Client_Adapter
*/ */
public function ping($query) public function ping($query)
{ {
$this->_getSolrData( $data = $this->_getSolrData(
new Solarium_Client_Request($this->_options, $query) new Solarium_Client_Request($this->_options, $query),
'xml'
); );
$response = new Solarium_Client_Response_Ping($query); $response = new Solarium_Client_Response_Ping($query, $data);
return $response->getResult(); return $response->getResult();
} }
...@@ -91,7 +92,7 @@ class Solarium_Client_Adapter_Stream extends Solarium_Client_Adapter ...@@ -91,7 +92,7 @@ class Solarium_Client_Adapter_Stream extends Solarium_Client_Adapter
* @param Solarium_Client_Request * @param Solarium_Client_Request
* @return array * @return array
*/ */
protected function _getSolrData($request) protected function _getSolrData($request, $mode = 'json')
{ {
if (null !== $request && null !== $request->getPostData()) { if (null !== $request && null !== $request->getPostData()) {
$context = stream_context_create( $context = stream_context_create(
...@@ -113,13 +114,39 @@ class Solarium_Client_Adapter_Stream extends Solarium_Client_Adapter ...@@ -113,13 +114,39 @@ class Solarium_Client_Adapter_Stream extends Solarium_Client_Adapter
throw new Solarium_Exception($error['message']); throw new Solarium_Exception($error['message']);
} }
if ($mode == 'json') {
$data = json_decode($data, true); $data = json_decode($data, true);
if (null === $data) { if (null === $data) {
throw new Solarium_Exception( throw new Solarium_Exception(
'Solr JSON response could not be decoded'); 'Solr JSON response could not be decoded');
} }
} else if ($mode == 'xml') {
$data = $this->simplexmlToArray(simplexml_load_string($data));
} else {
throw new Solarium_Exception('Unknown Solr client data mode');
}
return $data; return $data;
} }
function simplexmlToArray($xml) {
if (get_class($xml) == 'SimpleXMLElement') {
$attributes = $xml->attributes();
foreach($attributes as $k=>$v) {
if ($v) $a[$k] = (string) $v;
}
$x = $xml;
$xml = get_object_vars($xml);
}
if (is_array($xml)) {
if (count($xml) == 0) return (string) $x; // for CDATA
foreach($xml as $key=>$value) {
$r[$key] = $this->simplexmlToArray($value);
}
if (isset($a)) $r['@attributes'] = $a; // Attributes
return $r;
}
return (string) $xml;
}
} }
\ No newline at end of file
...@@ -40,8 +40,8 @@ class Solarium_Client_Response_Ping extends Solarium_Client_Response ...@@ -40,8 +40,8 @@ class Solarium_Client_Response_Ping extends Solarium_Client_Response
$resultClass = $this->_query->getOption('resultclass'); $resultClass = $this->_query->getOption('resultclass');
return new $resultClass( return new $resultClass(
$this->_data['responseHeader']['status'], $this->_data['ping']['status'],
$this->_data['responseHeader']['QTime'] 0
); );
} }
......
...@@ -240,7 +240,7 @@ class Solarium_Query_Update extends Solarium_Query ...@@ -240,7 +240,7 @@ class Solarium_Query_Update extends Solarium_Query
* *
* @param boolean $waitFlush * @param boolean $waitFlush
* @param boolean $waitSearcher * @param boolean $waitSearcher
* @param boolean $maxSegments * @param int $maxSegments
* @return Solarium_Query_Update Provides fluent interface * @return Solarium_Query_Update Provides fluent interface
*/ */
public function addOptimize($waitFlush = null, $waitSearcher = null, public function addOptimize($waitFlush = null, $waitSearcher = null,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment