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