MediaWiki master
BaseTemplate.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Skin;
8
14
24abstract class BaseTemplate extends QuickTemplate {
25
35 public function getMsg( $name, ...$params ) {
36 return $this->getSkin()->msg( $name, ...$params );
37 }
38
45 public function msg( $str ) {
46 echo $this->getMsg( $str )->escaped();
47 }
48
53 public function getPersonalTools() {
54 wfDeprecated( __METHOD__, 'Call $this->getSkin()->getPersonalToolsForMakeListItem instead (T422975)', '1.46' );
55 return $this->getSkin()->getPersonalToolsForMakeListItem( $this->get( 'personal_urls' ) );
56 }
57
63 protected function getSidebar( $options = [] ) {
64 // Force the rendering of the following portals
65 $sidebar = $this->data['sidebar'];
66 if ( !isset( $sidebar['SEARCH'] ) ) {
67 // @phan-suppress-next-line PhanTypeMismatchDimAssignment False positive
68 $sidebar['SEARCH'] = true;
69 }
70 if ( !isset( $sidebar['TOOLBOX'] ) ) {
71 $sidebar['TOOLBOX'] = true;
72 }
73 if ( !isset( $sidebar['LANGUAGES'] ) ) {
74 $sidebar['LANGUAGES'] = true;
75 }
76
77 if ( !isset( $options['search'] ) || $options['search'] !== true ) {
78 unset( $sidebar['SEARCH'] );
79 }
80 if ( isset( $options['toolbox'] ) && $options['toolbox'] === false ) {
81 unset( $sidebar['TOOLBOX'] );
82 }
83 if ( isset( $options['languages'] ) && $options['languages'] === false ) {
84 unset( $sidebar['LANGUAGES'] );
85 }
86
87 $boxes = [];
88 foreach ( $sidebar as $boxName => $content ) {
89 if ( $content === false ) {
90 continue;
91 }
92 switch ( $boxName ) {
93 case 'SEARCH':
94 // Search is a special case, skins should custom implement this
95 $boxes[$boxName] = [
96 'id' => 'p-search',
97 'header' => $this->getMsg( 'search' )->text(),
98 'generated' => false,
99 'content' => true,
100 ];
101 break;
102 case 'TOOLBOX':
103 $msgObj = $this->getMsg( 'toolbox' );
104 $boxes[$boxName] = [
105 'id' => 'p-tb',
106 'header' => $msgObj->exists() ? $msgObj->text() : 'toolbox',
107 'generated' => false,
108 'content' => $content,
109 ];
110 break;
111 case 'LANGUAGES':
112 if ( $this->data['language_urls'] !== false ) {
113 $msgObj = $this->getMsg( 'otherlanguages' );
114 $boxes[$boxName] = [
115 'id' => 'p-lang',
116 'header' => $msgObj->exists() ? $msgObj->text() : 'otherlanguages',
117 'generated' => false,
118 'content' => $this->data['language_urls'] ?: [],
119 ];
120 }
121 break;
122 default:
123 $msgObj = $this->getMsg( $boxName );
124 $boxes[$boxName] = [
125 'id' => "p-$boxName",
126 'header' => $msgObj->exists() ? $msgObj->text() : $boxName,
127 'generated' => true,
128 'content' => $content,
129 ];
130 break;
131 }
132 }
133
134 if ( isset( $options['htmlOnly'] ) && $options['htmlOnly'] === true ) {
135 foreach ( $boxes as $boxName => $box ) {
136 if ( is_array( $box['content'] ) ) {
137 $content = '<ul>';
138 foreach ( $box['content'] as $key => $val ) {
139 $content .= "\n " . $this->getSkin()->makeListItem( $key, $val );
140 }
141 $content .= "\n</ul>\n";
142 $boxes[$boxName]['content'] = $content;
143 }
144 }
145 }
146
147 return $boxes;
148 }
149
158 protected function makeLink( $key, $item, $options = [] ) {
159 return $this->getSkin()->makeLink( $key, $item, $options );
160 }
161
170 public function makeListItem( $key, $item, $options = [] ) {
171 return $this->getSkin()->makeListItem( $key, $item, $options );
172 }
173
180 protected function makeSearchInput( $attrs = [] ) {
181 return $this->getSkin()->makeSearchInput( $attrs );
182 }
183
191 protected function makeSearchButton( $mode, $attrs = [] ) {
192 return $this->getSkin()->makeSearchButton( $mode, $attrs );
193 }
194
204 protected function getFooterLinks( $option = null ) {
205 $footerlinks = $this->get( 'footerlinks' );
206
207 // Reduce footer links down to only those which are being used
208 $validFooterLinks = [];
209 foreach ( $footerlinks as $category => $links ) {
210 $validFooterLinks[$category] = [];
211 foreach ( $links as $link ) {
212 if ( isset( $this->data[$link] ) && $this->data[$link] ) {
213 $validFooterLinks[$category][] = $link;
214 }
215 }
216 if ( count( $validFooterLinks[$category] ) <= 0 ) {
217 unset( $validFooterLinks[$category] );
218 }
219 }
220
221 if ( $option == 'flat' && count( $validFooterLinks ) ) {
222 // fold footerlinks into a single array using a bit of trickery
223 $validFooterLinks = array_merge( ...array_values( $validFooterLinks ) );
224 }
225
226 return $validFooterLinks;
227 }
228
233 private function unsetIconsWithoutImages( array &$icons ) {
234 // Unset any icons which don't have an image
235 foreach ( $icons as $iconsKey => &$iconsBlock ) {
236 foreach ( $iconsBlock as $iconKey => $icon ) {
237 if ( !is_string( $icon ) && !isset( $icon['src'] ) ) {
238 unset( $iconsBlock[$iconKey] );
239 }
240 }
241 if ( $iconsBlock === [] ) {
242 unset( $icons[$iconsKey] );
243 }
244 }
245 }
246
257 protected function getFooter( $iconStyle = 'icononly', $linkStyle = 'flat' ) {
258 $validFooterIcons = $this->get( 'footericons' );
259 if ( $iconStyle === 'icononly' ) {
260 $this->unsetIconsWithoutImages( $validFooterIcons );
261 }
262 $validFooterLinks = $this->getFooterLinks( $linkStyle );
263
264 $html = '';
265
266 if ( count( $validFooterIcons ) + count( $validFooterLinks ) > 0 ) {
267 $html .= Html::openElement( 'div', [
268 'id' => 'footer-bottom',
269 'class' => 'mw-footer',
270 'role' => 'contentinfo',
271 'lang' => $this->get( 'userlang' ),
272 'dir' => $this->get( 'dir' )
273 ] );
274 $footerEnd = Html::closeElement( 'div' );
275 } else {
276 $footerEnd = '';
277 }
278 foreach ( $validFooterIcons as $blockName => $footerIcons ) {
279 $html .= Html::openElement( 'div', [
280 'id' => Sanitizer::escapeIdForAttribute( "f-{$blockName}ico" ),
281 'class' => 'footer-icons'
282 ] );
283 foreach ( $footerIcons as $icon ) {
284 $html .= $this->getSkin()->makeFooterIcon( $icon );
285 }
286 $html .= Html::closeElement( 'div' );
287 }
288 if ( count( $validFooterLinks ) > 0 ) {
289 $html .= Html::openElement( 'ul', [ 'id' => 'f-list', 'class' => 'footer-places' ] );
290 foreach ( $validFooterLinks as $aLink ) {
291 $html .= Html::rawElement(
292 'li',
293 [ 'id' => Sanitizer::escapeIdForAttribute( $aLink ) ],
294 $this->get( $aLink )
295 );
296 }
297 $html .= Html::closeElement( 'ul' );
298 }
299
300 $html .= $this->getClear() . $footerEnd;
301
302 return $html;
303 }
304
311 protected function getClear() {
312 return Html::element( 'div', [ 'class' => 'visualClear' ] );
313 }
314
330 public function getIndicators() {
331 $out = "<div class=\"mw-indicators\">\n";
332 foreach ( $this->data['indicators'] as $id => $content ) {
333 $out .= Html::rawElement(
334 'div',
335 [
336 'id' => Sanitizer::escapeIdForAttribute( "mw-indicator-$id" ),
337 'class' => 'mw-indicator',
338 ],
339 $content
340 ) .
341 // Add whitespace between the <div>s because
342 // they get displayed with display: inline-block
343 "\n";
344 }
345 $out .= "</div>\n";
346 return $out;
347 }
348}
349
351class_alias( BaseTemplate::class, 'BaseTemplate' );
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
This class is a collection of static functions that serve two purposes:
Definition Html.php:44
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:144
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:34
Extended QuickTemplate with additional MediaWiki-specific helper methods.
makeSearchInput( $attrs=[])
Wrapper for Skin method.
getIndicators()
Get the suggested HTML for page status indicators: icons (or short text snippets) usually displayed i...
getClear()
Get a div with the core visualClear class, for clearing floats.
makeLink( $key, $item, $options=[])
Wrapper for Skin method.
makeListItem( $key, $item, $options=[])
Wrapper for Skin method.
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.
getFooter( $iconStyle='icononly', $linkStyle='flat')
Renderer for getFooterLinks.
getMsg( $name,... $params)
Get a Message object with its context set.
PHP-based skin template that holds data.
getSkin()
Get the Skin object related to this object.
Value object representing a message parameter with one of the types from {.
element(SerializerNode $parent, SerializerNode $node, $contents)