MediaWiki REL1_32
SpecialGadgets.php
Go to the documentation of this file.
1<?php
13
15 public function __construct() {
16 parent::__construct( 'Gadgets', '', true );
17 }
18
23 public function execute( $par ) {
24 $parts = explode( '/', $par );
25
26 if ( count( $parts ) == 2 && $parts[0] == 'export' ) {
27 $this->showExportForm( $parts[1] );
28 } else {
29 $this->showMainForm();
30 }
31 }
32
33 private function makeAnchor( $gadgetName ) {
34 return 'gadget-' . Sanitizer::escapeId( $gadgetName, [ 'noninitial' ] );
35 }
36
40 public function showMainForm() {
41 $output = $this->getOutput();
42 $this->setHeaders();
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 $output = $this->getOutput();
232 try {
233 $g = GadgetRepo::singleton()->getGadget( $gadget );
234 } catch ( InvalidArgumentException $e ) {
235 $output->showErrorPage( 'error', 'gadgets-not-found', [ $gadget ] );
236 return;
237 }
238
239 $this->setHeaders();
240 $output->setPageTitle( $this->msg( 'gadgets-export-title' ) );
241 $output->addWikiMsg( 'gadgets-export-text', $gadget, $g->getDefinition() );
242
243 $exportList = "MediaWiki:gadget-$gadget\n";
244 foreach ( $g->getScriptsAndStyles() as $page ) {
245 $exportList .= "$page\n";
246 }
247
248 $htmlForm = HTMLForm::factory( 'ooui', [], $this->getContext() );
249 $htmlForm
250 ->addHiddenField( 'title', SpecialPage::getTitleFor( 'Export' )->getPrefixedDBKey() )
251 ->addHiddenField( 'pages', $exportList )
252 ->addHiddenField( 'wpDownload', '1' )
253 ->addHiddenField( 'templates', '1' )
254 ->setAction( $wgScript )
255 ->setMethod( 'get' )
256 ->setSubmitText( $this->msg( 'gadgets-export-download' )->text() )
257 ->prepareForm()
258 ->displayForm( false );
259 }
260
261 protected function getGroupName() {
262 return 'wiki';
263 }
264}
This list may contain false positives That usually means there is additional text with links below the first Each row contains links to the first and second as well as the first line of the second redirect text
$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)
execute( $par)
Main execution function.
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)
Wrapper around wfMessage that sets the current context.
getPageTitle( $subpage=false)
Get a self-referential title object.
getLanguage()
Shortcut to get user's language.
MediaWiki Linker LinkRenderer null $linkRenderer
either a plain
Definition hooks.txt:2105
usually copyright or history_copyright This message must be in HTML not wikitext if the section is included from a template $section
Definition hooks.txt:3107
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place $output
Definition hooks.txt:2317
returning false will NOT prevent logging $e
Definition hooks.txt:2226
if(!isset( $args[0])) $lang