3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 use Wikimedia\WrappedString
;
22 use Wikimedia\WrappedStringList
;
25 * New base template for a skin's template extended from QuickTemplate
26 * this class features helper methods that provide common ways of interacting
27 * with the data stored in the QuickTemplate
29 abstract class BaseTemplate
extends QuickTemplate
{
32 * Get a Message object with its context set
34 * @param string $name Message name
35 * @param mixed $params,... Message params
38 public function getMsg( $name /* ... */ ) {
39 return $this->getSkin()->msg( ...func_get_args() );
42 function msg( $str ) {
43 echo $this->getMsg( $str )->escaped();
48 * @warning You should never use this method. I18n messages should be escaped
49 * @deprecated 1.32 Use ->msg() or ->getMsg() instead.
50 * @suppress SecurityCheck-XSS
51 * @return-taint exec_html
53 function msgHtml( $str ) {
54 wfDeprecated( __METHOD__
, '1.32' );
55 echo $this->getMsg( $str )->text();
59 * @deprecated since 1.33 Use ->msg() or ->getMsg() instead.
61 function msgWiki( $str ) {
62 // TODO: Add wfDeprecated( __METHOD__, '1.33' ) after 1.33 got released
63 echo $this->getMsg( $str )->parseAsBlock();
67 * Create an array of common toolbox items from the data in the quicktemplate
68 * stored by SkinTemplate.
69 * The resulting array is built according to a format intended to be passed
70 * through makeListItem to generate the html.
73 function getToolbox() {
75 if ( isset( $this->data
['nav_urls']['whatlinkshere'] )
76 && $this->data
['nav_urls']['whatlinkshere']
78 $toolbox['whatlinkshere'] = $this->data
['nav_urls']['whatlinkshere'];
79 $toolbox['whatlinkshere']['id'] = 't-whatlinkshere';
81 if ( isset( $this->data
['nav_urls']['recentchangeslinked'] )
82 && $this->data
['nav_urls']['recentchangeslinked']
84 $toolbox['recentchangeslinked'] = $this->data
['nav_urls']['recentchangeslinked'];
85 $toolbox['recentchangeslinked']['msg'] = 'recentchangeslinked-toolbox';
86 $toolbox['recentchangeslinked']['id'] = 't-recentchangeslinked';
87 $toolbox['recentchangeslinked']['rel'] = 'nofollow';
89 if ( isset( $this->data
['feeds'] ) && $this->data
['feeds'] ) {
90 $toolbox['feeds']['id'] = 'feedlinks';
91 $toolbox['feeds']['links'] = [];
92 foreach ( $this->data
['feeds'] as $key => $feed ) {
93 $toolbox['feeds']['links'][$key] = $feed;
94 $toolbox['feeds']['links'][$key]['id'] = "feed-$key";
95 $toolbox['feeds']['links'][$key]['rel'] = 'alternate';
96 $toolbox['feeds']['links'][$key]['type'] = "application/{$key}+xml";
97 $toolbox['feeds']['links'][$key]['class'] = 'feedlink';
100 foreach ( [ 'contributions', 'log', 'blockip', 'emailuser',
101 'userrights', 'upload', 'specialpages' ] as $special
103 if ( isset( $this->data
['nav_urls'][$special] ) && $this->data
['nav_urls'][$special] ) {
104 $toolbox[$special] = $this->data
['nav_urls'][$special];
105 $toolbox[$special]['id'] = "t-$special";
108 if ( isset( $this->data
['nav_urls']['print'] ) && $this->data
['nav_urls']['print'] ) {
109 $toolbox['print'] = $this->data
['nav_urls']['print'];
110 $toolbox['print']['id'] = 't-print';
111 $toolbox['print']['rel'] = 'alternate';
112 $toolbox['print']['msg'] = 'printableversion';
114 if ( isset( $this->data
['nav_urls']['permalink'] ) && $this->data
['nav_urls']['permalink'] ) {
115 $toolbox['permalink'] = $this->data
['nav_urls']['permalink'];
116 $toolbox['permalink']['id'] = 't-permalink';
118 if ( isset( $this->data
['nav_urls']['info'] ) && $this->data
['nav_urls']['info'] ) {
119 $toolbox['info'] = $this->data
['nav_urls']['info'];
120 $toolbox['info']['id'] = 't-info';
123 // Avoid PHP 7.1 warning from passing $this by reference
125 Hooks
::run( 'BaseTemplateToolbox', [ &$template, &$toolbox ] );
130 * Create an array of personal tools items from the data in the quicktemplate
131 * stored by SkinTemplate.
132 * The resulting array is built according to a format intended to be passed
133 * through makeListItem to generate the html.
134 * This is in reality the same list as already stored in personal_urls
135 * however it is reformatted so that you can just pass the individual items
136 * to makeListItem instead of hardcoding the element creation boilerplate.
139 function getPersonalTools() {
140 $personal_tools = [];
141 foreach ( $this->get( 'personal_urls' ) as $key => $plink ) {
142 # The class on a personal_urls item is meant to go on the <a> instead
143 # of the <li> so we have to use a single item "links" array instead
144 # of using most of the personal_url's keys directly.
147 [ 'single-id' => "pt-$key" ],
151 if ( isset( $plink['active'] ) ) {
152 $ptool['active'] = $plink['active'];
154 foreach ( [ 'href', 'class', 'text', 'dir', 'data', 'exists' ] as $k ) {
155 if ( isset( $plink[$k] ) ) {
156 $ptool['links'][0][$k] = $plink[$k];
159 $personal_tools[$key] = $ptool;
161 return $personal_tools;
164 function getSidebar( $options = [] ) {
165 // Force the rendering of the following portals
166 $sidebar = $this->data
['sidebar'];
167 if ( !isset( $sidebar['SEARCH'] ) ) {
168 $sidebar['SEARCH'] = true;
170 if ( !isset( $sidebar['TOOLBOX'] ) ) {
171 $sidebar['TOOLBOX'] = true;
173 if ( !isset( $sidebar['LANGUAGES'] ) ) {
174 $sidebar['LANGUAGES'] = true;
177 if ( !isset( $options['search'] ) ||
$options['search'] !== true ) {
178 unset( $sidebar['SEARCH'] );
180 if ( isset( $options['toolbox'] ) && $options['toolbox'] === false ) {
181 unset( $sidebar['TOOLBOX'] );
183 if ( isset( $options['languages'] ) && $options['languages'] === false ) {
184 unset( $sidebar['LANGUAGES'] );
188 foreach ( $sidebar as $boxName => $content ) {
189 if ( $content === false ) {
192 switch ( $boxName ) {
194 // Search is a special case, skins should custom implement this
197 'header' => $this->getMsg( 'search' )->text(),
198 'generated' => false,
203 $msgObj = $this->getMsg( 'toolbox' );
206 'header' => $msgObj->exists() ?
$msgObj->text() : 'toolbox',
207 'generated' => false,
208 'content' => $this->getToolbox(),
212 if ( $this->data
['language_urls'] !== false ) {
213 $msgObj = $this->getMsg( 'otherlanguages' );
216 'header' => $msgObj->exists() ?
$msgObj->text() : 'otherlanguages',
217 'generated' => false,
218 'content' => $this->data
['language_urls'] ?
: [],
223 $msgObj = $this->getMsg( $boxName );
225 'id' => "p-$boxName",
226 'header' => $msgObj->exists() ?
$msgObj->text() : $boxName,
228 'content' => $content,
234 // HACK: Compatibility with extensions still using SkinTemplateToolboxEnd
235 $hookContents = null;
236 if ( isset( $boxes['TOOLBOX'] ) ) {
238 // We pass an extra 'true' at the end so extensions using BaseTemplateToolbox
239 // can abort and avoid outputting double toolbox links
240 // Avoid PHP 7.1 warning from passing $this by reference
242 Hooks
::run( 'SkinTemplateToolboxEnd', [ &$template, true ] );
243 $hookContents = ob_get_contents();
245 if ( !trim( $hookContents ) ) {
246 $hookContents = null;
251 if ( isset( $options['htmlOnly'] ) && $options['htmlOnly'] === true ) {
252 foreach ( $boxes as $boxName => $box ) {
253 if ( is_array( $box['content'] ) ) {
255 foreach ( $box['content'] as $key => $val ) {
256 $content .= "\n " . $this->makeListItem( $key, $val );
258 // HACK, shove the toolbox end onto the toolbox if we're rendering itself
259 if ( $hookContents ) {
260 $content .= "\n $hookContents";
263 $content .= "\n</ul>\n";
264 $boxes[$boxName]['content'] = $content;
268 if ( $hookContents ) {
269 $boxes['TOOLBOXEND'] = [
270 'id' => 'p-toolboxend',
271 'header' => $boxes['TOOLBOX']['header'],
272 'generated' => false,
273 'content' => "<ul>{$hookContents}</ul>",
275 // HACK: Make sure that TOOLBOXEND is sorted next to TOOLBOX
277 foreach ( $boxes as $key => $box ) {
278 if ( $key === 'TOOLBOXEND' ) {
281 $boxes2[$key] = $box;
282 if ( $key === 'TOOLBOX' ) {
283 $boxes2['TOOLBOXEND'] = $boxes['TOOLBOXEND'];
295 * @param string $name
297 protected function renderAfterPortlet( $name ) {
298 echo $this->getAfterPortlet( $name );
302 * Allows extensions to hook into known portlets and add stuff to them
304 * @param string $name
306 * @return string html
309 protected function getAfterPortlet( $name ) {
312 Hooks
::run( 'BaseTemplateAfterPortlet', [ $this, $name, &$content ] );
314 if ( $content !== '' ) {
315 $html = Html
::rawElement(
317 [ 'class' => [ 'after-portlet', 'after-portlet-' . $name ] ],
326 * Makes a link, usually used by makeListItem to generate a link for an item
327 * in a list used in navigation lists, portlets, portals, sidebars, etc...
329 * @param string $key Usually a key from the list you are generating this
331 * @param array $item Contains some of a specific set of keys.
333 * The text of the link will be generated either from the contents of the
334 * "text" key in the $item array, if a "msg" key is present a message by
335 * that name will be used, and if neither of those are set the $key will be
336 * used as a message name.
338 * If a "href" key is not present makeLink will just output htmlescaped text.
339 * The "href", "id", "class", "rel", and "type" keys are used as attributes
340 * for the link if present.
342 * If an "id" or "single-id" (if you don't want the actual id to be output
343 * on the link) is present it will be used to generate a tooltip and
344 * accesskey for the link.
346 * The keys "context" and "primary" are ignored; these keys are used
347 * internally by skins and are not supposed to be included in the HTML
350 * If you don't want an accesskey, set $item['tooltiponly'] = true;
352 * If a "data" key is present, it must be an array, where the keys represent
353 * the data-xxx properties with their provided values. For example,
358 * will render as element properties:
359 * data-foo='1' data-bar='baz'
361 * @param array $options Can be used to affect the output of a link.
362 * Possible options are:
363 * - 'text-wrapper' key to specify a list of elements to wrap the text of
364 * a link in. This should be an array of arrays containing a 'tag' and
365 * optionally an 'attributes' key. If you only have one element you don't
366 * need to wrap it in another array. eg: To use <a><span>...</span></a>
367 * in all links use [ 'text-wrapper' => [ 'tag' => 'span' ] ]
369 * - 'link-class' key can be used to specify additional classes to apply
371 * - 'link-fallback' can be used to specify a tag to use instead of "<a>"
372 * if there is no link. eg: If you specify 'link-fallback' => 'span' than
373 * any non-link will output a "<span>" instead of just text.
377 function makeLink( $key, $item, $options = [] ) {
378 $text = $item['text'] ??
wfMessage( $item['msg'] ??
$key )->text();
380 $html = htmlspecialchars( $text );
382 if ( isset( $options['text-wrapper'] ) ) {
383 $wrapper = $options['text-wrapper'];
384 if ( isset( $wrapper['tag'] ) ) {
385 $wrapper = [ $wrapper ];
387 while ( count( $wrapper ) > 0 ) {
388 $element = array_pop( $wrapper );
389 $html = Html
::rawElement( $element['tag'], $element['attributes'] ??
null, $html );
393 if ( isset( $item['href'] ) ||
isset( $options['link-fallback'] ) ) {
395 foreach ( [ 'single-id', 'text', 'msg', 'tooltiponly', 'context', 'primary',
396 'tooltip-params', 'exists' ] as $k ) {
400 if ( isset( $attrs['data'] ) ) {
401 foreach ( $attrs['data'] as $key => $value ) {
402 $attrs[ 'data-' . $key ] = $value;
404 unset( $attrs[ 'data' ] );
407 if ( isset( $item['id'] ) && !isset( $item['single-id'] ) ) {
408 $item['single-id'] = $item['id'];
412 if ( isset( $item['tooltip-params'] ) ) {
413 $tooltipParams = $item['tooltip-params'];
416 if ( isset( $item['single-id'] ) ) {
417 $tooltipOption = isset( $item['exists'] ) && $item['exists'] === false ?
'nonexisting' : null;
419 if ( isset( $item['tooltiponly'] ) && $item['tooltiponly'] ) {
420 $title = Linker
::titleAttrib( $item['single-id'], $tooltipOption, $tooltipParams );
421 if ( $title !== false ) {
422 $attrs['title'] = $title;
425 $tip = Linker
::tooltipAndAccesskeyAttribs(
430 if ( isset( $tip['title'] ) && $tip['title'] !== false ) {
431 $attrs['title'] = $tip['title'];
433 if ( isset( $tip['accesskey'] ) && $tip['accesskey'] !== false ) {
434 $attrs['accesskey'] = $tip['accesskey'];
438 if ( isset( $options['link-class'] ) ) {
439 if ( isset( $attrs['class'] ) ) {
440 $attrs['class'] .= " {$options['link-class']}";
442 $attrs['class'] = $options['link-class'];
445 $html = Html
::rawElement( isset( $attrs['href'] )
447 : $options['link-fallback'], $attrs, $html );
454 * Generates a list item for a navigation, portlet, portal, sidebar... list
456 * @param string $key Usually a key from the list you are generating this link from.
457 * @param array $item Array of list item data containing some of a specific set of keys.
458 * The "id", "class" and "itemtitle" keys will be used as attributes for the list item,
459 * if "active" contains a value of true a "active" class will also be appended to class.
461 * @param array $options
463 * If you want something other than a "<li>" you can pass a tag name such as
464 * "tag" => "span" in the $options array to change the tag used.
465 * link/content data for the list item may come in one of two forms
466 * A "links" key may be used, in which case it should contain an array with
467 * a list of links to include inside the list item, see makeLink for the
468 * format of individual links array items.
470 * Otherwise the relevant keys from the list item $item array will be passed
471 * to makeLink instead. Note however that "id" and "class" are used by the
472 * list item directly so they will not be passed to makeLink
473 * (however the link will still support a tooltip and accesskey from it)
474 * If you need an id or class on a single link you should include a "links"
475 * array with just one link item inside of it. You can also set "link-class" in
476 * $item to set a class on the link itself. If you want to add a title
477 * to the list item itself, you can set "itemtitle" to the value.
478 * $options is also passed on to makeLink calls
482 function makeListItem( $key, $item, $options = [] ) {
483 if ( isset( $item['links'] ) ) {
485 foreach ( $item['links'] as $linkKey => $link ) {
486 $links[] = $this->makeLink( $linkKey, $link, $options );
488 $html = implode( ' ', $links );
491 // These keys are used by makeListItem and shouldn't be passed on to the link
492 foreach ( [ 'id', 'class', 'active', 'tag', 'itemtitle' ] as $k ) {
495 if ( isset( $item['id'] ) && !isset( $item['single-id'] ) ) {
496 // The id goes on the <li> not on the <a> for single links
497 // but makeSidebarLink still needs to know what id to use when
498 // generating tooltips and accesskeys.
499 $link['single-id'] = $item['id'];
501 if ( isset( $link['link-class'] ) ) {
502 // link-class should be set on the <a> itself,
503 // so pass it in as 'class'
504 $link['class'] = $link['link-class'];
505 unset( $link['link-class'] );
507 $html = $this->makeLink( $key, $link, $options );
511 foreach ( [ 'id', 'class' ] as $attr ) {
512 if ( isset( $item[$attr] ) ) {
513 $attrs[$attr] = $item[$attr];
516 if ( isset( $item['active'] ) && $item['active'] ) {
517 if ( !isset( $attrs['class'] ) ) {
518 $attrs['class'] = '';
520 $attrs['class'] .= ' active';
521 $attrs['class'] = trim( $attrs['class'] );
523 if ( isset( $item['itemtitle'] ) ) {
524 $attrs['title'] = $item['itemtitle'];
526 return Html
::rawElement( $options['tag'] ??
'li', $attrs, $html );
529 function makeSearchInput( $attrs = [] ) {
533 'placeholder' => wfMessage( 'searchsuggest-search' )->text(),
535 $realAttrs = array_merge( $realAttrs, Linker
::tooltipAndAccesskeyAttribs( 'search' ), $attrs );
536 return Html
::element( 'input', $realAttrs );
539 function makeSearchButton( $mode, $attrs = [] ) {
546 'value' => wfMessage( $mode == 'go' ?
'searcharticle' : 'searchbutton' )->text(),
548 $realAttrs = array_merge(
550 Linker
::tooltipAndAccesskeyAttribs( "search-$mode" ),
553 return Html
::element( 'input', $realAttrs );
559 $buttonAttrs = array_merge(
561 Linker
::tooltipAndAccesskeyAttribs( 'search-fulltext' ),
564 unset( $buttonAttrs['src'] );
565 unset( $buttonAttrs['alt'] );
566 unset( $buttonAttrs['width'] );
567 unset( $buttonAttrs['height'] );
569 'src' => $attrs['src'],
570 'alt' => $attrs['alt'] ??
wfMessage( 'searchbutton' )->text(),
571 'width' => $attrs['width'] ??
null,
572 'height' => $attrs['height'] ??
null,
574 return Html
::rawElement( 'button', $buttonAttrs, Html
::element( 'img', $imgAttrs ) );
576 throw new MWException( 'Unknown mode passed to BaseTemplate::makeSearchButton' );
581 * Returns an array of footerlinks trimmed down to only those footer links that
583 * If you pass "flat" as an option then the returned array will be a flat array
584 * of footer icons instead of a key/value array of footerlinks arrays broken
585 * up into categories.
586 * @param string|null $option
587 * @return array|mixed
589 function getFooterLinks( $option = null ) {
590 $footerlinks = $this->get( 'footerlinks' );
592 // Reduce footer links down to only those which are being used
593 $validFooterLinks = [];
594 foreach ( $footerlinks as $category => $links ) {
595 $validFooterLinks[$category] = [];
596 foreach ( $links as $link ) {
597 if ( isset( $this->data
[$link] ) && $this->data
[$link] ) {
598 $validFooterLinks[$category][] = $link;
601 if ( count( $validFooterLinks[$category] ) <= 0 ) {
602 unset( $validFooterLinks[$category] );
606 if ( $option == 'flat' ) {
607 // fold footerlinks into a single array using a bit of trickery
608 $validFooterLinks = array_merge( ...array_values( $validFooterLinks ) );
611 return $validFooterLinks;
615 * Returns an array of footer icons filtered down by options relevant to how
616 * the skin wishes to display them.
617 * If you pass "icononly" as the option all footer icons which do not have an
618 * image icon set will be filtered out.
619 * If you pass "nocopyright" then MediaWiki's copyright icon will not be included
620 * in the list of footer icons. This is mostly useful for skins which only
621 * display the text from footericons instead of the images and don't want a
622 * duplicate copyright statement because footerlinks already rendered one.
623 * @param string|null $option
626 function getFooterIcons( $option = null ) {
627 // Generate additional footer icons
628 $footericons = $this->get( 'footericons' );
630 if ( $option == 'icononly' ) {
631 // Unset any icons which don't have an image
632 foreach ( $footericons as &$footerIconsBlock ) {
633 foreach ( $footerIconsBlock as $footerIconKey => $footerIcon ) {
634 if ( !is_string( $footerIcon ) && !isset( $footerIcon['src'] ) ) {
635 unset( $footerIconsBlock[$footerIconKey] );
639 // Redo removal of any empty blocks
640 foreach ( $footericons as $footerIconsKey => &$footerIconsBlock ) {
641 if ( count( $footerIconsBlock ) <= 0 ) {
642 unset( $footericons[$footerIconsKey] );
645 } elseif ( $option == 'nocopyright' ) {
646 unset( $footericons['copyright']['copyright'] );
647 if ( count( $footericons['copyright'] ) <= 0 ) {
648 unset( $footericons['copyright'] );
656 * Renderer for getFooterIcons and getFooterLinks
658 * @param string $iconStyle $option for getFooterIcons: "icononly", "nocopyright"
659 * @param string $linkStyle $option for getFooterLinks: "flat"
661 * @return string html
664 protected function getFooter( $iconStyle = 'icononly', $linkStyle = 'flat' ) {
665 $validFooterIcons = $this->getFooterIcons( $iconStyle );
666 $validFooterLinks = $this->getFooterLinks( $linkStyle );
670 if ( count( $validFooterIcons ) +
count( $validFooterLinks ) > 0 ) {
671 $html .= Html
::openElement( 'div', [
672 'id' => 'footer-bottom',
673 'role' => 'contentinfo',
674 'lang' => $this->get( 'userlang' ),
675 'dir' => $this->get( 'dir' )
677 $footerEnd = Html
::closeElement( 'div' );
681 foreach ( $validFooterIcons as $blockName => $footerIcons ) {
682 $html .= Html
::openElement( 'div', [
683 'id' => Sanitizer
::escapeIdForAttribute( "f-{$blockName}ico" ),
684 'class' => 'footer-icons'
686 foreach ( $footerIcons as $icon ) {
687 $html .= $this->getSkin()->makeFooterIcon( $icon );
689 $html .= Html
::closeElement( 'div' );
691 if ( count( $validFooterLinks ) > 0 ) {
692 $html .= Html
::openElement( 'ul', [ 'id' => 'f-list', 'class' => 'footer-places' ] );
693 foreach ( $validFooterLinks as $aLink ) {
694 $html .= Html
::rawElement(
696 [ 'id' => Sanitizer
::escapeIdForAttribute( $aLink ) ],
700 $html .= Html
::closeElement( 'ul' );
703 $html .= $this->getClear() . $footerEnd;
709 * Get a div with the core visualClear class, for clearing floats
711 * @return string html
714 protected function getClear() {
715 return Html
::element( 'div', [ 'class' => 'visualClear' ] );
719 * Get the suggested HTML for page status indicators: icons (or short text snippets) usually
720 * displayed in the top-right corner of the page, outside of the main content.
722 * Your skin may implement this differently, for example by handling some indicator names
723 * specially with a different UI. However, it is recommended to use a `<div class="mw-indicator"
724 * id="mw-indicator-<id>" />` as a wrapper element for each indicator, for better compatibility
725 * with extensions and user scripts.
727 * The raw data is available in `$this->data['indicators']` as an associative array (keys:
728 * identifiers, values: contents) internally ordered by keys.
730 * @return string HTML
733 public function getIndicators() {
734 $out = "<div class=\"mw-indicators mw-body-content\">\n";
735 foreach ( $this->data
['indicators'] as $id => $content ) {
736 $out .= Html
::rawElement(
739 'id' => Sanitizer
::escapeIdForAttribute( "mw-indicator-$id" ),
740 'class' => 'mw-indicator',
752 function printTrail() {
753 echo $this->getTrail();
757 * Get the basic end-page trail including bottomscripts, reporttime, and
758 * debug stuff. This should be called right before outputting the closing
759 * body and html tags.
761 * @return string|WrappedStringList HTML
764 public function getTrail() {
765 return WrappedString
::join( "\n", [
766 MWDebug
::getDebugHTML( $this->getSkin()->getContext() ),
767 $this->get( 'bottomscripts' ),
768 $this->get( 'reporttime' )