3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 use MediaWiki\Auth\AuthManager
;
22 use MediaWiki\MediaWikiServices
;
25 * Base class for template-based skins.
27 * Template-filler skin base class
28 * Formerly generic PHPTal (http://phptal.sourceforge.net/) skin
29 * Based on Brion's smarty skin
30 * @copyright Copyright © Gabriel Wicke -- http://www.aulinx.de/
32 * @todo Needs some serious refactoring into functions that correspond
33 * to the computations individual esi snippets need. Most importantly no body
34 * parsing for most of those of course.
38 class SkinTemplate
extends Skin
{
40 * @var string Name of our skin, it probably needs to be all lower case.
41 * Child classes should override the default.
43 public $skinname = 'monobook';
46 * @var string For QuickTemplate, the name of the subclass which will
47 * actually fill the template. Child classes should override the default.
49 public $template = QuickTemplate
::class;
57 public $userpageUrlDetails;
60 * Add specific styles for this skin
62 * @param OutputPage $out
64 public function setupSkinUserCss( OutputPage
$out ) {
66 'mediawiki.legacy.shared',
67 'mediawiki.legacy.commonPrint',
68 'mediawiki.sectionAnchor'
70 if ( $out->isSyndicated() ) {
71 $moduleStyles[] = 'mediawiki.feedlink';
74 // Deprecated since 1.26: Unconditional loading of mediawiki.ui.button
75 // on every page is deprecated. Express a dependency instead.
76 if ( strpos( $out->getHTML(), 'mw-ui-button' ) !== false ) {
77 $moduleStyles[] = 'mediawiki.ui.button';
80 $out->addModuleStyles( $moduleStyles );
84 * Create the template engine object; we feed it a bunch of data
85 * and eventually it spits out some HTML. Should have interface
86 * roughly equivalent to PHPTAL 0.7.
88 * @param string $classname
89 * @param bool|string $repository Subdirectory where we keep template files
90 * @param bool|string $cache_dir
91 * @return QuickTemplate
94 function setupTemplate( $classname, $repository = false, $cache_dir = false ) {
95 return new $classname( $this->getConfig() );
99 * Generates array of language links for the current page
103 public function getLanguages() {
104 global $wgHideInterlanguageLinks;
105 if ( $wgHideInterlanguageLinks ) {
109 $userLang = $this->getLanguage();
112 foreach ( $this->getOutput()->getLanguageLinks() as $languageLinkText ) {
113 $class = 'interlanguage-link interwiki-' . explode( ':', $languageLinkText, 2 )[0];
115 $languageLinkTitle = Title
::newFromText( $languageLinkText );
116 if ( $languageLinkTitle ) {
117 $ilInterwikiCode = $languageLinkTitle->getInterwiki();
118 $ilLangName = Language
::fetchLanguageName( $ilInterwikiCode );
120 if ( strval( $ilLangName ) === '' ) {
121 $ilDisplayTextMsg = wfMessage( "interlanguage-link-$ilInterwikiCode" );
122 if ( !$ilDisplayTextMsg->isDisabled() ) {
123 // Use custom MW message for the display text
124 $ilLangName = $ilDisplayTextMsg->text();
126 // Last resort: fallback to the language link target
127 $ilLangName = $languageLinkText;
130 // Use the language autonym as display text
131 $ilLangName = $this->formatLanguageName( $ilLangName );
134 // CLDR extension or similar is required to localize the language name;
135 // otherwise we'll end up with the autonym again.
136 $ilLangLocalName = Language
::fetchLanguageName(
141 $languageLinkTitleText = $languageLinkTitle->getText();
142 if ( $ilLangLocalName === '' ) {
143 $ilFriendlySiteName = wfMessage( "interlanguage-link-sitename-$ilInterwikiCode" );
144 if ( !$ilFriendlySiteName->isDisabled() ) {
145 if ( $languageLinkTitleText === '' ) {
146 $ilTitle = wfMessage(
147 'interlanguage-link-title-nonlangonly',
148 $ilFriendlySiteName->text()
151 $ilTitle = wfMessage(
152 'interlanguage-link-title-nonlang',
153 $languageLinkTitleText,
154 $ilFriendlySiteName->text()
158 // we have nothing friendly to put in the title, so fall back to
159 // displaying the interlanguage link itself in the title text
160 // (similar to what is done in page content)
161 $ilTitle = $languageLinkTitle->getInterwiki() .
162 ":$languageLinkTitleText";
164 } elseif ( $languageLinkTitleText === '' ) {
165 $ilTitle = wfMessage(
166 'interlanguage-link-title-langonly',
170 $ilTitle = wfMessage(
171 'interlanguage-link-title',
172 $languageLinkTitleText,
177 $ilInterwikiCodeBCP47 = LanguageCode
::bcp47( $ilInterwikiCode );
179 'href' => $languageLinkTitle->getFullURL(),
180 'text' => $ilLangName,
183 'link-class' => 'interlanguage-link-target',
184 'lang' => $ilInterwikiCodeBCP47,
185 'hreflang' => $ilInterwikiCodeBCP47,
188 'SkinTemplateGetLanguageLink',
189 [ &$languageLink, $languageLinkTitle, $this->getTitle(), $this->getOutput() ]
191 $languageLinks[] = $languageLink;
195 return $languageLinks;
198 protected function setupTemplateForOutput() {
199 $request = $this->getRequest();
200 $user = $this->getUser();
201 $title = $this->getTitle();
203 $tpl = $this->setupTemplate( $this->template
, 'skins' );
205 $this->thispage
= $title->getPrefixedDBkey();
206 $this->titletxt
= $title->getPrefixedText();
207 $this->userpage
= $user->getUserPage()->getPrefixedText();
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
) {
220 $this->userpageUrlDetails
= self
::makeUrlDetails( $this->userpage
);
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
);
231 * initialize various variables and generate the template
233 * @param OutputPage $out
235 function outputPage( OutputPage
$out = null ) {
236 Profiler
::instance()->setTemplated( true );
239 if ( $out !== null ) {
240 // Deprecated since 1.20, note added in 1.25
241 wfDeprecated( __METHOD__
, '1.25' );
242 $oldContext = $this->getContext();
243 $this->setContext( $out->getContext() );
246 $out = $this->getOutput();
248 $this->initPage( $out );
249 $tpl = $this->prepareQuickTemplate();
251 $res = $tpl->execute();
253 // result may be an error
254 $this->printOrError( $res );
257 $this->setContext( $oldContext );
262 * Wrap the body text with language information and identifiable element
264 * @param Title $title
265 * @param string $html body text
266 * @return string html
268 protected function wrapHTML( $title, $html ) {
269 # An ID that includes the actual body text; without categories, contentSub, ...
270 $realBodyAttribs = [ 'id' => 'mw-content-text' ];
272 # Add a mw-content-ltr/rtl class to be able to style based on text
273 # direction when the content is different from the UI language (only
275 # Most information on special pages and file pages is in user language,
276 # rather than content language, so those will not get this
277 if ( Action
::getActionName( $this ) === 'view' &&
278 ( !$title->inNamespaces( NS_SPECIAL
, NS_FILE
) ||
$title->isRedirect() ) ) {
279 $pageLang = $title->getPageViewLanguage();
280 $realBodyAttribs['lang'] = $pageLang->getHtmlCode();
281 $realBodyAttribs['dir'] = $pageLang->getDir();
282 $realBodyAttribs['class'] = 'mw-content-' . $pageLang->getDir();
285 return Html
::rawElement( 'div', $realBodyAttribs, $html );
289 * initialize various variables and generate the template
292 * @return QuickTemplate The template to be executed by outputPage
294 protected function prepareQuickTemplate() {
295 global $wgContLang, $wgScript, $wgStylePath, $wgMimeType, $wgJsMimeType,
296 $wgSitename, $wgLogo, $wgMaxCredits,
297 $wgShowCreditsIfMax, $wgArticlePath,
298 $wgScriptPath, $wgServer;
300 $title = $this->getTitle();
301 $request = $this->getRequest();
302 $out = $this->getOutput();
303 $tpl = $this->setupTemplateForOutput();
305 $tpl->set( 'title', $out->getPageTitle() );
306 $tpl->set( 'pagetitle', $out->getHTMLTitle() );
307 $tpl->set( 'displaytitle', $out->mPageLinkTitle
);
309 $tpl->set( 'thispage', $this->thispage
);
310 $tpl->set( 'titleprefixeddbkey', $this->thispage
);
311 $tpl->set( 'titletext', $title->getText() );
312 $tpl->set( 'articleid', $title->getArticleID() );
314 $tpl->set( 'isarticle', $out->isArticle() );
316 $subpagestr = $this->subPageSubtitle();
317 if ( $subpagestr !== '' ) {
318 $subpagestr = '<span class="subpages">' . $subpagestr . '</span>';
320 $tpl->set( 'subtitle', $subpagestr . $out->getSubtitle() );
322 $undelete = $this->getUndeleteLink();
323 if ( $undelete === '' ) {
324 $tpl->set( 'undelete', '' );
326 $tpl->set( 'undelete', '<span class="subpages">' . $undelete . '</span>' );
329 $tpl->set( 'catlinks', $this->getCategories() );
330 if ( $out->isSyndicated() ) {
332 foreach ( $out->getSyndicationLinks() as $format => $link ) {
334 // Messages: feed-atom, feed-rss
335 'text' => $this->msg( "feed-$format" )->text(),
339 $tpl->set( 'feeds', $feeds );
341 $tpl->set( 'feeds', false );
344 $tpl->set( 'mimetype', $wgMimeType );
345 $tpl->set( 'jsmimetype', $wgJsMimeType );
346 $tpl->set( 'charset', 'UTF-8' );
347 $tpl->set( 'wgScript', $wgScript );
348 $tpl->set( 'skinname', $this->skinname
);
349 $tpl->set( 'skinclass', static::class );
350 $tpl->set( 'skin', $this );
351 $tpl->set( 'stylename', $this->stylename
);
352 $tpl->set( 'printable', $out->isPrintable() );
353 $tpl->set( 'handheld', $request->getBool( 'handheld' ) );
354 $tpl->set( 'loggedin', $this->loggedin
);
355 $tpl->set( 'notspecialpage', !$title->isSpecialPage() );
356 $tpl->set( 'searchaction', $this->escapeSearchLink() );
357 $tpl->set( 'searchtitle', SpecialPage
::getTitleFor( 'Search' )->getPrefixedDBkey() );
358 $tpl->set( 'search', trim( $request->getVal( 'search' ) ) );
359 $tpl->set( 'stylepath', $wgStylePath );
360 $tpl->set( 'articlepath', $wgArticlePath );
361 $tpl->set( 'scriptpath', $wgScriptPath );
362 $tpl->set( 'serverurl', $wgServer );
363 $tpl->set( 'logopath', $wgLogo );
364 $tpl->set( 'sitename', $wgSitename );
366 $userLang = $this->getLanguage();
367 $userLangCode = $userLang->getHtmlCode();
368 $userLangDir = $userLang->getDir();
370 $tpl->set( 'lang', $userLangCode );
371 $tpl->set( 'dir', $userLangDir );
372 $tpl->set( 'rtl', $userLang->isRTL() );
374 $tpl->set( 'capitalizeallnouns', $userLang->capitalizeAllNouns() ?
' capitalize-all-nouns' : '' );
375 $tpl->set( 'showjumplinks', true ); // showjumplinks preference has been removed
376 $tpl->set( 'username', $this->loggedin ?
$this->username
: null );
377 $tpl->set( 'userpage', $this->userpage
);
378 $tpl->set( 'userpageurl', $this->userpageUrlDetails
['href'] );
379 $tpl->set( 'userlang', $userLangCode );
381 // Users can have their language set differently than the
382 // content of the wiki. For these users, tell the web browser
383 // that interface elements are in a different language.
384 $tpl->set( 'userlangattributes', '' );
385 $tpl->set( 'specialpageattributes', '' ); # obsolete
386 // Used by VectorBeta to insert HTML before content but after the
387 // heading for the page title. Defaults to empty string.
388 $tpl->set( 'prebodyhtml', '' );
390 if ( $userLangCode !== $wgContLang->getHtmlCode() ||
$userLangDir !== $wgContLang->getDir() ) {
391 $escUserlang = htmlspecialchars( $userLangCode );
392 $escUserdir = htmlspecialchars( $userLangDir );
393 // Attributes must be in double quotes because htmlspecialchars() doesn't
394 // escape single quotes
395 $attrs = " lang=\"$escUserlang\" dir=\"$escUserdir\"";
396 $tpl->set( 'userlangattributes', $attrs );
399 $tpl->set( 'newtalk', $this->getNewtalks() );
400 $tpl->set( 'logo', $this->logoText() );
402 $tpl->set( 'copyright', false );
404 $tpl->set( 'viewcount', false );
405 $tpl->set( 'lastmod', false );
406 $tpl->set( 'credits', false );
407 $tpl->set( 'numberofwatchingusers', false );
408 if ( $out->isArticle() && $title->exists() ) {
409 if ( $this->isRevisionCurrent() ) {
410 if ( $wgMaxCredits != 0 ) {
411 $tpl->set( 'credits', Action
::factory( 'credits', $this->getWikiPage(),
412 $this->getContext() )->getCredits( $wgMaxCredits, $wgShowCreditsIfMax ) );
414 $tpl->set( 'lastmod', $this->lastModified() );
417 $tpl->set( 'copyright', $this->getCopyright() );
420 $tpl->set( 'copyrightico', $this->getCopyrightIcon() );
421 $tpl->set( 'poweredbyico', $this->getPoweredBy() );
422 $tpl->set( 'disclaimer', $this->disclaimerLink() );
423 $tpl->set( 'privacy', $this->privacyLink() );
424 $tpl->set( 'about', $this->aboutLink() );
426 $tpl->set( 'footerlinks', [
429 'numberofwatchingusers',
440 global $wgFooterIcons;
441 $tpl->set( 'footericons', $wgFooterIcons );
442 foreach ( $tpl->data
['footericons'] as $footerIconsKey => &$footerIconsBlock ) {
443 if ( count( $footerIconsBlock ) > 0 ) {
444 foreach ( $footerIconsBlock as &$footerIcon ) {
445 if ( isset( $footerIcon['src'] ) ) {
446 if ( !isset( $footerIcon['width'] ) ) {
447 $footerIcon['width'] = 88;
449 if ( !isset( $footerIcon['height'] ) ) {
450 $footerIcon['height'] = 31;
455 unset( $tpl->data
['footericons'][$footerIconsKey] );
459 $tpl->set( 'indicators', $out->getIndicators() );
461 $tpl->set( 'sitenotice', $this->getSiteNotice() );
462 $tpl->set( 'printfooter', $this->printSource() );
463 // Wrap the bodyText with #mw-content-text element
464 $out->mBodytext
= $this->wrapHTML( $title, $out->mBodytext
);
465 $tpl->set( 'bodytext', $out->mBodytext
);
467 $language_urls = $this->getLanguages();
468 if ( count( $language_urls ) ) {
469 $tpl->set( 'language_urls', $language_urls );
471 $tpl->set( 'language_urls', false );
475 $tpl->set( 'personal_urls', $this->buildPersonalUrls() );
476 $content_navigation = $this->buildContentNavigationUrls();
477 $content_actions = $this->buildContentActionUrls( $content_navigation );
478 $tpl->set( 'content_navigation', $content_navigation );
479 $tpl->set( 'content_actions', $content_actions );
481 $tpl->set( 'sidebar', $this->buildSidebar() );
482 $tpl->set( 'nav_urls', $this->buildNavUrls() );
484 // Do this last in case hooks above add bottom scripts
485 $tpl->set( 'bottomscripts', $this->bottomScripts() );
487 // Set the head scripts near the end, in case the above actions resulted in added scripts
488 $tpl->set( 'headelement', $out->headElement( $this ) );
490 $tpl->set( 'debug', '' );
491 $tpl->set( 'debughtml', $this->generateDebugHTML() );
492 $tpl->set( 'reporttime', wfReportTime() );
494 // Avoid PHP 7.1 warning of passing $this by reference
495 $skinTemplate = $this;
496 // original version by hansm
497 if ( !Hooks
::run( 'SkinTemplateOutputPageBeforeExec', [ &$skinTemplate, &$tpl ] ) ) {
498 wfDebug( __METHOD__
. ": Hook SkinTemplateOutputPageBeforeExec broke outputPage execution!\n" );
501 // Set the bodytext to another key so that skins can just output it on its own
502 // and output printfooter and debughtml separately
503 $tpl->set( 'bodycontent', $tpl->data
['bodytext'] );
505 // Append printfooter and debughtml onto bodytext so that skins that
506 // were already using bodytext before they were split out don't suddenly
507 // start not outputting information.
508 $tpl->data
['bodytext'] .= Html
::rawElement(
510 [ 'class' => 'printfooter' ],
511 "\n{$tpl->data['printfooter']}"
513 $tpl->data
['bodytext'] .= $tpl->data
['debughtml'];
515 // allow extensions adding stuff after the page content.
516 // See Skin::afterContentHook() for further documentation.
517 $tpl->set( 'dataAfterContent', $this->afterContentHook() );
523 * Get the HTML for the p-personal list
526 public function getPersonalToolsList() {
527 return $this->makePersonalToolsList();
531 * Get the HTML for the personal tools list
535 * @param array $personalTools
536 * @param array $options
539 public function makePersonalToolsList( $personalTools = null, $options = [] ) {
540 $tpl = $this->setupTemplateForOutput();
541 $tpl->set( 'personal_urls', $this->buildPersonalUrls() );
544 if ( $personalTools === null ) {
545 $personalTools = $tpl->getPersonalTools();
548 foreach ( $personalTools as $key => $item ) {
549 $html .= $tpl->makeListItem( $key, $item, $options );
556 * Get personal tools for the user
560 * @return array Array of personal tools
562 public function getStructuredPersonalTools() {
563 $tpl = $this->setupTemplateForOutput();
564 $tpl->set( 'personal_urls', $this->buildPersonalUrls() );
566 return $tpl->getPersonalTools();
570 * Format language name for use in sidebar interlanguage links list.
571 * By default it is capitalized.
573 * @param string $name Language name, e.g. "English" or "español"
577 function formatLanguageName( $name ) {
578 return $this->getLanguage()->ucfirst( $name );
582 * Output the string, or print error message if it's
583 * an error object of the appropriate type.
584 * For the base class, assume strings all around.
589 function printOrError( $str ) {
594 * Output a boolean indicating if buildPersonalUrls should output separate
595 * login and create account links or output a combined link
596 * By default we simply return a global config setting that affects most skins
597 * This is setup as a method so that like with $wgLogo and getLogo() a skin
598 * can override this setting and always output one or the other if it has
599 * a reason it can't output one of the two modes.
602 function useCombinedLoginLink() {
603 global $wgUseCombinedLoginLink;
604 return $wgUseCombinedLoginLink;
608 * build array of urls for personal toolbar
611 protected function buildPersonalUrls() {
612 $title = $this->getTitle();
613 $request = $this->getRequest();
614 $pageurl = $title->getLocalURL();
615 $authManager = AuthManager
::singleton();
617 /* set up the default links for the personal toolbar */
620 # Due to T34276, if a user does not have read permissions,
621 # $this->getTitle() will just give Special:Badtitle, which is
622 # not especially useful as a returnto parameter. Use the title
623 # from the request instead, if there was one.
624 if ( $this->getUser()->isAllowed( 'read' ) ) {
625 $page = $this->getTitle();
627 $page = Title
::newFromText( $request->getVal( 'title', '' ) );
629 $page = $request->getVal( 'returnto', $page );
631 if ( strval( $page ) !== '' ) {
632 $a['returnto'] = $page;
633 $query = $request->getVal( 'returntoquery', $this->thisquery
);
634 if ( $query != '' ) {
635 $a['returntoquery'] = $query;
639 $returnto = wfArrayToCgi( $a );
640 if ( $this->loggedin
) {
641 $personal_urls['userpage'] = [
642 'text' => $this->username
,
643 'href' => &$this->userpageUrlDetails
['href'],
644 'class' => $this->userpageUrlDetails
['exists'] ?
false : 'new',
645 'exists' => $this->userpageUrlDetails
['exists'],
646 'active' => ( $this->userpageUrlDetails
['href'] == $pageurl ),
649 $usertalkUrlDetails = $this->makeTalkUrlDetails( $this->userpage
);
650 $personal_urls['mytalk'] = [
651 'text' => $this->msg( 'mytalk' )->text(),
652 'href' => &$usertalkUrlDetails['href'],
653 'class' => $usertalkUrlDetails['exists'] ?
false : 'new',
654 'exists' => $usertalkUrlDetails['exists'],
655 'active' => ( $usertalkUrlDetails['href'] == $pageurl )
657 $href = self
::makeSpecialUrl( 'Preferences' );
658 $personal_urls['preferences'] = [
659 'text' => $this->msg( 'mypreferences' )->text(),
661 'active' => ( $href == $pageurl )
664 if ( $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
665 $href = self
::makeSpecialUrl( 'Watchlist' );
666 $personal_urls['watchlist'] = [
667 'text' => $this->msg( 'mywatchlist' )->text(),
669 'active' => ( $href == $pageurl )
673 # We need to do an explicit check for Special:Contributions, as we
674 # have to match both the title, and the target, which could come
675 # from request values (Special:Contributions?target=Jimbo_Wales)
676 # or be specified in "sub page" form
677 # (Special:Contributions/Jimbo_Wales). The plot
678 # thickens, because the Title object is altered for special pages,
679 # so it doesn't contain the original alias-with-subpage.
680 $origTitle = Title
::newFromText( $request->getText( 'title' ) );
681 if ( $origTitle instanceof Title
&& $origTitle->isSpecialPage() ) {
682 list( $spName, $spPar ) = SpecialPageFactory
::resolveAlias( $origTitle->getText() );
683 $active = $spName == 'Contributions'
684 && ( ( $spPar && $spPar == $this->username
)
685 ||
$request->getText( 'target' ) == $this->username
);
690 $href = self
::makeSpecialUrlSubpage( 'Contributions', $this->username
);
691 $personal_urls['mycontris'] = [
692 'text' => $this->msg( 'mycontris' )->text(),
697 // if we can't set the user, we can't unset it either
698 if ( $request->getSession()->canSetUser() ) {
699 $personal_urls['logout'] = [
700 'text' => $this->msg( 'pt-userlogout' )->text(),
701 'href' => self
::makeSpecialUrl( 'Userlogout',
702 // userlogout link must always contain an & character, otherwise we might not be able
703 // to detect a buggy precaching proxy (T19790)
704 $title->isSpecial( 'Preferences' ) ?
'noreturnto' : $returnto ),
709 $useCombinedLoginLink = $this->useCombinedLoginLink();
710 if ( !$authManager->canCreateAccounts() ||
!$authManager->canAuthenticateNow() ) {
711 // don't show combined login/signup link if one of those is actually not available
712 $useCombinedLoginLink = false;
715 $loginlink = $this->getUser()->isAllowed( 'createaccount' ) && $useCombinedLoginLink
716 ?
'nav-login-createaccount'
720 'text' => $this->msg( $loginlink )->text(),
721 'href' => self
::makeSpecialUrl( 'Userlogin', $returnto ),
722 'active' => $title->isSpecial( 'Userlogin' )
723 ||
$title->isSpecial( 'CreateAccount' ) && $useCombinedLoginLink,
725 $createaccount_url = [
726 'text' => $this->msg( 'pt-createaccount' )->text(),
727 'href' => self
::makeSpecialUrl( 'CreateAccount', $returnto ),
728 'active' => $title->isSpecial( 'CreateAccount' ),
731 // No need to show Talk and Contributions to anons if they can't contribute!
732 if ( User
::groupHasPermission( '*', 'edit' ) ) {
733 // Because of caching, we can't link directly to the IP talk and
734 // contributions pages. Instead we use the special page shortcuts
735 // (which work correctly regardless of caching). This means we can't
736 // determine whether these links are active or not, but since major
737 // skins (MonoBook, Vector) don't use this information, it's not a
739 $personal_urls['anontalk'] = [
740 'text' => $this->msg( 'anontalk' )->text(),
741 'href' => self
::makeSpecialUrlSubpage( 'Mytalk', false ),
744 $personal_urls['anoncontribs'] = [
745 'text' => $this->msg( 'anoncontribs' )->text(),
746 'href' => self
::makeSpecialUrlSubpage( 'Mycontributions', false ),
752 $authManager->canCreateAccounts()
753 && $this->getUser()->isAllowed( 'createaccount' )
754 && !$useCombinedLoginLink
756 $personal_urls['createaccount'] = $createaccount_url;
759 if ( $authManager->canAuthenticateNow() ) {
760 $key = User
::groupHasPermission( '*', 'read' )
763 $personal_urls[$key] = $login_url;
767 Hooks
::runWithoutAbort( 'PersonalUrls', [ &$personal_urls, &$title, $this ] );
768 return $personal_urls;
772 * Builds an array with tab definition
774 * @param Title $title Page Where the tab links to
775 * @param string|array $message Message key or an array of message keys (will fall back)
776 * @param bool $selected Display the tab as selected
777 * @param string $query Query string attached to tab URL
778 * @param bool $checkEdit Check if $title exists and mark with .new if one doesn't
782 function tabAction( $title, $message, $selected, $query = '', $checkEdit = false ) {
785 $classes[] = 'selected';
788 if ( $checkEdit && !$title->isKnown() ) {
791 if ( $query !== '' ) {
792 $query = 'action=edit&redlink=1&' . $query;
794 $query = 'action=edit&redlink=1';
798 $linkClass = MediaWikiServices
::getInstance()->getLinkRenderer()->getLinkClasses( $title );
800 // wfMessageFallback will nicely accept $message as an array of fallbacks
801 // or just a single key
802 $msg = wfMessageFallback( $message )->setContext( $this->getContext() );
803 if ( is_array( $message ) ) {
804 // for hook compatibility just keep the last message name
805 $message = end( $message );
807 if ( $msg->exists() ) {
808 $text = $msg->text();
811 $text = $wgContLang->getConverter()->convertNamespace(
812 MWNamespace
::getSubject( $title->getNamespace() ) );
815 // Avoid PHP 7.1 warning of passing $this by reference
816 $skinTemplate = $this;
818 if ( !Hooks
::run( 'SkinTemplateTabAction', [ &$skinTemplate,
819 $title, $message, $selected, $checkEdit,
820 &$classes, &$query, &$text, &$result ] ) ) {
825 'class' => implode( ' ', $classes ),
827 'href' => $title->getLocalURL( $query ),
830 if ( $linkClass !== '' ) {
831 $result['link-class'] = $linkClass;
837 function makeTalkUrlDetails( $name, $urlaction = '' ) {
838 $title = Title
::newFromText( $name );
839 if ( !is_object( $title ) ) {
840 throw new MWException( __METHOD__
. " given invalid pagename $name" );
842 $title = $title->getTalkPage();
843 self
::checkTitle( $title, $name );
845 'href' => $title->getLocalURL( $urlaction ),
846 'exists' => $title->isKnown(),
851 * @todo is this even used?
852 * @param string $name
853 * @param string $urlaction
856 function makeArticleUrlDetails( $name, $urlaction = '' ) {
857 $title = Title
::newFromText( $name );
858 $title = $title->getSubjectPage();
859 self
::checkTitle( $title, $name );
861 'href' => $title->getLocalURL( $urlaction ),
862 'exists' => $title->exists(),
867 * a structured array of links usually used for the tabs in a skin
869 * There are 4 standard sections
870 * namespaces: Used for namespace tabs like special, page, and talk namespaces
871 * views: Used for primary page views like read, edit, history
872 * actions: Used for most extra page actions like deletion, protection, etc...
873 * variants: Used to list the language variants for the page
875 * Each section's value is a key/value array of links for that section.
876 * The links themselves have these common keys:
877 * - class: The css classes to apply to the tab
878 * - text: The text to display on the tab
879 * - href: The href for the tab to point to
880 * - rel: An optional rel= for the tab's link
881 * - redundant: If true the tab will be dropped in skins using content_actions
882 * this is useful for tabs like "Read" which only have meaning in skins that
883 * take special meaning from the grouped structure of content_navigation
885 * Views also have an extra key which can be used:
886 * - primary: If this is not true skins like vector may try to hide the tab
887 * when the user has limited space in their browser window
889 * content_navigation using code also expects these ids to be present on the
890 * links, however these are usually automatically generated by SkinTemplate
891 * itself and are not necessary when using a hook. The only things these may
892 * matter to are people modifying content_navigation after it's initial creation:
893 * - id: A "preferred" id, most skins are best off outputting this preferred
894 * id for best compatibility.
895 * - tooltiponly: This is set to true for some tabs in cases where the system
896 * believes that the accesskey should not be added to the tab.
900 protected function buildContentNavigationUrls() {
901 global $wgDisableLangConversion;
903 // Display tabs for the relevant title rather than always the title itself
904 $title = $this->getRelevantTitle();
905 $onPage = $title->equals( $this->getTitle() );
907 $out = $this->getOutput();
908 $request = $this->getRequest();
909 $user = $this->getUser();
911 $content_navigation = [
919 $action = $request->getVal( 'action', 'view' );
921 $userCanRead = $title->quickUserCan( 'read', $user );
923 // Avoid PHP 7.1 warning of passing $this by reference
924 $skinTemplate = $this;
925 $preventActiveTabs = false;
926 Hooks
::run( 'SkinTemplatePreventOtherActiveTabs', [ &$skinTemplate, &$preventActiveTabs ] );
928 // Checks if page is some kind of content
929 if ( $title->canExist() ) {
930 // Gets page objects for the related namespaces
931 $subjectPage = $title->getSubjectPage();
932 $talkPage = $title->getTalkPage();
934 // Determines if this is a talk page
935 $isTalk = $title->isTalkPage();
937 // Generates XML IDs from namespace names
938 $subjectId = $title->getNamespaceKey( '' );
940 if ( $subjectId == 'main' ) {
943 $talkId = "{$subjectId}_talk";
946 $skname = $this->skinname
;
948 // Adds namespace links
949 $subjectMsg = [ "nstab-$subjectId" ];
950 if ( $subjectPage->isMainPage() ) {
951 array_unshift( $subjectMsg, 'mainpage-nstab' );
953 $content_navigation['namespaces'][$subjectId] = $this->tabAction(
954 $subjectPage, $subjectMsg, !$isTalk && !$preventActiveTabs, '', $userCanRead
956 $content_navigation['namespaces'][$subjectId]['context'] = 'subject';
957 $content_navigation['namespaces'][$talkId] = $this->tabAction(
958 $talkPage, [ "nstab-$talkId", 'talk' ], $isTalk && !$preventActiveTabs, '', $userCanRead
960 $content_navigation['namespaces'][$talkId]['context'] = 'talk';
962 if ( $userCanRead ) {
963 // Adds "view" view link
964 if ( $title->isKnown() ) {
965 $content_navigation['views']['view'] = $this->tabAction(
966 $isTalk ?
$talkPage : $subjectPage,
967 [ "$skname-view-view", 'view' ],
968 ( $onPage && ( $action == 'view' ||
$action == 'purge' ) ), '', true
970 // signal to hide this from simple content_actions
971 $content_navigation['views']['view']['redundant'] = true;
974 $page = $this->canUseWikiPage() ?
$this->getWikiPage() : false;
975 $isRemoteContent = $page && !$page->isLocal();
977 // If it is a non-local file, show a link to the file in its own repository
978 // @todo abstract this for remote content that isn't a file
979 if ( $isRemoteContent ) {
980 $content_navigation['views']['view-foreign'] = [
982 'text' => wfMessageFallback( "$skname-view-foreign", 'view-foreign' )->
983 setContext( $this->getContext() )->
984 params( $page->getWikiDisplayName() )->text(),
985 'href' => $page->getSourceURL(),
990 // Checks if user can edit the current page if it exists or create it otherwise
991 if ( $title->quickUserCan( 'edit', $user )
992 && ( $title->exists() ||
$title->quickUserCan( 'create', $user ) )
994 // Builds CSS class for talk page links
995 $isTalkClass = $isTalk ?
' istalk' : '';
996 // Whether the user is editing the page
997 $isEditing = $onPage && ( $action == 'edit' ||
$action == 'submit' );
998 // Whether to show the "Add a new section" tab
999 // Checks if this is a current rev of talk page and is not forced to be hidden
1000 $showNewSection = !$out->forceHideNewSectionLink()
1001 && ( ( $isTalk && $this->isRevisionCurrent() ) ||
$out->showNewSectionLink() );
1002 $section = $request->getVal( 'section' );
1004 if ( $title->exists()
1005 ||
( $title->getNamespace() == NS_MEDIAWIKI
1006 && $title->getDefaultMessageText() !== false
1009 $msgKey = $isRemoteContent ?
'edit-local' : 'edit';
1011 $msgKey = $isRemoteContent ?
'create-local' : 'create';
1013 $content_navigation['views']['edit'] = [
1014 'class' => ( $isEditing && ( $section !== 'new' ||
!$showNewSection )
1018 'text' => wfMessageFallback( "$skname-view-$msgKey", $msgKey )
1019 ->setContext( $this->getContext() )->text(),
1020 'href' => $title->getLocalURL( $this->editUrlOptions() ),
1021 'primary' => !$isRemoteContent, // don't collapse this in vector
1025 if ( $showNewSection ) {
1026 // Adds new section link
1027 // $content_navigation['actions']['addsection']
1028 $content_navigation['views']['addsection'] = [
1029 'class' => ( $isEditing && $section == 'new' ) ?
'selected' : false,
1030 'text' => wfMessageFallback( "$skname-action-addsection", 'addsection' )
1031 ->setContext( $this->getContext() )->text(),
1032 'href' => $title->getLocalURL( 'action=edit§ion=new' )
1035 // Checks if the page has some kind of viewable source content
1036 } elseif ( $title->hasSourceText() ) {
1037 // Adds view source view link
1038 $content_navigation['views']['viewsource'] = [
1039 'class' => ( $onPage && $action == 'edit' ) ?
'selected' : false,
1040 'text' => wfMessageFallback( "$skname-action-viewsource", 'viewsource' )
1041 ->setContext( $this->getContext() )->text(),
1042 'href' => $title->getLocalURL( $this->editUrlOptions() ),
1043 'primary' => true, // don't collapse this in vector
1047 // Checks if the page exists
1048 if ( $title->exists() ) {
1049 // Adds history view link
1050 $content_navigation['views']['history'] = [
1051 'class' => ( $onPage && $action == 'history' ) ?
'selected' : false,
1052 'text' => wfMessageFallback( "$skname-view-history", 'history_short' )
1053 ->setContext( $this->getContext() )->text(),
1054 'href' => $title->getLocalURL( 'action=history' ),
1057 if ( $title->quickUserCan( 'delete', $user ) ) {
1058 $content_navigation['actions']['delete'] = [
1059 'class' => ( $onPage && $action == 'delete' ) ?
'selected' : false,
1060 'text' => wfMessageFallback( "$skname-action-delete", 'delete' )
1061 ->setContext( $this->getContext() )->text(),
1062 'href' => $title->getLocalURL( 'action=delete' )
1066 if ( $title->quickUserCan( 'move', $user ) ) {
1067 $moveTitle = SpecialPage
::getTitleFor( 'Movepage', $title->getPrefixedDBkey() );
1068 $content_navigation['actions']['move'] = [
1069 'class' => $this->getTitle()->isSpecial( 'Movepage' ) ?
'selected' : false,
1070 'text' => wfMessageFallback( "$skname-action-move", 'move' )
1071 ->setContext( $this->getContext() )->text(),
1072 'href' => $moveTitle->getLocalURL()
1076 // article doesn't exist or is deleted
1077 if ( $user->isAllowed( 'deletedhistory' ) ) {
1078 $n = $title->isDeleted();
1080 $undelTitle = SpecialPage
::getTitleFor( 'Undelete', $title->getPrefixedDBkey() );
1081 // If the user can't undelete but can view deleted
1082 // history show them a "View .. deleted" tab instead.
1083 $msgKey = $user->isAllowed( 'undelete' ) ?
'undelete' : 'viewdeleted';
1084 $content_navigation['actions']['undelete'] = [
1085 'class' => $this->getTitle()->isSpecial( 'Undelete' ) ?
'selected' : false,
1086 'text' => wfMessageFallback( "$skname-action-$msgKey", "{$msgKey}_short" )
1087 ->setContext( $this->getContext() )->numParams( $n )->text(),
1088 'href' => $undelTitle->getLocalURL()
1094 if ( $title->quickUserCan( 'protect', $user ) && $title->getRestrictionTypes() &&
1095 MWNamespace
::getRestrictionLevels( $title->getNamespace(), $user ) !== [ '' ]
1097 $mode = $title->isProtected() ?
'unprotect' : 'protect';
1098 $content_navigation['actions'][$mode] = [
1099 'class' => ( $onPage && $action == $mode ) ?
'selected' : false,
1100 'text' => wfMessageFallback( "$skname-action-$mode", $mode )
1101 ->setContext( $this->getContext() )->text(),
1102 'href' => $title->getLocalURL( "action=$mode" )
1106 // Checks if the user is logged in
1107 if ( $this->loggedin
&& $user->isAllowedAll( 'viewmywatchlist', 'editmywatchlist' ) ) {
1109 * The following actions use messages which, if made particular to
1110 * the any specific skins, would break the Ajax code which makes this
1111 * action happen entirely inline. OutputPage::getJSVars
1112 * defines a set of messages in a javascript object - and these
1113 * messages are assumed to be global for all skins. Without making
1114 * a change to that procedure these messages will have to remain as
1115 * the global versions.
1117 $mode = $user->isWatched( $title ) ?
'unwatch' : 'watch';
1118 $content_navigation['actions'][$mode] = [
1119 'class' => 'mw-watchlink ' . (
1120 $onPage && ( $action == 'watch' ||
$action == 'unwatch' ) ?
'selected' : ''
1122 // uses 'watch' or 'unwatch' message
1123 'text' => $this->msg( $mode )->text(),
1124 'href' => $title->getLocalURL( [ 'action' => $mode ] ),
1125 // Set a data-mw=interface attribute, which the mediawiki.page.ajax
1126 // module will look for to make sure it's a trusted link
1128 'mw' => 'interface',
1134 // Avoid PHP 7.1 warning of passing $this by reference
1135 $skinTemplate = $this;
1136 Hooks
::runWithoutAbort(
1137 'SkinTemplateNavigation',
1138 [ &$skinTemplate, &$content_navigation ]
1141 if ( $userCanRead && !$wgDisableLangConversion ) {
1142 $pageLang = $title->getPageLanguage();
1143 // Gets list of language variants
1144 $variants = $pageLang->getVariants();
1145 // Checks that language conversion is enabled and variants exist
1146 // And if it is not in the special namespace
1147 if ( count( $variants ) > 1 ) {
1148 // Gets preferred variant (note that user preference is
1149 // only possible for wiki content language variant)
1150 $preferred = $pageLang->getPreferredVariant();
1151 if ( Action
::getActionName( $this ) === 'view' ) {
1152 $params = $request->getQueryValues();
1153 unset( $params['title'] );
1157 // Loops over each variant
1158 foreach ( $variants as $code ) {
1159 // Gets variant name from language code
1160 $varname = $pageLang->getVariantname( $code );
1161 // Appends variant link
1162 $content_navigation['variants'][] = [
1163 'class' => ( $code == $preferred ) ?
'selected' : false,
1165 'href' => $title->getLocalURL( [ 'variant' => $code ] +
$params ),
1166 'lang' => LanguageCode
::bcp47( $code ),
1167 'hreflang' => LanguageCode
::bcp47( $code ),
1173 // If it's not content, it's got to be a special page
1174 $content_navigation['namespaces']['special'] = [
1175 'class' => 'selected',
1176 'text' => $this->msg( 'nstab-special' )->text(),
1177 'href' => $request->getRequestURL(), // @see: T4457, T4510
1178 'context' => 'subject'
1181 // Avoid PHP 7.1 warning of passing $this by reference
1182 $skinTemplate = $this;
1183 Hooks
::runWithoutAbort( 'SkinTemplateNavigation::SpecialPage',
1184 [ &$skinTemplate, &$content_navigation ] );
1187 // Avoid PHP 7.1 warning of passing $this by reference
1188 $skinTemplate = $this;
1189 // Equiv to SkinTemplateContentActions
1190 Hooks
::runWithoutAbort( 'SkinTemplateNavigation::Universal',
1191 [ &$skinTemplate, &$content_navigation ] );
1193 // Setup xml ids and tooltip info
1194 foreach ( $content_navigation as $section => &$links ) {
1195 foreach ( $links as $key => &$link ) {
1197 if ( isset( $link['context'] ) && $link['context'] == 'subject' ) {
1198 $xmlID = 'ca-nstab-' . $xmlID;
1199 } elseif ( isset( $link['context'] ) && $link['context'] == 'talk' ) {
1201 $link['rel'] = 'discussion';
1202 } elseif ( $section == 'variants' ) {
1203 $xmlID = 'ca-varlang-' . $xmlID;
1205 $xmlID = 'ca-' . $xmlID;
1207 $link['id'] = $xmlID;
1211 # We don't want to give the watch tab an accesskey if the
1212 # page is being edited, because that conflicts with the
1213 # accesskey on the watch checkbox. We also don't want to
1214 # give the edit tab an accesskey, because that's fairly
1215 # superfluous and conflicts with an accesskey (Ctrl-E) often
1216 # used for editing in Safari.
1217 if ( in_array( $action, [ 'edit', 'submit' ] ) ) {
1218 if ( isset( $content_navigation['views']['edit'] ) ) {
1219 $content_navigation['views']['edit']['tooltiponly'] = true;
1221 if ( isset( $content_navigation['actions']['watch'] ) ) {
1222 $content_navigation['actions']['watch']['tooltiponly'] = true;
1224 if ( isset( $content_navigation['actions']['unwatch'] ) ) {
1225 $content_navigation['actions']['unwatch']['tooltiponly'] = true;
1229 return $content_navigation;
1233 * an array of edit links by default used for the tabs
1234 * @param array $content_navigation
1237 private function buildContentActionUrls( $content_navigation ) {
1238 // content_actions has been replaced with content_navigation for backwards
1239 // compatibility and also for skins that just want simple tabs content_actions
1240 // is now built by flattening the content_navigation arrays into one
1242 $content_actions = [];
1244 foreach ( $content_navigation as $links ) {
1245 foreach ( $links as $key => $value ) {
1246 if ( isset( $value['redundant'] ) && $value['redundant'] ) {
1247 // Redundant tabs are dropped from content_actions
1251 // content_actions used to have ids built using the "ca-$key" pattern
1252 // so the xmlID based id is much closer to the actual $key that we want
1253 // for that reason we'll just strip out the ca- if present and use
1254 // the latter potion of the "id" as the $key
1255 if ( isset( $value['id'] ) && substr( $value['id'], 0, 3 ) == 'ca-' ) {
1256 $key = substr( $value['id'], 3 );
1259 if ( isset( $content_actions[$key] ) ) {
1260 wfDebug( __METHOD__
. ": Found a duplicate key for $key while flattening " .
1261 "content_navigation into content_actions.\n" );
1265 $content_actions[$key] = $value;
1269 return $content_actions;
1273 * build array of common navigation links
1276 protected function buildNavUrls() {
1277 global $wgUploadNavigationUrl;
1279 $out = $this->getOutput();
1280 $request = $this->getRequest();
1283 $nav_urls['mainpage'] = [ 'href' => self
::makeMainPageUrl() ];
1284 if ( $wgUploadNavigationUrl ) {
1285 $nav_urls['upload'] = [ 'href' => $wgUploadNavigationUrl ];
1286 } elseif ( UploadBase
::isEnabled() && UploadBase
::isAllowed( $this->getUser() ) === true ) {
1287 $nav_urls['upload'] = [ 'href' => self
::makeSpecialUrl( 'Upload' ) ];
1289 $nav_urls['upload'] = false;
1291 $nav_urls['specialpages'] = [ 'href' => self
::makeSpecialUrl( 'Specialpages' ) ];
1293 $nav_urls['print'] = false;
1294 $nav_urls['permalink'] = false;
1295 $nav_urls['info'] = false;
1296 $nav_urls['whatlinkshere'] = false;
1297 $nav_urls['recentchangeslinked'] = false;
1298 $nav_urls['contributions'] = false;
1299 $nav_urls['log'] = false;
1300 $nav_urls['blockip'] = false;
1301 $nav_urls['emailuser'] = false;
1302 $nav_urls['userrights'] = false;
1304 // A print stylesheet is attached to all pages, but nobody ever
1305 // figures that out. :) Add a link...
1306 if ( !$out->isPrintable() && ( $out->isArticle() ||
$this->getTitle()->isSpecialPage() ) ) {
1307 $nav_urls['print'] = [
1308 'text' => $this->msg( 'printableversion' )->text(),
1309 'href' => $this->getTitle()->getLocalURL(
1310 $request->appendQueryValue( 'printable', 'yes' ) )
1314 if ( $out->isArticle() ) {
1315 // Also add a "permalink" while we're at it
1316 $revid = $this->getRevisionId();
1318 $nav_urls['permalink'] = [
1319 'text' => $this->msg( 'permalink' )->text(),
1320 'href' => $this->getTitle()->getLocalURL( "oldid=$revid" )
1324 // Avoid PHP 7.1 warning of passing $this by reference
1325 $skinTemplate = $this;
1326 // Use the copy of revision ID in case this undocumented, shady hook tries to mess with internals
1327 Hooks
::run( 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink',
1328 [ &$skinTemplate, &$nav_urls, &$revid, &$revid ] );
1331 if ( $out->isArticleRelated() ) {
1332 $nav_urls['whatlinkshere'] = [
1333 'href' => SpecialPage
::getTitleFor( 'Whatlinkshere', $this->thispage
)->getLocalURL()
1336 $nav_urls['info'] = [
1337 'text' => $this->msg( 'pageinfo-toolboxlink' )->text(),
1338 'href' => $this->getTitle()->getLocalURL( "action=info" )
1341 if ( $this->getTitle()->exists() ||
$this->getTitle()->inNamespace( NS_CATEGORY
) ) {
1342 $nav_urls['recentchangeslinked'] = [
1343 'href' => SpecialPage
::getTitleFor( 'Recentchangeslinked', $this->thispage
)->getLocalURL()
1348 $user = $this->getRelevantUser();
1350 $rootUser = $user->getName();
1352 $nav_urls['contributions'] = [
1353 'text' => $this->msg( 'contributions', $rootUser )->text(),
1354 'href' => self
::makeSpecialUrlSubpage( 'Contributions', $rootUser ),
1355 'tooltip-params' => [ $rootUser ],
1358 $nav_urls['log'] = [
1359 'href' => self
::makeSpecialUrlSubpage( 'Log', $rootUser )
1362 if ( $this->getUser()->isAllowed( 'block' ) ) {
1363 $nav_urls['blockip'] = [
1364 'text' => $this->msg( 'blockip', $rootUser )->text(),
1365 'href' => self
::makeSpecialUrlSubpage( 'Block', $rootUser )
1369 if ( $this->showEmailUser( $user ) ) {
1370 $nav_urls['emailuser'] = [
1371 'text' => $this->msg( 'tool-link-emailuser', $rootUser )->text(),
1372 'href' => self
::makeSpecialUrlSubpage( 'Emailuser', $rootUser ),
1373 'tooltip-params' => [ $rootUser ],
1377 if ( !$user->isAnon() ) {
1378 $sur = new UserrightsPage
;
1379 $sur->setContext( $this->getContext() );
1380 $canChange = $sur->userCanChangeRights( $user );
1381 $nav_urls['userrights'] = [
1382 'text' => $this->msg(
1383 $canChange ?
'tool-link-userrights' : 'tool-link-userrights-readonly',
1386 'href' => self
::makeSpecialUrlSubpage( 'Userrights', $rootUser )
1395 * Generate strings used for xml 'id' names
1398 protected function getNameSpaceKey() {
1399 return $this->getTitle()->getNamespaceKey();