Debian Codegraph @

extensions/Gadgets/includes/Content/GadgetDefinitionContentHandler.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
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 * @param Title $title
45 * @return bool
46 */
47 public function canBeUsedOn( Title $title ) {
48 return MediaWikiGadgetsJsonRepo::isGadgetDefinitionTitle( $title );
49 }
50
51 /** @inheritDoc */
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 * @param Content $content
63 * @param ValidationParams $validationParams
64 * @return StatusValue
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 * @inheritDoc
115 */
116 protected function fillParserOutput(
117 Content $content,
118 ContentParseParams $cpoParams,
119 ParserOutput &$parserOutput
120 ) {
121 '@phan-var GadgetDefinitionContent $content';
122 // Create a deep clone. FIXME: unserialize(serialize()) is hacky.
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 * Create a link on the page
175 * @param ParserOutput $parserOutput
176 * @param string &$text The text to link
177 * @param Title|null $title Link target title
178 * @return void
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 }