From 892562c2baad2f0afdbb1322a71f295c5693040d Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Tue, 17 Jun 2014 17:56:46 +1200 Subject: [PATCH] BUG Fix Solr 4.0 compatibility issue --- code/solr/Solr4Service.php | 32 ++++++++++++++++++ tests/Solr4ServiceTest.php | 66 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 tests/Solr4ServiceTest.php diff --git a/code/solr/Solr4Service.php b/code/solr/Solr4Service.php index c3c430d..037b337 100644 --- a/code/solr/Solr4Service.php +++ b/code/solr/Solr4Service.php @@ -17,6 +17,38 @@ class Solr4Service_Core extends SolrService_Core { $rawPost = ''; return $this->_sendRawPost($this->_updateUrl, $rawPost, $timeout); } + + /** + * @inheritdoc + * @see Solr4Service_Core::addDocuments + */ + public function addDocument(Apache_Solr_Document $document, $allowDups = false, + $overwritePending = true, $overwriteCommitted = true, $commitWithin = 0 + ) { + return $this->addDocuments(array($document), $allowDups, $overwritePending, $overwriteCommitted, $commitWithin); + } + + /** + * Solr 4.0 compat http://wiki.apache.org/solr/UpdateXmlMessages#Optional_attributes_for_.22add.22 + * Remove allowDups, overwritePending and overwriteComitted + */ + public function addDocuments($documents, $allowDups = false, $overwritePending = true, + $overwriteCommitted = true, $commitWithin = 0 + ) { + $overwriteVal = $allowDups ? 'false' : 'true'; + $commitWithin = (int) $commitWithin; + $commitWithinString = $commitWithin > 0 ? " commitWithin=\"{$commitWithin}\"" : ''; + + $rawPost = ""; + foreach ($documents as $document) { + if ($document instanceof Apache_Solr_Document) { + $rawPost .= $this->_documentToXmlFragment($document); + } + } + $rawPost .= ''; + + return $this->add($rawPost); + } } class Solr4Service extends SolrService { diff --git a/tests/Solr4ServiceTest.php b/tests/Solr4ServiceTest.php new file mode 100644 index 0000000..0ba2ca3 --- /dev/null +++ b/tests/Solr4ServiceTest.php @@ -0,0 +1,66 @@ +setField('id', $id); + $document->setField('title', "Item $id"); + return $document; + } + + public function testAddDocument() { + $service = $this->getMockService(); + $sent = $service->addDocument($this->getMockDocument('A'), false); + $this->assertEquals( + 'AItem A', + $sent + ); + $sent = $service->addDocument($this->getMockDocument('B'), true); + $this->assertEquals( + 'BItem B', + $sent + ); + } + + public function testAddDocuments() { + $service = $this->getMockService(); + $sent = $service->addDocuments(array( + $this->getMockDocument('C'), + $this->getMockDocument('D') + ), false); + $this->assertEquals( + 'CItem CDItem D', + $sent + ); + $sent = $service->addDocuments(array( + $this->getMockDocument('E'), + $this->getMockDocument('F') + ), true); + $this->assertEquals( + 'EItem EFItem F', + $sent + ); + } +} + +class Solr4ServiceTest_RecordingService extends Solr4Service_Core { + protected function _sendRawPost($url, $rawPost, $timeout = FALSE, $contentType = 'text/xml; charset=UTF-8') { + return $rawPost; + } + + protected function _sendRawGet($url, $timeout = FALSE) { + return $url; + } +}