|
| 1 | +<?php |
| 2 | + |
| 3 | +declare( strict_types = 1 ); |
| 4 | + |
| 5 | +namespace Maps; |
| 6 | + |
| 7 | +/** |
| 8 | + * Resolves the effective Leaflet configuration by combining the PHP settings with the |
| 9 | + * MediaWiki:Maps config page, with the wiki page taking precedence: |
| 10 | + * |
| 11 | + * * Layer definitions and availability maps are merged per name, the wiki value winning on |
| 12 | + * collision. |
| 13 | + * * The default layer and overlay selections are replaced wholesale when the wiki page sets them. |
| 14 | + * |
| 15 | + * The wiki page is read lazily on first use and the result is memoized for the request. When wiki |
| 16 | + * config is disabled, or the page is missing or unreadable, the PHP settings are used unchanged. |
| 17 | + * |
| 18 | + * @licence GNU GPL v2+ |
| 19 | + */ |
| 20 | +class CombiningLeafletConfigLookup implements LeafletConfigLookup { |
| 21 | + |
| 22 | + private ?LeafletConfig $config = null; |
| 23 | + |
| 24 | + /** |
| 25 | + * @param array{ |
| 26 | + * layerDefinitions: array, |
| 27 | + * defaultLayers: string[], |
| 28 | + * defaultOverlays: string[], |
| 29 | + * availableLayers: array<string, bool>, |
| 30 | + * availableOverlays: array<string, bool> |
| 31 | + * } $phpConfig |
| 32 | + */ |
| 33 | + public function __construct( |
| 34 | + private array $phpConfig, |
| 35 | + private LeafletConfigSource $wikiConfigSource, |
| 36 | + private bool $wikiConfigEnabled |
| 37 | + ) { |
| 38 | + } |
| 39 | + |
| 40 | + public function getConfig(): LeafletConfig { |
| 41 | + $this->config ??= $this->buildConfig(); |
| 42 | + |
| 43 | + return $this->config; |
| 44 | + } |
| 45 | + |
| 46 | + private function buildConfig(): LeafletConfig { |
| 47 | + $raw = $this->phpConfig; |
| 48 | + |
| 49 | + if ( $this->wikiConfigEnabled ) { |
| 50 | + $wikiConfig = $this->wikiConfigSource->getLeafletConfig(); |
| 51 | + |
| 52 | + if ( $wikiConfig !== null ) { |
| 53 | + $raw = $this->combine( $this->phpConfig, $wikiConfig ); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return new LeafletConfig( |
| 58 | + new LeafletLayerDefinitions( $raw['layerDefinitions'] ), |
| 59 | + $raw['defaultLayers'], |
| 60 | + $raw['defaultOverlays'], |
| 61 | + $raw['availableLayers'], |
| 62 | + $raw['availableOverlays'] |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + private function combine( array $php, array $wiki ): array { |
| 67 | + return [ |
| 68 | + 'layerDefinitions' => $this->mergeByName( $php['layerDefinitions'], $wiki['layerDefinitions'] ?? null ), |
| 69 | + 'defaultLayers' => $this->stringList( $wiki['defaultLayers'] ?? null ) ?? $php['defaultLayers'], |
| 70 | + 'defaultOverlays' => $this->stringList( $wiki['defaultOverlays'] ?? null ) ?? $php['defaultOverlays'], |
| 71 | + 'availableLayers' => $this->mergeAvailability( $php['availableLayers'], $wiki['availableLayers'] ?? null ), |
| 72 | + 'availableOverlays' => $this->mergeAvailability( $php['availableOverlays'], $wiki['availableOverlays'] ?? null ), |
| 73 | + ]; |
| 74 | + } |
| 75 | + |
| 76 | + private function mergeByName( array $php, mixed $wiki ): array { |
| 77 | + if ( !is_array( $wiki ) ) { |
| 78 | + return $php; |
| 79 | + } |
| 80 | + |
| 81 | + // Union rather than array_merge: array_merge renumbers integer-like keys, which would drop |
| 82 | + // a custom layer whose name is purely numeric (e.g. "1904"). Wiki entries go on the left so |
| 83 | + // they take precedence over a same-named PHP entry. |
| 84 | + return $wiki + $php; |
| 85 | + } |
| 86 | + |
| 87 | + private function mergeAvailability( array $php, mixed $wiki ): array { |
| 88 | + if ( !is_array( $wiki ) ) { |
| 89 | + return $php; |
| 90 | + } |
| 91 | + |
| 92 | + $coerced = []; |
| 93 | + |
| 94 | + foreach ( $wiki as $name => $enabled ) { |
| 95 | + $coerced[$name] = (bool)$enabled; |
| 96 | + } |
| 97 | + |
| 98 | + return $coerced + $php; |
| 99 | + } |
| 100 | + |
| 101 | + private function stringList( mixed $value ): ?array { |
| 102 | + if ( !is_array( $value ) ) { |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + foreach ( $value as $item ) { |
| 107 | + if ( !is_string( $item ) ) { |
| 108 | + return null; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return array_values( $value ); |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments