Adding functions to HTMLForm for explicitly setting the content of the header and...
[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()->getNamespace() == NS_SPECIAL ) {
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->getNamespace() == NS_SPECIAL ) {
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="http://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 $targetUser = User::newFromId( $id );
933 return $this->getUser()->canSendEmail() && # the sending user must have a confirmed email address
934 $targetUser->canReceiveEmail(); # the target user must have a confirmed email address and allow emails from users
935 }
936
937 /**
938 * Return a fully resolved style path url to images or styles stored in the common folder.
939 * This method returns a url resolved using the configured skin style path
940 * and includes the style version inside of the url.
941 * @param $name String: The name or path of a skin resource file
942 * @return String The fully resolved style path url including styleversion
943 */
944 function getCommonStylePath( $name ) {
945 global $wgStylePath, $wgStyleVersion;
946 return "$wgStylePath/common/$name?$wgStyleVersion";
947 }
948
949 /**
950 * Return a fully resolved style path url to images or styles stored in the curent skins's folder.
951 * This method returns a url resolved using the configured skin style path
952 * and includes the style version inside of the url.
953 * @param $name String: The name or path of a skin resource file
954 * @return String The fully resolved style path url including styleversion
955 */
956 function getSkinStylePath( $name ) {
957 global $wgStylePath, $wgStyleVersion;
958 return "$wgStylePath/{$this->stylename}/$name?$wgStyleVersion";
959 }
960
961 /* these are used extensively in SkinTemplate, but also some other places */
962 static function makeMainPageUrl( $urlaction = '' ) {
963 $title = Title::newMainPage();
964 self::checkTitle( $title, '' );
965
966 return $title->getLocalURL( $urlaction );
967 }
968
969 static function makeSpecialUrl( $name, $urlaction = '' ) {
970 $title = SpecialPage::getSafeTitleFor( $name );
971 return $title->getLocalURL( $urlaction );
972 }
973
974 static function makeSpecialUrlSubpage( $name, $subpage, $urlaction = '' ) {
975 $title = SpecialPage::getSafeTitleFor( $name, $subpage );
976 return $title->getLocalURL( $urlaction );
977 }
978
979 static function makeI18nUrl( $name, $urlaction = '' ) {
980 $title = Title::newFromText( wfMsgForContent( $name ) );
981 self::checkTitle( $title, $name );
982 return $title->getLocalURL( $urlaction );
983 }
984
985 static function makeUrl( $name, $urlaction = '' ) {
986 $title = Title::newFromText( $name );
987 self::checkTitle( $title, $name );
988
989 return $title->getLocalURL( $urlaction );
990 }
991
992 /**
993 * If url string starts with http, consider as external URL, else
994 * internal
995 * @param $name String
996 * @return String URL
997 */
998 static function makeInternalOrExternalUrl( $name ) {
999 if ( preg_match( '/^(?:' . wfUrlProtocols() . ')/', $name ) ) {
1000 return $name;
1001 } else {
1002 return self::makeUrl( $name );
1003 }
1004 }
1005
1006 # this can be passed the NS number as defined in Language.php
1007 static function makeNSUrl( $name, $urlaction = '', $namespace = NS_MAIN ) {
1008 $title = Title::makeTitleSafe( $namespace, $name );
1009 self::checkTitle( $title, $name );
1010
1011 return $title->getLocalURL( $urlaction );
1012 }
1013
1014 /* these return an array with the 'href' and boolean 'exists' */
1015 static function makeUrlDetails( $name, $urlaction = '' ) {
1016 $title = Title::newFromText( $name );
1017 self::checkTitle( $title, $name );
1018
1019 return array(
1020 'href' => $title->getLocalURL( $urlaction ),
1021 'exists' => $title->getArticleID() != 0,
1022 );
1023 }
1024
1025 /**
1026 * Make URL details where the article exists (or at least it's convenient to think so)
1027 * @param $name String Article name
1028 * @param $urlaction String
1029 * @return Array
1030 */
1031 static function makeKnownUrlDetails( $name, $urlaction = '' ) {
1032 $title = Title::newFromText( $name );
1033 self::checkTitle( $title, $name );
1034
1035 return array(
1036 'href' => $title->getLocalURL( $urlaction ),
1037 'exists' => true
1038 );
1039 }
1040
1041 # make sure we have some title to operate on
1042 static function checkTitle( &$title, $name ) {
1043 if ( !is_object( $title ) ) {
1044 $title = Title::newFromText( $name );
1045 if ( !is_object( $title ) ) {
1046 $title = Title::newFromText( '--error: link target missing--' );
1047 }
1048 }
1049 }
1050
1051 /**
1052 * Build an array that represents the sidebar(s), the navigation bar among them
1053 *
1054 * @return array
1055 */
1056 function buildSidebar() {
1057 global $parserMemc, $wgEnableSidebarCache, $wgSidebarCacheExpiry;
1058 wfProfileIn( __METHOD__ );
1059
1060 $key = wfMemcKey( 'sidebar', $this->getLang()->getCode() );
1061
1062 if ( $wgEnableSidebarCache ) {
1063 $cachedsidebar = $parserMemc->get( $key );
1064 if ( $cachedsidebar ) {
1065 wfProfileOut( __METHOD__ );
1066 return $cachedsidebar;
1067 }
1068 }
1069
1070 $bar = array();
1071 $this->addToSidebar( $bar, 'sidebar' );
1072
1073 wfRunHooks( 'SkinBuildSidebar', array( $this, &$bar ) );
1074 if ( $wgEnableSidebarCache ) {
1075 $parserMemc->set( $key, $bar, $wgSidebarCacheExpiry );
1076 }
1077
1078 wfProfileOut( __METHOD__ );
1079 return $bar;
1080 }
1081 /**
1082 * Add content from a sidebar system message
1083 * Currently only used for MediaWiki:Sidebar (but may be used by Extensions)
1084 *
1085 * This is just a wrapper around addToSidebarPlain() for backwards compatibility
1086 *
1087 * @param &$bar array
1088 * @param $message String
1089 */
1090 function addToSidebar( &$bar, $message ) {
1091 $this->addToSidebarPlain( $bar, wfMsgForContentNoTrans( $message ) );
1092 }
1093
1094 /**
1095 * Add content from plain text
1096 * @since 1.17
1097 * @param &$bar array
1098 * @param $text string
1099 * @return Array
1100 */
1101 function addToSidebarPlain( &$bar, $text ) {
1102 $lines = explode( "\n", $text );
1103
1104 $heading = '';
1105
1106 foreach ( $lines as $line ) {
1107 if ( strpos( $line, '*' ) !== 0 ) {
1108 continue;
1109 }
1110 $line = rtrim( $line, "\r" ); // for Windows compat
1111
1112 if ( strpos( $line, '**' ) !== 0 ) {
1113 $heading = trim( $line, '* ' );
1114 if ( !array_key_exists( $heading, $bar ) ) {
1115 $bar[$heading] = array();
1116 }
1117 } else {
1118 $line = trim( $line, '* ' );
1119
1120 if ( strpos( $line, '|' ) !== false ) { // sanity check
1121 $line = MessageCache::singleton()->transform( $line, false, null, $this->getTitle() );
1122 $line = array_map( 'trim', explode( '|', $line, 2 ) );
1123 $extraAttribs = array();
1124
1125 $msgLink = $this->msg( $line[0] )->inContentLanguage();
1126 if ( $msgLink->exists() ) {
1127 $link = $msgLink->text();
1128 if ( $link == '-' ) {
1129 continue;
1130 }
1131 } else {
1132 $link = $line[0];
1133 }
1134
1135 $msgText = $this->msg( $line[1] );
1136 if ( $msgText->exists() ) {
1137 $text = $msgText->text();
1138 } else {
1139 $text = $line[1];
1140 }
1141
1142 if ( preg_match( '/^(?:' . wfUrlProtocols() . ')/', $link ) ) {
1143 $href = $link;
1144
1145 // Parser::getExternalLinkAttribs won't work here because of the Namespace things
1146 global $wgNoFollowLinks, $wgNoFollowDomainExceptions;
1147 if ( $wgNoFollowLinks && !wfMatchesDomainList( $href, $wgNoFollowDomainExceptions ) ) {
1148 $extraAttribs['rel'] = 'nofollow';
1149 }
1150
1151 global $wgExternalLinkTarget;
1152 if ( $wgExternalLinkTarget) {
1153 $extraAttribs['target'] = $wgExternalLinkTarget;
1154 }
1155 } else {
1156 $title = Title::newFromText( $link );
1157
1158 if ( $title ) {
1159 $title = $title->fixSpecialName();
1160 $href = $title->getLinkURL();
1161 } else {
1162 $href = 'INVALID-TITLE';
1163 }
1164 }
1165
1166 $bar[$heading][] = array_merge( array(
1167 'text' => $text,
1168 'href' => $href,
1169 'id' => 'n-' . Sanitizer::escapeId( strtr( $line[1], ' ', '-' ), 'noninitial' ),
1170 'active' => false
1171 ), $extraAttribs );
1172 } else {
1173 continue;
1174 }
1175 }
1176 }
1177
1178 return $bar;
1179 }
1180
1181 /**
1182 * Should we load mediawiki.legacy.wikiprintable? Skins that have their own
1183 * print stylesheet should override this and return false. (This is an
1184 * ugly hack to get Monobook to play nicely with OutputPage::headElement().)
1185 *
1186 * @return bool
1187 */
1188 public function commonPrintStylesheet() {
1189 return true;
1190 }
1191
1192 /**
1193 * Gets new talk page messages for the current user.
1194 * @return MediaWiki message or if no new talk page messages, nothing
1195 */
1196 function getNewtalks() {
1197 $out = $this->getOutput();
1198
1199 $newtalks = $this->getUser()->getNewMessageLinks();
1200 $ntl = '';
1201
1202 if ( count( $newtalks ) == 1 && $newtalks[0]['wiki'] === wfWikiID() ) {
1203 $userTitle = $this->getUser()->getUserPage();
1204 $userTalkTitle = $userTitle->getTalkPage();
1205
1206 if ( !$userTalkTitle->equals( $out->getTitle() ) ) {
1207 $newMessagesLink = Linker::linkKnown(
1208 $userTalkTitle,
1209 $this->msg( 'newmessageslink' )->escaped(),
1210 array(),
1211 array( 'redirect' => 'no' )
1212 );
1213
1214 $newMessagesDiffLink = Linker::linkKnown(
1215 $userTalkTitle,
1216 $this->msg( 'newmessagesdifflink' )->escaped(),
1217 array(),
1218 array( 'diff' => 'cur' )
1219 );
1220
1221 $ntl = $this->msg(
1222 'youhavenewmessages',
1223 $newMessagesLink,
1224 $newMessagesDiffLink
1225 )->text();
1226 # Disable Squid cache
1227 $out->setSquidMaxage( 0 );
1228 }
1229 } elseif ( count( $newtalks ) ) {
1230 // _>" " for BC <= 1.16
1231 $sep = str_replace( '_', ' ', $this->msg( 'newtalkseparator' )->escaped() );
1232 $msgs = array();
1233
1234 foreach ( $newtalks as $newtalk ) {
1235 $msgs[] = Xml::element(
1236 'a',
1237 array( 'href' => $newtalk['link'] ), $newtalk['wiki']
1238 );
1239 }
1240 $parts = implode( $sep, $msgs );
1241 $ntl = $this->msg( 'youhavenewmessagesmulti' )->rawParams( $parts )->escaped();
1242 $out->setSquidMaxage( 0 );
1243 }
1244
1245 return $ntl;
1246 }
1247
1248 /**
1249 * Get a cached notice
1250 *
1251 * @param $name String: message name, or 'default' for $wgSiteNotice
1252 * @return String: HTML fragment
1253 */
1254 private function getCachedNotice( $name ) {
1255 global $wgRenderHashAppend, $parserMemc, $wgContLang;
1256
1257 wfProfileIn( __METHOD__ );
1258
1259 $needParse = false;
1260
1261 if( $name === 'default' ) {
1262 // special case
1263 global $wgSiteNotice;
1264 $notice = $wgSiteNotice;
1265 if( empty( $notice ) ) {
1266 wfProfileOut( __METHOD__ );
1267 return false;
1268 }
1269 } else {
1270 $msg = $this->msg( $name )->inContentLanguage();
1271 if( $msg->isDisabled() ) {
1272 wfProfileOut( __METHOD__ );
1273 return false;
1274 }
1275 $notice = $msg->plain();
1276 }
1277
1278 // Use the extra hash appender to let eg SSL variants separately cache.
1279 $key = wfMemcKey( $name . $wgRenderHashAppend );
1280 $cachedNotice = $parserMemc->get( $key );
1281 if( is_array( $cachedNotice ) ) {
1282 if( md5( $notice ) == $cachedNotice['hash'] ) {
1283 $notice = $cachedNotice['html'];
1284 } else {
1285 $needParse = true;
1286 }
1287 } else {
1288 $needParse = true;
1289 }
1290
1291 if ( $needParse ) {
1292 $parsed = $this->getOutput()->parse( $notice );
1293 $parserMemc->set( $key, array( 'html' => $parsed, 'hash' => md5( $notice ) ), 600 );
1294 $notice = $parsed;
1295 }
1296
1297 $notice = Html::rawElement( 'div', array( 'id' => 'localNotice',
1298 'lang' => $wgContLang->getCode(), 'dir' => $wgContLang->getDir() ), $notice );
1299 wfProfileOut( __METHOD__ );
1300 return $notice;
1301 }
1302
1303 /**
1304 * Get a notice based on page's namespace
1305 *
1306 * @return String: HTML fragment
1307 */
1308 function getNamespaceNotice() {
1309 wfProfileIn( __METHOD__ );
1310
1311 $key = 'namespacenotice-' . $this->getTitle()->getNsText();
1312 $namespaceNotice = $this->getCachedNotice( $key );
1313 if ( $namespaceNotice && substr( $namespaceNotice, 0, 7 ) != '<p>&lt;' ) {
1314 $namespaceNotice = '<div id="namespacebanner">' . $namespaceNotice . '</div>';
1315 } else {
1316 $namespaceNotice = '';
1317 }
1318
1319 wfProfileOut( __METHOD__ );
1320 return $namespaceNotice;
1321 }
1322
1323 /**
1324 * Get the site notice
1325 *
1326 * @return String: HTML fragment
1327 */
1328 function getSiteNotice() {
1329 wfProfileIn( __METHOD__ );
1330 $siteNotice = '';
1331
1332 if ( wfRunHooks( 'SiteNoticeBefore', array( &$siteNotice, $this ) ) ) {
1333 if ( is_object( $this->getUser() ) && $this->getUser()->isLoggedIn() ) {
1334 $siteNotice = $this->getCachedNotice( 'sitenotice' );
1335 } else {
1336 $anonNotice = $this->getCachedNotice( 'anonnotice' );
1337 if ( !$anonNotice ) {
1338 $siteNotice = $this->getCachedNotice( 'sitenotice' );
1339 } else {
1340 $siteNotice = $anonNotice;
1341 }
1342 }
1343 if ( !$siteNotice ) {
1344 $siteNotice = $this->getCachedNotice( 'default' );
1345 }
1346 }
1347
1348 wfRunHooks( 'SiteNoticeAfter', array( &$siteNotice, $this ) );
1349 wfProfileOut( __METHOD__ );
1350 return $siteNotice;
1351 }
1352
1353 /**
1354 * Create a section edit link. This supersedes editSectionLink() and
1355 * editSectionLinkForOther().
1356 *
1357 * @param $nt Title The title being linked to (may not be the same as
1358 * $wgTitle, if the section is included from a template)
1359 * @param $section string The designation of the section being pointed to,
1360 * to be included in the link, like "&section=$section"
1361 * @param $tooltip string The tooltip to use for the link: will be escaped
1362 * and wrapped in the 'editsectionhint' message
1363 * @param $lang string Language code
1364 * @return string HTML to use for edit link
1365 */
1366 public function doEditSectionLink( Title $nt, $section, $tooltip = null, $lang = false ) {
1367 // HTML generated here should probably have userlangattributes
1368 // added to it for LTR text on RTL pages
1369 $attribs = array();
1370 if ( !is_null( $tooltip ) ) {
1371 # Bug 25462: undo double-escaping.
1372 $tooltip = Sanitizer::decodeCharReferences( $tooltip );
1373 $attribs['title'] = wfMsgExt( 'editsectionhint', array( 'language' => $lang, 'parsemag' ), $tooltip );
1374 }
1375 $link = Linker::link( $nt, wfMsgExt( 'editsection', array( 'language' => $lang ) ),
1376 $attribs,
1377 array( 'action' => 'edit', 'section' => $section ),
1378 array( 'noclasses', 'known' )
1379 );
1380
1381 # Run the old hook. This takes up half of the function . . . hopefully
1382 # we can rid of it someday.
1383 $attribs = '';
1384 if ( $tooltip ) {
1385 $attribs = wfMsgExt( 'editsectionhint', array( 'language' => $lang, 'parsemag', 'escape' ), $tooltip );
1386 $attribs = " title=\"$attribs\"";
1387 }
1388 $result = null;
1389 wfRunHooks( 'EditSectionLink', array( &$this, $nt, $section, $attribs, $link, &$result, $lang ) );
1390 if ( !is_null( $result ) ) {
1391 # For reverse compatibility, add the brackets *after* the hook is
1392 # run, and even add them to hook-provided text. (This is the main
1393 # reason that the EditSectionLink hook is deprecated in favor of
1394 # DoEditSectionLink: it can't change the brackets or the span.)
1395 $result = wfMsgExt( 'editsection-brackets', array( 'escape', 'replaceafter', 'language' => $lang ), $result );
1396 return "<span class=\"editsection\">$result</span>";
1397 }
1398
1399 # Add the brackets and the span, and *then* run the nice new hook, with
1400 # clean and non-redundant arguments.
1401 $result = wfMsgExt( 'editsection-brackets', array( 'escape', 'replaceafter', 'language' => $lang ), $link );
1402 $result = "<span class=\"editsection\">$result</span>";
1403
1404 wfRunHooks( 'DoEditSectionLink', array( $this, $nt, $section, $tooltip, &$result, $lang ) );
1405 return $result;
1406 }
1407
1408 /**
1409 * Use PHP's magic __call handler to intercept legacy calls to the linker
1410 * for backwards compatibility.
1411 *
1412 * @param $fname String Name of called method
1413 * @param $args Array Arguments to the method
1414 */
1415 function __call( $fname, $args ) {
1416 $realFunction = array( 'Linker', $fname );
1417 if ( is_callable( $realFunction ) ) {
1418 return call_user_func_array( $realFunction, $args );
1419 } else {
1420 $className = get_class( $this );
1421 throw new MWException( "Call to undefined method $className::$fname" );
1422 }
1423 }
1424
1425 }