Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
64.89% |
61 / 94 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ConfigurationValidator | |
64.89% |
61 / 94 |
|
33.33% |
1 / 3 |
42.94 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| validateConfiguration | |
73.75% |
59 / 80 |
|
0.00% |
0 / 1 |
14.60 | |||
| isValidRemoveTags | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
90 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Wikispeech; |
| 4 | |
| 5 | /** |
| 6 | * @file |
| 7 | * @ingroup Extensions |
| 8 | * @license GPL-2.0-or-later |
| 9 | */ |
| 10 | |
| 11 | use Config; |
| 12 | use Psr\Log\LoggerInterface; |
| 13 | |
| 14 | class ConfigurationValidator { |
| 15 | |
| 16 | /** @var Config */ |
| 17 | private $config; |
| 18 | |
| 19 | /** @var LoggerInterface */ |
| 20 | private $logger; |
| 21 | |
| 22 | public function __construct( Config $config, LoggerInterface $logger ) { |
| 23 | $this->config = $config; |
| 24 | $this->logger = $logger; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Investigates whether or not configuration is valid. |
| 29 | * |
| 30 | * Writes all invalid configuration entries to the log. |
| 31 | * |
| 32 | * @since 0.1.11 |
| 33 | * @return bool true if all configuration passes validation |
| 34 | */ |
| 35 | public function validateConfiguration(): bool { |
| 36 | $success = true; |
| 37 | |
| 38 | $speechoidUrl = $this->config->get( 'WikispeechSpeechoidUrl' ); |
| 39 | if ( !filter_var( $speechoidUrl, FILTER_VALIDATE_URL ) ) { |
| 40 | $this->logger |
| 41 | ->warning( __METHOD__ . ': Configuration value for ' . |
| 42 | '\'WikispeechSpeechoidUrl\' is not a valid URL: {value}', |
| 43 | [ 'value' => $speechoidUrl ] |
| 44 | ); |
| 45 | $success = false; |
| 46 | } |
| 47 | $speechoidResponseTimeoutSeconds = $this->config |
| 48 | ->get( 'WikispeechSpeechoidResponseTimeoutSeconds' ); |
| 49 | if ( $speechoidResponseTimeoutSeconds && |
| 50 | !is_int( $speechoidResponseTimeoutSeconds ) ) { |
| 51 | $this->logger |
| 52 | ->warning( __METHOD__ . ': Configuration value ' . |
| 53 | '\'WikispeechSpeechoidResponseTimeoutSeconds\' ' . |
| 54 | 'is not a falsy or integer value.' |
| 55 | ); |
| 56 | $success = false; |
| 57 | } |
| 58 | |
| 59 | $utteranceTimeToLiveDays = $this->config |
| 60 | ->get( 'WikispeechUtteranceTimeToLiveDays' ); |
| 61 | if ( $utteranceTimeToLiveDays === null ) { |
| 62 | $this->logger |
| 63 | ->warning( __METHOD__ . ': Configuration value for ' . |
| 64 | '\'WikispeechUtteranceTimeToLiveDays\' is missing.' |
| 65 | ); |
| 66 | $success = false; |
| 67 | } |
| 68 | $utteranceTimeToLiveDays = intval( $utteranceTimeToLiveDays ); |
| 69 | if ( $utteranceTimeToLiveDays < 0 ) { |
| 70 | $this->logger |
| 71 | ->warning( __METHOD__ . ': Configuration value for ' . |
| 72 | '\'WikispeechUtteranceTimeToLiveDays\' must not be negative.' |
| 73 | ); |
| 74 | $success = false; |
| 75 | } |
| 76 | |
| 77 | $minimumMinutesBetweenFlushExpiredUtterancesJobs = $this->config |
| 78 | ->get( 'WikispeechMinimumMinutesBetweenFlushExpiredUtterancesJobs' ); |
| 79 | if ( $minimumMinutesBetweenFlushExpiredUtterancesJobs === null ) { |
| 80 | $this->logger |
| 81 | ->warning( __METHOD__ . ': Configuration value for ' . |
| 82 | '\'WikispeechMinimumMinutesBetweenFlushExpiredUtterancesJobs\' ' . |
| 83 | 'is missing.' |
| 84 | ); |
| 85 | $success = false; |
| 86 | } |
| 87 | $minimumMinutesBetweenFlushExpiredUtterancesJobs = intval( |
| 88 | $minimumMinutesBetweenFlushExpiredUtterancesJobs |
| 89 | ); |
| 90 | if ( $minimumMinutesBetweenFlushExpiredUtterancesJobs < 0 ) { |
| 91 | $this->logger |
| 92 | ->warning( __METHOD__ . ': Configuration value for ' . |
| 93 | '\'WikispeechMinimumMinutesBetweenFlushExpiredUtterancesJobs\'' . |
| 94 | ' must not be negative.' |
| 95 | ); |
| 96 | $success = false; |
| 97 | } |
| 98 | |
| 99 | $fileBackendName = $this->config->get( 'WikispeechUtteranceFileBackendName' ); |
| 100 | if ( $fileBackendName === null ) { |
| 101 | $this->logger |
| 102 | ->warning( __METHOD__ . ': Configuration value ' . |
| 103 | '\'WikispeechUtteranceFileBackendName\' is missing.' |
| 104 | ); |
| 105 | // This is not a failure. |
| 106 | // It will fall back on default, but admin should be aware. |
| 107 | } elseif ( !is_string( $fileBackendName ) ) { |
| 108 | $this->logger |
| 109 | ->warning( __METHOD__ . ': Configuration value ' . |
| 110 | '\'WikispeechUtteranceFileBackendName\' is not a string value.' |
| 111 | ); |
| 112 | $success = false; |
| 113 | } |
| 114 | |
| 115 | $fileBackendContainerName = $this->config |
| 116 | ->get( 'WikispeechUtteranceFileBackendContainerName' ); |
| 117 | if ( $fileBackendContainerName === null ) { |
| 118 | $this->logger |
| 119 | ->warning( __METHOD__ . ': Configuration value ' . |
| 120 | '\'WikispeechUtteranceFileBackendContainerName\' is missing.' |
| 121 | ); |
| 122 | $success = false; |
| 123 | } elseif ( !is_string( $fileBackendContainerName ) ) { |
| 124 | $this->logger |
| 125 | ->warning( __METHOD__ . ': Configuration value ' . |
| 126 | '\'WikispeechUtteranceFileBackendContainerName\' is not a string value.' |
| 127 | ); |
| 128 | $success = false; |
| 129 | } |
| 130 | |
| 131 | return $success; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Tests if a variable is valid as "remove tags". |
| 136 | * |
| 137 | * The variable should be an associative array. Keys should be |
| 138 | * strings and values should be booleans, strings or sequential |
| 139 | * arrays containing strings. |
| 140 | * |
| 141 | * @since 0.1.13 |
| 142 | * @param mixed $removeTags The variable to test. |
| 143 | * @return bool true if $removeTags is valid, else false. |
| 144 | */ |
| 145 | public static function isValidRemoveTags( $removeTags ) { |
| 146 | if ( !is_array( $removeTags ) ) { |
| 147 | return false; |
| 148 | } |
| 149 | foreach ( $removeTags as $tagName => $rule ) { |
| 150 | if ( !is_string( $tagName ) ) { |
| 151 | // A key isn't a string. |
| 152 | return false; |
| 153 | } |
| 154 | if ( is_array( $rule ) ) { |
| 155 | // Rule is a list of class names. |
| 156 | foreach ( $rule as $className ) { |
| 157 | if ( !is_string( $className ) ) { |
| 158 | // Only strings are valid if the rule is |
| 159 | // an array. |
| 160 | return false; |
| 161 | } |
| 162 | } |
| 163 | } elseif ( !is_bool( $rule ) && !is_string( $rule ) ) { |
| 164 | // Rule is not array, string or boolean. |
| 165 | return false; |
| 166 | } |
| 167 | } |
| 168 | return true; |
| 169 | } |
| 170 | } |