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