MediaWiki  master
BaseTemplate.php
Go to the documentation of this file.
1 <?php
21 use Wikimedia\WrappedString;
22 use Wikimedia\WrappedStringList;
23 
33 abstract 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 
61  public function getToolbox() {
62  wfDeprecated( __METHOD__, '1.35' );
63 
64  $toolbox = $this->getSkin()->makeToolbox(
65  $this->data['nav_urls'],
66  $this->data['feeds']
67  );
68 
69  // Merge content that might be added to the toolbox section by hook
70  if ( isset( $this->data['sidebar']['TOOLBOX'] ) ) {
71  $toolbox = array_merge( $toolbox, $this->data['sidebar']['TOOLBOX'] ?? [] );
72  }
73 
74  return $toolbox;
75  }
76 
80  public function getPersonalTools() {
81  return $this->getSkin()->getPersonalToolsForMakeListItem( $this->get( 'personal_urls' ) );
82  }
83 
89  protected function getSidebar( $options = [] ) {
90  // Force the rendering of the following portals
91  $sidebar = $this->data['sidebar'];
92  if ( !isset( $sidebar['SEARCH'] ) ) {
93  $sidebar['SEARCH'] = true;
94  }
95  if ( !isset( $sidebar['TOOLBOX'] ) ) {
96  $sidebar['TOOLBOX'] = true;
97  }
98  if ( !isset( $sidebar['LANGUAGES'] ) ) {
99  $sidebar['LANGUAGES'] = true;
100  }
101 
102  if ( !isset( $options['search'] ) || $options['search'] !== true ) {
103  unset( $sidebar['SEARCH'] );
104  }
105  if ( isset( $options['toolbox'] ) && $options['toolbox'] === false ) {
106  unset( $sidebar['TOOLBOX'] );
107  }
108  if ( isset( $options['languages'] ) && $options['languages'] === false ) {
109  unset( $sidebar['LANGUAGES'] );
110  }
111 
112  $boxes = [];
113  foreach ( $sidebar as $boxName => $content ) {
114  if ( $content === false ) {
115  continue;
116  }
117  switch ( $boxName ) {
118  case 'SEARCH':
119  // Search is a special case, skins should custom implement this
120  $boxes[$boxName] = [
121  'id' => 'p-search',
122  'header' => $this->getMsg( 'search' )->text(),
123  'generated' => false,
124  'content' => true,
125  ];
126  break;
127  case 'TOOLBOX':
128  $msgObj = $this->getMsg( 'toolbox' );
129  $boxes[$boxName] = [
130  'id' => 'p-tb',
131  'header' => $msgObj->exists() ? $msgObj->text() : 'toolbox',
132  'generated' => false,
133  'content' => $content,
134  ];
135  break;
136  case 'LANGUAGES':
137  if ( $this->data['language_urls'] !== false ) {
138  $msgObj = $this->getMsg( 'otherlanguages' );
139  $boxes[$boxName] = [
140  'id' => 'p-lang',
141  'header' => $msgObj->exists() ? $msgObj->text() : 'otherlanguages',
142  'generated' => false,
143  'content' => $this->data['language_urls'] ?: [],
144  ];
145  }
146  break;
147  default:
148  $msgObj = $this->getMsg( $boxName );
149  $boxes[$boxName] = [
150  'id' => "p-$boxName",
151  'header' => $msgObj->exists() ? $msgObj->text() : $boxName,
152  'generated' => true,
153  'content' => $content,
154  ];
155  break;
156  }
157  }
158 
159  if ( isset( $options['htmlOnly'] ) && $options['htmlOnly'] === true ) {
160  foreach ( $boxes as $boxName => $box ) {
161  if ( is_array( $box['content'] ) ) {
162  $content = '<ul>';
163  foreach ( $box['content'] as $key => $val ) {
164  $content .= "\n " . $this->getSkin()->makeListItem( $key, $val );
165  }
166  $content .= "\n</ul>\n";
167  $boxes[$boxName]['content'] = $content;
168  }
169  }
170  }
171 
172  return $boxes;
173  }
174 
179  protected function renderAfterPortlet( $name ) {
180  wfDeprecated( __METHOD__, '1.35' );
181  echo $this->getAfterPortlet( $name );
182  }
183 
194  protected function getAfterPortlet( $name ) {
195  wfDeprecated( __METHOD__, '1.35' );
196  $html = '';
197  $content = $this->getSkin()->getAfterPortlet( $name );
198 
199  if ( $content !== '' ) {
200  $html = Html::rawElement(
201  'div',
202  [ 'class' => [ 'after-portlet', 'after-portlet-' . $name ] ],
203  $content
204  );
205  }
206 
207  return $html;
208  }
209 
218  protected function makeLink( $key, $item, $options = [] ) {
219  return $this->getSkin()->makeLink( $key, $item, $options );
220  }
221 
230  public function makeListItem( $key, $item, $options = [] ) {
231  return $this->getSkin()->makeListItem( $key, $item, $options );
232  }
233 
240  protected function makeSearchInput( $attrs = [] ) {
241  return $this->getSkin()->makeSearchInput( $attrs );
242  }
243 
251  protected function makeSearchButton( $mode, $attrs = [] ) {
252  return $this->getSkin()->makeSearchButton( $mode, $attrs );
253  }
254 
264  protected function getFooterLinks( $option = null ) {
265  $footerlinks = $this->get( 'footerlinks' );
266 
267  // Reduce footer links down to only those which are being used
268  $validFooterLinks = [];
269  foreach ( $footerlinks as $category => $links ) {
270  $validFooterLinks[$category] = [];
271  foreach ( $links as $link ) {
272  if ( isset( $this->data[$link] ) && $this->data[$link] ) {
273  $validFooterLinks[$category][] = $link;
274  }
275  }
276  if ( count( $validFooterLinks[$category] ) <= 0 ) {
277  unset( $validFooterLinks[$category] );
278  }
279  }
280 
281  if ( $option == 'flat' && count( $validFooterLinks ) ) {
282  // fold footerlinks into a single array using a bit of trickery
283  $validFooterLinks = array_merge( ...array_values( $validFooterLinks ) );
284  }
285 
286  return $validFooterLinks;
287  }
288 
303  protected function getFooterIcons( $option = null ) {
304  wfDeprecated( __METHOD__, '1.35' );
305  // Generate additional footer icons
306  $footericons = $this->get( 'footericons' );
307 
308  if ( $option == 'icononly' ) {
309  // Unset any icons which don't have an image
310  $this->unsetIconsWithoutImages( $footericons );
311  } elseif ( $option == 'nocopyright' ) {
312  unset( $footericons['copyright'] );
313  }
314 
315  return $footericons;
316  }
317 
324  private function unsetIconsWithoutImages( array &$icons ) {
325  // Unset any icons which don't have an image
326  foreach ( $icons as $iconsKey => &$iconsBlock ) {
327  foreach ( $iconsBlock as $iconKey => $icon ) {
328  if ( !is_string( $icon ) && !isset( $icon['src'] ) ) {
329  unset( $iconsBlock[$iconKey] );
330  }
331  }
332  if ( $iconsBlock === [] ) {
333  unset( $icons[$iconsKey] );
334  }
335  }
336  }
337 
348  protected function getFooter( $iconStyle = 'icononly', $linkStyle = 'flat' ) {
349  $validFooterIcons = $this->get( 'footericons' );
350  if ( $iconStyle === 'icononly' ) {
351  $this->unsetIconsWithoutImages( $validFooterIcons );
352  } else {
353  // take a deprecated unsupported path
354  $validFooterIcons = $this->getFooterIcons( $iconStyle );
355  }
356  $validFooterLinks = $this->getFooterLinks( $linkStyle );
357 
358  $html = '';
359 
360  if ( count( $validFooterIcons ) + count( $validFooterLinks ) > 0 ) {
361  $html .= Html::openElement( 'div', [
362  'id' => 'footer-bottom',
363  'class' => 'mw-footer',
364  'role' => 'contentinfo',
365  'lang' => $this->get( 'userlang' ),
366  'dir' => $this->get( 'dir' )
367  ] );
368  $footerEnd = Html::closeElement( 'div' );
369  } else {
370  $footerEnd = '';
371  }
372  foreach ( $validFooterIcons as $blockName => $footerIcons ) {
373  $html .= Html::openElement( 'div', [
374  'id' => Sanitizer::escapeIdForAttribute( "f-{$blockName}ico" ),
375  'class' => 'footer-icons'
376  ] );
377  foreach ( $footerIcons as $icon ) {
378  $html .= $this->getSkin()->makeFooterIcon( $icon );
379  }
380  $html .= Html::closeElement( 'div' );
381  }
382  if ( count( $validFooterLinks ) > 0 ) {
383  $html .= Html::openElement( 'ul', [ 'id' => 'f-list', 'class' => 'footer-places' ] );
384  foreach ( $validFooterLinks as $aLink ) {
385  $html .= Html::rawElement(
386  'li',
387  [ 'id' => Sanitizer::escapeIdForAttribute( $aLink ) ],
388  $this->get( $aLink )
389  );
390  }
391  $html .= Html::closeElement( 'ul' );
392  }
393 
394  $html .= $this->getClear() . $footerEnd;
395 
396  return $html;
397  }
398 
405  protected function getClear() {
406  return Html::element( 'div', [ 'class' => 'visualClear' ] );
407  }
408 
424  public function getIndicators() {
425  $out = "<div class=\"mw-indicators\">\n";
426  foreach ( $this->data['indicators'] as $id => $content ) {
427  $out .= Html::rawElement(
428  'div',
429  [
430  'id' => Sanitizer::escapeIdForAttribute( "mw-indicator-$id" ),
431  'class' => 'mw-indicator',
432  ],
433  $content
434  ) .
435  // Add whitespace between the <div>s because
436  // they get displayed with display: inline-block
437  "\n";
438  }
439  $out .= "</div>\n";
440  return $out;
441  }
442 
447  protected function printTrail() {
448  wfDeprecated( __METHOD__, '1.39' );
449  echo $this->getTrail();
450  }
451 
461  public function getTrail() {
462  wfDeprecated( __METHOD__, '1.39' );
463  $skin = $this->getSkin();
464  $options = $skin->getOptions();
465 
466  return $options['bodyOnly'] ? '' : WrappedString::join( "\n", [
467  MWDebug::getDebugHTML( $skin->getContext() ),
468  $this->get( 'bottomscripts' ),
469  $this->get( 'reporttime' )
470  ] );
471  }
472 }
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
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.
renderAfterPortlet( $name)
getToolbox()
Create an array of common toolbox items from the data in the quicktemplate stored by SkinTemplate and...
getTrail()
Get the basic end-page trail including bottomscripts, reporttime, and debug stuff.
printTrail()
Output getTrail.
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.
getAfterPortlet( $name)
Allows extensions to hook into known portlets and add stuff to them.
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:236
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:214
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition: Html.php:256
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:320
static getDebugHTML(IContextSource $context)
Returns the HTML to add to the page for the toolbar.
Definition: MWDebug.php:618
PHP-based skin template that holds data.
getSkin()
Get the Skin object related to this object.
static escapeIdForAttribute( $id, $mode=self::ID_PRIMARY)
Given a section name or other user-generated or otherwise unsafe string, escapes it to be a valid HTM...
Definition: Sanitizer.php:946
$content
Definition: router.php:76