Merge "Avoid persisting default gadget configs in definitions"

This commit is contained in:
jenkins-bot 2024-01-09 17:27:16 +00:00 committed by Gerrit Code Review
commit cc4efec689
4 changed files with 62 additions and 16 deletions

View file

@ -63,7 +63,7 @@ class GadgetDefinitionContent extends JsonContent {
$this->validation = $this->getData();
} else {
$validator = new GadgetDefinitionValidator();
$this->validation = $validator->validate( $this->getAssocArray() );
$this->validation = $validator->validate( $this->getAssocArray(), true );
}
}
return $this->validation;
@ -80,7 +80,7 @@ class GadgetDefinitionContent extends JsonContent {
$info = wfObjectToArray( $this->getData()->getValue() );
/** @var GadgetDefinitionContentHandler $handler */
$handler = $this->getContentHandler();
$info = wfArrayPlus2d( $info, $handler->getDefaultMetadata() );
$info = wfArrayPlus2d( $info, $handler->getEmptyDefinition() );
return $info;
}

View file

@ -48,7 +48,19 @@ class GadgetDefinitionContentHandler extends JsonContentHandler {
public function makeEmptyContent() {
$class = $this->getContentClass();
return new $class( FormatJson::encode( $this->getDefaultMetadata(), "\t" ) );
return new $class( FormatJson::encode( $this->getEmptyDefinition(), "\t" ) );
}
public function getEmptyDefinition() {
return [
'settings' => [
'category' => '',
],
'module' => [
'pages' => [],
'dependencies' => [],
]
];
}
public function getDefaultMetadata() {
@ -95,22 +107,28 @@ class GadgetDefinitionContentHandler extends JsonContentHandler {
$this->makeLink( $parserOutput, $page, $title );
}
}
foreach ( $data->module->dependencies as &$dep ) {
if ( str_starts_with( $dep, 'ext.gadget.' ) ) {
$gadgetId = explode( 'ext.gadget.', $dep )[1];
$title = Title::makeTitleSafe( NS_GADGET_DEFINITION, $gadgetId );
$this->makeLink( $parserOutput, $dep, $title );
if ( isset( $data->module->dependencies ) ) {
foreach ( $data->module->dependencies as &$dep ) {
if ( str_starts_with( $dep, 'ext.gadget.' ) ) {
$gadgetId = explode( 'ext.gadget.', $dep )[ 1 ];
$title = Title::makeTitleSafe( NS_GADGET_DEFINITION, $gadgetId );
$this->makeLink( $parserOutput, $dep, $title );
}
}
}
foreach ( $data->module->peers as &$peer ) {
$title = Title::makeTitleSafe( NS_GADGET_DEFINITION, $peer );
$this->makeLink( $parserOutput, $peer, $title );
if ( isset( $data->module->peers ) ) {
foreach ( $data->module->peers as &$peer ) {
$title = Title::makeTitleSafe( NS_GADGET_DEFINITION, $peer );
$this->makeLink( $parserOutput, $peer, $title );
}
}
foreach ( $data->module->messages as &$msg ) {
$title = Title::makeTitleSafe( NS_MEDIAWIKI, $msg );
$this->makeLink( $parserOutput, $msg, $title );
if ( isset( $data->module->messages ) ) {
foreach ( $data->module->messages as &$msg ) {
$title = Title::makeTitleSafe( NS_MEDIAWIKI, $msg );
$this->makeLink( $parserOutput, $msg, $title );
}
}
if ( $data->settings->category ) {
if ( isset( $data->settings->category ) && $data->settings->category ) {
$this->makeLink(
$parserOutput,
$data->settings->category,

View file

@ -129,7 +129,10 @@ class GadgetDefinitionNamespaceRepo extends GadgetRepo {
return null;
}
return Gadget::serializeDefinition( $id, $content->getAssocArray() );
$handler = $content->getContentHandler();
'@phan-var \MediaWiki\Extension\Gadgets\Content\GadgetDefinitionContentHandler $handler';
$data = wfArrayPlus2d( $content->getAssocArray(), $handler->getDefaultMetadata() );
return Gadget::serializeDefinition( $id, $data );
},
[
'checkKeys' => [ $key ],

View file

@ -0,0 +1,25 @@
<?php
namespace phpunit\integration;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\SlotRecord;
use MediaWikiIntegrationTestCase;
/**
* @covers \MediaWiki\Extension\Gadgets\Content\GadgetDefinitionContentHandler
* @covers \MediaWiki\Extension\Gadgets\Content\GadgetDefinitionContent
* @group Gadgets
* @group Database
*/
class GadgetDefinitionContentHandlerTest extends MediaWikiIntegrationTestCase {
public function testHandler() {
$status = $this->editPage( 'Gadget definition:X1', '{}' );
/** @var RevisionRecord $rev */
$rev = $status->getValue()['revision-record'];
$revText = $rev->getContent( SlotRecord::MAIN )->serialize();
$handler = $this->getServiceContainer()->getContentHandlerFactory()->getContentHandler( 'GadgetDefinition' );
$this->assertEquals( $handler->makeEmptyContent()->serialize(), $revText );
}
}