MediaWiki master
BaseTemplate.php
Go to the documentation of this file.
1<?php
26
36abstract class BaseTemplate extends QuickTemplate {
37
47 public function getMsg( $name, ...$params ) {
48 return $this->getSkin()->msg( $name, ...$params );
49 }
50
51 public function msg( $str ) {
52 echo $this->getMsg( $str )->escaped();
53 }
54
58 public function getPersonalTools() {
59 return $this->getSkin()->getPersonalToolsForMakeListItem( $this->get( 'personal_urls' ) );
60 }
61
67 protected function getSidebar( $options = [] ) {
68 // Force the rendering of the following portals
69 $sidebar = $this->data['sidebar'];
70 if ( !isset( $sidebar['SEARCH'] ) ) {
71 // @phan-suppress-next-line PhanTypeMismatchDimAssignment False positive
72 $sidebar['SEARCH'] = true;
73 }
74 if ( !isset( $sidebar['TOOLBOX'] ) ) {
75 $sidebar['TOOLBOX'] = true;
76 }
77 if ( !isset( $sidebar['LANGUAGES'] ) ) {
78 $sidebar['LANGUAGES'] = true;
79 }
80
81 if ( !isset( $options['search'] ) || $options['search'] !== true ) {
82 unset( $sidebar['SEARCH'] );
83 }
84 if ( isset( $options['toolbox'] ) && $options['toolbox'] === false ) {
85 unset( $sidebar['TOOLBOX'] );
86 }
87 if ( isset( $options['languages'] ) && $options['languages'] === false ) {
88 unset( $sidebar['LANGUAGES'] );
89 }
90
91 $boxes = [];
92 foreach ( $sidebar as $boxName => $content ) {
93 if ( $content === false ) {
94 continue;
95 }
96 switch ( $boxName ) {
97 case 'SEARCH':
98 // Search is a special case, skins should custom implement this
99 $boxes[$boxName] = [
100 'id' => 'p-search',
101 'header' => $this->getMsg( 'search' )->text(),
102 'generated' => false,
103 'content' => true,
104 ];
105 break;
106 case 'TOOLBOX':
107 $msgObj = $this->getMsg( 'toolbox' );
108 $boxes[$boxName] = [
109 'id' => 'p-tb',
110 'header' => $msgObj->exists() ? $msgObj->text() : 'toolbox',
111 'generated' => false,
112 'content' => $content,
113 ];
114 break;
115 case 'LANGUAGES':
116 if ( $this->data['language_urls'] !== false ) {
117 $msgObj = $this->getMsg( 'otherlanguages' );
118 $boxes[$boxName] = [
119 'id' => 'p-lang',
120 'header' => $msgObj->exists() ? $msgObj->text() : 'otherlanguages',
121 'generated' => false,
122 'content' => $this->data['language_urls'] ?: [],
123 ];
124 }
125 break;
126 default:
127 $msgObj = $this->getMsg( $boxName );
128 $boxes[$boxName] = [
129 'id' => "p-$boxName",
130 'header' => $msgObj->exists() ? $msgObj->text() : $boxName,
131 'generated' => true,
132 'content' => $content,
133 ];
134 break;
135 }
136 }
137
138 if ( isset( $options['htmlOnly'] ) && $options['htmlOnly'] === true ) {
139 foreach ( $boxes as $boxName => $box ) {
140 if ( is_array( $box['content'] ) ) {
141 $content = '<ul>';
142 foreach ( $box['content'] as $key => $val ) {
143 $content .= "\n " . $this->getSkin()->makeListItem( $key, $val );
144 }
145 $content .= "\n</ul>\n";
146 $boxes[$boxName]['content'] = $content;
147 }
148 }
149 }
150
151 return $boxes;
152 }
153
162 protected function makeLink( $key, $item, $options = [] ) {
163 return $this->getSkin()->makeLink( $key, $item, $options );
164 }
165
174 public function makeListItem( $key, $item, $options = [] ) {
175 return $this->getSkin()->makeListItem( $key, $item, $options );
176 }
177
184 protected function makeSearchInput( $attrs = [] ) {
185 return $this->getSkin()->makeSearchInput( $attrs );
186 }
187
195 protected function makeSearchButton( $mode, $attrs = [] ) {
196 return $this->getSkin()->makeSearchButton( $mode, $attrs );
197 }
198
208 protected function getFooterLinks( $option = null ) {
209 $footerlinks = $this->get( 'footerlinks' );
210
211 // Reduce footer links down to only those which are being used
212 $validFooterLinks = [];
213 foreach ( $footerlinks as $category => $links ) {
214 $validFooterLinks[$category] = [];
215 foreach ( $links as $link ) {
216 if ( isset( $this->data[$link] ) && $this->data[$link] ) {
217 $validFooterLinks[$category][] = $link;
218 }
219 }
220 if ( count( $validFooterLinks[$category] ) <= 0 ) {
221 unset( $validFooterLinks[$category] );
222 }
223 }
224
225 if ( $option == 'flat' && count( $validFooterLinks ) ) {
226 // fold footerlinks into a single array using a bit of trickery
227 $validFooterLinks = array_merge( ...array_values( $validFooterLinks ) );
228 }
229
230 return $validFooterLinks;
231 }
232
247 protected function getFooterIcons( $option = null ) {
248 wfDeprecated( __METHOD__, '1.35' );
249 // Generate additional footer icons
250 $footericons = $this->get( 'footericons' );
251
252 if ( $option == 'icononly' ) {
253 // Unset any icons which don't have an image
254 $this->unsetIconsWithoutImages( $footericons );
255 } elseif ( $option == 'nocopyright' ) {
256 unset( $footericons['copyright'] );
257 }
258
259 return $footericons;
260 }
261
266 private function unsetIconsWithoutImages( array &$icons ) {
267 // Unset any icons which don't have an image
268 foreach ( $icons as $iconsKey => &$iconsBlock ) {
269 foreach ( $iconsBlock as $iconKey => $icon ) {
270 if ( !is_string( $icon ) && !isset( $icon['src'] ) ) {
271 unset( $iconsBlock[$iconKey] );
272 }
273 }
274 if ( $iconsBlock === [] ) {
275 unset( $icons[$iconsKey] );
276 }
277 }
278 }
279
290 protected function getFooter( $iconStyle = 'icononly', $linkStyle = 'flat' ) {
291 $validFooterIcons = $this->get( 'footericons' );
292 if ( $iconStyle === 'icononly' ) {
293 $this->unsetIconsWithoutImages( $validFooterIcons );
294 } else {
295 // take a deprecated unsupported path
296 $validFooterIcons = $this->getFooterIcons( $iconStyle );
297 }
298 $validFooterLinks = $this->getFooterLinks( $linkStyle );
299
300 $html = '';
301
302 if ( count( $validFooterIcons ) + count( $validFooterLinks ) > 0 ) {
303 $html .= Html::openElement( 'div', [
304 'id' => 'footer-bottom',
305 'class' => 'mw-footer',
306 'role' => 'contentinfo',
307 'lang' => $this->get( 'userlang' ),
308 'dir' => $this->get( 'dir' )
309 ] );
310 $footerEnd = Html::closeElement( 'div' );
311 } else {
312 $footerEnd = '';
313 }
314 foreach ( $validFooterIcons as $blockName => $footerIcons ) {
315 $html .= Html::openElement( 'div', [
316 'id' => Sanitizer::escapeIdForAttribute( "f-{$blockName}ico" ),
317 'class' => 'footer-icons'
318 ] );
319 foreach ( $footerIcons as $icon ) {
320 $html .= $this->getSkin()->makeFooterIcon( $icon );
321 }
322 $html .= Html::closeElement( 'div' );
323 }
324 if ( count( $validFooterLinks ) > 0 ) {
325 $html .= Html::openElement( 'ul', [ 'id' => 'f-list', 'class' => 'footer-places' ] );
326 foreach ( $validFooterLinks as $aLink ) {
327 $html .= Html::rawElement(
328 'li',
329 [ 'id' => Sanitizer::escapeIdForAttribute( $aLink ) ],
330 $this->get( $aLink )
331 );
332 }
333 $html .= Html::closeElement( 'ul' );
334 }
335
336 $html .= $this->getClear() . $footerEnd;
337
338 return $html;
339 }
340
347 protected function getClear() {
348 return Html::element( 'div', [ 'class' => 'visualClear' ] );
349 }
350
366 public function getIndicators() {
367 $out = "<div class=\"mw-indicators\">\n";
368 foreach ( $this->data['indicators'] as $id => $content ) {
369 $out .= Html::rawElement(
370 'div',
371 [
372 'id' => Sanitizer::escapeIdForAttribute( "mw-indicator-$id" ),
373 'class' => 'mw-indicator',
374 ],
375 $content
376 ) .
377 // Add whitespace between the <div>s because
378 // they get displayed with display: inline-block
379 "\n";
380 }
381 $out .= "</div>\n";
382 return $out;
383 }
384}
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
array $params
The job parameters.
Extended QuickTemplate with additional MediaWiki-specific helper methods.
getFooterLinks( $option=null)
Returns an array of footerlinks trimmed down to only those footer links that are valid.
makeSearchButton( $mode, $attrs=[])
Wrapper for Skin method.
makeLink( $key, $item, $options=[])
Wrapper for Skin method.
getSidebar( $options=[])
getFooterIcons( $option=null)
Returns an array of footer icons filtered down by options relevant to how the skin wishes to display ...
makeListItem( $key, $item, $options=[])
Wrapper for Skin method.
getMsg( $name,... $params)
Get a Message object with its context set.
getIndicators()
Get the suggested HTML for page status indicators: icons (or short text snippets) usually displayed i...
makeSearchInput( $attrs=[])
Wrapper for Skin method.
getFooter( $iconStyle='icononly', $linkStyle='flat')
Renderer for getFooterIcons and getFooterLinks.
getClear()
Get a div with the core visualClear class, for clearing floats.
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:155
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
PHP-based skin template that holds data.
getSkin()
Get the Skin object related to this object.
Value object representing a message parameter that consists of a list of values.