Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
MappingValidator
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 validate
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
 compareMappingToActual
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace CirrusSearch\Maintenance\Validators;
4
5use CirrusSearch\ElasticaErrorHandler;
6use CirrusSearch\Maintenance\Printer;
7use Elastica\Exception\ExceptionInterface;
8use Elastica\Index;
9use Elastica\Mapping;
10use MediaWiki\Language\RawMessage;
11use MediaWiki\Status\Status;
12use Wikimedia\Assert\Assert;
13
14class MappingValidator extends Validator {
15    /**
16     * @var Index
17     */
18    private $index;
19
20    /**
21     * @var string
22     */
23    private $masterTimeout;
24
25    /**
26     * @var bool
27     */
28    private $optimizeIndexForExperimentalHighlighter;
29
30    /**
31     * @var array
32     */
33    private $availablePlugins;
34
35    /**
36     * @var array
37     */
38    private $mappingConfig;
39
40    /**
41     * @todo this constructor takes way too much arguments - refactor
42     *
43     * @param Index $index
44     * @param string $masterTimeout
45     * @param bool $optimizeIndexForExperimentalHighlighter
46     * @param array $availablePlugins
47     * @param array $mappingConfig
48     * @param Printer|null $out
49     */
50    public function __construct(
51        Index $index,
52        $masterTimeout,
53        $optimizeIndexForExperimentalHighlighter,
54        array $availablePlugins,
55        array $mappingConfig,
56        ?Printer $out = null
57    ) {
58        parent::__construct( $out );
59
60        $this->index = $index;
61        $this->masterTimeout = $masterTimeout;
62        $this->optimizeIndexForExperimentalHighlighter = $optimizeIndexForExperimentalHighlighter;
63        $this->availablePlugins = $availablePlugins;
64        // Could be supported, but prefer consistency
65        Assert::parameter( isset( $mappingConfig['properties'] ), '$mappingConfig',
66            'Mapping types are no longer supported, properties must be top level' );
67        $this->mappingConfig = $mappingConfig;
68    }
69
70    /**
71     * @return Status
72     */
73    public function validate() {
74        $this->outputIndented( "Validating mappings..." );
75        if ( !$this->compareMappingToActual() ) {
76            $action = new Mapping( $this->mappingConfig['properties'] );
77            $action->setParam( "dynamic", false );
78
79            try {
80                $action->send( $this->index, [
81                    'master_timeout' => $this->masterTimeout,
82                ] );
83                $this->output( "corrected\n" );
84            } catch ( ExceptionInterface $e ) {
85                $this->output( "failed!\n" );
86                $message = ElasticaErrorHandler::extractMessage( $e );
87                return Status::newFatal( new RawMessage(
88                    "Couldn't update existing mappings. You may need to reindex.\nHere is elasticsearch's error message: $message\n" ) );
89            }
90        }
91
92        return Status::newGood();
93    }
94
95    /**
96     * Check that the mapping returned from Elasticsearch is as we want it.
97     *
98     * @return bool is the mapping good enough for us?
99     */
100    private function compareMappingToActual() {
101        $actualMappings = $this->index->getMapping();
102        $this->output( "\n" );
103        $this->outputIndented( "\tValidating mapping..." );
104        if ( $this->checkConfig( $actualMappings, $this->mappingConfig ) ) {
105            $this->output( "ok\n" );
106            return true;
107        } else {
108            $this->output( "different..." );
109            return false;
110        }
111    }
112}