* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*
* @package Solarium
* @subpackage Client
*/
/**
* Build an update request
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_RequestBuilder_Update extends Solarium_Client_RequestBuilder
{
/**
* Build request for an update query
*
* @param Solarium_Query_Update $query
* @return Solarium_Client_Request
*/
public function build($query)
{
$request = parent::build($query);
$request->setMethod(Solarium_Client_Request::METHOD_POST);
$request->setRawData($this->getRawData($query));
return $request;
}
/**
* Generates raw POST data
*
* Each commandtype is delegated to a separate builder method.
*
* @param Solarium_Query_Update $query
* @throws Solarium_Exception
* @return string
*/
public function getRawData($query)
{
$xml = '';
foreach ($query->getCommands() AS $command) {
switch ($command->getType()) {
case Solarium_Query_Update::COMMAND_ADD:
$xml .= $this->buildAddXml($command);
break;
case Solarium_Query_Update::COMMAND_DELETE:
$xml .= $this->buildDeleteXml($command);
break;
case Solarium_Query_Update::COMMAND_OPTIMIZE:
$xml .= $this->buildOptimizeXml($command);
break;
case Solarium_Query_Update::COMMAND_COMMIT:
$xml .= $this->buildCommitXml($command);
break;
case Solarium_Query_Update::COMMAND_ROLLBACK:
$xml .= $this->buildRollbackXml();
break;
default:
throw new Solarium_Exception('Unsupported command type');
break;
}
}
$xml .= '';
return $xml;
}
/**
* Build XML for an add command
*
* @param Solarium_Query_Update_Command_Add $command
* @return string
*/
public function buildAddXml($command)
{
$xml = 'boolAttrib('overwrite', $command->getOverwrite());
$xml .= $this->attrib('commitWithin', $command->getCommitWithin());
$xml .= '>';
foreach ($command->getDocuments() AS $doc) {
$xml .= 'attrib('boost', $doc->getBoost());
$xml .= '>';
foreach ($doc->getFields() AS $name => $value) {
$boost = $doc->getFieldBoost($name);
if (is_array($value)) {
foreach ($value AS $multival) {
$xml .= $this->_buildFieldXml($name, $boost, $multival);
}
} else {
$xml .= $this->_buildFieldXml($name, $boost, $value);
}
}
$xml .= '';
}
$xml .= '';
return $xml;
}
/**
* Build XML for a field
*
* Used in the add command
*
* @param string $name
* @param float $boost
* @param mixed $value
* @return string
*/
protected function _buildFieldXml($name, $boost, $value)
{
$xml = 'attrib('boost', $boost);
$xml .= '>' . htmlspecialchars($value, ENT_NOQUOTES, 'UTF-8');
$xml .= '';
return $xml;
}
/**
* Build XML for a delete command
*
* @param Solarium_Query_Update_Command_Delete $command
* @return string
*/
public function buildDeleteXml($command)
{
$xml = '';
foreach ($command->getIds() AS $id) {
$xml .= '' . htmlspecialchars($id, ENT_NOQUOTES, 'UTF-8')
. '';
}
foreach ($command->getQueries() AS $query) {
$xml .= '' . htmlspecialchars($query, ENT_NOQUOTES, 'UTF-8')
. '';
}
$xml .= '';
return $xml;
}
/**
* Build XML for an update command
*
* @param Solarium_Query_Update_Command_Optimize $command
* @return string
*/
public function buildOptimizeXml($command)
{
$xml = 'boolAttrib('waitFlush', $command->getWaitFlush());
$xml .= $this->boolAttrib('waitSearcher', $command->getWaitSearcher());
$xml .= $this->attrib('maxSegments', $command->getMaxSegments());
$xml .= '/>';
return $xml;
}
/**
* Build XML for a commit command
*
* @param Solarium_Query_Update_Command_Commit $command
* @return string
*/
public function buildCommitXml($command)
{
$xml = 'boolAttrib('waitFlush', $command->getWaitFlush());
$xml .= $this->boolAttrib('waitSearcher', $command->getWaitSearcher());
$xml .= $this->boolAttrib(
'expungeDeletes',
$command->getExpungeDeletes()
);
$xml .= '/>';
return $xml;
}
/**
* Build XMl for a rollback command
*
* @return string
*/
public function buildRollbackXml()
{
return '';
}
}