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
36 * @suppress PhanCommentParamWithoutRealParam HHVM bug T228695#5450847
39 public function getMsg( $name /* ... */ ) {
40 return $this->getSkin()->msg( ...func_get_args() );
43 function msg( $str ) {
44 echo $this->getMsg( $str )->escaped();
48 * @deprecated since 1.33 Use ->msg() or ->getMsg() instead.
50 function msgWiki( $str ) {
51 // TODO: Add wfDeprecated( __METHOD__, '1.33' ) after 1.33 got released
52 echo $this->getMsg( $str )->parseAsBlock();
56 * Create an array of common toolbox items from the data in the quicktemplate
57 * stored by SkinTemplate.
58 * The resulting array is built according to a format intended to be passed
59 * through makeListItem to generate the html.
62 function getToolbox() {
64 if ( isset( $this->data
['nav_urls']['whatlinkshere'] )
65 && $this->data
['nav_urls']['whatlinkshere']
67 $toolbox['whatlinkshere'] = $this->data
['nav_urls']['whatlinkshere'];
68 $toolbox['whatlinkshere']['id'] = 't-whatlinkshere';
70 if ( isset( $this->data
['nav_urls']['recentchangeslinked'] )
71 && $this->data
['nav_urls']['recentchangeslinked']
73 $toolbox['recentchangeslinked'] = $this->data
['nav_urls']['recentchangeslinked'];
74 $toolbox['recentchangeslinked']['msg'] = 'recentchangeslinked-toolbox';
75 $toolbox['recentchangeslinked']['id'] = 't-recentchangeslinked';
76 $toolbox['recentchangeslinked']['rel'] = 'nofollow';
78 if ( isset( $this->data
['feeds'] ) && $this->data
['feeds'] ) {
79 $toolbox['feeds']['id'] = 'feedlinks';
80 $toolbox['feeds']['links'] = [];
81 foreach ( $this->data
['feeds'] as $key => $feed ) {
82 $toolbox['feeds']['links'][$key] = $feed;
83 $toolbox['feeds']['links'][$key]['id'] = "feed-$key";
84 $toolbox['feeds']['links'][$key]['rel'] = 'alternate';
85 $toolbox['feeds']['links'][$key]['type'] = "application/{$key}+xml";
86 $toolbox['feeds']['links'][$key]['class'] = 'feedlink';
89 foreach ( [ 'contributions', 'log', 'blockip', 'emailuser', 'mute',
90 'userrights', 'upload', 'specialpages' ] as $special
92 if ( isset( $this->data
['nav_urls'][$special] ) && $this->data
['nav_urls'][$special] ) {
93 $toolbox[$special] = $this->data
['nav_urls'][$special];
94 $toolbox[$special]['id'] = "t-$special";
97 if ( isset( $this->data
['nav_urls']['print'] ) && $this->data
['nav_urls']['print'] ) {
98 $toolbox['print'] = $this->data
['nav_urls']['print'];
99 $toolbox['print']['id'] = 't-print';
100 $toolbox['print']['rel'] = 'alternate';
101 $toolbox['print']['msg'] = 'printableversion';
103 if ( isset( $this->data
['nav_urls']['permalink'] ) && $this->data
['nav_urls']['permalink'] ) {
104 $toolbox['permalink'] = $this->data
['nav_urls']['permalink'];
105 $toolbox['permalink']['id'] = 't-permalink';
107 if ( isset( $this->data
['nav_urls']['info'] ) && $this->data
['nav_urls']['info'] ) {
108 $toolbox['info'] = $this->data
['nav_urls']['info'];
109 $toolbox['info']['id'] = 't-info';
112 // Avoid PHP 7.1 warning from passing $this by reference
114 Hooks
::run( 'BaseTemplateToolbox', [ &$template, &$toolbox ] );
119 * Create an array of personal tools items from the data in the quicktemplate
120 * stored by SkinTemplate.
121 * The resulting array is built according to a format intended to be passed
122 * through makeListItem to generate the html.
123 * This is in reality the same list as already stored in personal_urls
124 * however it is reformatted so that you can just pass the individual items
125 * to makeListItem instead of hardcoding the element creation boilerplate.
128 function getPersonalTools() {
129 $personal_tools = [];
130 foreach ( $this->get( 'personal_urls' ) as $key => $plink ) {
131 # The class on a personal_urls item is meant to go on the <a> instead
132 # of the <li> so we have to use a single item "links" array instead
133 # of using most of the personal_url's keys directly.
136 [ 'single-id' => "pt-$key" ],
140 if ( isset( $plink['active'] ) ) {
141 $ptool['active'] = $plink['active'];
143 foreach ( [ 'href', 'class', 'text', 'dir', 'data', 'exists' ] as $k ) {
144 if ( isset( $plink[$k] ) ) {
145 $ptool['links'][0][$k] = $plink[$k];
148 $personal_tools[$key] = $ptool;
150 return $personal_tools;
153 function getSidebar( $options = [] ) {
154 // Force the rendering of the following portals
155 $sidebar = $this->data
['sidebar'];
156 if ( !isset( $sidebar['SEARCH'] ) ) {
157 $sidebar['SEARCH'] = true;
159 if ( !isset( $sidebar['TOOLBOX'] ) ) {
160 $sidebar['TOOLBOX'] = true;
162 if ( !isset( $sidebar['LANGUAGES'] ) ) {
163 $sidebar['LANGUAGES'] = true;
166 if ( !isset( $options['search'] ) ||
$options['search'] !== true ) {
167 unset( $sidebar['SEARCH'] );
169 if ( isset( $options['toolbox'] ) && $options['toolbox'] === false ) {
170 unset( $sidebar['TOOLBOX'] );
172 if ( isset( $options['languages'] ) && $options['languages'] === false ) {
173 unset( $sidebar['LANGUAGES'] );
177 foreach ( $sidebar as $boxName => $content ) {
178 if ( $content === false ) {
181 switch ( $boxName ) {
183 // Search is a special case, skins should custom implement this
186 'header' => $this->getMsg( 'search' )->text(),
187 'generated' => false,
192 $msgObj = $this->getMsg( 'toolbox' );
195 'header' => $msgObj->exists() ?
$msgObj->text() : 'toolbox',
196 'generated' => false,
197 'content' => $this->getToolbox(),
201 if ( $this->data
['language_urls'] !== false ) {
202 $msgObj = $this->getMsg( 'otherlanguages' );
205 'header' => $msgObj->exists() ?
$msgObj->text() : 'otherlanguages',
206 'generated' => false,
207 'content' => $this->data
['language_urls'] ?
: [],
212 $msgObj = $this->getMsg( $boxName );
214 'id' => "p-$boxName",
215 'header' => $msgObj->exists() ?
$msgObj->text() : $boxName,
217 'content' => $content,
223 // HACK: Compatibility with extensions still using SkinTemplateToolboxEnd
224 $hookContents = null;
225 if ( isset( $boxes['TOOLBOX'] ) ) {
227 // We pass an extra 'true' at the end so extensions using BaseTemplateToolbox
228 // can abort and avoid outputting double toolbox links
229 // Avoid PHP 7.1 warning from passing $this by reference
231 Hooks
::run( 'SkinTemplateToolboxEnd', [ &$template, true ] );
232 $hookContents = ob_get_contents();
234 if ( !trim( $hookContents ) ) {
235 $hookContents = null;
240 if ( isset( $options['htmlOnly'] ) && $options['htmlOnly'] === true ) {
241 foreach ( $boxes as $boxName => $box ) {
242 if ( is_array( $box['content'] ) ) {
244 foreach ( $box['content'] as $key => $val ) {
245 $content .= "\n " . $this->makeListItem( $key, $val );
247 // HACK, shove the toolbox end onto the toolbox if we're rendering itself
248 if ( $hookContents ) {
249 $content .= "\n $hookContents";
252 $content .= "\n</ul>\n";
253 $boxes[$boxName]['content'] = $content;
256 } elseif ( $hookContents ) {
257 $boxes['TOOLBOXEND'] = [
258 'id' => 'p-toolboxend',
259 'header' => $boxes['TOOLBOX']['header'],
260 'generated' => false,
261 'content' => "<ul>{$hookContents}</ul>",
263 // HACK: Make sure that TOOLBOXEND is sorted next to TOOLBOX
265 foreach ( $boxes as $key => $box ) {
266 if ( $key === 'TOOLBOXEND' ) {
269 $boxes2[$key] = $box;
270 if ( $key === 'TOOLBOX' ) {
271 $boxes2['TOOLBOXEND'] = $boxes['TOOLBOXEND'];
282 * @param string $name
284 protected function renderAfterPortlet( $name ) {
285 echo $this->getAfterPortlet( $name );
289 * Allows extensions to hook into known portlets and add stuff to them
291 * @param string $name
293 * @return string html
296 protected function getAfterPortlet( $name ) {
299 Hooks
::run( 'BaseTemplateAfterPortlet', [ $this, $name, &$content ] );
301 if ( $content !== '' ) {
302 $html = Html
::rawElement(
304 [ 'class' => [ 'after-portlet', 'after-portlet-' . $name ] ],
313 * Makes a link, usually used by makeListItem to generate a link for an item
314 * in a list used in navigation lists, portlets, portals, sidebars, etc...
316 * @param string $key Usually a key from the list you are generating this
318 * @param array $item Contains some of a specific set of keys.
320 * The text of the link will be generated either from the contents of the
321 * "text" key in the $item array, if a "msg" key is present a message by
322 * that name will be used, and if neither of those are set the $key will be
323 * used as a message name.
325 * If a "href" key is not present makeLink will just output htmlescaped text.
326 * The "href", "id", "class", "rel", and "type" keys are used as attributes
327 * for the link if present.
329 * If an "id" or "single-id" (if you don't want the actual id to be output
330 * on the link) is present it will be used to generate a tooltip and
331 * accesskey for the link.
333 * The keys "context" and "primary" are ignored; these keys are used
334 * internally by skins and are not supposed to be included in the HTML
337 * If you don't want an accesskey, set $item['tooltiponly'] = true;
339 * If a "data" key is present, it must be an array, where the keys represent
340 * the data-xxx properties with their provided values. For example,
345 * will render as element properties:
346 * data-foo='1' data-bar='baz'
348 * @param array $options Can be used to affect the output of a link.
349 * Possible options are:
350 * - 'text-wrapper' key to specify a list of elements to wrap the text of
351 * a link in. This should be an array of arrays containing a 'tag' and
352 * optionally an 'attributes' key. If you only have one element you don't
353 * need to wrap it in another array. eg: To use <a><span>...</span></a>
354 * in all links use [ 'text-wrapper' => [ 'tag' => 'span' ] ]
356 * - 'link-class' key can be used to specify additional classes to apply
358 * - 'link-fallback' can be used to specify a tag to use instead of "<a>"
359 * if there is no link. eg: If you specify 'link-fallback' => 'span' than
360 * any non-link will output a "<span>" instead of just text.
364 function makeLink( $key, $item, $options = [] ) {
365 $text = $item['text'] ??
wfMessage( $item['msg'] ??
$key )->text();
367 $html = htmlspecialchars( $text );
369 if ( isset( $options['text-wrapper'] ) ) {
370 $wrapper = $options['text-wrapper'];
371 if ( isset( $wrapper['tag'] ) ) {
372 $wrapper = [ $wrapper ];
374 while ( count( $wrapper ) > 0 ) {
375 $element = array_pop( $wrapper );
376 $html = Html
::rawElement( $element['tag'], $element['attributes'] ??
null, $html );
380 if ( isset( $item['href'] ) ||
isset( $options['link-fallback'] ) ) {
382 foreach ( [ 'single-id', 'text', 'msg', 'tooltiponly', 'context', 'primary',
383 'tooltip-params', 'exists' ] as $k ) {
387 if ( isset( $attrs['data'] ) ) {
388 foreach ( $attrs['data'] as $key => $value ) {
389 $attrs[ 'data-' . $key ] = $value;
391 unset( $attrs[ 'data' ] );
394 if ( isset( $item['id'] ) && !isset( $item['single-id'] ) ) {
395 $item['single-id'] = $item['id'];
399 if ( isset( $item['tooltip-params'] ) ) {
400 $tooltipParams = $item['tooltip-params'];
403 if ( isset( $item['single-id'] ) ) {
404 $tooltipOption = isset( $item['exists'] ) && $item['exists'] === false ?
'nonexisting' : null;
406 if ( isset( $item['tooltiponly'] ) && $item['tooltiponly'] ) {
407 $title = Linker
::titleAttrib( $item['single-id'], $tooltipOption, $tooltipParams );
408 if ( $title !== false ) {
409 $attrs['title'] = $title;
412 $tip = Linker
::tooltipAndAccesskeyAttribs(
417 if ( isset( $tip['title'] ) && $tip['title'] !== false ) {
418 $attrs['title'] = $tip['title'];
420 if ( isset( $tip['accesskey'] ) && $tip['accesskey'] !== false ) {
421 $attrs['accesskey'] = $tip['accesskey'];
425 if ( isset( $options['link-class'] ) ) {
426 if ( isset( $attrs['class'] ) ) {
427 $attrs['class'] .= " {$options['link-class']}";
429 $attrs['class'] = $options['link-class'];
432 $html = Html
::rawElement( isset( $attrs['href'] )
434 : $options['link-fallback'], $attrs, $html );
441 * Generates a list item for a navigation, portlet, portal, sidebar... list
443 * @param string $key Usually a key from the list you are generating this link from.
444 * @param array $item Array of list item data containing some of a specific set of keys.
445 * The "id", "class" and "itemtitle" keys will be used as attributes for the list item,
446 * if "active" contains a value of true a "active" class will also be appended to class.
447 * @phan-param array{id?:string,class?:string,itemtitle?:string,active?:bool} $item
449 * @param array $options
450 * @phan-param array{tag?:string} $options
452 * If you want something other than a "<li>" you can pass a tag name such as
453 * "tag" => "span" in the $options array to change the tag used.
454 * link/content data for the list item may come in one of two forms
455 * A "links" key may be used, in which case it should contain an array with
456 * a list of links to include inside the list item, see makeLink for the
457 * format of individual links array items.
459 * Otherwise the relevant keys from the list item $item array will be passed
460 * to makeLink instead. Note however that "id" and "class" are used by the
461 * list item directly so they will not be passed to makeLink
462 * (however the link will still support a tooltip and accesskey from it)
463 * If you need an id or class on a single link you should include a "links"
464 * array with just one link item inside of it. You can also set "link-class" in
465 * $item to set a class on the link itself. If you want to add a title
466 * to the list item itself, you can set "itemtitle" to the value.
467 * $options is also passed on to makeLink calls
471 function makeListItem( $key, $item, $options = [] ) {
472 // In case this is still set from SkinTemplate, we don't want it to appear in
473 // the HTML output (normally removed in SkinTemplate::buildContentActionUrls())
474 unset( $item['redundant'] );
476 if ( isset( $item['links'] ) ) {
478 foreach ( $item['links'] as $linkKey => $link ) {
479 $links[] = $this->makeLink( $linkKey, $link, $options );
481 $html = implode( ' ', $links );
484 // These keys are used by makeListItem and shouldn't be passed on to the link
485 foreach ( [ 'id', 'class', 'active', 'tag', 'itemtitle' ] as $k ) {
488 if ( isset( $item['id'] ) && !isset( $item['single-id'] ) ) {
489 // The id goes on the <li> not on the <a> for single links
490 // but makeSidebarLink still needs to know what id to use when
491 // generating tooltips and accesskeys.
492 $link['single-id'] = $item['id'];
494 if ( isset( $link['link-class'] ) ) {
495 // link-class should be set on the <a> itself,
496 // so pass it in as 'class'
497 $link['class'] = $link['link-class'];
498 unset( $link['link-class'] );
500 $html = $this->makeLink( $key, $link, $options );
504 foreach ( [ 'id', 'class' ] as $attr ) {
505 if ( isset( $item[$attr] ) ) {
506 $attrs[$attr] = $item[$attr];
509 if ( isset( $item['active'] ) && $item['active'] ) {
510 if ( !isset( $attrs['class'] ) ) {
511 $attrs['class'] = '';
513 $attrs['class'] .= ' active';
514 $attrs['class'] = trim( $attrs['class'] );
516 if ( isset( $item['itemtitle'] ) ) {
517 $attrs['title'] = $item['itemtitle'];
519 return Html
::rawElement( $options['tag'] ??
'li', $attrs, $html );
522 function makeSearchInput( $attrs = [] ) {
526 'placeholder' => wfMessage( 'searchsuggest-search' )->text(),
528 $realAttrs = array_merge( $realAttrs, Linker
::tooltipAndAccesskeyAttribs( 'search' ), $attrs );
529 return Html
::element( 'input', $realAttrs );
532 function makeSearchButton( $mode, $attrs = [] ) {
539 'value' => wfMessage( $mode == 'go' ?
'searcharticle' : 'searchbutton' )->text(),
541 $realAttrs = array_merge(
543 Linker
::tooltipAndAccesskeyAttribs( "search-$mode" ),
546 return Html
::element( 'input', $realAttrs );
552 $buttonAttrs = array_merge(
554 Linker
::tooltipAndAccesskeyAttribs( 'search-fulltext' ),
557 unset( $buttonAttrs['src'] );
558 unset( $buttonAttrs['alt'] );
559 unset( $buttonAttrs['width'] );
560 unset( $buttonAttrs['height'] );
562 'src' => $attrs['src'],
563 'alt' => $attrs['alt'] ??
wfMessage( 'searchbutton' )->text(),
564 'width' => $attrs['width'] ??
null,
565 'height' => $attrs['height'] ??
null,
567 return Html
::rawElement( 'button', $buttonAttrs, Html
::element( 'img', $imgAttrs ) );
569 throw new MWException( 'Unknown mode passed to BaseTemplate::makeSearchButton' );
574 * Returns an array of footerlinks trimmed down to only those footer links that
576 * If you pass "flat" as an option then the returned array will be a flat array
577 * of footer icons instead of a key/value array of footerlinks arrays broken
578 * up into categories.
579 * @param string|null $option
580 * @return array|mixed
582 function getFooterLinks( $option = null ) {
583 $footerlinks = $this->get( 'footerlinks' );
585 // Reduce footer links down to only those which are being used
586 $validFooterLinks = [];
587 foreach ( $footerlinks as $category => $links ) {
588 $validFooterLinks[$category] = [];
589 foreach ( $links as $link ) {
590 if ( isset( $this->data
[$link] ) && $this->data
[$link] ) {
591 $validFooterLinks[$category][] = $link;
594 if ( count( $validFooterLinks[$category] ) <= 0 ) {
595 unset( $validFooterLinks[$category] );
599 if ( $option == 'flat' ) {
600 // fold footerlinks into a single array using a bit of trickery
601 $validFooterLinks = array_merge( ...array_values( $validFooterLinks ) );
604 return $validFooterLinks;
608 * Returns an array of footer icons filtered down by options relevant to how
609 * the skin wishes to display them.
610 * If you pass "icononly" as the option all footer icons which do not have an
611 * image icon set will be filtered out.
612 * If you pass "nocopyright" then MediaWiki's copyright icon will not be included
613 * in the list of footer icons. This is mostly useful for skins which only
614 * display the text from footericons instead of the images and don't want a
615 * duplicate copyright statement because footerlinks already rendered one.
616 * @param string|null $option
619 function getFooterIcons( $option = null ) {
620 // Generate additional footer icons
621 $footericons = $this->get( 'footericons' );
623 if ( $option == 'icononly' ) {
624 // Unset any icons which don't have an image
625 foreach ( $footericons as &$footerIconsBlock ) {
626 foreach ( $footerIconsBlock as $footerIconKey => $footerIcon ) {
627 if ( !is_string( $footerIcon ) && !isset( $footerIcon['src'] ) ) {
628 unset( $footerIconsBlock[$footerIconKey] );
632 // Redo removal of any empty blocks
633 foreach ( $footericons as $footerIconsKey => &$footerIconsBlock ) {
634 if ( count( $footerIconsBlock ) <= 0 ) {
635 unset( $footericons[$footerIconsKey] );
638 } elseif ( $option == 'nocopyright' ) {
639 unset( $footericons['copyright']['copyright'] );
640 if ( count( $footericons['copyright'] ) <= 0 ) {
641 unset( $footericons['copyright'] );
649 * Renderer for getFooterIcons and getFooterLinks
651 * @param string $iconStyle $option for getFooterIcons: "icononly", "nocopyright"
652 * @param string $linkStyle $option for getFooterLinks: "flat"
654 * @return string html
657 protected function getFooter( $iconStyle = 'icononly', $linkStyle = 'flat' ) {
658 $validFooterIcons = $this->getFooterIcons( $iconStyle );
659 $validFooterLinks = $this->getFooterLinks( $linkStyle );
663 if ( count( $validFooterIcons ) +
count( $validFooterLinks ) > 0 ) {
664 $html .= Html
::openElement( 'div', [
665 'id' => 'footer-bottom',
666 'role' => 'contentinfo',
667 'lang' => $this->get( 'userlang' ),
668 'dir' => $this->get( 'dir' )
670 $footerEnd = Html
::closeElement( 'div' );
674 foreach ( $validFooterIcons as $blockName => $footerIcons ) {
675 $html .= Html
::openElement( 'div', [
676 'id' => Sanitizer
::escapeIdForAttribute( "f-{$blockName}ico" ),
677 'class' => 'footer-icons'
679 foreach ( $footerIcons as $icon ) {
680 $html .= $this->getSkin()->makeFooterIcon( $icon );
682 $html .= Html
::closeElement( 'div' );
684 if ( count( $validFooterLinks ) > 0 ) {
685 $html .= Html
::openElement( 'ul', [ 'id' => 'f-list', 'class' => 'footer-places' ] );
686 foreach ( $validFooterLinks as $aLink ) {
687 $html .= Html
::rawElement(
689 [ 'id' => Sanitizer
::escapeIdForAttribute( $aLink ) ],
693 $html .= Html
::closeElement( 'ul' );
696 $html .= $this->getClear() . $footerEnd;
702 * Get a div with the core visualClear class, for clearing floats
704 * @return string html
707 protected function getClear() {
708 return Html
::element( 'div', [ 'class' => 'visualClear' ] );
712 * Get the suggested HTML for page status indicators: icons (or short text snippets) usually
713 * displayed in the top-right corner of the page, outside of the main content.
715 * Your skin may implement this differently, for example by handling some indicator names
716 * specially with a different UI. However, it is recommended to use a `<div class="mw-indicator"
717 * id="mw-indicator-<id>" />` as a wrapper element for each indicator, for better compatibility
718 * with extensions and user scripts.
720 * The raw data is available in `$this->data['indicators']` as an associative array (keys:
721 * identifiers, values: contents) internally ordered by keys.
723 * @return string HTML
726 public function getIndicators() {
727 $out = "<div class=\"mw-indicators mw-body-content\">\n";
728 foreach ( $this->data
['indicators'] as $id => $content ) {
729 $out .= Html
::rawElement(
732 'id' => Sanitizer
::escapeIdForAttribute( "mw-indicator-$id" ),
733 'class' => 'mw-indicator',
745 function printTrail() {
746 echo $this->getTrail();
750 * Get the basic end-page trail including bottomscripts, reporttime, and
751 * debug stuff. This should be called right before outputting the closing
752 * body and html tags.
754 * @return string|WrappedStringList HTML
757 public function getTrail() {
758 return WrappedString
::join( "\n", [
759 MWDebug
::getDebugHTML( $this->getSkin()->getContext() ),
760 $this->get( 'bottomscripts' ),
761 $this->get( 'reporttime' )