Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
Validator | |
0.00% |
0 / 33 |
|
0.00% |
0 / 7 |
342 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
validate | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
printDebugCheckConfig | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
checkConfig | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
56 | |||
normalizeConfigValue | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
debugCheckConfig | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
output | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
outputIndented | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace CirrusSearch\Maintenance\Validators; |
4 | |
5 | use CirrusSearch\Maintenance\Printer; |
6 | use MediaWiki\Status\Status; |
7 | |
8 | abstract class Validator { |
9 | /** |
10 | * @var Printer |
11 | */ |
12 | protected $out; |
13 | |
14 | /** |
15 | * @var bool |
16 | */ |
17 | protected $printDebugCheckConfig = false; |
18 | |
19 | /** |
20 | * @param Printer $out to relay output to. |
21 | */ |
22 | public function __construct( Printer $out ) { |
23 | $this->out = $out; |
24 | } |
25 | |
26 | /** |
27 | * @return Status |
28 | */ |
29 | abstract public function validate(); |
30 | |
31 | /** |
32 | * @param bool $print |
33 | */ |
34 | public function printDebugCheckConfig( $print = true ) { |
35 | $this->printDebugCheckConfig = (bool)$print; |
36 | } |
37 | |
38 | /** |
39 | * @param mixed $actual |
40 | * @param array $required |
41 | * @param string|null $indent |
42 | * @return bool |
43 | */ |
44 | protected function checkConfig( $actual, array $required, $indent = null ) { |
45 | foreach ( $required as $key => $value ) { |
46 | $this->debugCheckConfig( "\n$indent$key: " ); |
47 | if ( !array_key_exists( $key, $actual ) ) { |
48 | $this->debugCheckConfig( "not found..." ); |
49 | return false; |
50 | } |
51 | if ( is_array( $value ) ) { |
52 | $this->debugCheckConfig( "descend..." ); |
53 | if ( !is_array( $actual[ $key ] ) ) { |
54 | $this->debugCheckConfig( "other not array..." ); |
55 | return false; |
56 | } |
57 | if ( !$this->checkConfig( $actual[ $key ], $value, $indent . "\t" ) ) { |
58 | return false; |
59 | } |
60 | continue; |
61 | } |
62 | |
63 | $actual[ $key ] = $this->normalizeConfigValue( $actual[ $key ] ); |
64 | $value = $this->normalizeConfigValue( $value ); |
65 | $this->debugCheckConfig( $actual[ $key ] . " ?? $value..." ); |
66 | // Note that I really mean !=, not !==. Coercion is cool here. |
67 | // print $actual[ $key ] . " $value\n"; |
68 | if ( $actual[ $key ] != $value ) { |
69 | $this->debugCheckConfig( 'different...' ); |
70 | return false; |
71 | } |
72 | } |
73 | return true; |
74 | } |
75 | |
76 | /** |
77 | * Normalize a config value for comparison. Elasticsearch will accept all kinds |
78 | * of config values but it tends to through back 'true' for true and 'false' for |
79 | * false so we normalize everything. Sometimes, oddly, it'll through back false |
80 | * for false.... |
81 | * |
82 | * @param mixed $value |
83 | * @return mixed |
84 | */ |
85 | private function normalizeConfigValue( $value ) { |
86 | if ( $value === true ) { |
87 | return 'true'; |
88 | } elseif ( $value === false ) { |
89 | return 'false'; |
90 | } |
91 | return $value; |
92 | } |
93 | |
94 | /** |
95 | * @param string $string |
96 | */ |
97 | private function debugCheckConfig( $string ) { |
98 | if ( $this->printDebugCheckConfig ) { |
99 | $this->output( $string ); |
100 | } |
101 | } |
102 | |
103 | /** |
104 | * @param string $message |
105 | * @param mixed|null $channel |
106 | */ |
107 | protected function output( $message, $channel = null ) { |
108 | if ( $this->out ) { |
109 | $this->out->output( $message, $channel ); |
110 | } |
111 | } |
112 | |
113 | /** |
114 | * @param string $message |
115 | */ |
116 | protected function outputIndented( $message ) { |
117 | if ( $this->out ) { |
118 | $this->out->outputIndented( $message ); |
119 | } |
120 | } |
121 | } |