gadgetRepo = $gadgetRepo; } /** * @param Title $title * @return bool */ public function canBeUsedOn( Title $title ) { return MediaWikiGadgetsJsonRepo::isGadgetDefinitionTitle( $title ); } /** @inheritDoc */ protected function getContentClass() { return GadgetDefinitionContent::class; } public function makeEmptyContent() { $class = $this->getContentClass(); return new $class( FormatJson::encode( $this->getEmptyDefinition(), "\t" ) ); } /** * @param Content $content * @param ValidationParams $validationParams * @return StatusValue */ public function validateSave( Content $content, ValidationParams $validationParams ) { $status = parent::validateSave( $content, $validationParams ); '@phan-var GadgetDefinitionContent $content'; if ( !$status->isOK() ) { return $content->validate(); } return $status; } public function getEmptyDefinition() { return [ 'settings' => [ 'category' => '', ], 'module' => [ 'pages' => [], 'dependencies' => [], ] ]; } public function getDefaultMetadata() { return [ 'settings' => [ 'rights' => [], 'default' => false, 'package' => false, 'requiresES6' => false, 'hidden' => false, 'skins' => [], 'actions' => [], 'namespaces' => [], 'categories' => [], 'contentModels' => [], 'category' => '', 'supportsUrlLoad' => false, ], 'module' => [ 'pages' => [], 'peers' => [], 'dependencies' => [], 'messages' => [], 'type' => '', ], ]; } /** * @inheritDoc */ protected function fillParserOutput( Content $content, ContentParseParams $cpoParams, ParserOutput &$parserOutput ) { '@phan-var GadgetDefinitionContent $content'; // Create a deep clone. FIXME: unserialize(serialize()) is hacky. $data = unserialize( serialize( $content->getData()->getValue() ) ); if ( $data !== null ) { if ( isset( $data->module->pages ) ) { foreach ( $data->module->pages as &$page ) { $title = Title::makeTitleSafe( NS_MEDIAWIKI, "Gadget-$page" ); $this->makeLink( $parserOutput, $page, $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 = $this->gadgetRepo->getGadgetDefinitionTitle( $gadgetId ); if ( $title ) { $this->makeLink( $parserOutput, $dep, $title ); } } } } if ( isset( $data->module->peers ) ) { foreach ( $data->module->peers as &$peer ) { $title = $this->gadgetRepo->getGadgetDefinitionTitle( $peer ); if ( $title ) { $this->makeLink( $parserOutput, $peer, $title ); } } } if ( isset( $data->module->messages ) ) { foreach ( $data->module->messages as &$msg ) { $title = Title::makeTitleSafe( NS_MEDIAWIKI, $msg ); $this->makeLink( $parserOutput, $msg, $title ); } } if ( isset( $data->settings->category ) && $data->settings->category ) { $this->makeLink( $parserOutput, $data->settings->category, Title::makeTitleSafe( NS_MEDIAWIKI, "gadget-section-" . $data->settings->category ) ); } } if ( !$cpoParams->getGenerateHtml() || !$content->isValid() ) { $parserOutput->setText( '' ); } else { $parserOutput->setText( $content->rootValueTable( $data ) ); $parserOutput->addModuleStyles( [ 'mediawiki.content.json' ] ); } } /** * Create a link on the page * @param ParserOutput $parserOutput * @param string &$text The text to link * @param Title|null $title Link target title * @return void */ private function makeLink( ParserOutput $parserOutput, string &$text, ?Title $title ) { if ( $title ) { $parserOutput->addLink( $title ); $text = new GadgetDefinitionContentArmor( Linker::link( $title, htmlspecialchars( '"' . $text . '"' ) ) ); } } }