| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Settings\Config; |
| 4 |
|
| 5 |
use JsonSchema\Constraints\Constraint; |
| 6 |
use JsonSchema\Validator; |
| 7 |
use MediaWiki\Config\Config; |
| 8 |
use MediaWiki\Settings\DynamicDefaultValues; |
| 9 |
use MediaWiki\Settings\SettingsBuilderException; |
| 10 |
use MediaWiki\Settings\Source\JsonSchemaTrait; |
| 11 |
use StatusValue; |
| 12 |
use function array_key_exists; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class ConfigSchemaAggregator implements ConfigSchema { |
| 22 |
use JsonSchemaTrait; |
| 23 |
|
| 24 |
|
| 25 |
private $schemas = []; |
| 26 |
|
| 27 |
|
| 28 |
private $defaults = []; |
| 29 |
|
| 30 |
|
| 31 |
private $dynamicDefaults = []; |
| 32 |
|
| 33 |
|
| 34 |
private $types = []; |
| 35 |
|
| 36 |
|
| 37 |
private $mergeStrategies = []; |
| 38 |
|
| 39 |
|
| 40 |
private $mergeStrategyCache; |
| 41 |
|
| 42 |
|
| 43 |
private $validator; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public function addSchema( string $key, array $schema, string $sourceName = 'unknown' ) { |
| 53 |
if ( isset( $schema['properties'] ) ) { |
| 54 |
|
| 55 |
$schema['default'] = self::getDefaultFromJsonSchema( $schema ); |
| 56 |
} |
| 57 |
|
| 58 |
$this->schemas[$key] = $schema; |
| 59 |
|
| 60 |
$this->setListValueInternal( $schema, $this->defaults, $key, 'default', $sourceName ); |
| 61 |
$this->setListValueInternal( $schema, $this->types, $key, 'type', $sourceName ); |
| 62 |
$this->setListValueInternal( $schema, $this->mergeStrategies, $key, 'mergeStrategy', $sourceName ); |
| 63 |
$this->setListValueInternal( $schema, $this->dynamicDefaults, $key, 'dynamicDefault', $sourceName ); |
| 64 |
|
| 65 |
if ( isset( $schema['mergeStrategy'] ) ) { |
| 66 |
|
| 67 |
$this->mergeStrategyCache = null; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
private function setListValueInternal( $schema, &$target, $key, $fieldName, $sourceName ) { |
| 85 |
if ( array_key_exists( $fieldName, $schema ) ) { |
| 86 |
if ( array_key_exists( $key, $target ) ) { |
| 87 |
throw new SettingsBuilderException( |
| 88 |
"Overriding $fieldName in schema for {key} from {source}", |
| 89 |
[ |
| 90 |
'source' => $sourceName, |
| 91 |
'key' => $key, |
| 92 |
] |
| 93 |
); |
| 94 |
} |
| 95 |
$target[$key] = $schema[$fieldName]; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public function addSchemaMulti( array $schemas ) { |
| 108 |
foreach ( $schemas as $key => $sch ) { |
| 109 |
$this->addSchema( $key, $sch ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
private function mergeListInternal( $values, &$target, $fieldName, $sourceName ) { |
| 126 |
$merged = array_merge( $target, $values ); |
| 127 |
if ( count( $merged ) < ( count( $target ) + count( $values ) ) ) { |
| 128 |
throw new SettingsBuilderException( 'Overriding config {field} from {source}', [ |
| 129 |
'field' => $fieldName, |
| 130 |
'source' => $sourceName, |
| 131 |
'old_values' => implode( ', ', array_intersect_key( $target, $values ) ), |
| 132 |
'new_values' => implode( ', ', array_intersect_key( $values, $target ) ), |
| 133 |
] ); |
| 134 |
} |
| 135 |
|
| 136 |
$target = $merged; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function addDefaults( array $defaults, string $sourceName = 'unknown' ) { |
| 146 |
$this->mergeListInternal( $defaults, $this->defaults, 'defaults', $sourceName ); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function addTypes( array $types, string $sourceName = 'unknown' ) { |
| 156 |
$this->mergeListInternal( $types, $this->types, 'types', $sourceName ); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public function addMergeStrategies( array $mergeStrategies, string $sourceName = 'unknown' ) { |
| 166 |
$this->mergeListInternal( |
| 167 |
$mergeStrategies, |
| 168 |
$this->mergeStrategies, |
| 169 |
'mergeStrategies', |
| 170 |
$sourceName |
| 171 |
); |
| 172 |
|
| 173 |
|
| 174 |
$this->mergeStrategyCache = null; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public function addDynamicDefaults( array $dynamicDefaults, string $sourceName = 'unknown' ) { |
| 186 |
$this->mergeListInternal( |
| 187 |
$dynamicDefaults, |
| 188 |
$this->dynamicDefaults, |
| 189 |
'dynamicDefaults', |
| 190 |
$sourceName |
| 191 |
); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public function getDefinedKeys(): array { |
| 200 |
return array_keys( |
| 201 |
array_merge( |
| 202 |
$this->schemas, |
| 203 |
$this->defaults, |
| 204 |
$this->types, |
| 205 |
$this->mergeStrategies, |
| 206 |
$this->dynamicDefaults |
| 207 |
) |
| 208 |
); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public function getSchemaFor( string $key ): array { |
| 219 |
$schema = $this->schemas[$key] ?? []; |
| 220 |
|
| 221 |
if ( isset( $this->defaults[$key] ) ) { |
| 222 |
$schema['default'] = $this->defaults[$key]; |
| 223 |
} |
| 224 |
|
| 225 |
if ( isset( $this->types[$key] ) ) { |
| 226 |
$schema['type'] = $this->types[$key]; |
| 227 |
} |
| 228 |
|
| 229 |
if ( isset( $this->mergeStrategies[$key] ) ) { |
| 230 |
$schema['mergeStrategy'] = $this->mergeStrategies[$key]; |
| 231 |
} |
| 232 |
|
| 233 |
if ( isset( $this->dynamicDefaults[$key] ) ) { |
| 234 |
$schema['dynamicDefault'] = $this->dynamicDefaults[$key]; |
| 235 |
} |
| 236 |
|
| 237 |
return $schema; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
public function hasSchemaFor( string $key ): bool { |
| 247 |
return isset( $this->schemas[ $key ] ) |
| 248 |
|| array_key_exists( $key, $this->defaults ) |
| 249 |
|| isset( $this->types[ $key ] ) |
| 250 |
|| isset( $this->mergeStrategies[ $key ] ) |
| 251 |
|| isset( $this->dynamicDefaults[ $key ] ); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public function getDefaults(): array { |
| 260 |
return $this->defaults; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public function getTypes(): array { |
| 269 |
return $this->types; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public function getMergeStrategyNames(): array { |
| 278 |
return $this->mergeStrategies; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
public function getDynamicDefaults(): array { |
| 288 |
return $this->dynamicDefaults; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public function hasDefaultFor( string $key ): bool { |
| 298 |
return array_key_exists( $key, $this->defaults ); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
public function getDefaultFor( string $key ) { |
| 309 |
return $this->defaults[$key] ?? null; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
public function getTypeFor( string $key ) { |
| 319 |
return $this->types[$key] ?? null; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
public function getDynamicDefaultDeclarationFor( string $key ): ?array { |
| 330 |
return $this->dynamicDefaults[$key] ?? null; |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
public function getMergeStrategyFor( string $key ): ?MergeStrategy { |
| 341 |
if ( $this->mergeStrategyCache === null ) { |
| 342 |
$this->initMergeStrategies(); |
| 343 |
} |
| 344 |
return $this->mergeStrategyCache[$key] ?? null; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public function getMergeStrategies() { |
| 354 |
if ( $this->mergeStrategyCache === null ) { |
| 355 |
$this->initMergeStrategies(); |
| 356 |
} |
| 357 |
return $this->mergeStrategyCache; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
private function initMergeStrategies() { |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
$strategiesByName = []; |
| 370 |
$strategiesByKey = []; |
| 371 |
|
| 372 |
|
| 373 |
$strategyNamesByKey = $this->mergeStrategies; |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
$types = array_diff_key( $this->types, $strategyNamesByKey ); |
| 378 |
foreach ( $types as $key => $type ) { |
| 379 |
$strategyNamesByKey[$key] = self::getStrategyForType( $type ); |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
foreach ( $strategyNamesByKey as $key => $strategyName ) { |
| 384 |
if ( !array_key_exists( $strategyName, $strategiesByName ) ) { |
| 385 |
$strategiesByName[$strategyName] = MergeStrategy::newFromName( $strategyName ); |
| 386 |
} |
| 387 |
$strategiesByKey[$key] = $strategiesByName[$strategyName]; |
| 388 |
} |
| 389 |
|
| 390 |
$this->mergeStrategyCache = $strategiesByKey; |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
private static function getStrategyForType( $type ) { |
| 401 |
if ( is_array( $type ) ) { |
| 402 |
if ( in_array( 'array', $type ) ) { |
| 403 |
$type = 'array'; |
| 404 |
} elseif ( in_array( 'object', $type ) ) { |
| 405 |
$type = 'object'; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
if ( $type === 'array' ) { |
| 410 |
|
| 411 |
|
| 412 |
return 'array_merge'; |
| 413 |
} elseif ( $type === 'object' ) { |
| 414 |
|
| 415 |
|
| 416 |
return 'array_plus'; |
| 417 |
} |
| 418 |
|
| 419 |
return 'replace'; |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
public function validateConfig( Config $config ): StatusValue { |
| 431 |
$result = StatusValue::newGood(); |
| 432 |
|
| 433 |
foreach ( $this->getDefinedKeys() as $key ) { |
| 434 |
|
| 435 |
if ( !$config->has( $key ) ) { |
| 436 |
$result->fatal( 'config-missing-key', $key ); |
| 437 |
continue; |
| 438 |
} |
| 439 |
|
| 440 |
$value = $config->get( $key ); |
| 441 |
$result->merge( $this->validateValue( $key, $value ) ); |
| 442 |
} |
| 443 |
return $result; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
public function validateValue( string $key, $value ): StatusValue { |
| 455 |
$status = StatusValue::newGood(); |
| 456 |
$schema = $this->getSchemaFor( $key ); |
| 457 |
|
| 458 |
if ( !$schema ) { |
| 459 |
return $status; |
| 460 |
} |
| 461 |
|
| 462 |
if ( !$this->validator ) { |
| 463 |
$this->validator = new Validator(); |
| 464 |
} |
| 465 |
|
| 466 |
$types = isset( $schema['type'] ) ? (array)$schema['type'] : []; |
| 467 |
|
| 468 |
if ( in_array( 'object', $types ) && is_array( $value ) ) { |
| 469 |
if ( $this->hasNumericKeys( $value ) ) { |
| 470 |
|
| 471 |
|
| 472 |
$status->warning( |
| 473 |
'config-invalid-key', |
| 474 |
$key, |
| 475 |
'Skipping validation of object with integer keys' |
| 476 |
); |
| 477 |
unset( $schema['type'] ); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
if ( in_array( 'integer', $types ) && is_float( $value ) ) { |
| 482 |
|
| 483 |
|
| 484 |
$intval = intval( $value ); |
| 485 |
if ( $intval == $value ) { |
| 486 |
$value = $intval; |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
$this->validator->validate( |
| 491 |
$value, |
| 492 |
$schema, |
| 493 |
Constraint::CHECK_MODE_TYPE_CAST |
| 494 |
); |
| 495 |
if ( !$this->validator->isValid() ) { |
| 496 |
foreach ( $this->validator->getErrors() as $error ) { |
| 497 |
$status->fatal( 'config-invalid-key', $key, $error['message'], var_export( $value, true ) ); |
| 498 |
} |
| 499 |
} |
| 500 |
$this->validator->reset(); |
| 501 |
return $status; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
private function hasNumericKeys( array $value ) { |
| 510 |
foreach ( $value as $key => $dummy ) { |
| 511 |
if ( is_int( $key ) ) { |
| 512 |
return true; |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
return false; |
| 517 |
} |
| 518 |
|
| 519 |
} |