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 Hooks
::run( 'BaseTemplateToolbox', [ &$this, &$toolbox ] );
120 * Create an array of personal tools items from the data in the quicktemplate
121 * stored by SkinTemplate.
122 * The resulting array is built according to a format intended to be passed
123 * through makeListItem to generate the html.
124 * This is in reality the same list as already stored in personal_urls
125 * however it is reformatted so that you can just pass the individual items
126 * to makeListItem instead of hardcoding the element creation boilerplate.
129 function getPersonalTools() {
130 $personal_tools = [];
131 foreach ( $this->get( 'personal_urls' ) as $key => $plink ) {
132 # The class on a personal_urls item is meant to go on the <a> instead
133 # of the <li> so we have to use a single item "links" array instead
134 # of using most of the personal_url's keys directly.
137 [ 'single-id' => "pt-$key" ],
141 if ( isset( $plink['active'] ) ) {
142 $ptool['active'] = $plink['active'];
144 foreach ( [ 'href', 'class', 'text', 'dir', 'data' ] as $k ) {
145 if ( isset( $plink[$k] ) ) {
146 $ptool['links'][0][$k] = $plink[$k];
149 $personal_tools[$key] = $ptool;
151 return $personal_tools;
154 function getSidebar( $options = [] ) {
155 // Force the rendering of the following portals
156 $sidebar = $this->data
['sidebar'];
157 if ( !isset( $sidebar['SEARCH'] ) ) {
158 $sidebar['SEARCH'] = true;
160 if ( !isset( $sidebar['TOOLBOX'] ) ) {
161 $sidebar['TOOLBOX'] = true;
163 if ( !isset( $sidebar['LANGUAGES'] ) ) {
164 $sidebar['LANGUAGES'] = true;
167 if ( !isset( $options['search'] ) ||
$options['search'] !== true ) {
168 unset( $sidebar['SEARCH'] );
170 if ( isset( $options['toolbox'] ) && $options['toolbox'] === false ) {
171 unset( $sidebar['TOOLBOX'] );
173 if ( isset( $options['languages'] ) && $options['languages'] === false ) {
174 unset( $sidebar['LANGUAGES'] );
178 foreach ( $sidebar as $boxName => $content ) {
179 if ( $content === false ) {
182 switch ( $boxName ) {
184 // Search is a special case, skins should custom implement this
187 'header' => $this->getMsg( 'search' )->text(),
188 'generated' => false,
193 $msgObj = $this->getMsg( 'toolbox' );
196 'header' => $msgObj->exists() ?
$msgObj->text() : 'toolbox',
197 'generated' => false,
198 'content' => $this->getToolbox(),
202 if ( $this->data
['language_urls'] ) {
203 $msgObj = $this->getMsg( 'otherlanguages' );
206 'header' => $msgObj->exists() ?
$msgObj->text() : 'otherlanguages',
207 'generated' => false,
208 'content' => $this->data
['language_urls'],
213 $msgObj = $this->getMsg( $boxName );
215 'id' => "p-$boxName",
216 'header' => $msgObj->exists() ?
$msgObj->text() : $boxName,
218 'content' => $content,
224 // HACK: Compatibility with extensions still using SkinTemplateToolboxEnd
225 $hookContents = null;
226 if ( isset( $boxes['TOOLBOX'] ) ) {
228 // We pass an extra 'true' at the end so extensions using BaseTemplateToolbox
229 // can abort and avoid outputting double toolbox links
230 Hooks
::run( 'SkinTemplateToolboxEnd', [ &$this, true ] );
231 $hookContents = ob_get_contents();
233 if ( !trim( $hookContents ) ) {
234 $hookContents = null;
239 if ( isset( $options['htmlOnly'] ) && $options['htmlOnly'] === true ) {
240 foreach ( $boxes as $boxName => $box ) {
241 if ( is_array( $box['content'] ) ) {
243 foreach ( $box['content'] as $key => $val ) {
244 $content .= "\n " . $this->makeListItem( $key, $val );
246 // HACK, shove the toolbox end onto the toolbox if we're rendering itself
247 if ( $hookContents ) {
248 $content .= "\n $hookContents";
251 $content .= "\n</ul>\n";
252 $boxes[$boxName]['content'] = $content;
256 if ( $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'];
283 * @param string $name
285 protected function renderAfterPortlet( $name ) {
287 Hooks
::run( 'BaseTemplateAfterPortlet', [ $this, $name, &$content ] );
289 if ( $content !== '' ) {
290 echo "<div class='after-portlet after-portlet-$name'>$content</div>";
296 * Makes a link, usually used by makeListItem to generate a link for an item
297 * in a list used in navigation lists, portlets, portals, sidebars, etc...
299 * @param string $key Usually a key from the list you are generating this
301 * @param array $item Contains some of a specific set of keys.
303 * The text of the link will be generated either from the contents of the
304 * "text" key in the $item array, if a "msg" key is present a message by
305 * that name will be used, and if neither of those are set the $key will be
306 * used as a message name.
308 * If a "href" key is not present makeLink will just output htmlescaped text.
309 * The "href", "id", "class", "rel", and "type" keys are used as attributes
310 * for the link if present.
312 * If an "id" or "single-id" (if you don't want the actual id to be output
313 * on the link) is present it will be used to generate a tooltip and
314 * accesskey for the link.
316 * The keys "context" and "primary" are ignored; these keys are used
317 * internally by skins and are not supposed to be included in the HTML
320 * If you don't want an accesskey, set $item['tooltiponly'] = true;
322 * If a "data" key is present, it must be an array, where the keys represent
323 * the data-xxx properties with their provided values. For example,
328 * will render as element properties:
329 * data-foo='1' data-bar='baz'
331 * @param array $options Can be used to affect the output of a link.
332 * Possible options are:
333 * - 'text-wrapper' key to specify a list of elements to wrap the text of
334 * a link in. This should be an array of arrays containing a 'tag' and
335 * optionally an 'attributes' key. If you only have one element you don't
336 * need to wrap it in another array. eg: To use <a><span>...</span></a>
337 * in all links use [ 'text-wrapper' => [ 'tag' => 'span' ] ]
339 * - 'link-class' key can be used to specify additional classes to apply
341 * - 'link-fallback' can be used to specify a tag to use instead of "<a>"
342 * if there is no link. eg: If you specify 'link-fallback' => 'span' than
343 * any non-link will output a "<span>" instead of just text.
347 function makeLink( $key, $item, $options = [] ) {
348 if ( isset( $item['text'] ) ) {
349 $text = $item['text'];
351 $text = $this->translator
->translate( isset( $item['msg'] ) ?
$item['msg'] : $key );
354 $html = htmlspecialchars( $text );
356 if ( isset( $options['text-wrapper'] ) ) {
357 $wrapper = $options['text-wrapper'];
358 if ( isset( $wrapper['tag'] ) ) {
359 $wrapper = [ $wrapper ];
361 while ( count( $wrapper ) > 0 ) {
362 $element = array_pop( $wrapper );
363 $html = Html
::rawElement( $element['tag'], isset( $element['attributes'] )
364 ?
$element['attributes']
369 if ( isset( $item['href'] ) ||
isset( $options['link-fallback'] ) ) {
371 foreach ( [ 'single-id', 'text', 'msg', 'tooltiponly', 'context', 'primary',
372 'tooltip-params' ] as $k ) {
376 if ( isset( $attrs['data'] ) ) {
377 foreach ( $attrs['data'] as $key => $value ) {
378 $attrs[ 'data-' . $key ] = $value;
380 unset( $attrs[ 'data' ] );
383 if ( isset( $item['id'] ) && !isset( $item['single-id'] ) ) {
384 $item['single-id'] = $item['id'];
388 if ( isset( $item['tooltip-params'] ) ) {
389 $tooltipParams = $item['tooltip-params'];
392 if ( isset( $item['single-id'] ) ) {
393 if ( isset( $item['tooltiponly'] ) && $item['tooltiponly'] ) {
394 $title = Linker
::titleAttrib( $item['single-id'], null, $tooltipParams );
395 if ( $title !== false ) {
396 $attrs['title'] = $title;
399 $tip = Linker
::tooltipAndAccesskeyAttribs( $item['single-id'], $tooltipParams );
400 if ( isset( $tip['title'] ) && $tip['title'] !== false ) {
401 $attrs['title'] = $tip['title'];
403 if ( isset( $tip['accesskey'] ) && $tip['accesskey'] !== false ) {
404 $attrs['accesskey'] = $tip['accesskey'];
408 if ( isset( $options['link-class'] ) ) {
409 if ( isset( $attrs['class'] ) ) {
410 $attrs['class'] .= " {$options['link-class']}";
412 $attrs['class'] = $options['link-class'];
415 $html = Html
::rawElement( isset( $attrs['href'] )
417 : $options['link-fallback'], $attrs, $html );
424 * Generates a list item for a navigation, portlet, portal, sidebar... list
426 * @param string $key Usually a key from the list you are generating this link from.
427 * @param array $item Array of list item data containing some of a specific set of keys.
428 * The "id", "class" and "itemtitle" keys will be used as attributes for the list item,
429 * if "active" contains a value of true a "active" class will also be appended to class.
431 * @param array $options
433 * If you want something other than a "<li>" you can pass a tag name such as
434 * "tag" => "span" in the $options array to change the tag used.
435 * link/content data for the list item may come in one of two forms
436 * A "links" key may be used, in which case it should contain an array with
437 * a list of links to include inside the list item, see makeLink for the
438 * format of individual links array items.
440 * Otherwise the relevant keys from the list item $item array will be passed
441 * to makeLink instead. Note however that "id" and "class" are used by the
442 * list item directly so they will not be passed to makeLink
443 * (however the link will still support a tooltip and accesskey from it)
444 * If you need an id or class on a single link you should include a "links"
445 * array with just one link item inside of it. You can also set "link-class" in
446 * $item to set a class on the link itself. If you want to add a title
447 * to the list item itself, you can set "itemtitle" to the value.
448 * $options is also passed on to makeLink calls
452 function makeListItem( $key, $item, $options = [] ) {
453 if ( isset( $item['links'] ) ) {
455 foreach ( $item['links'] as $linkKey => $link ) {
456 $links[] = $this->makeLink( $linkKey, $link, $options );
458 $html = implode( ' ', $links );
461 // These keys are used by makeListItem and shouldn't be passed on to the link
462 foreach ( [ 'id', 'class', 'active', 'tag', 'itemtitle' ] as $k ) {
465 if ( isset( $item['id'] ) && !isset( $item['single-id'] ) ) {
466 // The id goes on the <li> not on the <a> for single links
467 // but makeSidebarLink still needs to know what id to use when
468 // generating tooltips and accesskeys.
469 $link['single-id'] = $item['id'];
471 if ( isset( $link['link-class'] ) ) {
472 // link-class should be set on the <a> itself,
473 // so pass it in as 'class'
474 $link['class'] = $link['link-class'];
475 unset( $link['link-class'] );
477 $html = $this->makeLink( $key, $link, $options );
481 foreach ( [ 'id', 'class' ] as $attr ) {
482 if ( isset( $item[$attr] ) ) {
483 $attrs[$attr] = $item[$attr];
486 if ( isset( $item['active'] ) && $item['active'] ) {
487 if ( !isset( $attrs['class'] ) ) {
488 $attrs['class'] = '';
490 $attrs['class'] .= ' active';
491 $attrs['class'] = trim( $attrs['class'] );
493 if ( isset( $item['itemtitle'] ) ) {
494 $attrs['title'] = $item['itemtitle'];
496 return Html
::rawElement( isset( $options['tag'] ) ?
$options['tag'] : 'li', $attrs, $html );
499 function makeSearchInput( $attrs = [] ) {
503 'placeholder' => wfMessage( 'searchsuggest-search' )->text(),
504 'value' => $this->get( 'search', '' ),
506 $realAttrs = array_merge( $realAttrs, Linker
::tooltipAndAccesskeyAttribs( 'search' ), $attrs );
507 return Html
::element( 'input', $realAttrs );
510 function makeSearchButton( $mode, $attrs = [] ) {
517 'value' => $this->translator
->translate(
518 $mode == 'go' ?
'searcharticle' : 'searchbutton' ),
520 $realAttrs = array_merge(
522 Linker
::tooltipAndAccesskeyAttribs( "search-$mode" ),
525 return Html
::element( 'input', $realAttrs );
531 $buttonAttrs = array_merge(
533 Linker
::tooltipAndAccesskeyAttribs( 'search-fulltext' ),
536 unset( $buttonAttrs['src'] );
537 unset( $buttonAttrs['alt'] );
538 unset( $buttonAttrs['width'] );
539 unset( $buttonAttrs['height'] );
541 'src' => $attrs['src'],
542 'alt' => isset( $attrs['alt'] )
544 : $this->translator
->translate( 'searchbutton' ),
545 'width' => isset( $attrs['width'] ) ?
$attrs['width'] : null,
546 'height' => isset( $attrs['height'] ) ?
$attrs['height'] : null,
548 return Html
::rawElement( 'button', $buttonAttrs, Html
::element( 'img', $imgAttrs ) );
550 throw new MWException( 'Unknown mode passed to BaseTemplate::makeSearchButton' );
555 * Returns an array of footerlinks trimmed down to only those footer links that
557 * If you pass "flat" as an option then the returned array will be a flat array
558 * of footer icons instead of a key/value array of footerlinks arrays broken
559 * up into categories.
560 * @param string $option
561 * @return array|mixed
563 function getFooterLinks( $option = null ) {
564 $footerlinks = $this->get( 'footerlinks' );
566 // Reduce footer links down to only those which are being used
567 $validFooterLinks = [];
568 foreach ( $footerlinks as $category => $links ) {
569 $validFooterLinks[$category] = [];
570 foreach ( $links as $link ) {
571 if ( isset( $this->data
[$link] ) && $this->data
[$link] ) {
572 $validFooterLinks[$category][] = $link;
575 if ( count( $validFooterLinks[$category] ) <= 0 ) {
576 unset( $validFooterLinks[$category] );
580 if ( $option == 'flat' ) {
581 // fold footerlinks into a single array using a bit of trickery
582 $validFooterLinks = call_user_func_array(
584 array_values( $validFooterLinks )
588 return $validFooterLinks;
592 * Returns an array of footer icons filtered down by options relevant to how
593 * the skin wishes to display them.
594 * If you pass "icononly" as the option all footer icons which do not have an
595 * image icon set will be filtered out.
596 * If you pass "nocopyright" then MediaWiki's copyright icon will not be included
597 * in the list of footer icons. This is mostly useful for skins which only
598 * display the text from footericons instead of the images and don't want a
599 * duplicate copyright statement because footerlinks already rendered one.
600 * @param string $option
603 function getFooterIcons( $option = null ) {
604 // Generate additional footer icons
605 $footericons = $this->get( 'footericons' );
607 if ( $option == 'icononly' ) {
608 // Unset any icons which don't have an image
609 foreach ( $footericons as &$footerIconsBlock ) {
610 foreach ( $footerIconsBlock as $footerIconKey => $footerIcon ) {
611 if ( !is_string( $footerIcon ) && !isset( $footerIcon['src'] ) ) {
612 unset( $footerIconsBlock[$footerIconKey] );
616 // Redo removal of any empty blocks
617 foreach ( $footericons as $footerIconsKey => &$footerIconsBlock ) {
618 if ( count( $footerIconsBlock ) <= 0 ) {
619 unset( $footericons[$footerIconsKey] );
622 } elseif ( $option == 'nocopyright' ) {
623 unset( $footericons['copyright']['copyright'] );
624 if ( count( $footericons['copyright'] ) <= 0 ) {
625 unset( $footericons['copyright'] );
633 * Get the suggested HTML for page status indicators: icons (or short text snippets) usually
634 * displayed in the top-right corner of the page, outside of the main content.
636 * Your skin may implement this differently, for example by handling some indicator names
637 * specially with a different UI. However, it is recommended to use a `<div class="mw-indicator"
638 * id="mw-indicator-<id>" />` as a wrapper element for each indicator, for better compatibility
639 * with extensions and user scripts.
641 * The raw data is available in `$this->data['indicators']` as an associative array (keys:
642 * identifiers, values: contents) internally ordered by keys.
644 * @return string HTML
647 public function getIndicators() {
648 $out = "<div class=\"mw-indicators\">\n";
649 foreach ( $this->data
['indicators'] as $id => $content ) {
650 $out .= Html
::rawElement(
653 'id' => Sanitizer
::escapeId( "mw-indicator-$id" ),
654 'class' => 'mw-indicator',
664 * Output the basic end-page trail including bottomscripts, reporttime, and
665 * debug stuff. This should be called right before outputting the closing
666 * body and html tags.
668 function printTrail() {
670 <?php
echo MWDebug
::getDebugHTML( $this->getSkin()->getContext() ); ?
>
671 <?php
$this->html( 'bottomscripts' ); /* JS call to runBodyOnloadHook */ ?
>
672 <?php
$this->html( 'reporttime' ) ?
>