Solr4Service_Core::addDocument()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\FullTextSearch\Solr\Services;
4
5
class Solr4Service_Core extends SolrService_Core
6
{
7
    /**
8
     * Replace underlying commit function to remove waitFlush in 4.0+, since it's been deprecated and 4.4 throws errors
9
     * if you pass it
10
     */
11
    public function commit($expungeDeletes = false, $waitFlush = null, $waitSearcher = true, $timeout = 3600)
12
    {
13
        if ($waitFlush) {
14
            user_error('waitFlush must be false when using Solr 4.0+' . E_USER_ERROR);
15
        }
16
17
        $expungeValue = $expungeDeletes ? 'true' : 'false';
18
        $searcherValue = $waitSearcher ? 'true' : 'false';
19
20
        $rawPost = '<commit expungeDeletes="' . $expungeValue . '" waitSearcher="' . $searcherValue . '" />';
21
        return $this->_sendRawPost($this->_updateUrl, $rawPost, $timeout);
22
    }
23
24
    /**
25
     * @inheritdoc
26
     * @see Solr4Service_Core::addDocuments
27
     */
28
    public function addDocument(
29
        \Apache_Solr_Document $document,
30
        $allowDups = false,
31
        $overwritePending = true,
32
        $overwriteCommitted = true,
33
        $commitWithin = 0
34
    ) {
35
        return $this->addDocuments(array($document), $allowDups, $overwritePending, $overwriteCommitted, $commitWithin);
36
    }
37
38
    /**
39
     * Solr 4.0 compat http://wiki.apache.org/solr/UpdateXmlMessages#Optional_attributes_for_.22add.22
40
     * Remove allowDups, overwritePending and overwriteComitted
41
     */
42
    public function addDocuments(
43
        $documents,
44
        $allowDups = false,
45
        $overwritePending = true,
46
        $overwriteCommitted = true,
47
        $commitWithin = 0
48
    ) {
49
        $overwriteVal = $allowDups ? 'false' : 'true';
50
        $commitWithin = (int) $commitWithin;
51
        $commitWithinString = $commitWithin > 0 ? " commitWithin=\"{$commitWithin}\"" : '';
52
53
        $rawPost = "<add overwrite=\"{$overwriteVal}\"{$commitWithinString}>";
54
        foreach ($documents as $document) {
55
            if ($document instanceof \Apache_Solr_Document) {
56
                $rawPost .= $this->_documentToXmlFragment($document);
57
            }
58
        }
59
        $rawPost .= '</add>';
60
61
        return $this->add($rawPost);
62
    }
63
}
64