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