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 * Create the template engine object; we feed it a bunch of data
61 * and eventually it spits out some HTML. Should have interface
62 * roughly equivalent to PHPTAL 0.7.
64 * @param string $classname
65 * @param bool|string $repository Subdirectory where we keep template files
66 * @param bool|string $cache_dir
67 * @return QuickTemplate
70 function setupTemplate( $classname, $repository = false, $cache_dir = false ) {
71 return new $classname( $this->getConfig() );
75 * Generates array of language links for the current page
79 public function getLanguages() {
80 global $wgHideInterlanguageLinks;
81 if ( $wgHideInterlanguageLinks ) {
85 $userLang = $this->getLanguage();
88 foreach ( $this->getOutput()->getLanguageLinks() as $languageLinkText ) {
89 $class = 'interlanguage-link interwiki-' . explode( ':', $languageLinkText, 2 )[0];
91 $languageLinkTitle = Title
::newFromText( $languageLinkText );
92 if ( $languageLinkTitle ) {
93 $ilInterwikiCode = $languageLinkTitle->getInterwiki();
94 $ilLangName = Language
::fetchLanguageName( $ilInterwikiCode );
96 if ( strval( $ilLangName ) === '' ) {
97 $ilDisplayTextMsg = wfMessage( "interlanguage-link-$ilInterwikiCode" );
98 if ( !$ilDisplayTextMsg->isDisabled() ) {
99 // Use custom MW message for the display text
100 $ilLangName = $ilDisplayTextMsg->text();
102 // Last resort: fallback to the language link target
103 $ilLangName = $languageLinkText;
106 // Use the language autonym as display text
107 $ilLangName = $this->formatLanguageName( $ilLangName );
110 // CLDR extension or similar is required to localize the language name;
111 // otherwise we'll end up with the autonym again.
112 $ilLangLocalName = Language
::fetchLanguageName(
117 $languageLinkTitleText = $languageLinkTitle->getText();
118 if ( $ilLangLocalName === '' ) {
119 $ilFriendlySiteName = wfMessage( "interlanguage-link-sitename-$ilInterwikiCode" );
120 if ( !$ilFriendlySiteName->isDisabled() ) {
121 if ( $languageLinkTitleText === '' ) {
122 $ilTitle = wfMessage(
123 'interlanguage-link-title-nonlangonly',
124 $ilFriendlySiteName->text()
127 $ilTitle = wfMessage(
128 'interlanguage-link-title-nonlang',
129 $languageLinkTitleText,
130 $ilFriendlySiteName->text()
134 // we have nothing friendly to put in the title, so fall back to
135 // displaying the interlanguage link itself in the title text
136 // (similar to what is done in page content)
137 $ilTitle = $languageLinkTitle->getInterwiki() .
138 ":$languageLinkTitleText";
140 } elseif ( $languageLinkTitleText === '' ) {
141 $ilTitle = wfMessage(
142 'interlanguage-link-title-langonly',
146 $ilTitle = wfMessage(
147 'interlanguage-link-title',
148 $languageLinkTitleText,
153 $ilInterwikiCodeBCP47 = LanguageCode
::bcp47( $ilInterwikiCode );
155 'href' => $languageLinkTitle->getFullURL(),
156 'text' => $ilLangName,
159 'link-class' => 'interlanguage-link-target',
160 'lang' => $ilInterwikiCodeBCP47,
161 'hreflang' => $ilInterwikiCodeBCP47,
164 'SkinTemplateGetLanguageLink',
165 [ &$languageLink, $languageLinkTitle, $this->getTitle(), $this->getOutput() ]
167 $languageLinks[] = $languageLink;
171 return $languageLinks;
174 protected function setupTemplateForOutput() {
175 $request = $this->getRequest();
176 $user = $this->getUser();
177 $title = $this->getTitle();
179 $tpl = $this->setupTemplate( $this->template
, 'skins' );
181 $this->thispage
= $title->getPrefixedDBkey();
182 $this->titletxt
= $title->getPrefixedText();
183 $this->userpage
= $user->getUserPage()->getPrefixedText();
185 if ( !$request->wasPosted() ) {
186 $query = $request->getValues();
187 unset( $query['title'] );
188 unset( $query['returnto'] );
189 unset( $query['returntoquery'] );
191 $this->thisquery
= wfArrayToCgi( $query );
192 $this->loggedin
= $user->isLoggedIn();
193 $this->username
= $user->getName();
195 if ( $this->loggedin
) {
196 $this->userpageUrlDetails
= self
::makeUrlDetails( $this->userpage
);
198 # This won't be used in the standard skins, but we define it to preserve the interface
199 # To save time, we check for existence
200 $this->userpageUrlDetails
= self
::makeKnownUrlDetails( $this->userpage
);
207 * initialize various variables and generate the template
209 * @param OutputPage|null $out
211 function outputPage( OutputPage
$out = null ) {
212 Profiler
::instance()->setTemplated( true );
215 if ( $out !== null ) {
216 // Deprecated since 1.20, note added in 1.25
217 wfDeprecated( __METHOD__
, '1.25' );
218 $oldContext = $this->getContext();
219 $this->setContext( $out->getContext() );
222 $out = $this->getOutput();
224 $this->initPage( $out );
225 $tpl = $this->prepareQuickTemplate();
227 $res = $tpl->execute();
229 // result may be an error
230 $this->printOrError( $res );
233 $this->setContext( $oldContext );
238 * Wrap the body text with language information and identifiable element
240 * @param Title $title
241 * @param string $html body text
242 * @return string html
244 protected function wrapHTML( $title, $html ) {
245 # An ID that includes the actual body text; without categories, contentSub, ...
246 $realBodyAttribs = [ 'id' => 'mw-content-text' ];
248 # Add a mw-content-ltr/rtl class to be able to style based on text
249 # direction when the content is different from the UI language (only
251 # Most information on special pages and file pages is in user language,
252 # rather than content language, so those will not get this
253 if ( Action
::getActionName( $this ) === 'view' &&
254 ( !$title->inNamespaces( NS_SPECIAL
, NS_FILE
) ||
$title->isRedirect() ) ) {
255 $pageLang = $title->getPageViewLanguage();
256 $realBodyAttribs['lang'] = $pageLang->getHtmlCode();
257 $realBodyAttribs['dir'] = $pageLang->getDir();
258 $realBodyAttribs['class'] = 'mw-content-' . $pageLang->getDir();
261 return Html
::rawElement( 'div', $realBodyAttribs, $html );
265 * initialize various variables and generate the template
268 * @return QuickTemplate The template to be executed by outputPage
270 protected function prepareQuickTemplate() {
271 global $wgScript, $wgStylePath, $wgMimeType, $wgJsMimeType,
272 $wgSitename, $wgLogo, $wgMaxCredits,
273 $wgShowCreditsIfMax, $wgArticlePath,
274 $wgScriptPath, $wgServer;
276 $title = $this->getTitle();
277 $request = $this->getRequest();
278 $out = $this->getOutput();
279 $tpl = $this->setupTemplateForOutput();
281 $tpl->set( 'title', $out->getPageTitle() );
282 $tpl->set( 'pagetitle', $out->getHTMLTitle() );
283 $tpl->set( 'displaytitle', $out->mPageLinkTitle
);
285 $tpl->set( 'thispage', $this->thispage
);
286 $tpl->set( 'titleprefixeddbkey', $this->thispage
);
287 $tpl->set( 'titletext', $title->getText() );
288 $tpl->set( 'articleid', $title->getArticleID() );
290 $tpl->set( 'isarticle', $out->isArticle() );
292 $subpagestr = $this->subPageSubtitle();
293 if ( $subpagestr !== '' ) {
294 $subpagestr = '<span class="subpages">' . $subpagestr . '</span>';
296 $tpl->set( 'subtitle', $subpagestr . $out->getSubtitle() );
298 $undelete = $this->getUndeleteLink();
299 if ( $undelete === '' ) {
300 $tpl->set( 'undelete', '' );
302 $tpl->set( 'undelete', '<span class="subpages">' . $undelete . '</span>' );
305 $tpl->set( 'catlinks', $this->getCategories() );
306 if ( $out->isSyndicated() ) {
308 foreach ( $out->getSyndicationLinks() as $format => $link ) {
310 // Messages: feed-atom, feed-rss
311 'text' => $this->msg( "feed-$format" )->text(),
315 $tpl->set( 'feeds', $feeds );
317 $tpl->set( 'feeds', false );
320 $tpl->set( 'mimetype', $wgMimeType );
321 $tpl->set( 'jsmimetype', $wgJsMimeType );
322 $tpl->set( 'charset', 'UTF-8' );
323 $tpl->set( 'wgScript', $wgScript );
324 $tpl->set( 'skinname', $this->skinname
);
325 $tpl->set( 'skinclass', static::class );
326 $tpl->set( 'skin', $this );
327 $tpl->set( 'stylename', $this->stylename
);
328 $tpl->set( 'printable', $out->isPrintable() );
329 $tpl->set( 'handheld', $request->getBool( 'handheld' ) );
330 $tpl->set( 'loggedin', $this->loggedin
);
331 $tpl->set( 'notspecialpage', !$title->isSpecialPage() );
332 $tpl->set( 'searchaction', $this->escapeSearchLink() );
333 $tpl->set( 'searchtitle', SpecialPage
::getTitleFor( 'Search' )->getPrefixedDBkey() );
334 $tpl->set( 'search', trim( $request->getVal( 'search' ) ) );
335 $tpl->set( 'stylepath', $wgStylePath );
336 $tpl->set( 'articlepath', $wgArticlePath );
337 $tpl->set( 'scriptpath', $wgScriptPath );
338 $tpl->set( 'serverurl', $wgServer );
339 $tpl->set( 'logopath', $wgLogo );
340 $tpl->set( 'sitename', $wgSitename );
342 $userLang = $this->getLanguage();
343 $userLangCode = $userLang->getHtmlCode();
344 $userLangDir = $userLang->getDir();
346 $tpl->set( 'lang', $userLangCode );
347 $tpl->set( 'dir', $userLangDir );
348 $tpl->set( 'rtl', $userLang->isRTL() );
350 $tpl->set( 'capitalizeallnouns', $userLang->capitalizeAllNouns() ?
' capitalize-all-nouns' : '' );
351 $tpl->set( 'showjumplinks', true ); // showjumplinks preference has been removed
352 $tpl->set( 'username', $this->loggedin ?
$this->username
: null );
353 $tpl->set( 'userpage', $this->userpage
);
354 $tpl->set( 'userpageurl', $this->userpageUrlDetails
['href'] );
355 $tpl->set( 'userlang', $userLangCode );
357 // Users can have their language set differently than the
358 // content of the wiki. For these users, tell the web browser
359 // that interface elements are in a different language.
360 $tpl->set( 'userlangattributes', '' );
361 $tpl->set( 'specialpageattributes', '' ); # obsolete
362 // Used by VectorBeta to insert HTML before content but after the
363 // heading for the page title. Defaults to empty string.
364 $tpl->set( 'prebodyhtml', '' );
366 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
368 $userLangCode !== $contLang->getHtmlCode() ||
369 $userLangDir !== $contLang->getDir()
371 $escUserlang = htmlspecialchars( $userLangCode );
372 $escUserdir = htmlspecialchars( $userLangDir );
373 // Attributes must be in double quotes because htmlspecialchars() doesn't
374 // escape single quotes
375 $attrs = " lang=\"$escUserlang\" dir=\"$escUserdir\"";
376 $tpl->set( 'userlangattributes', $attrs );
379 $tpl->set( 'newtalk', $this->getNewtalks() );
380 $tpl->set( 'logo', $this->logoText() );
382 $tpl->set( 'copyright', false );
384 $tpl->set( 'viewcount', false );
385 $tpl->set( 'lastmod', false );
386 $tpl->set( 'credits', false );
387 $tpl->set( 'numberofwatchingusers', false );
388 if ( $out->isArticle() && $title->exists() ) {
389 if ( $this->isRevisionCurrent() ) {
390 if ( $wgMaxCredits != 0 ) {
391 $tpl->set( 'credits', Action
::factory( 'credits', $this->getWikiPage(),
392 $this->getContext() )->getCredits( $wgMaxCredits, $wgShowCreditsIfMax ) );
394 $tpl->set( 'lastmod', $this->lastModified() );
397 $tpl->set( 'copyright', $this->getCopyright() );
400 $tpl->set( 'copyrightico', $this->getCopyrightIcon() );
401 $tpl->set( 'poweredbyico', $this->getPoweredBy() );
402 $tpl->set( 'disclaimer', $this->disclaimerLink() );
403 $tpl->set( 'privacy', $this->privacyLink() );
404 $tpl->set( 'about', $this->aboutLink() );
406 $tpl->set( 'footerlinks', [
409 'numberofwatchingusers',
420 global $wgFooterIcons;
421 $tpl->set( 'footericons', $wgFooterIcons );
422 foreach ( $tpl->data
['footericons'] as $footerIconsKey => &$footerIconsBlock ) {
423 if ( count( $footerIconsBlock ) > 0 ) {
424 foreach ( $footerIconsBlock as &$footerIcon ) {
425 if ( isset( $footerIcon['src'] ) ) {
426 if ( !isset( $footerIcon['width'] ) ) {
427 $footerIcon['width'] = 88;
429 if ( !isset( $footerIcon['height'] ) ) {
430 $footerIcon['height'] = 31;
435 unset( $tpl->data
['footericons'][$footerIconsKey] );
439 $tpl->set( 'indicators', $out->getIndicators() );
441 $tpl->set( 'sitenotice', $this->getSiteNotice() );
442 $tpl->set( 'printfooter', $this->printSource() );
443 // Wrap the bodyText with #mw-content-text element
444 $out->mBodytext
= $this->wrapHTML( $title, $out->mBodytext
);
445 $tpl->set( 'bodytext', $out->mBodytext
);
447 $language_urls = $this->getLanguages();
448 if ( count( $language_urls ) ) {
449 $tpl->set( 'language_urls', $language_urls );
451 $tpl->set( 'language_urls', false );
455 $tpl->set( 'personal_urls', $this->buildPersonalUrls() );
456 $content_navigation = $this->buildContentNavigationUrls();
457 $content_actions = $this->buildContentActionUrls( $content_navigation );
458 $tpl->set( 'content_navigation', $content_navigation );
459 $tpl->set( 'content_actions', $content_actions );
461 $tpl->set( 'sidebar', $this->buildSidebar() );
462 $tpl->set( 'nav_urls', $this->buildNavUrls() );
464 // Do this last in case hooks above add bottom scripts
465 $tpl->set( 'bottomscripts', $this->bottomScripts() );
467 // Set the head scripts near the end, in case the above actions resulted in added scripts
468 $tpl->set( 'headelement', $out->headElement( $this ) );
470 $tpl->set( 'debug', '' );
471 $tpl->set( 'debughtml', $this->generateDebugHTML() );
472 $tpl->set( 'reporttime', wfReportTime( $out->getCSPNonce() ) );
474 // Avoid PHP 7.1 warning of passing $this by reference
475 $skinTemplate = $this;
476 // original version by hansm
477 if ( !Hooks
::run( 'SkinTemplateOutputPageBeforeExec', [ &$skinTemplate, &$tpl ] ) ) {
478 wfDebug( __METHOD__
. ": Hook SkinTemplateOutputPageBeforeExec broke outputPage execution!\n" );
481 // Set the bodytext to another key so that skins can just output it on its own
482 // and output printfooter and debughtml separately
483 $tpl->set( 'bodycontent', $tpl->data
['bodytext'] );
485 // Append printfooter and debughtml onto bodytext so that skins that
486 // were already using bodytext before they were split out don't suddenly
487 // start not outputting information.
488 $tpl->data
['bodytext'] .= Html
::rawElement(
490 [ 'class' => 'printfooter' ],
491 "\n{$tpl->data['printfooter']}"
493 $tpl->data
['bodytext'] .= $tpl->data
['debughtml'];
495 // allow extensions adding stuff after the page content.
496 // See Skin::afterContentHook() for further documentation.
497 $tpl->set( 'dataAfterContent', $this->afterContentHook() );
503 * Get the HTML for the p-personal list
506 public function getPersonalToolsList() {
507 return $this->makePersonalToolsList();
511 * Get the HTML for the personal tools list
515 * @param array|null $personalTools
516 * @param array $options
519 public function makePersonalToolsList( $personalTools = null, $options = [] ) {
520 $tpl = $this->setupTemplateForOutput();
521 $tpl->set( 'personal_urls', $this->buildPersonalUrls() );
524 if ( $personalTools === null ) {
525 $personalTools = $tpl->getPersonalTools();
528 foreach ( $personalTools as $key => $item ) {
529 $html .= $tpl->makeListItem( $key, $item, $options );
536 * Get personal tools for the user
540 * @return array Array of personal tools
542 public function getStructuredPersonalTools() {
543 $tpl = $this->setupTemplateForOutput();
544 $tpl->set( 'personal_urls', $this->buildPersonalUrls() );
546 return $tpl->getPersonalTools();
550 * Format language name for use in sidebar interlanguage links list.
551 * By default it is capitalized.
553 * @param string $name Language name, e.g. "English" or "español"
557 function formatLanguageName( $name ) {
558 return $this->getLanguage()->ucfirst( $name );
562 * Output the string, or print error message if it's
563 * an error object of the appropriate type.
564 * For the base class, assume strings all around.
569 function printOrError( $str ) {
574 * Output a boolean indicating if buildPersonalUrls should output separate
575 * login and create account links or output a combined link
576 * By default we simply return a global config setting that affects most skins
577 * This is setup as a method so that like with $wgLogo and getLogo() a skin
578 * can override this setting and always output one or the other if it has
579 * a reason it can't output one of the two modes.
582 function useCombinedLoginLink() {
583 global $wgUseCombinedLoginLink;
584 return $wgUseCombinedLoginLink;
588 * build array of urls for personal toolbar
591 protected function buildPersonalUrls() {
592 $title = $this->getTitle();
593 $request = $this->getRequest();
594 $pageurl = $title->getLocalURL();
595 $authManager = AuthManager
::singleton();
597 /* set up the default links for the personal toolbar */
600 # Due to T34276, if a user does not have read permissions,
601 # $this->getTitle() will just give Special:Badtitle, which is
602 # not especially useful as a returnto parameter. Use the title
603 # from the request instead, if there was one.
604 if ( $this->getUser()->isAllowed( 'read' ) ) {
605 $page = $this->getTitle();
607 $page = Title
::newFromText( $request->getVal( 'title', '' ) );
609 $page = $request->getVal( 'returnto', $page );
611 if ( strval( $page ) !== '' ) {
612 $a['returnto'] = $page;
613 $query = $request->getVal( 'returntoquery', $this->thisquery
);
614 if ( $query != '' ) {
615 $a['returntoquery'] = $query;
619 $returnto = wfArrayToCgi( $a );
620 if ( $this->loggedin
) {
621 $personal_urls['userpage'] = [
622 'text' => $this->username
,
623 'href' => &$this->userpageUrlDetails
['href'],
624 'class' => $this->userpageUrlDetails
['exists'] ?
false : 'new',
625 'exists' => $this->userpageUrlDetails
['exists'],
626 'active' => ( $this->userpageUrlDetails
['href'] == $pageurl ),
629 $usertalkUrlDetails = $this->makeTalkUrlDetails( $this->userpage
);
630 $personal_urls['mytalk'] = [
631 'text' => $this->msg( 'mytalk' )->text(),
632 'href' => &$usertalkUrlDetails['href'],
633 'class' => $usertalkUrlDetails['exists'] ?
false : 'new',
634 'exists' => $usertalkUrlDetails['exists'],
635 'active' => ( $usertalkUrlDetails['href'] == $pageurl )
637 $href = self
::makeSpecialUrl( 'Preferences' );
638 $personal_urls['preferences'] = [
639 'text' => $this->msg( 'mypreferences' )->text(),
641 'active' => ( $href == $pageurl )
644 if ( $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
645 $href = self
::makeSpecialUrl( 'Watchlist' );
646 $personal_urls['watchlist'] = [
647 'text' => $this->msg( 'mywatchlist' )->text(),
649 'active' => ( $href == $pageurl )
653 # We need to do an explicit check for Special:Contributions, as we
654 # have to match both the title, and the target, which could come
655 # from request values (Special:Contributions?target=Jimbo_Wales)
656 # or be specified in "sub page" form
657 # (Special:Contributions/Jimbo_Wales). The plot
658 # thickens, because the Title object is altered for special pages,
659 # so it doesn't contain the original alias-with-subpage.
660 $origTitle = Title
::newFromText( $request->getText( 'title' ) );
661 if ( $origTitle instanceof Title
&& $origTitle->isSpecialPage() ) {
662 list( $spName, $spPar ) = SpecialPageFactory
::resolveAlias( $origTitle->getText() );
663 $active = $spName == 'Contributions'
664 && ( ( $spPar && $spPar == $this->username
)
665 ||
$request->getText( 'target' ) == $this->username
);
670 $href = self
::makeSpecialUrlSubpage( 'Contributions', $this->username
);
671 $personal_urls['mycontris'] = [
672 'text' => $this->msg( 'mycontris' )->text(),
677 // if we can't set the user, we can't unset it either
678 if ( $request->getSession()->canSetUser() ) {
679 $personal_urls['logout'] = [
680 'text' => $this->msg( 'pt-userlogout' )->text(),
681 'href' => self
::makeSpecialUrl( 'Userlogout',
682 // userlogout link must always contain an & character, otherwise we might not be able
683 // to detect a buggy precaching proxy (T19790)
684 $title->isSpecial( 'Preferences' ) ?
'noreturnto' : $returnto ),
689 $useCombinedLoginLink = $this->useCombinedLoginLink();
690 if ( !$authManager->canCreateAccounts() ||
!$authManager->canAuthenticateNow() ) {
691 // don't show combined login/signup link if one of those is actually not available
692 $useCombinedLoginLink = false;
695 $loginlink = $this->getUser()->isAllowed( 'createaccount' ) && $useCombinedLoginLink
696 ?
'nav-login-createaccount'
700 'text' => $this->msg( $loginlink )->text(),
701 'href' => self
::makeSpecialUrl( 'Userlogin', $returnto ),
702 'active' => $title->isSpecial( 'Userlogin' )
703 ||
$title->isSpecial( 'CreateAccount' ) && $useCombinedLoginLink,
705 $createaccount_url = [
706 'text' => $this->msg( 'pt-createaccount' )->text(),
707 'href' => self
::makeSpecialUrl( 'CreateAccount', $returnto ),
708 'active' => $title->isSpecial( 'CreateAccount' ),
711 // No need to show Talk and Contributions to anons if they can't contribute!
712 if ( User
::groupHasPermission( '*', 'edit' ) ) {
713 // Because of caching, we can't link directly to the IP talk and
714 // contributions pages. Instead we use the special page shortcuts
715 // (which work correctly regardless of caching). This means we can't
716 // determine whether these links are active or not, but since major
717 // skins (MonoBook, Vector) don't use this information, it's not a
719 $personal_urls['anontalk'] = [
720 'text' => $this->msg( 'anontalk' )->text(),
721 'href' => self
::makeSpecialUrlSubpage( 'Mytalk', false ),
724 $personal_urls['anoncontribs'] = [
725 'text' => $this->msg( 'anoncontribs' )->text(),
726 'href' => self
::makeSpecialUrlSubpage( 'Mycontributions', false ),
732 $authManager->canCreateAccounts()
733 && $this->getUser()->isAllowed( 'createaccount' )
734 && !$useCombinedLoginLink
736 $personal_urls['createaccount'] = $createaccount_url;
739 if ( $authManager->canAuthenticateNow() ) {
740 $key = User
::groupHasPermission( '*', 'read' )
743 $personal_urls[$key] = $login_url;
747 Hooks
::runWithoutAbort( 'PersonalUrls', [ &$personal_urls, &$title, $this ] );
748 return $personal_urls;
752 * Builds an array with tab definition
754 * @param Title $title Page Where the tab links to
755 * @param string|array $message Message key or an array of message keys (will fall back)
756 * @param bool $selected Display the tab as selected
757 * @param string $query Query string attached to tab URL
758 * @param bool $checkEdit Check if $title exists and mark with .new if one doesn't
762 function tabAction( $title, $message, $selected, $query = '', $checkEdit = false ) {
765 $classes[] = 'selected';
768 if ( $checkEdit && !$title->isKnown() ) {
771 if ( $query !== '' ) {
772 $query = 'action=edit&redlink=1&' . $query;
774 $query = 'action=edit&redlink=1';
778 $linkClass = MediaWikiServices
::getInstance()->getLinkRenderer()->getLinkClasses( $title );
780 // wfMessageFallback will nicely accept $message as an array of fallbacks
781 // or just a single key
782 $msg = wfMessageFallback( $message )->setContext( $this->getContext() );
783 if ( is_array( $message ) ) {
784 // for hook compatibility just keep the last message name
785 $message = end( $message );
787 if ( $msg->exists() ) {
788 $text = $msg->text();
790 $text = MediaWikiServices
::getInstance()->getContentLanguage()->getConverter()->
791 convertNamespace( MWNamespace
::getSubject( $title->getNamespace() ) );
794 // Avoid PHP 7.1 warning of passing $this by reference
795 $skinTemplate = $this;
797 if ( !Hooks
::run( 'SkinTemplateTabAction', [ &$skinTemplate,
798 $title, $message, $selected, $checkEdit,
799 &$classes, &$query, &$text, &$result ] ) ) {
804 'class' => implode( ' ', $classes ),
806 'href' => $title->getLocalURL( $query ),
809 if ( $linkClass !== '' ) {
810 $result['link-class'] = $linkClass;
816 function makeTalkUrlDetails( $name, $urlaction = '' ) {
817 $title = Title
::newFromText( $name );
818 if ( !is_object( $title ) ) {
819 throw new MWException( __METHOD__
. " given invalid pagename $name" );
821 $title = $title->getTalkPage();
822 self
::checkTitle( $title, $name );
824 'href' => $title->getLocalURL( $urlaction ),
825 'exists' => $title->isKnown(),
830 * @todo is this even used?
831 * @param string $name
832 * @param string $urlaction
835 function makeArticleUrlDetails( $name, $urlaction = '' ) {
836 $title = Title
::newFromText( $name );
837 $title = $title->getSubjectPage();
838 self
::checkTitle( $title, $name );
840 'href' => $title->getLocalURL( $urlaction ),
841 'exists' => $title->exists(),
846 * a structured array of links usually used for the tabs in a skin
848 * There are 4 standard sections
849 * namespaces: Used for namespace tabs like special, page, and talk namespaces
850 * views: Used for primary page views like read, edit, history
851 * actions: Used for most extra page actions like deletion, protection, etc...
852 * variants: Used to list the language variants for the page
854 * Each section's value is a key/value array of links for that section.
855 * The links themselves have these common keys:
856 * - class: The css classes to apply to the tab
857 * - text: The text to display on the tab
858 * - href: The href for the tab to point to
859 * - rel: An optional rel= for the tab's link
860 * - redundant: If true the tab will be dropped in skins using content_actions
861 * this is useful for tabs like "Read" which only have meaning in skins that
862 * take special meaning from the grouped structure of content_navigation
864 * Views also have an extra key which can be used:
865 * - primary: If this is not true skins like vector may try to hide the tab
866 * when the user has limited space in their browser window
868 * content_navigation using code also expects these ids to be present on the
869 * links, however these are usually automatically generated by SkinTemplate
870 * itself and are not necessary when using a hook. The only things these may
871 * matter to are people modifying content_navigation after it's initial creation:
872 * - id: A "preferred" id, most skins are best off outputting this preferred
873 * id for best compatibility.
874 * - tooltiponly: This is set to true for some tabs in cases where the system
875 * believes that the accesskey should not be added to the tab.
879 protected function buildContentNavigationUrls() {
880 global $wgDisableLangConversion;
882 // Display tabs for the relevant title rather than always the title itself
883 $title = $this->getRelevantTitle();
884 $onPage = $title->equals( $this->getTitle() );
886 $out = $this->getOutput();
887 $request = $this->getRequest();
888 $user = $this->getUser();
890 $content_navigation = [
898 $action = $request->getVal( 'action', 'view' );
900 $userCanRead = $title->quickUserCan( 'read', $user );
902 // Avoid PHP 7.1 warning of passing $this by reference
903 $skinTemplate = $this;
904 $preventActiveTabs = false;
905 Hooks
::run( 'SkinTemplatePreventOtherActiveTabs', [ &$skinTemplate, &$preventActiveTabs ] );
907 // Checks if page is some kind of content
908 if ( $title->canExist() ) {
909 // Gets page objects for the related namespaces
910 $subjectPage = $title->getSubjectPage();
911 $talkPage = $title->getTalkPage();
913 // Determines if this is a talk page
914 $isTalk = $title->isTalkPage();
916 // Generates XML IDs from namespace names
917 $subjectId = $title->getNamespaceKey( '' );
919 if ( $subjectId == 'main' ) {
922 $talkId = "{$subjectId}_talk";
925 $skname = $this->skinname
;
927 // Adds namespace links
928 $subjectMsg = [ "nstab-$subjectId" ];
929 if ( $subjectPage->isMainPage() ) {
930 array_unshift( $subjectMsg, 'mainpage-nstab' );
932 $content_navigation['namespaces'][$subjectId] = $this->tabAction(
933 $subjectPage, $subjectMsg, !$isTalk && !$preventActiveTabs, '', $userCanRead
935 $content_navigation['namespaces'][$subjectId]['context'] = 'subject';
936 $content_navigation['namespaces'][$talkId] = $this->tabAction(
937 $talkPage, [ "nstab-$talkId", 'talk' ], $isTalk && !$preventActiveTabs, '', $userCanRead
939 $content_navigation['namespaces'][$talkId]['context'] = 'talk';
941 if ( $userCanRead ) {
942 // Adds "view" view link
943 if ( $title->isKnown() ) {
944 $content_navigation['views']['view'] = $this->tabAction(
945 $isTalk ?
$talkPage : $subjectPage,
946 [ "$skname-view-view", 'view' ],
947 ( $onPage && ( $action == 'view' ||
$action == 'purge' ) ), '', true
949 // signal to hide this from simple content_actions
950 $content_navigation['views']['view']['redundant'] = true;
953 $page = $this->canUseWikiPage() ?
$this->getWikiPage() : false;
954 $isRemoteContent = $page && !$page->isLocal();
956 // If it is a non-local file, show a link to the file in its own repository
957 // @todo abstract this for remote content that isn't a file
958 if ( $isRemoteContent ) {
959 $content_navigation['views']['view-foreign'] = [
961 'text' => wfMessageFallback( "$skname-view-foreign", 'view-foreign' )->
962 setContext( $this->getContext() )->
963 params( $page->getWikiDisplayName() )->text(),
964 'href' => $page->getSourceURL(),
969 // Checks if user can edit the current page if it exists or create it otherwise
970 if ( $title->quickUserCan( 'edit', $user )
971 && ( $title->exists() ||
$title->quickUserCan( 'create', $user ) )
973 // Builds CSS class for talk page links
974 $isTalkClass = $isTalk ?
' istalk' : '';
975 // Whether the user is editing the page
976 $isEditing = $onPage && ( $action == 'edit' ||
$action == 'submit' );
977 // Whether to show the "Add a new section" tab
978 // Checks if this is a current rev of talk page and is not forced to be hidden
979 $showNewSection = !$out->forceHideNewSectionLink()
980 && ( ( $isTalk && $this->isRevisionCurrent() ) ||
$out->showNewSectionLink() );
981 $section = $request->getVal( 'section' );
983 if ( $title->exists()
984 ||
( $title->getNamespace() == NS_MEDIAWIKI
985 && $title->getDefaultMessageText() !== false
988 $msgKey = $isRemoteContent ?
'edit-local' : 'edit';
990 $msgKey = $isRemoteContent ?
'create-local' : 'create';
992 $content_navigation['views']['edit'] = [
993 'class' => ( $isEditing && ( $section !== 'new' ||
!$showNewSection )
997 'text' => wfMessageFallback( "$skname-view-$msgKey", $msgKey )
998 ->setContext( $this->getContext() )->text(),
999 'href' => $title->getLocalURL( $this->editUrlOptions() ),
1000 'primary' => !$isRemoteContent, // don't collapse this in vector
1004 if ( $showNewSection ) {
1005 // Adds new section link
1006 // $content_navigation['actions']['addsection']
1007 $content_navigation['views']['addsection'] = [
1008 'class' => ( $isEditing && $section == 'new' ) ?
'selected' : false,
1009 'text' => wfMessageFallback( "$skname-action-addsection", 'addsection' )
1010 ->setContext( $this->getContext() )->text(),
1011 'href' => $title->getLocalURL( 'action=edit§ion=new' )
1014 // Checks if the page has some kind of viewable source content
1015 } elseif ( $title->hasSourceText() ) {
1016 // Adds view source view link
1017 $content_navigation['views']['viewsource'] = [
1018 'class' => ( $onPage && $action == 'edit' ) ?
'selected' : false,
1019 'text' => wfMessageFallback( "$skname-action-viewsource", 'viewsource' )
1020 ->setContext( $this->getContext() )->text(),
1021 'href' => $title->getLocalURL( $this->editUrlOptions() ),
1022 'primary' => true, // don't collapse this in vector
1026 // Checks if the page exists
1027 if ( $title->exists() ) {
1028 // Adds history view link
1029 $content_navigation['views']['history'] = [
1030 'class' => ( $onPage && $action == 'history' ) ?
'selected' : false,
1031 'text' => wfMessageFallback( "$skname-view-history", 'history_short' )
1032 ->setContext( $this->getContext() )->text(),
1033 'href' => $title->getLocalURL( 'action=history' ),
1036 if ( $title->quickUserCan( 'delete', $user ) ) {
1037 $content_navigation['actions']['delete'] = [
1038 'class' => ( $onPage && $action == 'delete' ) ?
'selected' : false,
1039 'text' => wfMessageFallback( "$skname-action-delete", 'delete' )
1040 ->setContext( $this->getContext() )->text(),
1041 'href' => $title->getLocalURL( 'action=delete' )
1045 if ( $title->quickUserCan( 'move', $user ) ) {
1046 $moveTitle = SpecialPage
::getTitleFor( 'Movepage', $title->getPrefixedDBkey() );
1047 $content_navigation['actions']['move'] = [
1048 'class' => $this->getTitle()->isSpecial( 'Movepage' ) ?
'selected' : false,
1049 'text' => wfMessageFallback( "$skname-action-move", 'move' )
1050 ->setContext( $this->getContext() )->text(),
1051 'href' => $moveTitle->getLocalURL()
1055 // article doesn't exist or is deleted
1056 if ( $user->isAllowed( 'deletedhistory' ) ) {
1057 $n = $title->isDeleted();
1059 $undelTitle = SpecialPage
::getTitleFor( 'Undelete', $title->getPrefixedDBkey() );
1060 // If the user can't undelete but can view deleted
1061 // history show them a "View .. deleted" tab instead.
1062 $msgKey = $user->isAllowed( 'undelete' ) ?
'undelete' : 'viewdeleted';
1063 $content_navigation['actions']['undelete'] = [
1064 'class' => $this->getTitle()->isSpecial( 'Undelete' ) ?
'selected' : false,
1065 'text' => wfMessageFallback( "$skname-action-$msgKey", "{$msgKey}_short" )
1066 ->setContext( $this->getContext() )->numParams( $n )->text(),
1067 'href' => $undelTitle->getLocalURL()
1073 if ( $title->quickUserCan( 'protect', $user ) && $title->getRestrictionTypes() &&
1074 MWNamespace
::getRestrictionLevels( $title->getNamespace(), $user ) !== [ '' ]
1076 $mode = $title->isProtected() ?
'unprotect' : 'protect';
1077 $content_navigation['actions'][$mode] = [
1078 'class' => ( $onPage && $action == $mode ) ?
'selected' : false,
1079 'text' => wfMessageFallback( "$skname-action-$mode", $mode )
1080 ->setContext( $this->getContext() )->text(),
1081 'href' => $title->getLocalURL( "action=$mode" )
1085 // Checks if the user is logged in
1086 if ( $this->loggedin
&& $user->isAllowedAll( 'viewmywatchlist', 'editmywatchlist' ) ) {
1088 * The following actions use messages which, if made particular to
1089 * the any specific skins, would break the Ajax code which makes this
1090 * action happen entirely inline. OutputPage::getJSVars
1091 * defines a set of messages in a javascript object - and these
1092 * messages are assumed to be global for all skins. Without making
1093 * a change to that procedure these messages will have to remain as
1094 * the global versions.
1096 $mode = $user->isWatched( $title ) ?
'unwatch' : 'watch';
1097 $content_navigation['actions'][$mode] = [
1098 'class' => 'mw-watchlink ' . (
1099 $onPage && ( $action == 'watch' ||
$action == 'unwatch' ) ?
'selected' : ''
1101 // uses 'watch' or 'unwatch' message
1102 'text' => $this->msg( $mode )->text(),
1103 'href' => $title->getLocalURL( [ 'action' => $mode ] ),
1104 // Set a data-mw=interface attribute, which the mediawiki.page.ajax
1105 // module will look for to make sure it's a trusted link
1107 'mw' => 'interface',
1113 // Avoid PHP 7.1 warning of passing $this by reference
1114 $skinTemplate = $this;
1115 Hooks
::runWithoutAbort(
1116 'SkinTemplateNavigation',
1117 [ &$skinTemplate, &$content_navigation ]
1120 if ( $userCanRead && !$wgDisableLangConversion ) {
1121 $pageLang = $title->getPageLanguage();
1122 // Checks that language conversion is enabled and variants exist
1123 // And if it is not in the special namespace
1124 if ( $pageLang->hasVariants() ) {
1125 // Gets list of language variants
1126 $variants = $pageLang->getVariants();
1127 // Gets preferred variant (note that user preference is
1128 // only possible for wiki content language variant)
1129 $preferred = $pageLang->getPreferredVariant();
1130 if ( Action
::getActionName( $this ) === 'view' ) {
1131 $params = $request->getQueryValues();
1132 unset( $params['title'] );
1136 // Loops over each variant
1137 foreach ( $variants as $code ) {
1138 // Gets variant name from language code
1139 $varname = $pageLang->getVariantname( $code );
1140 // Appends variant link
1141 $content_navigation['variants'][] = [
1142 'class' => ( $code == $preferred ) ?
'selected' : false,
1144 'href' => $title->getLocalURL( [ 'variant' => $code ] +
$params ),
1145 'lang' => LanguageCode
::bcp47( $code ),
1146 'hreflang' => LanguageCode
::bcp47( $code ),
1152 // If it's not content, it's got to be a special page
1153 $content_navigation['namespaces']['special'] = [
1154 'class' => 'selected',
1155 'text' => $this->msg( 'nstab-special' )->text(),
1156 'href' => $request->getRequestURL(), // @see: T4457, T4510
1157 'context' => 'subject'
1160 // Avoid PHP 7.1 warning of passing $this by reference
1161 $skinTemplate = $this;
1162 Hooks
::runWithoutAbort( 'SkinTemplateNavigation::SpecialPage',
1163 [ &$skinTemplate, &$content_navigation ] );
1166 // Avoid PHP 7.1 warning of passing $this by reference
1167 $skinTemplate = $this;
1168 // Equiv to SkinTemplateContentActions
1169 Hooks
::runWithoutAbort( 'SkinTemplateNavigation::Universal',
1170 [ &$skinTemplate, &$content_navigation ] );
1172 // Setup xml ids and tooltip info
1173 foreach ( $content_navigation as $section => &$links ) {
1174 foreach ( $links as $key => &$link ) {
1176 if ( isset( $link['context'] ) && $link['context'] == 'subject' ) {
1177 $xmlID = 'ca-nstab-' . $xmlID;
1178 } elseif ( isset( $link['context'] ) && $link['context'] == 'talk' ) {
1180 $link['rel'] = 'discussion';
1181 } elseif ( $section == 'variants' ) {
1182 $xmlID = 'ca-varlang-' . $xmlID;
1184 $xmlID = 'ca-' . $xmlID;
1186 $link['id'] = $xmlID;
1190 # We don't want to give the watch tab an accesskey if the
1191 # page is being edited, because that conflicts with the
1192 # accesskey on the watch checkbox. We also don't want to
1193 # give the edit tab an accesskey, because that's fairly
1194 # superfluous and conflicts with an accesskey (Ctrl-E) often
1195 # used for editing in Safari.
1196 if ( in_array( $action, [ 'edit', 'submit' ] ) ) {
1197 if ( isset( $content_navigation['views']['edit'] ) ) {
1198 $content_navigation['views']['edit']['tooltiponly'] = true;
1200 if ( isset( $content_navigation['actions']['watch'] ) ) {
1201 $content_navigation['actions']['watch']['tooltiponly'] = true;
1203 if ( isset( $content_navigation['actions']['unwatch'] ) ) {
1204 $content_navigation['actions']['unwatch']['tooltiponly'] = true;
1208 return $content_navigation;
1212 * an array of edit links by default used for the tabs
1213 * @param array $content_navigation
1216 private function buildContentActionUrls( $content_navigation ) {
1217 // content_actions has been replaced with content_navigation for backwards
1218 // compatibility and also for skins that just want simple tabs content_actions
1219 // is now built by flattening the content_navigation arrays into one
1221 $content_actions = [];
1223 foreach ( $content_navigation as $links ) {
1224 foreach ( $links as $key => $value ) {
1225 if ( isset( $value['redundant'] ) && $value['redundant'] ) {
1226 // Redundant tabs are dropped from content_actions
1230 // content_actions used to have ids built using the "ca-$key" pattern
1231 // so the xmlID based id is much closer to the actual $key that we want
1232 // for that reason we'll just strip out the ca- if present and use
1233 // the latter potion of the "id" as the $key
1234 if ( isset( $value['id'] ) && substr( $value['id'], 0, 3 ) == 'ca-' ) {
1235 $key = substr( $value['id'], 3 );
1238 if ( isset( $content_actions[$key] ) ) {
1239 wfDebug( __METHOD__
. ": Found a duplicate key for $key while flattening " .
1240 "content_navigation into content_actions.\n" );
1244 $content_actions[$key] = $value;
1248 return $content_actions;
1252 * build array of common navigation links
1255 protected function buildNavUrls() {
1256 global $wgUploadNavigationUrl;
1258 $out = $this->getOutput();
1259 $request = $this->getRequest();
1262 $nav_urls['mainpage'] = [ 'href' => self
::makeMainPageUrl() ];
1263 if ( $wgUploadNavigationUrl ) {
1264 $nav_urls['upload'] = [ 'href' => $wgUploadNavigationUrl ];
1265 } elseif ( UploadBase
::isEnabled() && UploadBase
::isAllowed( $this->getUser() ) === true ) {
1266 $nav_urls['upload'] = [ 'href' => self
::makeSpecialUrl( 'Upload' ) ];
1268 $nav_urls['upload'] = false;
1270 $nav_urls['specialpages'] = [ 'href' => self
::makeSpecialUrl( 'Specialpages' ) ];
1272 $nav_urls['print'] = false;
1273 $nav_urls['permalink'] = false;
1274 $nav_urls['info'] = false;
1275 $nav_urls['whatlinkshere'] = false;
1276 $nav_urls['recentchangeslinked'] = false;
1277 $nav_urls['contributions'] = false;
1278 $nav_urls['log'] = false;
1279 $nav_urls['blockip'] = false;
1280 $nav_urls['emailuser'] = false;
1281 $nav_urls['userrights'] = false;
1283 // A print stylesheet is attached to all pages, but nobody ever
1284 // figures that out. :) Add a link...
1285 if ( !$out->isPrintable() && ( $out->isArticle() ||
$this->getTitle()->isSpecialPage() ) ) {
1286 $nav_urls['print'] = [
1287 'text' => $this->msg( 'printableversion' )->text(),
1288 'href' => $this->getTitle()->getLocalURL(
1289 $request->appendQueryValue( 'printable', 'yes' ) )
1293 if ( $out->isArticle() ) {
1294 // Also add a "permalink" while we're at it
1295 $revid = $this->getRevisionId();
1297 $nav_urls['permalink'] = [
1298 'text' => $this->msg( 'permalink' )->text(),
1299 'href' => $this->getTitle()->getLocalURL( "oldid=$revid" )
1303 // Avoid PHP 7.1 warning of passing $this by reference
1304 $skinTemplate = $this;
1305 // Use the copy of revision ID in case this undocumented, shady hook tries to mess with internals
1306 Hooks
::run( 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink',
1307 [ &$skinTemplate, &$nav_urls, &$revid, &$revid ] );
1310 if ( $out->isArticleRelated() ) {
1311 $nav_urls['whatlinkshere'] = [
1312 'href' => SpecialPage
::getTitleFor( 'Whatlinkshere', $this->thispage
)->getLocalURL()
1315 $nav_urls['info'] = [
1316 'text' => $this->msg( 'pageinfo-toolboxlink' )->text(),
1317 'href' => $this->getTitle()->getLocalURL( "action=info" )
1320 if ( $this->getTitle()->exists() ||
$this->getTitle()->inNamespace( NS_CATEGORY
) ) {
1321 $nav_urls['recentchangeslinked'] = [
1322 'href' => SpecialPage
::getTitleFor( 'Recentchangeslinked', $this->thispage
)->getLocalURL()
1327 $user = $this->getRelevantUser();
1329 $rootUser = $user->getName();
1331 $nav_urls['contributions'] = [
1332 'text' => $this->msg( 'contributions', $rootUser )->text(),
1333 'href' => self
::makeSpecialUrlSubpage( 'Contributions', $rootUser ),
1334 'tooltip-params' => [ $rootUser ],
1337 $nav_urls['log'] = [
1338 'href' => self
::makeSpecialUrlSubpage( 'Log', $rootUser )
1341 if ( $this->getUser()->isAllowed( 'block' ) ) {
1342 $nav_urls['blockip'] = [
1343 'text' => $this->msg( 'blockip', $rootUser )->text(),
1344 'href' => self
::makeSpecialUrlSubpage( 'Block', $rootUser )
1348 if ( $this->showEmailUser( $user ) ) {
1349 $nav_urls['emailuser'] = [
1350 'text' => $this->msg( 'tool-link-emailuser', $rootUser )->text(),
1351 'href' => self
::makeSpecialUrlSubpage( 'Emailuser', $rootUser ),
1352 'tooltip-params' => [ $rootUser ],
1356 if ( !$user->isAnon() ) {
1357 $sur = new UserrightsPage
;
1358 $sur->setContext( $this->getContext() );
1359 $canChange = $sur->userCanChangeRights( $user );
1360 $nav_urls['userrights'] = [
1361 'text' => $this->msg(
1362 $canChange ?
'tool-link-userrights' : 'tool-link-userrights-readonly',
1365 'href' => self
::makeSpecialUrlSubpage( 'Userrights', $rootUser )
1374 * Generate strings used for xml 'id' names
1377 protected function getNameSpaceKey() {
1378 return $this->getTitle()->getNamespaceKey();