Skins: Support useskin=json

This allows rendering of the data passed to the skin rendering
layer, to allow developers to debug the information used to render
a skin.

Bug: T364696
Change-Id: I32aaa6a85d24df4f4689269f6a455823bb08196b
This commit is contained in:
Jon Robson 2024-05-23 09:00:03 -07:00 committed by Jdlrobson
parent 57731f2c90
commit c33b31728d
6 changed files with 35 additions and 3 deletions

View file

@ -2907,7 +2907,8 @@ class OutputPage extends ContextSource {
return $return ? '' : null;
}
$response = $this->getRequest()->response();
$request = $this->getRequest();
$response = $request->response();
$config = $this->getConfig();
if ( $this->mRedirect != '' ) {
@ -2957,7 +2958,6 @@ class OutputPage extends ContextSource {
# Buffer output; final headers may depend on later processing
ob_start();
$response->header( 'Content-type: ' . $config->get( MainConfigNames::MimeType ) . '; charset=UTF-8' );
$response->header( 'Content-language: ' .
MediaWikiServices::getInstance()->getContentLanguage()->getHtmlCode() );
@ -2990,6 +2990,7 @@ class OutputPage extends ContextSource {
}
if ( $this->mArticleBodyOnly ) {
$response->header( 'Content-type: ' . $config->get( MainConfigNames::MimeType ) . '; charset=UTF-8' );
if ( $this->cspOutputMode === self::CSP_HEADERS ) {
$this->CSP->sendHeaders();
}
@ -3001,6 +3002,15 @@ class OutputPage extends ContextSource {
}
$sk = $this->getSkin();
$skinOptions = $sk->getOptions();
if ( $skinOptions['format'] === 'json' ) {
$response->header( 'Content-type: application/json; charset=UTF-8' );
return json_encode( [
$this->msg( 'skin-json-warning' )->escaped() => $this->msg( 'skin-json-warning-message' )->escaped()
] + $sk->getTemplateData() );
}
$response->header( 'Content-type: ' . $config->get( MainConfigNames::MimeType ) . '; charset=UTF-8' );
$this->loadSkinModules( $sk );
MWDebug::addModules( $this );

View file

@ -2161,6 +2161,19 @@ return [
]
]
], true );
// Register a hidden skin for outputting skin json
$factory->register( 'json', 'SkinJSON', [
'class' => SkinApi::class,
'args' => [
[
'name' => 'json',
'styles' => [],
'supportsMwHeading' => true,
'format' => 'json',
'templateDirectory' => __DIR__ . '/skins/templates/apioutput',
]
]
] );
return $factory;
},

View file

@ -272,6 +272,11 @@ abstract class Skin extends ContextSource {
* - `name`: Required. Internal skin name, generally in lowercase to comply with conventions
* for interface message keys and CSS class names which embed this value.
*
* - `format`: Enable rendering of skin as json or html.
*
* Since: MW 1.43
* Default: `html`
*
* - `styles`: ResourceLoader style modules to load on all pages. Default: `[]`
*
* - `scripts`: ResourceLoader script modules to load on all pages. Default: `[]`
@ -2411,6 +2416,7 @@ abstract class Skin extends ContextSource {
'styles' => [],
'scripts' => [],
'toc' => true,
'format' => 'html',
'bodyClasses' => [],
'bodyOnly' => false,
'clientPrefEnabled' => false,

View file

@ -29,7 +29,6 @@
* @since 1.25
*/
class SkinApi extends SkinMustache {
public $skinname = 'apioutput';
/**
* Extension of class methods is discouraged.
* Developers are encouraged to improve the flexibility of SkinMustache

View file

@ -4414,6 +4414,8 @@
"skin-action-undelete": "Undelete",
"skin-action-delete": "Delete",
"skin-action-move": "Move",
"skin-json-warning": "@WARNING",
"skin-json-warning-message": "This is not an API, it is only for debugging! Do not use in gadgets or production code.",
"copyupload-allowed-domains": "",
"parsoid-stash-rate-limit-error": "Stashing failed because rate limit was exceeded. Please try again later.",
"parsoid-resource-limit-exceeded": "Parsoid resources limit exceeded: $1",

View file

@ -4676,6 +4676,8 @@
"skin-action-undelete": "Tab at top of page, in vector skin.\n{{Identical|Undelete}}",
"skin-action-delete": "Used in the Vector skin, as the name of a tab at the top of the page. See for example {{canonicalurl:Translating:MediaWiki|useskin=vector}}\n\n{{Identical|Delete}}",
"skin-action-move": "The \"Move\" action renames pages.\n\nUsed in the Vector skin, on the tabs at the top of the page. See for example {{canonicalurl:Talk:Main_Page|useskin=vector}}\n\n{{Identical|Move}}",
"skin-json-warning": "Should be prefixed with '@' character. Shown in useskin=json response.",
"skin-json-warning-message": "Shown in useskin=json response. Warns user that this is only intended for debugging.",
"copyupload-allowed-domains": "{{notranslate}}",
"parsoid-stash-rate-limit-error": "Error message returned by parsoid REST endpoints when the rate limit for stashing has been exceeded.",
"parsoid-resource-limit-exceeded": "Error message indicating that Parsoid exceeded resource limits. Parameters:\n* $1 - a non-translatable internal error message specifying the problem.",