Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / includes / SkinTemplate.php
blobe3a6ccf3e9c68c28a4c2bad549f9fb3f236d722d
1 <?php
2 /**
3 * Base class for template-based skins.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 /**
24 * Wrapper object for MediaWiki's localization functions,
25 * to be passed to the template engine.
27 * @private
28 * @ingroup Skins
30 class MediaWiki_I18N {
31 var $_context = array();
33 function set( $varName, $value ) {
34 $this->_context[$varName] = $value;
37 function translate( $value ) {
38 wfProfileIn( __METHOD__ );
40 // Hack for i18n:attributes in PHPTAL 1.0.0 dev version as of 2004-10-23
41 $value = preg_replace( '/^string:/', '', $value );
43 $value = wfMessage( $value )->text();
44 // interpolate variables
45 $m = array();
46 while ( preg_match( '/\$([0-9]*?)/sm', $value, $m ) ) {
47 list( $src, $var ) = $m;
48 wfSuppressWarnings();
49 $varValue = $this->_context[$var];
50 wfRestoreWarnings();
51 $value = str_replace( $src, $varValue, $value );
53 wfProfileOut( __METHOD__ );
54 return $value;
58 /**
59 * Template-filler skin base class
60 * Formerly generic PHPTal (http://phptal.sourceforge.net/) skin
61 * Based on Brion's smarty skin
62 * @copyright Copyright © Gabriel Wicke -- http://www.aulinx.de/
64 * @todo Needs some serious refactoring into functions that correspond
65 * to the computations individual esi snippets need. Most importantly no body
66 * parsing for most of those of course.
68 * @ingroup Skins
70 class SkinTemplate extends Skin {
71 /**#@+
72 * @private
75 /**
76 * Name of our skin, it probably needs to be all lower case. Child classes
77 * should override the default.
79 var $skinname = 'monobook';
81 /**
82 * Stylesheets set to use. Subdirectory in skins/ where various stylesheets
83 * are located. Child classes should override the default.
85 var $stylename = 'monobook';
87 /**
88 * For QuickTemplate, the name of the subclass which will actually fill the
89 * template. Child classes should override the default.
91 var $template = 'QuickTemplate';
93 /**
94 * Whether this skin use OutputPage::headElement() to generate the "<head>"
95 * tag
97 var $useHeadElement = false;
99 /**#@-*/
102 * Add specific styles for this skin
104 * @param $out OutputPage
106 function setupSkinUserCss( OutputPage $out ) {
107 $out->addModuleStyles( array( 'mediawiki.legacy.shared', 'mediawiki.legacy.commonPrint' ) );
111 * Create the template engine object; we feed it a bunch of data
112 * and eventually it spits out some HTML. Should have interface
113 * roughly equivalent to PHPTAL 0.7.
115 * @param $classname String
116 * @param string $repository subdirectory where we keep template files
117 * @param $cache_dir string
118 * @return QuickTemplate
119 * @private
121 function setupTemplate( $classname, $repository = false, $cache_dir = false ) {
122 return new $classname();
126 * Generates array of language links for the current page
128 * @return array
129 * @public
131 public function getLanguages() {
132 global $wgHideInterlanguageLinks;
133 $out = $this->getOutput();
135 # Language links
136 $language_urls = array();
138 if ( !$wgHideInterlanguageLinks ) {
139 foreach ( $out->getLanguageLinks() as $languageLinkText ) {
140 $languageLinkParts = explode( ':', $languageLinkText, 2 );
141 $class = 'interwiki-' . $languageLinkParts[0];
142 unset( $languageLinkParts );
143 $languageLinkTitle = Title::newFromText( $languageLinkText );
144 if ( $languageLinkTitle ) {
145 $ilInterwikiCode = $languageLinkTitle->getInterwiki();
146 $ilLangName = Language::fetchLanguageName( $ilInterwikiCode );
148 if ( strval( $ilLangName ) === '' ) {
149 $ilLangName = $languageLinkText;
150 } else {
151 $ilLangName = $this->formatLanguageName( $ilLangName );
154 $language_urls[] = array(
155 'href' => $languageLinkTitle->getFullURL(),
156 'text' => $ilLangName,
157 'title' => $languageLinkTitle->getText(),
158 'class' => $class,
159 'lang' => $ilInterwikiCode,
160 'hreflang' => $ilInterwikiCode
165 return $language_urls;
169 * initialize various variables and generate the template
171 * @param $out OutputPage
173 function outputPage( OutputPage $out = null ) {
174 global $wgContLang;
175 global $wgScript, $wgStylePath;
176 global $wgMimeType, $wgJsMimeType;
177 global $wgXhtmlNamespaces, $wgHtml5Version;
178 global $wgDisableCounters, $wgSitename, $wgLogo;
179 global $wgMaxCredits, $wgShowCreditsIfMax;
180 global $wgPageShowWatchingUsers;
181 global $wgArticlePath, $wgScriptPath, $wgServer;
183 wfProfileIn( __METHOD__ );
184 Profiler::instance()->setTemplated( true );
186 $oldContext = null;
187 if ( $out !== null ) {
188 // @todo Add wfDeprecated in 1.20
189 $oldContext = $this->getContext();
190 $this->setContext( $out->getContext() );
193 $out = $this->getOutput();
194 $request = $this->getRequest();
195 $user = $this->getUser();
196 $title = $this->getTitle();
198 wfProfileIn( __METHOD__ . '-init' );
199 $this->initPage( $out );
201 $tpl = $this->setupTemplate( $this->template, 'skins' );
202 wfProfileOut( __METHOD__ . '-init' );
204 wfProfileIn( __METHOD__ . '-stuff' );
205 $this->thispage = $title->getPrefixedDBkey();
206 $this->titletxt = $title->getPrefixedText();
207 $this->userpage = $user->getUserPage()->getPrefixedText();
208 $query = array();
209 if ( !$request->wasPosted() ) {
210 $query = $request->getValues();
211 unset( $query['title'] );
212 unset( $query['returnto'] );
213 unset( $query['returntoquery'] );
215 $this->thisquery = wfArrayToCgi( $query );
216 $this->loggedin = $user->isLoggedIn();
217 $this->username = $user->getName();
219 if ( $this->loggedin || $this->showIPinHeader() ) {
220 $this->userpageUrlDetails = self::makeUrlDetails( $this->userpage );
221 } else {
222 # This won't be used in the standard skins, but we define it to preserve the interface
223 # To save time, we check for existence
224 $this->userpageUrlDetails = self::makeKnownUrlDetails( $this->userpage );
227 wfProfileOut( __METHOD__ . '-stuff' );
229 wfProfileIn( __METHOD__ . '-stuff-head' );
230 if ( !$this->useHeadElement ) {
231 $tpl->set( 'pagecss', false );
232 $tpl->set( 'usercss', false );
234 $tpl->set( 'userjs', false );
235 $tpl->set( 'userjsprev', false );
237 $tpl->set( 'jsvarurl', false );
239 $tpl->set( 'xhtmldefaultnamespace', 'http://www.w3.org/1999/xhtml' );
240 $tpl->set( 'xhtmlnamespaces', $wgXhtmlNamespaces );
241 $tpl->set( 'html5version', $wgHtml5Version );
242 $tpl->set( 'headlinks', $out->getHeadLinks() );
243 $tpl->set( 'csslinks', $out->buildCssLinks() );
244 $tpl->set( 'pageclass', $this->getPageClasses( $title ) );
245 $tpl->set( 'skinnameclass', ( 'skin-' . Sanitizer::escapeClass( $this->getSkinName() ) ) );
247 wfProfileOut( __METHOD__ . '-stuff-head' );
249 wfProfileIn( __METHOD__ . '-stuff2' );
250 $tpl->set( 'title', $out->getPageTitle() );
251 $tpl->set( 'pagetitle', $out->getHTMLTitle() );
252 $tpl->set( 'displaytitle', $out->mPageLinkTitle );
254 $tpl->setRef( 'thispage', $this->thispage );
255 $tpl->setRef( 'titleprefixeddbkey', $this->thispage );
256 $tpl->set( 'titletext', $title->getText() );
257 $tpl->set( 'articleid', $title->getArticleID() );
259 $tpl->set( 'isarticle', $out->isArticle() );
261 $subpagestr = $this->subPageSubtitle();
262 if ( $subpagestr !== '' ) {
263 $subpagestr = '<span class="subpages">' . $subpagestr . '</span>';
265 $tpl->set( 'subtitle', $subpagestr . $out->getSubtitle() );
267 $undelete = $this->getUndeleteLink();
268 if ( $undelete === '' ) {
269 $tpl->set( 'undelete', '' );
270 } else {
271 $tpl->set( 'undelete', '<span class="subpages">' . $undelete . '</span>' );
274 $tpl->set( 'catlinks', $this->getCategories() );
275 if ( $out->isSyndicated() ) {
276 $feeds = array();
277 foreach ( $out->getSyndicationLinks() as $format => $link ) {
278 $feeds[$format] = array(
279 'text' => $this->msg( "feed-$format" )->text(),
280 'href' => $link
283 $tpl->setRef( 'feeds', $feeds );
284 } else {
285 $tpl->set( 'feeds', false );
288 $tpl->setRef( 'mimetype', $wgMimeType );
289 $tpl->setRef( 'jsmimetype', $wgJsMimeType );
290 $tpl->set( 'charset', 'UTF-8' );
291 $tpl->setRef( 'wgScript', $wgScript );
292 $tpl->setRef( 'skinname', $this->skinname );
293 $tpl->set( 'skinclass', get_class( $this ) );
294 $tpl->setRef( 'skin', $this );
295 $tpl->setRef( 'stylename', $this->stylename );
296 $tpl->set( 'printable', $out->isPrintable() );
297 $tpl->set( 'handheld', $request->getBool( 'handheld' ) );
298 $tpl->setRef( 'loggedin', $this->loggedin );
299 $tpl->set( 'notspecialpage', !$title->isSpecialPage() );
300 /* XXX currently unused, might get useful later
301 $tpl->set( 'editable', ( !$title->isSpecialPage() ) );
302 $tpl->set( 'exists', $title->getArticleID() != 0 );
303 $tpl->set( 'watch', $user->isWatched( $title ) ? 'unwatch' : 'watch' );
304 $tpl->set( 'protect', count( $title->isProtected() ) ? 'unprotect' : 'protect' );
305 $tpl->set( 'helppage', $this->msg( 'helppage' )->text() );
307 $tpl->set( 'searchaction', $this->escapeSearchLink() );
308 $tpl->set( 'searchtitle', SpecialPage::getTitleFor( 'Search' )->getPrefixedDBkey() );
309 $tpl->set( 'search', trim( $request->getVal( 'search' ) ) );
310 $tpl->setRef( 'stylepath', $wgStylePath );
311 $tpl->setRef( 'articlepath', $wgArticlePath );
312 $tpl->setRef( 'scriptpath', $wgScriptPath );
313 $tpl->setRef( 'serverurl', $wgServer );
314 $tpl->setRef( 'logopath', $wgLogo );
315 $tpl->setRef( 'sitename', $wgSitename );
317 $userLang = $this->getLanguage();
318 $userLangCode = $userLang->getHtmlCode();
319 $userLangDir = $userLang->getDir();
321 $tpl->set( 'lang', $userLangCode );
322 $tpl->set( 'dir', $userLangDir );
323 $tpl->set( 'rtl', $userLang->isRTL() );
325 $tpl->set( 'capitalizeallnouns', $userLang->capitalizeAllNouns() ? ' capitalize-all-nouns' : '' );
326 $tpl->set( 'showjumplinks', $user->getOption( 'showjumplinks' ) );
327 $tpl->set( 'username', $this->loggedin ? $this->username : null );
328 $tpl->setRef( 'userpage', $this->userpage );
329 $tpl->setRef( 'userpageurl', $this->userpageUrlDetails['href'] );
330 $tpl->set( 'userlang', $userLangCode );
332 // Users can have their language set differently than the
333 // content of the wiki. For these users, tell the web browser
334 // that interface elements are in a different language.
335 $tpl->set( 'userlangattributes', '' );
336 $tpl->set( 'specialpageattributes', '' ); # obsolete
338 if ( $userLangCode !== $wgContLang->getHtmlCode() || $userLangDir !== $wgContLang->getDir() ) {
339 $escUserlang = htmlspecialchars( $userLangCode );
340 $escUserdir = htmlspecialchars( $userLangDir );
341 // Attributes must be in double quotes because htmlspecialchars() doesn't
342 // escape single quotes
343 $attrs = " lang=\"$escUserlang\" dir=\"$escUserdir\"";
344 $tpl->set( 'userlangattributes', $attrs );
347 wfProfileOut( __METHOD__ . '-stuff2' );
349 wfProfileIn( __METHOD__ . '-stuff3' );
350 $tpl->set( 'newtalk', $this->getNewtalks() );
351 $tpl->set( 'logo', $this->logoText() );
353 $tpl->set( 'copyright', false );
354 $tpl->set( 'viewcount', false );
355 $tpl->set( 'lastmod', false );
356 $tpl->set( 'credits', false );
357 $tpl->set( 'numberofwatchingusers', false );
358 if ( $out->isArticle() && $title->exists() ) {
359 if ( $this->isRevisionCurrent() ) {
360 if ( !$wgDisableCounters ) {
361 $viewcount = $this->getWikiPage()->getCount();
362 if ( $viewcount ) {
363 $tpl->set( 'viewcount', $this->msg( 'viewcount' )->numParams( $viewcount )->parse() );
367 if ( $wgPageShowWatchingUsers ) {
368 $dbr = wfGetDB( DB_SLAVE );
369 $num = $dbr->selectField( 'watchlist', 'COUNT(*)',
370 array( 'wl_title' => $title->getDBkey(), 'wl_namespace' => $title->getNamespace() ),
371 __METHOD__
373 if ( $num > 0 ) {
374 $tpl->set( 'numberofwatchingusers',
375 $this->msg( 'number_of_watching_users_pageview' )->numParams( $num )->parse()
380 if ( $wgMaxCredits != 0 ) {
381 $tpl->set( 'credits', Action::factory( 'credits', $this->getWikiPage(),
382 $this->getContext() )->getCredits( $wgMaxCredits, $wgShowCreditsIfMax ) );
383 } else {
384 $tpl->set( 'lastmod', $this->lastModified() );
387 $tpl->set( 'copyright', $this->getCopyright() );
389 wfProfileOut( __METHOD__ . '-stuff3' );
391 wfProfileIn( __METHOD__ . '-stuff4' );
392 $tpl->set( 'copyrightico', $this->getCopyrightIcon() );
393 $tpl->set( 'poweredbyico', $this->getPoweredBy() );
394 $tpl->set( 'disclaimer', $this->disclaimerLink() );
395 $tpl->set( 'privacy', $this->privacyLink() );
396 $tpl->set( 'about', $this->aboutLink() );
398 $tpl->set( 'footerlinks', array(
399 'info' => array(
400 'lastmod',
401 'viewcount',
402 'numberofwatchingusers',
403 'credits',
404 'copyright',
406 'places' => array(
407 'privacy',
408 'about',
409 'disclaimer',
411 ) );
413 global $wgFooterIcons;
414 $tpl->set( 'footericons', $wgFooterIcons );
415 foreach ( $tpl->data['footericons'] as $footerIconsKey => &$footerIconsBlock ) {
416 if ( count( $footerIconsBlock ) > 0 ) {
417 foreach ( $footerIconsBlock as &$footerIcon ) {
418 if ( isset( $footerIcon['src'] ) ) {
419 if ( !isset( $footerIcon['width'] ) ) {
420 $footerIcon['width'] = 88;
422 if ( !isset( $footerIcon['height'] ) ) {
423 $footerIcon['height'] = 31;
427 } else {
428 unset( $tpl->data['footericons'][$footerIconsKey] );
432 $tpl->set( 'sitenotice', $this->getSiteNotice() );
433 $tpl->set( 'bottomscripts', $this->bottomScripts() );
434 $tpl->set( 'printfooter', $this->printSource() );
436 # An ID that includes the actual body text; without categories, contentSub, ...
437 $realBodyAttribs = array( 'id' => 'mw-content-text' );
439 # Add a mw-content-ltr/rtl class to be able to style based on text direction
440 # when the content is different from the UI language, i.e.:
441 # not for special pages or file pages AND only when viewing AND if the page exists
442 # (or is in MW namespace, because that has default content)
443 if ( !in_array( $title->getNamespace(), array( NS_SPECIAL, NS_FILE ) ) &&
444 Action::getActionName( $this ) === 'view' &&
445 ( $title->exists() || $title->getNamespace() == NS_MEDIAWIKI ) ) {
446 $pageLang = $title->getPageViewLanguage();
447 $realBodyAttribs['lang'] = $pageLang->getHtmlCode();
448 $realBodyAttribs['dir'] = $pageLang->getDir();
449 $realBodyAttribs['class'] = 'mw-content-' . $pageLang->getDir();
452 $out->mBodytext = Html::rawElement( 'div', $realBodyAttribs, $out->mBodytext );
453 $tpl->setRef( 'bodytext', $out->mBodytext );
455 $language_urls = $this->getLanguages();
456 if ( count( $language_urls ) ) {
457 $tpl->setRef( 'language_urls', $language_urls );
458 } else {
459 $tpl->set( 'language_urls', false );
461 wfProfileOut( __METHOD__ . '-stuff4' );
463 wfProfileIn( __METHOD__ . '-stuff5' );
464 # Personal toolbar
465 $tpl->set( 'personal_urls', $this->buildPersonalUrls() );
466 $content_navigation = $this->buildContentNavigationUrls();
467 $content_actions = $this->buildContentActionUrls( $content_navigation );
468 $tpl->setRef( 'content_navigation', $content_navigation );
469 $tpl->setRef( 'content_actions', $content_actions );
471 $tpl->set( 'sidebar', $this->buildSidebar() );
472 $tpl->set( 'nav_urls', $this->buildNavUrls() );
474 // Set the head scripts near the end, in case the above actions resulted in added scripts
475 if ( $this->useHeadElement ) {
476 $tpl->set( 'headelement', $out->headElement( $this ) );
477 } else {
478 $tpl->set( 'headscripts', $out->getHeadScripts() . $out->getHeadItems() );
481 $tpl->set( 'debug', '' );
482 $tpl->set( 'debughtml', $this->generateDebugHTML() );
483 $tpl->set( 'reporttime', wfReportTime() );
485 // original version by hansm
486 if ( !wfRunHooks( 'SkinTemplateOutputPageBeforeExec', array( &$this, &$tpl ) ) ) {
487 wfDebug( __METHOD__ . ": Hook SkinTemplateOutputPageBeforeExec broke outputPage execution!\n" );
490 // Set the bodytext to another key so that skins can just output it on it's own
491 // and output printfooter and debughtml separately
492 $tpl->set( 'bodycontent', $tpl->data['bodytext'] );
494 // Append printfooter and debughtml onto bodytext so that skins that were already
495 // using bodytext before they were split out don't suddenly start not outputting information
496 $tpl->data['bodytext'] .= Html::rawElement( 'div', array( 'class' => 'printfooter' ), "\n{$tpl->data['printfooter']}" ) . "\n";
497 $tpl->data['bodytext'] .= $tpl->data['debughtml'];
499 // allow extensions adding stuff after the page content.
500 // See Skin::afterContentHook() for further documentation.
501 $tpl->set( 'dataAfterContent', $this->afterContentHook() );
502 wfProfileOut( __METHOD__ . '-stuff5' );
504 // execute template
505 wfProfileIn( __METHOD__ . '-execute' );
506 $res = $tpl->execute();
507 wfProfileOut( __METHOD__ . '-execute' );
509 // result may be an error
510 $this->printOrError( $res );
512 if ( $oldContext ) {
513 $this->setContext( $oldContext );
515 wfProfileOut( __METHOD__ );
519 * Format language name for use in sidebar interlanguage links list.
520 * By default it is capitalized.
522 * @param string $name Language name, e.g. "English" or "español"
523 * @return string
524 * @private
526 function formatLanguageName( $name ) {
527 return $this->getLanguage()->ucfirst( $name );
531 * Output the string, or print error message if it's
532 * an error object of the appropriate type.
533 * For the base class, assume strings all around.
535 * @param $str Mixed
536 * @private
538 function printOrError( $str ) {
539 echo $str;
543 * Output a boolean indicating if buildPersonalUrls should output separate
544 * login and create account links or output a combined link
545 * By default we simply return a global config setting that affects most skins
546 * This is setup as a method so that like with $wgLogo and getLogo() a skin
547 * can override this setting and always output one or the other if it has
548 * a reason it can't output one of the two modes.
549 * @return bool
551 function useCombinedLoginLink() {
552 global $wgUseCombinedLoginLink;
553 return $wgUseCombinedLoginLink;
557 * build array of urls for personal toolbar
558 * @return array
560 protected function buildPersonalUrls() {
561 global $wgSecureLogin;
563 $title = $this->getTitle();
564 $request = $this->getRequest();
565 $pageurl = $title->getLocalURL();
566 wfProfileIn( __METHOD__ );
568 /* set up the default links for the personal toolbar */
569 $personal_urls = array();
571 # Due to bug 32276, if a user does not have read permissions,
572 # $this->getTitle() will just give Special:Badtitle, which is
573 # not especially useful as a returnto parameter. Use the title
574 # from the request instead, if there was one.
575 if ( $this->getUser()->isAllowed( 'read' ) ) {
576 $page = $this->getTitle();
577 } else {
578 $page = Title::newFromText( $request->getVal( 'title', '' ) );
580 $page = $request->getVal( 'returnto', $page );
581 $a = array();
582 if ( strval( $page ) !== '' ) {
583 $a['returnto'] = $page;
584 $query = $request->getVal( 'returntoquery', $this->thisquery );
585 if ( $query != '' ) {
586 $a['returntoquery'] = $query;
590 if ( $wgSecureLogin && $request->detectProtocol() === 'https' ) {
591 $a['wpStickHTTPS'] = true;
594 $returnto = wfArrayToCgi( $a );
595 if ( $this->loggedin ) {
596 $personal_urls['userpage'] = array(
597 'text' => $this->username,
598 'href' => &$this->userpageUrlDetails['href'],
599 'class' => $this->userpageUrlDetails['exists'] ? false : 'new',
600 'active' => ( $this->userpageUrlDetails['href'] == $pageurl ),
601 'dir' => 'auto'
603 $usertalkUrlDetails = $this->makeTalkUrlDetails( $this->userpage );
604 $personal_urls['mytalk'] = array(
605 'text' => $this->msg( 'mytalk' )->text(),
606 'href' => &$usertalkUrlDetails['href'],
607 'class' => $usertalkUrlDetails['exists'] ? false : 'new',
608 'active' => ( $usertalkUrlDetails['href'] == $pageurl )
610 $href = self::makeSpecialUrl( 'Preferences' );
611 $personal_urls['preferences'] = array(
612 'text' => $this->msg( 'mypreferences' )->text(),
613 'href' => $href,
614 'active' => ( $href == $pageurl )
616 $href = self::makeSpecialUrl( 'Watchlist' );
617 $personal_urls['watchlist'] = array(
618 'text' => $this->msg( 'mywatchlist' )->text(),
619 'href' => $href,
620 'active' => ( $href == $pageurl )
623 # We need to do an explicit check for Special:Contributions, as we
624 # have to match both the title, and the target, which could come
625 # from request values (Special:Contributions?target=Jimbo_Wales)
626 # or be specified in "sub page" form
627 # (Special:Contributions/Jimbo_Wales). The plot
628 # thickens, because the Title object is altered for special pages,
629 # so it doesn't contain the original alias-with-subpage.
630 $origTitle = Title::newFromText( $request->getText( 'title' ) );
631 if ( $origTitle instanceof Title && $origTitle->isSpecialPage() ) {
632 list( $spName, $spPar ) = SpecialPageFactory::resolveAlias( $origTitle->getText() );
633 $active = $spName == 'Contributions'
634 && ( ( $spPar && $spPar == $this->username )
635 || $request->getText( 'target' ) == $this->username );
636 } else {
637 $active = false;
640 $href = self::makeSpecialUrlSubpage( 'Contributions', $this->username );
641 $personal_urls['mycontris'] = array(
642 'text' => $this->msg( 'mycontris' )->text(),
643 'href' => $href,
644 'active' => $active
646 $personal_urls['logout'] = array(
647 'text' => $this->msg( 'userlogout' )->text(),
648 'href' => self::makeSpecialUrl( 'Userlogout',
649 // userlogout link must always contain an & character, otherwise we might not be able
650 // to detect a buggy precaching proxy (bug 17790)
651 $title->isSpecial( 'Preferences' ) ? 'noreturnto' : $returnto
653 'active' => false
655 } else {
656 $useCombinedLoginLink = $this->useCombinedLoginLink();
657 $loginlink = $this->getUser()->isAllowed( 'createaccount' ) && $useCombinedLoginLink
658 ? 'nav-login-createaccount'
659 : 'login';
660 $is_signup = $request->getText( 'type' ) == 'signup';
662 # anonlogin & login are the same
663 $proto = $wgSecureLogin ? PROTO_HTTPS : null;
665 $login_id = $this->showIPinHeader() ? 'anonlogin' : 'login';
666 $login_url = array(
667 'text' => $this->msg( $loginlink )->text(),
668 'href' => self::makeSpecialUrl( 'Userlogin', $returnto, $proto ),
669 'active' => $title->isSpecial( 'Userlogin' ) && ( $loginlink == 'nav-login-createaccount' || !$is_signup ),
670 'class' => $wgSecureLogin ? 'link-https' : ''
672 $createaccount_url = array(
673 'text' => $this->msg( 'createaccount' )->text(),
674 'href' => self::makeSpecialUrl( 'Userlogin', "$returnto&type=signup", $proto ),
675 'active' => $title->isSpecial( 'Userlogin' ) && $is_signup,
676 'class' => $wgSecureLogin ? 'link-https' : ''
679 if ( $this->showIPinHeader() ) {
680 $href = &$this->userpageUrlDetails['href'];
681 $personal_urls['anonuserpage'] = array(
682 'text' => $this->username,
683 'href' => $href,
684 'class' => $this->userpageUrlDetails['exists'] ? false : 'new',
685 'active' => ( $pageurl == $href )
687 $usertalkUrlDetails = $this->makeTalkUrlDetails( $this->userpage );
688 $href = &$usertalkUrlDetails['href'];
689 $personal_urls['anontalk'] = array(
690 'text' => $this->msg( 'anontalk' )->text(),
691 'href' => $href,
692 'class' => $usertalkUrlDetails['exists'] ? false : 'new',
693 'active' => ( $pageurl == $href )
697 if ( $this->getUser()->isAllowed( 'createaccount' ) && !$useCombinedLoginLink ) {
698 $personal_urls['createaccount'] = $createaccount_url;
701 $personal_urls[$login_id] = $login_url;
704 wfRunHooks( 'PersonalUrls', array( &$personal_urls, &$title ) );
705 wfProfileOut( __METHOD__ );
706 return $personal_urls;
710 * Builds an array with tab definition
712 * @param Title $title page where the tab links to
713 * @param string|array $message message key or an array of message keys (will fall back)
714 * @param boolean $selected display the tab as selected
715 * @param string $query query string attached to tab URL
716 * @param boolean $checkEdit check if $title exists and mark with .new if one doesn't
718 * @return array
720 function tabAction( $title, $message, $selected, $query = '', $checkEdit = false ) {
721 $classes = array();
722 if ( $selected ) {
723 $classes[] = 'selected';
725 if ( $checkEdit && !$title->isKnown() ) {
726 $classes[] = 'new';
727 if ( $query !== '' ) {
728 $query = 'action=edit&redlink=1&' . $query;
729 } else {
730 $query = 'action=edit&redlink=1';
734 // wfMessageFallback will nicely accept $message as an array of fallbacks
735 // or just a single key
736 $msg = wfMessageFallback( $message )->setContext( $this->getContext() );
737 if ( is_array( $message ) ) {
738 // for hook compatibility just keep the last message name
739 $message = end( $message );
741 if ( $msg->exists() ) {
742 $text = $msg->text();
743 } else {
744 global $wgContLang;
745 $text = $wgContLang->getFormattedNsText(
746 MWNamespace::getSubject( $title->getNamespace() ) );
749 $result = array();
750 if ( !wfRunHooks( 'SkinTemplateTabAction', array( &$this,
751 $title, $message, $selected, $checkEdit,
752 &$classes, &$query, &$text, &$result ) ) ) {
753 return $result;
756 return array(
757 'class' => implode( ' ', $classes ),
758 'text' => $text,
759 'href' => $title->getLocalURL( $query ),
760 'primary' => true );
763 function makeTalkUrlDetails( $name, $urlaction = '' ) {
764 $title = Title::newFromText( $name );
765 if ( !is_object( $title ) ) {
766 throw new MWException( __METHOD__ . " given invalid pagename $name" );
768 $title = $title->getTalkPage();
769 self::checkTitle( $title, $name );
770 return array(
771 'href' => $title->getLocalURL( $urlaction ),
772 'exists' => $title->getArticleID() != 0,
776 function makeArticleUrlDetails( $name, $urlaction = '' ) {
777 $title = Title::newFromText( $name );
778 $title = $title->getSubjectPage();
779 self::checkTitle( $title, $name );
780 return array(
781 'href' => $title->getLocalURL( $urlaction ),
782 'exists' => $title->getArticleID() != 0,
787 * a structured array of links usually used for the tabs in a skin
789 * There are 4 standard sections
790 * namespaces: Used for namespace tabs like special, page, and talk namespaces
791 * views: Used for primary page views like read, edit, history
792 * actions: Used for most extra page actions like deletion, protection, etc...
793 * variants: Used to list the language variants for the page
795 * Each section's value is a key/value array of links for that section.
796 * The links themselves have these common keys:
797 * - class: The css classes to apply to the tab
798 * - text: The text to display on the tab
799 * - href: The href for the tab to point to
800 * - rel: An optional rel= for the tab's link
801 * - redundant: If true the tab will be dropped in skins using content_actions
802 * this is useful for tabs like "Read" which only have meaning in skins that
803 * take special meaning from the grouped structure of content_navigation
805 * Views also have an extra key which can be used:
806 * - primary: If this is not true skins like vector may try to hide the tab
807 * when the user has limited space in their browser window
809 * content_navigation using code also expects these ids to be present on the
810 * links, however these are usually automatically generated by SkinTemplate
811 * itself and are not necessary when using a hook. The only things these may
812 * matter to are people modifying content_navigation after it's initial creation:
813 * - id: A "preferred" id, most skins are best off outputting this preferred id for best compatibility
814 * - tooltiponly: This is set to true for some tabs in cases where the system
815 * believes that the accesskey should not be added to the tab.
817 * @return array
819 protected function buildContentNavigationUrls() {
820 global $wgDisableLangConversion;
822 wfProfileIn( __METHOD__ );
824 // Display tabs for the relevant title rather than always the title itself
825 $title = $this->getRelevantTitle();
826 $onPage = $title->equals( $this->getTitle() );
828 $out = $this->getOutput();
829 $request = $this->getRequest();
830 $user = $this->getUser();
832 $content_navigation = array(
833 'namespaces' => array(),
834 'views' => array(),
835 'actions' => array(),
836 'variants' => array()
839 // parameters
840 $action = $request->getVal( 'action', 'view' );
842 $userCanRead = $title->quickUserCan( 'read', $user );
844 $preventActiveTabs = false;
845 wfRunHooks( 'SkinTemplatePreventOtherActiveTabs', array( &$this, &$preventActiveTabs ) );
847 // Checks if page is some kind of content
848 if ( $title->canExist() ) {
849 // Gets page objects for the related namespaces
850 $subjectPage = $title->getSubjectPage();
851 $talkPage = $title->getTalkPage();
853 // Determines if this is a talk page
854 $isTalk = $title->isTalkPage();
856 // Generates XML IDs from namespace names
857 $subjectId = $title->getNamespaceKey( '' );
859 if ( $subjectId == 'main' ) {
860 $talkId = 'talk';
861 } else {
862 $talkId = "{$subjectId}_talk";
865 $skname = $this->skinname;
867 // Adds namespace links
868 $subjectMsg = array( "nstab-$subjectId" );
869 if ( $subjectPage->isMainPage() ) {
870 array_unshift( $subjectMsg, 'mainpage-nstab' );
872 $content_navigation['namespaces'][$subjectId] = $this->tabAction(
873 $subjectPage, $subjectMsg, !$isTalk && !$preventActiveTabs, '', $userCanRead
875 $content_navigation['namespaces'][$subjectId]['context'] = 'subject';
876 $content_navigation['namespaces'][$talkId] = $this->tabAction(
877 $talkPage, array( "nstab-$talkId", 'talk' ), $isTalk && !$preventActiveTabs, '', $userCanRead
879 $content_navigation['namespaces'][$talkId]['context'] = 'talk';
881 if ( $userCanRead ) {
882 // Adds view view link
883 if ( $title->exists() ) {
884 $content_navigation['views']['view'] = $this->tabAction(
885 $isTalk ? $talkPage : $subjectPage,
886 array( "$skname-view-view", 'view' ),
887 ( $onPage && ( $action == 'view' || $action == 'purge' ) ), '', true
889 // signal to hide this from simple content_actions
890 $content_navigation['views']['view']['redundant'] = true;
893 wfProfileIn( __METHOD__ . '-edit' );
895 // Checks if user can edit the current page if it exists or create it otherwise
896 if ( $title->quickUserCan( 'edit', $user ) && ( $title->exists() || $title->quickUserCan( 'create', $user ) ) ) {
897 // Builds CSS class for talk page links
898 $isTalkClass = $isTalk ? ' istalk' : '';
899 // Whether the user is editing the page
900 $isEditing = $onPage && ( $action == 'edit' || $action == 'submit' );
901 // Whether to show the "Add a new section" tab
902 // Checks if this is a current rev of talk page and is not forced to be hidden
903 $showNewSection = !$out->forceHideNewSectionLink()
904 && ( ( $isTalk && $this->isRevisionCurrent() ) || $out->showNewSectionLink() );
905 $section = $request->getVal( 'section' );
907 $msgKey = $title->exists() || ( $title->getNamespace() == NS_MEDIAWIKI && $title->getDefaultMessageText() !== false ) ?
908 'edit' : 'create';
909 $content_navigation['views']['edit'] = array(
910 'class' => ( $isEditing && ( $section !== 'new' || !$showNewSection ) ? 'selected' : '' ) . $isTalkClass,
911 'text' => wfMessageFallback( "$skname-view-$msgKey", $msgKey )->setContext( $this->getContext() )->text(),
912 'href' => $title->getLocalURL( $this->editUrlOptions() ),
913 'primary' => true, // don't collapse this in vector
916 // section link
917 if ( $showNewSection ) {
918 // Adds new section link
919 //$content_navigation['actions']['addsection']
920 $content_navigation['views']['addsection'] = array(
921 'class' => ( $isEditing && $section == 'new' ) ? 'selected' : false,
922 'text' => wfMessageFallback( "$skname-action-addsection", 'addsection' )->setContext( $this->getContext() )->text(),
923 'href' => $title->getLocalURL( 'action=edit&section=new' )
926 // Checks if the page has some kind of viewable content
927 } elseif ( $title->hasSourceText() ) {
928 // Adds view source view link
929 $content_navigation['views']['viewsource'] = array(
930 'class' => ( $onPage && $action == 'edit' ) ? 'selected' : false,
931 'text' => wfMessageFallback( "$skname-action-viewsource", 'viewsource' )->setContext( $this->getContext() )->text(),
932 'href' => $title->getLocalURL( $this->editUrlOptions() ),
933 'primary' => true, // don't collapse this in vector
936 wfProfileOut( __METHOD__ . '-edit' );
938 wfProfileIn( __METHOD__ . '-live' );
939 // Checks if the page exists
940 if ( $title->exists() ) {
941 // Adds history view link
942 $content_navigation['views']['history'] = array(
943 'class' => ( $onPage && $action == 'history' ) ? 'selected' : false,
944 'text' => wfMessageFallback( "$skname-view-history", 'history_short' )->setContext( $this->getContext() )->text(),
945 'href' => $title->getLocalURL( 'action=history' ),
946 'rel' => 'archives',
949 if ( $title->quickUserCan( 'delete', $user ) ) {
950 $content_navigation['actions']['delete'] = array(
951 'class' => ( $onPage && $action == 'delete' ) ? 'selected' : false,
952 'text' => wfMessageFallback( "$skname-action-delete", 'delete' )->setContext( $this->getContext() )->text(),
953 'href' => $title->getLocalURL( 'action=delete' )
957 if ( $title->quickUserCan( 'move', $user ) ) {
958 $moveTitle = SpecialPage::getTitleFor( 'Movepage', $title->getPrefixedDBkey() );
959 $content_navigation['actions']['move'] = array(
960 'class' => $this->getTitle()->isSpecial( 'Movepage' ) ? 'selected' : false,
961 'text' => wfMessageFallback( "$skname-action-move", 'move' )->setContext( $this->getContext() )->text(),
962 'href' => $moveTitle->getLocalURL()
965 } else {
966 // article doesn't exist or is deleted
967 if ( $user->isAllowed( 'deletedhistory' ) ) {
968 $n = $title->isDeleted();
969 if ( $n ) {
970 $undelTitle = SpecialPage::getTitleFor( 'Undelete' );
971 // If the user can't undelete but can view deleted history show them a "View .. deleted" tab instead
972 $msgKey = $user->isAllowed( 'undelete' ) ? 'undelete' : 'viewdeleted';
973 $content_navigation['actions']['undelete'] = array(
974 'class' => $this->getTitle()->isSpecial( 'Undelete' ) ? 'selected' : false,
975 'text' => wfMessageFallback( "$skname-action-$msgKey", "{$msgKey}_short" )
976 ->setContext( $this->getContext() )->numParams( $n )->text(),
977 'href' => $undelTitle->getLocalURL( array( 'target' => $title->getPrefixedDBkey() ) )
983 if ( $title->getNamespace() !== NS_MEDIAWIKI && $title->quickUserCan( 'protect', $user ) && $title->getRestrictionTypes() ) {
984 $mode = $title->isProtected() ? 'unprotect' : 'protect';
985 $content_navigation['actions'][$mode] = array(
986 'class' => ( $onPage && $action == $mode ) ? 'selected' : false,
987 'text' => wfMessageFallback( "$skname-action-$mode", $mode )->setContext( $this->getContext() )->text(),
988 'href' => $title->getLocalURL( "action=$mode" )
992 wfProfileOut( __METHOD__ . '-live' );
994 // Checks if the user is logged in
995 if ( $this->loggedin ) {
997 * The following actions use messages which, if made particular to
998 * the any specific skins, would break the Ajax code which makes this
999 * action happen entirely inline. Skin::makeGlobalVariablesScript
1000 * defines a set of messages in a javascript object - and these
1001 * messages are assumed to be global for all skins. Without making
1002 * a change to that procedure these messages will have to remain as
1003 * the global versions.
1005 $mode = $user->isWatched( $title ) ? 'unwatch' : 'watch';
1006 $token = WatchAction::getWatchToken( $title, $user, $mode );
1007 $content_navigation['actions'][$mode] = array(
1008 'class' => $onPage && ( $action == 'watch' || $action == 'unwatch' ) ? 'selected' : false,
1009 // uses 'watch' or 'unwatch' message
1010 'text' => $this->msg( $mode )->text(),
1011 'href' => $title->getLocalURL( array( 'action' => $mode, 'token' => $token ) )
1016 wfRunHooks( 'SkinTemplateNavigation', array( &$this, &$content_navigation ) );
1018 if ( $userCanRead && !$wgDisableLangConversion ) {
1019 $pageLang = $title->getPageLanguage();
1020 // Gets list of language variants
1021 $variants = $pageLang->getVariants();
1022 // Checks that language conversion is enabled and variants exist
1023 // And if it is not in the special namespace
1024 if ( count( $variants ) > 1 ) {
1025 // Gets preferred variant (note that user preference is
1026 // only possible for wiki content language variant)
1027 $preferred = $pageLang->getPreferredVariant();
1028 if ( Action::getActionName( $this ) === 'view' ) {
1029 $params = $request->getQueryValues();
1030 unset( $params['title'] );
1031 } else {
1032 $params = array();
1034 // Loops over each variant
1035 foreach ( $variants as $code ) {
1036 // Gets variant name from language code
1037 $varname = $pageLang->getVariantname( $code );
1038 // Appends variant link
1039 $content_navigation['variants'][] = array(
1040 'class' => ( $code == $preferred ) ? 'selected' : false,
1041 'text' => $varname,
1042 'href' => $title->getLocalURL( array( 'variant' => $code ) + $params ),
1043 'lang' => $code,
1044 'hreflang' => $code
1049 } else {
1050 // If it's not content, it's got to be a special page
1051 $content_navigation['namespaces']['special'] = array(
1052 'class' => 'selected',
1053 'text' => $this->msg( 'nstab-special' )->text(),
1054 'href' => $request->getRequestURL(), // @see: bug 2457, bug 2510
1055 'context' => 'subject'
1058 wfRunHooks( 'SkinTemplateNavigation::SpecialPage',
1059 array( &$this, &$content_navigation ) );
1062 // Equiv to SkinTemplateContentActions
1063 wfRunHooks( 'SkinTemplateNavigation::Universal', array( &$this, &$content_navigation ) );
1065 // Setup xml ids and tooltip info
1066 foreach ( $content_navigation as $section => &$links ) {
1067 foreach ( $links as $key => &$link ) {
1068 $xmlID = $key;
1069 if ( isset( $link['context'] ) && $link['context'] == 'subject' ) {
1070 $xmlID = 'ca-nstab-' . $xmlID;
1071 } elseif ( isset( $link['context'] ) && $link['context'] == 'talk' ) {
1072 $xmlID = 'ca-talk';
1073 } elseif ( $section == 'variants' ) {
1074 $xmlID = 'ca-varlang-' . $xmlID;
1075 } else {
1076 $xmlID = 'ca-' . $xmlID;
1078 $link['id'] = $xmlID;
1082 # We don't want to give the watch tab an accesskey if the
1083 # page is being edited, because that conflicts with the
1084 # accesskey on the watch checkbox. We also don't want to
1085 # give the edit tab an accesskey, because that's fairly
1086 # superfluous and conflicts with an accesskey (Ctrl-E) often
1087 # used for editing in Safari.
1088 if ( in_array( $action, array( 'edit', 'submit' ) ) ) {
1089 if ( isset( $content_navigation['views']['edit'] ) ) {
1090 $content_navigation['views']['edit']['tooltiponly'] = true;
1092 if ( isset( $content_navigation['actions']['watch'] ) ) {
1093 $content_navigation['actions']['watch']['tooltiponly'] = true;
1095 if ( isset( $content_navigation['actions']['unwatch'] ) ) {
1096 $content_navigation['actions']['unwatch']['tooltiponly'] = true;
1100 wfProfileOut( __METHOD__ );
1102 return $content_navigation;
1106 * an array of edit links by default used for the tabs
1107 * @return array
1108 * @private
1110 function buildContentActionUrls( $content_navigation ) {
1112 wfProfileIn( __METHOD__ );
1114 // content_actions has been replaced with content_navigation for backwards
1115 // compatibility and also for skins that just want simple tabs content_actions
1116 // is now built by flattening the content_navigation arrays into one
1118 $content_actions = array();
1120 foreach ( $content_navigation as $links ) {
1122 foreach ( $links as $key => $value ) {
1124 if ( isset( $value['redundant'] ) && $value['redundant'] ) {
1125 // Redundant tabs are dropped from content_actions
1126 continue;
1129 // content_actions used to have ids built using the "ca-$key" pattern
1130 // so the xmlID based id is much closer to the actual $key that we want
1131 // for that reason we'll just strip out the ca- if present and use
1132 // the latter potion of the "id" as the $key
1133 if ( isset( $value['id'] ) && substr( $value['id'], 0, 3 ) == 'ca-' ) {
1134 $key = substr( $value['id'], 3 );
1137 if ( isset( $content_actions[$key] ) ) {
1138 wfDebug( __METHOD__ . ": Found a duplicate key for $key while flattening content_navigation into content_actions." );
1139 continue;
1142 $content_actions[$key] = $value;
1148 wfProfileOut( __METHOD__ );
1150 return $content_actions;
1154 * build array of common navigation links
1155 * @return array
1156 * @private
1158 protected function buildNavUrls() {
1159 global $wgUploadNavigationUrl;
1161 wfProfileIn( __METHOD__ );
1163 $out = $this->getOutput();
1164 $request = $this->getRequest();
1166 $nav_urls = array();
1167 $nav_urls['mainpage'] = array( 'href' => self::makeMainPageUrl() );
1168 if ( $wgUploadNavigationUrl ) {
1169 $nav_urls['upload'] = array( 'href' => $wgUploadNavigationUrl );
1170 } elseif ( UploadBase::isEnabled() && UploadBase::isAllowed( $this->getUser() ) === true ) {
1171 $nav_urls['upload'] = array( 'href' => self::makeSpecialUrl( 'Upload' ) );
1172 } else {
1173 $nav_urls['upload'] = false;
1175 $nav_urls['specialpages'] = array( 'href' => self::makeSpecialUrl( 'Specialpages' ) );
1177 $nav_urls['print'] = false;
1178 $nav_urls['permalink'] = false;
1179 $nav_urls['info'] = false;
1180 $nav_urls['whatlinkshere'] = false;
1181 $nav_urls['recentchangeslinked'] = false;
1182 $nav_urls['contributions'] = false;
1183 $nav_urls['log'] = false;
1184 $nav_urls['blockip'] = false;
1185 $nav_urls['emailuser'] = false;
1186 $nav_urls['userrights'] = false;
1188 // A print stylesheet is attached to all pages, but nobody ever
1189 // figures that out. :) Add a link...
1190 if ( $out->isArticle() ) {
1191 if ( !$out->isPrintable() ) {
1192 $nav_urls['print'] = array(
1193 'text' => $this->msg( 'printableversion' )->text(),
1194 'href' => $this->getTitle()->getLocalURL(
1195 $request->appendQueryValue( 'printable', 'yes', true ) )
1199 // Also add a "permalink" while we're at it
1200 $revid = $this->getRevisionId();
1201 if ( $revid ) {
1202 $nav_urls['permalink'] = array(
1203 'text' => $this->msg( 'permalink' )->text(),
1204 'href' => $this->getTitle()->getLocalURL( "oldid=$revid" )
1208 // Use the copy of revision ID in case this undocumented, shady hook tries to mess with internals
1209 wfRunHooks( 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink',
1210 array( &$this, &$nav_urls, &$revid, &$revid ) );
1213 if ( $out->isArticleRelated() ) {
1214 $nav_urls['whatlinkshere'] = array(
1215 'href' => SpecialPage::getTitleFor( 'Whatlinkshere', $this->thispage )->getLocalURL()
1218 $nav_urls['info'] = array(
1219 'text' => $this->msg( 'pageinfo-toolboxlink' )->text(),
1220 'href' => $this->getTitle()->getLocalURL( "action=info" )
1223 if ( $this->getTitle()->getArticleID() ) {
1224 $nav_urls['recentchangeslinked'] = array(
1225 'href' => SpecialPage::getTitleFor( 'Recentchangeslinked', $this->thispage )->getLocalURL()
1230 $user = $this->getRelevantUser();
1231 if ( $user ) {
1232 $rootUser = $user->getName();
1234 $nav_urls['contributions'] = array(
1235 'text' => $this->msg( 'contributions', $rootUser )->text(),
1236 'href' => self::makeSpecialUrlSubpage( 'Contributions', $rootUser )
1239 $nav_urls['log'] = array(
1240 'href' => self::makeSpecialUrlSubpage( 'Log', $rootUser )
1243 if ( $this->getUser()->isAllowed( 'block' ) ) {
1244 $nav_urls['blockip'] = array(
1245 'href' => self::makeSpecialUrlSubpage( 'Block', $rootUser )
1249 if ( $this->showEmailUser( $user ) ) {
1250 $nav_urls['emailuser'] = array(
1251 'href' => self::makeSpecialUrlSubpage( 'Emailuser', $rootUser )
1255 $sur = new UserrightsPage;
1256 if ( $sur->userCanExecute( $this->getUser() ) ) {
1257 $nav_urls['userrights'] = array(
1258 'href' => self::makeSpecialUrlSubpage( 'Userrights', $rootUser )
1263 wfProfileOut( __METHOD__ );
1264 return $nav_urls;
1268 * Generate strings used for xml 'id' names
1269 * @return string
1270 * @private
1272 function getNameSpaceKey() {
1273 return $this->getTitle()->getNamespaceKey();
1276 public function commonPrintStylesheet() {
1277 return false;
1282 * Generic wrapper for template functions, with interface
1283 * compatible with what we use of PHPTAL 0.7.
1284 * @ingroup Skins
1286 abstract class QuickTemplate {
1288 * Constructor
1290 function __construct() {
1291 $this->data = array();
1292 $this->translator = new MediaWiki_I18N();
1296 * Sets the value $value to $name
1297 * @param $name
1298 * @param $value
1300 public function set( $name, $value ) {
1301 $this->data[$name] = $value;
1305 * @param $name
1306 * @param $value
1308 public function setRef( $name, &$value ) {
1309 $this->data[$name] =& $value;
1313 * @param $t
1315 public function setTranslator( &$t ) {
1316 $this->translator = &$t;
1320 * Main function, used by classes that subclass QuickTemplate
1321 * to show the actual HTML output
1323 abstract public function execute();
1326 * @private
1328 function text( $str ) {
1329 echo htmlspecialchars( $this->data[$str] );
1333 * @private
1334 * @deprecated since 1.21; use Xml::encodeJsVar() or Xml::encodeJsCall() instead
1336 function jstext( $str ) {
1337 wfDeprecated( __METHOD__, '1.21' );
1338 echo Xml::escapeJsString( $this->data[$str] );
1342 * @private
1344 function html( $str ) {
1345 echo $this->data[$str];
1349 * @private
1351 function msg( $str ) {
1352 echo htmlspecialchars( $this->translator->translate( $str ) );
1356 * @private
1358 function msgHtml( $str ) {
1359 echo $this->translator->translate( $str );
1363 * An ugly, ugly hack.
1364 * @private
1366 function msgWiki( $str ) {
1367 global $wgOut;
1369 $text = $this->translator->translate( $str );
1370 echo $wgOut->parse( $text );
1374 * @private
1375 * @return bool
1377 function haveData( $str ) {
1378 return isset( $this->data[$str] );
1382 * @private
1384 * @return bool
1386 function haveMsg( $str ) {
1387 $msg = $this->translator->translate( $str );
1388 return ( $msg != '-' ) && ( $msg != '' ); # ????
1392 * Get the Skin object related to this object
1394 * @return Skin object
1396 public function getSkin() {
1397 return $this->data['skin'];
1402 * New base template for a skin's template extended from QuickTemplate
1403 * this class features helper methods that provide common ways of interacting
1404 * with the data stored in the QuickTemplate
1406 abstract class BaseTemplate extends QuickTemplate {
1409 * Get a Message object with its context set
1411 * @param string $name message name
1412 * @return Message
1414 public function getMsg( $name ) {
1415 return $this->getSkin()->msg( $name );
1418 function msg( $str ) {
1419 echo $this->getMsg( $str )->escaped();
1422 function msgHtml( $str ) {
1423 echo $this->getMsg( $str )->text();
1426 function msgWiki( $str ) {
1427 echo $this->getMsg( $str )->parseAsBlock();
1431 * Create an array of common toolbox items from the data in the quicktemplate
1432 * stored by SkinTemplate.
1433 * The resulting array is built according to a format intended to be passed
1434 * through makeListItem to generate the html.
1435 * @return array
1437 function getToolbox() {
1438 wfProfileIn( __METHOD__ );
1440 $toolbox = array();
1441 if ( isset( $this->data['nav_urls']['whatlinkshere'] ) && $this->data['nav_urls']['whatlinkshere'] ) {
1442 $toolbox['whatlinkshere'] = $this->data['nav_urls']['whatlinkshere'];
1443 $toolbox['whatlinkshere']['id'] = 't-whatlinkshere';
1445 if ( isset( $this->data['nav_urls']['recentchangeslinked'] ) && $this->data['nav_urls']['recentchangeslinked'] ) {
1446 $toolbox['recentchangeslinked'] = $this->data['nav_urls']['recentchangeslinked'];
1447 $toolbox['recentchangeslinked']['msg'] = 'recentchangeslinked-toolbox';
1448 $toolbox['recentchangeslinked']['id'] = 't-recentchangeslinked';
1450 if ( isset( $this->data['feeds'] ) && $this->data['feeds'] ) {
1451 $toolbox['feeds']['id'] = 'feedlinks';
1452 $toolbox['feeds']['links'] = array();
1453 foreach ( $this->data['feeds'] as $key => $feed ) {
1454 $toolbox['feeds']['links'][$key] = $feed;
1455 $toolbox['feeds']['links'][$key]['id'] = "feed-$key";
1456 $toolbox['feeds']['links'][$key]['rel'] = 'alternate';
1457 $toolbox['feeds']['links'][$key]['type'] = "application/{$key}+xml";
1458 $toolbox['feeds']['links'][$key]['class'] = 'feedlink';
1461 foreach ( array( 'contributions', 'log', 'blockip', 'emailuser', 'userrights', 'upload', 'specialpages' ) as $special ) {
1462 if ( isset( $this->data['nav_urls'][$special] ) && $this->data['nav_urls'][$special] ) {
1463 $toolbox[$special] = $this->data['nav_urls'][$special];
1464 $toolbox[$special]['id'] = "t-$special";
1467 if ( isset( $this->data['nav_urls']['print'] ) && $this->data['nav_urls']['print'] ) {
1468 $toolbox['print'] = $this->data['nav_urls']['print'];
1469 $toolbox['print']['id'] = 't-print';
1470 $toolbox['print']['rel'] = 'alternate';
1471 $toolbox['print']['msg'] = 'printableversion';
1473 if ( isset( $this->data['nav_urls']['permalink'] ) && $this->data['nav_urls']['permalink'] ) {
1474 $toolbox['permalink'] = $this->data['nav_urls']['permalink'];
1475 if ( $toolbox['permalink']['href'] === '' ) {
1476 unset( $toolbox['permalink']['href'] );
1477 $toolbox['ispermalink']['tooltiponly'] = true;
1478 $toolbox['ispermalink']['id'] = 't-ispermalink';
1479 $toolbox['ispermalink']['msg'] = 'permalink';
1480 } else {
1481 $toolbox['permalink']['id'] = 't-permalink';
1484 if ( isset( $this->data['nav_urls']['info'] ) && $this->data['nav_urls']['info'] ) {
1485 $toolbox['info'] = $this->data['nav_urls']['info'];
1486 $toolbox['info']['id'] = 't-info';
1489 wfRunHooks( 'BaseTemplateToolbox', array( &$this, &$toolbox ) );
1490 wfProfileOut( __METHOD__ );
1491 return $toolbox;
1495 * Create an array of personal tools items from the data in the quicktemplate
1496 * stored by SkinTemplate.
1497 * The resulting array is built according to a format intended to be passed
1498 * through makeListItem to generate the html.
1499 * This is in reality the same list as already stored in personal_urls
1500 * however it is reformatted so that you can just pass the individual items
1501 * to makeListItem instead of hardcoding the element creation boilerplate.
1502 * @return array
1504 function getPersonalTools() {
1505 $personal_tools = array();
1506 foreach ( $this->data['personal_urls'] as $key => $plink ) {
1507 # The class on a personal_urls item is meant to go on the <a> instead
1508 # of the <li> so we have to use a single item "links" array instead
1509 # of using most of the personal_url's keys directly.
1510 $ptool = array(
1511 'links' => array(
1512 array( 'single-id' => "pt-$key" ),
1514 'id' => "pt-$key",
1516 if ( isset( $plink['active'] ) ) {
1517 $ptool['active'] = $plink['active'];
1519 foreach ( array( 'href', 'class', 'text' ) as $k ) {
1520 if ( isset( $plink[$k] ) ) {
1521 $ptool['links'][0][$k] = $plink[$k];
1524 $personal_tools[$key] = $ptool;
1526 return $personal_tools;
1529 function getSidebar( $options = array() ) {
1530 // Force the rendering of the following portals
1531 $sidebar = $this->data['sidebar'];
1532 if ( !isset( $sidebar['SEARCH'] ) ) {
1533 $sidebar['SEARCH'] = true;
1535 if ( !isset( $sidebar['TOOLBOX'] ) ) {
1536 $sidebar['TOOLBOX'] = true;
1538 if ( !isset( $sidebar['LANGUAGES'] ) ) {
1539 $sidebar['LANGUAGES'] = true;
1542 if ( !isset( $options['search'] ) || $options['search'] !== true ) {
1543 unset( $sidebar['SEARCH'] );
1545 if ( isset( $options['toolbox'] ) && $options['toolbox'] === false ) {
1546 unset( $sidebar['TOOLBOX'] );
1548 if ( isset( $options['languages'] ) && $options['languages'] === false ) {
1549 unset( $sidebar['LANGUAGES'] );
1552 $boxes = array();
1553 foreach ( $sidebar as $boxName => $content ) {
1554 if ( $content === false ) {
1555 continue;
1557 switch ( $boxName ) {
1558 case 'SEARCH':
1559 // Search is a special case, skins should custom implement this
1560 $boxes[$boxName] = array(
1561 'id' => 'p-search',
1562 'header' => $this->getMsg( 'search' )->text(),
1563 'generated' => false,
1564 'content' => true,
1566 break;
1567 case 'TOOLBOX':
1568 $msgObj = $this->getMsg( 'toolbox' );
1569 $boxes[$boxName] = array(
1570 'id' => 'p-tb',
1571 'header' => $msgObj->exists() ? $msgObj->text() : 'toolbox',
1572 'generated' => false,
1573 'content' => $this->getToolbox(),
1575 break;
1576 case 'LANGUAGES':
1577 if ( $this->data['language_urls'] ) {
1578 $msgObj = $this->getMsg( 'otherlanguages' );
1579 $boxes[$boxName] = array(
1580 'id' => 'p-lang',
1581 'header' => $msgObj->exists() ? $msgObj->text() : 'otherlanguages',
1582 'generated' => false,
1583 'content' => $this->data['language_urls'],
1586 break;
1587 default:
1588 $msgObj = $this->getMsg( $boxName );
1589 $boxes[$boxName] = array(
1590 'id' => "p-$boxName",
1591 'header' => $msgObj->exists() ? $msgObj->text() : $boxName,
1592 'generated' => true,
1593 'content' => $content,
1595 break;
1599 // HACK: Compatibility with extensions still using SkinTemplateToolboxEnd
1600 $hookContents = null;
1601 if ( isset( $boxes['TOOLBOX'] ) ) {
1602 ob_start();
1603 // We pass an extra 'true' at the end so extensions using BaseTemplateToolbox
1604 // can abort and avoid outputting double toolbox links
1605 wfRunHooks( 'SkinTemplateToolboxEnd', array( &$this, true ) );
1606 $hookContents = ob_get_contents();
1607 ob_end_clean();
1608 if ( !trim( $hookContents ) ) {
1609 $hookContents = null;
1612 // END hack
1614 if ( isset( $options['htmlOnly'] ) && $options['htmlOnly'] === true ) {
1615 foreach ( $boxes as $boxName => $box ) {
1616 if ( is_array( $box['content'] ) ) {
1617 $content = '<ul>';
1618 foreach ( $box['content'] as $key => $val ) {
1619 $content .= "\n " . $this->makeListItem( $key, $val );
1621 // HACK, shove the toolbox end onto the toolbox if we're rendering itself
1622 if ( $hookContents ) {
1623 $content .= "\n $hookContents";
1625 // END hack
1626 $content .= "\n</ul>\n";
1627 $boxes[$boxName]['content'] = $content;
1630 } else {
1631 if ( $hookContents ) {
1632 $boxes['TOOLBOXEND'] = array(
1633 'id' => 'p-toolboxend',
1634 'header' => $boxes['TOOLBOX']['header'],
1635 'generated' => false,
1636 'content' => "<ul>{$hookContents}</ul>",
1638 // HACK: Make sure that TOOLBOXEND is sorted next to TOOLBOX
1639 $boxes2 = array();
1640 foreach ( $boxes as $key => $box ) {
1641 if ( $key === 'TOOLBOXEND' ) {
1642 continue;
1644 $boxes2[$key] = $box;
1645 if ( $key === 'TOOLBOX' ) {
1646 $boxes2['TOOLBOXEND'] = $boxes['TOOLBOXEND'];
1649 $boxes = $boxes2;
1650 // END hack
1654 return $boxes;
1658 * Makes a link, usually used by makeListItem to generate a link for an item
1659 * in a list used in navigation lists, portlets, portals, sidebars, etc...
1661 * @param string $key usually a key from the list you are generating this
1662 * link from.
1663 * @param array $item contains some of a specific set of keys.
1665 * The text of the link will be generated either from the contents of the
1666 * "text" key in the $item array, if a "msg" key is present a message by
1667 * that name will be used, and if neither of those are set the $key will be
1668 * used as a message name.
1670 * If a "href" key is not present makeLink will just output htmlescaped text.
1671 * The "href", "id", "class", "rel", and "type" keys are used as attributes
1672 * for the link if present.
1674 * If an "id" or "single-id" (if you don't want the actual id to be output
1675 * on the link) is present it will be used to generate a tooltip and
1676 * accesskey for the link.
1678 * If you don't want an accesskey, set $item['tooltiponly'] = true;
1680 * @param array $options can be used to affect the output of a link.
1681 * Possible options are:
1682 * - 'text-wrapper' key to specify a list of elements to wrap the text of
1683 * a link in. This should be an array of arrays containing a 'tag' and
1684 * optionally an 'attributes' key. If you only have one element you don't
1685 * need to wrap it in another array. eg: To use <a><span>...</span></a>
1686 * in all links use array( 'text-wrapper' => array( 'tag' => 'span' ) )
1687 * for your options.
1688 * - 'link-class' key can be used to specify additional classes to apply
1689 * to all links.
1690 * - 'link-fallback' can be used to specify a tag to use instead of "<a>"
1691 * if there is no link. eg: If you specify 'link-fallback' => 'span' than
1692 * any non-link will output a "<span>" instead of just text.
1694 * @return string
1696 function makeLink( $key, $item, $options = array() ) {
1697 if ( isset( $item['text'] ) ) {
1698 $text = $item['text'];
1699 } else {
1700 $text = $this->translator->translate( isset( $item['msg'] ) ? $item['msg'] : $key );
1703 $html = htmlspecialchars( $text );
1705 if ( isset( $options['text-wrapper'] ) ) {
1706 $wrapper = $options['text-wrapper'];
1707 if ( isset( $wrapper['tag'] ) ) {
1708 $wrapper = array( $wrapper );
1710 while ( count( $wrapper ) > 0 ) {
1711 $element = array_pop( $wrapper );
1712 $html = Html::rawElement( $element['tag'], isset( $element['attributes'] ) ? $element['attributes'] : null, $html );
1716 if ( isset( $item['href'] ) || isset( $options['link-fallback'] ) ) {
1717 $attrs = $item;
1718 foreach ( array( 'single-id', 'text', 'msg', 'tooltiponly' ) as $k ) {
1719 unset( $attrs[$k] );
1722 if ( isset( $item['id'] ) && !isset( $item['single-id'] ) ) {
1723 $item['single-id'] = $item['id'];
1725 if ( isset( $item['single-id'] ) ) {
1726 if ( isset( $item['tooltiponly'] ) && $item['tooltiponly'] ) {
1727 $title = Linker::titleAttrib( $item['single-id'] );
1728 if ( $title !== false ) {
1729 $attrs['title'] = $title;
1731 } else {
1732 $tip = Linker::tooltipAndAccesskeyAttribs( $item['single-id'] );
1733 if ( isset( $tip['title'] ) && $tip['title'] !== false ) {
1734 $attrs['title'] = $tip['title'];
1736 if ( isset( $tip['accesskey'] ) && $tip['accesskey'] !== false ) {
1737 $attrs['accesskey'] = $tip['accesskey'];
1741 if ( isset( $options['link-class'] ) ) {
1742 if ( isset( $attrs['class'] ) ) {
1743 $attrs['class'] .= " {$options['link-class']}";
1744 } else {
1745 $attrs['class'] = $options['link-class'];
1748 $html = Html::rawElement( isset( $attrs['href'] ) ? 'a' : $options['link-fallback'], $attrs, $html );
1751 return $html;
1755 * Generates a list item for a navigation, portlet, portal, sidebar... list
1757 * @param $key string, usually a key from the list you are generating this link from.
1758 * @param $item array, of list item data containing some of a specific set of keys.
1759 * The "id" and "class" keys will be used as attributes for the list item,
1760 * if "active" contains a value of true a "active" class will also be appended to class.
1762 * @param $options array
1764 * If you want something other than a "<li>" you can pass a tag name such as
1765 * "tag" => "span" in the $options array to change the tag used.
1766 * link/content data for the list item may come in one of two forms
1767 * A "links" key may be used, in which case it should contain an array with
1768 * a list of links to include inside the list item, see makeLink for the
1769 * format of individual links array items.
1771 * Otherwise the relevant keys from the list item $item array will be passed
1772 * to makeLink instead. Note however that "id" and "class" are used by the
1773 * list item directly so they will not be passed to makeLink
1774 * (however the link will still support a tooltip and accesskey from it)
1775 * If you need an id or class on a single link you should include a "links"
1776 * array with just one link item inside of it.
1777 * $options is also passed on to makeLink calls
1779 * @return string
1781 function makeListItem( $key, $item, $options = array() ) {
1782 if ( isset( $item['links'] ) ) {
1783 $html = '';
1784 foreach ( $item['links'] as $linkKey => $link ) {
1785 $html .= $this->makeLink( $linkKey, $link, $options );
1787 } else {
1788 $link = $item;
1789 // These keys are used by makeListItem and shouldn't be passed on to the link
1790 foreach ( array( 'id', 'class', 'active', 'tag' ) as $k ) {
1791 unset( $link[$k] );
1793 if ( isset( $item['id'] ) && !isset( $item['single-id'] ) ) {
1794 // The id goes on the <li> not on the <a> for single links
1795 // but makeSidebarLink still needs to know what id to use when
1796 // generating tooltips and accesskeys.
1797 $link['single-id'] = $item['id'];
1799 $html = $this->makeLink( $key, $link, $options );
1802 $attrs = array();
1803 foreach ( array( 'id', 'class' ) as $attr ) {
1804 if ( isset( $item[$attr] ) ) {
1805 $attrs[$attr] = $item[$attr];
1808 if ( isset( $item['active'] ) && $item['active'] ) {
1809 if ( !isset( $attrs['class'] ) ) {
1810 $attrs['class'] = '';
1812 $attrs['class'] .= ' active';
1813 $attrs['class'] = trim( $attrs['class'] );
1815 return Html::rawElement( isset( $options['tag'] ) ? $options['tag'] : 'li', $attrs, $html );
1818 function makeSearchInput( $attrs = array() ) {
1819 $realAttrs = array(
1820 'type' => 'search',
1821 'name' => 'search',
1822 'placeholder' => wfMessage( 'searchsuggest-search' )->text(),
1823 'value' => isset( $this->data['search'] ) ? $this->data['search'] : '',
1825 $realAttrs = array_merge( $realAttrs, Linker::tooltipAndAccesskeyAttribs( 'search' ), $attrs );
1826 return Html::element( 'input', $realAttrs );
1829 function makeSearchButton( $mode, $attrs = array() ) {
1830 switch ( $mode ) {
1831 case 'go':
1832 case 'fulltext':
1833 $realAttrs = array(
1834 'type' => 'submit',
1835 'name' => $mode,
1836 'value' => $this->translator->translate(
1837 $mode == 'go' ? 'searcharticle' : 'searchbutton' ),
1839 $realAttrs = array_merge(
1840 $realAttrs,
1841 Linker::tooltipAndAccesskeyAttribs( "search-$mode" ),
1842 $attrs
1844 return Html::element( 'input', $realAttrs );
1845 case 'image':
1846 $buttonAttrs = array(
1847 'type' => 'submit',
1848 'name' => 'button',
1850 $buttonAttrs = array_merge(
1851 $buttonAttrs,
1852 Linker::tooltipAndAccesskeyAttribs( 'search-fulltext' ),
1853 $attrs
1855 unset( $buttonAttrs['src'] );
1856 unset( $buttonAttrs['alt'] );
1857 unset( $buttonAttrs['width'] );
1858 unset( $buttonAttrs['height'] );
1859 $imgAttrs = array(
1860 'src' => $attrs['src'],
1861 'alt' => isset( $attrs['alt'] )
1862 ? $attrs['alt']
1863 : $this->translator->translate( 'searchbutton' ),
1864 'width' => isset( $attrs['width'] ) ? $attrs['width'] : null,
1865 'height' => isset( $attrs['height'] ) ? $attrs['height'] : null,
1867 return Html::rawElement( 'button', $buttonAttrs, Html::element( 'img', $imgAttrs ) );
1868 default:
1869 throw new MWException( 'Unknown mode passed to BaseTemplate::makeSearchButton' );
1874 * Returns an array of footerlinks trimmed down to only those footer links that
1875 * are valid.
1876 * If you pass "flat" as an option then the returned array will be a flat array
1877 * of footer icons instead of a key/value array of footerlinks arrays broken
1878 * up into categories.
1879 * @return array|mixed
1881 function getFooterLinks( $option = null ) {
1882 $footerlinks = $this->data['footerlinks'];
1884 // Reduce footer links down to only those which are being used
1885 $validFooterLinks = array();
1886 foreach ( $footerlinks as $category => $links ) {
1887 $validFooterLinks[$category] = array();
1888 foreach ( $links as $link ) {
1889 if ( isset( $this->data[$link] ) && $this->data[$link] ) {
1890 $validFooterLinks[$category][] = $link;
1893 if ( count( $validFooterLinks[$category] ) <= 0 ) {
1894 unset( $validFooterLinks[$category] );
1898 if ( $option == 'flat' ) {
1899 // fold footerlinks into a single array using a bit of trickery
1900 $validFooterLinks = call_user_func_array(
1901 'array_merge',
1902 array_values( $validFooterLinks )
1906 return $validFooterLinks;
1910 * Returns an array of footer icons filtered down by options relevant to how
1911 * the skin wishes to display them.
1912 * If you pass "icononly" as the option all footer icons which do not have an
1913 * image icon set will be filtered out.
1914 * If you pass "nocopyright" then MediaWiki's copyright icon will not be included
1915 * in the list of footer icons. This is mostly useful for skins which only
1916 * display the text from footericons instead of the images and don't want a
1917 * duplicate copyright statement because footerlinks already rendered one.
1918 * @return
1920 function getFooterIcons( $option = null ) {
1921 // Generate additional footer icons
1922 $footericons = $this->data['footericons'];
1924 if ( $option == 'icononly' ) {
1925 // Unset any icons which don't have an image
1926 foreach ( $footericons as &$footerIconsBlock ) {
1927 foreach ( $footerIconsBlock as $footerIconKey => $footerIcon ) {
1928 if ( !is_string( $footerIcon ) && !isset( $footerIcon['src'] ) ) {
1929 unset( $footerIconsBlock[$footerIconKey] );
1933 // Redo removal of any empty blocks
1934 foreach ( $footericons as $footerIconsKey => &$footerIconsBlock ) {
1935 if ( count( $footerIconsBlock ) <= 0 ) {
1936 unset( $footericons[$footerIconsKey] );
1939 } elseif ( $option == 'nocopyright' ) {
1940 unset( $footericons['copyright']['copyright'] );
1941 if ( count( $footericons['copyright'] ) <= 0 ) {
1942 unset( $footericons['copyright'] );
1946 return $footericons;
1950 * Output the basic end-page trail including bottomscripts, reporttime, and
1951 * debug stuff. This should be called right before outputting the closing
1952 * body and html tags.
1954 function printTrail() { ?>
1955 <?php $this->html( 'bottomscripts' ); /* JS call to runBodyOnloadHook */ ?>
1956 <?php $this->html( 'reporttime' ) ?>
1957 <?php echo MWDebug::getDebugHTML( $this->getSkin()->getContext() );