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
22 * New base template for a skin's template extended from QuickTemplate
23 * this class features helper methods that provide common ways of interacting
24 * with the data stored in the QuickTemplate
26 abstract class BaseTemplate
extends QuickTemplate
{
29 * Get a Message object with its context set
31 * @param string $name Message name
32 * @param ... $params Message params
35 public function getMsg( $name /* ... */ ) {
36 return call_user_func_array( [ $this->getSkin(), 'msg' ], func_get_args() );
39 function msg( $str ) {
40 echo $this->getMsg( $str )->escaped();
43 function msgHtml( $str ) {
44 echo $this->getMsg( $str )->text();
47 function msgWiki( $str ) {
48 echo $this->getMsg( $str )->parseAsBlock();
52 * Create an array of common toolbox items from the data in the quicktemplate
53 * stored by SkinTemplate.
54 * The resulting array is built according to a format intended to be passed
55 * through makeListItem to generate the html.
58 function getToolbox() {
60 if ( isset( $this->data
['nav_urls']['whatlinkshere'] )
61 && $this->data
['nav_urls']['whatlinkshere']
63 $toolbox['whatlinkshere'] = $this->data
['nav_urls']['whatlinkshere'];
64 $toolbox['whatlinkshere']['id'] = 't-whatlinkshere';
66 if ( isset( $this->data
['nav_urls']['recentchangeslinked'] )
67 && $this->data
['nav_urls']['recentchangeslinked']
69 $toolbox['recentchangeslinked'] = $this->data
['nav_urls']['recentchangeslinked'];
70 $toolbox['recentchangeslinked']['msg'] = 'recentchangeslinked-toolbox';
71 $toolbox['recentchangeslinked']['id'] = 't-recentchangeslinked';
72 $toolbox['recentchangeslinked']['rel'] = 'nofollow';
74 if ( isset( $this->data
['feeds'] ) && $this->data
['feeds'] ) {
75 $toolbox['feeds']['id'] = 'feedlinks';
76 $toolbox['feeds']['links'] = [];
77 foreach ( $this->data
['feeds'] as $key => $feed ) {
78 $toolbox['feeds']['links'][$key] = $feed;
79 $toolbox['feeds']['links'][$key]['id'] = "feed-$key";
80 $toolbox['feeds']['links'][$key]['rel'] = 'alternate';
81 $toolbox['feeds']['links'][$key]['type'] = "application/{$key}+xml";
82 $toolbox['feeds']['links'][$key]['class'] = 'feedlink';
85 foreach ( [ 'contributions', 'log', 'blockip', 'emailuser',
86 'userrights', 'upload', 'specialpages' ] as $special
88 if ( isset( $this->data
['nav_urls'][$special] ) && $this->data
['nav_urls'][$special] ) {
89 $toolbox[$special] = $this->data
['nav_urls'][$special];
90 $toolbox[$special]['id'] = "t-$special";
93 if ( isset( $this->data
['nav_urls']['print'] ) && $this->data
['nav_urls']['print'] ) {
94 $toolbox['print'] = $this->data
['nav_urls']['print'];
95 $toolbox['print']['id'] = 't-print';
96 $toolbox['print']['rel'] = 'alternate';
97 $toolbox['print']['msg'] = 'printableversion';
99 if ( isset( $this->data
['nav_urls']['permalink'] ) && $this->data
['nav_urls']['permalink'] ) {
100 $toolbox['permalink'] = $this->data
['nav_urls']['permalink'];
101 if ( $toolbox['permalink']['href'] === '' ) {
102 unset( $toolbox['permalink']['href'] );
103 $toolbox['ispermalink']['tooltiponly'] = true;
104 $toolbox['ispermalink']['id'] = 't-ispermalink';
105 $toolbox['ispermalink']['msg'] = 'permalink';
107 $toolbox['permalink']['id'] = 't-permalink';
110 if ( isset( $this->data
['nav_urls']['info'] ) && $this->data
['nav_urls']['info'] ) {
111 $toolbox['info'] = $this->data
['nav_urls']['info'];
112 $toolbox['info']['id'] = 't-info';
115 // Avoid PHP 7.1 warning from passing $this by reference
117 Hooks
::run( 'BaseTemplateToolbox', [ &$template, &$toolbox ] );
122 * Create an array of personal tools items from the data in the quicktemplate
123 * stored by SkinTemplate.
124 * The resulting array is built according to a format intended to be passed
125 * through makeListItem to generate the html.
126 * This is in reality the same list as already stored in personal_urls
127 * however it is reformatted so that you can just pass the individual items
128 * to makeListItem instead of hardcoding the element creation boilerplate.
131 function getPersonalTools() {
132 $personal_tools = [];
133 foreach ( $this->get( 'personal_urls' ) as $key => $plink ) {
134 # The class on a personal_urls item is meant to go on the <a> instead
135 # of the <li> so we have to use a single item "links" array instead
136 # of using most of the personal_url's keys directly.
139 [ 'single-id' => "pt-$key" ],
143 if ( isset( $plink['active'] ) ) {
144 $ptool['active'] = $plink['active'];
146 foreach ( [ 'href', 'class', 'text', 'dir', 'data' ] as $k ) {
147 if ( isset( $plink[$k] ) ) {
148 $ptool['links'][0][$k] = $plink[$k];
151 $personal_tools[$key] = $ptool;
153 return $personal_tools;
156 function getSidebar( $options = [] ) {
157 // Force the rendering of the following portals
158 $sidebar = $this->data
['sidebar'];
159 if ( !isset( $sidebar['SEARCH'] ) ) {
160 $sidebar['SEARCH'] = true;
162 if ( !isset( $sidebar['TOOLBOX'] ) ) {
163 $sidebar['TOOLBOX'] = true;
165 if ( !isset( $sidebar['LANGUAGES'] ) ) {
166 $sidebar['LANGUAGES'] = true;
169 if ( !isset( $options['search'] ) ||
$options['search'] !== true ) {
170 unset( $sidebar['SEARCH'] );
172 if ( isset( $options['toolbox'] ) && $options['toolbox'] === false ) {
173 unset( $sidebar['TOOLBOX'] );
175 if ( isset( $options['languages'] ) && $options['languages'] === false ) {
176 unset( $sidebar['LANGUAGES'] );
180 foreach ( $sidebar as $boxName => $content ) {
181 if ( $content === false ) {
184 switch ( $boxName ) {
186 // Search is a special case, skins should custom implement this
189 'header' => $this->getMsg( 'search' )->text(),
190 'generated' => false,
195 $msgObj = $this->getMsg( 'toolbox' );
198 'header' => $msgObj->exists() ?
$msgObj->text() : 'toolbox',
199 'generated' => false,
200 'content' => $this->getToolbox(),
204 if ( $this->data
['language_urls'] ) {
205 $msgObj = $this->getMsg( 'otherlanguages' );
208 'header' => $msgObj->exists() ?
$msgObj->text() : 'otherlanguages',
209 'generated' => false,
210 'content' => $this->data
['language_urls'],
215 $msgObj = $this->getMsg( $boxName );
217 'id' => "p-$boxName",
218 'header' => $msgObj->exists() ?
$msgObj->text() : $boxName,
220 'content' => $content,
226 // HACK: Compatibility with extensions still using SkinTemplateToolboxEnd
227 $hookContents = null;
228 if ( isset( $boxes['TOOLBOX'] ) ) {
230 // We pass an extra 'true' at the end so extensions using BaseTemplateToolbox
231 // can abort and avoid outputting double toolbox links
232 // Avoid PHP 7.1 warning from passing $this by reference
234 Hooks
::run( 'SkinTemplateToolboxEnd', [ &$template, true ] );
235 $hookContents = ob_get_contents();
237 if ( !trim( $hookContents ) ) {
238 $hookContents = null;
243 if ( isset( $options['htmlOnly'] ) && $options['htmlOnly'] === true ) {
244 foreach ( $boxes as $boxName => $box ) {
245 if ( is_array( $box['content'] ) ) {
247 foreach ( $box['content'] as $key => $val ) {
248 $content .= "\n " . $this->makeListItem( $key, $val );
250 // HACK, shove the toolbox end onto the toolbox if we're rendering itself
251 if ( $hookContents ) {
252 $content .= "\n $hookContents";
255 $content .= "\n</ul>\n";
256 $boxes[$boxName]['content'] = $content;
260 if ( $hookContents ) {
261 $boxes['TOOLBOXEND'] = [
262 'id' => 'p-toolboxend',
263 'header' => $boxes['TOOLBOX']['header'],
264 'generated' => false,
265 'content' => "<ul>{$hookContents}</ul>",
267 // HACK: Make sure that TOOLBOXEND is sorted next to TOOLBOX
269 foreach ( $boxes as $key => $box ) {
270 if ( $key === 'TOOLBOXEND' ) {
273 $boxes2[$key] = $box;
274 if ( $key === 'TOOLBOX' ) {
275 $boxes2['TOOLBOXEND'] = $boxes['TOOLBOXEND'];
287 * @param string $name
289 protected function renderAfterPortlet( $name ) {
291 Hooks
::run( 'BaseTemplateAfterPortlet', [ $this, $name, &$content ] );
293 if ( $content !== '' ) {
294 echo "<div class='after-portlet after-portlet-$name'>$content</div>";
299 * Makes a link, usually used by makeListItem to generate a link for an item
300 * in a list used in navigation lists, portlets, portals, sidebars, etc...
302 * @param string $key Usually a key from the list you are generating this
304 * @param array $item Contains some of a specific set of keys.
306 * The text of the link will be generated either from the contents of the
307 * "text" key in the $item array, if a "msg" key is present a message by
308 * that name will be used, and if neither of those are set the $key will be
309 * used as a message name.
311 * If a "href" key is not present makeLink will just output htmlescaped text.
312 * The "href", "id", "class", "rel", and "type" keys are used as attributes
313 * for the link if present.
315 * If an "id" or "single-id" (if you don't want the actual id to be output
316 * on the link) is present it will be used to generate a tooltip and
317 * accesskey for the link.
319 * The keys "context" and "primary" are ignored; these keys are used
320 * internally by skins and are not supposed to be included in the HTML
323 * If you don't want an accesskey, set $item['tooltiponly'] = true;
325 * If a "data" key is present, it must be an array, where the keys represent
326 * the data-xxx properties with their provided values. For example,
331 * will render as element properties:
332 * data-foo='1' data-bar='baz'
334 * @param array $options Can be used to affect the output of a link.
335 * Possible options are:
336 * - 'text-wrapper' key to specify a list of elements to wrap the text of
337 * a link in. This should be an array of arrays containing a 'tag' and
338 * optionally an 'attributes' key. If you only have one element you don't
339 * need to wrap it in another array. eg: To use <a><span>...</span></a>
340 * in all links use [ 'text-wrapper' => [ 'tag' => 'span' ] ]
342 * - 'link-class' key can be used to specify additional classes to apply
344 * - 'link-fallback' can be used to specify a tag to use instead of "<a>"
345 * if there is no link. eg: If you specify 'link-fallback' => 'span' than
346 * any non-link will output a "<span>" instead of just text.
350 function makeLink( $key, $item, $options = [] ) {
351 if ( isset( $item['text'] ) ) {
352 $text = $item['text'];
354 $text = $this->translator
->translate( isset( $item['msg'] ) ?
$item['msg'] : $key );
357 $html = htmlspecialchars( $text );
359 if ( isset( $options['text-wrapper'] ) ) {
360 $wrapper = $options['text-wrapper'];
361 if ( isset( $wrapper['tag'] ) ) {
362 $wrapper = [ $wrapper ];
364 while ( count( $wrapper ) > 0 ) {
365 $element = array_pop( $wrapper );
366 $html = Html
::rawElement( $element['tag'], isset( $element['attributes'] )
367 ?
$element['attributes']
372 if ( isset( $item['href'] ) ||
isset( $options['link-fallback'] ) ) {
374 foreach ( [ 'single-id', 'text', 'msg', 'tooltiponly', 'context', 'primary',
375 'tooltip-params' ] as $k ) {
379 if ( isset( $attrs['data'] ) ) {
380 foreach ( $attrs['data'] as $key => $value ) {
381 $attrs[ 'data-' . $key ] = $value;
383 unset( $attrs[ 'data' ] );
386 if ( isset( $item['id'] ) && !isset( $item['single-id'] ) ) {
387 $item['single-id'] = $item['id'];
391 if ( isset( $item['tooltip-params'] ) ) {
392 $tooltipParams = $item['tooltip-params'];
395 if ( isset( $item['single-id'] ) ) {
396 if ( isset( $item['tooltiponly'] ) && $item['tooltiponly'] ) {
397 $title = Linker
::titleAttrib( $item['single-id'], null, $tooltipParams );
398 if ( $title !== false ) {
399 $attrs['title'] = $title;
402 $tip = Linker
::tooltipAndAccesskeyAttribs( $item['single-id'], $tooltipParams );
403 if ( isset( $tip['title'] ) && $tip['title'] !== false ) {
404 $attrs['title'] = $tip['title'];
406 if ( isset( $tip['accesskey'] ) && $tip['accesskey'] !== false ) {
407 $attrs['accesskey'] = $tip['accesskey'];
411 if ( isset( $options['link-class'] ) ) {
412 if ( isset( $attrs['class'] ) ) {
413 $attrs['class'] .= " {$options['link-class']}";
415 $attrs['class'] = $options['link-class'];
418 $html = Html
::rawElement( isset( $attrs['href'] )
420 : $options['link-fallback'], $attrs, $html );
427 * Generates a list item for a navigation, portlet, portal, sidebar... list
429 * @param string $key Usually a key from the list you are generating this link from.
430 * @param array $item Array of list item data containing some of a specific set of keys.
431 * The "id", "class" and "itemtitle" keys will be used as attributes for the list item,
432 * if "active" contains a value of true a "active" class will also be appended to class.
434 * @param array $options
436 * If you want something other than a "<li>" you can pass a tag name such as
437 * "tag" => "span" in the $options array to change the tag used.
438 * link/content data for the list item may come in one of two forms
439 * A "links" key may be used, in which case it should contain an array with
440 * a list of links to include inside the list item, see makeLink for the
441 * format of individual links array items.
443 * Otherwise the relevant keys from the list item $item array will be passed
444 * to makeLink instead. Note however that "id" and "class" are used by the
445 * list item directly so they will not be passed to makeLink
446 * (however the link will still support a tooltip and accesskey from it)
447 * If you need an id or class on a single link you should include a "links"
448 * array with just one link item inside of it. You can also set "link-class" in
449 * $item to set a class on the link itself. If you want to add a title
450 * to the list item itself, you can set "itemtitle" to the value.
451 * $options is also passed on to makeLink calls
455 function makeListItem( $key, $item, $options = [] ) {
456 if ( isset( $item['links'] ) ) {
458 foreach ( $item['links'] as $linkKey => $link ) {
459 $links[] = $this->makeLink( $linkKey, $link, $options );
461 $html = implode( ' ', $links );
464 // These keys are used by makeListItem and shouldn't be passed on to the link
465 foreach ( [ 'id', 'class', 'active', 'tag', 'itemtitle' ] as $k ) {
468 if ( isset( $item['id'] ) && !isset( $item['single-id'] ) ) {
469 // The id goes on the <li> not on the <a> for single links
470 // but makeSidebarLink still needs to know what id to use when
471 // generating tooltips and accesskeys.
472 $link['single-id'] = $item['id'];
474 if ( isset( $link['link-class'] ) ) {
475 // link-class should be set on the <a> itself,
476 // so pass it in as 'class'
477 $link['class'] = $link['link-class'];
478 unset( $link['link-class'] );
480 $html = $this->makeLink( $key, $link, $options );
484 foreach ( [ 'id', 'class' ] as $attr ) {
485 if ( isset( $item[$attr] ) ) {
486 $attrs[$attr] = $item[$attr];
489 if ( isset( $item['active'] ) && $item['active'] ) {
490 if ( !isset( $attrs['class'] ) ) {
491 $attrs['class'] = '';
493 $attrs['class'] .= ' active';
494 $attrs['class'] = trim( $attrs['class'] );
496 if ( isset( $item['itemtitle'] ) ) {
497 $attrs['title'] = $item['itemtitle'];
499 return Html
::rawElement( isset( $options['tag'] ) ?
$options['tag'] : 'li', $attrs, $html );
502 function makeSearchInput( $attrs = [] ) {
506 'placeholder' => wfMessage( 'searchsuggest-search' )->text(),
507 'value' => $this->get( 'search', '' ),
509 $realAttrs = array_merge( $realAttrs, Linker
::tooltipAndAccesskeyAttribs( 'search' ), $attrs );
510 return Html
::element( 'input', $realAttrs );
513 function makeSearchButton( $mode, $attrs = [] ) {
520 'value' => $this->translator
->translate(
521 $mode == 'go' ?
'searcharticle' : 'searchbutton' ),
523 $realAttrs = array_merge(
525 Linker
::tooltipAndAccesskeyAttribs( "search-$mode" ),
528 return Html
::element( 'input', $realAttrs );
534 $buttonAttrs = array_merge(
536 Linker
::tooltipAndAccesskeyAttribs( 'search-fulltext' ),
539 unset( $buttonAttrs['src'] );
540 unset( $buttonAttrs['alt'] );
541 unset( $buttonAttrs['width'] );
542 unset( $buttonAttrs['height'] );
544 'src' => $attrs['src'],
545 'alt' => isset( $attrs['alt'] )
547 : $this->translator
->translate( 'searchbutton' ),
548 'width' => isset( $attrs['width'] ) ?
$attrs['width'] : null,
549 'height' => isset( $attrs['height'] ) ?
$attrs['height'] : null,
551 return Html
::rawElement( 'button', $buttonAttrs, Html
::element( 'img', $imgAttrs ) );
553 throw new MWException( 'Unknown mode passed to BaseTemplate::makeSearchButton' );
558 * Returns an array of footerlinks trimmed down to only those footer links that
560 * If you pass "flat" as an option then the returned array will be a flat array
561 * of footer icons instead of a key/value array of footerlinks arrays broken
562 * up into categories.
563 * @param string $option
564 * @return array|mixed
566 function getFooterLinks( $option = null ) {
567 $footerlinks = $this->get( 'footerlinks' );
569 // Reduce footer links down to only those which are being used
570 $validFooterLinks = [];
571 foreach ( $footerlinks as $category => $links ) {
572 $validFooterLinks[$category] = [];
573 foreach ( $links as $link ) {
574 if ( isset( $this->data
[$link] ) && $this->data
[$link] ) {
575 $validFooterLinks[$category][] = $link;
578 if ( count( $validFooterLinks[$category] ) <= 0 ) {
579 unset( $validFooterLinks[$category] );
583 if ( $option == 'flat' ) {
584 // fold footerlinks into a single array using a bit of trickery
585 $validFooterLinks = call_user_func_array(
587 array_values( $validFooterLinks )
591 return $validFooterLinks;
595 * Returns an array of footer icons filtered down by options relevant to how
596 * the skin wishes to display them.
597 * If you pass "icononly" as the option all footer icons which do not have an
598 * image icon set will be filtered out.
599 * If you pass "nocopyright" then MediaWiki's copyright icon will not be included
600 * in the list of footer icons. This is mostly useful for skins which only
601 * display the text from footericons instead of the images and don't want a
602 * duplicate copyright statement because footerlinks already rendered one.
603 * @param string $option
606 function getFooterIcons( $option = null ) {
607 // Generate additional footer icons
608 $footericons = $this->get( 'footericons' );
610 if ( $option == 'icononly' ) {
611 // Unset any icons which don't have an image
612 foreach ( $footericons as &$footerIconsBlock ) {
613 foreach ( $footerIconsBlock as $footerIconKey => $footerIcon ) {
614 if ( !is_string( $footerIcon ) && !isset( $footerIcon['src'] ) ) {
615 unset( $footerIconsBlock[$footerIconKey] );
619 // Redo removal of any empty blocks
620 foreach ( $footericons as $footerIconsKey => &$footerIconsBlock ) {
621 if ( count( $footerIconsBlock ) <= 0 ) {
622 unset( $footericons[$footerIconsKey] );
625 } elseif ( $option == 'nocopyright' ) {
626 unset( $footericons['copyright']['copyright'] );
627 if ( count( $footericons['copyright'] ) <= 0 ) {
628 unset( $footericons['copyright'] );
636 * Get the suggested HTML for page status indicators: icons (or short text snippets) usually
637 * displayed in the top-right corner of the page, outside of the main content.
639 * Your skin may implement this differently, for example by handling some indicator names
640 * specially with a different UI. However, it is recommended to use a `<div class="mw-indicator"
641 * id="mw-indicator-<id>" />` as a wrapper element for each indicator, for better compatibility
642 * with extensions and user scripts.
644 * The raw data is available in `$this->data['indicators']` as an associative array (keys:
645 * identifiers, values: contents) internally ordered by keys.
647 * @return string HTML
650 public function getIndicators() {
651 $out = "<div class=\"mw-indicators\">\n";
652 foreach ( $this->data
['indicators'] as $id => $content ) {
653 $out .= Html
::rawElement(
656 'id' => Sanitizer
::escapeId( "mw-indicator-$id" ),
657 'class' => 'mw-indicator',
667 * Output the basic end-page trail including bottomscripts, reporttime, and
668 * debug stuff. This should be called right before outputting the closing
669 * body and html tags.
671 function printTrail() {
673 <?php
echo MWDebug
::getDebugHTML( $this->getSkin()->getContext() ); ?
>
674 <?php
$this->html( 'bottomscripts' ); /* JS call to runBodyOnloadHook */ ?
>
675 <?php
$this->html( 'reporttime' ) ?
>