Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
IndexAllAliasValidator
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 3
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 updateIndices
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 shouldRemoveFromAlias
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace CirrusSearch\Maintenance\Validators;
4
5use CirrusSearch\Maintenance\Printer;
6use Elastica\Client;
7use MediaWiki\Status\Status;
8
9class IndexAllAliasValidator extends IndexAliasValidator {
10    /**
11     * @var string prefix of names of indices that should be removed
12     */
13    protected $shouldRemovePrefix;
14
15    /**
16     * @param Client $client
17     * @param string $aliasName
18     * @param string $specificIndexName
19     * @param bool $startOver
20     * @param string $type
21     * @param Printer|null $out
22     */
23    public function __construct( Client $client, $aliasName, $specificIndexName, $startOver, $type, Printer $out = null ) {
24        parent::__construct( $client, $aliasName, $specificIndexName, $startOver, $out );
25        $this->shouldRemovePrefix = $type;
26    }
27
28    /**
29     * @param string[] $add
30     * @param string[] $remove
31     * @return Status
32     */
33    protected function updateIndices( array $add, array $remove ) {
34        $data = [];
35
36        $this->output( "alias not already assigned to this index..." );
37
38        // We'll remove the all alias from the indices that we're about to delete while
39        // we add it to this index.  Elastica doesn't support this well so we have to
40        // build the request to Elasticsearch ourselves.
41
42        foreach ( $add as $indexName ) {
43            $data['actions'][] = [ 'add' => [ 'index' => $indexName, 'alias' => $this->aliasName ] ];
44        }
45
46        foreach ( $remove as $indexName ) {
47            $data['actions'][] = [ 'remove' => [ 'index' => $indexName, 'alias' => $this->aliasName ] ];
48        }
49
50        $this->client->request( '_aliases', \Elastica\Request::POST, $data );
51        $this->output( "corrected\n" );
52
53        return parent::updateIndices( $add, $remove );
54    }
55
56    /**
57     * @param string $name
58     * @return bool
59     */
60    protected function shouldRemoveFromAlias( $name ) {
61        // Only if the name starts with the type being processed otherwise we'd
62        // remove the content index from the all alias.
63        return strpos( $name, "$this->shouldRemovePrefix" ) === 0;
64    }
65}