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