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