MediaWiki  master
BaseTemplate.php
Go to the documentation of this file.
1 <?php
21 use Wikimedia\WrappedString;
22 use Wikimedia\WrappedStringList;
23 
31 abstract class BaseTemplate extends QuickTemplate {
32 
40  public function getMsg( $name, ...$params ) {
41  return $this->getSkin()->msg( $name, ...$params );
42  }
43 
44  public function msg( $str ) {
45  echo $this->getMsg( $str )->escaped();
46  }
47 
59  public function getToolbox() {
60  wfDeprecated( __METHOD__, '1.35' );
61 
62  $toolbox = $this->getSkin()->makeToolbox(
63  $this->data['nav_urls'],
64  $this->data['feeds']
65  );
66 
67  // Merge content that might be added to the toolbox section by hook
68  if ( isset( $this->data['sidebar']['TOOLBOX'] ) ) {
69  $toolbox = array_merge( $toolbox, $this->data['sidebar']['TOOLBOX'] ?? [] );
70  }
71 
72  // T253416: Deprecated hook
73  $this->getHookRunner()->onBaseTemplateToolbox( $this, $toolbox );
74  return $toolbox;
75  }
76 
81  public function getPersonalTools() {
82  return $this->getSkin()->getPersonalToolsForMakeListItem( $this->get( 'personal_urls' ) );
83  }
84 
90  protected function getSidebar( $options = [] ) {
91  // Force the rendering of the following portals
92  $sidebar = $this->data['sidebar'];
93  if ( !isset( $sidebar['SEARCH'] ) ) {
94  $sidebar['SEARCH'] = true;
95  }
96  if ( !isset( $sidebar['TOOLBOX'] ) ) {
97  $sidebar['TOOLBOX'] = true;
98  }
99  if ( !isset( $sidebar['LANGUAGES'] ) ) {
100  $sidebar['LANGUAGES'] = true;
101  }
102 
103  if ( !isset( $options['search'] ) || $options['search'] !== true ) {
104  unset( $sidebar['SEARCH'] );
105  }
106  if ( isset( $options['toolbox'] ) && $options['toolbox'] === false ) {
107  unset( $sidebar['TOOLBOX'] );
108  }
109  if ( isset( $options['languages'] ) && $options['languages'] === false ) {
110  unset( $sidebar['LANGUAGES'] );
111  }
112 
113  $boxes = [];
114  foreach ( $sidebar as $boxName => $content ) {
115  if ( $content === false ) {
116  continue;
117  }
118  switch ( $boxName ) {
119  case 'SEARCH':
120  // Search is a special case, skins should custom implement this
121  $boxes[$boxName] = [
122  'id' => 'p-search',
123  'header' => $this->getMsg( 'search' )->text(),
124  'generated' => false,
125  'content' => true,
126  ];
127  break;
128  case 'TOOLBOX':
129  $msgObj = $this->getMsg( 'toolbox' );
130  $boxes[$boxName] = [
131  'id' => 'p-tb',
132  'header' => $msgObj->exists() ? $msgObj->text() : 'toolbox',
133  'generated' => false,
134  'content' => $content,
135  ];
136  break;
137  case 'LANGUAGES':
138  if ( $this->data['language_urls'] !== false ) {
139  $msgObj = $this->getMsg( 'otherlanguages' );
140  $boxes[$boxName] = [
141  'id' => 'p-lang',
142  'header' => $msgObj->exists() ? $msgObj->text() : 'otherlanguages',
143  'generated' => false,
144  'content' => $this->data['language_urls'] ?: [],
145  ];
146  }
147  break;
148  default:
149  $msgObj = $this->getMsg( $boxName );
150  $boxes[$boxName] = [
151  'id' => "p-$boxName",
152  'header' => $msgObj->exists() ? $msgObj->text() : $boxName,
153  'generated' => true,
154  'content' => $content,
155  ];
156  break;
157  }
158  }
159 
160  // HACK: Compatibility with extensions still using SkinTemplateToolboxEnd
161  $hookContents = null;
162  if ( isset( $boxes['TOOLBOX'] ) ) {
163  ob_start();
164  // We pass an extra 'true' at the end so extensions using BaseTemplateToolbox
165  // can abort and avoid outputting double toolbox links
166  $this->getHookRunner()->onSkinTemplateToolboxEnd( $this, true );
167  $hookContents = ob_get_contents();
168  ob_end_clean();
169  if ( !trim( $hookContents ) ) {
170  $hookContents = null;
171  }
172  }
173  // END hack
174 
175  if ( isset( $options['htmlOnly'] ) && $options['htmlOnly'] === true ) {
176  foreach ( $boxes as $boxName => $box ) {
177  if ( is_array( $box['content'] ) ) {
178  $content = '<ul>';
179  foreach ( $box['content'] as $key => $val ) {
180  $content .= "\n " . $this->getSkin()->makeListItem( $key, $val );
181  }
182  // HACK, shove the toolbox end onto the toolbox if we're rendering itself
183  if ( $hookContents ) {
184  $content .= "\n $hookContents";
185  }
186  // END hack
187  $content .= "\n</ul>\n";
188  $boxes[$boxName]['content'] = $content;
189  }
190  }
191  } elseif ( $hookContents ) {
192  $boxes['TOOLBOXEND'] = [
193  'id' => 'p-toolboxend',
194  'header' => $boxes['TOOLBOX']['header'],
195  'generated' => false,
196  'content' => "<ul>{$hookContents}</ul>",
197  ];
198  // HACK: Make sure that TOOLBOXEND is sorted next to TOOLBOX
199  $boxes2 = [];
200  foreach ( $boxes as $key => $box ) {
201  if ( $key === 'TOOLBOXEND' ) {
202  continue;
203  }
204  $boxes2[$key] = $box;
205  if ( $key === 'TOOLBOX' ) {
206  $boxes2['TOOLBOXEND'] = $boxes['TOOLBOXEND'];
207  }
208  }
209  $boxes = $boxes2;
210  // END hack
211  }
212 
213  return $boxes;
214  }
215 
220  protected function renderAfterPortlet( $name ) {
221  echo $this->getAfterPortlet( $name );
222  }
223 
234  protected function getAfterPortlet( $name ) {
235  $html = '';
236  $content = '';
237  $this->getHookRunner()->onBaseTemplateAfterPortlet( $this, $name, $content );
238  $content .= $this->getSkin()->getAfterPortlet( $name );
239 
240  if ( $content !== '' ) {
241  $html = Html::rawElement(
242  'div',
243  [ 'class' => [ 'after-portlet', 'after-portlet-' . $name ] ],
244  $content
245  );
246  }
247 
248  return $html;
249  }
250 
255  protected function makeLink( $key, $item, $options = [] ) {
256  return $this->getSkin()->makeLink( $key, $item, $options );
257  }
258 
263  public function makeListItem( $key, $item, $options = [] ) {
264  return $this->getSkin()->makeListItem( $key, $item, $options );
265  }
266 
270  protected function makeSearchInput( $attrs = [] ) {
271  return $this->getSkin()->makeSearchInput( $attrs );
272  }
273 
277  protected function makeSearchButton( $mode, $attrs = [] ) {
278  return $this->getSkin()->makeSearchButton( $mode, $attrs );
279  }
280 
290  protected function getFooterLinks( $option = null ) {
291  $footerlinks = $this->get( 'footerlinks' );
292 
293  // Reduce footer links down to only those which are being used
294  $validFooterLinks = [];
295  foreach ( $footerlinks as $category => $links ) {
296  $validFooterLinks[$category] = [];
297  foreach ( $links as $link ) {
298  if ( isset( $this->data[$link] ) && $this->data[$link] ) {
299  $validFooterLinks[$category][] = $link;
300  }
301  }
302  if ( count( $validFooterLinks[$category] ) <= 0 ) {
303  unset( $validFooterLinks[$category] );
304  }
305  }
306 
307  if ( $option == 'flat' && count( $validFooterLinks ) ) {
308  // fold footerlinks into a single array using a bit of trickery
309  $validFooterLinks = array_merge( ...array_values( $validFooterLinks ) );
310  }
311 
312  return $validFooterLinks;
313  }
314 
329  protected function getFooterIcons( $option = null ) {
330  // Generate additional footer icons
331  $footericons = $this->get( 'footericons' );
332 
333  if ( $option == 'icononly' ) {
334  // Unset any icons which don't have an image
335  foreach ( $footericons as $footerIconsKey => &$footerIconsBlock ) {
336  foreach ( $footerIconsBlock as $footerIconKey => $footerIcon ) {
337  if ( !is_string( $footerIcon ) && !isset( $footerIcon['src'] ) ) {
338  unset( $footerIconsBlock[$footerIconKey] );
339  }
340  }
341  if ( $footerIconsBlock === [] ) {
342  unset( $footericons[$footerIconsKey] );
343  }
344  }
345  } elseif ( $option == 'nocopyright' ) {
346  unset( $footericons['copyright'] );
347  }
348 
349  return $footericons;
350  }
351 
361  protected function getFooter( $iconStyle = 'icononly', $linkStyle = 'flat' ) {
362  $validFooterIcons = $this->getFooterIcons( $iconStyle );
363  $validFooterLinks = $this->getFooterLinks( $linkStyle );
364 
365  $html = '';
366 
367  if ( count( $validFooterIcons ) + count( $validFooterLinks ) > 0 ) {
368  $html .= Html::openElement( 'div', [
369  'id' => 'footer-bottom',
370  'class' => 'mw-footer',
371  'role' => 'contentinfo',
372  'lang' => $this->get( 'userlang' ),
373  'dir' => $this->get( 'dir' )
374  ] );
375  $footerEnd = Html::closeElement( 'div' );
376  } else {
377  $footerEnd = '';
378  }
379  foreach ( $validFooterIcons as $blockName => $footerIcons ) {
380  $html .= Html::openElement( 'div', [
381  'id' => Sanitizer::escapeIdForAttribute( "f-{$blockName}ico" ),
382  'class' => 'footer-icons'
383  ] );
384  foreach ( $footerIcons as $icon ) {
385  $html .= $this->getSkin()->makeFooterIcon( $icon );
386  }
387  $html .= Html::closeElement( 'div' );
388  }
389  if ( count( $validFooterLinks ) > 0 ) {
390  $html .= Html::openElement( 'ul', [ 'id' => 'f-list', 'class' => 'footer-places' ] );
391  foreach ( $validFooterLinks as $aLink ) {
392  $html .= Html::rawElement(
393  'li',
394  [ 'id' => Sanitizer::escapeIdForAttribute( $aLink ) ],
395  $this->get( $aLink )
396  );
397  }
398  $html .= Html::closeElement( 'ul' );
399  }
400 
401  $html .= $this->getClear() . $footerEnd;
402 
403  return $html;
404  }
405 
412  protected function getClear() {
413  return Html::element( 'div', [ 'class' => 'visualClear' ] );
414  }
415 
431  public function getIndicators() {
432  $out = "<div class=\"mw-indicators mw-body-content\">\n";
433  foreach ( $this->data['indicators'] as $id => $content ) {
434  $out .= Html::rawElement(
435  'div',
436  [
437  'id' => Sanitizer::escapeIdForAttribute( "mw-indicator-$id" ),
438  'class' => 'mw-indicator',
439  ],
440  $content
441  ) . "\n";
442  }
443  $out .= "</div>\n";
444  return $out;
445  }
446 
450  protected function printTrail() {
451  echo $this->getTrail();
452  }
453 
462  public function getTrail() {
463  return WrappedString::join( "\n", [
465  $this->get( 'bottomscripts' ),
466  $this->get( 'reporttime' )
467  ] );
468  }
469 }
BaseTemplate\getPersonalTools
getPersonalTools()
Definition: BaseTemplate.php:81
BaseTemplate\getFooterLinks
getFooterLinks( $option=null)
Returns an array of footerlinks trimmed down to only those footer links that are valid.
Definition: BaseTemplate.php:290
BaseTemplate\msg
msg( $str)
Definition: BaseTemplate.php:44
MWDebug\getDebugHTML
static getDebugHTML(IContextSource $context)
Returns the HTML to add to the page for the toolbar.
Definition: MWDebug.php:610
BaseTemplate\makeSearchButton
makeSearchButton( $mode, $attrs=[])
Definition: BaseTemplate.php:277
Sanitizer\escapeIdForAttribute
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:815
BaseTemplate\getClear
getClear()
Get a div with the core visualClear class, for clearing floats.
Definition: BaseTemplate.php:412
getContext
getContext()
QuickTemplate\getSkin
getSkin()
Get the Skin object related to this object.
Definition: QuickTemplate.php:139
Html\closeElement
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:318
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1033
BaseTemplate\getAfterPortlet
getAfterPortlet( $name)
Allows extensions to hook into known portlets and add stuff to them.
Definition: BaseTemplate.php:234
BaseTemplate\printTrail
printTrail()
Output getTrail.
Definition: BaseTemplate.php:450
BaseTemplate\makeListItem
makeListItem( $key, $item, $options=[])
Definition: BaseTemplate.php:263
BaseTemplate\getIndicators
getIndicators()
Get the suggested HTML for page status indicators: icons (or short text snippets) usually displayed i...
Definition: BaseTemplate.php:431
BaseTemplate\getFooter
getFooter( $iconStyle='icononly', $linkStyle='flat')
Renderer for getFooterIcons and getFooterLinks.
Definition: BaseTemplate.php:361
$content
$content
Definition: router.php:76
BaseTemplate\renderAfterPortlet
renderAfterPortlet( $name)
Definition: BaseTemplate.php:220
BaseTemplate\makeSearchInput
makeSearchInput( $attrs=[])
Definition: BaseTemplate.php:270
QuickTemplate
Generic wrapper for template functions, with interface compatible with what we use of PHPTAL 0....
Definition: QuickTemplate.php:30
BaseTemplate\getToolbox
getToolbox()
Create an array of common toolbox items from the data in the quicktemplate stored by SkinTemplate and...
Definition: BaseTemplate.php:59
BaseTemplate\getTrail
getTrail()
Get the basic end-page trail including bottomscripts, reporttime, and debug stuff.
Definition: BaseTemplate.php:462
BaseTemplate\getMsg
getMsg( $name,... $params)
Get a Message object with its context set.
Definition: BaseTemplate.php:40
Html\openElement
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition: Html.php:254
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:212
BaseTemplate\makeLink
makeLink( $key, $item, $options=[])
Definition: BaseTemplate.php:255
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:234
BaseTemplate\getFooterIcons
getFooterIcons( $option=null)
Returns an array of footer icons filtered down by options relevant to how the skin wishes to display ...
Definition: BaseTemplate.php:329
BaseTemplate
New base template for a skin's template extended from QuickTemplate this class features helper method...
Definition: BaseTemplate.php:31
BaseTemplate\getSidebar
getSidebar( $options=[])
Definition: BaseTemplate.php:90