Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Version
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
 newLog
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace CirrusSearch;
4
5use MediaWiki\Status\Status;
6
7/**
8 * Fetch the Elasticsearch version
9 *
10 * @license GPL-2.0-or-later
11 */
12class Version extends ElasticsearchIntermediary {
13
14    public function __construct( Connection $conn ) {
15        parent::__construct( $conn, null, 0 );
16    }
17
18    /**
19     * Get the version of Elasticsearch with which we're communicating.
20     *
21     * @return Status<array<string,string>> version number as a string
22     */
23    public function get() {
24        try {
25            $this->startNewLog( 'fetching elasticsearch version', 'version' );
26            // If this times out the cluster is in really bad shape but we should still
27            // check it.
28            $this->connection->setTimeout( $this->getClientTimeout( 'version' ) );
29            $response = $this->connection->getClient()->request( '' );
30            self::throwIfNotOk( $this->connection, $response );
31            $this->success();
32        } catch ( \Elastica\Exception\ExceptionInterface $e ) {
33            return $this->failure( $e );
34        }
35        $version = $response->getData()['version'];
36        return Status::newGood( [
37            'distribution' => $version['distribution'] ?? 'elasticsearch',
38            'version' => $version['number'],
39        ] );
40    }
41
42    /**
43     * @param string $description
44     * @param string $queryType
45     * @param string[] $extra
46     * @return SearchRequestLog
47     */
48    protected function newLog( $description, $queryType, array $extra = [] ) {
49        return new SearchRequestLog(
50            $this->connection->getClient(),
51            $description,
52            $queryType,
53            $extra
54        );
55    }
56}