MediaWiki master
BaseTemplate.php
Go to the documentation of this file.
1<?php
23
33abstract class BaseTemplate extends QuickTemplate {
34
42 public function getMsg( $name, ...$params ) {
43 return $this->getSkin()->msg( $name, ...$params );
44 }
45
46 public function msg( $str ) {
47 echo $this->getMsg( $str )->escaped();
48 }
49
53 public function getPersonalTools() {
54 return $this->getSkin()->getPersonalToolsForMakeListItem( $this->get( 'personal_urls' ) );
55 }
56
62 protected function getSidebar( $options = [] ) {
63 // Force the rendering of the following portals
64 $sidebar = $this->data['sidebar'];
65 if ( !isset( $sidebar['SEARCH'] ) ) {
66 // @phan-suppress-next-line PhanTypeMismatchDimAssignment False positive
67 $sidebar['SEARCH'] = true;
68 }
69 if ( !isset( $sidebar['TOOLBOX'] ) ) {
70 $sidebar['TOOLBOX'] = true;
71 }
72 if ( !isset( $sidebar['LANGUAGES'] ) ) {
73 $sidebar['LANGUAGES'] = true;
74 }
75
76 if ( !isset( $options['search'] ) || $options['search'] !== true ) {
77 unset( $sidebar['SEARCH'] );
78 }
79 if ( isset( $options['toolbox'] ) && $options['toolbox'] === false ) {
80 unset( $sidebar['TOOLBOX'] );
81 }
82 if ( isset( $options['languages'] ) && $options['languages'] === false ) {
83 unset( $sidebar['LANGUAGES'] );
84 }
85
86 $boxes = [];
87 foreach ( $sidebar as $boxName => $content ) {
88 if ( $content === false ) {
89 continue;
90 }
91 switch ( $boxName ) {
92 case 'SEARCH':
93 // Search is a special case, skins should custom implement this
94 $boxes[$boxName] = [
95 'id' => 'p-search',
96 'header' => $this->getMsg( 'search' )->text(),
97 'generated' => false,
98 'content' => true,
99 ];
100 break;
101 case 'TOOLBOX':
102 $msgObj = $this->getMsg( 'toolbox' );
103 $boxes[$boxName] = [
104 'id' => 'p-tb',
105 'header' => $msgObj->exists() ? $msgObj->text() : 'toolbox',
106 'generated' => false,
107 'content' => $content,
108 ];
109 break;
110 case 'LANGUAGES':
111 if ( $this->data['language_urls'] !== false ) {
112 $msgObj = $this->getMsg( 'otherlanguages' );
113 $boxes[$boxName] = [
114 'id' => 'p-lang',
115 'header' => $msgObj->exists() ? $msgObj->text() : 'otherlanguages',
116 'generated' => false,
117 'content' => $this->data['language_urls'] ?: [],
118 ];
119 }
120 break;
121 default:
122 $msgObj = $this->getMsg( $boxName );
123 $boxes[$boxName] = [
124 'id' => "p-$boxName",
125 'header' => $msgObj->exists() ? $msgObj->text() : $boxName,
126 'generated' => true,
127 'content' => $content,
128 ];
129 break;
130 }
131 }
132
133 if ( isset( $options['htmlOnly'] ) && $options['htmlOnly'] === true ) {
134 foreach ( $boxes as $boxName => $box ) {
135 if ( is_array( $box['content'] ) ) {
136 $content = '<ul>';
137 foreach ( $box['content'] as $key => $val ) {
138 $content .= "\n " . $this->getSkin()->makeListItem( $key, $val );
139 }
140 $content .= "\n</ul>\n";
141 $boxes[$boxName]['content'] = $content;
142 }
143 }
144 }
145
146 return $boxes;
147 }
148
157 protected function makeLink( $key, $item, $options = [] ) {
158 return $this->getSkin()->makeLink( $key, $item, $options );
159 }
160
169 public function makeListItem( $key, $item, $options = [] ) {
170 return $this->getSkin()->makeListItem( $key, $item, $options );
171 }
172
179 protected function makeSearchInput( $attrs = [] ) {
180 return $this->getSkin()->makeSearchInput( $attrs );
181 }
182
190 protected function makeSearchButton( $mode, $attrs = [] ) {
191 return $this->getSkin()->makeSearchButton( $mode, $attrs );
192 }
193
203 protected function getFooterLinks( $option = null ) {
204 $footerlinks = $this->get( 'footerlinks' );
205
206 // Reduce footer links down to only those which are being used
207 $validFooterLinks = [];
208 foreach ( $footerlinks as $category => $links ) {
209 $validFooterLinks[$category] = [];
210 foreach ( $links as $link ) {
211 if ( isset( $this->data[$link] ) && $this->data[$link] ) {
212 $validFooterLinks[$category][] = $link;
213 }
214 }
215 if ( count( $validFooterLinks[$category] ) <= 0 ) {
216 unset( $validFooterLinks[$category] );
217 }
218 }
219
220 if ( $option == 'flat' && count( $validFooterLinks ) ) {
221 // fold footerlinks into a single array using a bit of trickery
222 $validFooterLinks = array_merge( ...array_values( $validFooterLinks ) );
223 }
224
225 return $validFooterLinks;
226 }
227
242 protected function getFooterIcons( $option = null ) {
243 wfDeprecated( __METHOD__, '1.35' );
244 // Generate additional footer icons
245 $footericons = $this->get( 'footericons' );
246
247 if ( $option == 'icononly' ) {
248 // Unset any icons which don't have an image
249 $this->unsetIconsWithoutImages( $footericons );
250 } elseif ( $option == 'nocopyright' ) {
251 unset( $footericons['copyright'] );
252 }
253
254 return $footericons;
255 }
256
263 private function unsetIconsWithoutImages( array &$icons ) {
264 // Unset any icons which don't have an image
265 foreach ( $icons as $iconsKey => &$iconsBlock ) {
266 foreach ( $iconsBlock as $iconKey => $icon ) {
267 if ( !is_string( $icon ) && !isset( $icon['src'] ) ) {
268 unset( $iconsBlock[$iconKey] );
269 }
270 }
271 if ( $iconsBlock === [] ) {
272 unset( $icons[$iconsKey] );
273 }
274 }
275 }
276
287 protected function getFooter( $iconStyle = 'icononly', $linkStyle = 'flat' ) {
288 $validFooterIcons = $this->get( 'footericons' );
289 if ( $iconStyle === 'icononly' ) {
290 $this->unsetIconsWithoutImages( $validFooterIcons );
291 } else {
292 // take a deprecated unsupported path
293 $validFooterIcons = $this->getFooterIcons( $iconStyle );
294 }
295 $validFooterLinks = $this->getFooterLinks( $linkStyle );
296
297 $html = '';
298
299 if ( count( $validFooterIcons ) + count( $validFooterLinks ) > 0 ) {
300 $html .= Html::openElement( 'div', [
301 'id' => 'footer-bottom',
302 'class' => 'mw-footer',
303 'role' => 'contentinfo',
304 'lang' => $this->get( 'userlang' ),
305 'dir' => $this->get( 'dir' )
306 ] );
307 $footerEnd = Html::closeElement( 'div' );
308 } else {
309 $footerEnd = '';
310 }
311 foreach ( $validFooterIcons as $blockName => $footerIcons ) {
312 $html .= Html::openElement( 'div', [
313 'id' => Sanitizer::escapeIdForAttribute( "f-{$blockName}ico" ),
314 'class' => 'footer-icons'
315 ] );
316 foreach ( $footerIcons as $icon ) {
317 $html .= $this->getSkin()->makeFooterIcon( $icon );
318 }
319 $html .= Html::closeElement( 'div' );
320 }
321 if ( count( $validFooterLinks ) > 0 ) {
322 $html .= Html::openElement( 'ul', [ 'id' => 'f-list', 'class' => 'footer-places' ] );
323 foreach ( $validFooterLinks as $aLink ) {
324 $html .= Html::rawElement(
325 'li',
326 [ 'id' => Sanitizer::escapeIdForAttribute( $aLink ) ],
327 $this->get( $aLink )
328 );
329 }
330 $html .= Html::closeElement( 'ul' );
331 }
332
333 $html .= $this->getClear() . $footerEnd;
334
335 return $html;
336 }
337
344 protected function getClear() {
345 return Html::element( 'div', [ 'class' => 'visualClear' ] );
346 }
347
363 public function getIndicators() {
364 $out = "<div class=\"mw-indicators\">\n";
365 foreach ( $this->data['indicators'] as $id => $content ) {
366 $out .= Html::rawElement(
367 'div',
368 [
369 'id' => Sanitizer::escapeIdForAttribute( "mw-indicator-$id" ),
370 'class' => 'mw-indicator',
371 ],
372 $content
373 ) .
374 // Add whitespace between the <div>s because
375 // they get displayed with display: inline-block
376 "\n";
377 }
378 $out .= "</div>\n";
379 return $out;
380 }
381}
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
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
PHP-based skin template that holds data.
getSkin()
Get the Skin object related to this object.