MediaWiki REL1_34
SpecialGadgets.php
Go to the documentation of this file.
1<?php
13
15 public function __construct() {
16 parent::__construct( 'Gadgets', '', true );
17 }
18
22 public function execute( $par ) {
23 $parts = explode( '/', $par );
24
25 if ( count( $parts ) == 2 && $parts[0] == 'export' ) {
26 $this->showExportForm( $parts[1] );
27 } else {
28 $this->showMainForm();
29 }
30 }
31
32 private function makeAnchor( $gadgetName ) {
33 return 'gadget-' . Sanitizer::escapeIdForAttribute( $gadgetName );
34 }
35
39 public function showMainForm() {
40 $output = $this->getOutput();
41 $this->setHeaders();
42 $this->addHelpLink( 'Extension:Gadgets' );
43 $output->setPageTitle( $this->msg( 'gadgets-title' ) );
44 $output->addWikiMsg( 'gadgets-pagetext' );
45
46 $gadgets = GadgetRepo::singleton()->getStructuredList();
47 if ( !$gadgets ) {
48 return;
49 }
50
51 $output->disallowUserJs();
52 $lang = $this->getLanguage();
53 $langSuffix = "";
54 if ( !$lang->equals( MediaWikiServices::getInstance()->getContentLanguage() ) ) {
55 $langSuffix = "/" . $lang->getCode();
56 }
57
58 $listOpen = false;
59
60 $editInterfaceMessage = $this->getUser()->isAllowed( 'editinterface' )
61 ? 'edit'
62 : 'viewsource';
63
65 foreach ( $gadgets as $section => $entries ) {
66 if ( $section !== false && $section !== '' ) {
67 $t = Title::makeTitleSafe( NS_MEDIAWIKI, "Gadget-section-$section$langSuffix" );
68 $lnkTarget = $t
69 ? $linkRenderer->makeLink( $t, $this->msg( $editInterfaceMessage )->text(),
70 [], [ 'action' => 'edit' ] )
71 : htmlspecialchars( $section );
72 $lnk = "&#160; &#160; [$lnkTarget]";
73
74 $ttext = $this->msg( "gadget-section-$section" )->parse();
75
76 if ( $listOpen ) {
77 $output->addHTML( Xml::closeElement( 'ul' ) . "\n" );
78 $listOpen = false;
79 }
80
81 $output->addHTML( Html::rawElement( 'h2', [], $ttext . $lnk ) . "\n" );
82 }
83
87 foreach ( $entries as $gadget ) {
88 $name = $gadget->getName();
89 $t = Title::makeTitleSafe( NS_MEDIAWIKI, "Gadget-{$name}$langSuffix" );
90 if ( !$t ) {
91 continue;
92 }
93
94 $links = [];
95 $links[] = $linkRenderer->makeLink(
96 $t,
97 $this->msg( $editInterfaceMessage )->text(),
98 [],
99 [ 'action' => 'edit' ]
100 );
101 $links[] = $linkRenderer->makeLink(
102 $this->getPageTitle( "export/{$name}" ),
103 $this->msg( 'gadgets-export' )->text()
104 );
105
106 $nameHtml = $this->msg( "gadget-{$name}" )->parse();
107
108 if ( !$listOpen ) {
109 $listOpen = true;
110 $output->addHTML( Html::openElement( 'ul' ) );
111 }
112
113 $actionsHtml = '&#160;&#160;' .
114 $this->msg( 'parentheses' )->rawParams( $lang->pipeList( $links ) )->escaped();
115 $output->addHTML(
116 Html::openElement( 'li', [ 'id' => $this->makeAnchor( $name ) ] ) .
117 $nameHtml . $actionsHtml
118 );
119 // Whether the next portion of the list item contents needs
120 // a line break between it and the next portion.
121 // This is set to false after lists, but true after lines of text.
122 $needLineBreakAfter = true;
123
124 // Portion: Show files, dependencies, speers
125 if ( $needLineBreakAfter ) {
126 $output->addHTML( '<br />' );
127 }
128 $output->addHTML(
129 $this->msg( 'gadgets-uses' )->escaped() .
130 $this->msg( 'colon-separator' )->escaped()
131 );
132 $lnk = [];
133 foreach ( $gadget->getPeers() as $peer ) {
134 $lnk[] = Html::element(
135 'a',
136 [ 'href' => '#' . $this->makeAnchor( $peer ) ],
137 $peer
138 );
139 }
140 foreach ( $gadget->getScriptsAndStyles() as $codePage ) {
141 $t = Title::newFromText( $codePage );
142 if ( !$t ) {
143 continue;
144 }
145 $lnk[] = $linkRenderer->makeLink( $t, $t->getText() );
146 }
147 $output->addHTML( $lang->commaList( $lnk ) );
148
149 // Portion: Legacy scripts
150 if ( $gadget->getLegacyScripts() ) {
151 if ( $needLineBreakAfter ) {
152 $output->addHTML( '<br />' );
153 }
154 $output->addHTML( Html::rawElement(
155 'span',
156 [ 'class' => 'mw-gadget-legacy errorbox' ],
157 $this->msg( 'gadgets-legacy' )->parse()
158 ) );
159 $needLineBreakAfter = true;
160 }
161
162 // Portion: Show required rights (optional)
163 $rights = [];
164 foreach ( $gadget->getRequiredRights() as $right ) {
165 $rights[] = '* ' . Html::element(
166 'code',
167 [ 'title' => $this->msg( "right-$right" )->plain() ],
168 $right
169 );
170 }
171 if ( $rights ) {
172 if ( $needLineBreakAfter ) {
173 $output->addHTML( '<br />' );
174 }
175 $output->addHTML(
176 $this->msg( 'gadgets-required-rights', implode( "\n", $rights ), count( $rights ) )->parse()
177 );
178 $needLineBreakAfter = false;
179 }
180
181 // Portion: Show required skins (optional)
182 $requiredSkins = $gadget->getRequiredSkins();
183 // $requiredSkins can be an array, or true (if all skins are supported)
184 if ( is_array( $requiredSkins ) ) {
185 $skins = [];
186 $validskins = Skin::getSkinNames();
187 foreach ( $requiredSkins as $skinid ) {
188 if ( isset( $validskins[$skinid] ) ) {
189 $skins[] = $this->msg( "skinname-$skinid" )->plain();
190 } else {
191 $skins[] = $skinid;
192 }
193 }
194 if ( $skins ) {
195 if ( $needLineBreakAfter ) {
196 $output->addHTML( '<br />' );
197 }
198 $output->addHTML(
199 $this->msg( 'gadgets-required-skins', $lang->commaList( $skins ) )
200 ->numParams( count( $skins ) )->parse()
201 );
202 $needLineBreakAfter = true;
203 }
204 }
205
206 // Portion: Show on by default (optional)
207 if ( $gadget->isOnByDefault() ) {
208 if ( $needLineBreakAfter ) {
209 $output->addHTML( '<br />' );
210 }
211 $output->addHTML( $this->msg( 'gadgets-default' )->parse() );
212 $needLineBreakAfter = true;
213 }
214
215 $output->addHTML( Html::closeElement( 'li' ) . "\n" );
216 }
217 }
218
219 if ( $listOpen ) {
220 $output->addHTML( Html::closeElement( 'ul' ) . "\n" );
221 }
222 }
223
228 public function showExportForm( $gadget ) {
229 global $wgScript;
230
231 $this->addHelpLink( 'Extension:Gadgets' );
232 $output = $this->getOutput();
233 try {
234 $g = GadgetRepo::singleton()->getGadget( $gadget );
235 } catch ( InvalidArgumentException $e ) {
236 $output->showErrorPage( 'error', 'gadgets-not-found', [ $gadget ] );
237 return;
238 }
239
240 $this->setHeaders();
241 $output->setPageTitle( $this->msg( 'gadgets-export-title' ) );
242 $output->addWikiMsg( 'gadgets-export-text', $gadget, $g->getDefinition() );
243
244 $exportList = "MediaWiki:gadget-$gadget\n";
245 foreach ( $g->getScriptsAndStyles() as $page ) {
246 $exportList .= "$page\n";
247 }
248
249 $htmlForm = HTMLForm::factory( 'ooui', [], $this->getContext() );
250 $htmlForm
251 ->addHiddenField( 'title', SpecialPage::getTitleFor( 'Export' )->getPrefixedDBKey() )
252 ->addHiddenField( 'pages', $exportList )
253 ->addHiddenField( 'wpDownload', '1' )
254 ->addHiddenField( 'templates', '1' )
255 ->setAction( $wgScript )
256 ->setMethod( 'get' )
257 ->setSubmitText( $this->msg( 'gadgets-export-download' )->text() )
258 ->prepareForm()
259 ->displayForm( false );
260 }
261
262 protected function getGroupName() {
263 return 'wiki';
264 }
265}
$wgScript
The URL path to index.php.
static singleton()
Get the configured default GadgetRepo.
MediaWikiServices is the service locator for the application scope of MediaWiki.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
makeAnchor( $gadgetName)
showExportForm( $gadget)
Exports a gadget with its dependencies in a serialized form.
showMainForm()
Displays form showing the list of installed gadgets.
Parent class for all special pages.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getUser()
Shortcut to get the User executing this instance.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getPageTitle( $subpage=false)
Get a self-referential title object.
getLanguage()
Shortcut to get user's language.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
MediaWiki Linker LinkRenderer null $linkRenderer
const NS_MEDIAWIKI
Definition Defines.php:77
if(!isset( $args[0])) $lang