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