Commit 27157240 authored by Bas de Nooijer's avatar Bas de Nooijer

Merge branch 'feature/new-structure' into develop

parents a41cc287 c09ef6f2
This diff is collapsed.
......@@ -56,58 +56,175 @@
*/
abstract class Solarium_Client_Adapter extends Solarium_Configurable
{
/**
* Default options
*
* The defaults match a standard Solr example instance as distributed by
* the Apache Lucene Solr project.
*
* @var array
*/
protected $_options = array(
'host' => '127.0.0.1',
'port' => 8983,
'path' => '/solr',
'core' => null,
'timeout' => 5,
);
/**
* Initialization hook
*
* In this case the path needs to be cleaned of trailing slashes.
* @see setPath()
*/
protected function _init()
{
foreach ($this->_options AS $name => $value) {
switch ($name) {
case 'path':
$this->setPath($value);
break;
}
}
}
/**
* Set host option
*
* @param string $host This can be a hostname or an IP address
* @return Solarium_Client Provides fluent interface
*/
public function setHost($host)
{
return $this->_setOption('host', $host);
}
/**
* Get host option
*
* @return string
*/
public function getHost()
{
return $this->getOption('host');
}
/**
* Set port option
*
* @param int $port Common values are 80, 8080 and 8983
* @return Solarium_Client Provides fluent interface
*/
public function setPort($port)
{
return $this->_setOption('port', $port);
}
/**
* Get port option
*
* @return int
*/
public function getPort()
{
return $this->getOption('port');
}
/**
* Set options
* Set path option
*
* Overrides any existing values
* If the path has a trailing slash it will be removed.
*
* @param string $path
* @return Solarium_Client Provides fluent interface
*/
public function setPath($path)
{
if (substr($path, -1) == '/') $path = substr($path, 0, -1);
return $this->_setOption('path', $path);
}
/**
* Get path option
*
* @param array $options
* @return void
*/
public function setOptions($options)
public function getPath()
{
$this->_setOptions($options, true);
return $this->getOption('path');
}
/**
* Execute a select query
* Set core option
*
* Abstract method to require an implementation inside all adapters.
* If the adapter cannot support this method it should implement a method
* that throws an exception.
* @param string $core
* @return Solarium_Client Provides fluent interface
*/
public function setCore($core)
{
return $this->_setOption('core', $core);
}
/**
* Get core option
*
* @abstract
* @param Solarium_Query_Select $query
* @return Solarium_Result_Select
* @return string
*/
abstract public function select($query);
public function getCore()
{
return $this->getOption('core');
}
/**
* Execute a ping query
* Set timeout option
*
* Abstract method to require an implementation inside all adapters.
* If the adapter cannot support this method it should implement a method
* that throws an exception.
* @param int $timeout
* @return Solarium_Client Provides fluent interface
*/
public function setTimeout($timeout)
{
return $this->_setOption('timeout', $timeout);
}
/**
* Get timeout option
*
* @abstract
* @param Solarium_Query_Ping $query
* @return boolean
* @return string
*/
abstract public function ping($query);
public function getTimeout()
{
return $this->getOption('timeout');
}
/**
* Execute an update query
* Execute a request
*
* Abstract method to require an implementation inside all adapters.
* If the adapter cannot support this method it should implement a method
* that throws an exception.
*
* @abstract
* @param Solarium_Query_Update $query
* @return Solarium_Result_Update
* @param Solarium_Client_Request $request
* @return Solarium_Client_Response
*/
abstract public function execute($request);
/**
* Get the base url for all requests
*
* Based on host, path, port and core options.
*
* @return void
*/
abstract public function update($query);
public function getBaseUri()
{
$uri = 'http://' . $this->getHost() . ':' . $this->getPort() . $this->getPath() . '/';
$core = $this->getCore();
if (!empty($core)) {
$uri .= $core.'/';
}
return $uri;
}
}
\ No newline at end of file
......@@ -45,66 +45,58 @@ class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter
{
/**
* Executes a select query
*
* @param Solarium_Query_Select $query
* @return Solarium_Result_Select
*/
public function select($query)
{
$request = new Solarium_Client_Request_Select($this->_options, $query);
$data = $this->_handleRequest($request);
$response = new Solarium_Client_Response_Select($query, $data);
return $response->getResult();
}
/**
* Executes a ping query
* Handle Solr communication
*
* @param Solarium_Query_Ping $query
* @return boolean
* @throws Solarium_Exception
* @param Solarium_Client_Request $request
* @return Solarium_Client_Response
*/
public function ping($query)
public function execute($request)
{
$request = new Solarium_Client_Request_Ping($this->_options, $query);
return (boolean)$this->_handleRequest($request);
$context = $this->createContext($request);
$uri = $this->getBaseUri() . $request->getUri();
list($data, $headers) = $this->_getData($uri, $context);
$this->check($data, $headers);
return new Solarium_Client_Response($data, $headers);
}
/**
* Executes an update query
*
* @param Solarium_Query_Update $query
* @return Solarium_Result_Update
* Check result of a request
*
* @throws Solarium_Client_HttpException
* @param string $data
* @param array $headers
* @return void
*/
public function update($query)
public function check($data, $headers)
{
$request = new Solarium_Client_Request_Update($this->_options, $query);
$data = $this->_handleRequest($request);
$response = new Solarium_Client_Response_Update($query, $data);
return $response->getResult();
// if there is no data and there are no headers it's a total failure,
// a connection to the host was impossible.
if (false === $data && count($headers) == 0) {
throw new Solarium_Client_HttpException("HTTP request failed");
}
}
/**
* Handle Solr communication
* Create a stream context for a request
*
* @throws Solarium_Exception
* @param Solarium_Client_Request
* @return array
* @param Solarium_Client_Request $request
* @return resource
*/
protected function _handleRequest($request)
public function createContext($request)
{
$method = $request->getMethod();
$context = stream_context_create(
array('http' => array(
'method' => $method,
'timeout' => $this->getOption('timeout')
'timeout' => $this->getTimeout()
))
);
if ($method == Solarium_Client_Request::POST) {
if ($method == Solarium_Client_Request::METHOD_POST) {
$data = $request->getRawData();
if (null !== $data) {
stream_context_set_option(
......@@ -113,94 +105,44 @@ class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter
'content',
$data
);
stream_context_set_option(
$context,
'http',
'header',
'Content-Type: text/xml; charset=UTF-8'
);
$request->addHeader('Content-Type: text/xml; charset=UTF-8');
}
}
$data = @file_get_contents($request->getUri(), false, $context);
// if there is no data and there are no headers it's a total failure,
// a connection to the host was impossible.
if (false === $data && !isset($http_response_header)) {
throw new Solarium_Client_HttpException("HTTP request failed");
$headers = $request->getHeaders();
if (count($headers) > 0) {
stream_context_set_option(
$context,
'http',
'header',
implode("\r\n", $headers)
);
}
$this->_checkHeaders($http_response_header);
if ($method == Solarium_Client_Request::HEAD) {
// HEAD request has no result data
return true;
} else {
if (false === $data) {
$error = error_get_last();
throw new Solarium_Exception($error['message']);
}
return $this->_jsonDecode($data);
}
return $context;
}
/**
* Check HTTP headers
* Execute request
*
* The status header is parsed, an exception will be thrown for an error
* code.
*
* @throws Solarium_Client_HttpException
* @param array $headers
* @return void
* @param string $uri
* @param resource $context
* @return array
*/
protected function _checkHeaders($headers)
protected function _getData($uri, $context)
{
// get the status header
$statusHeader = null;
foreach ($headers AS $header) {
if (substr($header, 0, 4) == 'HTTP') {
$statusHeader = $header;
break;
}
}
$data = @file_get_contents($uri, false, $context);
if (null == $statusHeader) {
throw new Solarium_Client_HttpException("No HTTP status found");
// @codeCoverageIgnoreStart
if (isset($http_response_header)) {
$headers = $http_response_header;
} else {
$headers = array();
}
// @codeCoverageIgnoreEnd
// parse header like "$statusInfo[1]" into code and message
// $statusInfo[1] = the HTTP response code
// $statusInfo[2] = the response message
$statusInfo = explode(' ', $statusHeader, 3);
// check status for error (range of 400 and 500)
$statusNum = floor($statusInfo[1] / 100);
if ($statusNum == 4 || $statusNum == 5) {
throw new Solarium_Client_HttpException(
$statusInfo[2],
$statusInfo[1]
);
}
return array($data, $headers);
}
/**
* Decode json response data
*
* @throws Solarium_Exception
* @param string $data
* @return string
*/
protected function _jsonDecode($data)
{
$data = json_decode($data, true);
if (null === $data) {
throw new Solarium_Exception(
'Solr JSON response could not be decoded'
);
}
return $data;
}
}
\ No newline at end of file
......@@ -48,7 +48,7 @@
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_Adapter_ZendHttp extends Solarium_Client_Adapter_Http
class Solarium_Client_Adapter_ZendHttp extends Solarium_Client_Adapter
{
/**
......@@ -63,7 +63,7 @@ class Solarium_Client_Adapter_ZendHttp extends Solarium_Client_Adapter_Http
*
* Overrides any existing values.
*
* If the options array has an 'adapteroptions' entry it is forwarded to the
* If the options array has an 'options' entry it is forwarded to the
* Zend_Http_Client. See the Zend_Http_Clientdocs for the many config
* options available.
*
......@@ -71,24 +71,25 @@ class Solarium_Client_Adapter_ZendHttp extends Solarium_Client_Adapter_Http
* method, like Zend_Config
*
* @param array|object $options
* @param boolean $overwrite
* @return Solarium_Client_Adapter_ZendHttp Provides fluent interface
*/
public function setOptions($options)
public function setOptions($options, $overwrite = false)
{
parent::setOptions($options);
parent::setOptions($options, $overwrite);
// forward options to zendHttp instance
if (null !== $this->_zendHttp) {
// forward timeout setting
$this->_zendHttp->setConfig(
array('timeout' => $this->getOption('timeout'))
);
$adapterOptions = array('timeout' => $this->getTimeout());
// forward adapter options if available
if (isset($this->_options['adapteroptions'])) {
$this->_zendHttp->setConfig($this->_options['adapteroptions']);
if (isset($this->_options['options'])) {
$adapterOptions = array_merge($adapterOptions, $this->_options['options']);
}
$this->_zendHttp->setConfig($adapterOptions);
}
return $this;
......@@ -126,10 +127,12 @@ class Solarium_Client_Adapter_ZendHttp extends Solarium_Client_Adapter_Http
{
if (null == $this->_zendHttp) {
$options = array('timeout' => $this->getOption('timeout'));
if (isset($this->_options['adapteroptions'])) {
// forward zendhttp options
if (isset($this->_options['options'])) {
$options = array_merge(
$options,
$this->_options['adapteroptions']
$this->_options['options']
);
}
......@@ -143,14 +146,14 @@ class Solarium_Client_Adapter_ZendHttp extends Solarium_Client_Adapter_Http
* Execute a Solr request using the Zend_Http_Client instance
*
* @param Solarium_Client_Request $request
* @return string
* @return Solarium_Client_Response
*/
protected function _handleRequest($request)
public function execute($request)
{
$client = $this->getZendHttp();
$client->setMethod($request->getMethod());
$client->setUri($request->getUri());
$client->setUri($this->getBaseUri() . $request->getUri());
$client->setRawData($request->getRawData());
$response = $client->request();
......@@ -163,23 +166,13 @@ class Solarium_Client_Adapter_ZendHttp extends Solarium_Client_Adapter_Http
);
}
if ($request->getMethod() == Solarium_Client_Request::HEAD) {
return true;
if ($request->getMethod() == Solarium_Client_Request::METHOD_HEAD) {
$data = '';
} else {
$data = $response->getBody();
$type = $response->getHeader('Content-Type');
switch ($type) {
case 'text/plain; charset=utf-8':
return $this->_jsonDecode($data);
break;
default:
throw new Solarium_Exception(
'Unknown Content-Type in ZendHttp adapter: ' . $type
);
break;
}
}
return new Solarium_Client_Response($data, $response->getHeaders());
}
}
\ No newline at end of file
......@@ -78,7 +78,7 @@ class Solarium_Client_HttpException extends Solarium_Exception
{
$this->_statusMessage = $statusMessage;
$message = 'Solr HTTP error, ' . $statusMessage;
$message = 'Solr HTTP error: ' . $statusMessage;
if (null !== $code) {
$message .= ' (' . $code . ')';
}
......
This diff is collapsed.
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
*
* @package Solarium
* @subpackage Client
*/
/**
* Class for building Solarium client requests
*
* @package Solarium
* @subpackage Client
*/
abstract class Solarium_Client_RequestBuilder
{
/**
* Render a param with localParams
*
* LocalParams can be use in various Solr GET params.
* @link http://wiki.apache.org/solr/LocalParams
*
* @param string $value
* @param array $localParams in key => value format
* @return string with Solr localparams syntax
*/
public function renderLocalParams($value, $localParams = array())
{
$params = '';
foreach ($localParams AS $paramName => $paramValue) {
if (empty($paramValue)) continue;
if (is_array($paramValue)) {
$paramValue = implode($paramValue, ',');
}
$params .= $paramName . '=' . $paramValue . ' ';
}
if ($params !== '') {
$value = '{!' . trim($params) . '}' . $value;
}
return $value;
}
/**
* Render a boolean attribute
*
* For use in building XML messages
*
* @param string $name
* @param boolean $value
* @return string
*/
public function boolAttrib($name, $value)
{
if (null !== $value) {
$value = (true == $value) ? 'true' : 'false';
return $this->attrib($name, $value);
} else {
return '';
}
}
/**
* Render an attribute
*
* For use in building XML messages
*
* @param string $name
* @param striung $value
* @return string
*/
public function attrib($name, $value)
{
if (null !== $value) {
return ' ' . $name . '="' . $value . '"';
} else {
return '';
}
}
}
\ No newline at end of file
......@@ -41,31 +41,22 @@
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_Request_Ping extends Solarium_Client_Request
class Solarium_Client_RequestBuilder_Ping extends Solarium_Client_RequestBuilder
{
/**
* Get uri
* Build request for a ping query
*
* Uses the default {@link buildUri()} method, no special uri needed in this
* case.
*
* @return string
* @param Solarium_Query_Ping $query
* @return Solarium_Client_Request
*/
public function getUri()
public function build($query)
{
return $this->buildUri();
}
$request = new Solarium_Client_Request;
$request->setHandler($query->getHandler());
$request->setMethod(Solarium_Client_Request::METHOD_HEAD);
/**
* Get HTTP request method
*
* Ping has no useful result data, so a more optimal HEAD request is used.
*
* @return string
*/
public function getMethod()
{
return self::HEAD;
return $request;
}
}
\ No newline at end of file
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
*
* @package Solarium
* @subpackage Client
*/
/**
* Build a select request
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_RequestBuilder_Select extends Solarium_Client_RequestBuilder
{
/**
* Build request for a select query
*
* @param Solarium_Query_Select $query
* @return Solarium_Client_Request
*/
public function build($query)
{
$request = new Solarium_Client_Request;
$request->setHandler($query->getHandler());
// add basic params to request
$request->addParam('q', $query->getQuery());
$request->addParam('start', $query->getStart());
$request->addParam('rows', $query->getRows());
$request->addParam('fl', implode(',', $query->getFields()));
$request->addParam('wt', 'json');
// add sort fields to request
$sort = array();
foreach ($query->getSorts() AS $field => $order) {
$sort[] = $field . ' ' . $order;
}
if (count($sort) !== 0) {
$request->addParam('sort', implode(',', $sort));
}
// add filterqueries to request
$filterQueries = $query->getFilterQueries();
if (count($filterQueries) !== 0) {
foreach ($filterQueries AS $filterQuery) {
$fq = $this->renderLocalParams(
$filterQuery->getQuery(),
array('tag' => $filterQuery->getTags())
);
$request->addParam('fq', $fq);
}
}
// add components to request
$types = $query->getComponentTypes();
foreach ($query->getComponents() as $component) {
$componentBuilderClass = $types[$component->getType()]['requestbuilder'];
if (!empty($componentBuilderClass)) {
$componentBuilder = new $componentBuilderClass;
$request = $componentBuilder->build($component, $request);
}
}
return $request;
}
}
\ No newline at end of file
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
*
* @package Solarium
* @subpackage Client
*/
/**
* Add select component dismax to the request
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_RequestBuilder_Select_Component_DisMax
{
/**
* Add request settings for Dismax
*
* @param Solarium_Query_Select_Component_Dismax $component
* @param Solarium_Client_Request $request
* @return Solarium_Client_Request
*/
public function build($component, $request)
{
// enable dismax
$request->addParam('defType', 'dismax');
$request->addParam('q.alt', $component->getQueryAlternative());
$request->addParam('qf', $component->getQueryFields());
$request->addParam('mm', $component->getMinimumMatch());
$request->addParam('pf', $component->getPhraseFields());
$request->addParam('ps', $component->getPhraseSlop());
$request->addParam('qs', $component->getQueryPhraseSlop());
$request->addParam('tie', $component->getTie());
$request->addParam('bq', $component->getBoostQuery());
$request->addParam('bf', $component->getBoostFunctions());
return $request;
}
}
\ No newline at end of file
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
*
* @package Solarium
* @subpackage Client
*/
/**
* Add select component Highlighting to the request
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_RequestBuilder_Select_Component_Highlighting
{
/**
* Add request settings for Highlighting
*
* @param Solarium_Query_Select_Component_Highlighting $component
* @param Solarium_Client_Request $request
* @return Solarium_Client_Request
*/
public function build($component, $request)
{
// enable highlighting
$request->addParam('hl', true);
$request->addParam('hl.fl', $component->getFields());
$request->addParam('hl.snippets', $component->getSnippets());
$request->addParam('hl.fragsize', $component->getFragSize());
$request->addParam('hl.mergeContiguous', $component->getMergeContiguous());
$request->addParam('hl.requireFieldMatch', $component->getRequireFieldMatch());
$request->addParam('hl.maxAnalyzedChars', $component->getMaxAnalyzedChars());
$request->addParam('hl.alternateField', $component->getAlternateField());
$request->addParam('hl.maxAlternateFieldLength', $component->getMaxAlternateFieldLength());
$request->addParam('hl.formatter', $component->getFormatter());
$request->addParam('hl.simple.pre', $component->getSimplePrefix());
$request->addParam('hl.simple.post', $component->getSimplePostfix());
$request->addParam('hl.fragmenter', $component->getFragmenter());
$request->addParam('hl.fragListBuilder', $component->getFragListBuilder());
$request->addParam('hl.fragmentsBuilder', $component->getFragmentsBuilder());
$request->addParam('hl.useFastVectorHighlighter', $component->getUseFastVectorHighlighter());
$request->addParam('hl.usePhraseHighlighter', $component->getUsePhraseHighlighter());
$request->addParam('hl.highlightMultiTerm', $component->getHighlightMultiTerm());
$request->addParam('hl.regex.slop', $component->getRegexSlop());
$request->addParam('hl.regex.pattern', $component->getRegexPattern());
$request->addParam('hl.regex.maxAnalyzedChars', $component->getRegexMaxAnalyzedChars());
return $request;
}
}
\ No newline at end of file
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
*
* @package Solarium
* @subpackage Client
*/
/**
* Add select component morelikethis to the request
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_RequestBuilder_Select_Component_MoreLikeThis
{
/**
* Add request settings for morelikethis
*
* @param Solarium_Query_Select_Component_MoreLikeThis $component
* @param Solarium_Client_Request $request
* @return Solarium_Client_Request
*/
public function build($component, $request)
{
// enable morelikethis
$request->addParam('mlt', true);
$request->addParam('mlt.fl', $component->getFields());
$request->addParam('mlt.mintf', $component->getMinimumTermFrequency());
$request->addParam('mlt.mindf', $component->getMinimumDocumentFrequency());
$request->addParam('mlt.minwl', $component->getMinimumWordLength());
$request->addParam('mlt.maxwl', $component->getMaximumWordLength());
$request->addParam('mlt.maxqt', $component->getMaximumQueryTerms());
$request->addParam('mlt.maxntp', $component->getMaximumNumberOfTokens());
$request->addParam('mlt.boost', $component->getBoost());
$request->addParam('mlt.qf', $component->getQueryFields());
$request->addParam('mlt.count', $component->getCount());
return $request;
}
}
\ No newline at end of file
......@@ -41,34 +41,24 @@
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_Request_Update extends Solarium_Client_Request
class Solarium_Client_RequestBuilder_Update extends Solarium_Client_RequestBuilder
{
/**
* Get HTTP request method
* Build request for an update query
*
* Update uses raw POST data so a POST method has to be used.
*
* @return string
* @param Solarium_Query_Update $query
* @return Solarium_Client_Request
*/
public function getMethod()
public function build($query)
{
return self::POST;
}
/**
* Get uri
*
* Return the default url with the addition of the wt param.
* This enables a JSON response, that is the easiest and most efficient
* format to decode in the response handler.
*
* @return string
*/
public function getUri()
{
$this->_params = array('wt' => 'json');
return $this->buildUri();
$request = new Solarium_Client_Request;
$request->setHandler($query->getHandler());
$request->setMethod(Solarium_Client_Request::METHOD_POST);
$request->addParam('wt', 'json');
$request->setRawData($this->getRawData($query));
return $request;
}
/**
......@@ -76,27 +66,28 @@ class Solarium_Client_Request_Update extends Solarium_Client_Request
*
* Each commandtype is delegated to a separate builder method.
*
* @param Solarium_Query_Update $query
* @throws Solarium_Exception
* @return string
*/
public function getRawData()
public function getRawData($query)
{
$xml = '<update>';
foreach ($this->_query->getCommands() AS $command) {
foreach ($query->getCommands() AS $command) {
switch ($command->getType()) {
case Solarium_Query_Update_Command::ADD:
case Solarium_Query_Update::COMMAND_ADD:
$xml .= $this->buildAddXml($command);
break;
case Solarium_Query_Update_Command::DELETE:
case Solarium_Query_Update::COMMAND_DELETE:
$xml .= $this->buildDeleteXml($command);
break;
case Solarium_Query_Update_Command::OPTIMIZE:
case Solarium_Query_Update::COMMAND_OPTIMIZE:
$xml .= $this->buildOptimizeXml($command);
break;
case Solarium_Query_Update_Command::COMMIT:
case Solarium_Query_Update::COMMAND_COMMIT:
$xml .= $this->buildCommitXml($command);
break;
case Solarium_Query_Update_Command::ROLLBACK:
case Solarium_Query_Update::COMMAND_ROLLBACK:
$xml .= $this->buildRollbackXml();
break;
default:
......
......@@ -36,63 +36,124 @@
*/
/**
* Base class for handling Solr HTTP responses
*
* Most {@link Solarium_Client_Adapter} implementations will use HTTP for
* communicating with Solr. While the HTTP part is adapter-specific, the parsing
* of the response into Solarium_Result classes is not. This abstract class is
* the base for several response handlers that do just that for the various
* querytypes.
* Class for describing a response
*
* @package Solarium
* @subpackage Client
*/
abstract class Solarium_Client_Response
class Solarium_Client_Response
{
/**
* Query instance
* Headers
*
* The query that was used for executing a request that led to this
* response. The query holds important settings for generating the right
* result, like the resultclass and documentclass settings.
* @var array
*/
protected $_headers;
/**
* Body
*
* @var Solarium_Query
* @var string
*/
protected $_query;
protected $_body;
/**
* Response data
* HTTP response code
*
* A (json)decoded HTTP response body data array.
* @var int
*/
protected $_statusCode;
/**
* HTTP response message
*
* @var array
* @var string
*/
protected $_data;
protected $_statusMessage;
/**
* Constructor
*
* @param Solarium_Query $query Query instance that was used for the request
* @param array $data Decoded data array of the HTTP response
* @param string $body
* @param array $headers
*/
public function __construct($query, $data = null)
public function __construct($body, $headers = array())
{
$this->_query = $query;
$this->_data = $data;
$this->_body = $body;
$this->_headers = $headers;
$this->_setHeaders($headers);
}
/**
* Get a Solarium_Result instance for the response
* Get body data
*
* When this method is called the actual response parsing is started.
* @return string
*/
public function getBody()
{
return $this->_body;
}
/**
* Get response headers
*
* @internal Must be implemented in descendents because this parsing is
* query specific.
* @return array
*/
public function getHeaders()
{
return $this->_headers;
}
/**
* Get status code
*
* @abstract
* @return mixed
* @return int
*/
abstract function getResult();
public function getStatusCode()
{
return $this->_statusCode;
}
/**
* Get status message
*
* @return string
*/
public function getStatusMessage()
{
return $this->_statusMessage;
}
/**
* Set headers
*
* @param array $headers
* @return void
*/
public function _setHeaders($headers)
{
$this->_headers = $headers;
// get the status header
$statusHeader = null;
foreach ($headers AS $header) {
if (substr($header, 0, 4) == 'HTTP') {
$statusHeader = $header;
break;
}
}
if (null == $statusHeader) {
throw new Solarium_Client_HttpException("No HTTP status found");
}
// parse header like "$statusInfo[1]" into code and message
// $statusInfo[1] = the HTTP response code
// $statusInfo[2] = the response message
$statusInfo = explode(' ', $statusHeader, 3);
$this->_statusCode = $statusInfo[1];
$this->_statusMessage = $statusInfo[2];
}
}
\ No newline at end of file
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
*
* @package Solarium
* @subpackage Client
*/
/**
* Base class for handling Solr response data
*
* Most {@link Solarium_Client_Adapter} implementations will use HTTP for
* communicating with Solr. While the HTTP part is adapter-specific, the parsing
* of the response into Solarium_Result classes is not. This abstract class is
* the base for several response handlers that do just that for the various
* querytypes.
*
* @package Solarium
* @subpackage Client
*/
abstract class Solarium_Client_ResponseParser
{
/**
* Get a Solarium_Result instance for the given data
*
* When this method is called the actual response parsing is started.
*
* @internal Must be implemented in descendents because this parsing is
* query specific.
*
* @abstract
*
* @param Solarium_Result $result
* @return mixed
*/
abstract function parse($result);
}
\ No newline at end of file
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
*
* @package Solarium
* @subpackage Client
*/
/**
* Parse select response data
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_ResponseParser_Select extends Solarium_Client_ResponseParser
{
/**
* Get result data for the response
*
* @param Solarium_Result_Select $result
* @return array
*/
public function parse($result)
{
$data = $result->getData();
$query = $result->getQuery();
// create document instances
$documentClass = $query->getOption('documentclass');
$documents = array();
if (isset($data['response']['docs'])) {
foreach ($data['response']['docs'] AS $doc) {
$fields = (array)$doc;
$documents[] = new $documentClass($fields);
}
}
// component results
$components = array();
$types = $query->getComponentTypes();
foreach ($query->getComponents() as $component) {
$componentParserClass = $types[$component->getType()]['responseparser'];
if (!empty($componentParserClass)) {
$componentParser = new $componentParserClass;
$components[$component->getType()] = $componentParser->parse($query, $component, $data);
}
}
return array(
'status' => $data['responseHeader']['status'],
'queryTime' => $data['responseHeader']['QTime'],
'numfound' => $data['response']['numFound'],
'documents' => $documents,
'components' => $components,
);
}
}
\ No newline at end of file
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
*
* @package Solarium
* @subpackage Client
*/
/**
* Parse select component Highlighting result from the data
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_ResponseParser_Select_Component_Highlighting
{
/**
* Parse result data into result objects
*
* @param Solarium_Query_Select $query
* @param Solarium_Query_Select_Component_Highlighting $highlighting
* @param array $data
* @return Solarium_Result_Select_Highlighting
*/
public function parse($query, $highlighting, $data)
{
$results = array();
if (isset($data['highlighting'])) {
$highlightResults = $data['highlighting'];
foreach ($highlightResults AS $key => $result) {
$results[$key] = new Solarium_Result_Select_Highlighting_Result(
$result
);
}
}
return new Solarium_Result_Select_Highlighting($results);
}
}
\ No newline at end of file
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
*
* @package Solarium
* @subpackage Client
*/
/**
* Parse select component MoreLikeThis result from the data
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_ResponseParser_Select_Component_MoreLikeThis
{
/**
* Parse result data into result objects
*
* @param Solarium_Query_Select $query
* @param Solarium_Query_Select_Component_MoreLikeThis $moreLikeThis
* @param array $data
* @return Solarium_Result_Select_MoreLikeThis
*/
public function parse($query, $moreLikeThis, $data)
{
$results = array();
if (isset($data['moreLikeThis'])) {
$documentClass = $query->getOption('documentclass');
$searchResults = $data['moreLikeThis'];
foreach ($searchResults AS $key => $result) {
// create document instances
$docs = array();
foreach ($result['docs'] AS $fields) {
$docs[] = new $documentClass($fields);
}
$results[$key] = new Solarium_Result_Select_MoreLikeThis_Result(
$result['numFound'],
$result['maxScore'],
$docs
);
}
}
return new Solarium_Result_Select_MoreLikeThis($results);
}
}
\ No newline at end of file
......@@ -38,29 +38,25 @@
/**
* Parse update response data
*
* Will create a result object based on response data of the type
* {@Solarium_Result_Update} (or your own resultclass setting)
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_Response_Update extends Solarium_Client_Response
class Solarium_Client_ResponseParser_Update extends Solarium_Client_ResponseParser
{
/**
* Get a result instance for the response
*
* When this method is called the actual response parsing is done.
* Parse response data
*
* @return mixed
* @param Solarium_Result_Update $result
* @return array
*/
public function getResult()
public function parse($result)
{
$resultClass = $this->_query->getOption('resultclass');
$data = $result->getData();
return new $resultClass(
$this->_data['responseHeader']['status'],
$this->_data['responseHeader']['QTime']
return array(
'status' => $data['responseHeader']['status'],
'queryTime' => $data['responseHeader']['QTime'],
);
}
......
......@@ -58,7 +58,7 @@ class Solarium_Configurable
* Constructor
*
* If options are passed they will be merged with {@link $_options} using
* the {@link _setOptions()} method.
* the {@link setOptions()} method.
*
* After handling the options the {@link _init()} method is called.
*
......@@ -68,9 +68,11 @@ class Solarium_Configurable
*/
public function __construct($options = null)
{
$this->_setOptions($options);
$this->_init();
if (null !== $options) {
$this->setOptions($options);
} else {
$this->_init();
}
}
/**
......@@ -87,7 +89,7 @@ class Solarium_Configurable
*
* @return void
*/
protected function _setOptions($options, $overwrite = false)
public function setOptions($options, $overwrite = false)
{
if (null !== $options) {
// first convert to array if needed
......@@ -105,6 +107,9 @@ class Solarium_Configurable
} else {
$this->_options = array_merge($this->_options, $options);
}
// re-init for new options
$this->_init();
}
}
......
......@@ -202,6 +202,19 @@ class Solarium_Document_ReadWrite extends Solarium_Document_ReadOnly
return $this->_boost;
}
/**
* Clear all fields
*
* @return Solarium_Document_ReadWrite Provides fluent interface
**/
public function clear()
{
$this->_fields = array();
$this->_fieldBoosts = array();
return $this;
}
/**
* Set field value
*
......
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
*
* @package Solarium
*/
/**
* Base class for plugins
*
* @package Solarium
* @subpackage Plugin
*/
abstract class Solarium_Plugin_Abstract extends Solarium_Configurable
{
/**
* Client instance
*
* @var Solarium_Client
*/
protected $_client;
/**
* Initialize
*
* This method is called when the plugin is registered to a client instance
*
* @param Solarium_Client $client
* @param array $options
*/
public function init($client, $options)
{
$this->_client = $client;
parent::__construct($options);
$this->_init();
}
/**
* Secondary init function
*
* This is an extension point for plugin implemenations
*
* @return void
*/
public function _init()
{
}
/**
*
* @param Solarium_Query $query
* @return void|Solarium_Client_Request
*/
public function preCreateRequest($query)
{
}
/**
* @param Solarium_Query $query
* @param Solarium_Client_Request $request
* @return void
*/
public function postCreateRequest($query, $request)
{
}
/**
* @param Solarium_Client_Request $request
* @return void|Solarium_Client_Response
*/
public function preExecuteRequest($request)
{
}
/**
* @param Solarium_Client_Request $request
* @param Solarium_Client_Response $response
* @return void
*/
public function postExecuteRequest($request, $response)
{
}
/**
* @param Solarium_Query $query
* @param Solarium_Client_Response $response
* @return void|Solarium_Result
*/
public function preCreateResult($query, $response)
{
}
/**
* @param Solarium_Query $query
* @param Solarium_Client_Response $response
* @param Solarium_Result $result
* @return void
*/
public function postCreateResult($query, $response, $result)
{
}
/**
* @param Solarium_Query $query
* @return void|Solarium_Result
*/
public function preExecute($query)
{
}
/**
* @param Solarium_Query $query
* @param Solarium_Result $result
* @return void
*/
public function postExecute($query, $result)
{
}
/**
* @param string $query
* @param mixed $options
* @return void|Solarium_Query
*/
public function preCreateQuery($type, $options)
{
}
/**
* @param string $query
* @param mixed $options
* @param Solarium_Query
* @return void
*/
public function postCreateQuery($type, $options, $query)
{
}
}
\ No newline at end of file
......@@ -41,8 +41,15 @@
* @package Solarium
* @subpackage Query
*/
class Solarium_Query extends Solarium_Configurable
abstract class Solarium_Query extends Solarium_Configurable
{
/**
* Get type for this query
*
* @return string
*/
abstract public function getType();
/**
* Set handler option
......
......@@ -48,6 +48,16 @@
class Solarium_Query_Ping extends Solarium_Query
{
/**
* Get type for this query
*
* @return string
*/
public function getType()
{
return Solarium_Client::QUERYTYPE_PING;
}
/**
* Default options
*
......
......@@ -54,6 +54,24 @@ class Solarium_Query_Select extends Solarium_Query
const SORT_DESC = 'desc';
const SORT_ASC = 'asc';
/**
* Query components
*/
const COMPONENT_FACETSET = 'facetset';
const COMPONENT_DISMAX = 'dismax';
const COMPONENT_MORELIKETHIS = 'morelikethis';
const COMPONENT_HIGHLIGHTING = 'highlighting';
/**
* Get type for this query
*
* @return string
*/
public function getType()
{
return Solarium_Client::QUERYTYPE_SELECT;
}
/**
* Default options
*
......@@ -69,6 +87,34 @@ class Solarium_Query_Select extends Solarium_Query
'fields' => '*,score',
);
/**
* Default select query component types
*
* @var array
*/
protected $_componentTypes = array(
self::COMPONENT_FACETSET => array(
'component' => 'Solarium_Query_Select_Component_FacetSet',
'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_FacetSet',
'responseparser' => 'Solarium_Client_ResponseParser_Select_Component_FacetSet',
),
self::COMPONENT_DISMAX => array(
'component' => 'Solarium_Query_Select_Component_DisMax',
'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_DisMax',
'responseparser' => null,
),
self::COMPONENT_MORELIKETHIS => array(
'component' => 'Solarium_Query_Select_Component_MoreLikeThis',
'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_MoreLikeThis',
'responseparser' => 'Solarium_Client_ResponseParser_Select_Component_MoreLikeThis',
),
self::COMPONENT_HIGHLIGHTING => array(
'component' => 'Solarium_Query_Select_Component_Highlighting',
'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_Highlighting',
'responseparser' => 'Solarium_Client_ResponseParser_Select_Component_Highlighting',
),
);
/**
* Fields to fetch
*
......@@ -77,11 +123,11 @@ class Solarium_Query_Select extends Solarium_Query
protected $_fields = array();
/**
* Fields to sort on
* Items to sort on
*
* @var array
*/
protected $_sortFields = array();
protected $_sorts = array();
/**
* Filterqueries
......@@ -123,7 +169,7 @@ class Solarium_Query_Select extends Solarium_Query
$this->addFilterQueries($value);
break;
case 'sort':
$this->addSortFields($value);
$this->addSorts($value);
break;
case 'fields':
$this->addFields($value);
......@@ -340,88 +386,99 @@ class Solarium_Query_Select extends Solarium_Query
}
/**
* Add a sort field
* Add a sort
*
* @param string $field
* @param string $sort
* @param string $order
* @return Solarium_Query Provides fluent interface
*/
public function addSortField($field, $order)
public function addSort($sort, $order)
{
$this->_sortFields[$field] = $order;
$this->_sorts[$sort] = $order;
return $this;
}
/**
* Add multiple sort fields
* Add multiple sorts
*
* The input array must contain fieldnames as keys and the order as values.
* The input array must contain sort items as keys and the order as values.
*
* @param array $sortFields
* @param array $sorts
* @return Solarium_Query Provides fluent interface
*/
public function addSortFields(array $sortFields)
public function addSorts(array $sorts)
{
foreach ($sortFields AS $sortField => $sortOrder) {
$this->addSortField($sortField, $sortOrder);
foreach ($sorts AS $sort => $order) {
$this->addSort($sort, $order);
}
return $this;
}
/**
* Remove a sortfield
* Remove a sort
*
* @param string $field
* @param string $sort
* @return Solarium_Query Provides fluent interface
*/
public function removeSortField($field)
public function removeSort($sort)
{
if (isset($this->_sortFields[$field])) {
unset($this->_sortFields[$field]);
if (isset($this->_sorts[$sort])) {
unset($this->_sorts[$sort]);
}
return $this;
}
/**
* Remove all sortfields
* Remove all sorts
*
* @return Solarium_Query Provides fluent interface
*/
public function clearSortFields()
public function clearSorts()
{
$this->_sortFields = array();
$this->_sorts = array();
return $this;
}
/**
* Get a list of the sortfields
* Get a list of the sorts
*
* @return array
*/
public function getSortFields()
public function getSorts()
{
return $this->_sortFields;
return $this->_sorts;
}
/**
* Set multiple sortfields
* Set multiple sorts
*
* This overwrites any existing sortfields
* This overwrites any existing sorts
*
* @param array $fields
* @param array $sorts
* @return Solarium_Query Provides fluent interface
*/
public function setSortFields($fields)
public function setSorts($sorts)
{
$this->clearSortFields();
$this->addSortFields($fields);
$this->clearSorts();
$this->addSorts($sorts);
return $this;
}
/**
* Create a filterquery instance
*
* @param mixed $options
* @return Solarium_Query_Select_FilterQuery
*/
public function createFilterQuery($options = null)
{
return new Solarium_Query_Select_FilterQuery($options);
}
/**
* Add a filter query
*
......@@ -537,6 +594,36 @@ class Solarium_Query_Select extends Solarium_Query
$this->addFilterQueries($filterQueries);
}
/**
* Get all registered component types
*
* @return array
*/
public function getComponentTypes()
{
return $this->_componentTypes;
}
/**
* Register a component type
*
* @param string $key
* @param string $component
* @param string $requestBuilder
* @param string $responseParser
* @return Solarium_Query Provides fluent interface
*/
public function registerComponentType($key, $component, $requestBuilder=null, $responseParser=null)
{
$this->_componentTypes[$key] = array(
'component' => $component,
'requestbuilder' => $requestBuilder,
'responseparser' => $responseParser,
);
return $this;
}
/**
* Get all registered components
*
......@@ -565,26 +652,14 @@ class Solarium_Query_Select extends Solarium_Query
} else {
if ($autoload == true) {
switch ($key) {
case Solarium_Query_Select_Component::MORELIKETHIS:
$className = 'Solarium_Query_Select_Component_MoreLikeThis';
break;
case Solarium_Query_Select_Component::FACETSET:
$className = 'Solarium_Query_Select_Component_FacetSet';
break;
case Solarium_Query_Select_Component::DISMAX:
$className = 'Solarium_Query_Select_Component_DisMax';
break;
case Solarium_Query_Select_Component::HIGHLIGHTING:
$className = 'Solarium_Query_Select_Component_Highlighting';
break;
default:
throw new Solarium_Exception('Cannot autoload unknown component: ' . $key);
if (!isset($this->_componentTypes[$key])) {
throw new Solarium_Exception('Cannot autoload unknown component: ' . $key);
}
$className = $this->_componentTypes[$key]['component'];
$component = new $className($config);
$this->setComponent($key, $component);
return $this->_components[$key];
return $component;
}
return null;
}
......@@ -642,7 +717,7 @@ class Solarium_Query_Select extends Solarium_Query
*/
public function getMoreLikeThis()
{
return $this->getComponent(Solarium_Query_Select_Component::MORELIKETHIS, true);
return $this->getComponent(Solarium_Query_Select::COMPONENT_MORELIKETHIS, true);
}
/**
......@@ -654,7 +729,7 @@ class Solarium_Query_Select extends Solarium_Query
*/
public function getFacetSet()
{
return $this->getComponent(Solarium_Query_Select_Component::FACETSET, true);
return $this->getComponent(Solarium_Query_Select::COMPONENT_FACETSET, true);
}
/**
......@@ -666,7 +741,7 @@ class Solarium_Query_Select extends Solarium_Query
*/
public function getDisMax()
{
return $this->getComponent(Solarium_Query_Select_Component::DISMAX, true);
return $this->getComponent(Solarium_Query_Select::COMPONENT_DISMAX, true);
}
/**
......@@ -678,7 +753,7 @@ class Solarium_Query_Select extends Solarium_Query
*/
public function getHighlighting()
{
return $this->getComponent(Solarium_Query_Select_Component::HIGHLIGHTING, true);
return $this->getComponent(Solarium_Query_Select::COMPONENT_HIGHLIGHTING, true);
}
/**
......
......@@ -44,14 +44,6 @@
class Solarium_Query_Select_Component extends Solarium_Configurable
{
/**
* Component types
*/
const MORELIKETHIS = 'morelikethis';
const FACETSET = 'facetset';
const DISMAX = 'dismax';
const HIGHLIGHTING = 'highlighting';
/**
* Component type
*
......
......@@ -51,7 +51,7 @@ class Solarium_Query_Select_Component_DisMax extends Solarium_Query_Select_Compo
*
* @var string
*/
protected $_type = self::DISMAX;
protected $_type = Solarium_Query_Select::COMPONENT_DISMAX;
/**
* Set QueryAlternative option
......
......@@ -46,14 +46,6 @@
abstract class Solarium_Query_Select_Component_Facet extends Solarium_Configurable
{
/**
* Constants for the facet types
*/
const QUERY = 'query';
const FIELD = 'field';
const MULTIQUERY = 'multiquery';
const RANGE = 'range';
/**
* Exclude tags for this facet
*
......
......@@ -74,7 +74,7 @@ class Solarium_Query_Select_Component_Facet_Field extends Solarium_Query_Select_
*/
public function getType()
{
return self::FIELD;
return Solarium_Query_Select_Component_FacetSet::FACET_FIELD;
}
/**
......
......@@ -64,6 +64,8 @@ class Solarium_Query_Select_Component_Facet_MultiQuery extends Solarium_Query_Se
*/
protected function _init()
{
parent::_init();
foreach ($this->_options AS $name => $value) {
switch ($name) {
case 'query':
......@@ -81,7 +83,7 @@ class Solarium_Query_Select_Component_Facet_MultiQuery extends Solarium_Query_Se
*/
public function getType()
{
return self::MULTIQUERY;
return Solarium_Query_Select_Component_FacetSet::FACET_MULTIQUERY;
}
/**
......
......@@ -62,7 +62,7 @@ class Solarium_Query_Select_Component_Facet_Query extends Solarium_Query_Select_
*/
public function getType()
{
return self::QUERY;
return Solarium_Query_Select_Component_FacetSet::FACET_QUERY;
}
/**
......
......@@ -74,6 +74,8 @@ class Solarium_Query_Select_Component_Facet_Range extends Solarium_Query_Select_
*/
protected function _init()
{
parent::_init();
foreach ($this->_options AS $name => $value) {
switch ($name) {
case 'include':
......@@ -93,7 +95,7 @@ class Solarium_Query_Select_Component_Facet_Range extends Solarium_Query_Select_
*/
public function getType()
{
return self::RANGE;
return Solarium_Query_Select_Component_FacetSet::FACET_RANGE;
}
/**
......
......@@ -46,12 +46,32 @@
class Solarium_Query_Select_Component_FacetSet extends Solarium_Query_Select_Component
{
/**
* Facet type keys
*/
const FACET_FIELD = 'field';
const FACET_QUERY = 'query';
const FACET_MULTIQUERY = 'multiquery';
const FACET_RANGE = 'range';
/**
* Facet type mapping
*
* @var array
*/
protected $_facetTypes = array(
self::FACET_FIELD => 'Solarium_Query_Select_Component_Facet_Field',
self::FACET_QUERY => 'Solarium_Query_Select_Component_Facet_Query',
self::FACET_MULTIQUERY => 'Solarium_Query_Select_Component_Facet_MultiQuery',
self::FACET_RANGE => 'Solarium_Query_Select_Component_Facet_Range',
);
/**
* Component type
*
* @var string
*/
protected $_type = self::FACETSET;
protected $_type = Solarium_Query_Select::COMPONENT_FACETSET;
/**
* Default options
......@@ -224,8 +244,7 @@ class Solarium_Query_Select_Component_FacetSet extends Solarium_Query_Select_Com
public function addFacet($facet)
{
if (is_array($facet)) {
$className = 'Solarium_Query_Select_Component_Facet_'.ucfirst($facet['type']);
$facet = new $className($facet);
$facet = $this->createFacet($facet['type'], $facet);
}
$key = $facet->getKey();
......@@ -328,4 +347,65 @@ class Solarium_Query_Select_Component_FacetSet extends Solarium_Query_Select_Com
$this->addFacets($facets);
}
/**
* @param string $type
* @param array|object|null $options
* @return Solarium_Query_Select_Component_Facet
*/
public function createFacet($type, $options = null)
{
$type = strtolower($type);
if (!isset($this->_facetTypes[$type])) {
throw new Solarium_Exception("Facettype unknown: " . $type);
}
$class = $this->_facetTypes[$type];
return new $class($options);
}
/**
* Get a facet field instance
*
* @param mixed $options
* @return Solarium_Query_Select_Component_Facet_Field
*/
public function createFacetField($options = null)
{
return $this->createFacet(self::FACET_FIELD, $options);
}
/**
* Get a facet query instance
*
* @param mixed $options
* @return Solarium_Query_Select_Component_Facet_Query
*/
public function createFacetQuery($options = null)
{
return $this->createFacet(self::FACET_QUERY, $options);
}
/**
* Get a facet multiquery instance
*
* @param mixed $options
* @return Solarium_Query_Select_Component_Facet_MultiQuery
*/
public function createFacetMultiQuery($options = null)
{
return $this->createFacet(self::FACET_MULTIQUERY, $options);
}
/**
* Get a facet range instance
*
* @param mixed $options
* @return Solarium_Query_Select_Component_Facet_Range
*/
public function createFacetRange($options = null)
{
return $this->createFacet(self::FACET_RANGE, $options);
}
}
\ No newline at end of file
......@@ -56,7 +56,7 @@ class Solarium_Query_Select_Component_Highlighting extends Solarium_Query_Select
*
* @var string
*/
protected $_type = self::HIGHLIGHTING;
protected $_type = Solarium_Query_Select::COMPONENT_HIGHLIGHTING;
/**
* Set fields option
......
......@@ -51,7 +51,7 @@ class Solarium_Query_Select_Component_MoreLikeThis extends Solarium_Query_Select
*
* @var string
*/
protected $_type = self::MORELIKETHIS;
protected $_type = Solarium_Query_Select::COMPONENT_MORELIKETHIS;
/**
* Set fields option
......
......@@ -173,7 +173,7 @@ class Solarium_Query_Select_Helper
*/
public function functionCall($name, $params = array())
{
return $name . '(' . implode($params,',') . ')';
return $name . '(' . implode($params, ',') . ')';
}
}
\ No newline at end of file
......@@ -48,6 +48,28 @@
class Solarium_Query_Update extends Solarium_Query
{
/**
* Update command type names
*/
const COMMAND_ADD = 'add';
const COMMAND_DELETE = 'delete';
const COMMAND_COMMIT = 'commit';
const COMMAND_ROLLBACK = 'rollback';
const COMMAND_OPTIMIZE = 'optimize';
/**
* Update command types
*
* @var array
*/
protected $_commandTypes = array(
self::COMMAND_ADD => 'Solarium_Query_Update_Command_Add',
self::COMMAND_DELETE => 'Solarium_Query_Update_Command_Delete',
self::COMMAND_COMMIT => 'Solarium_Query_Update_Command_Commit',
self::COMMAND_OPTIMIZE => 'Solarium_Query_Update_Command_Optimize',
self::COMMAND_ROLLBACK => 'Solarium_Query_Update_Command_Rollback',
);
/**
* Default options
*
......@@ -68,6 +90,16 @@ class Solarium_Query_Update extends Solarium_Query
*/
protected $_commands = array();
/**
* Get type for this query
*
* @return string
*/
public function getType()
{
return Solarium_Client::QUERYTYPE_UPDATE;
}
/**
* Initialize options
*
......@@ -80,31 +112,41 @@ class Solarium_Query_Update extends Solarium_Query
{
if (isset($this->_options['command'])) {
foreach ($this->_options['command'] as $key => $value) {
switch ($value['type']) {
case 'delete':
$command = new Solarium_Query_Update_Command_Delete($value);
break;
case 'commit':
$command = new Solarium_Query_Update_Command_Commit($value);
break;
case 'optimize':
$command = new Solarium_Query_Update_Command_Optimize($value);
break;
case 'rollback':
$command = new Solarium_Query_Update_Command_Rollback($value);
break;
case 'add':
throw new Solarium_Exception(
"Adding documents is not supported in configuration, use the API for this"
);
$type = $value['type'];
if ($type == self::COMMAND_ADD) {
throw new Solarium_Exception(
"Adding documents is not supported in configuration, use the API for this"
);
}
$this->add($key, $command);
$this->add($key, $this->createCommand($type, $value));
}
}
}
/**
* Create a command instance
*
* @throws Solarium_Exception
* @param string $type
* @param mixed $options
* @return Solarium_Query_Update_Command
*/
public function createCommand($type, $options = null)
{
$type = strtolower($type);
if (!isset($this->_commandTypes[$type])) {
throw new Solarium_Exception("Update commandtype unknown: " . $type);
}
$class = $this->_commandTypes[$type];
return new $class($options);
}
/**
* Get all commands for this update query
*
......
......@@ -44,15 +44,6 @@
abstract class Solarium_Query_Update_Command extends Solarium_Configurable
{
/**
* Command types
*/
const ADD = 'add';
const DELETE = 'delete';
const COMMIT = 'commit';
const OPTIMIZE = 'optimize';
const ROLLBACK = 'rollback';
/**
* Returns command type, for use in adapters
*
......
......@@ -61,14 +61,14 @@ class Solarium_Query_Update_Command_Add extends Solarium_Query_Update_Command
*/
public function getType()
{
return Solarium_Query_Update_Command::ADD;
return Solarium_Query_Update::COMMAND_ADD;
}
/**
* Add a single document
*
* @param object $document
* @return Solarium_Query_Update_Add Provides fluent interface
* @return Solarium_Query_Update_Command_Add Provides fluent interface
*/
public function addDocument($document)
{
......@@ -80,12 +80,26 @@ class Solarium_Query_Update_Command_Add extends Solarium_Query_Update_Command
/**
* Add multiple documents
*
* @param array $documents
* @return Solarium_Query_Update_Add Provides fluent interface
* @param array|Traversable $documents
* @return Solarium_Query_Update_Command_Add Provides fluent interface
*/
public function addDocuments($documents)
{
$this->_documents = array_merge($this->_documents, $documents);
//if we don't have documents so far, accept arrays or Traversable objects as-is
if (empty($this->_documents)) {
$this->_documents = $documents;
return $this;
}
//if something Traversable is passed in, and there are existing documents, convert all to arrays before merging
if ($documents instanceof Traversable) {
$documents = iterator_to_array($documents);
}
if ($this->_documents instanceof Traversable) {
$this->_documents = array_merge(iterator_to_array($this->_documents), $documents);
} else {
$this->_documents = array_merge($this->_documents, $documents);
}
return $this;
}
......@@ -104,7 +118,7 @@ class Solarium_Query_Update_Command_Add extends Solarium_Query_Update_Command
* Set overwrite option
*
* @param boolean $overwrite
* @return Solarium_Query_Update_Add Provides fluent interface
* @return Solarium_Query_Update_Command_Add Provides fluent interface
*/
public function setOverwrite($overwrite)
{
......@@ -125,7 +139,7 @@ class Solarium_Query_Update_Command_Add extends Solarium_Query_Update_Command
* Get commitWithin option
*
* @param boolean $commitWithin
* @return Solarium_Query_Update_Add Provides fluent interface
* @return Solarium_Query_Update_Command_Add Provides fluent interface
*/
public function setCommitWithin($commitWithin)
{
......
......@@ -54,7 +54,7 @@ class Solarium_Query_Update_Command_Commit extends Solarium_Query_Update_Command
*/
public function getType()
{
return Solarium_Query_Update_Command::COMMIT;
return Solarium_Query_Update::COMMAND_COMMIT;
}
/**
......@@ -71,7 +71,7 @@ class Solarium_Query_Update_Command_Commit extends Solarium_Query_Update_Command
* Set waitFlush option
*
* @param boolean $waitFlush
* @return Solarium_Query_Update_Commit Provides fluent interface
* @return Solarium_Query_Update_Command_Commit Provides fluent interface
*/
public function setWaitFlush($waitFlush)
{
......@@ -92,7 +92,7 @@ class Solarium_Query_Update_Command_Commit extends Solarium_Query_Update_Command
* Set waitSearcher option
*
* @param boolean $waitSearcher
* @return Solarium_Query_Update_Commit Provides fluent interface
* @return Solarium_Query_Update_Command_Commit Provides fluent interface
*/
public function setWaitSearcher($waitSearcher)
{
......@@ -113,7 +113,7 @@ class Solarium_Query_Update_Command_Commit extends Solarium_Query_Update_Command
* Set expungeDeletes option
*
* @param boolean $expungeDeletes
* @return Solarium_Query_Update_Commit Provides fluent interface
* @return Solarium_Query_Update_Command_Commit Provides fluent interface
*/
public function setExpungeDeletes($expungeDeletes)
{
......
......@@ -68,7 +68,7 @@ class Solarium_Query_Update_Command_Delete extends Solarium_Query_Update_Command
*/
public function getType()
{
return Solarium_Query_Update_Command::DELETE;
return Solarium_Query_Update::COMMAND_DELETE;
}
/**
......@@ -101,7 +101,7 @@ class Solarium_Query_Update_Command_Delete extends Solarium_Query_Update_Command
* Add a single ID to the delete command
*
* @param int|string $id
* @return Solarium_Query_Update_Delete Provides fluent interface
* @return Solarium_Query_Update_Command_Delete Provides fluent interface
*/
public function addId($id)
{
......@@ -114,7 +114,7 @@ class Solarium_Query_Update_Command_Delete extends Solarium_Query_Update_Command
* Add multiple IDs to the delete command
*
* @param array $id
* @return Solarium_Query_Update_Delete Provides fluent interface
* @return Solarium_Query_Update_Command_Delete Provides fluent interface
*/
public function addIds($ids)
{
......@@ -127,7 +127,7 @@ class Solarium_Query_Update_Command_Delete extends Solarium_Query_Update_Command
* Add a single query to the delete command
*
* @param string $query
* @return Solarium_Query_Update_Delete Provides fluent interface
* @return Solarium_Query_Update_Command_Delete Provides fluent interface
*/
public function addQuery($query)
{
......@@ -140,7 +140,7 @@ class Solarium_Query_Update_Command_Delete extends Solarium_Query_Update_Command
* Add multiple queries to the delete command
*
* @param array $queries
* @return Solarium_Query_Update_Delete Provides fluent interface
* @return Solarium_Query_Update_Command_Delete Provides fluent interface
*/
public function addQueries($queries)
{
......
......@@ -54,7 +54,7 @@ class Solarium_Query_Update_Command_Optimize
*/
public function getType()
{
return Solarium_Query_Update_Command::OPTIMIZE;
return Solarium_Query_Update::COMMAND_OPTIMIZE;
}
/**
......@@ -71,7 +71,7 @@ class Solarium_Query_Update_Command_Optimize
* Set waitFlush option
*
* @param boolean $waitFlush
* @return Solarium_Query_Update_Optimize Provides fluent interface
* @return Solarium_Query_Update_Command_Optimize Provides fluent interface
*/
public function setWaitFlush($waitFlush)
{
......@@ -92,7 +92,7 @@ class Solarium_Query_Update_Command_Optimize
* Set waitSearcher option
*
* @param boolean $waitSearcher
* @return Solarium_Query_Update_Optimize Provides fluent interface
* @return Solarium_Query_Update_Command_Optimize Provides fluent interface
*/
public function setWaitSearcher($waitSearcher)
{
......@@ -113,7 +113,7 @@ class Solarium_Query_Update_Command_Optimize
* Set maxSegments option
*
* @param boolean $maxSegments
* @return Solarium_Query_Update_Optimize Provides fluent interface
* @return Solarium_Query_Update_Command_Optimize Provides fluent interface
*/
public function setMaxSegments($maxSegments)
{
......
......@@ -54,7 +54,7 @@ class Solarium_Query_Update_Command_Rollback
*/
public function getType()
{
return Solarium_Query_Update_Command::ROLLBACK;
return Solarium_Query_Update::COMMAND_ROLLBACK;
}
}
\ No newline at end of file
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
*
* @package Solarium
* @subpackage Result
*/
/**
* Query result
*
* This base class provides access to the response and decoded data. If you need more functionality
* like resultset parsing use one of the subclasses
*
* @package Solarium
* @subpackage Result
*/
class Solarium_Result
{
/**
* Response object
*
* @var Solarium_Client_Response
*/
protected $_response;
/**
* Decode response data
*
* This is lazy loaded, see getData()
*
* @var array
*/
protected $_data;
/**
* Query used for this request
*
* @var Solarium_Query
*/
protected $_query;
/**
* @var Solarium_Client
*/
protected $_client;
/**
* Constructor
*
* @param Solarium_Client $client
* @param Solarium_Query $query
* @param Solarium_Client_Response $response
* @return void
*/
public function __construct($client, $query, $response)
{
$this->_client = $client;
$this->_query = $query;
$this->_response = $response;
// check status for error (range of 400 and 500)
$statusNum = floor($response->getStatusCode() / 100);
if ($statusNum == 4 || $statusNum == 5) {
throw new Solarium_Client_HttpException(
$response->getStatusMessage(),
$response->getStatusCode()
);
}
}
/**
* Get response object
*
* This is the raw HTTP response object, not the parsed data!
*
* @return Solarium_Client_Response
*/
public function getResponse()
{
return $this->_response;
}
/**
* Get query instance
*
* @return Solarium_Query
*/
public function getQuery()
{
return $this->_query;
}
/**
* Get Solr response data
*
* Included a lazy loading mechanism: JSON body data is decoded on first use and then saved for reuse.
*
* @return array
*/
public function getData()
{
if (null == $this->_data) {
$this->_data = json_decode($this->_response->getBody(), true);
if (null === $this->_data) {
throw new Solarium_Exception(
'Solr JSON response could not be decoded'
);
}
}
return $this->_data;
}
}
\ No newline at end of file
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
*
* @package Solarium
* @subpackage Result
*/
/**
* QueryType result
*
* @package Solarium
* @subpackage Result
*/
class Solarium_Result_QueryType extends Solarium_Result
{
/**
* Lazy load parsing indicator
*
* @var bool
*/
protected $_parsed = false;
/**
* Parse response into result objects
*
* Only runs once
*
* @return void
*/
protected function _parseResponse()
{
if (!$this->_parsed) {
$queryType = $this->_query->getType();
$queryTypes = $this->_client->getQueryTypes();
if (!isset($queryTypes[$queryType])) {
throw new Solarium_Exception('No responseparser registered for querytype: '. $queryType);
}
$responseParserClass = $queryTypes[$queryType]['responseparser'];
$responseParser = new $responseParserClass;
$this->_mapData($responseParser->parse($this));
$this->_parsed = true;
}
}
/**
* Map parser data into properties
*
* @param array $mapData
* @return void
*/
protected function _mapData($mapData)
{
foreach ($mapData AS $key => $data) {
$this->{'_'.$key} = $data;
}
}
}
\ No newline at end of file
......@@ -58,7 +58,7 @@
* @package Solarium
* @subpackage Result
*/
class Solarium_Result_Select extends Solarium_Result_Query
class Solarium_Result_Select extends Solarium_Result_QueryType
implements IteratorAggregate, Countable
{
......@@ -69,7 +69,7 @@ class Solarium_Result_Select extends Solarium_Result_Query
*
* @var int
*/
protected $_numFound;
protected $_numfound;
/**
* Document instances array
......@@ -79,88 +79,81 @@ class Solarium_Result_Select extends Solarium_Result_Query
protected $_documents;
/**
* Facet result instances
*
* @var array
* Component results
*/
protected $_facets;
protected $_components;
/**
* Component results
* Status code returned by Solr
*
* @var int
*/
protected $_components;
protected $_status;
/**
* Constructor
* Solr index queryTime
*
* This is the only point where data can be set in this immutable value
* object.
* This doesn't include things like the HTTP responsetime. Purely the Solr
* query execution time.
*
* @param int $status
* @param int $queryTime
* @param int $numFound
* @param array $documents
* @param array $facets
* @param array $components
* @return void
* @var int
*/
public function __construct($status, $queryTime, $numFound, $documents,
$facets, $components)
{
$this->_status = $status;
$this->_queryTime = $queryTime;
$this->_numFound = $numFound;
$this->_documents = $documents;
$this->_facets = $facets;
$this->_components = $components;
}
protected $_queryTime;
/**
* get Solr numFound
* Get Solr status code
*
* Returns the total number of documents found by Solr (this is NOT the
* number of document fetched from Solr!)
* This is not the HTTP status code! The normal value for success is 0.
*
* @return int
*/
public function getNumFound()
public function getStatus()
{
return $this->_numFound;
$this->_parseResponse();
return $this->_status;
}
/**
* Get all documents
* Get Solr query time
*
* @return array
* This doesn't include things like the HTTP responsetime. Purely the Solr
* query execution time.
*
* @return int
*/
public function getDocuments()
public function getQueryTime()
{
return $this->_documents;
$this->_parseResponse();
return $this->_queryTime;
}
/**
* Get all facet results
* get Solr numFound
*
* @return array
* Returns the total number of documents found by Solr (this is NOT the
* number of document fetched from Solr!)
*
* @return int
*/
public function getFacets()
public function getNumFound()
{
return $this->_facets;
$this->_parseResponse();
return $this->_numfound;
}
/**
* Get a facet result by key
* Get all documents
*
* @param string $key
* @return Solarium_Result_Select_Facet
* @return array
*/
public function getFacet($key)
public function getDocuments()
{
if (isset($this->_facets[$key])) {
return $this->_facets[$key];
} else {
return null;
}
$this->_parseResponse();
return $this->_documents;
}
/**
......@@ -170,6 +163,8 @@ class Solarium_Result_Select extends Solarium_Result_Query
*/
public function getIterator()
{
$this->_parseResponse();
return new ArrayIterator($this->_documents);
}
......@@ -180,6 +175,8 @@ class Solarium_Result_Select extends Solarium_Result_Query
*/
public function count()
{
$this->_parseResponse();
return count($this->_documents);
}
......@@ -190,6 +187,8 @@ class Solarium_Result_Select extends Solarium_Result_Query
*/
public function getComponents()
{
$this->_parseResponse();
return $this->_components;
}
......@@ -201,6 +200,8 @@ class Solarium_Result_Select extends Solarium_Result_Query
*/
public function getComponent($key)
{
$this->_parseResponse();
if (isset($this->_components[$key])) {
return $this->_components[$key];
} else {
......@@ -217,7 +218,7 @@ class Solarium_Result_Select extends Solarium_Result_Query
*/
public function getMoreLikeThis()
{
return $this->getComponent(Solarium_Query_Select_Component::MORELIKETHIS);
return $this->getComponent(Solarium_Query_Select::COMPONENT_MORELIKETHIS);
}
/**
......@@ -229,6 +230,18 @@ class Solarium_Result_Select extends Solarium_Result_Query
*/
public function getHighlighting()
{
return $this->getComponent(Solarium_Query_Select_Component::HIGHLIGHTING);
return $this->getComponent(Solarium_Query_Select::COMPONENT_HIGHLIGHTING);
}
/**
* Get facetset component result
*
* This is a convenience method that maps presets to getComponent
*
* @return Solarium_Result_Select_Component_FacetSet
*/
public function getFacetSet()
{
return $this->getComponent(Solarium_Query_Select::COMPONENT_FACETSET);
}
}
\ No newline at end of file
......@@ -36,69 +36,74 @@
*/
/**
* Query result
*
* This base class provides methods for two common result values: status and
* querytime.
* Select component facetset result
*
* @package Solarium
* @subpackage Result
*/
class Solarium_Result_Query
class Solarium_Result_Select_FacetSet implements IteratorAggregate, Countable
{
/**
* Status code returned by Solr
* Facet array
*
* @var int
* @var array
*/
protected $_status;
protected $_facets;
/**
* Solr index queryTime
*
* This doesn't include things like the HTTP responsetime. Purely the Solr
* query execution time.
* Constructor
*
* @var int
* @param array $facets
* @return void
*/
protected $_queryTime;
public function __construct($facets)
{
$this->_facets = $facets;
}
/**
* Constructor
* Get a facet by key
*
* @param int $status
* @param int $queryTime
* @return void
* @param mixed $key
* @return mixed
*/
public function __construct($status, $queryTime)
public function getFacet($key)
{
$this->_status = $status;
$this->_queryTime = $queryTime;
if (isset($this->_facets[$key])) {
return $this->_facets[$key];
} else {
return null;
}
}
/**
* Get Solr status code
* Get all results
*
* This is not the HTTP status code! The normal value for success is 0.
* @return array
*/
public function getFacets()
{
return $this->_facets;
}
/**
* IteratorAggregate implementation
*
* @return int
* @return ArrayIterator
*/
public function getStatus()
public function getIterator()
{
return $this->_status;
return new ArrayIterator($this->_facets);
}
/**
* Get Solr query time
* Countable implementation
*
* This doesn't include things like the HTTP responsetime. Purely the Solr
* query execution time.
*
* @return int
*/
public function getQueryTime()
public function count()
{
return $this->_queryTime;
return count($this->_facets);
}
}
\ No newline at end of file
This diff is collapsed.
......@@ -69,7 +69,7 @@ class Solarium_Version
*
* @var string
*/
const VERSION = '1.0.0';
const VERSION = '2.0.0';
/**
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -29,24 +29,20 @@
* policies, either expressed or implied, of the copyright holder.
*/
class Solarium_Result_QueryTest extends PHPUnit_Framework_TestCase
class Solarium_Client_ResponseParser_UpdateTest extends PHPUnit_Framework_TestCase
{
protected $_result;
public function setUp()
public function testParse()
{
$this->_result = new Solarium_Result_Query(0,45);
}
$data = '{"responseHeader" : {"status":1,"QTime":15}}';
public function testGetStatus()
{
$this->assertEquals(0, $this->_result->getStatus());
}
$response = new Solarium_Client_Response($data, array('HTTP 1.1 200 OK'));
$result = new Solarium_Result_Update(null,null,$response);
$parser = new Solarium_Client_ResponseParser_Update;
$parsed = $parser->parse($result);
public function testGetQueryTime()
{
$this->assertEquals(45, $this->_result->getQueryTime());
}
$this->assertEquals(1, $parsed['status']);
$this->assertEquals(15, $parsed['queryTime']);
}
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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