MediaWiki master
BaseTemplate.php
Go to the documentation of this file.
1<?php
24
34abstract class BaseTemplate extends QuickTemplate {
35
43 public function getMsg( $name, ...$params ) {
44 return $this->getSkin()->msg( $name, ...$params );
45 }
46
47 public function msg( $str ) {
48 echo $this->getMsg( $str )->escaped();
49 }
50
54 public function getPersonalTools() {
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
243 protected function getFooterIcons( $option = null ) {
244 wfDeprecated( __METHOD__, '1.35' );
245 // Generate additional footer icons
246 $footericons = $this->get( 'footericons' );
247
248 if ( $option == 'icononly' ) {
249 // Unset any icons which don't have an image
250 $this->unsetIconsWithoutImages( $footericons );
251 } elseif ( $option == 'nocopyright' ) {
252 unset( $footericons['copyright'] );
253 }
254
255 return $footericons;
256 }
257
264 private function unsetIconsWithoutImages( array &$icons ) {
265 // Unset any icons which don't have an image
266 foreach ( $icons as $iconsKey => &$iconsBlock ) {
267 foreach ( $iconsBlock as $iconKey => $icon ) {
268 if ( !is_string( $icon ) && !isset( $icon['src'] ) ) {
269 unset( $iconsBlock[$iconKey] );
270 }
271 }
272 if ( $iconsBlock === [] ) {
273 unset( $icons[$iconsKey] );
274 }
275 }
276 }
277
288 protected function getFooter( $iconStyle = 'icononly', $linkStyle = 'flat' ) {
289 $validFooterIcons = $this->get( 'footericons' );
290 if ( $iconStyle === 'icononly' ) {
291 $this->unsetIconsWithoutImages( $validFooterIcons );
292 } else {
293 // take a deprecated unsupported path
294 $validFooterIcons = $this->getFooterIcons( $iconStyle );
295 }
296 $validFooterLinks = $this->getFooterLinks( $linkStyle );
297
298 $html = '';
299
300 if ( count( $validFooterIcons ) + count( $validFooterLinks ) > 0 ) {
301 $html .= Html::openElement( 'div', [
302 'id' => 'footer-bottom',
303 'class' => 'mw-footer',
304 'role' => 'contentinfo',
305 'lang' => $this->get( 'userlang' ),
306 'dir' => $this->get( 'dir' )
307 ] );
308 $footerEnd = Html::closeElement( 'div' );
309 } else {
310 $footerEnd = '';
311 }
312 foreach ( $validFooterIcons as $blockName => $footerIcons ) {
313 $html .= Html::openElement( 'div', [
314 'id' => Sanitizer::escapeIdForAttribute( "f-{$blockName}ico" ),
315 'class' => 'footer-icons'
316 ] );
317 foreach ( $footerIcons as $icon ) {
318 $html .= $this->getSkin()->makeFooterIcon( $icon );
319 }
320 $html .= Html::closeElement( 'div' );
321 }
322 if ( count( $validFooterLinks ) > 0 ) {
323 $html .= Html::openElement( 'ul', [ 'id' => 'f-list', 'class' => 'footer-places' ] );
324 foreach ( $validFooterLinks as $aLink ) {
325 $html .= Html::rawElement(
326 'li',
327 [ 'id' => Sanitizer::escapeIdForAttribute( $aLink ) ],
328 $this->get( $aLink )
329 );
330 }
331 $html .= Html::closeElement( 'ul' );
332 }
333
334 $html .= $this->getClear() . $footerEnd;
335
336 return $html;
337 }
338
345 protected function getClear() {
346 return Html::element( 'div', [ 'class' => 'visualClear' ] );
347 }
348
364 public function getIndicators() {
365 $out = "<div class=\"mw-indicators\">\n";
366 foreach ( $this->data['indicators'] as $id => $content ) {
367 $out .= Html::rawElement(
368 'div',
369 [
370 'id' => Sanitizer::escapeIdForAttribute( "mw-indicator-$id" ),
371 'class' => 'mw-indicator',
372 ],
373 $content
374 ) .
375 // Add whitespace between the <div>s because
376 // they get displayed with display: inline-block
377 "\n";
378 }
379 $out .= "</div>\n";
380 return $out;
381 }
382}
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:158
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
PHP-based skin template that holds data.
getSkin()
Get the Skin object related to this object.