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