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

- implemented postbigrequest plugin

- added an example script
- added unittests
parent 3cf80cd2
<?php
require('init.php');
htmlHeader();
// create a client instance and autoload the postbigrequest plugin
$client = new Solarium_Client($config);
$client->getPlugin('postbigrequest');
// create a basic query to execute
$query = $client->createSelect();
// add a huge filterquery to create a very long query string
// note: normally you would use a range for this, it's just an easy way to create a very big querystring as a test
$fq = '';
for($i=1; $i<=1000; $i++) {
$fq .= ' OR price:'.$i;
}
$fq = substr($fq, 4);
$query->createFilterQuery('fq')->setQuery($fq);
// without the plugin this query would fail as it is bigger than the default servlet container header buffer
$resultset = $client->select($query);
// display the total number of documents found by solr
echo 'NumFound: '.$resultset->getNumFound();
// show documents using the resultset iterator
foreach ($resultset as $document) {
echo '<hr/><table>';
// the documents are also iterable, to get all fields
foreach($document AS $field => $value)
{
// this converts multivalue fields to a comma-separated string
if(is_array($value)) $value = implode(', ', $value);
echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>';
}
echo '</table>';
}
htmlFooter();
\ No newline at end of file
...@@ -113,6 +113,7 @@ ...@@ -113,6 +113,7 @@
<li>7. Plugins</li> <li>7. Plugins</li>
<ul style="list-style:none;"> <ul style="list-style:none;">
<li><a href="7.1-plugin-loadbalancer.php">7.1 Loadbalancer</a></li> <li><a href="7.1-plugin-loadbalancer.php">7.1 Loadbalancer</a></li>
<li><a href="7.2-plugin-postbigrequest.php">7.2 Post Big Requests</a></li>
</ul> </ul>
</ul> </ul>
......
...@@ -60,6 +60,27 @@ class Solarium_Plugin_PostBigRequest extends Solarium_Plugin_Abstract ...@@ -60,6 +60,27 @@ class Solarium_Plugin_PostBigRequest extends Solarium_Plugin_Abstract
'maxquerystringlength' => 1024, 'maxquerystringlength' => 1024,
); );
/**
* Set maxquerystringlength enabled option
*
* @param integer $value
* @return self Provides fluent interface
*/
public function setMaxQueryStringLength($value)
{
return $this->_setOption('maxquerystringlength', $value);
}
/**
* Get maxquerystringlength option
*
* @return integer
*/
public function getMaxQueryStringLength()
{
return $this->getOption('maxquerystringlength');
}
/** /**
* Event hook to adjust client settings just before query execution * Event hook to adjust client settings just before query execution
* *
...@@ -69,7 +90,13 @@ class Solarium_Plugin_PostBigRequest extends Solarium_Plugin_Abstract ...@@ -69,7 +90,13 @@ class Solarium_Plugin_PostBigRequest extends Solarium_Plugin_Abstract
*/ */
public function postCreateRequest($query, $request) public function postCreateRequest($query, $request)
{ {
$queryString = $request->getQueryString();
if (strlen($queryString) > $this->getMaxQueryStringLength()) {
$request->setMethod(Solarium_Client_Request::METHOD_POST);
$request->setRawData($queryString);
$request->clearParams();
$request->addHeader('Content-Type: application/x-www-form-urlencoded');
}
} }
} }
\ 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.
*/
class Solarium_Plugin_PostBigRequestTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Plugin_PostBigRequest
*/
protected $_plugin;
/**
* @var Solarium_Client
*/
protected $_client;
/**
* @var Solarium_Query_Select
*/
protected $query;
public function setUp()
{
$this->_plugin = new Solarium_Plugin_PostBigRequest();
$this->_client = new Solarium_Client();
$this->_query = $this->_client->createSelect();
}
public function testSetAndGetMaxQueryStringLength()
{
$this->_plugin->setMaxQueryStringLength(512);
$this->assertEquals(512, $this->_plugin->getMaxQueryStringLength());
}
public function testPostCreateRequest()
{
// create a very long query
$fq = '';
for($i=1; $i<=1000; $i++) {
$fq .= ' OR price:'.$i;
}
$fq = substr($fq, 4);
$this->_query->createFilterQuery('fq')->setQuery($fq);
$requestOutput = $this->_client->createRequest($this->_query);
$requestInput = clone $requestOutput;
$this->_plugin->postCreateRequest($this->_query, $requestOutput);
$this->assertEquals(Solarium_Client_Request::METHOD_GET, $requestInput->getMethod());
$this->assertEquals(Solarium_Client_Request::METHOD_POST, $requestOutput->getMethod());
$this->assertEquals($requestInput->getQueryString(), $requestOutput->getRawData());
$this->assertEquals('', $requestOutput->getQueryString());
}
public function testPostCreateRequestUnalteredSmallRequest()
{
$requestOutput = $this->_client->createRequest($this->_query);
$requestInput = clone $requestOutput;
$this->_plugin->postCreateRequest($this->_query, $requestOutput);
$this->assertEquals($requestInput, $requestOutput);
}
}
\ No newline at end of file
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