Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / includes / skins / SkinTemplate.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21 use MediaWiki\Auth\AuthManager;
22 use MediaWiki\MediaWikiServices;
23
24 /**
25 * Base class for template-based skins.
26 *
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/
31 *
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.
35 *
36 * @ingroup Skins
37 */
38 class SkinTemplate extends Skin {
39 /**
40 * @var string Name of our skin, it probably needs to be all lower case.
41 * Child classes should override the default.
42 */
43 public $skinname = 'monobook';
44
45 /**
46 * @var string For QuickTemplate, the name of the subclass which will
47 * actually fill the template. Child classes should override the default.
48 */
49 public $template = QuickTemplate::class;
50
51 public $thispage;
52 public $titletxt;
53 public $userpage;
54 public $thisquery;
55 public $loggedin;
56 public $username;
57 public $userpageUrlDetails;
58
59 /**
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.
63 *
64 * @param string $classname
65 * @param bool|string $repository Subdirectory where we keep template files
66 * @param bool|string $cache_dir
67 * @return QuickTemplate
68 * @private
69 */
70 function setupTemplate( $classname, $repository = false, $cache_dir = false ) {
71 return new $classname( $this->getConfig() );
72 }
73
74 /**
75 * Generates array of language links for the current page
76 *
77 * @return array
78 */
79 public function getLanguages() {
80 global $wgHideInterlanguageLinks;
81 if ( $wgHideInterlanguageLinks ) {
82 return [];
83 }
84
85 $userLang = $this->getLanguage();
86 $languageLinks = [];
87
88 foreach ( $this->getOutput()->getLanguageLinks() as $languageLinkText ) {
89 $class = 'interlanguage-link interwiki-' . explode( ':', $languageLinkText, 2 )[0];
90
91 $languageLinkTitle = Title::newFromText( $languageLinkText );
92 if ( $languageLinkTitle ) {
93 $ilInterwikiCode = $languageLinkTitle->getInterwiki();
94 $ilLangName = Language::fetchLanguageName( $ilInterwikiCode );
95
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();
101 } else {
102 // Last resort: fallback to the language link target
103 $ilLangName = $languageLinkText;
104 }
105 } else {
106 // Use the language autonym as display text
107 $ilLangName = $this->formatLanguageName( $ilLangName );
108 }
109
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(
113 $ilInterwikiCode,
114 $userLang->getCode()
115 );
116
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()
125 )->text();
126 } else {
127 $ilTitle = wfMessage(
128 'interlanguage-link-title-nonlang',
129 $languageLinkTitleText,
130 $ilFriendlySiteName->text()
131 )->text();
132 }
133 } else {
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";
139 }
140 } elseif ( $languageLinkTitleText === '' ) {
141 $ilTitle = wfMessage(
142 'interlanguage-link-title-langonly',
143 $ilLangLocalName
144 )->text();
145 } else {
146 $ilTitle = wfMessage(
147 'interlanguage-link-title',
148 $languageLinkTitleText,
149 $ilLangLocalName
150 )->text();
151 }
152
153 $ilInterwikiCodeBCP47 = LanguageCode::bcp47( $ilInterwikiCode );
154 $languageLink = [
155 'href' => $languageLinkTitle->getFullURL(),
156 'text' => $ilLangName,
157 'title' => $ilTitle,
158 'class' => $class,
159 'link-class' => 'interlanguage-link-target',
160 'lang' => $ilInterwikiCodeBCP47,
161 'hreflang' => $ilInterwikiCodeBCP47,
162 ];
163 Hooks::run(
164 'SkinTemplateGetLanguageLink',
165 [ &$languageLink, $languageLinkTitle, $this->getTitle(), $this->getOutput() ]
166 );
167 $languageLinks[] = $languageLink;
168 }
169 }
170
171 return $languageLinks;
172 }
173
174 /**
175 * @return QuickTemplate
176 */
177 protected function setupTemplateForOutput() {
178 $request = $this->getRequest();
179 $user = $this->getUser();
180 $title = $this->getTitle();
181
182 $tpl = $this->setupTemplate( $this->template, 'skins' );
183
184 $this->thispage = $title->getPrefixedDBkey();
185 $this->titletxt = $title->getPrefixedText();
186 $this->userpage = $user->getUserPage()->getPrefixedText();
187 $query = [];
188 if ( !$request->wasPosted() ) {
189 $query = $request->getValues();
190 unset( $query['title'] );
191 unset( $query['returnto'] );
192 unset( $query['returntoquery'] );
193 }
194 $this->thisquery = wfArrayToCgi( $query );
195 $this->loggedin = $user->isLoggedIn();
196 $this->username = $user->getName();
197
198 if ( $this->loggedin ) {
199 $this->userpageUrlDetails = self::makeUrlDetails( $this->userpage );
200 } else {
201 # This won't be used in the standard skins, but we define it to preserve the interface
202 # To save time, we check for existence
203 $this->userpageUrlDetails = self::makeKnownUrlDetails( $this->userpage );
204 }
205
206 return $tpl;
207 }
208
209 /**
210 * Initialize various variables and generate the template
211 */
212 function outputPage() {
213 Profiler::instance()->setTemplated( true );
214 $out = $this->getOutput();
215
216 $this->initPage( $out );
217 $tpl = $this->prepareQuickTemplate();
218 // execute template
219 $res = $tpl->execute();
220
221 // result may be an error
222 $this->printOrError( $res );
223 }
224
225 /**
226 * Wrap the body text with language information and identifiable element
227 *
228 * @param Title $title
229 * @param string $html body text
230 * @return string html
231 */
232 protected function wrapHTML( $title, $html ) {
233 # An ID that includes the actual body text; without categories, contentSub, ...
234 $realBodyAttribs = [ 'id' => 'mw-content-text' ];
235
236 # Add a mw-content-ltr/rtl class to be able to style based on text
237 # direction when the content is different from the UI language (only
238 # when viewing)
239 # Most information on special pages and file pages is in user language,
240 # rather than content language, so those will not get this
241 if ( Action::getActionName( $this ) === 'view' &&
242 ( !$title->inNamespaces( NS_SPECIAL, NS_FILE ) || $title->isRedirect() ) ) {
243 $pageLang = $title->getPageViewLanguage();
244 $realBodyAttribs['lang'] = $pageLang->getHtmlCode();
245 $realBodyAttribs['dir'] = $pageLang->getDir();
246 $realBodyAttribs['class'] = 'mw-content-' . $pageLang->getDir();
247 }
248
249 return Html::rawElement( 'div', $realBodyAttribs, $html );
250 }
251
252 /**
253 * initialize various variables and generate the template
254 *
255 * @since 1.23
256 * @return QuickTemplate The template to be executed by outputPage
257 */
258 protected function prepareQuickTemplate() {
259 global $wgScript, $wgStylePath, $wgMimeType,
260 $wgSitename, $wgLogo, $wgMaxCredits,
261 $wgShowCreditsIfMax, $wgArticlePath,
262 $wgScriptPath, $wgServer;
263
264 $title = $this->getTitle();
265 $request = $this->getRequest();
266 $out = $this->getOutput();
267 $tpl = $this->setupTemplateForOutput();
268
269 $tpl->set( 'title', $out->getPageTitle() );
270 $tpl->set( 'pagetitle', $out->getHTMLTitle() );
271 $tpl->set( 'displaytitle', $out->mPageLinkTitle );
272
273 $tpl->set( 'thispage', $this->thispage );
274 $tpl->set( 'titleprefixeddbkey', $this->thispage );
275 $tpl->set( 'titletext', $title->getText() );
276 $tpl->set( 'articleid', $title->getArticleID() );
277
278 $tpl->set( 'isarticle', $out->isArticle() );
279
280 $subpagestr = $this->subPageSubtitle();
281 if ( $subpagestr !== '' ) {
282 $subpagestr = '<span class="subpages">' . $subpagestr . '</span>';
283 }
284 $tpl->set( 'subtitle', $subpagestr . $out->getSubtitle() );
285
286 $undelete = $this->getUndeleteLink();
287 if ( $undelete === '' ) {
288 $tpl->set( 'undelete', '' );
289 } else {
290 $tpl->set( 'undelete', '<span class="subpages">' . $undelete . '</span>' );
291 }
292
293 $tpl->set( 'catlinks', $this->getCategories() );
294 if ( $out->isSyndicated() ) {
295 $feeds = [];
296 foreach ( $out->getSyndicationLinks() as $format => $link ) {
297 $feeds[$format] = [
298 // Messages: feed-atom, feed-rss
299 'text' => $this->msg( "feed-$format" )->text(),
300 'href' => $link
301 ];
302 }
303 $tpl->set( 'feeds', $feeds );
304 } else {
305 $tpl->set( 'feeds', false );
306 }
307
308 $tpl->set( 'mimetype', $wgMimeType );
309 $tpl->set( 'charset', 'UTF-8' );
310 $tpl->set( 'wgScript', $wgScript );
311 $tpl->set( 'skinname', $this->skinname );
312 $tpl->set( 'skinclass', static::class );
313 $tpl->set( 'skin', $this );
314 $tpl->set( 'stylename', $this->stylename );
315 $tpl->set( 'printable', $out->isPrintable() );
316 $tpl->set( 'handheld', $request->getBool( 'handheld' ) );
317 $tpl->set( 'loggedin', $this->loggedin );
318 $tpl->set( 'notspecialpage', !$title->isSpecialPage() );
319 $tpl->set( 'searchaction', $this->getSearchLink() );
320 $tpl->set( 'searchtitle', SpecialPage::getTitleFor( 'Search' )->getPrefixedDBkey() );
321 $tpl->set( 'search', trim( $request->getVal( 'search' ) ) );
322 $tpl->set( 'stylepath', $wgStylePath );
323 $tpl->set( 'articlepath', $wgArticlePath );
324 $tpl->set( 'scriptpath', $wgScriptPath );
325 $tpl->set( 'serverurl', $wgServer );
326 $tpl->set( 'logopath', $wgLogo );
327 $tpl->set( 'sitename', $wgSitename );
328
329 $userLang = $this->getLanguage();
330 $userLangCode = $userLang->getHtmlCode();
331 $userLangDir = $userLang->getDir();
332
333 $tpl->set( 'lang', $userLangCode );
334 $tpl->set( 'dir', $userLangDir );
335 $tpl->set( 'rtl', $userLang->isRTL() );
336
337 $tpl->set( 'capitalizeallnouns', $userLang->capitalizeAllNouns() ? ' capitalize-all-nouns' : '' );
338 $tpl->set( 'showjumplinks', true ); // showjumplinks preference has been removed
339 $tpl->set( 'username', $this->loggedin ? $this->username : null );
340 $tpl->set( 'userpage', $this->userpage );
341 $tpl->set( 'userpageurl', $this->userpageUrlDetails['href'] );
342 $tpl->set( 'userlang', $userLangCode );
343
344 // Users can have their language set differently than the
345 // content of the wiki. For these users, tell the web browser
346 // that interface elements are in a different language.
347 $tpl->set( 'userlangattributes', '' );
348 $tpl->set( 'specialpageattributes', '' ); # obsolete
349 // Used by VectorBeta to insert HTML before content but after the
350 // heading for the page title. Defaults to empty string.
351 $tpl->set( 'prebodyhtml', '' );
352
353 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
354 if (
355 $userLangCode !== $contLang->getHtmlCode() ||
356 $userLangDir !== $contLang->getDir()
357 ) {
358 $escUserlang = htmlspecialchars( $userLangCode );
359 $escUserdir = htmlspecialchars( $userLangDir );
360 // Attributes must be in double quotes because htmlspecialchars() doesn't
361 // escape single quotes
362 $attrs = " lang=\"$escUserlang\" dir=\"$escUserdir\"";
363 $tpl->set( 'userlangattributes', $attrs );
364 }
365
366 $tpl->set( 'newtalk', $this->getNewtalks() );
367 $tpl->set( 'logo', $this->logoText() );
368
369 $tpl->set( 'copyright', false );
370 // No longer used
371 $tpl->set( 'viewcount', false );
372 $tpl->set( 'lastmod', false );
373 $tpl->set( 'credits', false );
374 $tpl->set( 'numberofwatchingusers', false );
375 if ( $title->exists() ) {
376 if ( $out->isArticle() && $this->isRevisionCurrent() ) {
377 if ( $wgMaxCredits != 0 ) {
378 /** @var CreditsAction $action */
379 $action = Action::factory(
380 'credits', $this->getWikiPage(), $this->getContext() );
381 $tpl->set( 'credits',
382 $action->getCredits( $wgMaxCredits, $wgShowCreditsIfMax ) );
383 } else {
384 $tpl->set( 'lastmod', $this->lastModified() );
385 }
386 }
387 if ( $out->showsCopyright() ) {
388 $tpl->set( 'copyright', $this->getCopyright() );
389 }
390 }
391
392 $tpl->set( 'copyrightico', $this->getCopyrightIcon() );
393 $tpl->set( 'poweredbyico', $this->getPoweredBy() );
394 $tpl->set( 'disclaimer', $this->disclaimerLink() );
395 $tpl->set( 'privacy', $this->privacyLink() );
396 $tpl->set( 'about', $this->aboutLink() );
397
398 $tpl->set( 'footerlinks', [
399 'info' => [
400 'lastmod',
401 'numberofwatchingusers',
402 'credits',
403 'copyright',
404 ],
405 'places' => [
406 'privacy',
407 'about',
408 'disclaimer',
409 ],
410 ] );
411
412 global $wgFooterIcons;
413 $tpl->set( 'footericons', $wgFooterIcons );
414 foreach ( $tpl->data['footericons'] as $footerIconsKey => &$footerIconsBlock ) {
415 if ( count( $footerIconsBlock ) > 0 ) {
416 foreach ( $footerIconsBlock as &$footerIcon ) {
417 if ( isset( $footerIcon['src'] ) ) {
418 if ( !isset( $footerIcon['width'] ) ) {
419 $footerIcon['width'] = 88;
420 }
421 if ( !isset( $footerIcon['height'] ) ) {
422 $footerIcon['height'] = 31;
423 }
424 }
425 }
426 } else {
427 unset( $tpl->data['footericons'][$footerIconsKey] );
428 }
429 }
430
431 $tpl->set( 'indicators', $out->getIndicators() );
432
433 $tpl->set( 'sitenotice', $this->getSiteNotice() );
434 $tpl->set( 'printfooter', $this->printSource() );
435 // Wrap the bodyText with #mw-content-text element
436 $out->mBodytext = $this->wrapHTML( $title, $out->mBodytext );
437 $tpl->set( 'bodytext', $out->mBodytext );
438
439 $language_urls = $this->getLanguages();
440 if ( count( $language_urls ) ) {
441 $tpl->set( 'language_urls', $language_urls );
442 } else {
443 $tpl->set( 'language_urls', false );
444 }
445
446 # Personal toolbar
447 $tpl->set( 'personal_urls', $this->buildPersonalUrls() );
448 $content_navigation = $this->buildContentNavigationUrls();
449 $content_actions = $this->buildContentActionUrls( $content_navigation );
450 $tpl->set( 'content_navigation', $content_navigation );
451 $tpl->set( 'content_actions', $content_actions );
452
453 $tpl->set( 'sidebar', $this->buildSidebar() );
454 $tpl->set( 'nav_urls', $this->buildNavUrls() );
455
456 // Do this last in case hooks above add bottom scripts
457 $tpl->set( 'bottomscripts', $this->bottomScripts() );
458
459 // Set the head scripts near the end, in case the above actions resulted in added scripts
460 $tpl->set( 'headelement', $out->headElement( $this ) );
461
462 $tpl->set( 'debug', '' );
463 $tpl->set( 'debughtml', $this->generateDebugHTML() );
464 $tpl->set( 'reporttime', wfReportTime( $out->getCSPNonce() ) );
465
466 // Avoid PHP 7.1 warning of passing $this by reference
467 $skinTemplate = $this;
468 // original version by hansm
469 if ( !Hooks::run( 'SkinTemplateOutputPageBeforeExec', [ &$skinTemplate, &$tpl ] ) ) {
470 wfDebug( __METHOD__ . ": Hook SkinTemplateOutputPageBeforeExec broke outputPage execution!\n" );
471 }
472
473 // Set the bodytext to another key so that skins can just output it on its own
474 // and output printfooter and debughtml separately
475 $tpl->set( 'bodycontent', $tpl->data['bodytext'] );
476
477 // Append printfooter and debughtml onto bodytext so that skins that
478 // were already using bodytext before they were split out don't suddenly
479 // start not outputting information.
480 $tpl->data['bodytext'] .= Html::rawElement(
481 'div',
482 [ 'class' => 'printfooter' ],
483 "\n{$tpl->data['printfooter']}"
484 ) . "\n";
485 $tpl->data['bodytext'] .= $tpl->data['debughtml'];
486
487 // allow extensions adding stuff after the page content.
488 // See Skin::afterContentHook() for further documentation.
489 $tpl->set( 'dataAfterContent', $this->afterContentHook() );
490
491 return $tpl;
492 }
493
494 /**
495 * Get the HTML for the p-personal list
496 * @return string
497 */
498 public function getPersonalToolsList() {
499 return $this->makePersonalToolsList();
500 }
501
502 /**
503 * Get the HTML for the personal tools list
504 *
505 * @since 1.31
506 *
507 * @param array|null $personalTools
508 * @param array $options
509 * @return string
510 */
511 public function makePersonalToolsList( $personalTools = null, $options = [] ) {
512 $tpl = $this->setupTemplateForOutput();
513 $tpl->set( 'personal_urls', $this->buildPersonalUrls() );
514 $html = '';
515
516 if ( $personalTools === null ) {
517 $personalTools = ( $tpl instanceof BaseTemplate )
518 ? $tpl->getPersonalTools()
519 : [];
520 }
521
522 foreach ( $personalTools as $key => $item ) {
523 $html .= $tpl->makeListItem( $key, $item, $options );
524 }
525
526 return $html;
527 }
528
529 /**
530 * Get personal tools for the user
531 *
532 * @since 1.31
533 *
534 * @return array Array of personal tools
535 */
536 public function getStructuredPersonalTools() {
537 $tpl = $this->setupTemplateForOutput();
538 $tpl->set( 'personal_urls', $this->buildPersonalUrls() );
539
540 return ( $tpl instanceof BaseTemplate ) ? $tpl->getPersonalTools() : [];
541 }
542
543 /**
544 * Format language name for use in sidebar interlanguage links list.
545 * By default it is capitalized.
546 *
547 * @param string $name Language name, e.g. "English" or "español"
548 * @return string
549 * @private
550 */
551 function formatLanguageName( $name ) {
552 return $this->getLanguage()->ucfirst( $name );
553 }
554
555 /**
556 * Output the string, or print error message if it's
557 * an error object of the appropriate type.
558 * For the base class, assume strings all around.
559 *
560 * @param string $str
561 * @private
562 */
563 function printOrError( $str ) {
564 echo $str;
565 }
566
567 /**
568 * Output a boolean indicating if buildPersonalUrls should output separate
569 * login and create account links or output a combined link
570 * By default we simply return a global config setting that affects most skins
571 * This is setup as a method so that like with $wgLogo and getLogo() a skin
572 * can override this setting and always output one or the other if it has
573 * a reason it can't output one of the two modes.
574 * @return bool
575 */
576 function useCombinedLoginLink() {
577 global $wgUseCombinedLoginLink;
578 return $wgUseCombinedLoginLink;
579 }
580
581 /**
582 * build array of urls for personal toolbar
583 * @return array
584 */
585 protected function buildPersonalUrls() {
586 $title = $this->getTitle();
587 $request = $this->getRequest();
588 $pageurl = $title->getLocalURL();
589 $authManager = AuthManager::singleton();
590
591 /* set up the default links for the personal toolbar */
592 $personal_urls = [];
593
594 # Due to T34276, if a user does not have read permissions,
595 # $this->getTitle() will just give Special:Badtitle, which is
596 # not especially useful as a returnto parameter. Use the title
597 # from the request instead, if there was one.
598 if ( $this->getUser()->isAllowed( 'read' ) ) {
599 $page = $this->getTitle();
600 } else {
601 $page = Title::newFromText( $request->getVal( 'title', '' ) );
602 }
603 $page = $request->getVal( 'returnto', $page );
604 $returnto = [];
605 if ( strval( $page ) !== '' ) {
606 $returnto['returnto'] = $page;
607 $query = $request->getVal( 'returntoquery', $this->thisquery );
608 $paramsArray = wfCgiToArray( $query );
609 $query = wfArrayToCgi( $paramsArray );
610 if ( $query != '' ) {
611 $returnto['returntoquery'] = $query;
612 }
613 }
614
615 if ( $this->loggedin ) {
616 $personal_urls['userpage'] = [
617 'text' => $this->username,
618 'href' => &$this->userpageUrlDetails['href'],
619 'class' => $this->userpageUrlDetails['exists'] ? false : 'new',
620 'exists' => $this->userpageUrlDetails['exists'],
621 'active' => ( $this->userpageUrlDetails['href'] == $pageurl ),
622 'dir' => 'auto'
623 ];
624 $usertalkUrlDetails = $this->makeTalkUrlDetails( $this->userpage );
625 $personal_urls['mytalk'] = [
626 'text' => $this->msg( 'mytalk' )->text(),
627 'href' => &$usertalkUrlDetails['href'],
628 'class' => $usertalkUrlDetails['exists'] ? false : 'new',
629 'exists' => $usertalkUrlDetails['exists'],
630 'active' => ( $usertalkUrlDetails['href'] == $pageurl )
631 ];
632 $href = self::makeSpecialUrl( 'Preferences' );
633 $personal_urls['preferences'] = [
634 'text' => $this->msg( 'mypreferences' )->text(),
635 'href' => $href,
636 'active' => ( $href == $pageurl )
637 ];
638
639 if ( $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
640 $href = self::makeSpecialUrl( 'Watchlist' );
641 $personal_urls['watchlist'] = [
642 'text' => $this->msg( 'mywatchlist' )->text(),
643 'href' => $href,
644 'active' => ( $href == $pageurl )
645 ];
646 }
647
648 # We need to do an explicit check for Special:Contributions, as we
649 # have to match both the title, and the target, which could come
650 # from request values (Special:Contributions?target=Jimbo_Wales)
651 # or be specified in "sub page" form
652 # (Special:Contributions/Jimbo_Wales). The plot
653 # thickens, because the Title object is altered for special pages,
654 # so it doesn't contain the original alias-with-subpage.
655 $origTitle = Title::newFromText( $request->getText( 'title' ) );
656 if ( $origTitle instanceof Title && $origTitle->isSpecialPage() ) {
657 list( $spName, $spPar ) =
658 MediaWikiServices::getInstance()->getSpecialPageFactory()->
659 resolveAlias( $origTitle->getText() );
660 $active = $spName == 'Contributions'
661 && ( ( $spPar && $spPar == $this->username )
662 || $request->getText( 'target' ) == $this->username );
663 } else {
664 $active = false;
665 }
666
667 $href = self::makeSpecialUrlSubpage( 'Contributions', $this->username );
668 $personal_urls['mycontris'] = [
669 'text' => $this->msg( 'mycontris' )->text(),
670 'href' => $href,
671 'active' => $active
672 ];
673
674 // if we can't set the user, we can't unset it either
675 if ( $request->getSession()->canSetUser() ) {
676 $personal_urls['logout'] = [
677 'text' => $this->msg( 'pt-userlogout' )->text(),
678 'href' => self::makeSpecialUrl( 'Userlogout',
679 // Note: userlogout link must always contain an & character, otherwise we might not be able
680 // to detect a buggy precaching proxy (T19790)
681 ( $title->isSpecial( 'Preferences' ) ? [] : $returnto ) ),
682 'active' => false
683 ];
684 }
685 } else {
686 $useCombinedLoginLink = $this->useCombinedLoginLink();
687 if ( !$authManager->canCreateAccounts() || !$authManager->canAuthenticateNow() ) {
688 // don't show combined login/signup link if one of those is actually not available
689 $useCombinedLoginLink = false;
690 }
691
692 $loginlink = $this->getUser()->isAllowed( 'createaccount' ) && $useCombinedLoginLink
693 ? 'nav-login-createaccount'
694 : 'pt-login';
695
696 $login_url = [
697 'text' => $this->msg( $loginlink )->text(),
698 'href' => self::makeSpecialUrl( 'Userlogin', $returnto ),
699 'active' => $title->isSpecial( 'Userlogin' )
700 || $title->isSpecial( 'CreateAccount' ) && $useCombinedLoginLink,
701 ];
702 $createaccount_url = [
703 'text' => $this->msg( 'pt-createaccount' )->text(),
704 'href' => self::makeSpecialUrl( 'CreateAccount', $returnto ),
705 'active' => $title->isSpecial( 'CreateAccount' ),
706 ];
707
708 // No need to show Talk and Contributions to anons if they can't contribute!
709 if ( User::groupHasPermission( '*', 'edit' ) ) {
710 // Because of caching, we can't link directly to the IP talk and
711 // contributions pages. Instead we use the special page shortcuts
712 // (which work correctly regardless of caching). This means we can't
713 // determine whether these links are active or not, but since major
714 // skins (MonoBook, Vector) don't use this information, it's not a
715 // huge loss.
716 $personal_urls['anontalk'] = [
717 'text' => $this->msg( 'anontalk' )->text(),
718 'href' => self::makeSpecialUrlSubpage( 'Mytalk', false ),
719 'active' => false
720 ];
721 $personal_urls['anoncontribs'] = [
722 'text' => $this->msg( 'anoncontribs' )->text(),
723 'href' => self::makeSpecialUrlSubpage( 'Mycontributions', false ),
724 'active' => false
725 ];
726 }
727
728 if (
729 $authManager->canCreateAccounts()
730 && $this->getUser()->isAllowed( 'createaccount' )
731 && !$useCombinedLoginLink
732 ) {
733 $personal_urls['createaccount'] = $createaccount_url;
734 }
735
736 if ( $authManager->canAuthenticateNow() ) {
737 $key = User::groupHasPermission( '*', 'read' )
738 ? 'login'
739 : 'login-private';
740 $personal_urls[$key] = $login_url;
741 }
742 }
743
744 Hooks::runWithoutAbort( 'PersonalUrls', [ &$personal_urls, &$title, $this ] );
745 return $personal_urls;
746 }
747
748 /**
749 * Builds an array with tab definition
750 *
751 * @param Title $title Page Where the tab links to
752 * @param string|array $message Message key or an array of message keys (will fall back)
753 * @param bool $selected Display the tab as selected
754 * @param string $query Query string attached to tab URL
755 * @param bool $checkEdit Check if $title exists and mark with .new if one doesn't
756 *
757 * @return array
758 */
759 function tabAction( $title, $message, $selected, $query = '', $checkEdit = false ) {
760 $classes = [];
761 if ( $selected ) {
762 $classes[] = 'selected';
763 }
764 $exists = true;
765 if ( $checkEdit && !$title->isKnown() ) {
766 $classes[] = 'new';
767 $exists = false;
768 if ( $query !== '' ) {
769 $query = 'action=edit&redlink=1&' . $query;
770 } else {
771 $query = 'action=edit&redlink=1';
772 }
773 }
774
775 $services = MediaWikiServices::getInstance();
776 $linkClass = $services->getLinkRenderer()->getLinkClasses( $title );
777
778 // wfMessageFallback will nicely accept $message as an array of fallbacks
779 // or just a single key
780 $msg = wfMessageFallback( $message )->setContext( $this->getContext() );
781 if ( is_array( $message ) ) {
782 // for hook compatibility just keep the last message name
783 $message = end( $message );
784 }
785 if ( $msg->exists() ) {
786 $text = $msg->text();
787 } else {
788 $text = $services->getContentLanguage()->getConverter()->
789 convertNamespace( $services->getNamespaceInfo()->
790 getSubject( $title->getNamespace() ) );
791 }
792
793 // Avoid PHP 7.1 warning of passing $this by reference
794 $skinTemplate = $this;
795 $result = [];
796 if ( !Hooks::run( 'SkinTemplateTabAction', [ &$skinTemplate,
797 $title, $message, $selected, $checkEdit,
798 &$classes, &$query, &$text, &$result ] ) ) {
799 return $result;
800 }
801
802 $result = [
803 'class' => implode( ' ', $classes ),
804 'text' => $text,
805 'href' => $title->getLocalURL( $query ),
806 'exists' => $exists,
807 'primary' => true ];
808 if ( $linkClass !== '' ) {
809 $result['link-class'] = $linkClass;
810 }
811
812 return $result;
813 }
814
815 function makeTalkUrlDetails( $name, $urlaction = '' ) {
816 $title = Title::newFromText( $name );
817 if ( !is_object( $title ) ) {
818 throw new MWException( __METHOD__ . " given invalid pagename $name" );
819 }
820 $title = $title->getTalkPage();
821 self::checkTitle( $title, $name );
822 return [
823 'href' => $title->getLocalURL( $urlaction ),
824 'exists' => $title->isKnown(),
825 ];
826 }
827
828 /**
829 * @todo is this even used?
830 * @param string $name
831 * @param string $urlaction
832 * @return array
833 */
834 function makeArticleUrlDetails( $name, $urlaction = '' ) {
835 $title = Title::newFromText( $name );
836 $title = $title->getSubjectPage();
837 self::checkTitle( $title, $name );
838 return [
839 'href' => $title->getLocalURL( $urlaction ),
840 'exists' => $title->exists(),
841 ];
842 }
843
844 /**
845 * a structured array of links usually used for the tabs in a skin
846 *
847 * There are 4 standard sections
848 * namespaces: Used for namespace tabs like special, page, and talk namespaces
849 * views: Used for primary page views like read, edit, history
850 * actions: Used for most extra page actions like deletion, protection, etc...
851 * variants: Used to list the language variants for the page
852 *
853 * Each section's value is a key/value array of links for that section.
854 * The links themselves have these common keys:
855 * - class: The css classes to apply to the tab
856 * - text: The text to display on the tab
857 * - href: The href for the tab to point to
858 * - rel: An optional rel= for the tab's link
859 * - redundant: If true the tab will be dropped in skins using content_actions
860 * this is useful for tabs like "Read" which only have meaning in skins that
861 * take special meaning from the grouped structure of content_navigation
862 *
863 * Views also have an extra key which can be used:
864 * - primary: If this is not true skins like vector may try to hide the tab
865 * when the user has limited space in their browser window
866 *
867 * content_navigation using code also expects these ids to be present on the
868 * links, however these are usually automatically generated by SkinTemplate
869 * itself and are not necessary when using a hook. The only things these may
870 * matter to are people modifying content_navigation after it's initial creation:
871 * - id: A "preferred" id, most skins are best off outputting this preferred
872 * id for best compatibility.
873 * - tooltiponly: This is set to true for some tabs in cases where the system
874 * believes that the accesskey should not be added to the tab.
875 *
876 * @return array
877 */
878 protected function buildContentNavigationUrls() {
879 global $wgDisableLangConversion;
880
881 // Display tabs for the relevant title rather than always the title itself
882 $title = $this->getRelevantTitle();
883 $onPage = $title->equals( $this->getTitle() );
884
885 $out = $this->getOutput();
886 $request = $this->getRequest();
887 $user = $this->getUser();
888
889 $content_navigation = [
890 'namespaces' => [],
891 'views' => [],
892 'actions' => [],
893 'variants' => []
894 ];
895
896 // parameters
897 $action = $request->getVal( 'action', 'view' );
898
899 $userCanRead = $title->quickUserCan( 'read', $user );
900
901 // Avoid PHP 7.1 warning of passing $this by reference
902 $skinTemplate = $this;
903 $preventActiveTabs = false;
904 Hooks::run( 'SkinTemplatePreventOtherActiveTabs', [ &$skinTemplate, &$preventActiveTabs ] );
905
906 // Checks if page is some kind of content
907 if ( $title->canExist() ) {
908 // Gets page objects for the related namespaces
909 $subjectPage = $title->getSubjectPage();
910 $talkPage = $title->getTalkPage();
911
912 // Determines if this is a talk page
913 $isTalk = $title->isTalkPage();
914
915 // Generates XML IDs from namespace names
916 $subjectId = $title->getNamespaceKey( '' );
917
918 if ( $subjectId == 'main' ) {
919 $talkId = 'talk';
920 } else {
921 $talkId = "{$subjectId}_talk";
922 }
923
924 $skname = $this->skinname;
925
926 // Adds namespace links
927 $subjectMsg = [ "nstab-$subjectId" ];
928 if ( $subjectPage->isMainPage() ) {
929 array_unshift( $subjectMsg, 'mainpage-nstab' );
930 }
931 $content_navigation['namespaces'][$subjectId] = $this->tabAction(
932 $subjectPage, $subjectMsg, !$isTalk && !$preventActiveTabs, '', $userCanRead
933 );
934 $content_navigation['namespaces'][$subjectId]['context'] = 'subject';
935 $content_navigation['namespaces'][$talkId] = $this->tabAction(
936 $talkPage, [ "nstab-$talkId", 'talk' ], $isTalk && !$preventActiveTabs, '', $userCanRead
937 );
938 $content_navigation['namespaces'][$talkId]['context'] = 'talk';
939
940 if ( $userCanRead ) {
941 // Adds "view" view link
942 if ( $title->isKnown() ) {
943 $content_navigation['views']['view'] = $this->tabAction(
944 $isTalk ? $talkPage : $subjectPage,
945 [ "$skname-view-view", 'view' ],
946 ( $onPage && ( $action == 'view' || $action == 'purge' ) ), '', true
947 );
948 // signal to hide this from simple content_actions
949 $content_navigation['views']['view']['redundant'] = true;
950 }
951
952 $page = $this->canUseWikiPage() ? $this->getWikiPage() : false;
953 $isRemoteContent = $page && !$page->isLocal();
954
955 // If it is a non-local file, show a link to the file in its own repository
956 // @todo abstract this for remote content that isn't a file
957 if ( $isRemoteContent ) {
958 $content_navigation['views']['view-foreign'] = [
959 'class' => '',
960 'text' => wfMessageFallback( "$skname-view-foreign", 'view-foreign' )->
961 setContext( $this->getContext() )->
962 params( $page->getWikiDisplayName() )->text(),
963 'href' => $page->getSourceURL(),
964 'primary' => false,
965 ];
966 }
967
968 // Checks if user can edit the current page if it exists or create it otherwise
969 if ( $title->quickUserCan( 'edit', $user )
970 && ( $title->exists() || $title->quickUserCan( 'create', $user ) )
971 ) {
972 // Builds CSS class for talk page links
973 $isTalkClass = $isTalk ? ' istalk' : '';
974 // Whether the user is editing the page
975 $isEditing = $onPage && ( $action == 'edit' || $action == 'submit' );
976 // Whether to show the "Add a new section" tab
977 // Checks if this is a current rev of talk page and is not forced to be hidden
978 $showNewSection = !$out->forceHideNewSectionLink()
979 && ( ( $isTalk && $this->isRevisionCurrent() ) || $out->showNewSectionLink() );
980 $section = $request->getVal( 'section' );
981
982 if ( $title->exists()
983 || ( $title->getNamespace() == NS_MEDIAWIKI
984 && $title->getDefaultMessageText() !== false
985 )
986 ) {
987 $msgKey = $isRemoteContent ? 'edit-local' : 'edit';
988 } else {
989 $msgKey = $isRemoteContent ? 'create-local' : 'create';
990 }
991 $content_navigation['views']['edit'] = [
992 'class' => ( $isEditing && ( $section !== 'new' || !$showNewSection )
993 ? 'selected'
994 : ''
995 ) . $isTalkClass,
996 'text' => wfMessageFallback( "$skname-view-$msgKey", $msgKey )
997 ->setContext( $this->getContext() )->text(),
998 'href' => $title->getLocalURL( $this->editUrlOptions() ),
999 'primary' => !$isRemoteContent, // don't collapse this in vector
1000 ];
1001
1002 // section link
1003 if ( $showNewSection ) {
1004 // Adds new section link
1005 // $content_navigation['actions']['addsection']
1006 $content_navigation['views']['addsection'] = [
1007 'class' => ( $isEditing && $section == 'new' ) ? 'selected' : false,
1008 'text' => wfMessageFallback( "$skname-action-addsection", 'addsection' )
1009 ->setContext( $this->getContext() )->text(),
1010 'href' => $title->getLocalURL( 'action=edit&section=new' )
1011 ];
1012 }
1013 // Checks if the page has some kind of viewable source content
1014 } elseif ( $title->hasSourceText() ) {
1015 // Adds view source view link
1016 $content_navigation['views']['viewsource'] = [
1017 'class' => ( $onPage && $action == 'edit' ) ? 'selected' : false,
1018 'text' => wfMessageFallback( "$skname-action-viewsource", 'viewsource' )
1019 ->setContext( $this->getContext() )->text(),
1020 'href' => $title->getLocalURL( $this->editUrlOptions() ),
1021 'primary' => true, // don't collapse this in vector
1022 ];
1023 }
1024
1025 // Checks if the page exists
1026 if ( $title->exists() ) {
1027 // Adds history view link
1028 $content_navigation['views']['history'] = [
1029 'class' => ( $onPage && $action == 'history' ) ? 'selected' : false,
1030 'text' => wfMessageFallback( "$skname-view-history", 'history_short' )
1031 ->setContext( $this->getContext() )->text(),
1032 'href' => $title->getLocalURL( 'action=history' ),
1033 ];
1034
1035 if ( $title->quickUserCan( 'delete', $user ) ) {
1036 $content_navigation['actions']['delete'] = [
1037 'class' => ( $onPage && $action == 'delete' ) ? 'selected' : false,
1038 'text' => wfMessageFallback( "$skname-action-delete", 'delete' )
1039 ->setContext( $this->getContext() )->text(),
1040 'href' => $title->getLocalURL( 'action=delete' )
1041 ];
1042 }
1043
1044 if ( $title->quickUserCan( 'move', $user ) ) {
1045 $moveTitle = SpecialPage::getTitleFor( 'Movepage', $title->getPrefixedDBkey() );
1046 $content_navigation['actions']['move'] = [
1047 'class' => $this->getTitle()->isSpecial( 'Movepage' ) ? 'selected' : false,
1048 'text' => wfMessageFallback( "$skname-action-move", 'move' )
1049 ->setContext( $this->getContext() )->text(),
1050 'href' => $moveTitle->getLocalURL()
1051 ];
1052 }
1053 } else {
1054 // article doesn't exist or is deleted
1055 if ( $title->quickUserCan( 'deletedhistory', $user ) ) {
1056 $n = $title->isDeleted();
1057 if ( $n ) {
1058 $undelTitle = SpecialPage::getTitleFor( 'Undelete', $title->getPrefixedDBkey() );
1059 // If the user can't undelete but can view deleted
1060 // history show them a "View .. deleted" tab instead.
1061 $msgKey = $title->quickUserCan( 'undelete', $user ) ? 'undelete' : 'viewdeleted';
1062 $content_navigation['actions']['undelete'] = [
1063 'class' => $this->getTitle()->isSpecial( 'Undelete' ) ? 'selected' : false,
1064 'text' => wfMessageFallback( "$skname-action-$msgKey", "{$msgKey}_short" )
1065 ->setContext( $this->getContext() )->numParams( $n )->text(),
1066 'href' => $undelTitle->getLocalURL()
1067 ];
1068 }
1069 }
1070 }
1071
1072 if ( $title->quickUserCan( 'protect', $user ) && $title->getRestrictionTypes() &&
1073 MediaWikiServices::getInstance()->getNamespaceInfo()->
1074 getRestrictionLevels( $title->getNamespace(), $user ) !== [ '' ]
1075 ) {
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" )
1082 ];
1083 }
1084
1085 // Checks if the user is logged in
1086 if ( $this->loggedin && $user->isAllowedAll( 'viewmywatchlist', 'editmywatchlist' ) ) {
1087 /**
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.
1095 */
1096 $mode = $user->isWatched( $title ) ? 'unwatch' : 'watch';
1097 $content_navigation['actions'][$mode] = [
1098 'class' => 'mw-watchlink ' . (
1099 $onPage && ( $action == 'watch' || $action == 'unwatch' ) ? 'selected' : ''
1100 ),
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
1106 'data' => [
1107 'mw' => 'interface',
1108 ],
1109 ];
1110 }
1111 }
1112
1113 // Avoid PHP 7.1 warning of passing $this by reference
1114 $skinTemplate = $this;
1115 Hooks::runWithoutAbort(
1116 'SkinTemplateNavigation',
1117 [ &$skinTemplate, &$content_navigation ]
1118 );
1119
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'] );
1133 } else {
1134 $params = [];
1135 }
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,
1143 'text' => $varname,
1144 'href' => $title->getLocalURL( [ 'variant' => $code ] + $params ),
1145 'lang' => LanguageCode::bcp47( $code ),
1146 'hreflang' => LanguageCode::bcp47( $code ),
1147 ];
1148 }
1149 }
1150 }
1151 } else {
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'
1158 ];
1159
1160 // Avoid PHP 7.1 warning of passing $this by reference
1161 $skinTemplate = $this;
1162 Hooks::runWithoutAbort( 'SkinTemplateNavigation::SpecialPage',
1163 [ &$skinTemplate, &$content_navigation ] );
1164 }
1165
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 ] );
1171
1172 // Setup xml ids and tooltip info
1173 foreach ( $content_navigation as $section => &$links ) {
1174 foreach ( $links as $key => &$link ) {
1175 $xmlID = $key;
1176 if ( isset( $link['context'] ) && $link['context'] == 'subject' ) {
1177 $xmlID = 'ca-nstab-' . $xmlID;
1178 } elseif ( isset( $link['context'] ) && $link['context'] == 'talk' ) {
1179 $xmlID = 'ca-talk';
1180 $link['rel'] = 'discussion';
1181 } elseif ( $section == 'variants' ) {
1182 $xmlID = 'ca-varlang-' . $xmlID;
1183 } else {
1184 $xmlID = 'ca-' . $xmlID;
1185 }
1186 $link['id'] = $xmlID;
1187 }
1188 }
1189
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;
1199 }
1200 if ( isset( $content_navigation['actions']['watch'] ) ) {
1201 $content_navigation['actions']['watch']['tooltiponly'] = true;
1202 }
1203 if ( isset( $content_navigation['actions']['unwatch'] ) ) {
1204 $content_navigation['actions']['unwatch']['tooltiponly'] = true;
1205 }
1206 }
1207
1208 return $content_navigation;
1209 }
1210
1211 /**
1212 * an array of edit links by default used for the tabs
1213 * @param array $content_navigation
1214 * @return array
1215 */
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
1220
1221 $content_actions = [];
1222
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
1227 continue;
1228 }
1229
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 );
1236 }
1237
1238 if ( isset( $content_actions[$key] ) ) {
1239 wfDebug( __METHOD__ . ": Found a duplicate key for $key while flattening " .
1240 "content_navigation into content_actions.\n" );
1241 continue;
1242 }
1243
1244 $content_actions[$key] = $value;
1245 }
1246 }
1247
1248 return $content_actions;
1249 }
1250
1251 /**
1252 * build array of common navigation links
1253 * @return array
1254 */
1255 protected function buildNavUrls() {
1256 global $wgUploadNavigationUrl;
1257
1258 $out = $this->getOutput();
1259 $request = $this->getRequest();
1260
1261 $nav_urls = [];
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' ) ];
1267 } else {
1268 $nav_urls['upload'] = false;
1269 }
1270 $nav_urls['specialpages'] = [ 'href' => self::makeSpecialUrl( 'Specialpages' ) ];
1271
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['mute'] = false;
1281 $nav_urls['emailuser'] = false;
1282 $nav_urls['userrights'] = false;
1283
1284 // A print stylesheet is attached to all pages, but nobody ever
1285 // figures that out. :) Add a link...
1286 if ( !$out->isPrintable() && ( $out->isArticle() || $this->getTitle()->isSpecialPage() ) ) {
1287 $nav_urls['print'] = [
1288 'text' => $this->msg( 'printableversion' )->text(),
1289 'href' => $this->getTitle()->getLocalURL(
1290 $request->appendQueryValue( 'printable', 'yes' ) )
1291 ];
1292 }
1293
1294 if ( $out->isArticle() ) {
1295 // Also add a "permalink" while we're at it
1296 $revid = $this->getRevisionId();
1297 if ( $revid ) {
1298 $nav_urls['permalink'] = [
1299 'text' => $this->msg( 'permalink' )->text(),
1300 'href' => $this->getTitle()->getLocalURL( "oldid=$revid" )
1301 ];
1302 }
1303
1304 // Avoid PHP 7.1 warning of passing $this by reference
1305 $skinTemplate = $this;
1306 // Use the copy of revision ID in case this undocumented, shady hook tries to mess with internals
1307 Hooks::run( 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink',
1308 [ &$skinTemplate, &$nav_urls, &$revid, &$revid ] );
1309 }
1310
1311 if ( $out->isArticleRelated() ) {
1312 $nav_urls['whatlinkshere'] = [
1313 'href' => SpecialPage::getTitleFor( 'Whatlinkshere', $this->thispage )->getLocalURL()
1314 ];
1315
1316 $nav_urls['info'] = [
1317 'text' => $this->msg( 'pageinfo-toolboxlink' )->text(),
1318 'href' => $this->getTitle()->getLocalURL( "action=info" )
1319 ];
1320
1321 if ( $this->getTitle()->exists() || $this->getTitle()->inNamespace( NS_CATEGORY ) ) {
1322 $nav_urls['recentchangeslinked'] = [
1323 'href' => SpecialPage::getTitleFor( 'Recentchangeslinked', $this->thispage )->getLocalURL()
1324 ];
1325 }
1326 }
1327
1328 $user = $this->getRelevantUser();
1329 if ( $user ) {
1330 $rootUser = $user->getName();
1331
1332 $nav_urls['contributions'] = [
1333 'text' => $this->msg( 'contributions', $rootUser )->text(),
1334 'href' => self::makeSpecialUrlSubpage( 'Contributions', $rootUser ),
1335 'tooltip-params' => [ $rootUser ],
1336 ];
1337
1338 $nav_urls['log'] = [
1339 'href' => self::makeSpecialUrlSubpage( 'Log', $rootUser )
1340 ];
1341
1342 if ( $this->getUser()->isAllowed( 'block' ) ) {
1343 $nav_urls['blockip'] = [
1344 'text' => $this->msg( 'blockip', $rootUser )->text(),
1345 'href' => self::makeSpecialUrlSubpage( 'Block', $rootUser )
1346 ];
1347 }
1348
1349 if ( $this->showEmailUser( $user ) ) {
1350 $nav_urls['emailuser'] = [
1351 'text' => $this->msg( 'tool-link-emailuser', $rootUser )->text(),
1352 'href' => self::makeSpecialUrlSubpage( 'Emailuser', $rootUser ),
1353 'tooltip-params' => [ $rootUser ],
1354 ];
1355 }
1356
1357 if ( !$user->isAnon() ) {
1358 if ( $this->getUser()->isRegistered() && $this->getConfig()->get( 'EnableSpecialMute' ) ) {
1359 $nav_urls['mute'] = [
1360 'text' => $this->msg( 'mute-preferences' )->text(),
1361 'href' => self::makeSpecialUrlSubpage( 'Mute', $rootUser )
1362 ];
1363 }
1364
1365 $sur = new UserrightsPage;
1366 $sur->setContext( $this->getContext() );
1367 $canChange = $sur->userCanChangeRights( $user );
1368 $nav_urls['userrights'] = [
1369 'text' => $this->msg(
1370 $canChange ? 'tool-link-userrights' : 'tool-link-userrights-readonly',
1371 $rootUser
1372 )->text(),
1373 'href' => self::makeSpecialUrlSubpage( 'Userrights', $rootUser )
1374 ];
1375 }
1376 }
1377
1378 return $nav_urls;
1379 }
1380
1381 /**
1382 * Generate strings used for xml 'id' names
1383 * @return string
1384 */
1385 protected function getNameSpaceKey() {
1386 return $this->getTitle()->getNamespaceKey();
1387 }
1388 }