Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 42 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
IndexAliasValidator | |
0.00% |
0 / 42 |
|
0.00% |
0 / 3 |
156 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
validate | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
72 | |||
shouldRemoveFromAlias | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
updateIndices | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace CirrusSearch\Maintenance\Validators; |
4 | |
5 | use CirrusSearch\Maintenance\ConfigUtils; |
6 | use CirrusSearch\Maintenance\Printer; |
7 | use Elastica\Client; |
8 | use MediaWiki\Language\RawMessage; |
9 | use MediaWiki\Status\Status; |
10 | |
11 | abstract class IndexAliasValidator extends Validator { |
12 | /** |
13 | * @var Client |
14 | */ |
15 | protected $client; |
16 | |
17 | /** |
18 | * @var string |
19 | */ |
20 | protected $aliasName; |
21 | |
22 | /** |
23 | * @var string |
24 | */ |
25 | protected $specificIndexName; |
26 | |
27 | /** |
28 | * @var bool |
29 | */ |
30 | private $startOver; |
31 | |
32 | /** |
33 | * @var array |
34 | */ |
35 | protected $create = []; |
36 | |
37 | /** |
38 | * @var array |
39 | */ |
40 | protected $remove = []; |
41 | |
42 | /** |
43 | * @var ConfigUtils |
44 | */ |
45 | private $configUtils; |
46 | |
47 | /** |
48 | * @param Client $client |
49 | * @param string $aliasName |
50 | * @param string $specificIndexName |
51 | * @param bool $startOver |
52 | * @param Printer $out |
53 | */ |
54 | public function __construct( Client $client, $aliasName, $specificIndexName, $startOver, Printer $out ) { |
55 | parent::__construct( $out ); |
56 | |
57 | $this->client = $client; |
58 | $this->aliasName = $aliasName; |
59 | $this->specificIndexName = $specificIndexName; |
60 | $this->startOver = $startOver; |
61 | $this->configUtils = new ConfigUtils( $client, $out ); |
62 | } |
63 | |
64 | public function validate(): Status { |
65 | // arrays of aliases to be added/removed |
66 | $add = $remove = []; |
67 | |
68 | $this->outputIndented( "\tValidating $this->aliasName alias..." ); |
69 | $sv = $this->configUtils->isIndex( $this->aliasName ); |
70 | if ( !$sv->isGood() ) { |
71 | return $sv; |
72 | } |
73 | if ( $sv->getValue() ) { |
74 | $this->output( "is an index..." ); |
75 | if ( $this->startOver ) { |
76 | $this->client->getIndex( $this->aliasName )->delete(); |
77 | $this->output( "index removed..." ); |
78 | |
79 | $add[] = $this->specificIndexName; |
80 | } else { |
81 | $this->output( "cannot correct!\n" ); |
82 | return Status::newFatal( new RawMessage( |
83 | "There is currently an index with the name of the alias. Rerun this\n" . |
84 | "script with --startOver and it'll remove the index and continue.\n" ) ); |
85 | } |
86 | } else { |
87 | $sv = $this->configUtils->getIndicesWithAlias( $this->aliasName ); |
88 | if ( !$sv->isGood() ) { |
89 | return $sv; |
90 | } |
91 | foreach ( $sv->getValue() as $indexName ) { |
92 | if ( $indexName === $this->specificIndexName ) { |
93 | $this->output( "ok\n" ); |
94 | return Status::newGood(); |
95 | } elseif ( $this->shouldRemoveFromAlias( $indexName ) ) { |
96 | $remove[] = $indexName; |
97 | } |
98 | } |
99 | |
100 | $add[] = $this->specificIndexName; |
101 | } |
102 | |
103 | return $this->updateIndices( $add, $remove ); |
104 | } |
105 | |
106 | /** |
107 | * @param string $name |
108 | * @return bool |
109 | */ |
110 | abstract protected function shouldRemoveFromAlias( $name ); |
111 | |
112 | /** |
113 | * @param string[] $add Array of indices to add |
114 | * @param string[] $remove Array of indices to remove |
115 | * @return Status |
116 | */ |
117 | protected function updateIndices( array $add, array $remove ) { |
118 | $remove = array_filter( $remove, function ( $name ) { |
119 | return $this->client->getIndex( $name )->exists(); |
120 | } ); |
121 | if ( $remove ) { |
122 | $this->outputIndented( "\tRemoving old indices...\n" ); |
123 | foreach ( $remove as $indexName ) { |
124 | $this->outputIndented( "\t\t$indexName..." ); |
125 | $this->client->getIndex( $indexName )->delete(); |
126 | $this->output( "done\n" ); |
127 | } |
128 | } |
129 | |
130 | return Status::newGood(); |
131 | } |
132 | } |