|
| 1 | +<?php |
| 2 | + |
| 3 | +declare( strict_types = 1 ); |
| 4 | + |
| 5 | +namespace Maps\Config; |
| 6 | + |
| 7 | +use MediaWiki\Html\Html; |
| 8 | +use MessageLocalizer; |
| 9 | + |
| 10 | +/** |
| 11 | + * Renders the on-page configuration reference for the MediaWiki:Maps config page from the config |
| 12 | + * schema, so it can never drift from the settings that are actually exposed. Per group it renders a |
| 13 | + * table of page key, the value shape accepted there, and the LocalSettings.php setting it overrides. |
| 14 | + * The value shapes come from each type's own describe(); the setting name is the anchor into the |
| 15 | + * external documentation, so no per-setting prose is duplicated here. |
| 16 | + */ |
| 17 | +class ConfigDocumentationBuilder { |
| 18 | + |
| 19 | + public const ANCHOR = 'maps-config-reference'; |
| 20 | + |
| 21 | + private const DOCUMENTATION_URL = 'https://maps.extension.wiki/wiki/Configuration'; |
| 22 | + |
| 23 | + public function __construct( |
| 24 | + private ConfigSchema $schema, |
| 25 | + private MessageLocalizer $messageLocalizer |
| 26 | + ) { |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * A one-line pointer to the on-page reference and the external documentation. Rendered to HTML |
| 31 | + * so it can be placed directly in the edit form and the view output, neither of which parses |
| 32 | + * wikitext. |
| 33 | + */ |
| 34 | + public function buildPointer(): string { |
| 35 | + return Html::rawElement( |
| 36 | + 'div', |
| 37 | + [ 'class' => 'maps-config-docs-pointer' ], |
| 38 | + $this->messageLocalizer->msg( 'maps-config-docs-pointer', self::ANCHOR, self::DOCUMENTATION_URL )->parse() |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + public function buildReference(): string { |
| 43 | + $sections = ''; |
| 44 | + |
| 45 | + foreach ( $this->groupedSettings() as $group => $settings ) { |
| 46 | + $sections .= $this->renderGroup( (string)$group, $settings ); |
| 47 | + } |
| 48 | + |
| 49 | + return Html::rawElement( |
| 50 | + 'div', |
| 51 | + [ 'class' => 'maps-config-docs' ], |
| 52 | + Html::element( |
| 53 | + 'h2', |
| 54 | + [ 'id' => self::ANCHOR ], |
| 55 | + $this->messageLocalizer->msg( 'maps-config-docs-heading' )->text() |
| 56 | + ) . $sections |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @return array<string, ConfigSetting[]> Group name to its settings, in schema order. |
| 62 | + */ |
| 63 | + private function groupedSettings(): array { |
| 64 | + $groups = []; |
| 65 | + |
| 66 | + foreach ( $this->schema->getSettings() as $setting ) { |
| 67 | + $groups[$setting->group][] = $setting; |
| 68 | + } |
| 69 | + |
| 70 | + return $groups; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param ConfigSetting[] $settings |
| 75 | + */ |
| 76 | + private function renderGroup( string $group, array $settings ): string { |
| 77 | + return Html::rawElement( 'h3', [], Html::element( 'code', [], $group ) ) |
| 78 | + . $this->renderTable( $settings ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @param ConfigSetting[] $settings |
| 83 | + */ |
| 84 | + private function renderTable( array $settings ): string { |
| 85 | + $rows = $this->renderHeaderRow(); |
| 86 | + |
| 87 | + foreach ( $settings as $setting ) { |
| 88 | + $rows .= $this->renderRow( $setting ); |
| 89 | + } |
| 90 | + |
| 91 | + return Html::rawElement( 'table', [ 'class' => 'wikitable' ], $rows ); |
| 92 | + } |
| 93 | + |
| 94 | + private function renderHeaderRow(): string { |
| 95 | + return Html::rawElement( |
| 96 | + 'tr', |
| 97 | + [], |
| 98 | + Html::element( 'th', [], $this->messageLocalizer->msg( 'maps-config-docs-column-key' )->text() ) |
| 99 | + . Html::element( 'th', [], $this->messageLocalizer->msg( 'maps-config-docs-column-type' )->text() ) |
| 100 | + . Html::element( 'th', [], $this->messageLocalizer->msg( 'maps-config-docs-column-setting' )->text() ) |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + private function renderRow( ConfigSetting $setting ): string { |
| 105 | + return Html::rawElement( |
| 106 | + 'tr', |
| 107 | + [], |
| 108 | + Html::rawElement( 'td', [], Html::element( 'code', [], $setting->key ) ) |
| 109 | + . Html::element( 'td', [], $this->describeType( $setting->type ) ) |
| 110 | + . Html::rawElement( 'td', [], Html::element( 'code', [], '$' . $setting->settingName ) ) |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + private function describeType( ConfigType $type ): string { |
| 115 | + return $this->messageLocalizer->msg( ...$type->describe() )->text(); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments