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 ( $title->exists() ) {
389 if ( $out->isArticle() ) {
390 if ( $this->isRevisionCurrent() ) {
391 if ( $wgMaxCredits != 0 ) {
392 $tpl->set( 'credits', Action
::factory( 'credits', $this->getWikiPage(),
393 $this->getContext() )->getCredits( $wgMaxCredits, $wgShowCreditsIfMax ) );
395 $tpl->set( 'lastmod', $this->lastModified() );
399 if ( $out->showsCopyright() ) {
400 $tpl->set( 'copyright', $this->getCopyright() );
404 $tpl->set( 'copyrightico', $this->getCopyrightIcon() );
405 $tpl->set( 'poweredbyico', $this->getPoweredBy() );
406 $tpl->set( 'disclaimer', $this->disclaimerLink() );
407 $tpl->set( 'privacy', $this->privacyLink() );
408 $tpl->set( 'about', $this->aboutLink() );
410 $tpl->set( 'footerlinks', [
413 'numberofwatchingusers',
424 global $wgFooterIcons;
425 $tpl->set( 'footericons', $wgFooterIcons );
426 foreach ( $tpl->data
['footericons'] as $footerIconsKey => &$footerIconsBlock ) {
427 if ( count( $footerIconsBlock ) > 0 ) {
428 foreach ( $footerIconsBlock as &$footerIcon ) {
429 if ( isset( $footerIcon['src'] ) ) {
430 if ( !isset( $footerIcon['width'] ) ) {
431 $footerIcon['width'] = 88;
433 if ( !isset( $footerIcon['height'] ) ) {
434 $footerIcon['height'] = 31;
439 unset( $tpl->data
['footericons'][$footerIconsKey] );
443 $tpl->set( 'indicators', $out->getIndicators() );
445 $tpl->set( 'sitenotice', $this->getSiteNotice() );
446 $tpl->set( 'printfooter', $this->printSource() );
447 // Wrap the bodyText with #mw-content-text element
448 $out->mBodytext
= $this->wrapHTML( $title, $out->mBodytext
);
449 $tpl->set( 'bodytext', $out->mBodytext
);
451 $language_urls = $this->getLanguages();
452 if ( count( $language_urls ) ) {
453 $tpl->set( 'language_urls', $language_urls );
455 $tpl->set( 'language_urls', false );
459 $tpl->set( 'personal_urls', $this->buildPersonalUrls() );
460 $content_navigation = $this->buildContentNavigationUrls();
461 $content_actions = $this->buildContentActionUrls( $content_navigation );
462 $tpl->set( 'content_navigation', $content_navigation );
463 $tpl->set( 'content_actions', $content_actions );
465 $tpl->set( 'sidebar', $this->buildSidebar() );
466 $tpl->set( 'nav_urls', $this->buildNavUrls() );
468 // Do this last in case hooks above add bottom scripts
469 $tpl->set( 'bottomscripts', $this->bottomScripts() );
471 // Set the head scripts near the end, in case the above actions resulted in added scripts
472 $tpl->set( 'headelement', $out->headElement( $this ) );
474 $tpl->set( 'debug', '' );
475 $tpl->set( 'debughtml', $this->generateDebugHTML() );
476 $tpl->set( 'reporttime', wfReportTime( $out->getCSPNonce() ) );
478 // Avoid PHP 7.1 warning of passing $this by reference
479 $skinTemplate = $this;
480 // original version by hansm
481 if ( !Hooks
::run( 'SkinTemplateOutputPageBeforeExec', [ &$skinTemplate, &$tpl ] ) ) {
482 wfDebug( __METHOD__
. ": Hook SkinTemplateOutputPageBeforeExec broke outputPage execution!\n" );
485 // Set the bodytext to another key so that skins can just output it on its own
486 // and output printfooter and debughtml separately
487 $tpl->set( 'bodycontent', $tpl->data
['bodytext'] );
489 // Append printfooter and debughtml onto bodytext so that skins that
490 // were already using bodytext before they were split out don't suddenly
491 // start not outputting information.
492 $tpl->data
['bodytext'] .= Html
::rawElement(
494 [ 'class' => 'printfooter' ],
495 "\n{$tpl->data['printfooter']}"
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() );
507 * Get the HTML for the p-personal list
510 public function getPersonalToolsList() {
511 return $this->makePersonalToolsList();
515 * Get the HTML for the personal tools list
519 * @param array|null $personalTools
520 * @param array $options
523 public function makePersonalToolsList( $personalTools = null, $options = [] ) {
524 $tpl = $this->setupTemplateForOutput();
525 $tpl->set( 'personal_urls', $this->buildPersonalUrls() );
528 if ( $personalTools === null ) {
529 $personalTools = $tpl->getPersonalTools();
532 foreach ( $personalTools as $key => $item ) {
533 $html .= $tpl->makeListItem( $key, $item, $options );
540 * Get personal tools for the user
544 * @return array Array of personal tools
546 public function getStructuredPersonalTools() {
547 $tpl = $this->setupTemplateForOutput();
548 $tpl->set( 'personal_urls', $this->buildPersonalUrls() );
550 return $tpl->getPersonalTools();
554 * Format language name for use in sidebar interlanguage links list.
555 * By default it is capitalized.
557 * @param string $name Language name, e.g. "English" or "español"
561 function formatLanguageName( $name ) {
562 return $this->getLanguage()->ucfirst( $name );
566 * Output the string, or print error message if it's
567 * an error object of the appropriate type.
568 * For the base class, assume strings all around.
573 function printOrError( $str ) {
578 * Output a boolean indicating if buildPersonalUrls should output separate
579 * login and create account links or output a combined link
580 * By default we simply return a global config setting that affects most skins
581 * This is setup as a method so that like with $wgLogo and getLogo() a skin
582 * can override this setting and always output one or the other if it has
583 * a reason it can't output one of the two modes.
586 function useCombinedLoginLink() {
587 global $wgUseCombinedLoginLink;
588 return $wgUseCombinedLoginLink;
592 * build array of urls for personal toolbar
595 protected function buildPersonalUrls() {
596 $title = $this->getTitle();
597 $request = $this->getRequest();
598 $pageurl = $title->getLocalURL();
599 $authManager = AuthManager
::singleton();
601 /* set up the default links for the personal toolbar */
604 # Due to T34276, if a user does not have read permissions,
605 # $this->getTitle() will just give Special:Badtitle, which is
606 # not especially useful as a returnto parameter. Use the title
607 # from the request instead, if there was one.
608 if ( $this->getUser()->isAllowed( 'read' ) ) {
609 $page = $this->getTitle();
611 $page = Title
::newFromText( $request->getVal( 'title', '' ) );
613 $page = $request->getVal( 'returnto', $page );
615 if ( strval( $page ) !== '' ) {
616 $a['returnto'] = $page;
617 $query = $request->getVal( 'returntoquery', $this->thisquery
);
618 if ( $query != '' ) {
619 $a['returntoquery'] = $query;
623 $returnto = wfArrayToCgi( $a );
624 if ( $this->loggedin
) {
625 $personal_urls['userpage'] = [
626 'text' => $this->username
,
627 'href' => &$this->userpageUrlDetails
['href'],
628 'class' => $this->userpageUrlDetails
['exists'] ?
false : 'new',
629 'exists' => $this->userpageUrlDetails
['exists'],
630 'active' => ( $this->userpageUrlDetails
['href'] == $pageurl ),
633 $usertalkUrlDetails = $this->makeTalkUrlDetails( $this->userpage
);
634 $personal_urls['mytalk'] = [
635 'text' => $this->msg( 'mytalk' )->text(),
636 'href' => &$usertalkUrlDetails['href'],
637 'class' => $usertalkUrlDetails['exists'] ?
false : 'new',
638 'exists' => $usertalkUrlDetails['exists'],
639 'active' => ( $usertalkUrlDetails['href'] == $pageurl )
641 $href = self
::makeSpecialUrl( 'Preferences' );
642 $personal_urls['preferences'] = [
643 'text' => $this->msg( 'mypreferences' )->text(),
645 'active' => ( $href == $pageurl )
648 if ( $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
649 $href = self
::makeSpecialUrl( 'Watchlist' );
650 $personal_urls['watchlist'] = [
651 'text' => $this->msg( 'mywatchlist' )->text(),
653 'active' => ( $href == $pageurl )
657 # We need to do an explicit check for Special:Contributions, as we
658 # have to match both the title, and the target, which could come
659 # from request values (Special:Contributions?target=Jimbo_Wales)
660 # or be specified in "sub page" form
661 # (Special:Contributions/Jimbo_Wales). The plot
662 # thickens, because the Title object is altered for special pages,
663 # so it doesn't contain the original alias-with-subpage.
664 $origTitle = Title
::newFromText( $request->getText( 'title' ) );
665 if ( $origTitle instanceof Title
&& $origTitle->isSpecialPage() ) {
666 list( $spName, $spPar ) =
667 MediaWikiServices
::getInstance()->getSpecialPageFactory()->
668 resolveAlias( $origTitle->getText() );
669 $active = $spName == 'Contributions'
670 && ( ( $spPar && $spPar == $this->username
)
671 ||
$request->getText( 'target' ) == $this->username
);
676 $href = self
::makeSpecialUrlSubpage( 'Contributions', $this->username
);
677 $personal_urls['mycontris'] = [
678 'text' => $this->msg( 'mycontris' )->text(),
683 // if we can't set the user, we can't unset it either
684 if ( $request->getSession()->canSetUser() ) {
685 $personal_urls['logout'] = [
686 'text' => $this->msg( 'pt-userlogout' )->text(),
687 'href' => self
::makeSpecialUrl( 'Userlogout',
688 // userlogout link must always contain an & character, otherwise we might not be able
689 // to detect a buggy precaching proxy (T19790)
690 $title->isSpecial( 'Preferences' ) ?
'noreturnto' : $returnto ),
695 $useCombinedLoginLink = $this->useCombinedLoginLink();
696 if ( !$authManager->canCreateAccounts() ||
!$authManager->canAuthenticateNow() ) {
697 // don't show combined login/signup link if one of those is actually not available
698 $useCombinedLoginLink = false;
701 $loginlink = $this->getUser()->isAllowed( 'createaccount' ) && $useCombinedLoginLink
702 ?
'nav-login-createaccount'
706 'text' => $this->msg( $loginlink )->text(),
707 'href' => self
::makeSpecialUrl( 'Userlogin', $returnto ),
708 'active' => $title->isSpecial( 'Userlogin' )
709 ||
$title->isSpecial( 'CreateAccount' ) && $useCombinedLoginLink,
711 $createaccount_url = [
712 'text' => $this->msg( 'pt-createaccount' )->text(),
713 'href' => self
::makeSpecialUrl( 'CreateAccount', $returnto ),
714 'active' => $title->isSpecial( 'CreateAccount' ),
717 // No need to show Talk and Contributions to anons if they can't contribute!
718 if ( User
::groupHasPermission( '*', 'edit' ) ) {
719 // Because of caching, we can't link directly to the IP talk and
720 // contributions pages. Instead we use the special page shortcuts
721 // (which work correctly regardless of caching). This means we can't
722 // determine whether these links are active or not, but since major
723 // skins (MonoBook, Vector) don't use this information, it's not a
725 $personal_urls['anontalk'] = [
726 'text' => $this->msg( 'anontalk' )->text(),
727 'href' => self
::makeSpecialUrlSubpage( 'Mytalk', false ),
730 $personal_urls['anoncontribs'] = [
731 'text' => $this->msg( 'anoncontribs' )->text(),
732 'href' => self
::makeSpecialUrlSubpage( 'Mycontributions', false ),
738 $authManager->canCreateAccounts()
739 && $this->getUser()->isAllowed( 'createaccount' )
740 && !$useCombinedLoginLink
742 $personal_urls['createaccount'] = $createaccount_url;
745 if ( $authManager->canAuthenticateNow() ) {
746 $key = User
::groupHasPermission( '*', 'read' )
749 $personal_urls[$key] = $login_url;
753 Hooks
::runWithoutAbort( 'PersonalUrls', [ &$personal_urls, &$title, $this ] );
754 return $personal_urls;
758 * Builds an array with tab definition
760 * @param Title $title Page Where the tab links to
761 * @param string|array $message Message key or an array of message keys (will fall back)
762 * @param bool $selected Display the tab as selected
763 * @param string $query Query string attached to tab URL
764 * @param bool $checkEdit Check if $title exists and mark with .new if one doesn't
768 function tabAction( $title, $message, $selected, $query = '', $checkEdit = false ) {
771 $classes[] = 'selected';
774 if ( $checkEdit && !$title->isKnown() ) {
777 if ( $query !== '' ) {
778 $query = 'action=edit&redlink=1&' . $query;
780 $query = 'action=edit&redlink=1';
784 $linkClass = MediaWikiServices
::getInstance()->getLinkRenderer()->getLinkClasses( $title );
786 // wfMessageFallback will nicely accept $message as an array of fallbacks
787 // or just a single key
788 $msg = wfMessageFallback( $message )->setContext( $this->getContext() );
789 if ( is_array( $message ) ) {
790 // for hook compatibility just keep the last message name
791 $message = end( $message );
793 if ( $msg->exists() ) {
794 $text = $msg->text();
796 $text = MediaWikiServices
::getInstance()->getContentLanguage()->getConverter()->
797 convertNamespace( MWNamespace
::getSubject( $title->getNamespace() ) );
800 // Avoid PHP 7.1 warning of passing $this by reference
801 $skinTemplate = $this;
803 if ( !Hooks
::run( 'SkinTemplateTabAction', [ &$skinTemplate,
804 $title, $message, $selected, $checkEdit,
805 &$classes, &$query, &$text, &$result ] ) ) {
810 'class' => implode( ' ', $classes ),
812 'href' => $title->getLocalURL( $query ),
815 if ( $linkClass !== '' ) {
816 $result['link-class'] = $linkClass;
822 function makeTalkUrlDetails( $name, $urlaction = '' ) {
823 $title = Title
::newFromText( $name );
824 if ( !is_object( $title ) ) {
825 throw new MWException( __METHOD__
. " given invalid pagename $name" );
827 $title = $title->getTalkPage();
828 self
::checkTitle( $title, $name );
830 'href' => $title->getLocalURL( $urlaction ),
831 'exists' => $title->isKnown(),
836 * @todo is this even used?
837 * @param string $name
838 * @param string $urlaction
841 function makeArticleUrlDetails( $name, $urlaction = '' ) {
842 $title = Title
::newFromText( $name );
843 $title = $title->getSubjectPage();
844 self
::checkTitle( $title, $name );
846 'href' => $title->getLocalURL( $urlaction ),
847 'exists' => $title->exists(),
852 * a structured array of links usually used for the tabs in a skin
854 * There are 4 standard sections
855 * namespaces: Used for namespace tabs like special, page, and talk namespaces
856 * views: Used for primary page views like read, edit, history
857 * actions: Used for most extra page actions like deletion, protection, etc...
858 * variants: Used to list the language variants for the page
860 * Each section's value is a key/value array of links for that section.
861 * The links themselves have these common keys:
862 * - class: The css classes to apply to the tab
863 * - text: The text to display on the tab
864 * - href: The href for the tab to point to
865 * - rel: An optional rel= for the tab's link
866 * - redundant: If true the tab will be dropped in skins using content_actions
867 * this is useful for tabs like "Read" which only have meaning in skins that
868 * take special meaning from the grouped structure of content_navigation
870 * Views also have an extra key which can be used:
871 * - primary: If this is not true skins like vector may try to hide the tab
872 * when the user has limited space in their browser window
874 * content_navigation using code also expects these ids to be present on the
875 * links, however these are usually automatically generated by SkinTemplate
876 * itself and are not necessary when using a hook. The only things these may
877 * matter to are people modifying content_navigation after it's initial creation:
878 * - id: A "preferred" id, most skins are best off outputting this preferred
879 * id for best compatibility.
880 * - tooltiponly: This is set to true for some tabs in cases where the system
881 * believes that the accesskey should not be added to the tab.
885 protected function buildContentNavigationUrls() {
886 global $wgDisableLangConversion;
888 // Display tabs for the relevant title rather than always the title itself
889 $title = $this->getRelevantTitle();
890 $onPage = $title->equals( $this->getTitle() );
892 $out = $this->getOutput();
893 $request = $this->getRequest();
894 $user = $this->getUser();
896 $content_navigation = [
904 $action = $request->getVal( 'action', 'view' );
906 $userCanRead = $title->quickUserCan( 'read', $user );
908 // Avoid PHP 7.1 warning of passing $this by reference
909 $skinTemplate = $this;
910 $preventActiveTabs = false;
911 Hooks
::run( 'SkinTemplatePreventOtherActiveTabs', [ &$skinTemplate, &$preventActiveTabs ] );
913 // Checks if page is some kind of content
914 if ( $title->canExist() ) {
915 // Gets page objects for the related namespaces
916 $subjectPage = $title->getSubjectPage();
917 $talkPage = $title->getTalkPage();
919 // Determines if this is a talk page
920 $isTalk = $title->isTalkPage();
922 // Generates XML IDs from namespace names
923 $subjectId = $title->getNamespaceKey( '' );
925 if ( $subjectId == 'main' ) {
928 $talkId = "{$subjectId}_talk";
931 $skname = $this->skinname
;
933 // Adds namespace links
934 $subjectMsg = [ "nstab-$subjectId" ];
935 if ( $subjectPage->isMainPage() ) {
936 array_unshift( $subjectMsg, 'mainpage-nstab' );
938 $content_navigation['namespaces'][$subjectId] = $this->tabAction(
939 $subjectPage, $subjectMsg, !$isTalk && !$preventActiveTabs, '', $userCanRead
941 $content_navigation['namespaces'][$subjectId]['context'] = 'subject';
942 $content_navigation['namespaces'][$talkId] = $this->tabAction(
943 $talkPage, [ "nstab-$talkId", 'talk' ], $isTalk && !$preventActiveTabs, '', $userCanRead
945 $content_navigation['namespaces'][$talkId]['context'] = 'talk';
947 if ( $userCanRead ) {
948 // Adds "view" view link
949 if ( $title->isKnown() ) {
950 $content_navigation['views']['view'] = $this->tabAction(
951 $isTalk ?
$talkPage : $subjectPage,
952 [ "$skname-view-view", 'view' ],
953 ( $onPage && ( $action == 'view' ||
$action == 'purge' ) ), '', true
955 // signal to hide this from simple content_actions
956 $content_navigation['views']['view']['redundant'] = true;
959 $page = $this->canUseWikiPage() ?
$this->getWikiPage() : false;
960 $isRemoteContent = $page && !$page->isLocal();
962 // If it is a non-local file, show a link to the file in its own repository
963 // @todo abstract this for remote content that isn't a file
964 if ( $isRemoteContent ) {
965 $content_navigation['views']['view-foreign'] = [
967 'text' => wfMessageFallback( "$skname-view-foreign", 'view-foreign' )->
968 setContext( $this->getContext() )->
969 params( $page->getWikiDisplayName() )->text(),
970 'href' => $page->getSourceURL(),
975 // Checks if user can edit the current page if it exists or create it otherwise
976 if ( $title->quickUserCan( 'edit', $user )
977 && ( $title->exists() ||
$title->quickUserCan( 'create', $user ) )
979 // Builds CSS class for talk page links
980 $isTalkClass = $isTalk ?
' istalk' : '';
981 // Whether the user is editing the page
982 $isEditing = $onPage && ( $action == 'edit' ||
$action == 'submit' );
983 // Whether to show the "Add a new section" tab
984 // Checks if this is a current rev of talk page and is not forced to be hidden
985 $showNewSection = !$out->forceHideNewSectionLink()
986 && ( ( $isTalk && $this->isRevisionCurrent() ) ||
$out->showNewSectionLink() );
987 $section = $request->getVal( 'section' );
989 if ( $title->exists()
990 ||
( $title->getNamespace() == NS_MEDIAWIKI
991 && $title->getDefaultMessageText() !== false
994 $msgKey = $isRemoteContent ?
'edit-local' : 'edit';
996 $msgKey = $isRemoteContent ?
'create-local' : 'create';
998 $content_navigation['views']['edit'] = [
999 'class' => ( $isEditing && ( $section !== 'new' ||
!$showNewSection )
1003 'text' => wfMessageFallback( "$skname-view-$msgKey", $msgKey )
1004 ->setContext( $this->getContext() )->text(),
1005 'href' => $title->getLocalURL( $this->editUrlOptions() ),
1006 'primary' => !$isRemoteContent, // don't collapse this in vector
1010 if ( $showNewSection ) {
1011 // Adds new section link
1012 // $content_navigation['actions']['addsection']
1013 $content_navigation['views']['addsection'] = [
1014 'class' => ( $isEditing && $section == 'new' ) ?
'selected' : false,
1015 'text' => wfMessageFallback( "$skname-action-addsection", 'addsection' )
1016 ->setContext( $this->getContext() )->text(),
1017 'href' => $title->getLocalURL( 'action=edit§ion=new' )
1020 // Checks if the page has some kind of viewable source content
1021 } elseif ( $title->hasSourceText() ) {
1022 // Adds view source view link
1023 $content_navigation['views']['viewsource'] = [
1024 'class' => ( $onPage && $action == 'edit' ) ?
'selected' : false,
1025 'text' => wfMessageFallback( "$skname-action-viewsource", 'viewsource' )
1026 ->setContext( $this->getContext() )->text(),
1027 'href' => $title->getLocalURL( $this->editUrlOptions() ),
1028 'primary' => true, // don't collapse this in vector
1032 // Checks if the page exists
1033 if ( $title->exists() ) {
1034 // Adds history view link
1035 $content_navigation['views']['history'] = [
1036 'class' => ( $onPage && $action == 'history' ) ?
'selected' : false,
1037 'text' => wfMessageFallback( "$skname-view-history", 'history_short' )
1038 ->setContext( $this->getContext() )->text(),
1039 'href' => $title->getLocalURL( 'action=history' ),
1042 if ( $title->quickUserCan( 'delete', $user ) ) {
1043 $content_navigation['actions']['delete'] = [
1044 'class' => ( $onPage && $action == 'delete' ) ?
'selected' : false,
1045 'text' => wfMessageFallback( "$skname-action-delete", 'delete' )
1046 ->setContext( $this->getContext() )->text(),
1047 'href' => $title->getLocalURL( 'action=delete' )
1051 if ( $title->quickUserCan( 'move', $user ) ) {
1052 $moveTitle = SpecialPage
::getTitleFor( 'Movepage', $title->getPrefixedDBkey() );
1053 $content_navigation['actions']['move'] = [
1054 'class' => $this->getTitle()->isSpecial( 'Movepage' ) ?
'selected' : false,
1055 'text' => wfMessageFallback( "$skname-action-move", 'move' )
1056 ->setContext( $this->getContext() )->text(),
1057 'href' => $moveTitle->getLocalURL()
1061 // article doesn't exist or is deleted
1062 if ( $title->quickUserCan( 'deletedhistory', $user ) ) {
1063 $n = $title->isDeleted();
1065 $undelTitle = SpecialPage
::getTitleFor( 'Undelete', $title->getPrefixedDBkey() );
1066 // If the user can't undelete but can view deleted
1067 // history show them a "View .. deleted" tab instead.
1068 $msgKey = $title->quickUserCan( 'undelete', $user ) ?
'undelete' : 'viewdeleted';
1069 $content_navigation['actions']['undelete'] = [
1070 'class' => $this->getTitle()->isSpecial( 'Undelete' ) ?
'selected' : false,
1071 'text' => wfMessageFallback( "$skname-action-$msgKey", "{$msgKey}_short" )
1072 ->setContext( $this->getContext() )->numParams( $n )->text(),
1073 'href' => $undelTitle->getLocalURL()
1079 if ( $title->quickUserCan( 'protect', $user ) && $title->getRestrictionTypes() &&
1080 MWNamespace
::getRestrictionLevels( $title->getNamespace(), $user ) !== [ '' ]
1082 $mode = $title->isProtected() ?
'unprotect' : 'protect';
1083 $content_navigation['actions'][$mode] = [
1084 'class' => ( $onPage && $action == $mode ) ?
'selected' : false,
1085 'text' => wfMessageFallback( "$skname-action-$mode", $mode )
1086 ->setContext( $this->getContext() )->text(),
1087 'href' => $title->getLocalURL( "action=$mode" )
1091 // Checks if the user is logged in
1092 if ( $this->loggedin
&& $user->isAllowedAll( 'viewmywatchlist', 'editmywatchlist' ) ) {
1094 * The following actions use messages which, if made particular to
1095 * the any specific skins, would break the Ajax code which makes this
1096 * action happen entirely inline. OutputPage::getJSVars
1097 * defines a set of messages in a javascript object - and these
1098 * messages are assumed to be global for all skins. Without making
1099 * a change to that procedure these messages will have to remain as
1100 * the global versions.
1102 $mode = $user->isWatched( $title ) ?
'unwatch' : 'watch';
1103 $content_navigation['actions'][$mode] = [
1104 'class' => 'mw-watchlink ' . (
1105 $onPage && ( $action == 'watch' ||
$action == 'unwatch' ) ?
'selected' : ''
1107 // uses 'watch' or 'unwatch' message
1108 'text' => $this->msg( $mode )->text(),
1109 'href' => $title->getLocalURL( [ 'action' => $mode ] ),
1110 // Set a data-mw=interface attribute, which the mediawiki.page.ajax
1111 // module will look for to make sure it's a trusted link
1113 'mw' => 'interface',
1119 // Avoid PHP 7.1 warning of passing $this by reference
1120 $skinTemplate = $this;
1121 Hooks
::runWithoutAbort(
1122 'SkinTemplateNavigation',
1123 [ &$skinTemplate, &$content_navigation ]
1126 if ( $userCanRead && !$wgDisableLangConversion ) {
1127 $pageLang = $title->getPageLanguage();
1128 // Checks that language conversion is enabled and variants exist
1129 // And if it is not in the special namespace
1130 if ( $pageLang->hasVariants() ) {
1131 // Gets list of language variants
1132 $variants = $pageLang->getVariants();
1133 // Gets preferred variant (note that user preference is
1134 // only possible for wiki content language variant)
1135 $preferred = $pageLang->getPreferredVariant();
1136 if ( Action
::getActionName( $this ) === 'view' ) {
1137 $params = $request->getQueryValues();
1138 unset( $params['title'] );
1142 // Loops over each variant
1143 foreach ( $variants as $code ) {
1144 // Gets variant name from language code
1145 $varname = $pageLang->getVariantname( $code );
1146 // Appends variant link
1147 $content_navigation['variants'][] = [
1148 'class' => ( $code == $preferred ) ?
'selected' : false,
1150 'href' => $title->getLocalURL( [ 'variant' => $code ] +
$params ),
1151 'lang' => LanguageCode
::bcp47( $code ),
1152 'hreflang' => LanguageCode
::bcp47( $code ),
1158 // If it's not content, it's got to be a special page
1159 $content_navigation['namespaces']['special'] = [
1160 'class' => 'selected',
1161 'text' => $this->msg( 'nstab-special' )->text(),
1162 'href' => $request->getRequestURL(), // @see: T4457, T4510
1163 'context' => 'subject'
1166 // Avoid PHP 7.1 warning of passing $this by reference
1167 $skinTemplate = $this;
1168 Hooks
::runWithoutAbort( 'SkinTemplateNavigation::SpecialPage',
1169 [ &$skinTemplate, &$content_navigation ] );
1172 // Avoid PHP 7.1 warning of passing $this by reference
1173 $skinTemplate = $this;
1174 // Equiv to SkinTemplateContentActions
1175 Hooks
::runWithoutAbort( 'SkinTemplateNavigation::Universal',
1176 [ &$skinTemplate, &$content_navigation ] );
1178 // Setup xml ids and tooltip info
1179 foreach ( $content_navigation as $section => &$links ) {
1180 foreach ( $links as $key => &$link ) {
1182 if ( isset( $link['context'] ) && $link['context'] == 'subject' ) {
1183 $xmlID = 'ca-nstab-' . $xmlID;
1184 } elseif ( isset( $link['context'] ) && $link['context'] == 'talk' ) {
1186 $link['rel'] = 'discussion';
1187 } elseif ( $section == 'variants' ) {
1188 $xmlID = 'ca-varlang-' . $xmlID;
1190 $xmlID = 'ca-' . $xmlID;
1192 $link['id'] = $xmlID;
1196 # We don't want to give the watch tab an accesskey if the
1197 # page is being edited, because that conflicts with the
1198 # accesskey on the watch checkbox. We also don't want to
1199 # give the edit tab an accesskey, because that's fairly
1200 # superfluous and conflicts with an accesskey (Ctrl-E) often
1201 # used for editing in Safari.
1202 if ( in_array( $action, [ 'edit', 'submit' ] ) ) {
1203 if ( isset( $content_navigation['views']['edit'] ) ) {
1204 $content_navigation['views']['edit']['tooltiponly'] = true;
1206 if ( isset( $content_navigation['actions']['watch'] ) ) {
1207 $content_navigation['actions']['watch']['tooltiponly'] = true;
1209 if ( isset( $content_navigation['actions']['unwatch'] ) ) {
1210 $content_navigation['actions']['unwatch']['tooltiponly'] = true;
1214 return $content_navigation;
1218 * an array of edit links by default used for the tabs
1219 * @param array $content_navigation
1222 private function buildContentActionUrls( $content_navigation ) {
1223 // content_actions has been replaced with content_navigation for backwards
1224 // compatibility and also for skins that just want simple tabs content_actions
1225 // is now built by flattening the content_navigation arrays into one
1227 $content_actions = [];
1229 foreach ( $content_navigation as $links ) {
1230 foreach ( $links as $key => $value ) {
1231 if ( isset( $value['redundant'] ) && $value['redundant'] ) {
1232 // Redundant tabs are dropped from content_actions
1236 // content_actions used to have ids built using the "ca-$key" pattern
1237 // so the xmlID based id is much closer to the actual $key that we want
1238 // for that reason we'll just strip out the ca- if present and use
1239 // the latter potion of the "id" as the $key
1240 if ( isset( $value['id'] ) && substr( $value['id'], 0, 3 ) == 'ca-' ) {
1241 $key = substr( $value['id'], 3 );
1244 if ( isset( $content_actions[$key] ) ) {
1245 wfDebug( __METHOD__
. ": Found a duplicate key for $key while flattening " .
1246 "content_navigation into content_actions.\n" );
1250 $content_actions[$key] = $value;
1254 return $content_actions;
1258 * build array of common navigation links
1261 protected function buildNavUrls() {
1262 global $wgUploadNavigationUrl;
1264 $out = $this->getOutput();
1265 $request = $this->getRequest();
1268 $nav_urls['mainpage'] = [ 'href' => self
::makeMainPageUrl() ];
1269 if ( $wgUploadNavigationUrl ) {
1270 $nav_urls['upload'] = [ 'href' => $wgUploadNavigationUrl ];
1271 } elseif ( UploadBase
::isEnabled() && UploadBase
::isAllowed( $this->getUser() ) === true ) {
1272 $nav_urls['upload'] = [ 'href' => self
::makeSpecialUrl( 'Upload' ) ];
1274 $nav_urls['upload'] = false;
1276 $nav_urls['specialpages'] = [ 'href' => self
::makeSpecialUrl( 'Specialpages' ) ];
1278 $nav_urls['print'] = false;
1279 $nav_urls['permalink'] = false;
1280 $nav_urls['info'] = false;
1281 $nav_urls['whatlinkshere'] = false;
1282 $nav_urls['recentchangeslinked'] = false;
1283 $nav_urls['contributions'] = false;
1284 $nav_urls['log'] = false;
1285 $nav_urls['blockip'] = false;
1286 $nav_urls['emailuser'] = false;
1287 $nav_urls['userrights'] = false;
1289 // A print stylesheet is attached to all pages, but nobody ever
1290 // figures that out. :) Add a link...
1291 if ( !$out->isPrintable() && ( $out->isArticle() ||
$this->getTitle()->isSpecialPage() ) ) {
1292 $nav_urls['print'] = [
1293 'text' => $this->msg( 'printableversion' )->text(),
1294 'href' => $this->getTitle()->getLocalURL(
1295 $request->appendQueryValue( 'printable', 'yes' ) )
1299 if ( $out->isArticle() ) {
1300 // Also add a "permalink" while we're at it
1301 $revid = $this->getRevisionId();
1303 $nav_urls['permalink'] = [
1304 'text' => $this->msg( 'permalink' )->text(),
1305 'href' => $this->getTitle()->getLocalURL( "oldid=$revid" )
1309 // Avoid PHP 7.1 warning of passing $this by reference
1310 $skinTemplate = $this;
1311 // Use the copy of revision ID in case this undocumented, shady hook tries to mess with internals
1312 Hooks
::run( 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink',
1313 [ &$skinTemplate, &$nav_urls, &$revid, &$revid ] );
1316 if ( $out->isArticleRelated() ) {
1317 $nav_urls['whatlinkshere'] = [
1318 'href' => SpecialPage
::getTitleFor( 'Whatlinkshere', $this->thispage
)->getLocalURL()
1321 $nav_urls['info'] = [
1322 'text' => $this->msg( 'pageinfo-toolboxlink' )->text(),
1323 'href' => $this->getTitle()->getLocalURL( "action=info" )
1326 if ( $this->getTitle()->exists() ||
$this->getTitle()->inNamespace( NS_CATEGORY
) ) {
1327 $nav_urls['recentchangeslinked'] = [
1328 'href' => SpecialPage
::getTitleFor( 'Recentchangeslinked', $this->thispage
)->getLocalURL()
1333 $user = $this->getRelevantUser();
1335 $rootUser = $user->getName();
1337 $nav_urls['contributions'] = [
1338 'text' => $this->msg( 'contributions', $rootUser )->text(),
1339 'href' => self
::makeSpecialUrlSubpage( 'Contributions', $rootUser ),
1340 'tooltip-params' => [ $rootUser ],
1343 $nav_urls['log'] = [
1344 'href' => self
::makeSpecialUrlSubpage( 'Log', $rootUser )
1347 if ( $this->getUser()->isAllowed( 'block' ) ) {
1348 $nav_urls['blockip'] = [
1349 'text' => $this->msg( 'blockip', $rootUser )->text(),
1350 'href' => self
::makeSpecialUrlSubpage( 'Block', $rootUser )
1354 if ( $this->showEmailUser( $user ) ) {
1355 $nav_urls['emailuser'] = [
1356 'text' => $this->msg( 'tool-link-emailuser', $rootUser )->text(),
1357 'href' => self
::makeSpecialUrlSubpage( 'Emailuser', $rootUser ),
1358 'tooltip-params' => [ $rootUser ],
1362 if ( !$user->isAnon() ) {
1363 $sur = new UserrightsPage
;
1364 $sur->setContext( $this->getContext() );
1365 $canChange = $sur->userCanChangeRights( $user );
1366 $nav_urls['userrights'] = [
1367 'text' => $this->msg(
1368 $canChange ?
'tool-link-userrights' : 'tool-link-userrights-readonly',
1371 'href' => self
::makeSpecialUrlSubpage( 'Userrights', $rootUser )
1380 * Generate strings used for xml 'id' names
1383 protected function getNameSpaceKey() {
1384 return $this->getTitle()->getNamespaceKey();