Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 265 |
|
0.00% |
0 / 16 |
CRAP | |
0.00% |
0 / 1 |
| SpecialEditChecks | |
0.00% |
0 / 265 |
|
0.00% |
0 / 16 |
4160 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getGroupName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isListed | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 88 |
|
0.00% |
0 / 1 |
240 | |||
| outputSection | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| collectChecks | |
0.00% |
0 / 36 |
|
0.00% |
0 / 1 |
210 | |||
| getMatchRuleChecks | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
12 | |||
| getShowValue | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| buildTableHtml | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| buildRowHtml | |
0.00% |
0 / 40 |
|
0.00% |
0 / 1 |
56 | |||
| buildWidgetPlaceholder | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| getConfigValueFromData | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
30 | |||
| configDetails | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| matchItemDetails | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
2 | |||
| jsonTableFromObjectString | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| jsonTable | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Special page listing Edit Checks and their configuration. |
| 4 | */ |
| 5 | |
| 6 | namespace MediaWiki\Extension\VisualEditor; |
| 7 | |
| 8 | use MediaWiki\Content\JsonContent; |
| 9 | use MediaWiki\Extension\VisualEditor\EditCheck\ConfigMerger; |
| 10 | use MediaWiki\Extension\VisualEditor\EditCheck\ResourceLoaderData; |
| 11 | use MediaWiki\Html\Html; |
| 12 | use MediaWiki\Html\TocGeneratorTrait; |
| 13 | use MediaWiki\Parser\Sanitizer; |
| 14 | use MediaWiki\SpecialPage\SpecialPage; |
| 15 | use MediaWiki\Title\Title; |
| 16 | |
| 17 | class SpecialEditChecks extends SpecialPage { |
| 18 | use TocGeneratorTrait; |
| 19 | |
| 20 | private EditCheckFileParser $parser; |
| 21 | private array $baseDefaultConfig = []; |
| 22 | |
| 23 | /** |
| 24 | * @inheritDoc |
| 25 | */ |
| 26 | public function __construct() { |
| 27 | parent::__construct( 'EditChecks' ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @inheritDoc |
| 32 | */ |
| 33 | protected function getGroupName() { |
| 34 | return 'wiki'; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @inheritDoc |
| 39 | */ |
| 40 | public function isListed() { |
| 41 | return (bool)$this->getConfig()->get( 'VisualEditorEditCheck' ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @inheritDoc |
| 46 | */ |
| 47 | public function execute( $par ) { |
| 48 | $this->setHeaders(); |
| 49 | $out = $this->getOutput(); |
| 50 | if ( !$this->getConfig()->get( 'VisualEditorEditCheck' ) ) { |
| 51 | $out->addHTML( Html::element( 'p', [], $this->msg( 'editcheck-specialeditchecks-disabled' )->text() ) ); |
| 52 | return; |
| 53 | } |
| 54 | $out->addModuleStyles( [ |
| 55 | 'ext.visualEditor.editCheck.special', |
| 56 | 'mediawiki.content.json' |
| 57 | ] ); |
| 58 | $out->addModules( 'ext.visualEditor.editCheck.special.widgets' ); |
| 59 | |
| 60 | $contentLang = $this->getContext()->getLanguage()->getCode(); |
| 61 | $dir = dirname( __DIR__ ); |
| 62 | $baseDir = $dir . '/editcheck/modules/editchecks'; |
| 63 | $checksDir = $baseDir . '/checks'; |
| 64 | $abstractClasses = [ |
| 65 | 'BaseEditCheck.js', |
| 66 | 'AsyncTextCheck.js', |
| 67 | ]; |
| 68 | $onWikiConfig = ResourceLoaderData::getConfig( $this->getContext() ); |
| 69 | $this->parser = new EditCheckFileParser( $this->getContext() ); |
| 70 | $baseCheck = $this->collectChecks( $baseDir . '/BaseEditCheck.js', [], true ); |
| 71 | if ( isset( $baseCheck[0]['defaultConfig'] ) ) { |
| 72 | $baseDefaultConfig = $this->parser->tryJsonDecodeObjectString( $baseCheck[0]['defaultConfig'] ); |
| 73 | $this->baseDefaultConfig = is_array( $baseDefaultConfig ) ? $baseDefaultConfig : []; |
| 74 | } |
| 75 | |
| 76 | $out->addHtml( $this->msg( 'editcheck-specialeditchecks-info' )->parseAsBlock() ); |
| 77 | |
| 78 | $abChecks = []; |
| 79 | $unsupportedChecks = []; |
| 80 | $defaultChecks = $this->collectChecks( |
| 81 | $checksDir . '/*.js', $abstractClasses, false, true, null, $onWikiConfig |
| 82 | ); |
| 83 | $disabledChecks = $this->collectChecks( |
| 84 | $checksDir . '/*.js', $abstractClasses, false, false, false, $onWikiConfig |
| 85 | ); |
| 86 | $experimentalEnabledChecks = $this->collectChecks( |
| 87 | $checksDir . '/*.js', [], false, false, true, $onWikiConfig |
| 88 | ); |
| 89 | $abTest = $this->getConfig()->get( 'VisualEditorEditCheckABTest' ); |
| 90 | if ( $abTest !== null ) { |
| 91 | foreach ( [ &$defaultChecks, &$disabledChecks, &$experimentalEnabledChecks ] as &$checks ) { |
| 92 | foreach ( $checks as $i => $check ) { |
| 93 | // Extract AB test check |
| 94 | if ( $check['name'] === (string)$abTest ) { |
| 95 | $abChecks[] = $check; |
| 96 | unset( $checks[$i] ); |
| 97 | } |
| 98 | // Extract unsupported checks (not in allowedContentLanguages) |
| 99 | if ( $check['allowedContentLanguages'] ) { |
| 100 | if ( !in_array( $contentLang, $check['allowedContentLanguages'], true ) ) { |
| 101 | $unsupportedChecks[] = $check; |
| 102 | unset( $checks[$i] ); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | $this->outputSection( 'default-checks', $this->msg( 'editcheck-specialeditchecks-header-default' )->text() ); |
| 110 | $out->addHTML( $this->buildTableHtml( $defaultChecks, $onWikiConfig ) ); |
| 111 | |
| 112 | if ( $abChecks ) { |
| 113 | $this->outputSection( 'abtest-checks', $this->msg( 'editcheck-specialeditchecks-header-abtest' )->text() ); |
| 114 | $out->addHTML( $this->buildTableHtml( $abChecks, $onWikiConfig ) ); |
| 115 | } |
| 116 | |
| 117 | if ( $this->getConfig()->get( 'VisualEditorEnableEditCheckSuggestionsBeta' ) ) { |
| 118 | // Split beta and experimental checks based on if they are enabled by default |
| 119 | $this->addTocSection( 'betafeatures', 'editcheck-specialeditchecks-header-betafeatures' ); |
| 120 | $out->addHTML( Html::rawElement( 'h2', [ 'id' => Sanitizer::escapeIdForAttribute( 'betafeatures' ) ], |
| 121 | Html::element( 'a', [ |
| 122 | 'href' => $this->getTitleFor( 'Preferences' )->getLocalURL() . '#mw-prefsection-betafeatures', |
| 123 | ], |
| 124 | $this->msg( 'editcheck-specialeditchecks-header-betafeatures' )->text() ) ) |
| 125 | ); |
| 126 | $out->addHTML( $this->buildTableHtml( $experimentalEnabledChecks, $onWikiConfig, true ) ); |
| 127 | |
| 128 | $this->outputSection( |
| 129 | 'experimental-checks', |
| 130 | $this->msg( 'editcheck-specialeditchecks-header-experimental' )->text() |
| 131 | ); |
| 132 | $out->addHTML( $this->buildTableHtml( $disabledChecks, $onWikiConfig, true ) ); |
| 133 | } else { |
| 134 | $allExperimentalChecks = array_merge( $experimentalEnabledChecks, $disabledChecks ); |
| 135 | // Sort checks by 'name' property |
| 136 | usort( $allExperimentalChecks, static function ( $a, $b ) { |
| 137 | return strcmp( $a['name'], $b['name'] ); |
| 138 | } ); |
| 139 | $this->outputSection( |
| 140 | 'experimental-checks', |
| 141 | $this->msg( 'editcheck-specialeditchecks-header-experimental' )->text() |
| 142 | ); |
| 143 | $out->addHTML( $this->buildTableHtml( $allExperimentalChecks, $onWikiConfig, true ) ); |
| 144 | } |
| 145 | |
| 146 | if ( $unsupportedChecks ) { |
| 147 | $this->outputSection( |
| 148 | 'unsupported-checks', |
| 149 | $this->msg( 'editcheck-specialeditchecks-header-unsupported' )->text() |
| 150 | ); |
| 151 | $out->addHTML( $this->buildTableHtml( $unsupportedChecks, $onWikiConfig ) ); |
| 152 | } |
| 153 | |
| 154 | if ( isset( $baseCheck[0]['defaultConfig'] ) ) { |
| 155 | $this->outputSection( 'base-check', $this->msg( 'editcheck-specialeditchecks-header-base' )->text() ); |
| 156 | $out->addHTML( $this->configDetails( |
| 157 | $this->jsonTableFromObjectString( $baseCheck[ 0 ]['defaultConfig'] ), |
| 158 | isset( $onWikiConfig['*'] ) ? $this->jsonTable( $onWikiConfig['*'] ) : '' |
| 159 | ) ); |
| 160 | } |
| 161 | |
| 162 | $out->addTOCPlaceholder( $this->getTocData() ); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Output a section header and add it to the TOC. |
| 167 | * |
| 168 | * @param string $id Section ID |
| 169 | * @param string $label Section label |
| 170 | */ |
| 171 | private function outputSection( string $id, string $label ): void { |
| 172 | $out = $this->getOutput(); |
| 173 | $out->addHTML( Html::element( 'h2', [ 'id' => Sanitizer::escapeIdForAttribute( $id ) ], $label ) ); |
| 174 | $this->addTocSection( $id, 'rawmessage', $label ); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Collect edit checks from the given directory. |
| 179 | * |
| 180 | * @param string $glob Glob pattern for files |
| 181 | * @param array $excludeFiles List of filenames to exclude |
| 182 | * @param bool $includeAbstract Whether to include abstract classes |
| 183 | * @param bool|null $showAsCheck If a boolean, only checks whose 'showAsCheck' value matches this |
| 184 | * @param bool|null $showAsSuggestion If a boolean, only checks whose 'showAsSuggestion' value matches this |
| 185 | * @return array List of edit checks with metadata |
| 186 | */ |
| 187 | private function collectChecks( |
| 188 | string $glob, array $excludeFiles = [], bool $includeAbstract = false, |
| 189 | ?bool $showAsCheck = null, ?bool $showAsSuggestion = null, array $onWikiConfig = [] |
| 190 | ): array { |
| 191 | $checks = []; |
| 192 | $files = glob( $glob ) ?: []; |
| 193 | foreach ( $files as $file ) { |
| 194 | if ( in_array( basename( $file ), $excludeFiles, true ) ) { |
| 195 | continue; |
| 196 | } |
| 197 | $src = file_get_contents( $file ); |
| 198 | if ( $src === false ) { |
| 199 | continue; |
| 200 | } |
| 201 | |
| 202 | $name = $this->parser->extractStaticValue( $src, 'name' ); |
| 203 | |
| 204 | // Skip abstract classes (those without a name) |
| 205 | if ( !$includeAbstract && $name === '' ) { |
| 206 | continue; |
| 207 | } |
| 208 | |
| 209 | $checks[] = [ |
| 210 | 'file' => $file, |
| 211 | 'name' => $name, |
| 212 | 'allowedContentLanguages' => $this->parser->extractStaticValue( $src, 'allowedContentLanguages' ), |
| 213 | 'defaultConfig' => $this->parser->extractDefaultConfig( $src ), |
| 214 | ]; |
| 215 | } |
| 216 | usort( $checks, static function ( $a, $b ) { |
| 217 | return strcmp( basename( $a['file'] ), basename( $b['file'] ) ); |
| 218 | } ); |
| 219 | |
| 220 | $filteredChecks = []; |
| 221 | foreach ( $checks as $checkData ) { |
| 222 | // Each textMatch rule has its own config, so it can be in a different section from textMatch |
| 223 | $entries = [ $checkData ]; |
| 224 | if ( $checkData['name'] === 'textMatch' ) { |
| 225 | array_push( $entries, ...$this->getMatchRuleChecks( $checkData, $onWikiConfig ) ); |
| 226 | } |
| 227 | foreach ( $entries as $entry ) { |
| 228 | if ( $showAsCheck !== null && |
| 229 | $this->getShowValue( $entry, $onWikiConfig, 'showAsCheck' ) !== $showAsCheck |
| 230 | ) { |
| 231 | continue; |
| 232 | } |
| 233 | if ( $showAsSuggestion !== null && |
| 234 | $this->getShowValue( $entry, $onWikiConfig, 'showAsSuggestion' ) !== $showAsSuggestion |
| 235 | ) { |
| 236 | continue; |
| 237 | } |
| 238 | $filteredChecks[] = $entry; |
| 239 | } |
| 240 | } |
| 241 | return $filteredChecks; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Get check data for each matchRule of the textMatch check. |
| 246 | * |
| 247 | * @param array $checkData textMatch check data |
| 248 | * @param array $onWikiConfig On-wiki configuration overrides |
| 249 | * @return array List of matchRule check data |
| 250 | */ |
| 251 | private function getMatchRuleChecks( array $checkData, array $onWikiConfig ): array { |
| 252 | $matchRules = $this->getConfigValueFromData( $checkData, $onWikiConfig, 'matchRules' ) |
| 253 | // In T424678 we renamed matchItems to matchRules, but allow 'matchItems' |
| 254 | // for backwards compatibility temporarily |
| 255 | ?? $this->getConfigValueFromData( $checkData, $onWikiConfig, 'matchItems' ) |
| 256 | ?? []; |
| 257 | $ruleChecks = []; |
| 258 | foreach ( $matchRules as $name => $item ) { |
| 259 | $importTitle = null; |
| 260 | if ( isset( $item['import'] ) ) { |
| 261 | $importTitle = Title::newFromText( $item['import'] ); |
| 262 | $item = json_decode( $this->msg( $importTitle->getText() )->inContentLanguage()->text(), true ); |
| 263 | } |
| 264 | $ruleChecks[] = [ |
| 265 | 'file' => '', |
| 266 | // TextMatchEditCheckAction#getConfigAnchor links to this name. Keep the format the same. |
| 267 | 'name' => $checkData['name'] . " ($name)", |
| 268 | 'matchRuleId' => (string)$name, |
| 269 | 'parent' => $checkData, |
| 270 | 'allowedContentLanguages' => '', |
| 271 | 'defaultConfig' => json_encode( $item['config'] ?? '' ), |
| 272 | 'matchItem' => $item, |
| 273 | 'importTitle' => $importTitle, |
| 274 | ]; |
| 275 | } |
| 276 | return $ruleChecks; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Get whether a check shows as a check or as a suggestion. |
| 281 | * |
| 282 | * @param array $checkData Check data |
| 283 | * @param array $onWikiConfig On-wiki configuration overrides |
| 284 | * @param string $key 'showAsCheck' or 'showAsSuggestion' |
| 285 | * @return bool |
| 286 | */ |
| 287 | private function getShowValue( array $checkData, array $onWikiConfig, string $key ): bool { |
| 288 | if ( isset( $checkData['parent'] ) ) { |
| 289 | // A matchRule shows only if textMatch and the matchRule both permit it. |
| 290 | // Keep these defaults the same as TextMatchRule.static.defaultConfig. |
| 291 | $ruleDefaults = [ 'showAsCheck' => false, 'showAsSuggestion' => true ]; |
| 292 | return $this->getShowValue( $checkData['parent'], $onWikiConfig, $key ) && |
| 293 | (bool)( $checkData['matchItem']['config'][$key] ?? $ruleDefaults[$key] ); |
| 294 | } |
| 295 | return (bool)( $this->getConfigValueFromData( $checkData, $onWikiConfig, $key ) ?? true ); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Build HTML table listing the given edit checks. |
| 300 | * |
| 301 | * @param array $checks List of edit checks |
| 302 | * @param array $onWikiConfig On-wiki configuration overrides |
| 303 | * @param bool $suggestions |
| 304 | * @return string |
| 305 | */ |
| 306 | private function buildTableHtml( |
| 307 | array $checks, array $onWikiConfig, bool $suggestions = false |
| 308 | ): string { |
| 309 | if ( !$checks ) { |
| 310 | return Html::element( 'p', [], $this->msg( 'table_pager_empty' )->text() ); |
| 311 | } |
| 312 | $html = Html::openElement( 'table', [ 'class' => 'wikitable mw-editchecks' ] ); |
| 313 | $html .= Html::rawElement( 'tr', [ 'class' => 'mw-editchecks-header' ], |
| 314 | Html::element( 'th', [ 'class' => 'mw-editchecks-name' ], |
| 315 | $this->msg( 'editcheck-specialeditchecks-col-name' )->text() ) . |
| 316 | Html::element( 'th', [ 'class' => 'mw-editchecks-appearance' ], |
| 317 | $this->msg( 'editcheck-specialeditchecks-col-appearance' )->text() ) . |
| 318 | Html::element( 'th', [ 'class' => 'mw-editchecks-config' ], |
| 319 | $this->msg( 'editcheck-specialeditchecks-config-summary' )->text() ) |
| 320 | ); |
| 321 | foreach ( $checks as $checkData ) { |
| 322 | $html .= $this->buildRowHtml( $checkData, $onWikiConfig, $suggestions ); |
| 323 | } |
| 324 | $html .= Html::closeElement( 'table' ); |
| 325 | return $html; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Build HTML for a single edit check row. |
| 330 | * |
| 331 | * @param array $checkData Edit check data |
| 332 | * @param array $onWikiConfig On-wiki configuration overrides |
| 333 | * @param bool $suggestions |
| 334 | * @return string Row HTML or empty string if filtered out |
| 335 | */ |
| 336 | private function buildRowHtml( |
| 337 | array $checkData, array $onWikiConfig, bool $suggestions = false |
| 338 | ): string { |
| 339 | $html = ''; |
| 340 | $override = ''; |
| 341 | if ( isset( $onWikiConfig[$checkData['name']] ) ) { |
| 342 | $override = $this->jsonTable( $onWikiConfig[$checkData['name']] ); |
| 343 | } |
| 344 | $defaultConfig = ''; |
| 345 | if ( $checkData['defaultConfig'] ) { |
| 346 | $defaultConfig = $this->jsonTableFromObjectString( $checkData['defaultConfig'] ); |
| 347 | } |
| 348 | |
| 349 | $widget = $this->buildWidgetPlaceholder( $checkData, $suggestions ); |
| 350 | $this->addTocSubSection( $checkData['name'], 'rawmessage', $checkData['name'] ); |
| 351 | |
| 352 | $html .= Html::rawElement( 'tr', [ |
| 353 | 'id' => Sanitizer::escapeIdForAttribute( $checkData['name'] ) |
| 354 | ], |
| 355 | Html::rawElement( 'td', [], |
| 356 | Html::element( 'strong', [], $checkData['name'] ) . |
| 357 | Html::element( 'div', [], basename( $checkData['file'] ) ) |
| 358 | ) . |
| 359 | Html::rawElement( 'td', [], $widget ) . |
| 360 | Html::rawElement( 'td', [], |
| 361 | ( isset( $checkData['importTitle'] ) ? |
| 362 | Html::rawElement( 'p', [], |
| 363 | $this->msg( 'editcheck-specialeditchecks-imported-configs' ) |
| 364 | ->rawParams( |
| 365 | Html::element( |
| 366 | 'a', |
| 367 | [ 'href' => $checkData['importTitle']->getLocalURL() ], |
| 368 | $checkData['importTitle']->getPrefixedText() |
| 369 | ) |
| 370 | ) |
| 371 | ->parse() |
| 372 | ) : '' |
| 373 | ) . |
| 374 | ( $defaultConfig !== '' || $override !== '' ? |
| 375 | $this->configDetails( $defaultConfig, $override ) : '' |
| 376 | ) . |
| 377 | ( !empty( $checkData['matchItem'] ) ? |
| 378 | $this->matchItemDetails( $checkData['matchItem'] ) : '' |
| 379 | ) |
| 380 | ) |
| 381 | ); |
| 382 | return $html; |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Build the element into which the client shows the check cards. |
| 387 | * |
| 388 | * The client uses the editor code for the cards, so they look the same as in the editor. |
| 389 | * |
| 390 | * @param array $checkData Edit check data |
| 391 | * @param bool $suggestion |
| 392 | * @return string |
| 393 | */ |
| 394 | private function buildWidgetPlaceholder( array $checkData, bool $suggestion ): string { |
| 395 | $matchRuleId = $checkData['matchRuleId'] ?? null; |
| 396 | return Html::element( 'div', [ |
| 397 | 'class' => 've-ui-editCheckDialog mw-editchecks-widget', |
| 398 | 'data-check' => $matchRuleId === null ? $checkData['name'] : 'textMatch', |
| 399 | 'data-match-rule' => $matchRuleId, |
| 400 | 'data-suggestion' => $suggestion ? '1' : null, |
| 401 | ] ); |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Get a configuration value for a given check, merged in the same sequence as the client. |
| 406 | * |
| 407 | * @param array $checkData Check metadata |
| 408 | * @param array $onWikiConfig On-wiki configuration overrides |
| 409 | * @param string $key Configuration key to retrieve |
| 410 | * @return mixed|null Value, or null if not found |
| 411 | */ |
| 412 | private function getConfigValueFromData( array $checkData, array $onWikiConfig, string $key ) { |
| 413 | $defaultConfig = $checkData['defaultConfig'] !== '' ? |
| 414 | $this->parser->tryJsonDecodeObjectString( $checkData['defaultConfig'] ) : |
| 415 | null; |
| 416 | $sharedConfig = $onWikiConfig['*'] ?? null; |
| 417 | $checkConfig = $onWikiConfig[$checkData['name']] ?? null; |
| 418 | $config = ConfigMerger::merge( |
| 419 | $this->baseDefaultConfig, |
| 420 | is_array( $defaultConfig ) ? $defaultConfig : [], |
| 421 | is_array( $sharedConfig ) ? $sharedConfig : [], |
| 422 | is_array( $checkConfig ) ? $checkConfig : [] |
| 423 | ); |
| 424 | return $config[$key] ?? null; |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Build the details element showing default and on-wiki configuration. |
| 429 | * |
| 430 | * @param string $defaultConfig Default configuration display |
| 431 | * @param string $override On-wiki override display |
| 432 | * @return string |
| 433 | */ |
| 434 | private function configDetails( string $defaultConfig, string $override ): string { |
| 435 | return ( $defaultConfig !== '' ? |
| 436 | Html::element( 'strong', [ 'class' => 'mw-editchecks-config-header' ], |
| 437 | $this->msg( 'editcheck-specialeditchecks-config-default' )->text() ) . |
| 438 | $defaultConfig |
| 439 | : '' ) . |
| 440 | ( $override !== '' ? |
| 441 | Html::rawElement( 'details', [], |
| 442 | Html::rawElement( 'summary', [], |
| 443 | Html::element( 'strong', [ 'class' => 'mw-editchecks-config-header' ], |
| 444 | $this->msg( 'editcheck-specialeditchecks-config-onwiki' )->text() ) |
| 445 | ) . |
| 446 | $override |
| 447 | ) |
| 448 | : '' |
| 449 | ); |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Build the details element showing a textMatch matchItem configuration. |
| 454 | * |
| 455 | * @param array $matchItem Match item data |
| 456 | * @return string |
| 457 | */ |
| 458 | private function matchItemDetails( array $matchItem ): string { |
| 459 | // Skip already displayed fields |
| 460 | $matchItemFiltered = array_filter( |
| 461 | $matchItem, |
| 462 | static function ( $key ) { |
| 463 | return !in_array( $key, [ 'config', 'title', 'message', 'prompt', 'footer' ], true ); |
| 464 | }, |
| 465 | ARRAY_FILTER_USE_KEY |
| 466 | ); |
| 467 | |
| 468 | return Html::rawElement( 'details', [], |
| 469 | Html::rawElement( 'summary', [], |
| 470 | Html::element( 'strong', [ 'class' => 'mw-editchecks-config-header' ], |
| 471 | $this->msg( 'editcheck-specialeditchecks-config-matchitem' )->text() ) |
| 472 | ) . |
| 473 | $this->jsonTable( $matchItemFiltered ) |
| 474 | ); |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Attempt to convert a JS object literal to valid JSON for display. |
| 479 | * Returns pretty-printed JSON string or null on failure. |
| 480 | * |
| 481 | * @param string $js JS object literal |
| 482 | * @return string |
| 483 | */ |
| 484 | private function jsonTableFromObjectString( string $js ): string { |
| 485 | $data = $this->parser->tryJsonDecodeObjectString( $js ); |
| 486 | |
| 487 | return $data === null ? $js : $this->jsonTable( $data ); |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Format a JSON value as a table. |
| 492 | * |
| 493 | * @param mixed $value |
| 494 | * @return string |
| 495 | */ |
| 496 | private function jsonTable( $value ): string { |
| 497 | $json = json_encode( $value ); |
| 498 | $content = new JsonContent( $json ); |
| 499 | return $content->rootValueTable( $content->getData()->getValue() ); |
| 500 | } |
| 501 | } |