Solr   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 145
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configure_server() 0 7 1
A service() 0 23 4
A get_indexes() 0 3 1
A solr_options() 0 35 4
A set_service_class() 0 4 1
1
<?php
2
3
namespace SilverStripe\FullTextSearch\Solr;
4
5
use SilverStripe\Control\Director;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\Core\Manifest\Module;
8
use SilverStripe\Core\Manifest\ModuleLoader;
9
use SilverStripe\FullTextSearch\Search\FullTextSearch;
10
use SilverStripe\FullTextSearch\Solr\Services\Solr4Service;
11
use SilverStripe\FullTextSearch\Solr\Services\Solr3Service;
12
use SilverStripe\FullTextSearch\Solr\Services\SolrService;
13
use SilverStripe\FullTextSearch\Solr\Services\SolrService_Core;
14
15
class Solr
16
{
17
    /**
18
     * Configuration on where to find the solr server and how to get new index configurations into it.
19
     *
20
     * Required fields:
21
     * host (default: localhost) - The host or IP Solr is listening on
22
     * port (default: 8983) - The port Solr is listening on
23
     * path (default: /solr) - The suburl the solr service is available on
24
     *
25
     * Optional fields:
26
     * version (default: 4) - The Solr server version. Currently supports 3 and 4 (you can add a sub-version like 4.5 if
27
     *   you like, but currently it has no effect)
28
     * service (default: depends on version, Solr3Service for 3, Solr4Service for 4)
29
     *   the class that provides actual communcation to the Solr server
30
     * extraspath (default: <basefolder>/fulltextsearch/conf/solr/{version}/extras/) - Absolute path to
31
     *   the folder containing templates which are used for generating the schema and field definitions.
32
     * templates (default: <basefolder>/fulltextsearch/conf/solr/{version}/templates/) - Absolute path to
33
     *   the configuration default files, e.g. solrconfig.xml.
34
     *
35
     * indexstore => an array with
36
     *
37
     *   mode - a classname which implements SolrConfigStore, or 'file', 'webdav' or 'post'
38
     *
39
     *   When mode == SolrConfigStore_File or 'file' (indexes should be written on a local filesystem)
40
     *      path - The (locally accessible) path to write the index configurations to.
41
     *      remotepath (default: the same as indexpath) - The path that the Solr server will read the index configurations from
42
     *
43
     *   When mode == SolrConfigStore_Post or 'post' (indexes should stored on a remote Solr server via post)
44
     *   This mode will require custom software on the remote solr server which handles receiving the post and
45
     *   passing on that information to solr. It is up to the user of this mode to write such software.
46
     *      path (default: /solrindex) - The suburl on the solr host that is set up to accept index configurations
47
     *      port (default: none) - The port on the remote server which is set up to receive the post information
48
     *
49
     *   When mode == SolrConfigStore_WebDAV or 'webdav' (indexes should stored on a remote Solr server via webdav)
50
     *      auth (default: none) - A username:password pair string to use to auth against the webdav server
51
     *      path (default: /solrindex) - The suburl on the solr host that is set up to accept index configurations via webdav
52
     *      port (default: none) - The port for WebDAV if different from the Solr port
53
     *      remotepath - The path that the Solr server will read the index configurations from
54
     */
55
    protected static $solr_options = array();
56
57
    /** A cache of solr_options with the defaults all merged in */
58
    protected static $merged_solr_options = null;
59
60
    /**
61
     * Update the configuration for Solr. See $solr_options for a discussion of the accepted array keys
62
     * @param array $options - The options to update
63
     */
64
    public static function configure_server($options = array())
65
    {
66
        self::$solr_options = array_merge(self::$solr_options, $options);
67
        self::$merged_solr_options = null;
68
69
        self::$service_singleton = null;
70
        self::$service_core_singletons = array();
71
    }
72
73
    /**
74
     * Get the configured Solr options with the defaults all merged in
75
     * @return array - The merged options
76
     */
77
    public static function solr_options()
78
    {
79
        if (self::$merged_solr_options) {
80
            return self::$merged_solr_options;
81
        }
82
83
        $defaults = array(
84
            'host' => 'localhost',
85
            'port' => 8983,
86
            'path' => '/solr',
87
            'version' => '4'
88
        );
89
90
        // Build some by-version defaults
91
        $version = isset(self::$solr_options['version']) ? self::$solr_options['version'] : $defaults['version'];
92
93
        /** @var Module $module */
94
        $module = ModuleLoader::getModule('silverstripe/fulltextsearch');
95
        $modulePath = $module->getPath();
96
97
        if (version_compare($version, '4', '>=')) {
98
            $versionDefaults = [
99
                'service'       => Solr4Service::class,
100
                'extraspath'    => $modulePath . '/conf/solr/4/extras/',
101
                'templatespath' => $modulePath . '/conf/solr/4/templates/',
102
            ];
103
        } else {
104
            $versionDefaults = [
105
                'service'       => Solr3Service::class,
106
                'extraspath'    => $modulePath . '/conf/solr/3/extras/',
107
                'templatespath' => $modulePath . '/conf/solr/3/templates/',
108
            ];
109
        }
110
111
        return (self::$merged_solr_options = array_merge($defaults, $versionDefaults, self::$solr_options));
112
    }
113
114
115
    public static function set_service_class($class)
116
    {
117
        user_error('set_service_class is deprecated - pass as part of $options to configure_server', E_USER_WARNING);
118
        self::configure_server(array('service' => $class));
119
    }
120
121
    /** @var SolrService | null - The instance of SolrService for core management */
122
    protected static $service_singleton = null;
123
    /** @var SolrService_Core[] - The instances of SolrService_Core for each core */
124
    protected static $service_core_singletons = array();
125
126
    /**
127
     * Get a SolrService
128
     *
129
     * @param string $core Optional name of index class
130
     * @return SolrService|SolrService_Core
131
     */
132
    public static function service($core = null)
133
    {
134
        $options = self::solr_options();
135
136
        if (!self::$service_singleton) {
137
            self::$service_singleton = Injector::inst()->create(
138
                $options['service'],
139
                $options['host'],
140
                $options['port'],
141
                $options['path']
142
            );
143
        }
144
145
        if ($core) {
146
            if (!isset(self::$service_core_singletons[$core])) {
147
                self::$service_core_singletons[$core] = self::$service_singleton->serviceForCore(
148
                    singleton($core)->getIndexName()
149
                );
150
            }
151
152
            return self::$service_core_singletons[$core];
153
        }
154
        return self::$service_singleton;
155
    }
156
157
    public static function get_indexes()
158
    {
159
        return FullTextSearch::get_indexes(SolrIndex::class);
160
    }
161
}
162