Merge "Fix user-friendlyness of block confirmation screen"
[lhc/web/wiklou.git] / includes / specialpage / SpecialPage.php
1 <?php
2 /**
3 * Parent class for all special pages.
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 * @ingroup SpecialPage
22 */
23
24 /**
25 * Parent class for all special pages.
26 *
27 * Includes some static functions for handling the special page list deprecated
28 * in favor of SpecialPageFactory.
29 *
30 * @todo Turn this into a real ContextSource
31 * @ingroup SpecialPage
32 */
33 class SpecialPage {
34 // The canonical name of this special page
35 // Also used for the default <h1> heading, @see getDescription()
36 protected $mName;
37
38 // The local name of this special page
39 private $mLocalName;
40
41 // Minimum user level required to access this page, or "" for anyone.
42 // Also used to categorise the pages in Special:Specialpages
43 private $mRestriction;
44
45 // Listed in Special:Specialpages?
46 private $mListed;
47
48 // Function name called by the default execute()
49 private $mFunction;
50
51 // File which needs to be included before the function above can be called
52 private $mFile;
53
54 // Whether or not this special page is being included from an article
55 protected $mIncluding;
56
57 // Whether the special page can be included in an article
58 protected $mIncludable;
59
60 /**
61 * Current request context
62 * @var IContextSource
63 */
64 protected $mContext;
65
66 /**
67 * Initialise the special page list
68 * This must be called before accessing SpecialPage::$mList
69 * @deprecated since 1.18
70 */
71 static function initList() {
72 wfDeprecated( __METHOD__, '1.18' );
73 // Noop
74 }
75
76 /**
77 * @deprecated since 1.18
78 */
79 static function initAliasList() {
80 wfDeprecated( __METHOD__, '1.18' );
81 // Noop
82 }
83
84 /**
85 * Given a special page alias, return the special page name.
86 * Returns false if there is no such alias.
87 *
88 * @param $alias String
89 * @return String or false
90 * @deprecated since 1.18 call SpecialPageFactory method directly
91 */
92 static function resolveAlias( $alias ) {
93 wfDeprecated( __METHOD__, '1.18' );
94 list( $name, /*...*/ ) = SpecialPageFactory::resolveAlias( $alias );
95 return $name;
96 }
97
98 /**
99 * Given a special page name with a possible subpage, return an array
100 * where the first element is the special page name and the second is the
101 * subpage.
102 *
103 * @param $alias String
104 * @return Array
105 * @deprecated since 1.18 call SpecialPageFactory method directly
106 */
107 static function resolveAliasWithSubpage( $alias ) {
108 return SpecialPageFactory::resolveAlias( $alias );
109 }
110
111 /**
112 * Add a page to a certain display group for Special:SpecialPages
113 *
114 * @param $page Mixed: SpecialPage or string
115 * @param $group String
116 * @deprecated since 1.18 call SpecialPageFactory method directly
117 */
118 static function setGroup( $page, $group ) {
119 wfDeprecated( __METHOD__, '1.18' );
120 SpecialPageFactory::setGroup( $page, $group );
121 }
122
123 /**
124 * Get the group that the special page belongs in on Special:SpecialPage
125 *
126 * @param $page SpecialPage
127 * @return string
128 * @deprecated since 1.18 call SpecialPageFactory method directly
129 */
130 static function getGroup( &$page ) {
131 wfDeprecated( __METHOD__, '1.18' );
132 return $page->getFinalGroupName();
133 }
134
135 /**
136 * Remove a special page from the list
137 * Formerly used to disable expensive or dangerous special pages. The
138 * preferred method is now to add a SpecialPage_initList hook.
139 * @deprecated since 1.18
140 *
141 * @param string $name the page to remove
142 */
143 static function removePage( $name ) {
144 wfDeprecated( __METHOD__, '1.18' );
145 unset( SpecialPageFactory::getList()->$name );
146 }
147
148 /**
149 * Check if a given name exist as a special page or as a special page alias
150 *
151 * @param string $name name of a special page
152 * @return Boolean: true if a special page exists with this name
153 * @deprecated since 1.18 call SpecialPageFactory method directly
154 */
155 static function exists( $name ) {
156 wfDeprecated( __METHOD__, '1.18' );
157 return SpecialPageFactory::exists( $name );
158 }
159
160 /**
161 * Find the object with a given name and return it (or NULL)
162 *
163 * @param $name String
164 * @return SpecialPage object or null if the page doesn't exist
165 * @deprecated since 1.18 call SpecialPageFactory method directly
166 */
167 static function getPage( $name ) {
168 wfDeprecated( __METHOD__, '1.18' );
169 return SpecialPageFactory::getPage( $name );
170 }
171
172 /**
173 * Get a special page with a given localised name, or NULL if there
174 * is no such special page.
175 *
176 * @param $alias String
177 * @return SpecialPage object or null if the page doesn't exist
178 * @deprecated since 1.18 call SpecialPageFactory method directly
179 */
180 static function getPageByAlias( $alias ) {
181 wfDeprecated( __METHOD__, '1.18' );
182 return SpecialPageFactory::getPage( $alias );
183 }
184
185 /**
186 * Return categorised listable special pages which are available
187 * for the current user, and everyone.
188 *
189 * @param $user User object to check permissions, $wgUser will be used
190 * if not provided
191 * @return array Associative array mapping page's name to its SpecialPage object
192 * @deprecated since 1.18 call SpecialPageFactory method directly
193 */
194 static function getUsablePages( User $user = null ) {
195 wfDeprecated( __METHOD__, '1.18' );
196 return SpecialPageFactory::getUsablePages( $user );
197 }
198
199 /**
200 * Return categorised listable special pages for all users
201 *
202 * @return array Associative array mapping page's name to its SpecialPage object
203 * @deprecated since 1.18 call SpecialPageFactory method directly
204 */
205 static function getRegularPages() {
206 wfDeprecated( __METHOD__, '1.18' );
207 return SpecialPageFactory::getRegularPages();
208 }
209
210 /**
211 * Return categorised listable special pages which are available
212 * for the current user, but not for everyone
213 *
214 * @return array Associative array mapping page's name to its SpecialPage object
215 * @deprecated since 1.18 call SpecialPageFactory method directly
216 */
217 static function getRestrictedPages() {
218 wfDeprecated( __METHOD__, '1.18' );
219 return SpecialPageFactory::getRestrictedPages();
220 }
221
222 /**
223 * Execute a special page path.
224 * The path may contain parameters, e.g. Special:Name/Params
225 * Extracts the special page name and call the execute method, passing the parameters
226 *
227 * Returns a title object if the page is redirected, false if there was no such special
228 * page, and true if it was successful.
229 *
230 * @param $title Title object
231 * @param $context IContextSource
232 * @param $including Bool output is being captured for use in {{special:whatever}}
233 * @return Bool
234 * @deprecated since 1.18 call SpecialPageFactory method directly
235 */
236 public static function executePath( &$title, IContextSource &$context, $including = false ) {
237 wfDeprecated( __METHOD__, '1.18' );
238 return SpecialPageFactory::executePath( $title, $context, $including );
239 }
240
241 /**
242 * Get the local name for a specified canonical name
243 *
244 * @param $name String
245 * @param $subpage Mixed: boolean false, or string
246 *
247 * @return String
248 * @deprecated since 1.18 call SpecialPageFactory method directly
249 */
250 static function getLocalNameFor( $name, $subpage = false ) {
251 wfDeprecated( __METHOD__, '1.18' );
252 return SpecialPageFactory::getLocalNameFor( $name, $subpage );
253 }
254
255 /**
256 * Get a localised Title object for a specified special page name
257 *
258 * @param string $name
259 * @param string|bool $subpage Subpage string, or false to not use a subpage
260 * @param string $fragment The link fragment (after the "#")
261 * @return Title
262 * @throws MWException
263 */
264 public static function getTitleFor( $name, $subpage = false, $fragment = '' ) {
265 $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
266 return Title::makeTitle( NS_SPECIAL, $name, $fragment );
267 }
268
269 /**
270 * Get a localised Title object for a page name with a possibly unvalidated subpage
271 *
272 * @param string $name
273 * @param string|bool $subpage Subpage string, or false to not use a subpage
274 * @return Title|null Title object or null if the page doesn't exist
275 */
276 public static function getSafeTitleFor( $name, $subpage = false ) {
277 $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
278 if ( $name ) {
279 return Title::makeTitleSafe( NS_SPECIAL, $name );
280 } else {
281 return null;
282 }
283 }
284
285 /**
286 * Get a title for a given alias
287 *
288 * @param $alias String
289 * @return Title or null if there is no such alias
290 * @deprecated since 1.18 call SpecialPageFactory method directly
291 */
292 static function getTitleForAlias( $alias ) {
293 wfDeprecated( __METHOD__, '1.18' );
294 return SpecialPageFactory::getTitleForAlias( $alias );
295 }
296
297 /**
298 * Default constructor for special pages
299 * Derivative classes should call this from their constructor
300 * Note that if the user does not have the required level, an error message will
301 * be displayed by the default execute() method, without the global function ever
302 * being called.
303 *
304 * If you override execute(), you can recover the default behavior with userCanExecute()
305 * and displayRestrictionError()
306 *
307 * @param string $name Name of the special page, as seen in links and URLs
308 * @param string $restriction User right required, e.g. "block" or "delete"
309 * @param bool $listed Whether the page is listed in Special:Specialpages
310 * @param callable|bool $function Function called by execute(). By default
311 * it is constructed from $name
312 * @param string $file File which is included by execute(). It is also
313 * constructed from $name by default
314 * @param bool $includable Whether the page can be included in normal pages
315 */
316 public function __construct(
317 $name = '', $restriction = '', $listed = true,
318 $function = false, $file = 'default', $includable = false
319 ) {
320 $this->init( $name, $restriction, $listed, $function, $file, $includable );
321 }
322
323 /**
324 * Do the real work for the constructor, mainly so __call() can intercept
325 * calls to SpecialPage()
326 * @param string $name Name of the special page, as seen in links and URLs
327 * @param string $restriction User right required, e.g. "block" or "delete"
328 * @param bool $listed Whether the page is listed in Special:Specialpages
329 * @param callable|bool $function Function called by execute(). By default
330 * it is constructed from $name
331 * @param string $file File which is included by execute(). It is also
332 * constructed from $name by default
333 * @param bool $includable Whether the page can be included in normal pages
334 */
335 private function init( $name, $restriction, $listed, $function, $file, $includable ) {
336 $this->mName = $name;
337 $this->mRestriction = $restriction;
338 $this->mListed = $listed;
339 $this->mIncludable = $includable;
340 if ( !$function ) {
341 $this->mFunction = 'wfSpecial' . $name;
342 } else {
343 $this->mFunction = $function;
344 }
345 if ( $file === 'default' ) {
346 $this->mFile = __DIR__ . "/specials/Special$name.php";
347 } else {
348 $this->mFile = $file;
349 }
350 }
351
352 /**
353 * Use PHP's magic __call handler to get calls to the old PHP4 constructor
354 * because PHP E_STRICT yells at you for having __construct() and SpecialPage()
355 *
356 * @param string $fName Name of called method
357 * @param array $a Arguments to the method
358 * @throws MWException
359 * @deprecated since 1.17, call parent::__construct()
360 */
361 public function __call( $fName, $a ) {
362 // Deprecated messages now, remove in 1.19 or 1.20?
363 wfDeprecated( __METHOD__, '1.17' );
364
365 // Sometimes $fName is SpecialPage, sometimes it's specialpage. <3 PHP
366 if ( strtolower( $fName ) == 'specialpage' ) {
367 $name = isset( $a[0] ) ? $a[0] : '';
368 $restriction = isset( $a[1] ) ? $a[1] : '';
369 $listed = isset( $a[2] ) ? $a[2] : true;
370 $function = isset( $a[3] ) ? $a[3] : false;
371 $file = isset( $a[4] ) ? $a[4] : 'default';
372 $includable = isset( $a[5] ) ? $a[5] : false;
373 $this->init( $name, $restriction, $listed, $function, $file, $includable );
374 } else {
375 $className = get_class( $this );
376 throw new MWException( "Call to undefined method $className::$fName" );
377 }
378 }
379
380 /**
381 * Get the name of this Special Page.
382 * @return string
383 */
384 function getName() {
385 return $this->mName;
386 }
387
388 /**
389 * Get the permission that a user must have to execute this page
390 * @return string
391 */
392 function getRestriction() {
393 return $this->mRestriction;
394 }
395
396 /**
397 * Get the file which will be included by SpecialPage::execute() if your extension is
398 * still stuck in the past and hasn't overridden the execute() method. No modern code
399 * should want or need to know this.
400 * @return string
401 * @deprecated since 1.18
402 */
403 function getFile() {
404 wfDeprecated( __METHOD__, '1.18' );
405 return $this->mFile;
406 }
407
408 // @todo FIXME: Decide which syntax to use for this, and stick to it
409 /**
410 * Whether this special page is listed in Special:SpecialPages
411 * @since r3583 (v1.3)
412 * @return bool
413 */
414 function isListed() {
415 return $this->mListed;
416 }
417 /**
418 * Set whether this page is listed in Special:Specialpages, at run-time
419 * @since 1.3
420 * @param bool $listed
421 * @return bool
422 */
423 function setListed( $listed ) {
424 return wfSetVar( $this->mListed, $listed );
425 }
426 /**
427 * Get or set whether this special page is listed in Special:SpecialPages
428 * @since 1.6
429 * @param bool $x
430 * @return bool
431 */
432 function listed( $x = null ) {
433 return wfSetVar( $this->mListed, $x );
434 }
435
436 /**
437 * Whether it's allowed to transclude the special page via {{Special:Foo/params}}
438 * @return bool
439 */
440 public function isIncludable() {
441 return $this->mIncludable;
442 }
443
444 /**
445 * These mutators are very evil, as the relevant variables should not mutate. So
446 * don't use them.
447 * @param $x Mixed
448 * @return Mixed
449 * @deprecated since 1.18
450 */
451 function name( $x = null ) {
452 wfDeprecated( __METHOD__, '1.18' );
453 return wfSetVar( $this->mName, $x );
454 }
455
456 /**
457 * These mutators are very evil, as the relevant variables should not mutate. So
458 * don't use them.
459 * @param $x Mixed
460 * @return Mixed
461 * @deprecated since 1.18
462 */
463 function restriction( $x = null ) {
464 wfDeprecated( __METHOD__, '1.18' );
465 return wfSetVar( $this->mRestriction, $x );
466 }
467
468 /**
469 * These mutators are very evil, as the relevant variables should not mutate. So
470 * don't use them.
471 * @param $x Mixed
472 * @return Mixed
473 * @deprecated since 1.18
474 */
475 function func( $x = null ) {
476 wfDeprecated( __METHOD__, '1.18' );
477 return wfSetVar( $this->mFunction, $x );
478 }
479
480 /**
481 * These mutators are very evil, as the relevant variables should not mutate. So
482 * don't use them.
483 * @param $x Mixed
484 * @return Mixed
485 * @deprecated since 1.18
486 */
487 function file( $x = null ) {
488 wfDeprecated( __METHOD__, '1.18' );
489 return wfSetVar( $this->mFile, $x );
490 }
491
492 /**
493 * These mutators are very evil, as the relevant variables should not mutate. So
494 * don't use them.
495 * @param $x Mixed
496 * @return Mixed
497 * @deprecated since 1.18
498 */
499 function includable( $x = null ) {
500 wfDeprecated( __METHOD__, '1.18' );
501 return wfSetVar( $this->mIncludable, $x );
502 }
503
504 /**
505 * Whether the special page is being evaluated via transclusion
506 * @param bool $x
507 * @return bool
508 */
509 function including( $x = null ) {
510 return wfSetVar( $this->mIncluding, $x );
511 }
512
513 /**
514 * Get the localised name of the special page
515 * @return string
516 */
517 function getLocalName() {
518 if ( !isset( $this->mLocalName ) ) {
519 $this->mLocalName = SpecialPageFactory::getLocalNameFor( $this->mName );
520 }
521 return $this->mLocalName;
522 }
523
524 /**
525 * Is this page expensive (for some definition of expensive)?
526 * Expensive pages are disabled or cached in miser mode. Originally used
527 * (and still overridden) by QueryPage and subclasses, moved here so that
528 * Special:SpecialPages can safely call it for all special pages.
529 *
530 * @return bool
531 */
532 public function isExpensive() {
533 return false;
534 }
535
536 /**
537 * Is this page cached?
538 * Expensive pages are cached or disabled in miser mode.
539 * Used by QueryPage and subclasses, moved here so that
540 * Special:SpecialPages can safely call it for all special pages.
541 *
542 * @return bool
543 * @since 1.21
544 */
545 public function isCached() {
546 return false;
547 }
548
549 /**
550 * Can be overridden by subclasses with more complicated permissions
551 * schemes.
552 *
553 * @return bool Should the page be displayed with the restricted-access
554 * pages?
555 */
556 public function isRestricted() {
557 // DWIM: If anons can do something, then it is not restricted
558 return $this->mRestriction != '' && !User::groupHasPermission( '*', $this->mRestriction );
559 }
560
561 /**
562 * Checks if the given user (identified by an object) can execute this
563 * special page (as defined by $mRestriction). Can be overridden by sub-
564 * classes with more complicated permissions schemes.
565 *
566 * @param User $user The user to check
567 * @return bool Does the user have permission to view the page?
568 */
569 public function userCanExecute( User $user ) {
570 return $user->isAllowed( $this->mRestriction );
571 }
572
573 /**
574 * Output an error message telling the user what access level they have to have
575 * @throws PermissionsError
576 */
577 function displayRestrictionError() {
578 throw new PermissionsError( $this->mRestriction );
579 }
580
581 /**
582 * Checks if userCanExecute, and if not throws a PermissionsError
583 *
584 * @since 1.19
585 * @return void
586 * @throws PermissionsError
587 */
588 public function checkPermissions() {
589 if ( !$this->userCanExecute( $this->getUser() ) ) {
590 $this->displayRestrictionError();
591 }
592 }
593
594 /**
595 * If the wiki is currently in readonly mode, throws a ReadOnlyError
596 *
597 * @since 1.19
598 * @return void
599 * @throws ReadOnlyError
600 */
601 public function checkReadOnly() {
602 if ( wfReadOnly() ) {
603 throw new ReadOnlyError;
604 }
605 }
606
607 /**
608 * If the user is not logged in, throws UserNotLoggedIn error.
609 *
610 * Default error message includes a link to Special:Userlogin with properly set 'returnto' query
611 * parameter.
612 *
613 * @since 1.23
614 * @param string|Message $reasonMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
615 * will be used as message keys. If a string is given, the message will also receive a
616 * formatted login link (generated using the 'loginreqlink' message) as first parameter. If a
617 * Message is given, it will be passed on verbatim.
618 * @param string|Message $titleMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
619 * will be used as message keys.
620 * @throws UserNotLoggedIn
621 */
622 public function requireLogin( $reasonMsg = null, $titleMsg = null ) {
623 if ( $this->getUser()->isAnon() ) {
624 // Use default messages if not given or explicit null passed
625 if ( !$reasonMsg ) {
626 $reasonMsg = 'exception-nologin-text-manual';
627 }
628 if ( !$titleMsg ) {
629 $titleMsg = 'exception-nologin';
630 }
631
632 // Convert to Messages with current context
633 if ( is_string( $reasonMsg ) ) {
634 $loginreqlink = Linker::linkKnown(
635 SpecialPage::getTitleFor( 'Userlogin' ),
636 $this->msg( 'loginreqlink' )->escaped(),
637 array(),
638 array( 'returnto' => $this->getPageTitle()->getPrefixedText() )
639 );
640 $reasonMsg = $this->msg( $reasonMsg )->rawParams( $loginreqlink );
641 }
642 if ( is_string( $titleMsg ) ) {
643 $titleMsg = $this->msg( $titleMsg );
644 }
645
646 throw new UserNotLoggedIn( $reasonMsg, $titleMsg );
647 }
648 }
649
650 /**
651 * Sets headers - this should be called from the execute() method of all derived classes!
652 */
653 function setHeaders() {
654 $out = $this->getOutput();
655 $out->setArticleRelated( false );
656 $out->setRobotPolicy( "noindex,nofollow" );
657 $out->setPageTitle( $this->getDescription() );
658 }
659
660 /**
661 * Entry point.
662 *
663 * @since 1.20
664 *
665 * @param string|null $subPage
666 */
667 final public function run( $subPage ) {
668 /**
669 * Gets called before @see SpecialPage::execute.
670 *
671 * @since 1.20
672 *
673 * @param SpecialPage $this
674 * @param string|null $subPage
675 */
676 wfRunHooks( 'SpecialPageBeforeExecute', array( $this, $subPage ) );
677
678 $this->beforeExecute( $subPage );
679 $this->execute( $subPage );
680 $this->afterExecute( $subPage );
681
682 /**
683 * Gets called after @see SpecialPage::execute.
684 *
685 * @since 1.20
686 *
687 * @param SpecialPage $this
688 * @param string|null $subPage
689 */
690 wfRunHooks( 'SpecialPageAfterExecute', array( $this, $subPage ) );
691 }
692
693 /**
694 * Gets called before @see SpecialPage::execute.
695 *
696 * @since 1.20
697 *
698 * @param string|null $subPage
699 */
700 protected function beforeExecute( $subPage ) {
701 // No-op
702 }
703
704 /**
705 * Gets called after @see SpecialPage::execute.
706 *
707 * @since 1.20
708 *
709 * @param string|null $subPage
710 */
711 protected function afterExecute( $subPage ) {
712 // No-op
713 }
714
715 /**
716 * Default execute method
717 * Checks user permissions, calls the function given in mFunction
718 *
719 * This must be overridden by subclasses; it will be made abstract in a future version
720 *
721 * @param string|null $subPage
722 */
723 public function execute( $subPage ) {
724 $this->setHeaders();
725 $this->checkPermissions();
726
727 $func = $this->mFunction;
728 // only load file if the function does not exist
729 if ( !is_callable( $func ) && $this->mFile ) {
730 require_once $this->mFile;
731 }
732 $this->outputHeader();
733 call_user_func( $func, $subPage, $this );
734 }
735
736 /**
737 * Outputs a summary message on top of special pages
738 * Per default the message key is the canonical name of the special page
739 * May be overridden, i.e. by extensions to stick with the naming conventions
740 * for message keys: 'extensionname-xxx'
741 *
742 * @param string $summaryMessageKey Message key of the summary
743 */
744 function outputHeader( $summaryMessageKey = '' ) {
745 global $wgContLang;
746
747 if ( $summaryMessageKey == '' ) {
748 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
749 } else {
750 $msg = $summaryMessageKey;
751 }
752 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
753 $this->getOutput()->wrapWikiMsg(
754 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
755 }
756
757 }
758
759 /**
760 * Returns the name that goes in the \<h1\> in the special page itself, and
761 * also the name that will be listed in Special:Specialpages
762 *
763 * Derived classes can override this, but usually it is easier to keep the
764 * default behavior.
765 *
766 * @return string
767 */
768 function getDescription() {
769 return $this->msg( strtolower( $this->mName ) )->text();
770 }
771
772 /**
773 * Get a self-referential title object
774 *
775 * @param string|bool $subpage
776 * @return Title
777 * @deprecated in 1.23, use SpecialPage::getPageTitle
778 */
779 function getTitle( $subpage = false ) {
780 wfDeprecated( __METHOD__, '1.23' );
781 return $this->getPageTitle( $subpage );
782 }
783
784 /**
785 * Get a self-referential title object
786 *
787 * @param string|bool $subpage
788 * @return Title
789 * @since 1.23
790 */
791 function getPageTitle( $subpage = false ) {
792 return self::getTitleFor( $this->mName, $subpage );
793 }
794
795 /**
796 * Sets the context this SpecialPage is executed in
797 *
798 * @param IContextSource $context
799 * @since 1.18
800 */
801 public function setContext( $context ) {
802 $this->mContext = $context;
803 }
804
805 /**
806 * Gets the context this SpecialPage is executed in
807 *
808 * @return IContextSource|RequestContext
809 * @since 1.18
810 */
811 public function getContext() {
812 if ( $this->mContext instanceof IContextSource ) {
813 return $this->mContext;
814 } else {
815 wfDebug( __METHOD__ . " called and \$mContext is null. " .
816 "Return RequestContext::getMain(); for sanity\n" );
817 return RequestContext::getMain();
818 }
819 }
820
821 /**
822 * Get the WebRequest being used for this instance
823 *
824 * @return WebRequest
825 * @since 1.18
826 */
827 public function getRequest() {
828 return $this->getContext()->getRequest();
829 }
830
831 /**
832 * Get the OutputPage being used for this instance
833 *
834 * @return OutputPage
835 * @since 1.18
836 */
837 public function getOutput() {
838 return $this->getContext()->getOutput();
839 }
840
841 /**
842 * Shortcut to get the User executing this instance
843 *
844 * @return User
845 * @since 1.18
846 */
847 public function getUser() {
848 return $this->getContext()->getUser();
849 }
850
851 /**
852 * Shortcut to get the skin being used for this instance
853 *
854 * @return Skin
855 * @since 1.18
856 */
857 public function getSkin() {
858 return $this->getContext()->getSkin();
859 }
860
861 /**
862 * Shortcut to get user's language
863 *
864 * @deprecated since 1.19 Use getLanguage instead
865 * @return Language
866 * @since 1.18
867 */
868 public function getLang() {
869 wfDeprecated( __METHOD__, '1.19' );
870 return $this->getLanguage();
871 }
872
873 /**
874 * Shortcut to get user's language
875 *
876 * @return Language
877 * @since 1.19
878 */
879 public function getLanguage() {
880 return $this->getContext()->getLanguage();
881 }
882
883 /**
884 * Return the full title, including $par
885 *
886 * @return Title
887 * @since 1.18
888 */
889 public function getFullTitle() {
890 return $this->getContext()->getTitle();
891 }
892
893 /**
894 * Wrapper around wfMessage that sets the current context.
895 *
896 * @return Message
897 * @see wfMessage
898 */
899 public function msg( /* $args */ ) {
900 $message = call_user_func_array(
901 array( $this->getContext(), 'msg' ),
902 func_get_args()
903 );
904 // RequestContext passes context to wfMessage, and the language is set from
905 // the context, but setting the language for Message class removes the
906 // interface message status, which breaks for example usernameless gender
907 // invocations. Restore the flag when not including special page in content.
908 if ( $this->including() ) {
909 $message->setInterfaceMessageFlag( false );
910 }
911 return $message;
912 }
913
914 /**
915 * Adds RSS/atom links
916 *
917 * @param array $params
918 */
919 protected function addFeedLinks( $params ) {
920 global $wgFeedClasses;
921
922 $feedTemplate = wfScript( 'api' );
923
924 foreach ( $wgFeedClasses as $format => $class ) {
925 $theseParams = $params + array( 'feedformat' => $format );
926 $url = wfAppendQuery( $feedTemplate, $theseParams );
927 $this->getOutput()->addFeedLink( $format, $url );
928 }
929 }
930
931 /**
932 * Get the group that the special page belongs in on Special:SpecialPage
933 * Use this method, instead of getGroupName to allow customization
934 * of the group name from the wiki side
935 *
936 * @return string Group of this special page
937 * @since 1.21
938 */
939 public function getFinalGroupName() {
940 global $wgSpecialPageGroups;
941 $name = $this->getName();
942
943 // Allow overbidding the group from the wiki side
944 $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
945 if ( !$msg->isBlank() ) {
946 $group = $msg->text();
947 } else {
948 // Than use the group from this object
949 $group = $this->getGroupName();
950
951 // Group '-' is used as default to have the chance to determine,
952 // if the special pages overrides this method,
953 // if not overridden, $wgSpecialPageGroups is checked for b/c
954 if ( $group === '-' && isset( $wgSpecialPageGroups[$name] ) ) {
955 $group = $wgSpecialPageGroups[$name];
956 }
957 }
958
959 // never give '-' back, change to 'other'
960 if ( $group === '-' ) {
961 $group = 'other';
962 }
963
964 return $group;
965 }
966
967 /**
968 * Under which header this special page is listed in Special:SpecialPages
969 * See messages 'specialpages-group-*' for valid names
970 * This method defaults to group 'other'
971 *
972 * @return string
973 * @since 1.21
974 */
975 protected function getGroupName() {
976 // '-' used here to determine, if this group is overridden or has a hardcoded 'other'
977 // Needed for b/c in getFinalGroupName
978 return '-';
979 }
980 }