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

Various fixes for porting the extract querytype to Solarium 3 (work in progress...)

parent a7335438
...@@ -174,7 +174,7 @@ class ZendHttp extends Configurable implements AdapterInterface ...@@ -174,7 +174,7 @@ class ZendHttp extends Configurable implements AdapterInterface
switch ($request->getMethod()) { switch ($request->getMethod()) {
case Request::METHOD_GET: case Request::METHOD_GET:
$client->setMethod(\Zend_Http_Client::GET); $client->setMethod(\Zend_Http_Client::GET);
$client->setParameterGet($request->getQueryAsArray()); $client->setParameterGet($request->getParams());
break; break;
case Request::METHOD_POST: case Request::METHOD_POST:
$client->setMethod(\Zend_Http_Client::POST); $client->setMethod(\Zend_Http_Client::POST);
...@@ -182,24 +182,22 @@ class ZendHttp extends Configurable implements AdapterInterface ...@@ -182,24 +182,22 @@ class ZendHttp extends Configurable implements AdapterInterface
if ($request->getFileUpload()) { if ($request->getFileUpload()) {
$this->prepareFileUpload($client, $request); $this->prepareFileUpload($client, $request);
} else { } else {
$client->setParameterGet($request->getQueryAsArray()); $client->setParameterGet($request->getParams());
$client->setRawData($request->getRawData()); $client->setRawData($request->getRawData());
$request->addHeader('Content-Type: text/xml; charset=UTF-8'); $request->addHeader('Content-Type: text/xml; charset=UTF-8');
} }
break; break;
case Request::METHOD_HEAD: case Request::METHOD_HEAD:
$client->setMethod(\Zend_Http_Client::HEAD); $client->setMethod(\Zend_Http_Client::HEAD);
$client->setParameterGet($request->getQueryAsArray()); $client->setParameterGet($request->getParams());
break; break;
default: default:
throw new OutOfBoundsException('Unsupported method: ' . $request->getMethod()); throw new OutOfBoundsException('Unsupported method: ' . $request->getMethod());
break; break;
} }
$client->setMethod($request->getMethod());
$client->setUri($endpoint->getBaseUri() . $request->getUri()); $client->setUri($endpoint->getBaseUri() . $request->getUri());
$client->setHeaders($request->getHeaders()); $client->setHeaders($request->getHeaders());
$client->setRawData($request->getRawData());
$this->timeout = $endpoint->getTimeout(); $this->timeout = $endpoint->getTimeout();
$response = $client->request(); $response = $client->request();
...@@ -271,7 +269,7 @@ class ZendHttp extends Configurable implements AdapterInterface ...@@ -271,7 +269,7 @@ class ZendHttp extends Configurable implements AdapterInterface
$client->setFileUpload('content', 'content', $content, 'application/octet-stream; charset=binary'); $client->setFileUpload('content', 'content', $content, 'application/octet-stream; charset=binary');
// set query params as "multipart/form-data" fields // set query params as "multipart/form-data" fields
foreach ($request->getQueryAsArray() as $name => $value) { foreach ($request->getParams() as $name => $value) {
$client->setFileUpload(null, $name, $value, 'text/plain; charset=utf-8'); $client->setFileUpload(null, $name, $value, 'text/plain; charset=utf-8');
} }
} }
......
...@@ -38,19 +38,28 @@ ...@@ -38,19 +38,28 @@
* @subpackage Query * @subpackage Query
*/ */
/**
* @namespace
*/
namespace Solarium\QueryType\Extract;
use Solarium\Core\Query\Query as BaseQuery;
use Solarium\Core\Client\Client;
use Solarium\QueryType\Update\ResponseParser as UpdateResponseParser;
use Solarium\QueryType\Update\Query\Document;
/** /**
* Extract query * Extract query
* *
* Sends a document extract request to Solr, i.e. upload rich document content * Sends a document extract request to Solr, i.e. upload rich document content
* such as PDF, Word or HTML, parse the file contents and add it to the index. * such as PDF, Word or HTML, parse the file contents and add it to the index.
* *
* The Solr server must have the {@link http://wiki.apache.org/solr/ExtractingRequestHandler * The Solr server must have the {@link http://wiki.apache.org/solr/ExtractingRequestHandler
* ExtractingRequestHandler} enabled. * ExtractingRequestHandler} enabled.
* *
* @package Solarium * @package Solarium
* @subpackage Query * @subpackage Query
*/ */
class Solarium_Query_Extract extends Solarium_Query class Query extends BaseQuery
{ {
/** /**
* Default options * Default options
...@@ -59,7 +68,8 @@ class Solarium_Query_Extract extends Solarium_Query ...@@ -59,7 +68,8 @@ class Solarium_Query_Extract extends Solarium_Query
*/ */
protected $_options = array( protected $_options = array(
'handler' => 'update/extract', 'handler' => 'update/extract',
'resultclass' => 'Solarium_Result_Update', 'resultclass' => 'Solarium\QueryType\Extract\Result',
'omitheader' => true,
); );
/** /**
...@@ -67,7 +77,7 @@ class Solarium_Query_Extract extends Solarium_Query ...@@ -67,7 +77,7 @@ class Solarium_Query_Extract extends Solarium_Query
* *
* @var array * @var array
*/ */
protected $_fieldMappings = array(); protected $fieldMappings = array();
/** /**
* Get type for this query * Get type for this query
...@@ -76,9 +86,29 @@ class Solarium_Query_Extract extends Solarium_Query ...@@ -76,9 +86,29 @@ class Solarium_Query_Extract extends Solarium_Query
*/ */
public function getType() public function getType()
{ {
return Solarium_Client::QUERYTYPE_EXTRACT; return Client::QUERY_EXTRACT;
} }
/**
* Get a requestbuilder for this query
*
* @return RequestBuilder
*/
public function getRequestBuilder()
{
return new RequestBuilder;
}
/**
* Get a response parser for this query
*
* @return UpdateResponseParser
*/
public function getResponseParser()
{
return new UpdateResponseParser;
}
/** /**
* Initialize options * Initialize options
* *
...@@ -94,26 +124,24 @@ class Solarium_Query_Extract extends Solarium_Query ...@@ -94,26 +124,24 @@ class Solarium_Query_Extract extends Solarium_Query
} }
} }
// {{{ Options
/** /**
* Set the document with literal fields and boost settings * Set the document with literal fields and boost settings
* *
* The fields in the document are indexed together with the generated * The fields in the document are indexed together with the generated
* fields that Solr extracts from the file. * fields that Solr extracts from the file.
* *
* @param Solarium_Document_ReadWrite $document * @param Document $document
* @return Solarium_Query_Extract * @return self
*/ */
public function setDocument($document) public function setDocument($document)
{ {
return $this->_setOption('document', $document); return $this->setOption('document', $document);
} }
/** /**
* Get the document with literal fields and boost settings * Get the document with literal fields and boost settings
* *
* @return Solarium_Document_ReadWrite|null * @return Document|null
*/ */
public function getDocument() public function getDocument()
{ {
...@@ -122,18 +150,18 @@ class Solarium_Query_Extract extends Solarium_Query ...@@ -122,18 +150,18 @@ class Solarium_Query_Extract extends Solarium_Query
/** /**
* Set the file to upload and index * Set the file to upload and index
* *
* @param string $filename * @param string $filename
* @return Solarium_Query_Extract * @return self
*/ */
public function setFile($filename) public function setFile($filename)
{ {
return $this->_setOption('file', $filename); return $this->setOption('file', $filename);
} }
/** /**
* Get the file to upload and index * Get the file to upload and index
* *
* @return string|null * @return string|null
*/ */
public function getFile() public function getFile()
...@@ -145,16 +173,16 @@ class Solarium_Query_Extract extends Solarium_Query ...@@ -145,16 +173,16 @@ class Solarium_Query_Extract extends Solarium_Query
* Set the prefix for fields that are not defined in the schema * Set the prefix for fields that are not defined in the schema
* *
* @param string $uprefix * @param string $uprefix
* @return Solarium_Query_Extract * @return self
*/ */
public function setUprefix($uprefix) public function setUprefix($uprefix)
{ {
return $this->_setOption('uprefix', $uprefix); return $this->setOption('uprefix', $uprefix);
} }
/** /**
* Get the prefix for fields that are not defined in the schema * Get the prefix for fields that are not defined in the schema
* *
* @return string|null * @return string|null
*/ */
public function getUprefix() public function getUprefix()
...@@ -163,21 +191,21 @@ class Solarium_Query_Extract extends Solarium_Query ...@@ -163,21 +191,21 @@ class Solarium_Query_Extract extends Solarium_Query
} }
/** /**
* Set the field to use if uprefix is not specified and a field cannot be * Set the field to use if uprefix is not specified and a field cannot be
* determined * determined
* *
* @param string $defaultField * @param string $defaultField
* @return Solarium_Query_Extract * @return self
*/ */
public function setDefaultField($defaultField) public function setDefaultField($defaultField)
{ {
return $this->_setOption('defaultField', $defaultField); return $this->setOption('defaultField', $defaultField);
} }
/** /**
* Get the field to use if uprefix is not specified and a field cannot be * Get the field to use if uprefix is not specified and a field cannot be
* determined * determined
* *
* @return string|null * @return string|null
*/ */
public function getDefaultField() public function getDefaultField()
...@@ -186,20 +214,20 @@ class Solarium_Query_Extract extends Solarium_Query ...@@ -186,20 +214,20 @@ class Solarium_Query_Extract extends Solarium_Query
} }
/** /**
* Set if all field names should be mapped to lowercase with underscores. * Set if all field names should be mapped to lowercase with underscores.
* For example, Content-Type would be mapped to content_type. * For example, Content-Type would be mapped to content_type.
* *
* @param bool $lowerNames * @param bool $lowerNames
* @return Solarium_Query_Extract * @return self
*/ */
public function setLowernames($lowerNames) public function setLowernames($lowerNames)
{ {
return $this->_setOption('lowernames', (bool) $lowerNames); return $this->setOption('lowernames', (bool) $lowerNames);
} }
/** /**
* Get if all field names should be mapped to lowercase with underscores * Get if all field names should be mapped to lowercase with underscores
* *
* @return bool * @return bool
*/ */
public function getLowernames() public function getLowernames()
...@@ -211,16 +239,16 @@ class Solarium_Query_Extract extends Solarium_Query ...@@ -211,16 +239,16 @@ class Solarium_Query_Extract extends Solarium_Query
* Set if the extract should be committed immediately * Set if the extract should be committed immediately
* *
* @param bool $commit * @param bool $commit
* @return Solarium_Query_Extract Provides fluent interface * @return self Provides fluent interface
*/ */
public function setCommit($commit) public function setCommit($commit)
{ {
return $this->_setOption('commit', (bool) $commit); return $this->setOption('commit', (bool) $commit);
} }
/** /**
* Get if the extract should be committed immediately * Get if the extract should be committed immediately
* *
* @return bool * @return bool
*/ */
public function getCommit() public function getCommit()
...@@ -232,16 +260,16 @@ class Solarium_Query_Extract extends Solarium_Query ...@@ -232,16 +260,16 @@ class Solarium_Query_Extract extends Solarium_Query
* Set milliseconds until extract update is committed. Since Solr 3.4 * Set milliseconds until extract update is committed. Since Solr 3.4
* *
* @param int $commitWithin * @param int $commitWithin
* @return Solarium_Query_Extract Provides fluent interface * @return self Provides fluent interface
*/ */
public function setCommitWithin($commitWithin) public function setCommitWithin($commitWithin)
{ {
return $this->_setOption('commitWithin', $commitWithin); return $this->setOption('commitWithin', $commitWithin);
} }
/** /**
* Get milliseconds until extract update is committed. Since Solr 3.4 * Get milliseconds until extract update is committed. Since Solr 3.4
* *
* @return int * @return int
*/ */
public function getCommitWithin() public function getCommitWithin()
...@@ -249,23 +277,19 @@ class Solarium_Query_Extract extends Solarium_Query ...@@ -249,23 +277,19 @@ class Solarium_Query_Extract extends Solarium_Query
return $this->getOption('commitWithin'); return $this->getOption('commitWithin');
} }
// }}}
// {{{ Field Mappings
/** /**
* Add a name mapping from one field to another * Add a name mapping from one field to another
* *
* Example: fmap.content=text will cause the content field normally * Example: fmap.content=text will cause the content field normally
* generated by Tika to be moved to the "text" field. * generated by Tika to be moved to the "text" field.
* *
* @param string $fromField Original field name * @param string $fromField Original field name
* @param mixed|array $toField New field name * @param mixed|array $toField New field name
* @return Solarium_Query_Extract Provides fluent interface * @return self Provides fluent interface
*/ */
public function addFieldMapping($fromField, $toField) public function addFieldMapping($fromField, $toField)
{ {
$this->_fieldMappings[$fromField] = $toField; $this->fieldMappings[$fromField] = $toField;
return $this; return $this;
} }
...@@ -274,7 +298,7 @@ class Solarium_Query_Extract extends Solarium_Query ...@@ -274,7 +298,7 @@ class Solarium_Query_Extract extends Solarium_Query
* Add multiple field name mappings * Add multiple field name mappings
* *
* @param array $mappings Name mapping in the form [$fromField => $toField, ...] * @param array $mappings Name mapping in the form [$fromField => $toField, ...]
* @return Solarium_Query_Extract Provides fluent interface * @return self Provides fluent interface
*/ */
public function addFieldMappings($mappings) public function addFieldMappings($mappings)
{ {
...@@ -289,12 +313,12 @@ class Solarium_Query_Extract extends Solarium_Query ...@@ -289,12 +313,12 @@ class Solarium_Query_Extract extends Solarium_Query
* Remove a field name mapping * Remove a field name mapping
* *
* @param string $fromField * @param string $fromField
* @return Solarium_Query_Extract Provides fluent interface * @return self Provides fluent interface
*/ */
public function removeFieldMapping($fromField) public function removeFieldMapping($fromField)
{ {
if (isset($this->_fieldMappings[$fromField])) { if (isset($this->fieldMappings[$fromField])) {
unset($this->_fieldMappings[$fromField]); unset($this->fieldMappings[$fromField]);
} }
return $this; return $this;
...@@ -303,11 +327,11 @@ class Solarium_Query_Extract extends Solarium_Query ...@@ -303,11 +327,11 @@ class Solarium_Query_Extract extends Solarium_Query
/** /**
* Remove all field name mappings * Remove all field name mappings
* *
* @return Solarium_Query_Extract Provides fluent interface * @return self Provides fluent interface
*/ */
public function clearFieldMappings() public function clearFieldMappings()
{ {
$this->_fieldMappings = array(); $this->fieldMappings = array();
return $this; return $this;
} }
...@@ -318,14 +342,14 @@ class Solarium_Query_Extract extends Solarium_Query ...@@ -318,14 +342,14 @@ class Solarium_Query_Extract extends Solarium_Query
*/ */
public function getFieldMappings() public function getFieldMappings()
{ {
return $this->_fieldMappings; return $this->fieldMappings;
} }
/** /**
* Set many field name mappings. This overwrites any existing fields. * Set many field name mappings. This overwrites any existing fields.
* *
* @param array $mappings Name mapping in the form [$fromField => $toField, ...] * @param array $mappings Name mapping in the form [$fromField => $toField, ...]
* @return Solarium_Query_Extract Provides fluent interface * @return self Provides fluent interface
*/ */
public function setFieldMappings($mappings) public function setFieldMappings($mappings)
{ {
...@@ -334,6 +358,4 @@ class Solarium_Query_Extract extends Solarium_Query ...@@ -334,6 +358,4 @@ class Solarium_Query_Extract extends Solarium_Query
return $this; return $this;
} }
// }}}
} }
...@@ -38,27 +38,35 @@ ...@@ -38,27 +38,35 @@
* @subpackage Client * @subpackage Client
*/ */
/**
* @namespace
*/
namespace Solarium\QueryType\Extract;
use Solarium\Core\Query\QueryInterface;
use Solarium\Core\Query\RequestBuilder as BaseRequestBuilder;
use Solarium\Core\Client\Request;
use Solarium\Exception\RuntimeException;
/** /**
* Build an extract request * Build an extract request
* *
* @package Solarium * @package Solarium
* @subpackage Client * @subpackage Client
*/ */
class Solarium_Client_RequestBuilder_Extract extends Solarium_Client_RequestBuilder class RequestBuilder extends BaseRequestBuilder
{ {
/** /**
* Build the request * Build the request
* *
* @param Solarium_Query_Extract $query * @param Query $query
* @return Solarium_Client_Request * @return Request
*/ */
public function build($query) public function build(QueryInterface $query)
{ {
$request = parent::build($query); $request = parent::build($query);
$request->setMethod(Solarium_Client_Request::METHOD_POST); $request->setMethod(Request::METHOD_POST);
// common options
// add common options to request
$request->addParam('commit', $query->getCommit()); $request->addParam('commit', $query->getCommit());
$request->addParam('commitWithin', $query->getCommitWithin()); $request->addParam('commitWithin', $query->getCommitWithin());
...@@ -70,11 +78,10 @@ class Solarium_Client_RequestBuilder_Extract extends Solarium_Client_RequestBuil ...@@ -70,11 +78,10 @@ class Solarium_Client_RequestBuilder_Extract extends Solarium_Client_RequestBuil
$request->addParam('fmap.' . $fromField, $toField); $request->addParam('fmap.' . $fromField, $toField);
} }
// document // add document settings to request
if (($doc = $query->getDocument()) != null) { if (($doc = $query->getDocument()) != null) {
if ($doc->getBoost() !== null) { if ($doc->getBoost() !== null) {
throw new Solarium_Exception('Extract does not support document-level boosts, use field boosts instead.'); throw new RuntimeException('Extract does not support document-level boosts, use field boosts instead.');
} }
// literal.* // literal.*
...@@ -91,8 +98,7 @@ class Solarium_Client_RequestBuilder_Extract extends Solarium_Client_RequestBuil ...@@ -91,8 +98,7 @@ class Solarium_Client_RequestBuilder_Extract extends Solarium_Client_RequestBuil
} }
} }
// file // add file to request
$request->setFileUpload($query->getFile()); $request->setFileUpload($query->getFile());
$request->addParam('resource.name', basename($query->getFile())); $request->addParam('resource.name', basename($query->getFile()));
......
<?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
* @link http://www.solarium-project.org/
*/
/**
* @namespace
*/
namespace Solarium\QueryType\Extract;
use Solarium\QueryType\Update\Result as UpdateResult;
/**
* An extract result is similar to an update result, but we do want to return a query specific result class instead of
* an update query result class.
*/
class Result extends UpdateResult
{
}
...@@ -89,12 +89,12 @@ class ZendHttpTest extends \PHPUnit_Framework_TestCase ...@@ -89,12 +89,12 @@ class ZendHttpTest extends \PHPUnit_Framework_TestCase
public function testExecute() public function testExecute()
{ {
$method = Request::METHOD_GET; $method = Request::METHOD_POST;
$rawData = 'xyz'; $rawData = 'xyz';
$responseData = 'abc'; $responseData = 'abc';
$handler = 'myhandler'; $handler = 'myhandler';
$headers = array( $headers = array(
'Content-Type: application/x-www-form-urlencoded' 'X-test: 123'
); );
$request = new Request(); $request = new Request();
...@@ -116,7 +116,10 @@ class ZendHttpTest extends \PHPUnit_Framework_TestCase ...@@ -116,7 +116,10 @@ class ZendHttpTest extends \PHPUnit_Framework_TestCase
->with($this->equalTo('http://127.0.0.1:8983/solr/myhandler?')); ->with($this->equalTo('http://127.0.0.1:8983/solr/myhandler?'));
$mock->expects($this->once()) $mock->expects($this->once())
->method('setHeaders') ->method('setHeaders')
->with($this->equalTo($headers)); ->with($this->equalTo(array(
'X-test: 123',
'Content-Type: text/xml; charset=UTF-8',
)));
$mock->expects($this->once()) $mock->expects($this->once())
->method('setRawData') ->method('setRawData')
->with($this->equalTo($rawData)); ->with($this->equalTo($rawData));
......
...@@ -488,6 +488,7 @@ authentication: Array ...@@ -488,6 +488,7 @@ authentication: Array
resource: /myHandler?param1=1&param2=test+content resource: /myHandler?param1=1&param2=test+content
resource urldecoded: /myHandler?param1=1&param2=test content resource urldecoded: /myHandler?param1=1&param2=test content
raw data: post data raw data: post data
file upload:
', ',
(string) $this->request (string) $this->request
); );
......
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