Create a user.groups module in ResourceLoader, which bundles a CSS and JS page for...
[lhc/web/wiklou.git] / includes / Skin.php
1 <?php
2 /**
3 * @defgroup Skins Skins
4 */
5
6 if ( !defined( 'MEDIAWIKI' ) ) {
7 die( 1 );
8 }
9
10 /**
11 * The main skin class that provide methods and properties for all other skins.
12 * This base class is also the "Standard" skin.
13 *
14 * See docs/skin.txt for more information.
15 *
16 * @ingroup Skins
17 */
18 abstract class Skin extends Linker {
19 /**#@+
20 * @private
21 */
22 var $mWatchLinkNum = 0; // Appended to end of watch link id's
23 /**#@-*/
24 protected $mRevisionId; // The revision ID we're looking at, null if not applicable.
25 protected $skinname = 'standard';
26 // @todo Fixme: should be protected :-\
27 var $mTitle = null;
28 protected $mRelevantTitle = null;
29 protected $mRelevantUser = null;
30
31 /** Constructor, call parent constructor */
32 function __construct() {
33 parent::__construct();
34 }
35
36 /**
37 * Fetch the set of available skins.
38 * @return array of strings
39 */
40 static function getSkinNames() {
41 global $wgValidSkinNames;
42 static $skinsInitialised = false;
43
44 if ( !$skinsInitialised || !count( $wgValidSkinNames ) ) {
45 # Get a list of available skins
46 # Build using the regular expression '^(.*).php$'
47 # Array keys are all lower case, array value keep the case used by filename
48 #
49 wfProfileIn( __METHOD__ . '-init' );
50
51 global $wgStyleDirectory;
52
53 $skinDir = dir( $wgStyleDirectory );
54
55 # while code from www.php.net
56 while ( false !== ( $file = $skinDir->read() ) ) {
57 // Skip non-PHP files, hidden files, and '.dep' includes
58 $matches = array();
59
60 if ( preg_match( '/^([^.]*)\.php$/', $file, $matches ) ) {
61 $aSkin = $matches[1];
62 $wgValidSkinNames[strtolower( $aSkin )] = $aSkin;
63 }
64 }
65 $skinDir->close();
66 $skinsInitialised = true;
67 wfProfileOut( __METHOD__ . '-init' );
68 }
69 return $wgValidSkinNames;
70 }
71
72 /**
73 * Fetch the list of usable skins in regards to $wgSkipSkins.
74 * Useful for Special:Preferences and other places where you
75 * only want to show skins users _can_ use.
76 * @return array of strings
77 */
78 public static function getUsableSkins() {
79 global $wgSkipSkins;
80
81 $usableSkins = self::getSkinNames();
82
83 foreach ( $wgSkipSkins as $skip ) {
84 unset( $usableSkins[$skip] );
85 }
86
87 return $usableSkins;
88 }
89
90 /**
91 * Normalize a skin preference value to a form that can be loaded.
92 * If a skin can't be found, it will fall back to the configured
93 * default (or the old 'Classic' skin if that's broken).
94 * @param $key String: 'monobook', 'standard', etc.
95 * @return string
96 */
97 static function normalizeKey( $key ) {
98 global $wgDefaultSkin;
99
100 $skinNames = Skin::getSkinNames();
101
102 if ( $key == '' ) {
103 // Don't return the default immediately;
104 // in a misconfiguration we need to fall back.
105 $key = $wgDefaultSkin;
106 }
107
108 if ( isset( $skinNames[$key] ) ) {
109 return $key;
110 }
111
112 // Older versions of the software used a numeric setting
113 // in the user preferences.
114 $fallback = array(
115 0 => $wgDefaultSkin,
116 1 => 'nostalgia',
117 2 => 'cologneblue'
118 );
119
120 if ( isset( $fallback[$key] ) ) {
121 $key = $fallback[$key];
122 }
123
124 if ( isset( $skinNames[$key] ) ) {
125 return $key;
126 } else if ( isset( $skinNames[$wgDefaultSkin] ) ) {
127 return $wgDefaultSkin;
128 } else {
129 return 'vector';
130 }
131 }
132
133 /**
134 * Factory method for loading a skin of a given type
135 * @param $key String: 'monobook', 'standard', etc.
136 * @return Skin
137 */
138 static function &newFromKey( $key ) {
139 global $wgStyleDirectory;
140
141 $key = Skin::normalizeKey( $key );
142
143 $skinNames = Skin::getSkinNames();
144 $skinName = $skinNames[$key];
145 $className = "Skin{$skinName}";
146
147 # Grab the skin class and initialise it.
148 if ( !class_exists( $className ) ) {
149 // Preload base classes to work around APC/PHP5 bug
150 $deps = "{$wgStyleDirectory}/{$skinName}.deps.php";
151
152 if ( file_exists( $deps ) ) {
153 include_once( $deps );
154 }
155 require_once( "{$wgStyleDirectory}/{$skinName}.php" );
156
157 # Check if we got if not failback to default skin
158 if ( !class_exists( $className ) ) {
159 # DO NOT die if the class isn't found. This breaks maintenance
160 # scripts and can cause a user account to be unrecoverable
161 # except by SQL manipulation if a previously valid skin name
162 # is no longer valid.
163 wfDebug( "Skin class does not exist: $className\n" );
164 $className = 'SkinVector';
165 require_once( "{$wgStyleDirectory}/Vector.php" );
166 }
167 }
168 $skin = new $className;
169 return $skin;
170 }
171
172 /** @return string path to the skin stylesheet */
173 function getStylesheet() {
174 return 'common/wikistandard.css';
175 }
176
177 /** @return string skin name */
178 public function getSkinName() {
179 return $this->skinname;
180 }
181
182 function initPage( OutputPage $out ) {
183 global $wgFavicon, $wgAppleTouchIcon, $wgEnableAPI;
184
185 wfProfileIn( __METHOD__ );
186
187 # Generally the order of the favicon and apple-touch-icon links
188 # should not matter, but Konqueror (3.5.9 at least) incorrectly
189 # uses whichever one appears later in the HTML source. Make sure
190 # apple-touch-icon is specified first to avoid this.
191 if ( false !== $wgAppleTouchIcon ) {
192 $out->addLink( array( 'rel' => 'apple-touch-icon', 'href' => $wgAppleTouchIcon ) );
193 }
194
195 if ( false !== $wgFavicon ) {
196 $out->addLink( array( 'rel' => 'shortcut icon', 'href' => $wgFavicon ) );
197 }
198
199 # OpenSearch description link
200 $out->addLink( array(
201 'rel' => 'search',
202 'type' => 'application/opensearchdescription+xml',
203 'href' => wfScript( 'opensearch_desc' ),
204 'title' => wfMsgForContent( 'opensearch-desc' ),
205 ) );
206
207 if ( $wgEnableAPI ) {
208 # Real Simple Discovery link, provides auto-discovery information
209 # for the MediaWiki API (and potentially additional custom API
210 # support such as WordPress or Twitter-compatible APIs for a
211 # blogging extension, etc)
212 $out->addLink( array(
213 'rel' => 'EditURI',
214 'type' => 'application/rsd+xml',
215 'href' => wfExpandUrl( wfAppendQuery( wfScript( 'api' ), array( 'action' => 'rsd' ) ) ),
216 ) );
217 }
218
219 $this->addMetadataLinks( $out );
220
221 $this->mRevisionId = $out->mRevisionId;
222
223 $this->preloadExistence();
224
225 wfProfileOut( __METHOD__ );
226 }
227
228 /**
229 * Preload the existence of three commonly-requested pages in a single query
230 */
231 function preloadExistence() {
232 global $wgUser;
233
234 // User/talk link
235 $titles = array( $wgUser->getUserPage(), $wgUser->getTalkPage() );
236
237 // Other tab link
238 if ( $this->mTitle->getNamespace() == NS_SPECIAL ) {
239 // nothing
240 } elseif ( $this->mTitle->isTalkPage() ) {
241 $titles[] = $this->mTitle->getSubjectPage();
242 } else {
243 $titles[] = $this->mTitle->getTalkPage();
244 }
245
246 $lb = new LinkBatch( $titles );
247 $lb->setCaller( __METHOD__ );
248 $lb->execute();
249 }
250
251 /**
252 * Adds metadata links below to the HTML output.
253 * <ol>
254 * <li>Creative Commons
255 * <br />See http://wiki.creativecommons.org/Extend_Metadata.
256 * </li>
257 * <li>Dublin Core</li>
258 * <li>Use hreflang to specify canonical and alternate links
259 * <br />See http://www.google.com/support/webmasters/bin/answer.py?answer=189077
260 * </li>
261 * <li>Copyright</li>
262 * <ol>
263 *
264 * @param $out Object: instance of OutputPage
265 */
266 function addMetadataLinks( OutputPage $out ) {
267 global $wgEnableDublinCoreRdf, $wgEnableCreativeCommonsRdf;
268 global $wgDisableLangConversion, $wgCanonicalLanguageLinks, $wgContLang;
269 global $wgRightsPage, $wgRightsUrl;
270
271 if ( $out->isArticleRelated() ) {
272 # note: buggy CC software only reads first "meta" link
273 if ( $wgEnableCreativeCommonsRdf ) {
274 $out->addMetadataLink( array(
275 'title' => 'Creative Commons',
276 'type' => 'application/rdf+xml',
277 'href' => $this->mTitle->getLocalURL( 'action=creativecommons' ) )
278 );
279 }
280
281 if ( $wgEnableDublinCoreRdf ) {
282 $out->addMetadataLink( array(
283 'title' => 'Dublin Core',
284 'type' => 'application/rdf+xml',
285 'href' => $this->mTitle->getLocalURL( 'action=dublincore' ) )
286 );
287 }
288 }
289
290 if ( !$wgDisableLangConversion && $wgCanonicalLanguageLinks
291 && $wgContLang->hasVariants() ) {
292
293 $urlvar = $wgContLang->getURLVariant();
294
295 if ( !$urlvar ) {
296 $variants = $wgContLang->getVariants();
297 foreach ( $variants as $_v ) {
298 $out->addLink( array(
299 'rel' => 'alternate',
300 'hreflang' => $_v,
301 'href' => $this->mTitle->getLocalURL( '', $_v ) )
302 );
303 }
304 } else {
305 $out->addLink( array(
306 'rel' => 'canonical',
307 'href' => $this->mTitle->getFullURL() )
308 );
309 }
310 }
311
312 $copyright = '';
313 if ( $wgRightsPage ) {
314 $copy = Title::newFromText( $wgRightsPage );
315
316 if ( $copy ) {
317 $copyright = $copy->getLocalURL();
318 }
319 }
320
321 if ( !$copyright && $wgRightsUrl ) {
322 $copyright = $wgRightsUrl;
323 }
324
325 if ( $copyright ) {
326 $out->addLink( array(
327 'rel' => 'copyright',
328 'href' => $copyright )
329 );
330 }
331 }
332
333 /**
334 * Set some local variables
335 */
336 protected function setMembers() {
337 global $wgUser;
338 $this->mUser = $wgUser;
339 $this->userpage = $wgUser->getUserPage()->getPrefixedText();
340 $this->usercss = false;
341 }
342
343 /**
344 * Whether the revision displayed is the latest revision of the page
345 *
346 * @return Boolean
347 */
348 public function isRevisionCurrent() {
349 return $this->mRevisionId == 0 || $this->mRevisionId == $this->mTitle->getLatestRevID();
350 }
351
352 /**
353 * Set the title
354 * @param $t Title object to use
355 */
356 public function setTitle( $t ) {
357 $this->mTitle = $t;
358 }
359
360 /** Get the title */
361 public function getTitle() {
362 return $this->mTitle;
363 }
364
365 /**
366 * Set the "relevant" title
367 * @see self::getRelevantTitle()
368 * @param $t Title object to use
369 */
370 public function setRelevantTitle( $t ) {
371 $this->mRelevantTitle = $t;
372 }
373
374 /**
375 * Return the "relevant" title.
376 * A "relevant" title is not necessarily the actual title of the page.
377 * Special pages like Special:MovePage use set the page they are acting on
378 * as their "relevant" title, this allows the skin system to display things
379 * such as content tabs which belong to to that page instead of displaying
380 * a basic special page tab which has almost no meaning.
381 */
382 public function getRelevantTitle() {
383 if ( isset($this->mRelevantTitle) ) {
384 return $this->mRelevantTitle;
385 }
386 return $this->mTitle;
387 }
388
389 /**
390 * Set the "relevant" user
391 * @see self::getRelevantUser()
392 * @param $u User object to use
393 */
394 public function setRelevantUser( $u ) {
395 $this->mRelevantUser = $u;
396 }
397
398 /**
399 * Return the "relevant" user.
400 * A "relevant" user is similar to a relevant title. Special pages like
401 * Special:Contributions mark the user which they are relevant to so that
402 * things like the toolbox can display the information they usually are only
403 * able to display on a user's userpage and talkpage.
404 */
405 public function getRelevantUser() {
406 if ( isset($this->mRelevantUser) ) {
407 return $this->mRelevantUser;
408 }
409 $title = $this->getRelevantTitle();
410 if( $title->getNamespace() == NS_USER || $title->getNamespace() == NS_USER_TALK ) {
411 $rootUser = strtok( $title->getText(), '/' );
412 if ( User::isIP( $rootUser ) ) {
413 $this->mRelevantUser = User::newFromName( $rootUser, false );
414 } else {
415 $user = User::newFromName( $rootUser );
416 if ( $user->isLoggedIn() ) {
417 $this->mRelevantUser = $user;
418 }
419 }
420 return $this->mRelevantUser;
421 }
422 return null;
423 }
424
425 /**
426 * Outputs the HTML generated by other functions.
427 * @param $out Object: instance of OutputPage
428 * @todo Exterminate!
429 */
430 function outputPage( OutputPage $out ) {
431 global $wgDebugComments;
432 wfProfileIn( __METHOD__ );
433
434 $this->setMembers();
435 $this->initPage( $out );
436
437 // See self::afterContentHook() for documentation
438 $afterContent = $this->afterContentHook();
439
440 $out->out( $out->headElement( $this ) );
441
442 if ( $wgDebugComments ) {
443 $out->out( "<!-- Debug output:\n" .
444 $out->mDebugtext . "-->\n" );
445 }
446
447 $out->out( $this->beforeContent() );
448
449 $out->out( $out->mBodytext . "\n" );
450
451 $out->out( $this->afterContent() );
452
453 $out->out( $afterContent );
454
455 $out->out( $this->bottomScripts( $out ) );
456
457 $out->out( wfReportTime() );
458
459 $out->out( "\n</body></html>" );
460 wfProfileOut( __METHOD__ );
461 }
462
463 static function makeVariablesScript( $data ) {
464 if ( $data ) {
465 return Html::inlineScript(
466 ResourceLoader::makeLoaderConditionalScript( ResourceLoader::makeConfigSetScript( $data ) )
467 );
468 } else {
469 return '';
470 }
471 }
472
473 /**
474 * To make it harder for someone to slip a user a fake
475 * user-JavaScript or user-CSS preview, a random token
476 * is associated with the login session. If it's not
477 * passed back with the preview request, we won't render
478 * the code.
479 *
480 * @param $action String: 'edit', 'submit' etc.
481 * @return bool
482 */
483 public function userCanPreview( $action ) {
484 global $wgRequest, $wgUser;
485
486 if ( $action != 'submit' ) {
487 return false;
488 }
489 if ( !$wgRequest->wasPosted() ) {
490 return false;
491 }
492 if ( !$this->mTitle->userCanEditCssSubpage() ) {
493 return false;
494 }
495 if ( !$this->mTitle->userCanEditJsSubpage() ) {
496 return false;
497 }
498
499 return $wgUser->matchEditToken(
500 $wgRequest->getVal( 'wpEditToken' ) );
501 }
502
503 /**
504 * Generated JavaScript action=raw&gen=js
505 * This returns MediaWiki:Common.js and MediaWiki:[Skinname].js concate-
506 * nated together. For some bizarre reason, it does *not* return any
507 * custom user JS from subpages. Huh?
508 *
509 * There's absolutely no reason to have separate Monobook/Common JSes.
510 * Any JS that cares can just check the skin variable generated at the
511 * top. For now Monobook.js will be maintained, but it should be consi-
512 * dered deprecated.
513 *
514 * @param $skinName String: If set, overrides the skin name
515 * @return string
516 */
517 public function generateUserJs( $skinName = null ) {
518
519 // Stub - see ResourceLoaderSiteModule, CologneBlue, Simple and Standard skins override this
520
521 return '';
522 }
523
524 /**
525 * Generate user stylesheet for action=raw&gen=css
526 */
527 public function generateUserStylesheet() {
528
529 // Stub - see ResourceLoaderUserModule, CologneBlue, Simple and Standard skins override this
530
531 return '';
532 }
533
534 /**
535 * Split for easier subclassing in SkinSimple, SkinStandard and SkinCologneBlue
536 * Anything in here won't be generated if $wgAllowUserCssPrefs is false.
537 */
538 protected function reallyGenerateUserStylesheet() {
539
540 // Stub - see ResourceLoaderUserModule, CologneBlue, Simple and Standard skins override this
541
542 return '';
543 }
544
545 /**
546 * @private
547 */
548 function setupUserCss( OutputPage $out ) {
549 global $wgRequest, $wgUser;
550 global $wgUseSiteCss, $wgAllowUserCss, $wgAllowUserCssPrefs;
551
552 wfProfileIn( __METHOD__ );
553
554 $this->setupSkinUserCss( $out );
555 // Add any extension CSS
556 foreach ( $out->getExtStyle() as $url ) {
557 $out->addStyle( $url );
558 }
559
560 // Per-site custom styles
561 if ( $wgUseSiteCss ) {
562 $out->addModuleStyles( 'site' );
563 if( $wgUser->isLoggedIn() ){
564 $out->addModuleStyles( 'user.groups' );
565 }
566 }
567
568 // Per-user custom styles
569 if ( $wgAllowUserCss ) {
570 if ( $this->mTitle->isCssSubpage() && $this->userCanPreview( $wgRequest->getVal( 'action' ) ) ) {
571 // @FIXME: properly escape the cdata!
572 $out->addInlineStyle( $wgRequest->getText( 'wpTextbox1' ) );
573 } else {
574 $out->addModuleStyles( 'user' );
575 }
576 }
577
578 // Per-user preference styles
579 if ( $wgAllowUserCssPrefs ) {
580 $out->addModuleStyles( 'user.options' );
581 }
582
583 wfProfileOut( __METHOD__ );
584 }
585
586 /**
587 * Get the query to generate a dynamic stylesheet
588 *
589 * @return array
590 */
591 public static function getDynamicStylesheetQuery() {
592 global $wgSquidMaxage;
593
594 return array(
595 'action' => 'raw',
596 'maxage' => $wgSquidMaxage,
597 'usemsgcache' => 'yes',
598 'ctype' => 'text/css',
599 'smaxage' => $wgSquidMaxage,
600 );
601 }
602
603 /**
604 * Add skin specific stylesheets
605 * @param $out OutputPage
606 * @delete
607 */
608 abstract function setupSkinUserCss( OutputPage $out );
609
610 function getPageClasses( $title ) {
611 $numeric = 'ns-' . $title->getNamespace();
612
613 if ( $title->getNamespace() == NS_SPECIAL ) {
614 $type = 'ns-special';
615 // bug 23315: provide a class based on the canonical special page name without subpages
616 list( $canonicalName ) = SpecialPage::resolveAliasWithSubpage( $title->getDBkey() );
617 if ( $canonicalName ) {
618 $type .= ' ' . Sanitizer::escapeClass( "mw-special-$canonicalName" );
619 } else {
620 $type .= ' mw-invalidspecialpage';
621 }
622 } elseif ( $title->isTalkPage() ) {
623 $type = 'ns-talk';
624 } else {
625 $type = 'ns-subject';
626 }
627
628 $name = Sanitizer::escapeClass( 'page-' . $title->getPrefixedText() );
629
630 return "$numeric $type $name";
631 }
632
633 /**
634 * This will be called by OutputPage::headElement when it is creating the
635 * <body> tag, skins can override it if they have a need to add in any
636 * body attributes or classes of their own.
637 */
638 function addToBodyAttributes( $out, &$bodyAttrs ) {
639 // does nothing by default
640 }
641
642 /**
643 * URL to the logo
644 */
645 function getLogo() {
646 global $wgLogo;
647 return $wgLogo;
648 }
649
650 function getCategoryLinks() {
651 global $wgOut, $wgUseCategoryBrowser;
652 global $wgContLang, $wgUser;
653
654 if ( count( $wgOut->mCategoryLinks ) == 0 ) {
655 return '';
656 }
657
658 # Separator
659 $sep = wfMsgExt( 'catseparator', array( 'parsemag', 'escapenoentities' ) );
660
661 // Use Unicode bidi embedding override characters,
662 // to make sure links don't smash each other up in ugly ways.
663 $dir = $wgContLang->getDir();
664 $embed = "<span dir='$dir'>";
665 $pop = '</span>';
666
667 $allCats = $wgOut->getCategoryLinks();
668 $s = '';
669 $colon = wfMsgExt( 'colon-separator', 'escapenoentities' );
670
671 if ( !empty( $allCats['normal'] ) ) {
672 $t = $embed . implode( "{$pop} {$sep} {$embed}" , $allCats['normal'] ) . $pop;
673
674 $msg = wfMsgExt( 'pagecategories', array( 'parsemag', 'escapenoentities' ), count( $allCats['normal'] ) );
675 $s .= '<div id="mw-normal-catlinks">' .
676 $this->link( Title::newFromText( wfMsgForContent( 'pagecategorieslink' ) ), $msg )
677 . $colon . $t . '</div>';
678 }
679
680 # Hidden categories
681 if ( isset( $allCats['hidden'] ) ) {
682 if ( $wgUser->getBoolOption( 'showhiddencats' ) ) {
683 $class = 'mw-hidden-cats-user-shown';
684 } elseif ( $this->mTitle->getNamespace() == NS_CATEGORY ) {
685 $class = 'mw-hidden-cats-ns-shown';
686 } else {
687 $class = 'mw-hidden-cats-hidden';
688 }
689
690 $s .= "<div id=\"mw-hidden-catlinks\" class=\"$class\">" .
691 wfMsgExt( 'hidden-categories', array( 'parsemag', 'escapenoentities' ), count( $allCats['hidden'] ) ) .
692 $colon . $embed . implode( "$pop $sep $embed", $allCats['hidden'] ) . $pop .
693 '</div>';
694 }
695
696 # optional 'dmoz-like' category browser. Will be shown under the list
697 # of categories an article belong to
698 if ( $wgUseCategoryBrowser ) {
699 $s .= '<br /><hr />';
700
701 # get a big array of the parents tree
702 $parenttree = $this->mTitle->getParentCategoryTree();
703 # Skin object passed by reference cause it can not be
704 # accessed under the method subfunction drawCategoryBrowser
705 $tempout = explode( "\n", $this->drawCategoryBrowser( $parenttree, $this ) );
706 # Clean out bogus first entry and sort them
707 unset( $tempout[0] );
708 asort( $tempout );
709 # Output one per line
710 $s .= implode( "<br />\n", $tempout );
711 }
712
713 return $s;
714 }
715
716 /**
717 * Render the array as a serie of links.
718 * @param $tree Array: categories tree returned by Title::getParentCategoryTree
719 * @param &skin Object: skin passed by reference
720 * @return String separated by &gt;, terminate with "\n"
721 */
722 function drawCategoryBrowser( $tree, &$skin ) {
723 $return = '';
724
725 foreach ( $tree as $element => $parent ) {
726 if ( empty( $parent ) ) {
727 # element start a new list
728 $return .= "\n";
729 } else {
730 # grab the others elements
731 $return .= $this->drawCategoryBrowser( $parent, $skin ) . ' &gt; ';
732 }
733
734 # add our current element to the list
735 $eltitle = Title::newFromText( $element );
736 $return .= $skin->link( $eltitle, $eltitle->getText() );
737 }
738
739 return $return;
740 }
741
742 function getCategories() {
743 $catlinks = $this->getCategoryLinks();
744
745 $classes = 'catlinks';
746
747 global $wgOut, $wgUser;
748
749 // Check what we're showing
750 $allCats = $wgOut->getCategoryLinks();
751 $showHidden = $wgUser->getBoolOption( 'showhiddencats' ) ||
752 $this->mTitle->getNamespace() == NS_CATEGORY;
753
754 if ( empty( $allCats['normal'] ) && !( !empty( $allCats['hidden'] ) && $showHidden ) ) {
755 $classes .= ' catlinks-allhidden';
756 }
757
758 return "<div id='catlinks' class='$classes'>{$catlinks}</div>";
759 }
760
761 /**
762 * This runs a hook to allow extensions placing their stuff after content
763 * and article metadata (e.g. categories).
764 * Note: This function has nothing to do with afterContent().
765 *
766 * This hook is placed here in order to allow using the same hook for all
767 * skins, both the SkinTemplate based ones and the older ones, which directly
768 * use this class to get their data.
769 *
770 * The output of this function gets processed in SkinTemplate::outputPage() for
771 * the SkinTemplate based skins, all other skins should directly echo it.
772 *
773 * Returns an empty string by default, if not changed by any hook function.
774 */
775 protected function afterContentHook() {
776 $data = '';
777
778 if ( wfRunHooks( 'SkinAfterContent', array( &$data, $this ) ) ) {
779 // adding just some spaces shouldn't toggle the output
780 // of the whole <div/>, so we use trim() here
781 if ( trim( $data ) != '' ) {
782 // Doing this here instead of in the skins to
783 // ensure that the div has the same ID in all
784 // skins
785 $data = "<div id='mw-data-after-content'>\n" .
786 "\t$data\n" .
787 "</div>\n";
788 }
789 } else {
790 wfDebug( "Hook SkinAfterContent changed output processing.\n" );
791 }
792
793 return $data;
794 }
795
796 /**
797 * Generate debug data HTML for displaying at the bottom of the main content
798 * area.
799 * @return String HTML containing debug data, if enabled (otherwise empty).
800 */
801 protected function generateDebugHTML() {
802 global $wgShowDebug, $wgOut;
803
804 if ( $wgShowDebug ) {
805 $listInternals = $this->formatDebugHTML( $wgOut->mDebugtext );
806 return "\n<hr />\n<strong>Debug data:</strong><ul style=\"font-family:monospace;\" id=\"mw-debug-html\">" .
807 $listInternals . "</ul>\n";
808 }
809
810 return '';
811 }
812
813 private function formatDebugHTML( $debugText ) {
814 $lines = explode( "\n", $debugText );
815 $curIdent = 0;
816 $ret = '<li>';
817
818 foreach ( $lines as $line ) {
819 $display = ltrim( $line );
820 $ident = strlen( $line ) - strlen( $display );
821 $diff = $ident - $curIdent;
822
823 if ( $display == '' ) {
824 $display = "\xc2\xa0";
825 }
826
827 if ( !$ident && $diff < 0 && substr( $display, 0, 9 ) != 'Entering ' && substr( $display, 0, 8 ) != 'Exiting ' ) {
828 $ident = $curIdent;
829 $diff = 0;
830 $display = '<span style="background:yellow;">' . htmlspecialchars( $display ) . '</span>';
831 } else {
832 $display = htmlspecialchars( $display );
833 }
834
835 if ( $diff < 0 ) {
836 $ret .= str_repeat( "</li></ul>\n", -$diff ) . "</li><li>\n";
837 } elseif ( $diff == 0 ) {
838 $ret .= "</li><li>\n";
839 } else {
840 $ret .= str_repeat( "<ul><li>\n", $diff );
841 }
842 $ret .= $display . "\n";
843
844 $curIdent = $ident;
845 }
846
847 $ret .= str_repeat( '</li></ul>', $curIdent ) . '</li>';
848
849 return $ret;
850 }
851
852 /**
853 * This gets called shortly before the </body> tag.
854 * @param $out OutputPage object
855 * @return String HTML-wrapped JS code to be put before </body>
856 */
857 function bottomScripts( $out ) {
858 $bottomScriptText = "\n" . $out->getHeadScripts( $this );
859 wfRunHooks( 'SkinAfterBottomScripts', array( $this, &$bottomScriptText ) );
860
861 return $bottomScriptText;
862 }
863
864 /** @return string Retrievied from HTML text */
865 function printSource() {
866 $url = htmlspecialchars( $this->mTitle->getFullURL() );
867 return wfMsg( 'retrievedfrom', '<a href="' . $url . '">' . $url . '</a>' );
868 }
869
870 function getUndeleteLink() {
871 global $wgUser, $wgLang, $wgRequest;
872
873 $action = $wgRequest->getVal( 'action', 'view' );
874
875 if ( $wgUser->isAllowed( 'deletedhistory' ) &&
876 ( $this->mTitle->getArticleId() == 0 || $action == 'history' ) ) {
877 $n = $this->mTitle->isDeleted();
878
879 if ( $n ) {
880 if ( $wgUser->isAllowed( 'undelete' ) ) {
881 $msg = 'thisisdeleted';
882 } else {
883 $msg = 'viewdeleted';
884 }
885
886 return wfMsg(
887 $msg,
888 $this->link(
889 SpecialPage::getTitleFor( 'Undelete', $this->mTitle->getPrefixedDBkey() ),
890 wfMsgExt( 'restorelink', array( 'parsemag', 'escape' ), $wgLang->formatNum( $n ) ),
891 array(),
892 array(),
893 array( 'known', 'noclasses' )
894 )
895 );
896 }
897 }
898
899 return '';
900 }
901
902 function subPageSubtitle() {
903 $subpages = '';
904
905 if ( !wfRunHooks( 'SkinSubPageSubtitle', array( &$subpages, $this ) ) ) {
906 return $subpages;
907 }
908
909 global $wgOut;
910
911 if ( $wgOut->isArticle() && MWNamespace::hasSubpages( $this->mTitle->getNamespace() ) ) {
912 $ptext = $this->mTitle->getPrefixedText();
913 if ( preg_match( '/\//', $ptext ) ) {
914 $links = explode( '/', $ptext );
915 array_pop( $links );
916 $c = 0;
917 $growinglink = '';
918 $display = '';
919
920 foreach ( $links as $link ) {
921 $growinglink .= $link;
922 $display .= $link;
923 $linkObj = Title::newFromText( $growinglink );
924
925 if ( is_object( $linkObj ) && $linkObj->exists() ) {
926 $getlink = $this->link(
927 $linkObj,
928 htmlspecialchars( $display ),
929 array(),
930 array(),
931 array( 'known', 'noclasses' )
932 );
933
934 $c++;
935
936 if ( $c > 1 ) {
937 $subpages .= wfMsgExt( 'pipe-separator', 'escapenoentities' );
938 } else {
939 $subpages .= '&lt; ';
940 }
941
942 $subpages .= $getlink;
943 $display = '';
944 } else {
945 $display .= '/';
946 }
947 $growinglink .= '/';
948 }
949 }
950 }
951
952 return $subpages;
953 }
954
955 /**
956 * Returns true if the IP should be shown in the header
957 */
958 function showIPinHeader() {
959 global $wgShowIPinHeader;
960 return $wgShowIPinHeader && session_id() != '';
961 }
962
963 function getSearchLink() {
964 $searchPage = SpecialPage::getTitleFor( 'Search' );
965 return $searchPage->getLocalURL();
966 }
967
968 function escapeSearchLink() {
969 return htmlspecialchars( $this->getSearchLink() );
970 }
971
972 function getCopyright( $type = 'detect' ) {
973 global $wgRightsPage, $wgRightsUrl, $wgRightsText, $wgRequest;
974
975 if ( $type == 'detect' ) {
976 $diff = $wgRequest->getVal( 'diff' );
977
978 if ( is_null( $diff ) && !$this->isRevisionCurrent() && wfMsgForContent( 'history_copyright' ) !== '-' ) {
979 $type = 'history';
980 } else {
981 $type = 'normal';
982 }
983 }
984
985 if ( $type == 'history' ) {
986 $msg = 'history_copyright';
987 } else {
988 $msg = 'copyright';
989 }
990
991 $out = '';
992
993 if ( $wgRightsPage ) {
994 $title = Title::newFromText( $wgRightsPage );
995 $link = $this->linkKnown( $title, $wgRightsText );
996 } elseif ( $wgRightsUrl ) {
997 $link = $this->makeExternalLink( $wgRightsUrl, $wgRightsText );
998 } elseif ( $wgRightsText ) {
999 $link = $wgRightsText;
1000 } else {
1001 # Give up now
1002 return $out;
1003 }
1004
1005 // Allow for site and per-namespace customization of copyright notice.
1006 $forContent = true;
1007
1008 wfRunHooks( 'SkinCopyrightFooter', array( $this->mTitle, $type, &$msg, &$link, &$forContent ) );
1009
1010 if ( $forContent ) {
1011 $out .= wfMsgForContent( $msg, $link );
1012 } else {
1013 $out .= wfMsg( $msg, $link );
1014 }
1015
1016 return $out;
1017 }
1018
1019 function getCopyrightIcon() {
1020 global $wgRightsUrl, $wgRightsText, $wgRightsIcon, $wgCopyrightIcon;
1021
1022 $out = '';
1023
1024 if ( isset( $wgCopyrightIcon ) && $wgCopyrightIcon ) {
1025 $out = $wgCopyrightIcon;
1026 } elseif ( $wgRightsIcon ) {
1027 $icon = htmlspecialchars( $wgRightsIcon );
1028
1029 if ( $wgRightsUrl ) {
1030 $url = htmlspecialchars( $wgRightsUrl );
1031 $out .= '<a href="' . $url . '">';
1032 }
1033
1034 $text = htmlspecialchars( $wgRightsText );
1035 $out .= "<img src=\"$icon\" alt=\"$text\" width=\"88\" height=\"31\" />";
1036
1037 if ( $wgRightsUrl ) {
1038 $out .= '</a>';
1039 }
1040 }
1041
1042 return $out;
1043 }
1044
1045 /**
1046 * Gets the powered by MediaWiki icon.
1047 * @return string
1048 */
1049 function getPoweredBy() {
1050 global $wgStylePath;
1051
1052 $url = htmlspecialchars( "$wgStylePath/common/images/poweredby_mediawiki_88x31.png" );
1053 $text = '<a href="http://www.mediawiki.org/"><img src="' . $url . '" height="31" width="88" alt="Powered by MediaWiki" /></a>';
1054 wfRunHooks( 'SkinGetPoweredBy', array( &$text, $this ) );
1055 return $text;
1056 }
1057
1058 /**
1059 * Get the timestamp of the latest revision, formatted in user language
1060 *
1061 * @param $article Article object. Used if we're working with the current revision
1062 * @return String
1063 */
1064 protected function lastModified( $article ) {
1065 global $wgLang;
1066
1067 if ( !$this->isRevisionCurrent() ) {
1068 $timestamp = Revision::getTimestampFromId( $this->mTitle, $this->mRevisionId );
1069 } else {
1070 $timestamp = $article->getTimestamp();
1071 }
1072
1073 if ( $timestamp ) {
1074 $d = $wgLang->date( $timestamp, true );
1075 $t = $wgLang->time( $timestamp, true );
1076 $s = ' ' . wfMsg( 'lastmodifiedat', $d, $t );
1077 } else {
1078 $s = '';
1079 }
1080
1081 if ( wfGetLB()->getLaggedSlaveMode() ) {
1082 $s .= ' <strong>' . wfMsg( 'laggedslavemode' ) . '</strong>';
1083 }
1084
1085 return $s;
1086 }
1087
1088 function logoText( $align = '' ) {
1089 if ( $align != '' ) {
1090 $a = " align='{$align}'";
1091 } else {
1092 $a = '';
1093 }
1094
1095 $mp = wfMsg( 'mainpage' );
1096 $mptitle = Title::newMainPage();
1097 $url = ( is_object( $mptitle ) ? $mptitle->escapeLocalURL() : '' );
1098
1099 $logourl = $this->getLogo();
1100 $s = "<a href='{$url}'><img{$a} src='{$logourl}' alt='[{$mp}]' /></a>";
1101
1102 return $s;
1103 }
1104
1105 /**
1106 * Renders a $wgFooterIcons icon acording to the method's arguments
1107 * @param $icon Array: The icon to build the html for, see $wgFooterIcons for the format of this array
1108 * @param $withImage Boolean: Whether to use the icon's image or output a text-only footericon
1109 */
1110 function makeFooterIcon( $icon, $withImage = 'withImage' ) {
1111 if ( is_string( $icon ) ) {
1112 $html = $icon;
1113 } else { // Assuming array
1114 $url = isset($icon["url"]) ? $icon["url"] : null;
1115 unset( $icon["url"] );
1116 if ( isset( $icon["src"] ) && $withImage === 'withImage' ) {
1117 $html = Html::element( 'img', $icon ); // do this the lazy way, just pass icon data as an attribute array
1118 } else {
1119 $html = htmlspecialchars( $icon["alt"] );
1120 }
1121 if ( $url ) {
1122 $html = Html::rawElement( 'a', array( "href" => $url ), $html );
1123 }
1124 }
1125 return $html;
1126 }
1127
1128 /**
1129 * Gets the link to the wiki's main page.
1130 * @return string
1131 */
1132 function mainPageLink() {
1133 $s = $this->link(
1134 Title::newMainPage(),
1135 wfMsg( 'mainpage' ),
1136 array(),
1137 array(),
1138 array( 'known', 'noclasses' )
1139 );
1140
1141 return $s;
1142 }
1143
1144 public function footerLink( $desc, $page ) {
1145 // if the link description has been set to "-" in the default language,
1146 if ( wfMsgForContent( $desc ) == '-' ) {
1147 // then it is disabled, for all languages.
1148 return '';
1149 } else {
1150 // Otherwise, we display the link for the user, described in their
1151 // language (which may or may not be the same as the default language),
1152 // but we make the link target be the one site-wide page.
1153 $title = Title::newFromText( wfMsgForContent( $page ) );
1154
1155 return $this->linkKnown(
1156 $title,
1157 wfMsgExt( $desc, array( 'parsemag', 'escapenoentities' ) )
1158 );
1159 }
1160 }
1161
1162 /**
1163 * Gets the link to the wiki's privacy policy page.
1164 */
1165 function privacyLink() {
1166 return $this->footerLink( 'privacy', 'privacypage' );
1167 }
1168
1169 /**
1170 * Gets the link to the wiki's about page.
1171 */
1172 function aboutLink() {
1173 return $this->footerLink( 'aboutsite', 'aboutpage' );
1174 }
1175
1176 /**
1177 * Gets the link to the wiki's general disclaimers page.
1178 */
1179 function disclaimerLink() {
1180 return $this->footerLink( 'disclaimers', 'disclaimerpage' );
1181 }
1182
1183 /**
1184 * Return URL options for the 'edit page' link.
1185 * This may include an 'oldid' specifier, if the current page view is such.
1186 *
1187 * @return array
1188 * @private
1189 */
1190 function editUrlOptions() {
1191 $options = array( 'action' => 'edit' );
1192
1193 if ( !$this->isRevisionCurrent() ) {
1194 $options['oldid'] = intval( $this->mRevisionId );
1195 }
1196
1197 return $options;
1198 }
1199
1200 function showEmailUser( $id ) {
1201 global $wgUser;
1202 $targetUser = User::newFromId( $id );
1203 return $wgUser->canSendEmail() && # the sending user must have a confirmed email address
1204 $targetUser->canReceiveEmail(); # the target user must have a confirmed email address and allow emails from users
1205 }
1206
1207 /**
1208 * Return a fully resolved style path url to images or styles stored in the common folder.
1209 * This method returns a url resolved using the configured skin style path
1210 * and includes the style version inside of the url.
1211 * @param $name String: The name or path of a skin resource file
1212 * @return String The fully resolved style path url including styleversion
1213 */
1214 function getCommonStylePath( $name ) {
1215 global $wgStylePath, $wgStyleVersion;
1216 return "$wgStylePath/common/$name?$wgStyleVersion";
1217 }
1218
1219 /**
1220 * Return a fully resolved style path url to images or styles stored in the curent skins's folder.
1221 * This method returns a url resolved using the configured skin style path
1222 * and includes the style version inside of the url.
1223 * @param $name String: The name or path of a skin resource file
1224 * @return String The fully resolved style path url including styleversion
1225 */
1226 function getSkinStylePath( $name ) {
1227 global $wgStylePath, $wgStyleVersion;
1228 return "$wgStylePath/{$this->stylename}/$name?$wgStyleVersion";
1229 }
1230
1231 /* these are used extensively in SkinTemplate, but also some other places */
1232 static function makeMainPageUrl( $urlaction = '' ) {
1233 $title = Title::newMainPage();
1234 self::checkTitle( $title, '' );
1235
1236 return $title->getLocalURL( $urlaction );
1237 }
1238
1239 static function makeSpecialUrl( $name, $urlaction = '' ) {
1240 $title = SpecialPage::getTitleFor( $name );
1241 return $title->getLocalURL( $urlaction );
1242 }
1243
1244 static function makeSpecialUrlSubpage( $name, $subpage, $urlaction = '' ) {
1245 $title = SpecialPage::getSafeTitleFor( $name, $subpage );
1246 return $title->getLocalURL( $urlaction );
1247 }
1248
1249 static function makeI18nUrl( $name, $urlaction = '' ) {
1250 $title = Title::newFromText( wfMsgForContent( $name ) );
1251 self::checkTitle( $title, $name );
1252 return $title->getLocalURL( $urlaction );
1253 }
1254
1255 static function makeUrl( $name, $urlaction = '' ) {
1256 $title = Title::newFromText( $name );
1257 self::checkTitle( $title, $name );
1258
1259 return $title->getLocalURL( $urlaction );
1260 }
1261
1262 /**
1263 * If url string starts with http, consider as external URL, else
1264 * internal
1265 */
1266 static function makeInternalOrExternalUrl( $name ) {
1267 if ( preg_match( '/^(?:' . wfUrlProtocols() . ')/', $name ) ) {
1268 return $name;
1269 } else {
1270 return self::makeUrl( $name );
1271 }
1272 }
1273
1274 # this can be passed the NS number as defined in Language.php
1275 static function makeNSUrl( $name, $urlaction = '', $namespace = NS_MAIN ) {
1276 $title = Title::makeTitleSafe( $namespace, $name );
1277 self::checkTitle( $title, $name );
1278
1279 return $title->getLocalURL( $urlaction );
1280 }
1281
1282 /* these return an array with the 'href' and boolean 'exists' */
1283 static function makeUrlDetails( $name, $urlaction = '' ) {
1284 $title = Title::newFromText( $name );
1285 self::checkTitle( $title, $name );
1286
1287 return array(
1288 'href' => $title->getLocalURL( $urlaction ),
1289 'exists' => $title->getArticleID() != 0,
1290 );
1291 }
1292
1293 /**
1294 * Make URL details where the article exists (or at least it's convenient to think so)
1295 */
1296 static function makeKnownUrlDetails( $name, $urlaction = '' ) {
1297 $title = Title::newFromText( $name );
1298 self::checkTitle( $title, $name );
1299
1300 return array(
1301 'href' => $title->getLocalURL( $urlaction ),
1302 'exists' => true
1303 );
1304 }
1305
1306 # make sure we have some title to operate on
1307 static function checkTitle( &$title, $name ) {
1308 if ( !is_object( $title ) ) {
1309 $title = Title::newFromText( $name );
1310 if ( !is_object( $title ) ) {
1311 $title = Title::newFromText( '--error: link target missing--' );
1312 }
1313 }
1314 }
1315
1316 /**
1317 * Build an array that represents the sidebar(s), the navigation bar among them
1318 *
1319 * @return array
1320 */
1321 function buildSidebar() {
1322 global $parserMemc, $wgEnableSidebarCache, $wgSidebarCacheExpiry;
1323 global $wgLang;
1324 wfProfileIn( __METHOD__ );
1325
1326 $key = wfMemcKey( 'sidebar', $wgLang->getCode() );
1327
1328 if ( $wgEnableSidebarCache ) {
1329 $cachedsidebar = $parserMemc->get( $key );
1330 if ( $cachedsidebar ) {
1331 wfProfileOut( __METHOD__ );
1332 return $cachedsidebar;
1333 }
1334 }
1335
1336 $bar = array();
1337 $this->addToSidebar( $bar, 'sidebar' );
1338
1339 wfRunHooks( 'SkinBuildSidebar', array( $this, &$bar ) );
1340 if ( $wgEnableSidebarCache ) {
1341 $parserMemc->set( $key, $bar, $wgSidebarCacheExpiry );
1342 }
1343
1344 wfProfileOut( __METHOD__ );
1345 return $bar;
1346 }
1347 /**
1348 * Add content from a sidebar system message
1349 * Currently only used for MediaWiki:Sidebar (but may be used by Extensions)
1350 *
1351 * This is just a wrapper around addToSidebarPlain() for backwards compatibility
1352 *
1353 * @param &$bar array
1354 * @param $message String
1355 */
1356 function addToSidebar( &$bar, $message ) {
1357 $this->addToSidebarPlain( $bar, wfMsgForContent( $message ) );
1358 }
1359
1360 /**
1361 * Add content from plain text
1362 * @since 1.17
1363 * @param &$bar array
1364 * @param $text string
1365 */
1366 function addToSidebarPlain( &$bar, $text ) {
1367 $lines = explode( "\n", $text );
1368 $wikiBar = array(); # We need to handle the wikitext on a different variable, to avoid trying to do an array operation on text, which would be a fatal error.
1369
1370 $heading = '';
1371
1372 foreach ( $lines as $line ) {
1373 if ( strpos( $line, '*' ) !== 0 ) {
1374 continue;
1375 }
1376
1377 if ( strpos( $line, '**' ) !== 0 ) {
1378 $heading = trim( $line, '* ' );
1379 if ( !array_key_exists( $heading, $bar ) ) {
1380 $bar[$heading] = array();
1381 }
1382 } else {
1383 $line = trim( $line, '* ' );
1384
1385 if ( strpos( $line, '|' ) !== false ) { // sanity check
1386 $line = array_map( 'trim', explode( '|', $line, 2 ) );
1387 $link = wfMsgForContent( $line[0] );
1388
1389 if ( $link == '-' ) {
1390 continue;
1391 }
1392
1393 $text = wfMsgExt( $line[1], 'parsemag' );
1394
1395 if ( wfEmptyMsg( $line[1], $text ) ) {
1396 $text = $line[1];
1397 }
1398
1399 if ( wfEmptyMsg( $line[0], $link ) ) {
1400 $link = $line[0];
1401 }
1402
1403 if ( preg_match( '/^(?:' . wfUrlProtocols() . ')/', $link ) ) {
1404 $href = $link;
1405 } else {
1406 $title = Title::newFromText( $link );
1407
1408 if ( $title ) {
1409 $title = $title->fixSpecialName();
1410 $href = $title->getLocalURL();
1411 } else {
1412 $href = 'INVALID-TITLE';
1413 }
1414 }
1415
1416 $bar[$heading][] = array(
1417 'text' => $text,
1418 'href' => $href,
1419 'id' => 'n-' . strtr( $line[1], ' ', '-' ),
1420 'active' => false
1421 );
1422 } else if ( ( substr( $line, 0, 2 ) == '{{' ) && ( substr( $line, -2 ) == '}}' ) ) {
1423 global $wgParser, $wgTitle;
1424
1425 $line = substr( $line, 2, strlen( $line ) - 4 );
1426
1427 $options = new ParserOptions();
1428 $options->setEditSection( false );
1429 $options->setInterfaceMessage( true );
1430 $wikiBar[$heading] = $wgParser->parse( wfMsgForContentNoTrans( $line ) , $wgTitle, $options )->getText();
1431 } else {
1432 continue;
1433 }
1434 }
1435 }
1436
1437 if ( count( $wikiBar ) > 0 ) {
1438 $bar = array_merge( $bar, $wikiBar );
1439 }
1440
1441 return $bar;
1442 }
1443
1444 /**
1445 * Should we include common/wikiprintable.css? Skins that have their own
1446 * print stylesheet should override this and return false. (This is an
1447 * ugly hack to get Monobook to play nicely with
1448 * OutputPage::headElement().)
1449 *
1450 * @return bool
1451 */
1452 public function commonPrintStylesheet() {
1453 return true;
1454 }
1455
1456 /**
1457 * Gets new talk page messages for the current user.
1458 * @return MediaWiki message or if no new talk page messages, nothing
1459 */
1460 function getNewtalks() {
1461 global $wgUser, $wgOut;
1462
1463 $newtalks = $wgUser->getNewMessageLinks();
1464 $ntl = '';
1465
1466 if ( count( $newtalks ) == 1 && $newtalks[0]['wiki'] === wfWikiID() ) {
1467 $userTitle = $this->mUser->getUserPage();
1468 $userTalkTitle = $userTitle->getTalkPage();
1469
1470 if ( !$userTalkTitle->equals( $this->mTitle ) ) {
1471 $newMessagesLink = $this->link(
1472 $userTalkTitle,
1473 wfMsgHtml( 'newmessageslink' ),
1474 array(),
1475 array( 'redirect' => 'no' ),
1476 array( 'known', 'noclasses' )
1477 );
1478
1479 $newMessagesDiffLink = $this->link(
1480 $userTalkTitle,
1481 wfMsgHtml( 'newmessagesdifflink' ),
1482 array(),
1483 array( 'diff' => 'cur' ),
1484 array( 'known', 'noclasses' )
1485 );
1486
1487 $ntl = wfMsg(
1488 'youhavenewmessages',
1489 $newMessagesLink,
1490 $newMessagesDiffLink
1491 );
1492 # Disable Squid cache
1493 $wgOut->setSquidMaxage( 0 );
1494 }
1495 } elseif ( count( $newtalks ) ) {
1496 // _>" " for BC <= 1.16
1497 $sep = str_replace( '_', ' ', wfMsgHtml( 'newtalkseparator' ) );
1498 $msgs = array();
1499
1500 foreach ( $newtalks as $newtalk ) {
1501 $msgs[] = Xml::element(
1502 'a',
1503 array( 'href' => $newtalk['link'] ), $newtalk['wiki']
1504 );
1505 }
1506 $parts = implode( $sep, $msgs );
1507 $ntl = wfMsgHtml( 'youhavenewmessagesmulti', $parts );
1508 $wgOut->setSquidMaxage( 0 );
1509 }
1510
1511 return $ntl;
1512 }
1513
1514 /**
1515 * Get a cached notice
1516 *
1517 * @param $name String: message name, or 'default' for $wgSiteNotice
1518 * @return String: HTML fragment
1519 */
1520 private function getCachedNotice( $name ) {
1521 global $wgOut, $wgRenderHashAppend, $parserMemc;
1522
1523 wfProfileIn( __METHOD__ );
1524
1525 $needParse = false;
1526
1527 if( $name === 'default' ) {
1528 // special case
1529 global $wgSiteNotice;
1530 $notice = $wgSiteNotice;
1531 if( empty( $notice ) ) {
1532 wfProfileOut( __METHOD__ );
1533 return false;
1534 }
1535 } else {
1536 $msg = wfMessage( $name )->inContentLanguage();
1537 if( $msg->isDisabled() ) {
1538 wfProfileOut( __METHOD__ );
1539 return false;
1540 }
1541 $notice = $msg->plain();
1542 }
1543
1544 // Use the extra hash appender to let eg SSL variants separately cache.
1545 $key = wfMemcKey( $name . $wgRenderHashAppend );
1546 $cachedNotice = $parserMemc->get( $key );
1547 if( is_array( $cachedNotice ) ) {
1548 if( md5( $notice ) == $cachedNotice['hash'] ) {
1549 $notice = $cachedNotice['html'];
1550 } else {
1551 $needParse = true;
1552 }
1553 } else {
1554 $needParse = true;
1555 }
1556
1557 if ( $needParse ) {
1558 if( is_object( $wgOut ) ) {
1559 $parsed = $wgOut->parse( $notice );
1560 $parserMemc->set( $key, array( 'html' => $parsed, 'hash' => md5( $notice ) ), 600 );
1561 $notice = $parsed;
1562 } else {
1563 wfDebug( 'wfGetCachedNotice called for ' . $name . ' with no $wgOut available' . "\n" );
1564 $notice = '';
1565 }
1566 }
1567
1568 $notice = '<div id="localNotice">' .$notice . '</div>';
1569 wfProfileOut( __METHOD__ );
1570 return $notice;
1571 }
1572
1573 /**
1574 * Get a notice based on page's namespace
1575 *
1576 * @return String: HTML fragment
1577 */
1578 function getNamespaceNotice() {
1579 wfProfileIn( __METHOD__ );
1580
1581 $key = 'namespacenotice-' . $this->mTitle->getNsText();
1582 $namespaceNotice = wfGetCachedNotice( $key );
1583 if ( $namespaceNotice && substr( $namespaceNotice, 0, 7 ) != '<p>&lt;' ) {
1584 $namespaceNotice = '<div id="namespacebanner">' . $namespaceNotice . '</div>';
1585 } else {
1586 $namespaceNotice = '';
1587 }
1588
1589 wfProfileOut( $fname );
1590 return $namespaceNotice;
1591 }
1592
1593 /**
1594 * Get the site notice
1595 *
1596 * @return String: HTML fragment
1597 */
1598 function getSiteNotice() {
1599 global $wgUser;
1600
1601 wfProfileIn( __METHOD__ );
1602 $siteNotice = '';
1603
1604 if ( wfRunHooks( 'SiteNoticeBefore', array( &$siteNotice, $this ) ) ) {
1605 if ( is_object( $wgUser ) && $wgUser->isLoggedIn() ) {
1606 $siteNotice = $this->getCachedNotice( 'sitenotice' );
1607 } else {
1608 $anonNotice = $this->getCachedNotice( 'anonnotice' );
1609 if ( !$anonNotice ) {
1610 $siteNotice = $this->getCachedNotice( 'sitenotice' );
1611 } else {
1612 $siteNotice = $anonNotice;
1613 }
1614 }
1615 if ( !$siteNotice ) {
1616 $siteNotice = $this->getCachedNotice( 'default' );
1617 }
1618 }
1619
1620 wfRunHooks( 'SiteNoticeAfter', array( &$siteNotice, $this ) );
1621 wfProfileOut( __METHOD__ );
1622 return $siteNotice;
1623 }
1624 }