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