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