Follow-up r83336, r83460: abandon hardcoded styles altogether, and instead use a...
[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 *
365 * @return Title
366 */
367 public function getTitle() {
368 return $this->mTitle;
369 }
370
371 /**
372 * Set the "relevant" title
373 * @see self::getRelevantTitle()
374 * @param $t Title object to use
375 */
376 public function setRelevantTitle( $t ) {
377 $this->mRelevantTitle = $t;
378 }
379
380 /**
381 * Return the "relevant" title.
382 * A "relevant" title is not necessarily the actual title of the page.
383 * Special pages like Special:MovePage use set the page they are acting on
384 * as their "relevant" title, this allows the skin system to display things
385 * such as content tabs which belong to to that page instead of displaying
386 * a basic special page tab which has almost no meaning.
387 */
388 public function getRelevantTitle() {
389 if ( isset($this->mRelevantTitle) ) {
390 return $this->mRelevantTitle;
391 }
392 return $this->mTitle;
393 }
394
395 /**
396 * Set the "relevant" user
397 * @see self::getRelevantUser()
398 * @param $u User object to use
399 */
400 public function setRelevantUser( $u ) {
401 $this->mRelevantUser = $u;
402 }
403
404 /**
405 * Return the "relevant" user.
406 * A "relevant" user is similar to a relevant title. Special pages like
407 * Special:Contributions mark the user which they are relevant to so that
408 * things like the toolbox can display the information they usually are only
409 * able to display on a user's userpage and talkpage.
410 */
411 public function getRelevantUser() {
412 if ( isset($this->mRelevantUser) ) {
413 return $this->mRelevantUser;
414 }
415 $title = $this->getRelevantTitle();
416 if( $title->getNamespace() == NS_USER || $title->getNamespace() == NS_USER_TALK ) {
417 $rootUser = strtok( $title->getText(), '/' );
418 if ( User::isIP( $rootUser ) ) {
419 $this->mRelevantUser = User::newFromName( $rootUser, false );
420 } else {
421 $user = User::newFromName( $rootUser );
422 if ( $user->isLoggedIn() ) {
423 $this->mRelevantUser = $user;
424 }
425 }
426 return $this->mRelevantUser;
427 }
428 return null;
429 }
430
431 /**
432 * Outputs the HTML generated by other functions.
433 * @param $out Object: instance of OutputPage
434 * @todo Exterminate!
435 */
436 function outputPage( OutputPage $out ) {
437 global $wgDebugComments;
438 wfProfileIn( __METHOD__ );
439
440 $this->setMembers();
441 $this->initPage( $out );
442
443 // See self::afterContentHook() for documentation
444 $afterContent = $this->afterContentHook();
445
446 $out->out( $out->headElement( $this ) );
447
448 if ( $wgDebugComments ) {
449 $out->out( "<!-- Debug output:\n" .
450 $out->mDebugtext . "-->\n" );
451 }
452
453 $out->out( $this->beforeContent() );
454
455 $out->out( $out->mBodytext . "\n" );
456
457 $out->out( $this->afterContent() );
458
459 $out->out( $afterContent );
460
461 $out->out( $this->bottomScripts( $out ) );
462
463 $out->out( wfReportTime() );
464
465 $out->out( "\n</body></html>" );
466 wfProfileOut( __METHOD__ );
467 }
468
469 static function makeVariablesScript( $data ) {
470 if ( $data ) {
471 return Html::inlineScript(
472 ResourceLoader::makeLoaderConditionalScript( ResourceLoader::makeConfigSetScript( $data ) )
473 );
474 } else {
475 return '';
476 }
477 }
478
479 /**
480 * To make it harder for someone to slip a user a fake
481 * user-JavaScript or user-CSS preview, a random token
482 * is associated with the login session. If it's not
483 * passed back with the preview request, we won't render
484 * the code.
485 *
486 * @param $action String: 'edit', 'submit' etc.
487 * @return bool
488 */
489 public function userCanPreview( $action ) {
490 global $wgRequest, $wgUser;
491
492 if ( $action != 'submit' ) {
493 return false;
494 }
495 if ( !$wgRequest->wasPosted() ) {
496 return false;
497 }
498 if ( !$this->mTitle->userCanEditCssSubpage() ) {
499 return false;
500 }
501 if ( !$this->mTitle->userCanEditJsSubpage() ) {
502 return false;
503 }
504
505 return $wgUser->matchEditToken(
506 $wgRequest->getVal( 'wpEditToken' ) );
507 }
508
509 /**
510 * Generated JavaScript action=raw&gen=js
511 * This returns MediaWiki:Common.js and MediaWiki:[Skinname].js concate-
512 * nated together. For some bizarre reason, it does *not* return any
513 * custom user JS from subpages. Huh?
514 *
515 * There's absolutely no reason to have separate Monobook/Common JSes.
516 * Any JS that cares can just check the skin variable generated at the
517 * top. For now Monobook.js will be maintained, but it should be consi-
518 * dered deprecated.
519 *
520 * @param $skinName String: If set, overrides the skin name
521 * @return string
522 */
523 public function generateUserJs( $skinName = null ) {
524
525 // Stub - see ResourceLoaderSiteModule, CologneBlue, Simple and Standard skins override this
526
527 return '';
528 }
529
530 /**
531 * Generate user stylesheet for action=raw&gen=css
532 */
533 public function generateUserStylesheet() {
534
535 // Stub - see ResourceLoaderUserModule, CologneBlue, Simple and Standard skins override this
536
537 return '';
538 }
539
540 /**
541 * Split for easier subclassing in SkinSimple, SkinStandard and SkinCologneBlue
542 * Anything in here won't be generated if $wgAllowUserCssPrefs is false.
543 */
544 protected function reallyGenerateUserStylesheet() {
545
546 // Stub - see ResourceLoaderUserModule, CologneBlue, Simple and Standard skins override this
547
548 return '';
549 }
550
551 /**
552 * @private
553 */
554 function setupUserCss( OutputPage $out ) {
555 global $wgRequest, $wgUser;
556 global $wgUseSiteCss, $wgAllowUserCss, $wgAllowUserCssPrefs;
557
558 wfProfileIn( __METHOD__ );
559
560 $this->setupSkinUserCss( $out );
561 // Add any extension CSS
562 foreach ( $out->getExtStyle() as $url ) {
563 $out->addStyle( $url );
564 }
565
566 // Per-site custom styles
567 if ( $wgUseSiteCss ) {
568 $out->addModuleStyles( array( 'site', 'noscript' ) );
569 if( $wgUser->isLoggedIn() ){
570 $out->addModuleStyles( 'user.groups' );
571 }
572 }
573
574 // Per-user custom styles
575 if ( $wgAllowUserCss ) {
576 if ( $this->mTitle->isCssSubpage() && $this->userCanPreview( $wgRequest->getVal( 'action' ) ) ) {
577 // @FIXME: properly escape the cdata!
578 $out->addInlineStyle( $wgRequest->getText( 'wpTextbox1' ) );
579 } else {
580 $out->addModuleStyles( 'user' );
581 }
582 }
583
584 // Per-user preference styles
585 if ( $wgAllowUserCssPrefs ) {
586 $out->addModuleStyles( 'user.options' );
587 }
588
589 wfProfileOut( __METHOD__ );
590 }
591
592 /**
593 * Get the query to generate a dynamic stylesheet
594 *
595 * @return array
596 */
597 public static function getDynamicStylesheetQuery() {
598 global $wgSquidMaxage;
599
600 return array(
601 'action' => 'raw',
602 'maxage' => $wgSquidMaxage,
603 'usemsgcache' => 'yes',
604 'ctype' => 'text/css',
605 'smaxage' => $wgSquidMaxage,
606 );
607 }
608
609 /**
610 * Add skin specific stylesheets
611 * @param $out OutputPage
612 * @delete
613 */
614 abstract function setupSkinUserCss( OutputPage $out );
615
616 function getPageClasses( $title ) {
617 $numeric = 'ns-' . $title->getNamespace();
618
619 if ( $title->getNamespace() == NS_SPECIAL ) {
620 $type = 'ns-special';
621 // bug 23315: provide a class based on the canonical special page name without subpages
622 list( $canonicalName ) = SpecialPage::resolveAliasWithSubpage( $title->getDBkey() );
623 if ( $canonicalName ) {
624 $type .= ' ' . Sanitizer::escapeClass( "mw-special-$canonicalName" );
625 } else {
626 $type .= ' mw-invalidspecialpage';
627 }
628 } elseif ( $title->isTalkPage() ) {
629 $type = 'ns-talk';
630 } else {
631 $type = 'ns-subject';
632 }
633
634 $name = Sanitizer::escapeClass( 'page-' . $title->getPrefixedText() );
635
636 return "$numeric $type $name";
637 }
638
639 /**
640 * This will be called by OutputPage::headElement when it is creating the
641 * <body> tag, skins can override it if they have a need to add in any
642 * body attributes or classes of their own.
643 */
644 function addToBodyAttributes( $out, &$bodyAttrs ) {
645 // does nothing by default
646 }
647
648 /**
649 * URL to the logo
650 */
651 function getLogo() {
652 global $wgLogo;
653 return $wgLogo;
654 }
655
656 /**
657 * The format without an explicit $out argument is deprecated
658 */
659 function getCategoryLinks( OutputPage $out=null ) {
660 global $wgUseCategoryBrowser, $wgContLang, $wgUser;
661
662 if ( is_null( $out ) ) {
663 // Backwards compatibility for when there was no $out arg
664 global $wgOut;
665 $out = $wgOut;
666 }
667
668 if ( count( $out->mCategoryLinks ) == 0 ) {
669 return '';
670 }
671
672 # Separator
673 $sep = wfMsgExt( 'catseparator', array( 'parsemag', 'escapenoentities' ) );
674
675 // Use Unicode bidi embedding override characters,
676 // to make sure links don't smash each other up in ugly ways.
677 $dir = $wgContLang->getDir();
678 $embed = "<span dir='$dir'>";
679 $pop = '</span>';
680
681 $allCats = $out->getCategoryLinks();
682 $s = '';
683 $colon = wfMsgExt( 'colon-separator', 'escapenoentities' );
684
685 if ( !empty( $allCats['normal'] ) ) {
686 $t = $embed . implode( "{$pop} {$sep} {$embed}" , $allCats['normal'] ) . $pop;
687
688 $msg = wfMsgExt( 'pagecategories', array( 'parsemag', 'escapenoentities' ), count( $allCats['normal'] ) );
689 $s .= '<div id="mw-normal-catlinks">' .
690 $this->link( Title::newFromText( wfMsgForContent( 'pagecategorieslink' ) ), $msg )
691 . $colon . $t . '</div>';
692 }
693
694 # Hidden categories
695 if ( isset( $allCats['hidden'] ) ) {
696 if ( $wgUser->getBoolOption( 'showhiddencats' ) ) {
697 $class = 'mw-hidden-cats-user-shown';
698 } elseif ( $this->mTitle->getNamespace() == NS_CATEGORY ) {
699 $class = 'mw-hidden-cats-ns-shown';
700 } else {
701 $class = 'mw-hidden-cats-hidden';
702 }
703
704 $s .= "<div id=\"mw-hidden-catlinks\" class=\"$class\">" .
705 wfMsgExt( 'hidden-categories', array( 'parsemag', 'escapenoentities' ), count( $allCats['hidden'] ) ) .
706 $colon . $embed . implode( "$pop $sep $embed", $allCats['hidden'] ) . $pop .
707 '</div>';
708 }
709
710 # optional 'dmoz-like' category browser. Will be shown under the list
711 # of categories an article belong to
712 if ( $wgUseCategoryBrowser ) {
713 $s .= '<br /><hr />';
714
715 # get a big array of the parents tree
716 $parenttree = $this->mTitle->getParentCategoryTree();
717 # Skin object passed by reference cause it can not be
718 # accessed under the method subfunction drawCategoryBrowser
719 $tempout = explode( "\n", $this->drawCategoryBrowser( $parenttree, $this ) );
720 # Clean out bogus first entry and sort them
721 unset( $tempout[0] );
722 asort( $tempout );
723 # Output one per line
724 $s .= implode( "<br />\n", $tempout );
725 }
726
727 return $s;
728 }
729
730 /**
731 * Render the array as a serie of links.
732 * @param $tree Array: categories tree returned by Title::getParentCategoryTree
733 * @param &skin Object: skin passed by reference
734 * @return String separated by &gt;, terminate with "\n"
735 */
736 function drawCategoryBrowser( $tree, &$skin ) {
737 $return = '';
738
739 foreach ( $tree as $element => $parent ) {
740 if ( empty( $parent ) ) {
741 # element start a new list
742 $return .= "\n";
743 } else {
744 # grab the others elements
745 $return .= $this->drawCategoryBrowser( $parent, $skin ) . ' &gt; ';
746 }
747
748 # add our current element to the list
749 $eltitle = Title::newFromText( $element );
750 $return .= $skin->link( $eltitle, $eltitle->getText() );
751 }
752
753 return $return;
754 }
755
756 /**
757 * The ->getCategories() form is deprecated, please instead use
758 * the ->getCategories( $out ) form with whatout OutputPage is on hand
759 */
760 function getCategories( OutputPage $out=null ) {
761 if ( is_null( $out ) ) {
762 // Backwards compatibility for when there was no $out arg
763 global $wgOut;
764 $out = $wgOut;
765 }
766
767 $catlinks = $this->getCategoryLinks( $out );
768
769 $classes = 'catlinks';
770
771 global $wgUser;
772
773 // Check what we're showing
774 $allCats = $out->getCategoryLinks();
775 $showHidden = $wgUser->getBoolOption( 'showhiddencats' ) ||
776 $this->mTitle->getNamespace() == NS_CATEGORY;
777
778 if ( empty( $allCats['normal'] ) && !( !empty( $allCats['hidden'] ) && $showHidden ) ) {
779 $classes .= ' catlinks-allhidden';
780 }
781
782 return "<div id='catlinks' class='$classes'>{$catlinks}</div>";
783 }
784
785 /**
786 * This runs a hook to allow extensions placing their stuff after content
787 * and article metadata (e.g. categories).
788 * Note: This function has nothing to do with afterContent().
789 *
790 * This hook is placed here in order to allow using the same hook for all
791 * skins, both the SkinTemplate based ones and the older ones, which directly
792 * use this class to get their data.
793 *
794 * The output of this function gets processed in SkinTemplate::outputPage() for
795 * the SkinTemplate based skins, all other skins should directly echo it.
796 *
797 * Returns an empty string by default, if not changed by any hook function.
798 */
799 protected function afterContentHook() {
800 $data = '';
801
802 if ( wfRunHooks( 'SkinAfterContent', array( &$data, $this ) ) ) {
803 // adding just some spaces shouldn't toggle the output
804 // of the whole <div/>, so we use trim() here
805 if ( trim( $data ) != '' ) {
806 // Doing this here instead of in the skins to
807 // ensure that the div has the same ID in all
808 // skins
809 $data = "<div id='mw-data-after-content'>\n" .
810 "\t$data\n" .
811 "</div>\n";
812 }
813 } else {
814 wfDebug( "Hook SkinAfterContent changed output processing.\n" );
815 }
816
817 return $data;
818 }
819
820 /**
821 * Generate debug data HTML for displaying at the bottom of the main content
822 * area.
823 * @return String HTML containing debug data, if enabled (otherwise empty).
824 */
825 protected function generateDebugHTML( OutputPage $out ) {
826 global $wgShowDebug;
827
828 if ( $wgShowDebug ) {
829 $listInternals = $this->formatDebugHTML( $out->mDebugtext );
830 return "\n<hr />\n<strong>Debug data:</strong><ul id=\"mw-debug-html\">" .
831 $listInternals . "</ul>\n";
832 }
833
834 return '';
835 }
836
837 private function formatDebugHTML( $debugText ) {
838 global $wgDebugTimestamps;
839
840 $lines = explode( "\n", $debugText );
841 $curIdent = 0;
842 $ret = '<li>';
843
844 foreach ( $lines as $line ) {
845 $pre = '';
846 if ( $wgDebugTimestamps ) {
847 $matches = array();
848 if ( preg_match( '/^(\d+\.\d+\s{2})/', $line, $matches ) ) {
849 $pre = $matches[1];
850 $line = substr( $line, strlen( $pre ) );
851 }
852 }
853 $display = ltrim( $line );
854 $ident = strlen( $line ) - strlen( $display );
855 $diff = $ident - $curIdent;
856
857 $display = $pre . $display;
858 if ( $display == '' ) {
859 $display = "\xc2\xa0";
860 }
861
862 if ( !$ident && $diff < 0 && substr( $display, 0, 9 ) != 'Entering ' && substr( $display, 0, 8 ) != 'Exiting ' ) {
863 $ident = $curIdent;
864 $diff = 0;
865 $display = '<span style="background:yellow;">' . htmlspecialchars( $display ) . '</span>';
866 } else {
867 $display = htmlspecialchars( $display );
868 }
869
870 if ( $diff < 0 ) {
871 $ret .= str_repeat( "</li></ul>\n", -$diff ) . "</li><li>\n";
872 } elseif ( $diff == 0 ) {
873 $ret .= "</li><li>\n";
874 } else {
875 $ret .= str_repeat( "<ul><li>\n", $diff );
876 }
877 $ret .= "<tt>$display</tt>\n";
878
879 $curIdent = $ident;
880 }
881
882 $ret .= str_repeat( '</li></ul>', $curIdent ) . '</li>';
883
884 return $ret;
885 }
886
887 /**
888 * This gets called shortly before the </body> tag.
889 * @param $out OutputPage object
890 * @return String HTML-wrapped JS code to be put before </body>
891 */
892 function bottomScripts( $out ) {
893 $bottomScriptText = "\n" . $out->getHeadScripts( $this );
894 wfRunHooks( 'SkinAfterBottomScripts', array( $this, &$bottomScriptText ) );
895
896 return $bottomScriptText;
897 }
898
899 /** @return string Retrievied from HTML text */
900 function printSource() {
901 $url = htmlspecialchars( $this->mTitle->getFullURL() );
902 return wfMsg( 'retrievedfrom', '<a href="' . $url . '">' . $url . '</a>' );
903 }
904
905 function getUndeleteLink() {
906 global $wgUser, $wgLang, $wgRequest;
907
908 $action = $wgRequest->getVal( 'action', 'view' );
909
910 if ( $wgUser->isAllowed( 'deletedhistory' ) &&
911 ( $this->mTitle->getArticleId() == 0 || $action == 'history' ) ) {
912 $n = $this->mTitle->isDeleted();
913
914 if ( $n ) {
915 if ( $wgUser->isAllowed( 'undelete' ) ) {
916 $msg = 'thisisdeleted';
917 } else {
918 $msg = 'viewdeleted';
919 }
920
921 return wfMsg(
922 $msg,
923 $this->link(
924 SpecialPage::getTitleFor( 'Undelete', $this->mTitle->getPrefixedDBkey() ),
925 wfMsgExt( 'restorelink', array( 'parsemag', 'escape' ), $wgLang->formatNum( $n ) ),
926 array(),
927 array(),
928 array( 'known', 'noclasses' )
929 )
930 );
931 }
932 }
933
934 return '';
935 }
936
937 /**
938 * The format without an explicit $out argument is deprecated
939 */
940 function subPageSubtitle( OutputPage $out=null ) {
941 if ( is_null( $out ) ) {
942 // Backwards compatibility for when there was no $out arg
943 global $wgOut;
944 $out = $wgOut;
945 }
946
947 $subpages = '';
948
949 if ( !wfRunHooks( 'SkinSubPageSubtitle', array( &$subpages, $this, $out ) ) ) {
950 return $subpages;
951 }
952
953 if ( $out->isArticle() && MWNamespace::hasSubpages( $out->getTitle()->getNamespace() ) ) {
954 $ptext = $this->mTitle->getPrefixedText();
955 if ( preg_match( '/\//', $ptext ) ) {
956 $links = explode( '/', $ptext );
957 array_pop( $links );
958 $c = 0;
959 $growinglink = '';
960 $display = '';
961
962 foreach ( $links as $link ) {
963 $growinglink .= $link;
964 $display .= $link;
965 $linkObj = Title::newFromText( $growinglink );
966
967 if ( is_object( $linkObj ) && $linkObj->exists() ) {
968 $getlink = $this->link(
969 $linkObj,
970 htmlspecialchars( $display ),
971 array(),
972 array(),
973 array( 'known', 'noclasses' )
974 );
975
976 $c++;
977
978 if ( $c > 1 ) {
979 $subpages .= wfMsgExt( 'pipe-separator', 'escapenoentities' );
980 } else {
981 $subpages .= '&lt; ';
982 }
983
984 $subpages .= $getlink;
985 $display = '';
986 } else {
987 $display .= '/';
988 }
989 $growinglink .= '/';
990 }
991 }
992 }
993
994 return $subpages;
995 }
996
997 /**
998 * Returns true if the IP should be shown in the header
999 */
1000 function showIPinHeader() {
1001 global $wgShowIPinHeader;
1002 return $wgShowIPinHeader && session_id() != '';
1003 }
1004
1005 function getSearchLink() {
1006 $searchPage = SpecialPage::getTitleFor( 'Search' );
1007 return $searchPage->getLocalURL();
1008 }
1009
1010 function escapeSearchLink() {
1011 return htmlspecialchars( $this->getSearchLink() );
1012 }
1013
1014 function getCopyright( $type = 'detect' ) {
1015 global $wgRightsPage, $wgRightsUrl, $wgRightsText, $wgRequest;
1016
1017 if ( $type == 'detect' ) {
1018 $diff = $wgRequest->getVal( 'diff' );
1019
1020 if ( is_null( $diff ) && !$this->isRevisionCurrent() && wfMsgForContent( 'history_copyright' ) !== '-' ) {
1021 $type = 'history';
1022 } else {
1023 $type = 'normal';
1024 }
1025 }
1026
1027 if ( $type == 'history' ) {
1028 $msg = 'history_copyright';
1029 } else {
1030 $msg = 'copyright';
1031 }
1032
1033 $out = '';
1034
1035 if ( $wgRightsPage ) {
1036 $title = Title::newFromText( $wgRightsPage );
1037 $link = $this->linkKnown( $title, $wgRightsText );
1038 } elseif ( $wgRightsUrl ) {
1039 $link = $this->makeExternalLink( $wgRightsUrl, $wgRightsText );
1040 } elseif ( $wgRightsText ) {
1041 $link = $wgRightsText;
1042 } else {
1043 # Give up now
1044 return $out;
1045 }
1046
1047 // Allow for site and per-namespace customization of copyright notice.
1048 $forContent = true;
1049
1050 wfRunHooks( 'SkinCopyrightFooter', array( $this->mTitle, $type, &$msg, &$link, &$forContent ) );
1051
1052 if ( $forContent ) {
1053 $out .= wfMsgForContent( $msg, $link );
1054 } else {
1055 $out .= wfMsg( $msg, $link );
1056 }
1057
1058 return $out;
1059 }
1060
1061 function getCopyrightIcon() {
1062 global $wgRightsUrl, $wgRightsText, $wgRightsIcon, $wgCopyrightIcon;
1063
1064 $out = '';
1065
1066 if ( isset( $wgCopyrightIcon ) && $wgCopyrightIcon ) {
1067 $out = $wgCopyrightIcon;
1068 } elseif ( $wgRightsIcon ) {
1069 $icon = htmlspecialchars( $wgRightsIcon );
1070
1071 if ( $wgRightsUrl ) {
1072 $url = htmlspecialchars( $wgRightsUrl );
1073 $out .= '<a href="' . $url . '">';
1074 }
1075
1076 $text = htmlspecialchars( $wgRightsText );
1077 $out .= "<img src=\"$icon\" alt=\"$text\" width=\"88\" height=\"31\" />";
1078
1079 if ( $wgRightsUrl ) {
1080 $out .= '</a>';
1081 }
1082 }
1083
1084 return $out;
1085 }
1086
1087 /**
1088 * Gets the powered by MediaWiki icon.
1089 * @return string
1090 */
1091 function getPoweredBy() {
1092 global $wgStylePath;
1093
1094 $url = htmlspecialchars( "$wgStylePath/common/images/poweredby_mediawiki_88x31.png" );
1095 $text = '<a href="http://www.mediawiki.org/"><img src="' . $url . '" height="31" width="88" alt="Powered by MediaWiki" /></a>';
1096 wfRunHooks( 'SkinGetPoweredBy', array( &$text, $this ) );
1097 return $text;
1098 }
1099
1100 /**
1101 * Get the timestamp of the latest revision, formatted in user language
1102 *
1103 * @param $article Article object. Used if we're working with the current revision
1104 * @return String
1105 */
1106 protected function lastModified( $article ) {
1107 global $wgLang;
1108
1109 if ( !$this->isRevisionCurrent() ) {
1110 $timestamp = Revision::getTimestampFromId( $this->mTitle, $this->mRevisionId );
1111 } else {
1112 $timestamp = $article->getTimestamp();
1113 }
1114
1115 if ( $timestamp ) {
1116 $d = $wgLang->date( $timestamp, true );
1117 $t = $wgLang->time( $timestamp, true );
1118 $s = ' ' . wfMsg( 'lastmodifiedat', $d, $t );
1119 } else {
1120 $s = '';
1121 }
1122
1123 if ( wfGetLB()->getLaggedSlaveMode() ) {
1124 $s .= ' <strong>' . wfMsg( 'laggedslavemode' ) . '</strong>';
1125 }
1126
1127 return $s;
1128 }
1129
1130 function logoText( $align = '' ) {
1131 if ( $align != '' ) {
1132 $a = " align='{$align}'";
1133 } else {
1134 $a = '';
1135 }
1136
1137 $mp = wfMsg( 'mainpage' );
1138 $mptitle = Title::newMainPage();
1139 $url = ( is_object( $mptitle ) ? $mptitle->escapeLocalURL() : '' );
1140
1141 $logourl = $this->getLogo();
1142 $s = "<a href='{$url}'><img{$a} src='{$logourl}' alt='[{$mp}]' /></a>";
1143
1144 return $s;
1145 }
1146
1147 /**
1148 * Renders a $wgFooterIcons icon acording to the method's arguments
1149 * @param $icon Array: The icon to build the html for, see $wgFooterIcons for the format of this array
1150 * @param $withImage Boolean: Whether to use the icon's image or output a text-only footericon
1151 */
1152 function makeFooterIcon( $icon, $withImage = 'withImage' ) {
1153 if ( is_string( $icon ) ) {
1154 $html = $icon;
1155 } else { // Assuming array
1156 $url = isset($icon["url"]) ? $icon["url"] : null;
1157 unset( $icon["url"] );
1158 if ( isset( $icon["src"] ) && $withImage === 'withImage' ) {
1159 $html = Html::element( 'img', $icon ); // do this the lazy way, just pass icon data as an attribute array
1160 } else {
1161 $html = htmlspecialchars( $icon["alt"] );
1162 }
1163 if ( $url ) {
1164 $html = Html::rawElement( 'a', array( "href" => $url ), $html );
1165 }
1166 }
1167 return $html;
1168 }
1169
1170 /**
1171 * Gets the link to the wiki's main page.
1172 * @return string
1173 */
1174 function mainPageLink() {
1175 $s = $this->link(
1176 Title::newMainPage(),
1177 wfMsg( 'mainpage' ),
1178 array(),
1179 array(),
1180 array( 'known', 'noclasses' )
1181 );
1182
1183 return $s;
1184 }
1185
1186 public function footerLink( $desc, $page ) {
1187 // if the link description has been set to "-" in the default language,
1188 if ( wfMsgForContent( $desc ) == '-' ) {
1189 // then it is disabled, for all languages.
1190 return '';
1191 } else {
1192 // Otherwise, we display the link for the user, described in their
1193 // language (which may or may not be the same as the default language),
1194 // but we make the link target be the one site-wide page.
1195 $title = Title::newFromText( wfMsgForContent( $page ) );
1196
1197 return $this->linkKnown(
1198 $title,
1199 wfMsgExt( $desc, array( 'parsemag', 'escapenoentities' ) )
1200 );
1201 }
1202 }
1203
1204 /**
1205 * Gets the link to the wiki's privacy policy page.
1206 */
1207 function privacyLink() {
1208 return $this->footerLink( 'privacy', 'privacypage' );
1209 }
1210
1211 /**
1212 * Gets the link to the wiki's about page.
1213 */
1214 function aboutLink() {
1215 return $this->footerLink( 'aboutsite', 'aboutpage' );
1216 }
1217
1218 /**
1219 * Gets the link to the wiki's general disclaimers page.
1220 */
1221 function disclaimerLink() {
1222 return $this->footerLink( 'disclaimers', 'disclaimerpage' );
1223 }
1224
1225 /**
1226 * Return URL options for the 'edit page' link.
1227 * This may include an 'oldid' specifier, if the current page view is such.
1228 *
1229 * @return array
1230 * @private
1231 */
1232 function editUrlOptions() {
1233 $options = array( 'action' => 'edit' );
1234
1235 if ( !$this->isRevisionCurrent() ) {
1236 $options['oldid'] = intval( $this->mRevisionId );
1237 }
1238
1239 return $options;
1240 }
1241
1242 function showEmailUser( $id ) {
1243 global $wgUser;
1244 $targetUser = User::newFromId( $id );
1245 return $wgUser->canSendEmail() && # the sending user must have a confirmed email address
1246 $targetUser->canReceiveEmail(); # the target user must have a confirmed email address and allow emails from users
1247 }
1248
1249 /**
1250 * Return a fully resolved style path url to images or styles stored in the common folder.
1251 * This method returns a url resolved using the configured skin style path
1252 * and includes the style version inside of the url.
1253 * @param $name String: The name or path of a skin resource file
1254 * @return String The fully resolved style path url including styleversion
1255 */
1256 function getCommonStylePath( $name ) {
1257 global $wgStylePath, $wgStyleVersion;
1258 return "$wgStylePath/common/$name?$wgStyleVersion";
1259 }
1260
1261 /**
1262 * Return a fully resolved style path url to images or styles stored in the curent skins's folder.
1263 * This method returns a url resolved using the configured skin style path
1264 * and includes the style version inside of the url.
1265 * @param $name String: The name or path of a skin resource file
1266 * @return String The fully resolved style path url including styleversion
1267 */
1268 function getSkinStylePath( $name ) {
1269 global $wgStylePath, $wgStyleVersion;
1270 return "$wgStylePath/{$this->stylename}/$name?$wgStyleVersion";
1271 }
1272
1273 /* these are used extensively in SkinTemplate, but also some other places */
1274 static function makeMainPageUrl( $urlaction = '' ) {
1275 $title = Title::newMainPage();
1276 self::checkTitle( $title, '' );
1277
1278 return $title->getLocalURL( $urlaction );
1279 }
1280
1281 static function makeSpecialUrl( $name, $urlaction = '' ) {
1282 $title = SpecialPage::getTitleFor( $name );
1283 return $title->getLocalURL( $urlaction );
1284 }
1285
1286 static function makeSpecialUrlSubpage( $name, $subpage, $urlaction = '' ) {
1287 $title = SpecialPage::getSafeTitleFor( $name, $subpage );
1288 return $title->getLocalURL( $urlaction );
1289 }
1290
1291 static function makeI18nUrl( $name, $urlaction = '' ) {
1292 $title = Title::newFromText( wfMsgForContent( $name ) );
1293 self::checkTitle( $title, $name );
1294 return $title->getLocalURL( $urlaction );
1295 }
1296
1297 static function makeUrl( $name, $urlaction = '' ) {
1298 $title = Title::newFromText( $name );
1299 self::checkTitle( $title, $name );
1300
1301 return $title->getLocalURL( $urlaction );
1302 }
1303
1304 /**
1305 * If url string starts with http, consider as external URL, else
1306 * internal
1307 */
1308 static function makeInternalOrExternalUrl( $name ) {
1309 if ( preg_match( '/^(?:' . wfUrlProtocols() . ')/', $name ) ) {
1310 return $name;
1311 } else {
1312 return self::makeUrl( $name );
1313 }
1314 }
1315
1316 # this can be passed the NS number as defined in Language.php
1317 static function makeNSUrl( $name, $urlaction = '', $namespace = NS_MAIN ) {
1318 $title = Title::makeTitleSafe( $namespace, $name );
1319 self::checkTitle( $title, $name );
1320
1321 return $title->getLocalURL( $urlaction );
1322 }
1323
1324 /* these return an array with the 'href' and boolean 'exists' */
1325 static function makeUrlDetails( $name, $urlaction = '' ) {
1326 $title = Title::newFromText( $name );
1327 self::checkTitle( $title, $name );
1328
1329 return array(
1330 'href' => $title->getLocalURL( $urlaction ),
1331 'exists' => $title->getArticleID() != 0,
1332 );
1333 }
1334
1335 /**
1336 * Make URL details where the article exists (or at least it's convenient to think so)
1337 */
1338 static function makeKnownUrlDetails( $name, $urlaction = '' ) {
1339 $title = Title::newFromText( $name );
1340 self::checkTitle( $title, $name );
1341
1342 return array(
1343 'href' => $title->getLocalURL( $urlaction ),
1344 'exists' => true
1345 );
1346 }
1347
1348 # make sure we have some title to operate on
1349 static function checkTitle( &$title, $name ) {
1350 if ( !is_object( $title ) ) {
1351 $title = Title::newFromText( $name );
1352 if ( !is_object( $title ) ) {
1353 $title = Title::newFromText( '--error: link target missing--' );
1354 }
1355 }
1356 }
1357
1358 /**
1359 * Build an array that represents the sidebar(s), the navigation bar among them
1360 *
1361 * @return array
1362 */
1363 function buildSidebar() {
1364 global $parserMemc, $wgEnableSidebarCache, $wgSidebarCacheExpiry;
1365 global $wgLang;
1366 wfProfileIn( __METHOD__ );
1367
1368 $key = wfMemcKey( 'sidebar', $wgLang->getCode() );
1369
1370 if ( $wgEnableSidebarCache ) {
1371 $cachedsidebar = $parserMemc->get( $key );
1372 if ( $cachedsidebar ) {
1373 wfProfileOut( __METHOD__ );
1374 return $cachedsidebar;
1375 }
1376 }
1377
1378 $bar = array();
1379 $this->addToSidebar( $bar, 'sidebar' );
1380
1381 wfRunHooks( 'SkinBuildSidebar', array( $this, &$bar ) );
1382 if ( $wgEnableSidebarCache ) {
1383 $parserMemc->set( $key, $bar, $wgSidebarCacheExpiry );
1384 }
1385
1386 wfProfileOut( __METHOD__ );
1387 return $bar;
1388 }
1389 /**
1390 * Add content from a sidebar system message
1391 * Currently only used for MediaWiki:Sidebar (but may be used by Extensions)
1392 *
1393 * This is just a wrapper around addToSidebarPlain() for backwards compatibility
1394 *
1395 * @param &$bar array
1396 * @param $message String
1397 */
1398 function addToSidebar( &$bar, $message ) {
1399 $this->addToSidebarPlain( $bar, wfMsgForContentNoTrans( $message ) );
1400 }
1401
1402 /**
1403 * Add content from plain text
1404 * @since 1.17
1405 * @param &$bar array
1406 * @param $text string
1407 */
1408 function addToSidebarPlain( &$bar, $text ) {
1409 $lines = explode( "\n", $text );
1410 $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.
1411
1412 $heading = '';
1413
1414 foreach ( $lines as $line ) {
1415 if ( strpos( $line, '*' ) !== 0 ) {
1416 continue;
1417 }
1418
1419 if ( strpos( $line, '**' ) !== 0 ) {
1420 $heading = trim( $line, '* ' );
1421 if ( !array_key_exists( $heading, $bar ) ) {
1422 $bar[$heading] = array();
1423 }
1424 } else {
1425 $line = trim( $line, '* ' );
1426
1427 if ( strpos( $line, '|' ) !== false ) { // sanity check
1428 $line = MessageCache::singleton()->transform( $line, false, null, $this->mTitle );
1429 $line = array_map( 'trim', explode( '|', $line, 2 ) );
1430 $link = wfMsgForContent( $line[0] );
1431
1432 if ( $link == '-' ) {
1433 continue;
1434 }
1435
1436 $text = wfMsgExt( $line[1], 'parsemag' );
1437
1438 if ( wfEmptyMsg( $line[1] ) ) {
1439 $text = $line[1];
1440 }
1441
1442 if ( wfEmptyMsg( $line[0] ) ) {
1443 $link = $line[0];
1444 }
1445
1446 if ( preg_match( '/^(?:' . wfUrlProtocols() . ')/', $link ) ) {
1447 $href = $link;
1448 } else {
1449 $title = Title::newFromText( $link );
1450
1451 if ( $title ) {
1452 $title = $title->fixSpecialName();
1453 $href = $title->getLocalURL();
1454 } else {
1455 $href = 'INVALID-TITLE';
1456 }
1457 }
1458
1459 $bar[$heading][] = array(
1460 'text' => $text,
1461 'href' => $href,
1462 'id' => 'n-' . strtr( $line[1], ' ', '-' ),
1463 'active' => false
1464 );
1465 } else if ( ( substr( $line, 0, 2 ) == '{{' ) && ( substr( $line, -2 ) == '}}' ) ) {
1466 global $wgParser;
1467
1468 $line = substr( $line, 2, strlen( $line ) - 4 );
1469
1470 $options = new ParserOptions();
1471 $options->setEditSection( false );
1472 $options->setInterfaceMessage( true );
1473 $wikiBar[$heading] = $wgParser->parse( wfMsgForContentNoTrans( $line ) , $this->mTitle, $options )->getText();
1474 } else {
1475 continue;
1476 }
1477 }
1478 }
1479
1480 if ( count( $wikiBar ) > 0 ) {
1481 $bar = array_merge( $bar, $wikiBar );
1482 }
1483
1484 return $bar;
1485 }
1486
1487 /**
1488 * Should we include common/wikiprintable.css? Skins that have their own
1489 * print stylesheet should override this and return false. (This is an
1490 * ugly hack to get Monobook to play nicely with
1491 * OutputPage::headElement().)
1492 *
1493 * @return bool
1494 */
1495 public function commonPrintStylesheet() {
1496 return true;
1497 }
1498
1499 /**
1500 * Gets new talk page messages for the current user.
1501 * @return MediaWiki message or if no new talk page messages, nothing
1502 * The format without an explicit $out argument is deprecated
1503 */
1504 function getNewtalks( OutputPage $out=null ) {
1505 global $wgUser;
1506
1507 if ( is_null( $out ) ) {
1508 // Backwards compatibility for when there was no $out arg
1509 global $wgOut;
1510 $out = $wgOut;
1511 }
1512
1513 $newtalks = $wgUser->getNewMessageLinks();
1514 $ntl = '';
1515
1516 if ( count( $newtalks ) == 1 && $newtalks[0]['wiki'] === wfWikiID() ) {
1517 $userTitle = $this->mUser->getUserPage();
1518 $userTalkTitle = $userTitle->getTalkPage();
1519
1520 if ( !$userTalkTitle->equals( $out->getTitle() ) ) {
1521 $newMessagesLink = $this->link(
1522 $userTalkTitle,
1523 wfMsgHtml( 'newmessageslink' ),
1524 array(),
1525 array( 'redirect' => 'no' ),
1526 array( 'known', 'noclasses' )
1527 );
1528
1529 $newMessagesDiffLink = $this->link(
1530 $userTalkTitle,
1531 wfMsgHtml( 'newmessagesdifflink' ),
1532 array(),
1533 array( 'diff' => 'cur' ),
1534 array( 'known', 'noclasses' )
1535 );
1536
1537 $ntl = wfMsg(
1538 'youhavenewmessages',
1539 $newMessagesLink,
1540 $newMessagesDiffLink
1541 );
1542 # Disable Squid cache
1543 $out->setSquidMaxage( 0 );
1544 }
1545 } elseif ( count( $newtalks ) ) {
1546 // _>" " for BC <= 1.16
1547 $sep = str_replace( '_', ' ', wfMsgHtml( 'newtalkseparator' ) );
1548 $msgs = array();
1549
1550 foreach ( $newtalks as $newtalk ) {
1551 $msgs[] = Xml::element(
1552 'a',
1553 array( 'href' => $newtalk['link'] ), $newtalk['wiki']
1554 );
1555 }
1556 $parts = implode( $sep, $msgs );
1557 $ntl = wfMsgHtml( 'youhavenewmessagesmulti', $parts );
1558 $out->setSquidMaxage( 0 );
1559 }
1560
1561 return $ntl;
1562 }
1563
1564 /**
1565 * Get a cached notice
1566 *
1567 * @param $name String: message name, or 'default' for $wgSiteNotice
1568 * @return String: HTML fragment
1569 */
1570 private function getCachedNotice( $name ) {
1571 global $wgOut, $wgRenderHashAppend, $parserMemc;
1572
1573 wfProfileIn( __METHOD__ );
1574
1575 $needParse = false;
1576
1577 if( $name === 'default' ) {
1578 // special case
1579 global $wgSiteNotice;
1580 $notice = $wgSiteNotice;
1581 if( empty( $notice ) ) {
1582 wfProfileOut( __METHOD__ );
1583 return false;
1584 }
1585 } else {
1586 $msg = wfMessage( $name )->inContentLanguage();
1587 if( $msg->isDisabled() ) {
1588 wfProfileOut( __METHOD__ );
1589 return false;
1590 }
1591 $notice = $msg->plain();
1592 }
1593
1594 // Use the extra hash appender to let eg SSL variants separately cache.
1595 $key = wfMemcKey( $name . $wgRenderHashAppend );
1596 $cachedNotice = $parserMemc->get( $key );
1597 if( is_array( $cachedNotice ) ) {
1598 if( md5( $notice ) == $cachedNotice['hash'] ) {
1599 $notice = $cachedNotice['html'];
1600 } else {
1601 $needParse = true;
1602 }
1603 } else {
1604 $needParse = true;
1605 }
1606
1607 if ( $needParse ) {
1608 if( is_object( $wgOut ) ) {
1609 $parsed = $wgOut->parse( $notice );
1610 $parserMemc->set( $key, array( 'html' => $parsed, 'hash' => md5( $notice ) ), 600 );
1611 $notice = $parsed;
1612 } else {
1613 wfDebug( 'Skin::getCachedNotice called for ' . $name . ' with no $wgOut available' . "\n" );
1614 $notice = '';
1615 }
1616 }
1617
1618 $notice = '<div id="localNotice">' .$notice . '</div>';
1619 wfProfileOut( __METHOD__ );
1620 return $notice;
1621 }
1622
1623 /**
1624 * Get a notice based on page's namespace
1625 *
1626 * @return String: HTML fragment
1627 */
1628 function getNamespaceNotice() {
1629 wfProfileIn( __METHOD__ );
1630
1631 $key = 'namespacenotice-' . $this->mTitle->getNsText();
1632 $namespaceNotice = $this->getCachedNotice( $key );
1633 if ( $namespaceNotice && substr( $namespaceNotice, 0, 7 ) != '<p>&lt;' ) {
1634 $namespaceNotice = '<div id="namespacebanner">' . $namespaceNotice . '</div>';
1635 } else {
1636 $namespaceNotice = '';
1637 }
1638
1639 wfProfileOut( __METHOD__ );
1640 return $namespaceNotice;
1641 }
1642
1643 /**
1644 * Get the site notice
1645 *
1646 * @return String: HTML fragment
1647 */
1648 function getSiteNotice() {
1649 global $wgUser;
1650
1651 wfProfileIn( __METHOD__ );
1652 $siteNotice = '';
1653
1654 if ( wfRunHooks( 'SiteNoticeBefore', array( &$siteNotice, $this ) ) ) {
1655 if ( is_object( $wgUser ) && $wgUser->isLoggedIn() ) {
1656 $siteNotice = $this->getCachedNotice( 'sitenotice' );
1657 } else {
1658 $anonNotice = $this->getCachedNotice( 'anonnotice' );
1659 if ( !$anonNotice ) {
1660 $siteNotice = $this->getCachedNotice( 'sitenotice' );
1661 } else {
1662 $siteNotice = $anonNotice;
1663 }
1664 }
1665 if ( !$siteNotice ) {
1666 $siteNotice = $this->getCachedNotice( 'default' );
1667 }
1668 }
1669
1670 wfRunHooks( 'SiteNoticeAfter', array( &$siteNotice, $this ) );
1671 wfProfileOut( __METHOD__ );
1672 return $siteNotice;
1673 }
1674 }