Merge "Moved ScopedCallback to /libs"
[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 SpecialPageFactory::getGroup( $page );
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 $name String
259 * @param string|Bool $subpage subpage string, or false to not use a subpage
260 * @param string $fragment the link fragment (after the "#")
261 * @throws MWException
262 * @return Title object
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 $name String
273 * @param string|Bool $subpage subpage string, or false to not use a subpage
274 * @return 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 Callback|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 Callback|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 r3583 (v1.3)
420 * @param $listed Bool
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 r11308 (v1.6)
429 * @param $x Bool
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 $x Bool
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 */
516 function getLocalName() {
517 if ( !isset( $this->mLocalName ) ) {
518 $this->mLocalName = SpecialPageFactory::getLocalNameFor( $this->mName );
519 }
520 return $this->mLocalName;
521 }
522
523 /**
524 * Is this page expensive (for some definition of expensive)?
525 * Expensive pages are disabled or cached in miser mode. Originally used
526 * (and still overridden) by QueryPage and subclasses, moved here so that
527 * Special:SpecialPages can safely call it for all special pages.
528 *
529 * @return Boolean
530 */
531 public function isExpensive() {
532 return false;
533 }
534
535 /**
536 * Is this page cached?
537 * Expensive pages are cached or disabled in miser mode.
538 * Used by QueryPage and subclasses, moved here so that
539 * Special:SpecialPages can safely call it for all special pages.
540 *
541 * @return Boolean
542 * @since 1.21
543 */
544 public function isCached() {
545 return false;
546 }
547
548 /**
549 * Can be overridden by subclasses with more complicated permissions
550 * schemes.
551 *
552 * @return Boolean: should the page be displayed with the restricted-access
553 * pages?
554 */
555 public function isRestricted() {
556 // DWIM: If anons can do something, then it is not restricted
557 return $this->mRestriction != '' && !User::groupHasPermission( '*', $this->mRestriction );
558 }
559
560 /**
561 * Checks if the given user (identified by an object) can execute this
562 * special page (as defined by $mRestriction). Can be overridden by sub-
563 * classes with more complicated permissions schemes.
564 *
565 * @param $user User: the user to check
566 * @return Boolean: does the user have permission to view the page?
567 */
568 public function userCanExecute( User $user ) {
569 return $user->isAllowed( $this->mRestriction );
570 }
571
572 /**
573 * Output an error message telling the user what access level they have to have
574 */
575 function displayRestrictionError() {
576 throw new PermissionsError( $this->mRestriction );
577 }
578
579 /**
580 * Checks if userCanExecute, and if not throws a PermissionsError
581 *
582 * @since 1.19
583 */
584 public function checkPermissions() {
585 if ( !$this->userCanExecute( $this->getUser() ) ) {
586 $this->displayRestrictionError();
587 }
588 }
589
590 /**
591 * If the wiki is currently in readonly mode, throws a ReadOnlyError
592 *
593 * @since 1.19
594 * @throws ReadOnlyError
595 */
596 public function checkReadOnly() {
597 if ( wfReadOnly() ) {
598 throw new ReadOnlyError;
599 }
600 }
601
602 /**
603 * If the user is not logged in, throws UserNotLoggedIn error.
604 *
605 * Default error message includes a link to Special:Userlogin with properly set 'returnto' query
606 * parameter.
607 *
608 * @since 1.23
609 * @param string|Message $reasonMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
610 * will be used as message keys. If a string is given, the message will also receive a
611 * formatted login link (generated using the 'loginreqlink' message) as first parameter. If a
612 * Message is given, it will be passed on verbatim.
613 * @param string|Message $titleMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
614 * will be used as message keys.
615 * @throws UserNotLoggedIn
616 */
617 public function requireLogin( $reasonMsg = null, $titleMsg = null ) {
618 if ( $this->getUser()->isAnon() ) {
619 // Use default messages if not given or explicit null passed
620 if ( !$reasonMsg ) {
621 $reasonMsg = 'exception-nologin-text-manual';
622 }
623 if ( !$titleMsg ) {
624 $titleMsg = 'exception-nologin';
625 }
626
627 // Convert to Messages with current context
628 if ( is_string( $reasonMsg ) ) {
629 $loginreqlink = Linker::linkKnown(
630 SpecialPage::getTitleFor( 'Userlogin' ),
631 $this->msg( 'loginreqlink' )->escaped(),
632 array(),
633 array( 'returnto' => $this->getPageTitle()->getPrefixedText() )
634 );
635 $reasonMsg = $this->msg( $reasonMsg )->rawParams( $loginreqlink );
636 }
637 if ( is_string( $titleMsg ) ) {
638 $titleMsg = $this->msg( $titleMsg );
639 }
640
641 throw new UserNotLoggedIn( $reasonMsg, $titleMsg );
642 }
643 }
644
645 /**
646 * Sets headers - this should be called from the execute() method of all derived classes!
647 */
648 function setHeaders() {
649 $out = $this->getOutput();
650 $out->setArticleRelated( false );
651 $out->setRobotPolicy( "noindex,nofollow" );
652 $out->setPageTitle( $this->getDescription() );
653 }
654
655 /**
656 * Entry point.
657 *
658 * @since 1.20
659 *
660 * @param $subPage string|null
661 */
662 final public function run( $subPage ) {
663 /**
664 * Gets called before @see SpecialPage::execute.
665 *
666 * @since 1.20
667 *
668 * @param $special SpecialPage
669 * @param $subPage string|null
670 */
671 wfRunHooks( 'SpecialPageBeforeExecute', array( $this, $subPage ) );
672
673 $this->beforeExecute( $subPage );
674 $this->execute( $subPage );
675 $this->afterExecute( $subPage );
676
677 /**
678 * Gets called after @see SpecialPage::execute.
679 *
680 * @since 1.20
681 *
682 * @param $special SpecialPage
683 * @param $subPage string|null
684 */
685 wfRunHooks( 'SpecialPageAfterExecute', array( $this, $subPage ) );
686 }
687
688 /**
689 * Gets called before @see SpecialPage::execute.
690 *
691 * @since 1.20
692 *
693 * @param $subPage string|null
694 */
695 protected function beforeExecute( $subPage ) {
696 // No-op
697 }
698
699 /**
700 * Gets called after @see SpecialPage::execute.
701 *
702 * @since 1.20
703 *
704 * @param $subPage string|null
705 */
706 protected function afterExecute( $subPage ) {
707 // No-op
708 }
709
710 /**
711 * Default execute method
712 * Checks user permissions, calls the function given in mFunction
713 *
714 * This must be overridden by subclasses; it will be made abstract in a future version
715 *
716 * @param $subPage string|null
717 */
718 public function execute( $subPage ) {
719 $this->setHeaders();
720 $this->checkPermissions();
721
722 $func = $this->mFunction;
723 // only load file if the function does not exist
724 if ( !is_callable( $func ) && $this->mFile ) {
725 require_once $this->mFile;
726 }
727 $this->outputHeader();
728 call_user_func( $func, $subPage, $this );
729 }
730
731 /**
732 * Outputs a summary message on top of special pages
733 * Per default the message key is the canonical name of the special page
734 * May be overridden, i.e. by extensions to stick with the naming conventions
735 * for message keys: 'extensionname-xxx'
736 *
737 * @param string $summaryMessageKey message key of the summary
738 */
739 function outputHeader( $summaryMessageKey = '' ) {
740 global $wgContLang;
741
742 if ( $summaryMessageKey == '' ) {
743 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
744 } else {
745 $msg = $summaryMessageKey;
746 }
747 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
748 $this->getOutput()->wrapWikiMsg(
749 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
750 }
751
752 }
753
754 /**
755 * Returns the name that goes in the \<h1\> in the special page itself, and
756 * also the name that will be listed in Special:Specialpages
757 *
758 * Derived classes can override this, but usually it is easier to keep the
759 * default behavior.
760 *
761 * @return string
762 */
763 function getDescription() {
764 return $this->msg( strtolower( $this->mName ) )->text();
765 }
766
767 /**
768 * Get a self-referential title object
769 *
770 * @param $subpage String|Bool
771 * @return Title object
772 * @deprecated in 1.23, use SpecialPage::getPageTitle
773 */
774 function getTitle( $subpage = false ) {
775 wfDeprecated( __METHOD__, '1.23' );
776 return $this->getPageTitle( $subpage );
777 }
778
779 /**
780 * Get a self-referential title object
781 *
782 * @param $subpage String|Bool
783 * @return Title object
784 */
785 function getPageTitle( $subpage = false ) {
786 return self::getTitleFor( $this->mName, $subpage );
787 }
788
789 /**
790 * Sets the context this SpecialPage is executed in
791 *
792 * @param $context IContextSource
793 * @since 1.18
794 */
795 public function setContext( $context ) {
796 $this->mContext = $context;
797 }
798
799 /**
800 * Gets the context this SpecialPage is executed in
801 *
802 * @return IContextSource|RequestContext
803 * @since 1.18
804 */
805 public function getContext() {
806 if ( $this->mContext instanceof IContextSource ) {
807 return $this->mContext;
808 } else {
809 wfDebug( __METHOD__ . " called and \$mContext is null. " .
810 "Return RequestContext::getMain(); for sanity\n" );
811 return RequestContext::getMain();
812 }
813 }
814
815 /**
816 * Get the WebRequest being used for this instance
817 *
818 * @return WebRequest
819 * @since 1.18
820 */
821 public function getRequest() {
822 return $this->getContext()->getRequest();
823 }
824
825 /**
826 * Get the OutputPage being used for this instance
827 *
828 * @return OutputPage
829 * @since 1.18
830 */
831 public function getOutput() {
832 return $this->getContext()->getOutput();
833 }
834
835 /**
836 * Shortcut to get the User executing this instance
837 *
838 * @return User
839 * @since 1.18
840 */
841 public function getUser() {
842 return $this->getContext()->getUser();
843 }
844
845 /**
846 * Shortcut to get the skin being used for this instance
847 *
848 * @return Skin
849 * @since 1.18
850 */
851 public function getSkin() {
852 return $this->getContext()->getSkin();
853 }
854
855 /**
856 * Shortcut to get user's language
857 *
858 * @deprecated since 1.19 Use getLanguage instead
859 * @return Language
860 * @since 1.18
861 */
862 public function getLang() {
863 wfDeprecated( __METHOD__, '1.19' );
864 return $this->getLanguage();
865 }
866
867 /**
868 * Shortcut to get user's language
869 *
870 * @return Language
871 * @since 1.19
872 */
873 public function getLanguage() {
874 return $this->getContext()->getLanguage();
875 }
876
877 /**
878 * Return the full title, including $par
879 *
880 * @return Title
881 * @since 1.18
882 */
883 public function getFullTitle() {
884 return $this->getContext()->getTitle();
885 }
886
887 /**
888 * Wrapper around wfMessage that sets the current context.
889 *
890 * @return Message
891 * @see wfMessage
892 */
893 public function msg( /* $args */ ) {
894 $message = call_user_func_array(
895 array( $this->getContext(), 'msg' ),
896 func_get_args()
897 );
898 // RequestContext passes context to wfMessage, and the language is set from
899 // the context, but setting the language for Message class removes the
900 // interface message status, which breaks for example usernameless gender
901 // invocations. Restore the flag when not including special page in content.
902 if ( $this->including() ) {
903 $message->setInterfaceMessageFlag( false );
904 }
905 return $message;
906 }
907
908 /**
909 * Adds RSS/atom links
910 *
911 * @param $params array
912 */
913 protected function addFeedLinks( $params ) {
914 global $wgFeedClasses;
915
916 $feedTemplate = wfScript( 'api' );
917
918 foreach ( $wgFeedClasses as $format => $class ) {
919 $theseParams = $params + array( 'feedformat' => $format );
920 $url = wfAppendQuery( $feedTemplate, $theseParams );
921 $this->getOutput()->addFeedLink( $format, $url );
922 }
923 }
924
925 /**
926 * Get the group that the special page belongs in on Special:SpecialPage
927 * Use this method, instead of getGroupName to allow customization
928 * of the group name from the wiki side
929 *
930 * @return string Group of this special page
931 * @since 1.21
932 */
933 public function getFinalGroupName() {
934 global $wgSpecialPageGroups;
935 $name = $this->getName();
936
937 // Allow overbidding the group from the wiki side
938 $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
939 if ( !$msg->isBlank() ) {
940 $group = $msg->text();
941 } else {
942 // Than use the group from this object
943 $group = $this->getGroupName();
944
945 // Group '-' is used as default to have the chance to determine,
946 // if the special pages overrides this method,
947 // if not overridden, $wgSpecialPageGroups is checked for b/c
948 if ( $group === '-' && isset( $wgSpecialPageGroups[$name] ) ) {
949 $group = $wgSpecialPageGroups[$name];
950 }
951 }
952
953 // never give '-' back, change to 'other'
954 if ( $group === '-' ) {
955 $group = 'other';
956 }
957
958 return $group;
959 }
960
961 /**
962 * Under which header this special page is listed in Special:SpecialPages
963 * See messages 'specialpages-group-*' for valid names
964 * This method defaults to group 'other'
965 *
966 * @return string
967 * @since 1.21
968 */
969 protected function getGroupName() {
970 // '-' used here to determine, if this group is overridden or has a hardcoded 'other'
971 // Needed for b/c in getFinalGroupName
972 return '-';
973 }
974 }