| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Extension\Gadgets\Content; |
| 22 |
|
| 23 |
use MediaWiki\Content\Content; |
| 24 |
use MediaWiki\Content\JsonContentHandler; |
| 25 |
use MediaWiki\Content\Renderer\ContentParseParams; |
| 26 |
use MediaWiki\Content\ValidationParams; |
| 27 |
use MediaWiki\Extension\Gadgets\GadgetRepo; |
| 28 |
use MediaWiki\Extension\Gadgets\MediaWikiGadgetsJsonRepo; |
| 29 |
use MediaWiki\Json\FormatJson; |
| 30 |
use MediaWiki\Linker\Linker; |
| 31 |
use MediaWiki\Parser\ParserOutput; |
| 32 |
use MediaWiki\Title\Title; |
| 33 |
use StatusValue; |
| 34 |
|
| 35 |
class GadgetDefinitionContentHandler extends JsonContentHandler { |
| 36 |
private GadgetRepo $gadgetRepo; |
| 37 |
|
| 38 |
public function __construct( string $modelId, GadgetRepo $gadgetRepo ) { |
| 39 |
parent::__construct( $modelId ); |
| 40 |
$this->gadgetRepo = $gadgetRepo; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function canBeUsedOn( Title $title ) { |
| 48 |
return MediaWikiGadgetsJsonRepo::isGadgetDefinitionTitle( $title ); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
protected function getContentClass() { |
| 53 |
return GadgetDefinitionContent::class; |
| 54 |
} |
| 55 |
|
| 56 |
public function makeEmptyContent() { |
| 57 |
$class = $this->getContentClass(); |
| 58 |
return new $class( FormatJson::encode( $this->getEmptyDefinition(), "\t" ) ); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public function validateSave( Content $content, ValidationParams $validationParams ) { |
| 67 |
$status = parent::validateSave( $content, $validationParams ); |
| 68 |
'@phan-var GadgetDefinitionContent $content'; |
| 69 |
if ( !$status->isOK() ) { |
| 70 |
return $content->validate(); |
| 71 |
} |
| 72 |
return $status; |
| 73 |
} |
| 74 |
|
| 75 |
public function getEmptyDefinition() { |
| 76 |
return [ |
| 77 |
'settings' => [ |
| 78 |
'category' => '', |
| 79 |
], |
| 80 |
'module' => [ |
| 81 |
'pages' => [], |
| 82 |
'dependencies' => [], |
| 83 |
] |
| 84 |
]; |
| 85 |
} |
| 86 |
|
| 87 |
public function getDefaultMetadata() { |
| 88 |
return [ |
| 89 |
'settings' => [ |
| 90 |
'rights' => [], |
| 91 |
'default' => false, |
| 92 |
'package' => false, |
| 93 |
'requiresES6' => false, |
| 94 |
'hidden' => false, |
| 95 |
'skins' => [], |
| 96 |
'actions' => [], |
| 97 |
'namespaces' => [], |
| 98 |
'categories' => [], |
| 99 |
'contentModels' => [], |
| 100 |
'category' => '', |
| 101 |
'supportsUrlLoad' => false, |
| 102 |
], |
| 103 |
'module' => [ |
| 104 |
'pages' => [], |
| 105 |
'peers' => [], |
| 106 |
'dependencies' => [], |
| 107 |
'messages' => [], |
| 108 |
'type' => '', |
| 109 |
], |
| 110 |
]; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
protected function fillParserOutput( |
| 117 |
Content $content, |
| 118 |
ContentParseParams $cpoParams, |
| 119 |
ParserOutput &$parserOutput |
| 120 |
) { |
| 121 |
'@phan-var GadgetDefinitionContent $content'; |
| 122 |
|
| 123 |
$data = unserialize( serialize( $content->getData()->getValue() ) ); |
| 124 |
if ( $data !== null ) { |
| 125 |
if ( isset( $data->module->pages ) ) { |
| 126 |
foreach ( $data->module->pages as &$page ) { |
| 127 |
$title = Title::makeTitleSafe( NS_MEDIAWIKI, "Gadget-$page" ); |
| 128 |
$this->makeLink( $parserOutput, $page, $title ); |
| 129 |
} |
| 130 |
} |
| 131 |
if ( isset( $data->module->dependencies ) ) { |
| 132 |
foreach ( $data->module->dependencies as &$dep ) { |
| 133 |
if ( str_starts_with( $dep, 'ext.gadget.' ) ) { |
| 134 |
$gadgetId = explode( 'ext.gadget.', $dep )[ 1 ]; |
| 135 |
$title = $this->gadgetRepo->getGadgetDefinitionTitle( $gadgetId ); |
| 136 |
if ( $title ) { |
| 137 |
$this->makeLink( $parserOutput, $dep, $title ); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
if ( isset( $data->module->peers ) ) { |
| 143 |
foreach ( $data->module->peers as &$peer ) { |
| 144 |
$title = $this->gadgetRepo->getGadgetDefinitionTitle( $peer ); |
| 145 |
if ( $title ) { |
| 146 |
$this->makeLink( $parserOutput, $peer, $title ); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
if ( isset( $data->module->messages ) ) { |
| 151 |
foreach ( $data->module->messages as &$msg ) { |
| 152 |
$title = Title::makeTitleSafe( NS_MEDIAWIKI, $msg ); |
| 153 |
$this->makeLink( $parserOutput, $msg, $title ); |
| 154 |
} |
| 155 |
} |
| 156 |
if ( isset( $data->settings->category ) && $data->settings->category ) { |
| 157 |
$this->makeLink( |
| 158 |
$parserOutput, |
| 159 |
$data->settings->category, |
| 160 |
Title::makeTitleSafe( NS_MEDIAWIKI, "gadget-section-" . $data->settings->category ) |
| 161 |
); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
if ( !$cpoParams->getGenerateHtml() || !$content->isValid() ) { |
| 166 |
$parserOutput->setText( '' ); |
| 167 |
} else { |
| 168 |
$parserOutput->setText( $content->rootValueTable( $data ) ); |
| 169 |
$parserOutput->addModuleStyles( [ 'mediawiki.content.json' ] ); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
private function makeLink( ParserOutput $parserOutput, string &$text, ?Title $title ) { |
| 181 |
if ( $title ) { |
| 182 |
$parserOutput->addLink( $title ); |
| 183 |
$text = new GadgetDefinitionContentArmor( |
| 184 |
Linker::link( $title, htmlspecialchars( '"' . $text . '"' ) ) |
| 185 |
); |
| 186 |
} |
| 187 |
} |
| 188 |
} |