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