Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / skins / BaseTemplate.php
blobeedd2a114ec7c036b5e3175d8fccef315cd0760c
1 <?php
2 /**
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
18 * @file
21 /**
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 {
28 /**
29 * Get a Message object with its context set
31 * @param string $name Message name
32 * @return Message
34 public function getMsg( $name ) {
35 return $this->getSkin()->msg( $name );
38 function msg( $str ) {
39 echo $this->getMsg( $str )->escaped();
42 function msgHtml( $str ) {
43 echo $this->getMsg( $str )->text();
46 function msgWiki( $str ) {
47 echo $this->getMsg( $str )->parseAsBlock();
50 /**
51 * Create an array of common toolbox items from the data in the quicktemplate
52 * stored by SkinTemplate.
53 * The resulting array is built according to a format intended to be passed
54 * through makeListItem to generate the html.
55 * @return array
57 function getToolbox() {
59 $toolbox = array();
60 if ( isset( $this->data['nav_urls']['whatlinkshere'] )
61 && $this->data['nav_urls']['whatlinkshere']
62 ) {
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']
68 ) {
69 $toolbox['recentchangeslinked'] = $this->data['nav_urls']['recentchangeslinked'];
70 $toolbox['recentchangeslinked']['msg'] = 'recentchangeslinked-toolbox';
71 $toolbox['recentchangeslinked']['id'] = 't-recentchangeslinked';
73 if ( isset( $this->data['feeds'] ) && $this->data['feeds'] ) {
74 $toolbox['feeds']['id'] = 'feedlinks';
75 $toolbox['feeds']['links'] = array();
76 foreach ( $this->data['feeds'] as $key => $feed ) {
77 $toolbox['feeds']['links'][$key] = $feed;
78 $toolbox['feeds']['links'][$key]['id'] = "feed-$key";
79 $toolbox['feeds']['links'][$key]['rel'] = 'alternate';
80 $toolbox['feeds']['links'][$key]['type'] = "application/{$key}+xml";
81 $toolbox['feeds']['links'][$key]['class'] = 'feedlink';
84 foreach ( array( 'contributions', 'log', 'blockip', 'emailuser',
85 'userrights', 'upload', 'specialpages' ) as $special
86 ) {
87 if ( isset( $this->data['nav_urls'][$special] ) && $this->data['nav_urls'][$special] ) {
88 $toolbox[$special] = $this->data['nav_urls'][$special];
89 $toolbox[$special]['id'] = "t-$special";
92 if ( isset( $this->data['nav_urls']['print'] ) && $this->data['nav_urls']['print'] ) {
93 $toolbox['print'] = $this->data['nav_urls']['print'];
94 $toolbox['print']['id'] = 't-print';
95 $toolbox['print']['rel'] = 'alternate';
96 $toolbox['print']['msg'] = 'printableversion';
98 if ( isset( $this->data['nav_urls']['permalink'] ) && $this->data['nav_urls']['permalink'] ) {
99 $toolbox['permalink'] = $this->data['nav_urls']['permalink'];
100 if ( $toolbox['permalink']['href'] === '' ) {
101 unset( $toolbox['permalink']['href'] );
102 $toolbox['ispermalink']['tooltiponly'] = true;
103 $toolbox['ispermalink']['id'] = 't-ispermalink';
104 $toolbox['ispermalink']['msg'] = 'permalink';
105 } else {
106 $toolbox['permalink']['id'] = 't-permalink';
109 if ( isset( $this->data['nav_urls']['info'] ) && $this->data['nav_urls']['info'] ) {
110 $toolbox['info'] = $this->data['nav_urls']['info'];
111 $toolbox['info']['id'] = 't-info';
114 Hooks::run( 'BaseTemplateToolbox', array( &$this, &$toolbox ) );
115 return $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.
126 * @return array
128 function getPersonalTools() {
129 $personal_tools = array();
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.
134 $ptool = array(
135 'links' => array(
136 array( 'single-id' => "pt-$key" ),
138 'id' => "pt-$key",
140 if ( isset( $plink['active'] ) ) {
141 $ptool['active'] = $plink['active'];
143 foreach ( array( 'href', 'class', 'text', 'dir' ) 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 = array() ) {
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'] );
176 $boxes = array();
177 foreach ( $sidebar as $boxName => $content ) {
178 if ( $content === false ) {
179 continue;
181 switch ( $boxName ) {
182 case 'SEARCH':
183 // Search is a special case, skins should custom implement this
184 $boxes[$boxName] = array(
185 'id' => 'p-search',
186 'header' => $this->getMsg( 'search' )->text(),
187 'generated' => false,
188 'content' => true,
190 break;
191 case 'TOOLBOX':
192 $msgObj = $this->getMsg( 'toolbox' );
193 $boxes[$boxName] = array(
194 'id' => 'p-tb',
195 'header' => $msgObj->exists() ? $msgObj->text() : 'toolbox',
196 'generated' => false,
197 'content' => $this->getToolbox(),
199 break;
200 case 'LANGUAGES':
201 if ( $this->data['language_urls'] ) {
202 $msgObj = $this->getMsg( 'otherlanguages' );
203 $boxes[$boxName] = array(
204 'id' => 'p-lang',
205 'header' => $msgObj->exists() ? $msgObj->text() : 'otherlanguages',
206 'generated' => false,
207 'content' => $this->data['language_urls'],
210 break;
211 default:
212 $msgObj = $this->getMsg( $boxName );
213 $boxes[$boxName] = array(
214 'id' => "p-$boxName",
215 'header' => $msgObj->exists() ? $msgObj->text() : $boxName,
216 'generated' => true,
217 'content' => $content,
219 break;
223 // HACK: Compatibility with extensions still using SkinTemplateToolboxEnd
224 $hookContents = null;
225 if ( isset( $boxes['TOOLBOX'] ) ) {
226 ob_start();
227 // We pass an extra 'true' at the end so extensions using BaseTemplateToolbox
228 // can abort and avoid outputting double toolbox links
229 Hooks::run( 'SkinTemplateToolboxEnd', array( &$this, true ) );
230 $hookContents = ob_get_contents();
231 ob_end_clean();
232 if ( !trim( $hookContents ) ) {
233 $hookContents = null;
236 // END hack
238 if ( isset( $options['htmlOnly'] ) && $options['htmlOnly'] === true ) {
239 foreach ( $boxes as $boxName => $box ) {
240 if ( is_array( $box['content'] ) ) {
241 $content = '<ul>';
242 foreach ( $box['content'] as $key => $val ) {
243 $content .= "\n " . $this->makeListItem( $key, $val );
245 // HACK, shove the toolbox end onto the toolbox if we're rendering itself
246 if ( $hookContents ) {
247 $content .= "\n $hookContents";
249 // END hack
250 $content .= "\n</ul>\n";
251 $boxes[$boxName]['content'] = $content;
254 } else {
255 if ( $hookContents ) {
256 $boxes['TOOLBOXEND'] = array(
257 'id' => 'p-toolboxend',
258 'header' => $boxes['TOOLBOX']['header'],
259 'generated' => false,
260 'content' => "<ul>{$hookContents}</ul>",
262 // HACK: Make sure that TOOLBOXEND is sorted next to TOOLBOX
263 $boxes2 = array();
264 foreach ( $boxes as $key => $box ) {
265 if ( $key === 'TOOLBOXEND' ) {
266 continue;
268 $boxes2[$key] = $box;
269 if ( $key === 'TOOLBOX' ) {
270 $boxes2['TOOLBOXEND'] = $boxes['TOOLBOXEND'];
273 $boxes = $boxes2;
274 // END hack
278 return $boxes;
282 * @param string $name
284 protected function renderAfterPortlet( $name ) {
285 $content = '';
286 Hooks::run( 'BaseTemplateAfterPortlet', array( $this, $name, &$content ) );
288 if ( $content !== '' ) {
289 echo "<div class='after-portlet after-portlet-$name'>$content</div>";
295 * Makes a link, usually used by makeListItem to generate a link for an item
296 * in a list used in navigation lists, portlets, portals, sidebars, etc...
298 * @param string $key Usually a key from the list you are generating this
299 * link from.
300 * @param array $item Contains some of a specific set of keys.
302 * The text of the link will be generated either from the contents of the
303 * "text" key in the $item array, if a "msg" key is present a message by
304 * that name will be used, and if neither of those are set the $key will be
305 * used as a message name.
307 * If a "href" key is not present makeLink will just output htmlescaped text.
308 * The "href", "id", "class", "rel", and "type" keys are used as attributes
309 * for the link if present.
311 * If an "id" or "single-id" (if you don't want the actual id to be output
312 * on the link) is present it will be used to generate a tooltip and
313 * accesskey for the link.
315 * The keys "context" and "primary" are ignored; these keys are used
316 * internally by skins and are not supposed to be included in the HTML
317 * output.
319 * If you don't want an accesskey, set $item['tooltiponly'] = true;
321 * @param array $options Can be used to affect the output of a link.
322 * Possible options are:
323 * - 'text-wrapper' key to specify a list of elements to wrap the text of
324 * a link in. This should be an array of arrays containing a 'tag' and
325 * optionally an 'attributes' key. If you only have one element you don't
326 * need to wrap it in another array. eg: To use <a><span>...</span></a>
327 * in all links use array( 'text-wrapper' => array( 'tag' => 'span' ) )
328 * for your options.
329 * - 'link-class' key can be used to specify additional classes to apply
330 * to all links.
331 * - 'link-fallback' can be used to specify a tag to use instead of "<a>"
332 * if there is no link. eg: If you specify 'link-fallback' => 'span' than
333 * any non-link will output a "<span>" instead of just text.
335 * @return string
337 function makeLink( $key, $item, $options = array() ) {
338 if ( isset( $item['text'] ) ) {
339 $text = $item['text'];
340 } else {
341 $text = $this->translator->translate( isset( $item['msg'] ) ? $item['msg'] : $key );
344 $html = htmlspecialchars( $text );
346 if ( isset( $options['text-wrapper'] ) ) {
347 $wrapper = $options['text-wrapper'];
348 if ( isset( $wrapper['tag'] ) ) {
349 $wrapper = array( $wrapper );
351 while ( count( $wrapper ) > 0 ) {
352 $element = array_pop( $wrapper );
353 $html = Html::rawElement( $element['tag'], isset( $element['attributes'] )
354 ? $element['attributes']
355 : null, $html );
359 if ( isset( $item['href'] ) || isset( $options['link-fallback'] ) ) {
360 $attrs = $item;
361 foreach ( array( 'single-id', 'text', 'msg', 'tooltiponly', 'context', 'primary' ) as $k ) {
362 unset( $attrs[$k] );
365 if ( isset( $item['id'] ) && !isset( $item['single-id'] ) ) {
366 $item['single-id'] = $item['id'];
368 if ( isset( $item['single-id'] ) ) {
369 if ( isset( $item['tooltiponly'] ) && $item['tooltiponly'] ) {
370 $title = Linker::titleAttrib( $item['single-id'] );
371 if ( $title !== false ) {
372 $attrs['title'] = $title;
374 } else {
375 $tip = Linker::tooltipAndAccesskeyAttribs( $item['single-id'] );
376 if ( isset( $tip['title'] ) && $tip['title'] !== false ) {
377 $attrs['title'] = $tip['title'];
379 if ( isset( $tip['accesskey'] ) && $tip['accesskey'] !== false ) {
380 $attrs['accesskey'] = $tip['accesskey'];
384 if ( isset( $options['link-class'] ) ) {
385 if ( isset( $attrs['class'] ) ) {
386 $attrs['class'] .= " {$options['link-class']}";
387 } else {
388 $attrs['class'] = $options['link-class'];
391 $html = Html::rawElement( isset( $attrs['href'] )
392 ? 'a'
393 : $options['link-fallback'], $attrs, $html );
396 return $html;
400 * Generates a list item for a navigation, portlet, portal, sidebar... list
402 * @param string $key Usually a key from the list you are generating this link from.
403 * @param array $item Array of list item data containing some of a specific set of keys.
404 * The "id", "class" and "itemtitle" keys will be used as attributes for the list item,
405 * if "active" contains a value of true a "active" class will also be appended to class.
407 * @param array $options
409 * If you want something other than a "<li>" you can pass a tag name such as
410 * "tag" => "span" in the $options array to change the tag used.
411 * link/content data for the list item may come in one of two forms
412 * A "links" key may be used, in which case it should contain an array with
413 * a list of links to include inside the list item, see makeLink for the
414 * format of individual links array items.
416 * Otherwise the relevant keys from the list item $item array will be passed
417 * to makeLink instead. Note however that "id" and "class" are used by the
418 * list item directly so they will not be passed to makeLink
419 * (however the link will still support a tooltip and accesskey from it)
420 * If you need an id or class on a single link you should include a "links"
421 * array with just one link item inside of it. If you want to add a title
422 * to the list item itself, you can set "itemtitle" to the value.
423 * $options is also passed on to makeLink calls
425 * @return string
427 function makeListItem( $key, $item, $options = array() ) {
428 if ( isset( $item['links'] ) ) {
429 $links = array();
430 foreach ( $item['links'] as $linkKey => $link ) {
431 $links[] = $this->makeLink( $linkKey, $link, $options );
433 $html = implode( ' ', $links );
434 } else {
435 $link = $item;
436 // These keys are used by makeListItem and shouldn't be passed on to the link
437 foreach ( array( 'id', 'class', 'active', 'tag', 'itemtitle' ) as $k ) {
438 unset( $link[$k] );
440 if ( isset( $item['id'] ) && !isset( $item['single-id'] ) ) {
441 // The id goes on the <li> not on the <a> for single links
442 // but makeSidebarLink still needs to know what id to use when
443 // generating tooltips and accesskeys.
444 $link['single-id'] = $item['id'];
446 $html = $this->makeLink( $key, $link, $options );
449 $attrs = array();
450 foreach ( array( 'id', 'class' ) as $attr ) {
451 if ( isset( $item[$attr] ) ) {
452 $attrs[$attr] = $item[$attr];
455 if ( isset( $item['active'] ) && $item['active'] ) {
456 if ( !isset( $attrs['class'] ) ) {
457 $attrs['class'] = '';
459 $attrs['class'] .= ' active';
460 $attrs['class'] = trim( $attrs['class'] );
462 if ( isset( $item['itemtitle'] ) ) {
463 $attrs['title'] = $item['itemtitle'];
465 return Html::rawElement( isset( $options['tag'] ) ? $options['tag'] : 'li', $attrs, $html );
468 function makeSearchInput( $attrs = array() ) {
469 $realAttrs = array(
470 'type' => 'search',
471 'name' => 'search',
472 'placeholder' => wfMessage( 'searchsuggest-search' )->text(),
473 'value' => $this->get( 'search', '' ),
475 $realAttrs = array_merge( $realAttrs, Linker::tooltipAndAccesskeyAttribs( 'search' ), $attrs );
476 return Html::element( 'input', $realAttrs );
479 function makeSearchButton( $mode, $attrs = array() ) {
480 switch ( $mode ) {
481 case 'go':
482 case 'fulltext':
483 $realAttrs = array(
484 'type' => 'submit',
485 'name' => $mode,
486 'value' => $this->translator->translate(
487 $mode == 'go' ? 'searcharticle' : 'searchbutton' ),
489 $realAttrs = array_merge(
490 $realAttrs,
491 Linker::tooltipAndAccesskeyAttribs( "search-$mode" ),
492 $attrs
494 return Html::element( 'input', $realAttrs );
495 case 'image':
496 $buttonAttrs = array(
497 'type' => 'submit',
498 'name' => 'button',
500 $buttonAttrs = array_merge(
501 $buttonAttrs,
502 Linker::tooltipAndAccesskeyAttribs( 'search-fulltext' ),
503 $attrs
505 unset( $buttonAttrs['src'] );
506 unset( $buttonAttrs['alt'] );
507 unset( $buttonAttrs['width'] );
508 unset( $buttonAttrs['height'] );
509 $imgAttrs = array(
510 'src' => $attrs['src'],
511 'alt' => isset( $attrs['alt'] )
512 ? $attrs['alt']
513 : $this->translator->translate( 'searchbutton' ),
514 'width' => isset( $attrs['width'] ) ? $attrs['width'] : null,
515 'height' => isset( $attrs['height'] ) ? $attrs['height'] : null,
517 return Html::rawElement( 'button', $buttonAttrs, Html::element( 'img', $imgAttrs ) );
518 default:
519 throw new MWException( 'Unknown mode passed to BaseTemplate::makeSearchButton' );
524 * Returns an array of footerlinks trimmed down to only those footer links that
525 * are valid.
526 * If you pass "flat" as an option then the returned array will be a flat array
527 * of footer icons instead of a key/value array of footerlinks arrays broken
528 * up into categories.
529 * @param string $option
530 * @return array|mixed
532 function getFooterLinks( $option = null ) {
533 $footerlinks = $this->get( 'footerlinks' );
535 // Reduce footer links down to only those which are being used
536 $validFooterLinks = array();
537 foreach ( $footerlinks as $category => $links ) {
538 $validFooterLinks[$category] = array();
539 foreach ( $links as $link ) {
540 if ( isset( $this->data[$link] ) && $this->data[$link] ) {
541 $validFooterLinks[$category][] = $link;
544 if ( count( $validFooterLinks[$category] ) <= 0 ) {
545 unset( $validFooterLinks[$category] );
549 if ( $option == 'flat' ) {
550 // fold footerlinks into a single array using a bit of trickery
551 $validFooterLinks = call_user_func_array(
552 'array_merge',
553 array_values( $validFooterLinks )
557 return $validFooterLinks;
561 * Returns an array of footer icons filtered down by options relevant to how
562 * the skin wishes to display them.
563 * If you pass "icononly" as the option all footer icons which do not have an
564 * image icon set will be filtered out.
565 * If you pass "nocopyright" then MediaWiki's copyright icon will not be included
566 * in the list of footer icons. This is mostly useful for skins which only
567 * display the text from footericons instead of the images and don't want a
568 * duplicate copyright statement because footerlinks already rendered one.
569 * @param string $option
570 * @return string
572 function getFooterIcons( $option = null ) {
573 // Generate additional footer icons
574 $footericons = $this->get( 'footericons' );
576 if ( $option == 'icononly' ) {
577 // Unset any icons which don't have an image
578 foreach ( $footericons as &$footerIconsBlock ) {
579 foreach ( $footerIconsBlock as $footerIconKey => $footerIcon ) {
580 if ( !is_string( $footerIcon ) && !isset( $footerIcon['src'] ) ) {
581 unset( $footerIconsBlock[$footerIconKey] );
585 // Redo removal of any empty blocks
586 foreach ( $footericons as $footerIconsKey => &$footerIconsBlock ) {
587 if ( count( $footerIconsBlock ) <= 0 ) {
588 unset( $footericons[$footerIconsKey] );
591 } elseif ( $option == 'nocopyright' ) {
592 unset( $footericons['copyright']['copyright'] );
593 if ( count( $footericons['copyright'] ) <= 0 ) {
594 unset( $footericons['copyright'] );
598 return $footericons;
602 * Get the suggested HTML for page status indicators: icons (or short text snippets) usually
603 * displayed in the top-right corner of the page, outside of the main content.
605 * Your skin may implement this differently, for example by handling some indicator names
606 * specially with a different UI. However, it is recommended to use a `<div class="mw-indicator"
607 * id="mw-indicator-<id>" />` as a wrapper element for each indicator, for better compatibility
608 * with extensions and user scripts.
610 * The raw data is available in `$this->data['indicators']` as an associative array (keys:
611 * identifiers, values: contents) internally ordered by keys.
613 * @return string HTML
614 * @since 1.25
616 public function getIndicators() {
617 $out = "<div class=\"mw-indicators\">\n";
618 foreach ( $this->data['indicators'] as $id => $content ) {
619 $out .= Html::rawElement(
620 'div',
621 array(
622 'id' => Sanitizer::escapeId( "mw-indicator-$id" ),
623 'class' => 'mw-indicator',
625 $content
626 ) . "\n";
628 $out .= "</div>\n";
629 return $out;
633 * Output the basic end-page trail including bottomscripts, reporttime, and
634 * debug stuff. This should be called right before outputting the closing
635 * body and html tags.
637 function printTrail() { ?>
638 <?php echo MWDebug::getDebugHTML( $this->getSkin()->getContext() ); ?>
639 <?php $this->html( 'bottomscripts' ); /* JS call to runBodyOnloadHook */ ?>
640 <?php $this->html( 'reporttime' ) ?>
641 <?php