diff --git a/includes/Content/GadgetDefinitionContent.php b/includes/Content/GadgetDefinitionContent.php index 1f792401..24d02b3f 100644 --- a/includes/Content/GadgetDefinitionContent.php +++ b/includes/Content/GadgetDefinitionContent.php @@ -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; } diff --git a/includes/Content/GadgetDefinitionContentHandler.php b/includes/Content/GadgetDefinitionContentHandler.php index 7c4263c2..e4dd3851 100644 --- a/includes/Content/GadgetDefinitionContentHandler.php +++ b/includes/Content/GadgetDefinitionContentHandler.php @@ -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, diff --git a/includes/GadgetDefinitionNamespaceRepo.php b/includes/GadgetDefinitionNamespaceRepo.php index a919c74a..1ca694ba 100644 --- a/includes/GadgetDefinitionNamespaceRepo.php +++ b/includes/GadgetDefinitionNamespaceRepo.php @@ -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 ], diff --git a/tests/phpunit/integration/GadgetDefinitionContentHandlerTest.php b/tests/phpunit/integration/GadgetDefinitionContentHandlerTest.php new file mode 100644 index 00000000..fdb38f61 --- /dev/null +++ b/tests/phpunit/integration/GadgetDefinitionContentHandlerTest.php @@ -0,0 +1,25 @@ +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 ); + } +}