Merge "Revert "Log the reason why revision->getContent() returns null""
[lhc/web/wiklou.git] / includes / skins / Skin.php
1 <?php
2 /**
3 * Base class for all skins.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 use MediaWiki\MediaWikiServices;
24
25 /**
26 * @defgroup Skins Skins
27 */
28
29 /**
30 * The main skin class which provides methods and properties for all other skins.
31 *
32 * See docs/skin.txt for more information.
33 *
34 * @ingroup Skins
35 */
36 abstract class Skin extends ContextSource {
37 /**
38 * @var string|null
39 */
40 protected $skinname = null;
41
42 protected $mRelevantTitle = null;
43 protected $mRelevantUser = null;
44
45 /**
46 * @var string Stylesheets set to use. Subdirectory in skins/ where various stylesheets are
47 * located. Only needs to be set if you intend to use the getSkinStylePath() method.
48 */
49 public $stylename = null;
50
51 /**
52 * Fetch the set of available skins.
53 * @return array Associative array of strings
54 */
55 static function getSkinNames() {
56 return SkinFactory::getDefaultInstance()->getSkinNames();
57 }
58
59 /**
60 * Fetch the skinname messages for available skins.
61 * @return string[]
62 */
63 static function getSkinNameMessages() {
64 $messages = [];
65 foreach ( self::getSkinNames() as $skinKey => $skinName ) {
66 $messages[] = "skinname-$skinKey";
67 }
68 return $messages;
69 }
70
71 /**
72 * Fetch the list of user-selectable skins in regards to $wgSkipSkins.
73 * Useful for Special:Preferences and other places where you
74 * only want to show skins users _can_ use.
75 * @return string[]
76 * @since 1.23
77 */
78 public static function getAllowedSkins() {
79 global $wgSkipSkins;
80
81 $allowedSkins = self::getSkinNames();
82
83 foreach ( $wgSkipSkins as $skip ) {
84 unset( $allowedSkins[$skip] );
85 }
86
87 return $allowedSkins;
88 }
89
90 /**
91 * Normalize a skin preference value to a form that can be loaded.
92 *
93 * If a skin can't be found, it will fall back to the configured default ($wgDefaultSkin), or the
94 * hardcoded default ($wgFallbackSkin) if the default skin is unavailable too.
95 *
96 * @param string $key 'monobook', 'vector', etc.
97 * @return string
98 */
99 static function normalizeKey( $key ) {
100 global $wgDefaultSkin, $wgFallbackSkin;
101
102 $skinNames = self::getSkinNames();
103
104 // Make keys lowercase for case-insensitive matching.
105 $skinNames = array_change_key_case( $skinNames, CASE_LOWER );
106 $key = strtolower( $key );
107 $defaultSkin = strtolower( $wgDefaultSkin );
108 $fallbackSkin = strtolower( $wgFallbackSkin );
109
110 if ( $key == '' || $key == 'default' ) {
111 // Don't return the default immediately;
112 // in a misconfiguration we need to fall back.
113 $key = $defaultSkin;
114 }
115
116 if ( isset( $skinNames[$key] ) ) {
117 return $key;
118 }
119
120 // Older versions of the software used a numeric setting
121 // in the user preferences.
122 $fallback = [
123 0 => $defaultSkin,
124 2 => 'cologneblue'
125 ];
126
127 if ( isset( $fallback[$key] ) ) {
128 $key = $fallback[$key];
129 }
130
131 if ( isset( $skinNames[$key] ) ) {
132 return $key;
133 } elseif ( isset( $skinNames[$defaultSkin] ) ) {
134 return $defaultSkin;
135 } else {
136 return $fallbackSkin;
137 }
138 }
139
140 /**
141 * @since 1.31
142 * @param string|null $skinname
143 */
144 public function __construct( $skinname = null ) {
145 if ( is_string( $skinname ) ) {
146 $this->skinname = $skinname;
147 }
148 }
149
150 /**
151 * @return string|null Skin name
152 */
153 public function getSkinName() {
154 return $this->skinname;
155 }
156
157 /**
158 * @param OutputPage $out
159 */
160 public function initPage( OutputPage $out ) {
161 $this->preloadExistence();
162 }
163
164 /**
165 * Defines the ResourceLoader modules that should be added to the skin
166 * It is recommended that skins wishing to override call parent::getDefaultModules()
167 * and substitute out any modules they wish to change by using a key to look them up
168 *
169 * For style modules, use setupSkinUserCss() instead.
170 *
171 * @return array Array of modules with helper keys for easy overriding
172 */
173 public function getDefaultModules() {
174 global $wgUseAjax, $wgEnableAPI, $wgEnableWriteAPI;
175
176 $out = $this->getOutput();
177 $config = $this->getConfig();
178 $user = $out->getUser();
179 $modules = [
180 // modules not specific to any specific skin or page
181 'core' => [
182 // Enforce various default modules for all pages and all skins
183 // Keep this list as small as possible
184 'site',
185 'mediawiki.page.startup',
186 'mediawiki.user',
187 ],
188 // modules that enhance the page content in some way
189 'content' => [
190 'mediawiki.page.ready',
191 ],
192 // modules relating to search functionality
193 'search' => [],
194 // modules relating to functionality relating to watching an article
195 'watch' => [],
196 // modules which relate to the current users preferences
197 'user' => [],
198 ];
199
200 // Support for high-density display images if enabled
201 if ( $config->get( 'ResponsiveImages' ) ) {
202 $modules['core'][] = 'mediawiki.hidpi';
203 }
204
205 // Preload jquery.tablesorter for mediawiki.page.ready
206 if ( strpos( $out->getHTML(), 'sortable' ) !== false ) {
207 $modules['content'][] = 'jquery.tablesorter';
208 }
209
210 // Preload jquery.makeCollapsible for mediawiki.page.ready
211 if ( strpos( $out->getHTML(), 'mw-collapsible' ) !== false ) {
212 $modules['content'][] = 'jquery.makeCollapsible';
213 }
214
215 if ( $out->isTOCEnabled() ) {
216 $modules['content'][] = 'mediawiki.toc';
217 }
218
219 // Add various resources if required
220 if ( $wgUseAjax && $wgEnableAPI ) {
221 if ( $wgEnableWriteAPI && $user->isLoggedIn()
222 && $user->isAllowedAll( 'writeapi', 'viewmywatchlist', 'editmywatchlist' )
223 && $this->getRelevantTitle()->canExist()
224 ) {
225 $modules['watch'][] = 'mediawiki.page.watch.ajax';
226 }
227
228 $modules['search'][] = 'mediawiki.searchSuggest';
229 }
230
231 if ( $user->getBoolOption( 'editsectiononrightclick' ) ) {
232 $modules['user'][] = 'mediawiki.action.view.rightClickEdit';
233 }
234
235 // Crazy edit-on-double-click stuff
236 if ( $out->isArticle() && $user->getOption( 'editondblclick' ) ) {
237 $modules['user'][] = 'mediawiki.action.view.dblClickEdit';
238 }
239 return $modules;
240 }
241
242 /**
243 * Preload the existence of three commonly-requested pages in a single query
244 */
245 protected function preloadExistence() {
246 $titles = [];
247
248 // User/talk link
249 $user = $this->getUser();
250 if ( $user->isLoggedIn() ) {
251 $titles[] = $user->getUserPage();
252 $titles[] = $user->getTalkPage();
253 }
254
255 // Check, if the page can hold some kind of content, otherwise do nothing
256 $title = $this->getRelevantTitle();
257 if ( $title->canExist() ) {
258 if ( $title->isTalkPage() ) {
259 $titles[] = $title->getSubjectPage();
260 } else {
261 $titles[] = $title->getTalkPage();
262 }
263 }
264
265 // Footer links (used by SkinTemplate::prepareQuickTemplate)
266 foreach ( [
267 $this->footerLinkTitle( 'privacy', 'privacypage' ),
268 $this->footerLinkTitle( 'aboutsite', 'aboutpage' ),
269 $this->footerLinkTitle( 'disclaimers', 'disclaimerpage' ),
270 ] as $title ) {
271 if ( $title ) {
272 $titles[] = $title;
273 }
274 }
275
276 Hooks::run( 'SkinPreloadExistence', [ &$titles, $this ] );
277
278 if ( $titles ) {
279 $lb = new LinkBatch( $titles );
280 $lb->setCaller( __METHOD__ );
281 $lb->execute();
282 }
283 }
284
285 /**
286 * Get the current revision ID
287 *
288 * @return int
289 */
290 public function getRevisionId() {
291 return $this->getOutput()->getRevisionId();
292 }
293
294 /**
295 * Whether the revision displayed is the latest revision of the page
296 *
297 * @return bool
298 */
299 public function isRevisionCurrent() {
300 $revID = $this->getRevisionId();
301 return $revID == 0 || $revID == $this->getTitle()->getLatestRevID();
302 }
303
304 /**
305 * Set the "relevant" title
306 * @see self::getRelevantTitle()
307 * @param Title $t
308 */
309 public function setRelevantTitle( $t ) {
310 $this->mRelevantTitle = $t;
311 }
312
313 /**
314 * Return the "relevant" title.
315 * A "relevant" title is not necessarily the actual title of the page.
316 * Special pages like Special:MovePage use set the page they are acting on
317 * as their "relevant" title, this allows the skin system to display things
318 * such as content tabs which belong to to that page instead of displaying
319 * a basic special page tab which has almost no meaning.
320 *
321 * @return Title
322 */
323 public function getRelevantTitle() {
324 if ( isset( $this->mRelevantTitle ) ) {
325 return $this->mRelevantTitle;
326 }
327 return $this->getTitle();
328 }
329
330 /**
331 * Set the "relevant" user
332 * @see self::getRelevantUser()
333 * @param User $u
334 */
335 public function setRelevantUser( $u ) {
336 $this->mRelevantUser = $u;
337 }
338
339 /**
340 * Return the "relevant" user.
341 * A "relevant" user is similar to a relevant title. Special pages like
342 * Special:Contributions mark the user which they are relevant to so that
343 * things like the toolbox can display the information they usually are only
344 * able to display on a user's userpage and talkpage.
345 * @return User
346 */
347 public function getRelevantUser() {
348 if ( isset( $this->mRelevantUser ) ) {
349 return $this->mRelevantUser;
350 }
351 $title = $this->getRelevantTitle();
352 if ( $title->hasSubjectNamespace( NS_USER ) ) {
353 $rootUser = $title->getRootText();
354 if ( User::isIP( $rootUser ) ) {
355 $this->mRelevantUser = User::newFromName( $rootUser, false );
356 } else {
357 $user = User::newFromName( $rootUser, false );
358
359 if ( $user ) {
360 $user->load( User::READ_NORMAL );
361
362 if ( $user->isLoggedIn() ) {
363 $this->mRelevantUser = $user;
364 }
365 }
366 }
367 return $this->mRelevantUser;
368 }
369 return null;
370 }
371
372 /**
373 * Outputs the HTML generated by other functions.
374 * @param OutputPage $out
375 */
376 abstract function outputPage( OutputPage $out = null );
377
378 /**
379 * @param array $data
380 * @return string
381 */
382 static function makeVariablesScript( $data ) {
383 if ( $data ) {
384 return ResourceLoader::makeInlineScript(
385 ResourceLoader::makeConfigSetScript( $data )
386 );
387 } else {
388 return '';
389 }
390 }
391
392 /**
393 * Get the query to generate a dynamic stylesheet
394 *
395 * @return array
396 */
397 public static function getDynamicStylesheetQuery() {
398 global $wgSquidMaxage;
399
400 return [
401 'action' => 'raw',
402 'maxage' => $wgSquidMaxage,
403 'usemsgcache' => 'yes',
404 'ctype' => 'text/css',
405 'smaxage' => $wgSquidMaxage,
406 ];
407 }
408
409 /**
410 * Add skin specific stylesheets
411 * Calling this method with an $out of anything but the same OutputPage
412 * inside ->getOutput() is deprecated. The $out arg is kept
413 * for compatibility purposes with skins.
414 * @param OutputPage $out
415 * @todo delete
416 */
417 abstract function setupSkinUserCss( OutputPage $out );
418
419 /**
420 * TODO: document
421 * @param Title $title
422 * @return string
423 */
424 function getPageClasses( $title ) {
425 $numeric = 'ns-' . $title->getNamespace();
426
427 if ( $title->isSpecialPage() ) {
428 $type = 'ns-special';
429 // T25315: provide a class based on the canonical special page name without subpages
430 list( $canonicalName ) = SpecialPageFactory::resolveAlias( $title->getDBkey() );
431 if ( $canonicalName ) {
432 $type .= ' ' . Sanitizer::escapeClass( "mw-special-$canonicalName" );
433 } else {
434 $type .= ' mw-invalidspecialpage';
435 }
436 } elseif ( $title->isTalkPage() ) {
437 $type = 'ns-talk';
438 } else {
439 $type = 'ns-subject';
440 }
441
442 $name = Sanitizer::escapeClass( 'page-' . $title->getPrefixedText() );
443 $root = Sanitizer::escapeClass( 'rootpage-' . $title->getRootTitle()->getPrefixedText() );
444
445 return "$numeric $type $name $root";
446 }
447
448 /**
449 * Return values for <html> element
450 * @return array Array of associative name-to-value elements for <html> element
451 */
452 public function getHtmlElementAttributes() {
453 $lang = $this->getLanguage();
454 return [
455 'lang' => $lang->getHtmlCode(),
456 'dir' => $lang->getDir(),
457 'class' => 'client-nojs',
458 ];
459 }
460
461 /**
462 * This will be called by OutputPage::headElement when it is creating the
463 * "<body>" tag, skins can override it if they have a need to add in any
464 * body attributes or classes of their own.
465 * @param OutputPage $out
466 * @param array &$bodyAttrs
467 */
468 function addToBodyAttributes( $out, &$bodyAttrs ) {
469 // does nothing by default
470 }
471
472 /**
473 * URL to the logo
474 * @return string
475 */
476 function getLogo() {
477 global $wgLogo;
478 return $wgLogo;
479 }
480
481 /**
482 * Whether the logo should be preloaded with an HTTP link header or not
483 * @since 1.29
484 * @return bool
485 */
486 public function shouldPreloadLogo() {
487 return false;
488 }
489
490 /**
491 * @return string HTML
492 */
493 function getCategoryLinks() {
494 global $wgUseCategoryBrowser;
495
496 $out = $this->getOutput();
497 $allCats = $out->getCategoryLinks();
498
499 if ( !count( $allCats ) ) {
500 return '';
501 }
502
503 $embed = "<li>";
504 $pop = "</li>";
505
506 $s = '';
507 $colon = $this->msg( 'colon-separator' )->escaped();
508
509 if ( !empty( $allCats['normal'] ) ) {
510 $t = $embed . implode( "{$pop}{$embed}", $allCats['normal'] ) . $pop;
511
512 $msg = $this->msg( 'pagecategories' )->numParams( count( $allCats['normal'] ) )->escaped();
513 $linkPage = wfMessage( 'pagecategorieslink' )->inContentLanguage()->text();
514 $title = Title::newFromText( $linkPage );
515 $link = $title ? Linker::link( $title, $msg ) : $msg;
516 $s .= '<div id="mw-normal-catlinks" class="mw-normal-catlinks">' .
517 $link . $colon . '<ul>' . $t . '</ul>' . '</div>';
518 }
519
520 # Hidden categories
521 if ( isset( $allCats['hidden'] ) ) {
522 if ( $this->getUser()->getBoolOption( 'showhiddencats' ) ) {
523 $class = ' mw-hidden-cats-user-shown';
524 } elseif ( $this->getTitle()->getNamespace() == NS_CATEGORY ) {
525 $class = ' mw-hidden-cats-ns-shown';
526 } else {
527 $class = ' mw-hidden-cats-hidden';
528 }
529
530 $s .= "<div id=\"mw-hidden-catlinks\" class=\"mw-hidden-catlinks$class\">" .
531 $this->msg( 'hidden-categories' )->numParams( count( $allCats['hidden'] ) )->escaped() .
532 $colon . '<ul>' . $embed . implode( "{$pop}{$embed}", $allCats['hidden'] ) . $pop . '</ul>' .
533 '</div>';
534 }
535
536 # optional 'dmoz-like' category browser. Will be shown under the list
537 # of categories an article belong to
538 if ( $wgUseCategoryBrowser ) {
539 $s .= '<br /><hr />';
540
541 # get a big array of the parents tree
542 $parenttree = $this->getTitle()->getParentCategoryTree();
543 # Skin object passed by reference cause it can not be
544 # accessed under the method subfunction drawCategoryBrowser
545 $tempout = explode( "\n", $this->drawCategoryBrowser( $parenttree ) );
546 # Clean out bogus first entry and sort them
547 unset( $tempout[0] );
548 asort( $tempout );
549 # Output one per line
550 $s .= implode( "<br />\n", $tempout );
551 }
552
553 return $s;
554 }
555
556 /**
557 * Render the array as a series of links.
558 * @param array $tree Categories tree returned by Title::getParentCategoryTree
559 * @return string Separated by &gt;, terminate with "\n"
560 */
561 function drawCategoryBrowser( $tree ) {
562 $return = '';
563
564 foreach ( $tree as $element => $parent ) {
565 if ( empty( $parent ) ) {
566 # element start a new list
567 $return .= "\n";
568 } else {
569 # grab the others elements
570 $return .= $this->drawCategoryBrowser( $parent ) . ' &gt; ';
571 }
572
573 # add our current element to the list
574 $eltitle = Title::newFromText( $element );
575 $return .= Linker::link( $eltitle, htmlspecialchars( $eltitle->getText() ) );
576 }
577
578 return $return;
579 }
580
581 /**
582 * @return string HTML
583 */
584 function getCategories() {
585 $out = $this->getOutput();
586 $catlinks = $this->getCategoryLinks();
587
588 // Check what we're showing
589 $allCats = $out->getCategoryLinks();
590 $showHidden = $this->getUser()->getBoolOption( 'showhiddencats' ) ||
591 $this->getTitle()->getNamespace() == NS_CATEGORY;
592
593 $classes = [ 'catlinks' ];
594 if ( empty( $allCats['normal'] ) && !( !empty( $allCats['hidden'] ) && $showHidden ) ) {
595 $classes[] = 'catlinks-allhidden';
596 }
597
598 return Html::rawElement(
599 'div',
600 [ 'id' => 'catlinks', 'class' => $classes, 'data-mw' => 'interface' ],
601 $catlinks
602 );
603 }
604
605 /**
606 * This runs a hook to allow extensions placing their stuff after content
607 * and article metadata (e.g. categories).
608 * Note: This function has nothing to do with afterContent().
609 *
610 * This hook is placed here in order to allow using the same hook for all
611 * skins, both the SkinTemplate based ones and the older ones, which directly
612 * use this class to get their data.
613 *
614 * The output of this function gets processed in SkinTemplate::outputPage() for
615 * the SkinTemplate based skins, all other skins should directly echo it.
616 *
617 * @return string Empty by default, if not changed by any hook function.
618 */
619 protected function afterContentHook() {
620 $data = '';
621
622 if ( Hooks::run( 'SkinAfterContent', [ &$data, $this ] ) ) {
623 // adding just some spaces shouldn't toggle the output
624 // of the whole <div/>, so we use trim() here
625 if ( trim( $data ) != '' ) {
626 // Doing this here instead of in the skins to
627 // ensure that the div has the same ID in all
628 // skins
629 $data = "<div id='mw-data-after-content'>\n" .
630 "\t$data\n" .
631 "</div>\n";
632 }
633 } else {
634 wfDebug( "Hook SkinAfterContent changed output processing.\n" );
635 }
636
637 return $data;
638 }
639
640 /**
641 * Generate debug data HTML for displaying at the bottom of the main content
642 * area.
643 * @return string HTML containing debug data, if enabled (otherwise empty).
644 */
645 protected function generateDebugHTML() {
646 return MWDebug::getHTMLDebugLog();
647 }
648
649 /**
650 * This gets called shortly before the "</body>" tag.
651 *
652 * @return string HTML-wrapped JS code to be put before "</body>"
653 */
654 function bottomScripts() {
655 // TODO and the suckage continues. This function is really just a wrapper around
656 // OutputPage::getBottomScripts() which takes a Skin param. This should be cleaned
657 // up at some point
658 $bottomScriptText = $this->getOutput()->getBottomScripts();
659 Hooks::run( 'SkinAfterBottomScripts', [ $this, &$bottomScriptText ] );
660
661 return $bottomScriptText;
662 }
663
664 /**
665 * Text with the permalink to the source page,
666 * usually shown on the footer of a printed page
667 *
668 * @return string HTML text with an URL
669 */
670 function printSource() {
671 $oldid = $this->getRevisionId();
672 if ( $oldid ) {
673 $canonicalUrl = $this->getTitle()->getCanonicalURL( 'oldid=' . $oldid );
674 $url = htmlspecialchars( wfExpandIRI( $canonicalUrl ) );
675 } else {
676 // oldid not available for non existing pages
677 $url = htmlspecialchars( wfExpandIRI( $this->getTitle()->getCanonicalURL() ) );
678 }
679
680 return $this->msg( 'retrievedfrom' )
681 ->rawParams( '<a dir="ltr" href="' . $url . '">' . $url . '</a>' )
682 ->parse();
683 }
684
685 /**
686 * @return string HTML
687 */
688 function getUndeleteLink() {
689 $action = $this->getRequest()->getVal( 'action', 'view' );
690
691 if ( $this->getTitle()->userCan( 'deletedhistory', $this->getUser() ) &&
692 ( !$this->getTitle()->exists() || $action == 'history' ) ) {
693 $n = $this->getTitle()->isDeleted();
694
695 if ( $n ) {
696 if ( $this->getTitle()->quickUserCan( 'undelete', $this->getUser() ) ) {
697 $msg = 'thisisdeleted';
698 } else {
699 $msg = 'viewdeleted';
700 }
701
702 return $this->msg( $msg )->rawParams(
703 Linker::linkKnown(
704 SpecialPage::getTitleFor( 'Undelete', $this->getTitle()->getPrefixedDBkey() ),
705 $this->msg( 'restorelink' )->numParams( $n )->escaped() )
706 )->escaped();
707 }
708 }
709
710 return '';
711 }
712
713 /**
714 * @param OutputPage $out Defaults to $this->getOutput() if left as null
715 * @return string
716 */
717 function subPageSubtitle( $out = null ) {
718 if ( $out === null ) {
719 $out = $this->getOutput();
720 }
721 $title = $out->getTitle();
722 $subpages = '';
723
724 if ( !Hooks::run( 'SkinSubPageSubtitle', [ &$subpages, $this, $out ] ) ) {
725 return $subpages;
726 }
727
728 if ( $out->isArticle() && MWNamespace::hasSubpages( $title->getNamespace() ) ) {
729 $ptext = $title->getPrefixedText();
730 if ( strpos( $ptext, '/' ) !== false ) {
731 $links = explode( '/', $ptext );
732 array_pop( $links );
733 $c = 0;
734 $growinglink = '';
735 $display = '';
736 $lang = $this->getLanguage();
737
738 foreach ( $links as $link ) {
739 $growinglink .= $link;
740 $display .= $link;
741 $linkObj = Title::newFromText( $growinglink );
742
743 if ( is_object( $linkObj ) && $linkObj->isKnown() ) {
744 $getlink = Linker::linkKnown(
745 $linkObj,
746 htmlspecialchars( $display )
747 );
748
749 $c++;
750
751 if ( $c > 1 ) {
752 $subpages .= $lang->getDirMarkEntity() . $this->msg( 'pipe-separator' )->escaped();
753 } else {
754 $subpages .= '&lt; ';
755 }
756
757 $subpages .= $getlink;
758 $display = '';
759 } else {
760 $display .= '/';
761 }
762 $growinglink .= '/';
763 }
764 }
765 }
766
767 return $subpages;
768 }
769
770 /**
771 * @return string
772 */
773 function getSearchLink() {
774 $searchPage = SpecialPage::getTitleFor( 'Search' );
775 return $searchPage->getLocalURL();
776 }
777
778 /**
779 * @return string
780 */
781 function escapeSearchLink() {
782 return htmlspecialchars( $this->getSearchLink() );
783 }
784
785 /**
786 * @param string $type
787 * @return string
788 */
789 function getCopyright( $type = 'detect' ) {
790 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
791
792 if ( $type == 'detect' ) {
793 if ( !$this->isRevisionCurrent()
794 && !$this->msg( 'history_copyright' )->inContentLanguage()->isDisabled()
795 ) {
796 $type = 'history';
797 } else {
798 $type = 'normal';
799 }
800 }
801
802 if ( $type == 'history' ) {
803 $msg = 'history_copyright';
804 } else {
805 $msg = 'copyright';
806 }
807
808 if ( $wgRightsPage ) {
809 $title = Title::newFromText( $wgRightsPage );
810 $link = Linker::linkKnown( $title, $wgRightsText );
811 } elseif ( $wgRightsUrl ) {
812 $link = Linker::makeExternalLink( $wgRightsUrl, $wgRightsText );
813 } elseif ( $wgRightsText ) {
814 $link = $wgRightsText;
815 } else {
816 # Give up now
817 return '';
818 }
819
820 // Allow for site and per-namespace customization of copyright notice.
821 // @todo Remove deprecated $forContent param from hook handlers and then remove here.
822 $forContent = true;
823
824 Hooks::run(
825 'SkinCopyrightFooter',
826 [ $this->getTitle(), $type, &$msg, &$link, &$forContent ]
827 );
828
829 return $this->msg( $msg )->rawParams( $link )->text();
830 }
831
832 /**
833 * @return null|string
834 */
835 function getCopyrightIcon() {
836 global $wgRightsUrl, $wgRightsText, $wgRightsIcon, $wgFooterIcons;
837
838 $out = '';
839
840 if ( $wgFooterIcons['copyright']['copyright'] ) {
841 $out = $wgFooterIcons['copyright']['copyright'];
842 } elseif ( $wgRightsIcon ) {
843 $icon = htmlspecialchars( $wgRightsIcon );
844
845 if ( $wgRightsUrl ) {
846 $url = htmlspecialchars( $wgRightsUrl );
847 $out .= '<a href="' . $url . '">';
848 }
849
850 $text = htmlspecialchars( $wgRightsText );
851 $out .= "<img src=\"$icon\" alt=\"$text\" width=\"88\" height=\"31\" />";
852
853 if ( $wgRightsUrl ) {
854 $out .= '</a>';
855 }
856 }
857
858 return $out;
859 }
860
861 /**
862 * Gets the powered by MediaWiki icon.
863 * @return string
864 */
865 function getPoweredBy() {
866 global $wgResourceBasePath;
867
868 $url1 = htmlspecialchars(
869 "$wgResourceBasePath/resources/assets/poweredby_mediawiki_88x31.png"
870 );
871 $url1_5 = htmlspecialchars(
872 "$wgResourceBasePath/resources/assets/poweredby_mediawiki_132x47.png"
873 );
874 $url2 = htmlspecialchars(
875 "$wgResourceBasePath/resources/assets/poweredby_mediawiki_176x62.png"
876 );
877 $text = '<a href="//www.mediawiki.org/"><img src="' . $url1
878 . '" srcset="' . $url1_5 . ' 1.5x, ' . $url2 . ' 2x" '
879 . 'height="31" width="88" alt="Powered by MediaWiki" /></a>';
880 Hooks::run( 'SkinGetPoweredBy', [ &$text, $this ] );
881 return $text;
882 }
883
884 /**
885 * Get the timestamp of the latest revision, formatted in user language
886 *
887 * @return string
888 */
889 protected function lastModified() {
890 $timestamp = $this->getOutput()->getRevisionTimestamp();
891
892 # No cached timestamp, load it from the database
893 if ( $timestamp === null ) {
894 $timestamp = Revision::getTimestampFromId( $this->getTitle(), $this->getRevisionId() );
895 }
896
897 if ( $timestamp ) {
898 $d = $this->getLanguage()->userDate( $timestamp, $this->getUser() );
899 $t = $this->getLanguage()->userTime( $timestamp, $this->getUser() );
900 $s = ' ' . $this->msg( 'lastmodifiedat', $d, $t )->parse();
901 } else {
902 $s = '';
903 }
904
905 if ( MediaWikiServices::getInstance()->getDBLoadBalancer()->getLaggedReplicaMode() ) {
906 $s .= ' <strong>' . $this->msg( 'laggedslavemode' )->parse() . '</strong>';
907 }
908
909 return $s;
910 }
911
912 /**
913 * @param string $align
914 * @return string
915 */
916 function logoText( $align = '' ) {
917 if ( $align != '' ) {
918 $a = " style='float: {$align};'";
919 } else {
920 $a = '';
921 }
922
923 $mp = $this->msg( 'mainpage' )->escaped();
924 $mptitle = Title::newMainPage();
925 $url = ( is_object( $mptitle ) ? htmlspecialchars( $mptitle->getLocalURL() ) : '' );
926
927 $logourl = $this->getLogo();
928 $s = "<a href='{$url}'><img{$a} src='{$logourl}' alt='[{$mp}]' /></a>";
929
930 return $s;
931 }
932
933 /**
934 * Renders a $wgFooterIcons icon according to the method's arguments
935 * @param array $icon The icon to build the html for, see $wgFooterIcons
936 * for the format of this array.
937 * @param bool|string $withImage Whether to use the icon's image or output
938 * a text-only footericon.
939 * @return string HTML
940 */
941 function makeFooterIcon( $icon, $withImage = 'withImage' ) {
942 if ( is_string( $icon ) ) {
943 $html = $icon;
944 } else { // Assuming array
945 $url = isset( $icon["url"] ) ? $icon["url"] : null;
946 unset( $icon["url"] );
947 if ( isset( $icon["src"] ) && $withImage === 'withImage' ) {
948 // do this the lazy way, just pass icon data as an attribute array
949 $html = Html::element( 'img', $icon );
950 } else {
951 $html = htmlspecialchars( $icon["alt"] );
952 }
953 if ( $url ) {
954 global $wgExternalLinkTarget;
955 $html = Html::rawElement( 'a',
956 [ "href" => $url, "target" => $wgExternalLinkTarget ],
957 $html );
958 }
959 }
960 return $html;
961 }
962
963 /**
964 * Gets the link to the wiki's main page.
965 * @return string
966 */
967 function mainPageLink() {
968 $s = Linker::linkKnown(
969 Title::newMainPage(),
970 $this->msg( 'mainpage' )->escaped()
971 );
972
973 return $s;
974 }
975
976 /**
977 * Returns an HTML link for use in the footer
978 * @param string $desc The i18n message key for the link text
979 * @param string $page The i18n message key for the page to link to
980 * @return string HTML anchor
981 */
982 public function footerLink( $desc, $page ) {
983 $title = $this->footerLinkTitle( $desc, $page );
984 if ( !$title ) {
985 return '';
986 }
987
988 return Linker::linkKnown(
989 $title,
990 $this->msg( $desc )->escaped()
991 );
992 }
993
994 /**
995 * @param string $desc
996 * @param string $page
997 * @return Title|null
998 */
999 private function footerLinkTitle( $desc, $page ) {
1000 // If the link description has been set to "-" in the default language,
1001 if ( $this->msg( $desc )->inContentLanguage()->isDisabled() ) {
1002 // then it is disabled, for all languages.
1003 return null;
1004 }
1005 // Otherwise, we display the link for the user, described in their
1006 // language (which may or may not be the same as the default language),
1007 // but we make the link target be the one site-wide page.
1008 $title = Title::newFromText( $this->msg( $page )->inContentLanguage()->text() );
1009
1010 return $title ?: null;
1011 }
1012
1013 /**
1014 * Gets the link to the wiki's privacy policy page.
1015 * @return string HTML
1016 */
1017 function privacyLink() {
1018 return $this->footerLink( 'privacy', 'privacypage' );
1019 }
1020
1021 /**
1022 * Gets the link to the wiki's about page.
1023 * @return string HTML
1024 */
1025 function aboutLink() {
1026 return $this->footerLink( 'aboutsite', 'aboutpage' );
1027 }
1028
1029 /**
1030 * Gets the link to the wiki's general disclaimers page.
1031 * @return string HTML
1032 */
1033 function disclaimerLink() {
1034 return $this->footerLink( 'disclaimers', 'disclaimerpage' );
1035 }
1036
1037 /**
1038 * Return URL options for the 'edit page' link.
1039 * This may include an 'oldid' specifier, if the current page view is such.
1040 *
1041 * @return array
1042 * @private
1043 */
1044 function editUrlOptions() {
1045 $options = [ 'action' => 'edit' ];
1046
1047 if ( !$this->isRevisionCurrent() ) {
1048 $options['oldid'] = intval( $this->getRevisionId() );
1049 }
1050
1051 return $options;
1052 }
1053
1054 /**
1055 * @param User|int $id
1056 * @return bool
1057 */
1058 function showEmailUser( $id ) {
1059 if ( $id instanceof User ) {
1060 $targetUser = $id;
1061 } else {
1062 $targetUser = User::newFromId( $id );
1063 }
1064
1065 # The sending user must have a confirmed email address and the receiving
1066 # user must accept emails from the sender.
1067 return $this->getUser()->canSendEmail()
1068 && SpecialEmailUser::validateTarget( $targetUser, $this->getUser() ) === '';
1069 }
1070
1071 /**
1072 * Return a fully resolved style path url to images or styles stored in the current skins's folder.
1073 * This method returns a url resolved using the configured skin style path
1074 * and includes the style version inside of the url.
1075 *
1076 * Requires $stylename to be set, otherwise throws MWException.
1077 *
1078 * @param string $name The name or path of a skin resource file
1079 * @return string The fully resolved style path url including styleversion
1080 * @throws MWException
1081 */
1082 function getSkinStylePath( $name ) {
1083 global $wgStylePath, $wgStyleVersion;
1084
1085 if ( $this->stylename === null ) {
1086 $class = static::class;
1087 throw new MWException( "$class::\$stylename must be set to use getSkinStylePath()" );
1088 }
1089
1090 return "$wgStylePath/{$this->stylename}/$name?$wgStyleVersion";
1091 }
1092
1093 /* these are used extensively in SkinTemplate, but also some other places */
1094
1095 /**
1096 * @param string $urlaction
1097 * @return string
1098 */
1099 static function makeMainPageUrl( $urlaction = '' ) {
1100 $title = Title::newMainPage();
1101 self::checkTitle( $title, '' );
1102
1103 return $title->getLinkURL( $urlaction );
1104 }
1105
1106 /**
1107 * Make a URL for a Special Page using the given query and protocol.
1108 *
1109 * If $proto is set to null, make a local URL. Otherwise, make a full
1110 * URL with the protocol specified.
1111 *
1112 * @param string $name Name of the Special page
1113 * @param string $urlaction Query to append
1114 * @param string|null $proto Protocol to use or null for a local URL
1115 * @return string
1116 */
1117 static function makeSpecialUrl( $name, $urlaction = '', $proto = null ) {
1118 $title = SpecialPage::getSafeTitleFor( $name );
1119 if ( is_null( $proto ) ) {
1120 return $title->getLocalURL( $urlaction );
1121 } else {
1122 return $title->getFullURL( $urlaction, false, $proto );
1123 }
1124 }
1125
1126 /**
1127 * @param string $name
1128 * @param string $subpage
1129 * @param string $urlaction
1130 * @return string
1131 */
1132 static function makeSpecialUrlSubpage( $name, $subpage, $urlaction = '' ) {
1133 $title = SpecialPage::getSafeTitleFor( $name, $subpage );
1134 return $title->getLocalURL( $urlaction );
1135 }
1136
1137 /**
1138 * @param string $name
1139 * @param string $urlaction
1140 * @return string
1141 */
1142 static function makeI18nUrl( $name, $urlaction = '' ) {
1143 $title = Title::newFromText( wfMessage( $name )->inContentLanguage()->text() );
1144 self::checkTitle( $title, $name );
1145 return $title->getLocalURL( $urlaction );
1146 }
1147
1148 /**
1149 * @param string $name
1150 * @param string $urlaction
1151 * @return string
1152 */
1153 static function makeUrl( $name, $urlaction = '' ) {
1154 $title = Title::newFromText( $name );
1155 self::checkTitle( $title, $name );
1156
1157 return $title->getLocalURL( $urlaction );
1158 }
1159
1160 /**
1161 * If url string starts with http, consider as external URL, else
1162 * internal
1163 * @param string $name
1164 * @return string URL
1165 */
1166 static function makeInternalOrExternalUrl( $name ) {
1167 if ( preg_match( '/^(?i:' . wfUrlProtocols() . ')/', $name ) ) {
1168 return $name;
1169 } else {
1170 return self::makeUrl( $name );
1171 }
1172 }
1173
1174 /**
1175 * this can be passed the NS number as defined in Language.php
1176 * @param string $name
1177 * @param string $urlaction
1178 * @param int $namespace
1179 * @return string
1180 */
1181 static function makeNSUrl( $name, $urlaction = '', $namespace = NS_MAIN ) {
1182 $title = Title::makeTitleSafe( $namespace, $name );
1183 self::checkTitle( $title, $name );
1184
1185 return $title->getLocalURL( $urlaction );
1186 }
1187
1188 /**
1189 * these return an array with the 'href' and boolean 'exists'
1190 * @param string $name
1191 * @param string $urlaction
1192 * @return array
1193 */
1194 static function makeUrlDetails( $name, $urlaction = '' ) {
1195 $title = Title::newFromText( $name );
1196 self::checkTitle( $title, $name );
1197
1198 return [
1199 'href' => $title->getLocalURL( $urlaction ),
1200 'exists' => $title->isKnown(),
1201 ];
1202 }
1203
1204 /**
1205 * Make URL details where the article exists (or at least it's convenient to think so)
1206 * @param string $name Article name
1207 * @param string $urlaction
1208 * @return array
1209 */
1210 static function makeKnownUrlDetails( $name, $urlaction = '' ) {
1211 $title = Title::newFromText( $name );
1212 self::checkTitle( $title, $name );
1213
1214 return [
1215 'href' => $title->getLocalURL( $urlaction ),
1216 'exists' => true
1217 ];
1218 }
1219
1220 /**
1221 * make sure we have some title to operate on
1222 *
1223 * @param Title &$title
1224 * @param string $name
1225 */
1226 static function checkTitle( &$title, $name ) {
1227 if ( !is_object( $title ) ) {
1228 $title = Title::newFromText( $name );
1229 if ( !is_object( $title ) ) {
1230 $title = Title::newFromText( '--error: link target missing--' );
1231 }
1232 }
1233 }
1234
1235 /**
1236 * Build an array that represents the sidebar(s), the navigation bar among them.
1237 *
1238 * BaseTemplate::getSidebar can be used to simplify the format and id generation in new skins.
1239 *
1240 * The format of the returned array is [ heading => content, ... ], where:
1241 * - heading is the heading of a navigation portlet. It is either:
1242 * - magic string to be handled by the skins ('SEARCH' / 'LANGUAGES' / 'TOOLBOX' / ...)
1243 * - a message name (e.g. 'navigation'), the message should be HTML-escaped by the skin
1244 * - plain text, which should be HTML-escaped by the skin
1245 * - content is the contents of the portlet. It is either:
1246 * - HTML text (<ul><li>...</li>...</ul>)
1247 * - array of link data in a format accepted by BaseTemplate::makeListItem()
1248 * - (for a magic string as a key, any value)
1249 *
1250 * Note that extensions can control the sidebar contents using the SkinBuildSidebar hook
1251 * and can technically insert anything in here; skin creators are expected to handle
1252 * values described above.
1253 *
1254 * @return array
1255 */
1256 public function buildSidebar() {
1257 global $wgEnableSidebarCache, $wgSidebarCacheExpiry;
1258
1259 $callback = function ( $old = null, &$ttl = null ) {
1260 $bar = [];
1261 $this->addToSidebar( $bar, 'sidebar' );
1262 Hooks::run( 'SkinBuildSidebar', [ $this, &$bar ] );
1263 if ( MessageCache::singleton()->isDisabled() ) {
1264 $ttl = WANObjectCache::TTL_UNCACHEABLE; // bug T133069
1265 }
1266
1267 return $bar;
1268 };
1269
1270 $msgCache = MessageCache::singleton();
1271 $wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
1272
1273 $sidebar = $wgEnableSidebarCache
1274 ? $wanCache->getWithSetCallback(
1275 $wanCache->makeKey( 'sidebar', $this->getLanguage()->getCode() ),
1276 $wgSidebarCacheExpiry,
1277 $callback,
1278 [
1279 'checkKeys' => [
1280 // Unless there is both no exact $code override nor an i18n definition
1281 // in the the software, the only MediaWiki page to check is for $code.
1282 $msgCache->getCheckKey( $this->getLanguage()->getCode() )
1283 ],
1284 'lockTSE' => 30
1285 ]
1286 )
1287 : $callback();
1288
1289 // Apply post-processing to the cached value
1290 Hooks::run( 'SidebarBeforeOutput', [ $this, &$sidebar ] );
1291
1292 return $sidebar;
1293 }
1294
1295 /**
1296 * Add content from a sidebar system message
1297 * Currently only used for MediaWiki:Sidebar (but may be used by Extensions)
1298 *
1299 * This is just a wrapper around addToSidebarPlain() for backwards compatibility
1300 *
1301 * @param array &$bar
1302 * @param string $message
1303 */
1304 public function addToSidebar( &$bar, $message ) {
1305 $this->addToSidebarPlain( $bar, wfMessage( $message )->inContentLanguage()->plain() );
1306 }
1307
1308 /**
1309 * Add content from plain text
1310 * @since 1.17
1311 * @param array &$bar
1312 * @param string $text
1313 * @return array
1314 */
1315 function addToSidebarPlain( &$bar, $text ) {
1316 $lines = explode( "\n", $text );
1317
1318 $heading = '';
1319 $messageTitle = $this->getConfig()->get( 'EnableSidebarCache' )
1320 ? Title::newMainPage() : $this->getTitle();
1321
1322 foreach ( $lines as $line ) {
1323 if ( strpos( $line, '*' ) !== 0 ) {
1324 continue;
1325 }
1326 $line = rtrim( $line, "\r" ); // for Windows compat
1327
1328 if ( strpos( $line, '**' ) !== 0 ) {
1329 $heading = trim( $line, '* ' );
1330 if ( !array_key_exists( $heading, $bar ) ) {
1331 $bar[$heading] = [];
1332 }
1333 } else {
1334 $line = trim( $line, '* ' );
1335
1336 if ( strpos( $line, '|' ) !== false ) { // sanity check
1337 $line = MessageCache::singleton()->transform( $line, false, null, $messageTitle );
1338 $line = array_map( 'trim', explode( '|', $line, 2 ) );
1339 if ( count( $line ) !== 2 ) {
1340 // Second sanity check, could be hit by people doing
1341 // funky stuff with parserfuncs... (T35321)
1342 continue;
1343 }
1344
1345 $extraAttribs = [];
1346
1347 $msgLink = $this->msg( $line[0] )->title( $messageTitle )->inContentLanguage();
1348 if ( $msgLink->exists() ) {
1349 $link = $msgLink->text();
1350 if ( $link == '-' ) {
1351 continue;
1352 }
1353 } else {
1354 $link = $line[0];
1355 }
1356 $msgText = $this->msg( $line[1] )->title( $messageTitle );
1357 if ( $msgText->exists() ) {
1358 $text = $msgText->text();
1359 } else {
1360 $text = $line[1];
1361 }
1362
1363 if ( preg_match( '/^(?i:' . wfUrlProtocols() . ')/', $link ) ) {
1364 $href = $link;
1365
1366 // Parser::getExternalLinkAttribs won't work here because of the Namespace things
1367 global $wgNoFollowLinks, $wgNoFollowDomainExceptions;
1368 if ( $wgNoFollowLinks && !wfMatchesDomainList( $href, $wgNoFollowDomainExceptions ) ) {
1369 $extraAttribs['rel'] = 'nofollow';
1370 }
1371
1372 global $wgExternalLinkTarget;
1373 if ( $wgExternalLinkTarget ) {
1374 $extraAttribs['target'] = $wgExternalLinkTarget;
1375 }
1376 } else {
1377 $title = Title::newFromText( $link );
1378
1379 if ( $title ) {
1380 $title = $title->fixSpecialName();
1381 $href = $title->getLinkURL();
1382 } else {
1383 $href = 'INVALID-TITLE';
1384 }
1385 }
1386
1387 $bar[$heading][] = array_merge( [
1388 'text' => $text,
1389 'href' => $href,
1390 'id' => Sanitizer::escapeIdForAttribute( 'n-' . strtr( $line[1], ' ', '-' ) ),
1391 'active' => false,
1392 ], $extraAttribs );
1393 } else {
1394 continue;
1395 }
1396 }
1397 }
1398
1399 return $bar;
1400 }
1401
1402 /**
1403 * Gets new talk page messages for the current user and returns an
1404 * appropriate alert message (or an empty string if there are no messages)
1405 * @return string
1406 */
1407 function getNewtalks() {
1408 $newMessagesAlert = '';
1409 $user = $this->getUser();
1410 $newtalks = $user->getNewMessageLinks();
1411 $out = $this->getOutput();
1412
1413 // Allow extensions to disable or modify the new messages alert
1414 if ( !Hooks::run( 'GetNewMessagesAlert', [ &$newMessagesAlert, $newtalks, $user, $out ] ) ) {
1415 return '';
1416 }
1417 if ( $newMessagesAlert ) {
1418 return $newMessagesAlert;
1419 }
1420
1421 if ( count( $newtalks ) == 1 && $newtalks[0]['wiki'] === wfWikiID() ) {
1422 $uTalkTitle = $user->getTalkPage();
1423 $lastSeenRev = isset( $newtalks[0]['rev'] ) ? $newtalks[0]['rev'] : null;
1424 $nofAuthors = 0;
1425 if ( $lastSeenRev !== null ) {
1426 $plural = true; // Default if we have a last seen revision: if unknown, use plural
1427 $latestRev = Revision::newFromTitle( $uTalkTitle, false, Revision::READ_NORMAL );
1428 if ( $latestRev !== null ) {
1429 // Singular if only 1 unseen revision, plural if several unseen revisions.
1430 $plural = $latestRev->getParentId() !== $lastSeenRev->getId();
1431 $nofAuthors = $uTalkTitle->countAuthorsBetween(
1432 $lastSeenRev, $latestRev, 10, 'include_new' );
1433 }
1434 } else {
1435 // Singular if no revision -> diff link will show latest change only in any case
1436 $plural = false;
1437 }
1438 $plural = $plural ? 999 : 1;
1439 // 999 signifies "more than one revision". We don't know how many, and even if we did,
1440 // the number of revisions or authors is not necessarily the same as the number of
1441 // "messages".
1442 $newMessagesLink = Linker::linkKnown(
1443 $uTalkTitle,
1444 $this->msg( 'newmessageslinkplural' )->params( $plural )->escaped(),
1445 [],
1446 [ 'redirect' => 'no' ]
1447 );
1448
1449 $newMessagesDiffLink = Linker::linkKnown(
1450 $uTalkTitle,
1451 $this->msg( 'newmessagesdifflinkplural' )->params( $plural )->escaped(),
1452 [],
1453 $lastSeenRev !== null
1454 ? [ 'oldid' => $lastSeenRev->getId(), 'diff' => 'cur' ]
1455 : [ 'diff' => 'cur' ]
1456 );
1457
1458 if ( $nofAuthors >= 1 && $nofAuthors <= 10 ) {
1459 $newMessagesAlert = $this->msg(
1460 'youhavenewmessagesfromusers',
1461 $newMessagesLink,
1462 $newMessagesDiffLink
1463 )->numParams( $nofAuthors, $plural );
1464 } else {
1465 // $nofAuthors === 11 signifies "11 or more" ("more than 10")
1466 $newMessagesAlert = $this->msg(
1467 $nofAuthors > 10 ? 'youhavenewmessagesmanyusers' : 'youhavenewmessages',
1468 $newMessagesLink,
1469 $newMessagesDiffLink
1470 )->numParams( $plural );
1471 }
1472 $newMessagesAlert = $newMessagesAlert->text();
1473 # Disable CDN cache
1474 $out->setCdnMaxage( 0 );
1475 } elseif ( count( $newtalks ) ) {
1476 $sep = $this->msg( 'newtalkseparator' )->escaped();
1477 $msgs = [];
1478
1479 foreach ( $newtalks as $newtalk ) {
1480 $msgs[] = Xml::element(
1481 'a',
1482 [ 'href' => $newtalk['link'] ], $newtalk['wiki']
1483 );
1484 }
1485 $parts = implode( $sep, $msgs );
1486 $newMessagesAlert = $this->msg( 'youhavenewmessagesmulti' )->rawParams( $parts )->escaped();
1487 $out->setCdnMaxage( 0 );
1488 }
1489
1490 return $newMessagesAlert;
1491 }
1492
1493 /**
1494 * Get a cached notice
1495 *
1496 * @param string $name Message name, or 'default' for $wgSiteNotice
1497 * @return string|bool HTML fragment, or false to indicate that the caller
1498 * should fall back to the next notice in its sequence
1499 */
1500 private function getCachedNotice( $name ) {
1501 global $wgRenderHashAppend, $wgContLang;
1502
1503 $needParse = false;
1504
1505 if ( $name === 'default' ) {
1506 // special case
1507 global $wgSiteNotice;
1508 $notice = $wgSiteNotice;
1509 if ( empty( $notice ) ) {
1510 return false;
1511 }
1512 } else {
1513 $msg = $this->msg( $name )->inContentLanguage();
1514 if ( $msg->isBlank() ) {
1515 return '';
1516 } elseif ( $msg->isDisabled() ) {
1517 return false;
1518 }
1519 $notice = $msg->plain();
1520 }
1521
1522 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
1523 $parsed = $cache->getWithSetCallback(
1524 // Use the extra hash appender to let eg SSL variants separately cache
1525 // Key is verified with md5 hash of unparsed wikitext
1526 $cache->makeKey( $name, $wgRenderHashAppend, md5( $notice ) ),
1527 // TTL in seconds
1528 600,
1529 function () use ( $notice ) {
1530 return $this->getOutput()->parse( $notice );
1531 }
1532 );
1533
1534 return Html::rawElement(
1535 'div',
1536 [
1537 'id' => 'localNotice',
1538 'lang' => $wgContLang->getHtmlCode(),
1539 'dir' => $wgContLang->getDir()
1540 ],
1541 $parsed
1542 );
1543 }
1544
1545 /**
1546 * Get the site notice
1547 *
1548 * @return string HTML fragment
1549 */
1550 function getSiteNotice() {
1551 $siteNotice = '';
1552
1553 if ( Hooks::run( 'SiteNoticeBefore', [ &$siteNotice, $this ] ) ) {
1554 if ( is_object( $this->getUser() ) && $this->getUser()->isLoggedIn() ) {
1555 $siteNotice = $this->getCachedNotice( 'sitenotice' );
1556 } else {
1557 $anonNotice = $this->getCachedNotice( 'anonnotice' );
1558 if ( $anonNotice === false ) {
1559 $siteNotice = $this->getCachedNotice( 'sitenotice' );
1560 } else {
1561 $siteNotice = $anonNotice;
1562 }
1563 }
1564 if ( $siteNotice === false ) {
1565 $siteNotice = $this->getCachedNotice( 'default' );
1566 }
1567 }
1568
1569 Hooks::run( 'SiteNoticeAfter', [ &$siteNotice, $this ] );
1570 return $siteNotice;
1571 }
1572
1573 /**
1574 * Create a section edit link. This supersedes editSectionLink() and
1575 * editSectionLinkForOther().
1576 *
1577 * @param Title $nt The title being linked to (may not be the same as
1578 * the current page, if the section is included from a template)
1579 * @param string $section The designation of the section being pointed to,
1580 * to be included in the link, like "&section=$section"
1581 * @param string $tooltip The tooltip to use for the link: will be escaped
1582 * and wrapped in the 'editsectionhint' message
1583 * @param string $lang Language code
1584 * @return string HTML to use for edit link
1585 */
1586 public function doEditSectionLink( Title $nt, $section, $tooltip = null, $lang = false ) {
1587 // HTML generated here should probably have userlangattributes
1588 // added to it for LTR text on RTL pages
1589
1590 $lang = wfGetLangObj( $lang );
1591
1592 $attribs = [];
1593 if ( !is_null( $tooltip ) ) {
1594 $attribs['title'] = wfMessage( 'editsectionhint' )->rawParams( $tooltip )
1595 ->inLanguage( $lang )->text();
1596 }
1597
1598 $links = [
1599 'editsection' => [
1600 'text' => wfMessage( 'editsection' )->inLanguage( $lang )->escaped(),
1601 'targetTitle' => $nt,
1602 'attribs' => $attribs,
1603 'query' => [ 'action' => 'edit', 'section' => $section ],
1604 'options' => [ 'noclasses', 'known' ]
1605 ]
1606 ];
1607
1608 Hooks::run( 'SkinEditSectionLinks', [ $this, $nt, $section, $tooltip, &$links, $lang ] );
1609
1610 $result = '<span class="mw-editsection"><span class="mw-editsection-bracket">[</span>';
1611
1612 $linksHtml = [];
1613 foreach ( $links as $k => $linkDetails ) {
1614 $linksHtml[] = Linker::link(
1615 $linkDetails['targetTitle'],
1616 $linkDetails['text'],
1617 $linkDetails['attribs'],
1618 $linkDetails['query'],
1619 $linkDetails['options']
1620 );
1621 }
1622
1623 $result .= implode(
1624 '<span class="mw-editsection-divider">'
1625 . wfMessage( 'pipe-separator' )->inLanguage( $lang )->escaped()
1626 . '</span>',
1627 $linksHtml
1628 );
1629
1630 $result .= '<span class="mw-editsection-bracket">]</span></span>';
1631 // Deprecated, use SkinEditSectionLinks hook instead
1632 Hooks::run(
1633 'DoEditSectionLink',
1634 [ $this, $nt, $section, $tooltip, &$result, $lang ],
1635 '1.25'
1636 );
1637 return $result;
1638 }
1639
1640 }