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