5d6197e7ac70d05882a6f7fafb1facb94a552937
[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, $wgJsMimeType,
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( 'jsmimetype', $wgJsMimeType );
310 $tpl->set( 'charset', 'UTF-8' );
311 $tpl->set( 'wgScript', $wgScript );
312 $tpl->set( 'skinname', $this->skinname );
313 $tpl->set( 'skinclass', static::class );
314 $tpl->set( 'skin', $this );
315 $tpl->set( 'stylename', $this->stylename );
316 $tpl->set( 'printable', $out->isPrintable() );
317 $tpl->set( 'handheld', $request->getBool( 'handheld' ) );
318 $tpl->set( 'loggedin', $this->loggedin );
319 $tpl->set( 'notspecialpage', !$title->isSpecialPage() );
320 $tpl->set( 'searchaction', $this->getSearchLink() );
321 $tpl->set( 'searchtitle', SpecialPage::getTitleFor( 'Search' )->getPrefixedDBkey() );
322 $tpl->set( 'search', trim( $request->getVal( 'search' ) ) );
323 $tpl->set( 'stylepath', $wgStylePath );
324 $tpl->set( 'articlepath', $wgArticlePath );
325 $tpl->set( 'scriptpath', $wgScriptPath );
326 $tpl->set( 'serverurl', $wgServer );
327 $tpl->set( 'logopath', $wgLogo );
328 $tpl->set( 'sitename', $wgSitename );
329
330 $userLang = $this->getLanguage();
331 $userLangCode = $userLang->getHtmlCode();
332 $userLangDir = $userLang->getDir();
333
334 $tpl->set( 'lang', $userLangCode );
335 $tpl->set( 'dir', $userLangDir );
336 $tpl->set( 'rtl', $userLang->isRTL() );
337
338 $tpl->set( 'capitalizeallnouns', $userLang->capitalizeAllNouns() ? ' capitalize-all-nouns' : '' );
339 $tpl->set( 'showjumplinks', true ); // showjumplinks preference has been removed
340 $tpl->set( 'username', $this->loggedin ? $this->username : null );
341 $tpl->set( 'userpage', $this->userpage );
342 $tpl->set( 'userpageurl', $this->userpageUrlDetails['href'] );
343 $tpl->set( 'userlang', $userLangCode );
344
345 // Users can have their language set differently than the
346 // content of the wiki. For these users, tell the web browser
347 // that interface elements are in a different language.
348 $tpl->set( 'userlangattributes', '' );
349 $tpl->set( 'specialpageattributes', '' ); # obsolete
350 // Used by VectorBeta to insert HTML before content but after the
351 // heading for the page title. Defaults to empty string.
352 $tpl->set( 'prebodyhtml', '' );
353
354 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
355 if (
356 $userLangCode !== $contLang->getHtmlCode() ||
357 $userLangDir !== $contLang->getDir()
358 ) {
359 $escUserlang = htmlspecialchars( $userLangCode );
360 $escUserdir = htmlspecialchars( $userLangDir );
361 // Attributes must be in double quotes because htmlspecialchars() doesn't
362 // escape single quotes
363 $attrs = " lang=\"$escUserlang\" dir=\"$escUserdir\"";
364 $tpl->set( 'userlangattributes', $attrs );
365 }
366
367 $tpl->set( 'newtalk', $this->getNewtalks() );
368 $tpl->set( 'logo', $this->logoText() );
369
370 $tpl->set( 'copyright', false );
371 // No longer used
372 $tpl->set( 'viewcount', false );
373 $tpl->set( 'lastmod', false );
374 $tpl->set( 'credits', false );
375 $tpl->set( 'numberofwatchingusers', false );
376 if ( $title->exists() ) {
377 if ( $out->isArticle() && $this->isRevisionCurrent() ) {
378 if ( $wgMaxCredits != 0 ) {
379 /** @var CreditsAction $action */
380 $action = Action::factory(
381 'credits', $this->getWikiPage(), $this->getContext() );
382 $tpl->set( 'credits',
383 $action->getCredits( $wgMaxCredits, $wgShowCreditsIfMax ) );
384 } else {
385 $tpl->set( 'lastmod', $this->lastModified() );
386 }
387 }
388 if ( $out->showsCopyright() ) {
389 $tpl->set( 'copyright', $this->getCopyright() );
390 }
391 }
392
393 $tpl->set( 'copyrightico', $this->getCopyrightIcon() );
394 $tpl->set( 'poweredbyico', $this->getPoweredBy() );
395 $tpl->set( 'disclaimer', $this->disclaimerLink() );
396 $tpl->set( 'privacy', $this->privacyLink() );
397 $tpl->set( 'about', $this->aboutLink() );
398
399 $tpl->set( 'footerlinks', [
400 'info' => [
401 'lastmod',
402 'numberofwatchingusers',
403 'credits',
404 'copyright',
405 ],
406 'places' => [
407 'privacy',
408 'about',
409 'disclaimer',
410 ],
411 ] );
412
413 global $wgFooterIcons;
414 $tpl->set( 'footericons', $wgFooterIcons );
415 foreach ( $tpl->data['footericons'] as $footerIconsKey => &$footerIconsBlock ) {
416 if ( count( $footerIconsBlock ) > 0 ) {
417 foreach ( $footerIconsBlock as &$footerIcon ) {
418 if ( isset( $footerIcon['src'] ) ) {
419 if ( !isset( $footerIcon['width'] ) ) {
420 $footerIcon['width'] = 88;
421 }
422 if ( !isset( $footerIcon['height'] ) ) {
423 $footerIcon['height'] = 31;
424 }
425 }
426 }
427 } else {
428 unset( $tpl->data['footericons'][$footerIconsKey] );
429 }
430 }
431
432 $tpl->set( 'indicators', $out->getIndicators() );
433
434 $tpl->set( 'sitenotice', $this->getSiteNotice() );
435 $tpl->set( 'printfooter', $this->printSource() );
436 // Wrap the bodyText with #mw-content-text element
437 $out->mBodytext = $this->wrapHTML( $title, $out->mBodytext );
438 $tpl->set( 'bodytext', $out->mBodytext );
439
440 $language_urls = $this->getLanguages();
441 if ( count( $language_urls ) ) {
442 $tpl->set( 'language_urls', $language_urls );
443 } else {
444 $tpl->set( 'language_urls', false );
445 }
446
447 # Personal toolbar
448 $tpl->set( 'personal_urls', $this->buildPersonalUrls() );
449 $content_navigation = $this->buildContentNavigationUrls();
450 $content_actions = $this->buildContentActionUrls( $content_navigation );
451 $tpl->set( 'content_navigation', $content_navigation );
452 $tpl->set( 'content_actions', $content_actions );
453
454 $tpl->set( 'sidebar', $this->buildSidebar() );
455 $tpl->set( 'nav_urls', $this->buildNavUrls() );
456
457 // Do this last in case hooks above add bottom scripts
458 $tpl->set( 'bottomscripts', $this->bottomScripts() );
459
460 // Set the head scripts near the end, in case the above actions resulted in added scripts
461 $tpl->set( 'headelement', $out->headElement( $this ) );
462
463 $tpl->set( 'debug', '' );
464 $tpl->set( 'debughtml', $this->generateDebugHTML() );
465 $tpl->set( 'reporttime', wfReportTime( $out->getCSPNonce() ) );
466
467 // Avoid PHP 7.1 warning of passing $this by reference
468 $skinTemplate = $this;
469 // original version by hansm
470 if ( !Hooks::run( 'SkinTemplateOutputPageBeforeExec', [ &$skinTemplate, &$tpl ] ) ) {
471 wfDebug( __METHOD__ . ": Hook SkinTemplateOutputPageBeforeExec broke outputPage execution!\n" );
472 }
473
474 // Set the bodytext to another key so that skins can just output it on its own
475 // and output printfooter and debughtml separately
476 $tpl->set( 'bodycontent', $tpl->data['bodytext'] );
477
478 // Append printfooter and debughtml onto bodytext so that skins that
479 // were already using bodytext before they were split out don't suddenly
480 // start not outputting information.
481 $tpl->data['bodytext'] .= Html::rawElement(
482 'div',
483 [ 'class' => 'printfooter' ],
484 "\n{$tpl->data['printfooter']}"
485 ) . "\n";
486 $tpl->data['bodytext'] .= $tpl->data['debughtml'];
487
488 // allow extensions adding stuff after the page content.
489 // See Skin::afterContentHook() for further documentation.
490 $tpl->set( 'dataAfterContent', $this->afterContentHook() );
491
492 return $tpl;
493 }
494
495 /**
496 * Get the HTML for the p-personal list
497 * @return string
498 */
499 public function getPersonalToolsList() {
500 return $this->makePersonalToolsList();
501 }
502
503 /**
504 * Get the HTML for the personal tools list
505 *
506 * @since 1.31
507 *
508 * @param array|null $personalTools
509 * @param array $options
510 * @return string
511 */
512 public function makePersonalToolsList( $personalTools = null, $options = [] ) {
513 $tpl = $this->setupTemplateForOutput();
514 $tpl->set( 'personal_urls', $this->buildPersonalUrls() );
515 $html = '';
516
517 if ( $personalTools === null ) {
518 $personalTools = ( $tpl instanceof BaseTemplate )
519 ? $tpl->getPersonalTools()
520 : [];
521 }
522
523 foreach ( $personalTools as $key => $item ) {
524 $html .= $tpl->makeListItem( $key, $item, $options );
525 }
526
527 return $html;
528 }
529
530 /**
531 * Get personal tools for the user
532 *
533 * @since 1.31
534 *
535 * @return array Array of personal tools
536 */
537 public function getStructuredPersonalTools() {
538 $tpl = $this->setupTemplateForOutput();
539 $tpl->set( 'personal_urls', $this->buildPersonalUrls() );
540
541 return ( $tpl instanceof BaseTemplate ) ? $tpl->getPersonalTools() : [];
542 }
543
544 /**
545 * Format language name for use in sidebar interlanguage links list.
546 * By default it is capitalized.
547 *
548 * @param string $name Language name, e.g. "English" or "español"
549 * @return string
550 * @private
551 */
552 function formatLanguageName( $name ) {
553 return $this->getLanguage()->ucfirst( $name );
554 }
555
556 /**
557 * Output the string, or print error message if it's
558 * an error object of the appropriate type.
559 * For the base class, assume strings all around.
560 *
561 * @param string $str
562 * @private
563 */
564 function printOrError( $str ) {
565 echo $str;
566 }
567
568 /**
569 * Output a boolean indicating if buildPersonalUrls should output separate
570 * login and create account links or output a combined link
571 * By default we simply return a global config setting that affects most skins
572 * This is setup as a method so that like with $wgLogo and getLogo() a skin
573 * can override this setting and always output one or the other if it has
574 * a reason it can't output one of the two modes.
575 * @return bool
576 */
577 function useCombinedLoginLink() {
578 global $wgUseCombinedLoginLink;
579 return $wgUseCombinedLoginLink;
580 }
581
582 /**
583 * build array of urls for personal toolbar
584 * @return array
585 */
586 protected function buildPersonalUrls() {
587 $title = $this->getTitle();
588 $request = $this->getRequest();
589 $pageurl = $title->getLocalURL();
590 $authManager = AuthManager::singleton();
591
592 /* set up the default links for the personal toolbar */
593 $personal_urls = [];
594
595 # Due to T34276, if a user does not have read permissions,
596 # $this->getTitle() will just give Special:Badtitle, which is
597 # not especially useful as a returnto parameter. Use the title
598 # from the request instead, if there was one.
599 if ( $this->getUser()->isAllowed( 'read' ) ) {
600 $page = $this->getTitle();
601 } else {
602 $page = Title::newFromText( $request->getVal( 'title', '' ) );
603 }
604 $page = $request->getVal( 'returnto', $page );
605 $returnto = [];
606 if ( strval( $page ) !== '' ) {
607 $returnto['returnto'] = $page;
608 $query = $request->getVal( 'returntoquery', $this->thisquery );
609 $paramsArray = wfCgiToArray( $query );
610 $query = wfArrayToCgi( $paramsArray );
611 if ( $query != '' ) {
612 $returnto['returntoquery'] = $query;
613 }
614 }
615
616 if ( $this->loggedin ) {
617 $personal_urls['userpage'] = [
618 'text' => $this->username,
619 'href' => &$this->userpageUrlDetails['href'],
620 'class' => $this->userpageUrlDetails['exists'] ? false : 'new',
621 'exists' => $this->userpageUrlDetails['exists'],
622 'active' => ( $this->userpageUrlDetails['href'] == $pageurl ),
623 'dir' => 'auto'
624 ];
625 $usertalkUrlDetails = $this->makeTalkUrlDetails( $this->userpage );
626 $personal_urls['mytalk'] = [
627 'text' => $this->msg( 'mytalk' )->text(),
628 'href' => &$usertalkUrlDetails['href'],
629 'class' => $usertalkUrlDetails['exists'] ? false : 'new',
630 'exists' => $usertalkUrlDetails['exists'],
631 'active' => ( $usertalkUrlDetails['href'] == $pageurl )
632 ];
633 $href = self::makeSpecialUrl( 'Preferences' );
634 $personal_urls['preferences'] = [
635 'text' => $this->msg( 'mypreferences' )->text(),
636 'href' => $href,
637 'active' => ( $href == $pageurl )
638 ];
639
640 if ( $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
641 $href = self::makeSpecialUrl( 'Watchlist' );
642 $personal_urls['watchlist'] = [
643 'text' => $this->msg( 'mywatchlist' )->text(),
644 'href' => $href,
645 'active' => ( $href == $pageurl )
646 ];
647 }
648
649 # We need to do an explicit check for Special:Contributions, as we
650 # have to match both the title, and the target, which could come
651 # from request values (Special:Contributions?target=Jimbo_Wales)
652 # or be specified in "sub page" form
653 # (Special:Contributions/Jimbo_Wales). The plot
654 # thickens, because the Title object is altered for special pages,
655 # so it doesn't contain the original alias-with-subpage.
656 $origTitle = Title::newFromText( $request->getText( 'title' ) );
657 if ( $origTitle instanceof Title && $origTitle->isSpecialPage() ) {
658 list( $spName, $spPar ) =
659 MediaWikiServices::getInstance()->getSpecialPageFactory()->
660 resolveAlias( $origTitle->getText() );
661 $active = $spName == 'Contributions'
662 && ( ( $spPar && $spPar == $this->username )
663 || $request->getText( 'target' ) == $this->username );
664 } else {
665 $active = false;
666 }
667
668 $href = self::makeSpecialUrlSubpage( 'Contributions', $this->username );
669 $personal_urls['mycontris'] = [
670 'text' => $this->msg( 'mycontris' )->text(),
671 'href' => $href,
672 'active' => $active
673 ];
674
675 // if we can't set the user, we can't unset it either
676 if ( $request->getSession()->canSetUser() ) {
677 $personal_urls['logout'] = [
678 'text' => $this->msg( 'pt-userlogout' )->text(),
679 'href' => self::makeSpecialUrl( 'Userlogout',
680 // Note: userlogout link must always contain an & character, otherwise we might not be able
681 // to detect a buggy precaching proxy (T19790)
682 ( $title->isSpecial( 'Preferences' ) ? [] : $returnto ) ),
683 'active' => false
684 ];
685 }
686 } else {
687 $useCombinedLoginLink = $this->useCombinedLoginLink();
688 if ( !$authManager->canCreateAccounts() || !$authManager->canAuthenticateNow() ) {
689 // don't show combined login/signup link if one of those is actually not available
690 $useCombinedLoginLink = false;
691 }
692
693 $loginlink = $this->getUser()->isAllowed( 'createaccount' ) && $useCombinedLoginLink
694 ? 'nav-login-createaccount'
695 : 'pt-login';
696
697 $login_url = [
698 'text' => $this->msg( $loginlink )->text(),
699 'href' => self::makeSpecialUrl( 'Userlogin', $returnto ),
700 'active' => $title->isSpecial( 'Userlogin' )
701 || $title->isSpecial( 'CreateAccount' ) && $useCombinedLoginLink,
702 ];
703 $createaccount_url = [
704 'text' => $this->msg( 'pt-createaccount' )->text(),
705 'href' => self::makeSpecialUrl( 'CreateAccount', $returnto ),
706 'active' => $title->isSpecial( 'CreateAccount' ),
707 ];
708
709 // No need to show Talk and Contributions to anons if they can't contribute!
710 if ( User::groupHasPermission( '*', 'edit' ) ) {
711 // Because of caching, we can't link directly to the IP talk and
712 // contributions pages. Instead we use the special page shortcuts
713 // (which work correctly regardless of caching). This means we can't
714 // determine whether these links are active or not, but since major
715 // skins (MonoBook, Vector) don't use this information, it's not a
716 // huge loss.
717 $personal_urls['anontalk'] = [
718 'text' => $this->msg( 'anontalk' )->text(),
719 'href' => self::makeSpecialUrlSubpage( 'Mytalk', false ),
720 'active' => false
721 ];
722 $personal_urls['anoncontribs'] = [
723 'text' => $this->msg( 'anoncontribs' )->text(),
724 'href' => self::makeSpecialUrlSubpage( 'Mycontributions', false ),
725 'active' => false
726 ];
727 }
728
729 if (
730 $authManager->canCreateAccounts()
731 && $this->getUser()->isAllowed( 'createaccount' )
732 && !$useCombinedLoginLink
733 ) {
734 $personal_urls['createaccount'] = $createaccount_url;
735 }
736
737 if ( $authManager->canAuthenticateNow() ) {
738 $key = User::groupHasPermission( '*', 'read' )
739 ? 'login'
740 : 'login-private';
741 $personal_urls[$key] = $login_url;
742 }
743 }
744
745 Hooks::runWithoutAbort( 'PersonalUrls', [ &$personal_urls, &$title, $this ] );
746 return $personal_urls;
747 }
748
749 /**
750 * Builds an array with tab definition
751 *
752 * @param Title $title Page Where the tab links to
753 * @param string|array $message Message key or an array of message keys (will fall back)
754 * @param bool $selected Display the tab as selected
755 * @param string $query Query string attached to tab URL
756 * @param bool $checkEdit Check if $title exists and mark with .new if one doesn't
757 *
758 * @return array
759 */
760 function tabAction( $title, $message, $selected, $query = '', $checkEdit = false ) {
761 $classes = [];
762 if ( $selected ) {
763 $classes[] = 'selected';
764 }
765 $exists = true;
766 if ( $checkEdit && !$title->isKnown() ) {
767 $classes[] = 'new';
768 $exists = false;
769 if ( $query !== '' ) {
770 $query = 'action=edit&redlink=1&' . $query;
771 } else {
772 $query = 'action=edit&redlink=1';
773 }
774 }
775
776 $services = MediaWikiServices::getInstance();
777 $linkClass = $services->getLinkRenderer()->getLinkClasses( $title );
778
779 // wfMessageFallback will nicely accept $message as an array of fallbacks
780 // or just a single key
781 $msg = wfMessageFallback( $message )->setContext( $this->getContext() );
782 if ( is_array( $message ) ) {
783 // for hook compatibility just keep the last message name
784 $message = end( $message );
785 }
786 if ( $msg->exists() ) {
787 $text = $msg->text();
788 } else {
789 $text = $services->getContentLanguage()->getConverter()->
790 convertNamespace( $services->getNamespaceInfo()->
791 getSubject( $title->getNamespace() ) );
792 }
793
794 // Avoid PHP 7.1 warning of passing $this by reference
795 $skinTemplate = $this;
796 $result = [];
797 if ( !Hooks::run( 'SkinTemplateTabAction', [ &$skinTemplate,
798 $title, $message, $selected, $checkEdit,
799 &$classes, &$query, &$text, &$result ] ) ) {
800 return $result;
801 }
802
803 $result = [
804 'class' => implode( ' ', $classes ),
805 'text' => $text,
806 'href' => $title->getLocalURL( $query ),
807 'exists' => $exists,
808 'primary' => true ];
809 if ( $linkClass !== '' ) {
810 $result['link-class'] = $linkClass;
811 }
812
813 return $result;
814 }
815
816 function makeTalkUrlDetails( $name, $urlaction = '' ) {
817 $title = Title::newFromText( $name );
818 if ( !is_object( $title ) ) {
819 throw new MWException( __METHOD__ . " given invalid pagename $name" );
820 }
821 $title = $title->getTalkPage();
822 self::checkTitle( $title, $name );
823 return [
824 'href' => $title->getLocalURL( $urlaction ),
825 'exists' => $title->isKnown(),
826 ];
827 }
828
829 /**
830 * @todo is this even used?
831 * @param string $name
832 * @param string $urlaction
833 * @return array
834 */
835 function makeArticleUrlDetails( $name, $urlaction = '' ) {
836 $title = Title::newFromText( $name );
837 $title = $title->getSubjectPage();
838 self::checkTitle( $title, $name );
839 return [
840 'href' => $title->getLocalURL( $urlaction ),
841 'exists' => $title->exists(),
842 ];
843 }
844
845 /**
846 * a structured array of links usually used for the tabs in a skin
847 *
848 * There are 4 standard sections
849 * namespaces: Used for namespace tabs like special, page, and talk namespaces
850 * views: Used for primary page views like read, edit, history
851 * actions: Used for most extra page actions like deletion, protection, etc...
852 * variants: Used to list the language variants for the page
853 *
854 * Each section's value is a key/value array of links for that section.
855 * The links themselves have these common keys:
856 * - class: The css classes to apply to the tab
857 * - text: The text to display on the tab
858 * - href: The href for the tab to point to
859 * - rel: An optional rel= for the tab's link
860 * - redundant: If true the tab will be dropped in skins using content_actions
861 * this is useful for tabs like "Read" which only have meaning in skins that
862 * take special meaning from the grouped structure of content_navigation
863 *
864 * Views also have an extra key which can be used:
865 * - primary: If this is not true skins like vector may try to hide the tab
866 * when the user has limited space in their browser window
867 *
868 * content_navigation using code also expects these ids to be present on the
869 * links, however these are usually automatically generated by SkinTemplate
870 * itself and are not necessary when using a hook. The only things these may
871 * matter to are people modifying content_navigation after it's initial creation:
872 * - id: A "preferred" id, most skins are best off outputting this preferred
873 * id for best compatibility.
874 * - tooltiponly: This is set to true for some tabs in cases where the system
875 * believes that the accesskey should not be added to the tab.
876 *
877 * @return array
878 */
879 protected function buildContentNavigationUrls() {
880 global $wgDisableLangConversion;
881
882 // Display tabs for the relevant title rather than always the title itself
883 $title = $this->getRelevantTitle();
884 $onPage = $title->equals( $this->getTitle() );
885
886 $out = $this->getOutput();
887 $request = $this->getRequest();
888 $user = $this->getUser();
889
890 $content_navigation = [
891 'namespaces' => [],
892 'views' => [],
893 'actions' => [],
894 'variants' => []
895 ];
896
897 // parameters
898 $action = $request->getVal( 'action', 'view' );
899
900 $userCanRead = $title->quickUserCan( 'read', $user );
901
902 // Avoid PHP 7.1 warning of passing $this by reference
903 $skinTemplate = $this;
904 $preventActiveTabs = false;
905 Hooks::run( 'SkinTemplatePreventOtherActiveTabs', [ &$skinTemplate, &$preventActiveTabs ] );
906
907 // Checks if page is some kind of content
908 if ( $title->canExist() ) {
909 // Gets page objects for the related namespaces
910 $subjectPage = $title->getSubjectPage();
911 $talkPage = $title->getTalkPage();
912
913 // Determines if this is a talk page
914 $isTalk = $title->isTalkPage();
915
916 // Generates XML IDs from namespace names
917 $subjectId = $title->getNamespaceKey( '' );
918
919 if ( $subjectId == 'main' ) {
920 $talkId = 'talk';
921 } else {
922 $talkId = "{$subjectId}_talk";
923 }
924
925 $skname = $this->skinname;
926
927 // Adds namespace links
928 $subjectMsg = [ "nstab-$subjectId" ];
929 if ( $subjectPage->isMainPage() ) {
930 array_unshift( $subjectMsg, 'mainpage-nstab' );
931 }
932 $content_navigation['namespaces'][$subjectId] = $this->tabAction(
933 $subjectPage, $subjectMsg, !$isTalk && !$preventActiveTabs, '', $userCanRead
934 );
935 $content_navigation['namespaces'][$subjectId]['context'] = 'subject';
936 $content_navigation['namespaces'][$talkId] = $this->tabAction(
937 $talkPage, [ "nstab-$talkId", 'talk' ], $isTalk && !$preventActiveTabs, '', $userCanRead
938 );
939 $content_navigation['namespaces'][$talkId]['context'] = 'talk';
940
941 if ( $userCanRead ) {
942 // Adds "view" view link
943 if ( $title->isKnown() ) {
944 $content_navigation['views']['view'] = $this->tabAction(
945 $isTalk ? $talkPage : $subjectPage,
946 [ "$skname-view-view", 'view' ],
947 ( $onPage && ( $action == 'view' || $action == 'purge' ) ), '', true
948 );
949 // signal to hide this from simple content_actions
950 $content_navigation['views']['view']['redundant'] = true;
951 }
952
953 $page = $this->canUseWikiPage() ? $this->getWikiPage() : false;
954 $isRemoteContent = $page && !$page->isLocal();
955
956 // If it is a non-local file, show a link to the file in its own repository
957 // @todo abstract this for remote content that isn't a file
958 if ( $isRemoteContent ) {
959 $content_navigation['views']['view-foreign'] = [
960 'class' => '',
961 'text' => wfMessageFallback( "$skname-view-foreign", 'view-foreign' )->
962 setContext( $this->getContext() )->
963 params( $page->getWikiDisplayName() )->text(),
964 'href' => $page->getSourceURL(),
965 'primary' => false,
966 ];
967 }
968
969 // Checks if user can edit the current page if it exists or create it otherwise
970 if ( $title->quickUserCan( 'edit', $user )
971 && ( $title->exists() || $title->quickUserCan( 'create', $user ) )
972 ) {
973 // Builds CSS class for talk page links
974 $isTalkClass = $isTalk ? ' istalk' : '';
975 // Whether the user is editing the page
976 $isEditing = $onPage && ( $action == 'edit' || $action == 'submit' );
977 // Whether to show the "Add a new section" tab
978 // Checks if this is a current rev of talk page and is not forced to be hidden
979 $showNewSection = !$out->forceHideNewSectionLink()
980 && ( ( $isTalk && $this->isRevisionCurrent() ) || $out->showNewSectionLink() );
981 $section = $request->getVal( 'section' );
982
983 if ( $title->exists()
984 || ( $title->getNamespace() == NS_MEDIAWIKI
985 && $title->getDefaultMessageText() !== false
986 )
987 ) {
988 $msgKey = $isRemoteContent ? 'edit-local' : 'edit';
989 } else {
990 $msgKey = $isRemoteContent ? 'create-local' : 'create';
991 }
992 $content_navigation['views']['edit'] = [
993 'class' => ( $isEditing && ( $section !== 'new' || !$showNewSection )
994 ? 'selected'
995 : ''
996 ) . $isTalkClass,
997 'text' => wfMessageFallback( "$skname-view-$msgKey", $msgKey )
998 ->setContext( $this->getContext() )->text(),
999 'href' => $title->getLocalURL( $this->editUrlOptions() ),
1000 'primary' => !$isRemoteContent, // don't collapse this in vector
1001 ];
1002
1003 // section link
1004 if ( $showNewSection ) {
1005 // Adds new section link
1006 // $content_navigation['actions']['addsection']
1007 $content_navigation['views']['addsection'] = [
1008 'class' => ( $isEditing && $section == 'new' ) ? 'selected' : false,
1009 'text' => wfMessageFallback( "$skname-action-addsection", 'addsection' )
1010 ->setContext( $this->getContext() )->text(),
1011 'href' => $title->getLocalURL( 'action=edit&section=new' )
1012 ];
1013 }
1014 // Checks if the page has some kind of viewable source content
1015 } elseif ( $title->hasSourceText() ) {
1016 // Adds view source view link
1017 $content_navigation['views']['viewsource'] = [
1018 'class' => ( $onPage && $action == 'edit' ) ? 'selected' : false,
1019 'text' => wfMessageFallback( "$skname-action-viewsource", 'viewsource' )
1020 ->setContext( $this->getContext() )->text(),
1021 'href' => $title->getLocalURL( $this->editUrlOptions() ),
1022 'primary' => true, // don't collapse this in vector
1023 ];
1024 }
1025
1026 // Checks if the page exists
1027 if ( $title->exists() ) {
1028 // Adds history view link
1029 $content_navigation['views']['history'] = [
1030 'class' => ( $onPage && $action == 'history' ) ? 'selected' : false,
1031 'text' => wfMessageFallback( "$skname-view-history", 'history_short' )
1032 ->setContext( $this->getContext() )->text(),
1033 'href' => $title->getLocalURL( 'action=history' ),
1034 ];
1035
1036 if ( $title->quickUserCan( 'delete', $user ) ) {
1037 $content_navigation['actions']['delete'] = [
1038 'class' => ( $onPage && $action == 'delete' ) ? 'selected' : false,
1039 'text' => wfMessageFallback( "$skname-action-delete", 'delete' )
1040 ->setContext( $this->getContext() )->text(),
1041 'href' => $title->getLocalURL( 'action=delete' )
1042 ];
1043 }
1044
1045 if ( $title->quickUserCan( 'move', $user ) ) {
1046 $moveTitle = SpecialPage::getTitleFor( 'Movepage', $title->getPrefixedDBkey() );
1047 $content_navigation['actions']['move'] = [
1048 'class' => $this->getTitle()->isSpecial( 'Movepage' ) ? 'selected' : false,
1049 'text' => wfMessageFallback( "$skname-action-move", 'move' )
1050 ->setContext( $this->getContext() )->text(),
1051 'href' => $moveTitle->getLocalURL()
1052 ];
1053 }
1054 } else {
1055 // article doesn't exist or is deleted
1056 if ( $title->quickUserCan( 'deletedhistory', $user ) ) {
1057 $n = $title->isDeleted();
1058 if ( $n ) {
1059 $undelTitle = SpecialPage::getTitleFor( 'Undelete', $title->getPrefixedDBkey() );
1060 // If the user can't undelete but can view deleted
1061 // history show them a "View .. deleted" tab instead.
1062 $msgKey = $title->quickUserCan( 'undelete', $user ) ? 'undelete' : 'viewdeleted';
1063 $content_navigation['actions']['undelete'] = [
1064 'class' => $this->getTitle()->isSpecial( 'Undelete' ) ? 'selected' : false,
1065 'text' => wfMessageFallback( "$skname-action-$msgKey", "{$msgKey}_short" )
1066 ->setContext( $this->getContext() )->numParams( $n )->text(),
1067 'href' => $undelTitle->getLocalURL()
1068 ];
1069 }
1070 }
1071 }
1072
1073 if ( $title->quickUserCan( 'protect', $user ) && $title->getRestrictionTypes() &&
1074 MediaWikiServices::getInstance()->getNamespaceInfo()->
1075 getRestrictionLevels( $title->getNamespace(), $user ) !== [ '' ]
1076 ) {
1077 $mode = $title->isProtected() ? 'unprotect' : 'protect';
1078 $content_navigation['actions'][$mode] = [
1079 'class' => ( $onPage && $action == $mode ) ? 'selected' : false,
1080 'text' => wfMessageFallback( "$skname-action-$mode", $mode )
1081 ->setContext( $this->getContext() )->text(),
1082 'href' => $title->getLocalURL( "action=$mode" )
1083 ];
1084 }
1085
1086 // Checks if the user is logged in
1087 if ( $this->loggedin && $user->isAllowedAll( 'viewmywatchlist', 'editmywatchlist' ) ) {
1088 /**
1089 * The following actions use messages which, if made particular to
1090 * the any specific skins, would break the Ajax code which makes this
1091 * action happen entirely inline. OutputPage::getJSVars
1092 * defines a set of messages in a javascript object - and these
1093 * messages are assumed to be global for all skins. Without making
1094 * a change to that procedure these messages will have to remain as
1095 * the global versions.
1096 */
1097 $mode = $user->isWatched( $title ) ? 'unwatch' : 'watch';
1098 $content_navigation['actions'][$mode] = [
1099 'class' => 'mw-watchlink ' . (
1100 $onPage && ( $action == 'watch' || $action == 'unwatch' ) ? 'selected' : ''
1101 ),
1102 // uses 'watch' or 'unwatch' message
1103 'text' => $this->msg( $mode )->text(),
1104 'href' => $title->getLocalURL( [ 'action' => $mode ] ),
1105 // Set a data-mw=interface attribute, which the mediawiki.page.ajax
1106 // module will look for to make sure it's a trusted link
1107 'data' => [
1108 'mw' => 'interface',
1109 ],
1110 ];
1111 }
1112 }
1113
1114 // Avoid PHP 7.1 warning of passing $this by reference
1115 $skinTemplate = $this;
1116 Hooks::runWithoutAbort(
1117 'SkinTemplateNavigation',
1118 [ &$skinTemplate, &$content_navigation ]
1119 );
1120
1121 if ( $userCanRead && !$wgDisableLangConversion ) {
1122 $pageLang = $title->getPageLanguage();
1123 // Checks that language conversion is enabled and variants exist
1124 // And if it is not in the special namespace
1125 if ( $pageLang->hasVariants() ) {
1126 // Gets list of language variants
1127 $variants = $pageLang->getVariants();
1128 // Gets preferred variant (note that user preference is
1129 // only possible for wiki content language variant)
1130 $preferred = $pageLang->getPreferredVariant();
1131 if ( Action::getActionName( $this ) === 'view' ) {
1132 $params = $request->getQueryValues();
1133 unset( $params['title'] );
1134 } else {
1135 $params = [];
1136 }
1137 // Loops over each variant
1138 foreach ( $variants as $code ) {
1139 // Gets variant name from language code
1140 $varname = $pageLang->getVariantname( $code );
1141 // Appends variant link
1142 $content_navigation['variants'][] = [
1143 'class' => ( $code == $preferred ) ? 'selected' : false,
1144 'text' => $varname,
1145 'href' => $title->getLocalURL( [ 'variant' => $code ] + $params ),
1146 'lang' => LanguageCode::bcp47( $code ),
1147 'hreflang' => LanguageCode::bcp47( $code ),
1148 ];
1149 }
1150 }
1151 }
1152 } else {
1153 // If it's not content, it's got to be a special page
1154 $content_navigation['namespaces']['special'] = [
1155 'class' => 'selected',
1156 'text' => $this->msg( 'nstab-special' )->text(),
1157 'href' => $request->getRequestURL(), // @see: T4457, T4510
1158 'context' => 'subject'
1159 ];
1160
1161 // Avoid PHP 7.1 warning of passing $this by reference
1162 $skinTemplate = $this;
1163 Hooks::runWithoutAbort( 'SkinTemplateNavigation::SpecialPage',
1164 [ &$skinTemplate, &$content_navigation ] );
1165 }
1166
1167 // Avoid PHP 7.1 warning of passing $this by reference
1168 $skinTemplate = $this;
1169 // Equiv to SkinTemplateContentActions
1170 Hooks::runWithoutAbort( 'SkinTemplateNavigation::Universal',
1171 [ &$skinTemplate, &$content_navigation ] );
1172
1173 // Setup xml ids and tooltip info
1174 foreach ( $content_navigation as $section => &$links ) {
1175 foreach ( $links as $key => &$link ) {
1176 $xmlID = $key;
1177 if ( isset( $link['context'] ) && $link['context'] == 'subject' ) {
1178 $xmlID = 'ca-nstab-' . $xmlID;
1179 } elseif ( isset( $link['context'] ) && $link['context'] == 'talk' ) {
1180 $xmlID = 'ca-talk';
1181 $link['rel'] = 'discussion';
1182 } elseif ( $section == 'variants' ) {
1183 $xmlID = 'ca-varlang-' . $xmlID;
1184 } else {
1185 $xmlID = 'ca-' . $xmlID;
1186 }
1187 $link['id'] = $xmlID;
1188 }
1189 }
1190
1191 # We don't want to give the watch tab an accesskey if the
1192 # page is being edited, because that conflicts with the
1193 # accesskey on the watch checkbox. We also don't want to
1194 # give the edit tab an accesskey, because that's fairly
1195 # superfluous and conflicts with an accesskey (Ctrl-E) often
1196 # used for editing in Safari.
1197 if ( in_array( $action, [ 'edit', 'submit' ] ) ) {
1198 if ( isset( $content_navigation['views']['edit'] ) ) {
1199 $content_navigation['views']['edit']['tooltiponly'] = true;
1200 }
1201 if ( isset( $content_navigation['actions']['watch'] ) ) {
1202 $content_navigation['actions']['watch']['tooltiponly'] = true;
1203 }
1204 if ( isset( $content_navigation['actions']['unwatch'] ) ) {
1205 $content_navigation['actions']['unwatch']['tooltiponly'] = true;
1206 }
1207 }
1208
1209 return $content_navigation;
1210 }
1211
1212 /**
1213 * an array of edit links by default used for the tabs
1214 * @param array $content_navigation
1215 * @return array
1216 */
1217 private function buildContentActionUrls( $content_navigation ) {
1218 // content_actions has been replaced with content_navigation for backwards
1219 // compatibility and also for skins that just want simple tabs content_actions
1220 // is now built by flattening the content_navigation arrays into one
1221
1222 $content_actions = [];
1223
1224 foreach ( $content_navigation as $links ) {
1225 foreach ( $links as $key => $value ) {
1226 if ( isset( $value['redundant'] ) && $value['redundant'] ) {
1227 // Redundant tabs are dropped from content_actions
1228 continue;
1229 }
1230
1231 // content_actions used to have ids built using the "ca-$key" pattern
1232 // so the xmlID based id is much closer to the actual $key that we want
1233 // for that reason we'll just strip out the ca- if present and use
1234 // the latter potion of the "id" as the $key
1235 if ( isset( $value['id'] ) && substr( $value['id'], 0, 3 ) == 'ca-' ) {
1236 $key = substr( $value['id'], 3 );
1237 }
1238
1239 if ( isset( $content_actions[$key] ) ) {
1240 wfDebug( __METHOD__ . ": Found a duplicate key for $key while flattening " .
1241 "content_navigation into content_actions.\n" );
1242 continue;
1243 }
1244
1245 $content_actions[$key] = $value;
1246 }
1247 }
1248
1249 return $content_actions;
1250 }
1251
1252 /**
1253 * build array of common navigation links
1254 * @return array
1255 */
1256 protected function buildNavUrls() {
1257 global $wgUploadNavigationUrl;
1258
1259 $out = $this->getOutput();
1260 $request = $this->getRequest();
1261
1262 $nav_urls = [];
1263 $nav_urls['mainpage'] = [ 'href' => self::makeMainPageUrl() ];
1264 if ( $wgUploadNavigationUrl ) {
1265 $nav_urls['upload'] = [ 'href' => $wgUploadNavigationUrl ];
1266 } elseif ( UploadBase::isEnabled() && UploadBase::isAllowed( $this->getUser() ) === true ) {
1267 $nav_urls['upload'] = [ 'href' => self::makeSpecialUrl( 'Upload' ) ];
1268 } else {
1269 $nav_urls['upload'] = false;
1270 }
1271 $nav_urls['specialpages'] = [ 'href' => self::makeSpecialUrl( 'Specialpages' ) ];
1272
1273 $nav_urls['print'] = false;
1274 $nav_urls['permalink'] = false;
1275 $nav_urls['info'] = false;
1276 $nav_urls['whatlinkshere'] = false;
1277 $nav_urls['recentchangeslinked'] = false;
1278 $nav_urls['contributions'] = false;
1279 $nav_urls['log'] = false;
1280 $nav_urls['blockip'] = 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 $sur = new UserrightsPage;
1359 $sur->setContext( $this->getContext() );
1360 $canChange = $sur->userCanChangeRights( $user );
1361 $nav_urls['userrights'] = [
1362 'text' => $this->msg(
1363 $canChange ? 'tool-link-userrights' : 'tool-link-userrights-readonly',
1364 $rootUser
1365 )->text(),
1366 'href' => self::makeSpecialUrlSubpage( 'Userrights', $rootUser )
1367 ];
1368 }
1369 }
1370
1371 return $nav_urls;
1372 }
1373
1374 /**
1375 * Generate strings used for xml 'id' names
1376 * @return string
1377 */
1378 protected function getNameSpaceKey() {
1379 return $this->getTitle()->getNamespaceKey();
1380 }
1381 }