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