3 * Parent class for all special pages.
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.
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.
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
21 * @ingroup SpecialPage
25 * Parent class for all special pages.
27 * Includes some static functions for handling the special page list deprecated
28 * in favor of SpecialPageFactory.
30 * @todo Turn this into a real ContextSource
31 * @ingroup SpecialPage
34 // The canonical name of this special page
35 // Also used for the default <h1> heading, @see getDescription()
38 // The local name of this special page
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;
45 // Listed in Special:Specialpages?
48 // Function name called by the default execute()
51 // File which needs to be included before the function above can be called
54 // Whether or not this special page is being included from an article
55 protected $mIncluding;
57 // Whether the special page can be included in an article
58 protected $mIncludable;
61 * Current request context
67 * Initialise the special page list
68 * This must be called before accessing SpecialPage::$mList
69 * @deprecated since 1.18
71 static function initList() {
72 wfDeprecated( __METHOD__
, '1.18' );
77 * @deprecated since 1.18
79 static function initAliasList() {
80 wfDeprecated( __METHOD__
, '1.18' );
85 * Given a special page alias, return the special page name.
86 * Returns false if there is no such alias.
88 * @param $alias String
89 * @return String or false
90 * @deprecated since 1.18 call SpecialPageFactory method directly
92 static function resolveAlias( $alias ) {
93 wfDeprecated( __METHOD__
, '1.18' );
94 list( $name, /*...*/ ) = SpecialPageFactory
::resolveAlias( $alias );
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
103 * @param $alias String
105 * @deprecated since 1.18 call SpecialPageFactory method directly
107 static function resolveAliasWithSubpage( $alias ) {
108 return SpecialPageFactory
::resolveAlias( $alias );
112 * Add a page to a certain display group for Special:SpecialPages
114 * @param $page Mixed: SpecialPage or string
115 * @param $group String
116 * @deprecated since 1.18 call SpecialPageFactory method directly
118 static function setGroup( $page, $group ) {
119 wfDeprecated( __METHOD__
, '1.18' );
120 SpecialPageFactory
::setGroup( $page, $group );
124 * Get the group that the special page belongs in on Special:SpecialPage
126 * @param $page SpecialPage
128 * @deprecated since 1.18 call SpecialPageFactory method directly
130 static function getGroup( &$page ) {
131 wfDeprecated( __METHOD__
, '1.18' );
132 return SpecialPageFactory
::getGroup( $page );
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
141 * @param string $name the page to remove
143 static function removePage( $name ) {
144 wfDeprecated( __METHOD__
, '1.18' );
145 unset( SpecialPageFactory
::getList()->$name );
149 * Check if a given name exist as a special page or as a special page alias
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
155 static function exists( $name ) {
156 wfDeprecated( __METHOD__
, '1.18' );
157 return SpecialPageFactory
::exists( $name );
161 * Find the object with a given name and return it (or NULL)
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
167 static function getPage( $name ) {
168 wfDeprecated( __METHOD__
, '1.18' );
169 return SpecialPageFactory
::getPage( $name );
173 * Get a special page with a given localised name, or NULL if there
174 * is no such special page.
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
180 static function getPageByAlias( $alias ) {
181 wfDeprecated( __METHOD__
, '1.18' );
182 return SpecialPageFactory
::getPage( $alias );
186 * Return categorised listable special pages which are available
187 * for the current user, and everyone.
189 * @param $user User object to check permissions, $wgUser will be used
191 * @return array Associative array mapping page's name to its SpecialPage object
192 * @deprecated since 1.18 call SpecialPageFactory method directly
194 static function getUsablePages( User
$user = null ) {
195 wfDeprecated( __METHOD__
, '1.18' );
196 return SpecialPageFactory
::getUsablePages( $user );
200 * Return categorised listable special pages for all users
202 * @return array Associative array mapping page's name to its SpecialPage object
203 * @deprecated since 1.18 call SpecialPageFactory method directly
205 static function getRegularPages() {
206 wfDeprecated( __METHOD__
, '1.18' );
207 return SpecialPageFactory
::getRegularPages();
211 * Return categorised listable special pages which are available
212 * for the current user, but not for everyone
214 * @return array Associative array mapping page's name to its SpecialPage object
215 * @deprecated since 1.18 call SpecialPageFactory method directly
217 static function getRestrictedPages() {
218 wfDeprecated( __METHOD__
, '1.18' );
219 return SpecialPageFactory
::getRestrictedPages();
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
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.
230 * @param $title Title object
231 * @param $context IContextSource
232 * @param $including Bool output is being captured for use in {{special:whatever}}
234 * @deprecated since 1.18 call SpecialPageFactory method directly
236 public static function executePath( &$title, IContextSource
&$context, $including = false ) {
237 wfDeprecated( __METHOD__
, '1.18' );
238 return SpecialPageFactory
::executePath( $title, $context, $including );
242 * Get the local name for a specified canonical name
244 * @param $name String
245 * @param $subpage Mixed: boolean false, or string
248 * @deprecated since 1.18 call SpecialPageFactory method directly
250 static function getLocalNameFor( $name, $subpage = false ) {
251 wfDeprecated( __METHOD__
, '1.18' );
252 return SpecialPageFactory
::getLocalNameFor( $name, $subpage );
256 * Get a localised Title object for a specified special page name
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
264 public static function getTitleFor( $name, $subpage = false, $fragment = '' ) {
265 $name = SpecialPageFactory
::getLocalNameFor( $name, $subpage );
266 return Title
::makeTitle( NS_SPECIAL
, $name, $fragment );
270 * Get a localised Title object for a page name with a possibly unvalidated subpage
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
276 public static function getSafeTitleFor( $name, $subpage = false ) {
277 $name = SpecialPageFactory
::getLocalNameFor( $name, $subpage );
279 return Title
::makeTitleSafe( NS_SPECIAL
, $name );
286 * Get a title for a given alias
288 * @param $alias String
289 * @return Title or null if there is no such alias
290 * @deprecated since 1.18 call SpecialPageFactory method directly
292 static function getTitleForAlias( $alias ) {
293 wfDeprecated( __METHOD__
, '1.18' );
294 return SpecialPageFactory
::getTitleForAlias( $alias );
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
304 * If you override execute(), you can recover the default behavior with userCanExecute()
305 * and displayRestrictionError()
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
316 public function __construct(
317 $name = '', $restriction = '', $listed = true,
318 $function = false, $file = 'default', $includable = false
320 $this->init( $name, $restriction, $listed, $function, $file, $includable );
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
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;
341 $this->mFunction
= 'wfSpecial' . $name;
343 $this->mFunction
= $function;
345 if ( $file === 'default' ) {
346 $this->mFile
= __DIR__
. "/specials/Special$name.php";
348 $this->mFile
= $file;
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()
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()
361 public function __call( $fName, $a ) {
362 // Deprecated messages now, remove in 1.19 or 1.20?
363 wfDeprecated( __METHOD__
, '1.17' );
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 );
375 $className = get_class( $this );
376 throw new MWException( "Call to undefined method $className::$fName" );
381 * Get the name of this Special Page.
389 * Get the permission that a user must have to execute this page
392 function getRestriction() {
393 return $this->mRestriction
;
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.
401 * @deprecated since 1.18
404 wfDeprecated( __METHOD__
, '1.18' );
408 // @todo FIXME: Decide which syntax to use for this, and stick to it
410 * Whether this special page is listed in Special:SpecialPages
411 * @since r3583 (v1.3)
414 function isListed() {
415 return $this->mListed
;
418 * Set whether this page is listed in Special:Specialpages, at run-time
419 * @since r3583 (v1.3)
420 * @param $listed Bool
423 function setListed( $listed ) {
424 return wfSetVar( $this->mListed
, $listed );
427 * Get or set whether this special page is listed in Special:SpecialPages
428 * @since r11308 (v1.6)
432 function listed( $x = null ) {
433 return wfSetVar( $this->mListed
, $x );
437 * Whether it's allowed to transclude the special page via {{Special:Foo/params}}
440 public function isIncludable() {
441 return $this->mIncludable
;
445 * These mutators are very evil, as the relevant variables should not mutate. So
449 * @deprecated since 1.18
451 function name( $x = null ) {
452 wfDeprecated( __METHOD__
, '1.18' );
453 return wfSetVar( $this->mName
, $x );
457 * These mutators are very evil, as the relevant variables should not mutate. So
461 * @deprecated since 1.18
463 function restriction( $x = null ) {
464 wfDeprecated( __METHOD__
, '1.18' );
465 return wfSetVar( $this->mRestriction
, $x );
469 * These mutators are very evil, as the relevant variables should not mutate. So
473 * @deprecated since 1.18
475 function func( $x = null ) {
476 wfDeprecated( __METHOD__
, '1.18' );
477 return wfSetVar( $this->mFunction
, $x );
481 * These mutators are very evil, as the relevant variables should not mutate. So
485 * @deprecated since 1.18
487 function file( $x = null ) {
488 wfDeprecated( __METHOD__
, '1.18' );
489 return wfSetVar( $this->mFile
, $x );
493 * These mutators are very evil, as the relevant variables should not mutate. So
497 * @deprecated since 1.18
499 function includable( $x = null ) {
500 wfDeprecated( __METHOD__
, '1.18' );
501 return wfSetVar( $this->mIncludable
, $x );
505 * Whether the special page is being evaluated via transclusion
509 function including( $x = null ) {
510 return wfSetVar( $this->mIncluding
, $x );
514 * Get the localised name of the special page
516 function getLocalName() {
517 if ( !isset( $this->mLocalName
) ) {
518 $this->mLocalName
= SpecialPageFactory
::getLocalNameFor( $this->mName
);
520 return $this->mLocalName
;
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.
531 public function isExpensive() {
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.
544 public function isCached() {
549 * Can be overridden by subclasses with more complicated permissions
552 * @return Boolean: should the page be displayed with the restricted-access
555 public function isRestricted() {
556 // DWIM: If anons can do something, then it is not restricted
557 return $this->mRestriction
!= '' && !User
::groupHasPermission( '*', $this->mRestriction
);
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.
565 * @param $user User: the user to check
566 * @return Boolean: does the user have permission to view the page?
568 public function userCanExecute( User
$user ) {
569 return $user->isAllowed( $this->mRestriction
);
573 * Output an error message telling the user what access level they have to have
575 function displayRestrictionError() {
576 throw new PermissionsError( $this->mRestriction
);
580 * Checks if userCanExecute, and if not throws a PermissionsError
584 public function checkPermissions() {
585 if ( !$this->userCanExecute( $this->getUser() ) ) {
586 $this->displayRestrictionError();
591 * If the wiki is currently in readonly mode, throws a ReadOnlyError
594 * @throws ReadOnlyError
596 public function checkReadOnly() {
597 if ( wfReadOnly() ) {
598 throw new ReadOnlyError
;
603 * If the user is not logged in, throws UserNotLoggedIn error.
605 * Default error message includes a link to Special:Userlogin with properly set 'returnto' query
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
617 public function requireLogin( $reasonMsg = null, $titleMsg = null ) {
618 if ( $this->getUser()->isAnon() ) {
619 // Use default messages if not given or explicit null passed
621 $reasonMsg = 'exception-nologin-text-manual';
624 $titleMsg = 'exception-nologin';
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(),
633 array( 'returnto' => $this->getPageTitle()->getPrefixedText() )
635 $reasonMsg = $this->msg( $reasonMsg )->rawParams( $loginreqlink );
637 if ( is_string( $titleMsg ) ) {
638 $titleMsg = $this->msg( $titleMsg );
641 throw new UserNotLoggedIn( $reasonMsg, $titleMsg );
646 * Sets headers - this should be called from the execute() method of all derived classes!
648 function setHeaders() {
649 $out = $this->getOutput();
650 $out->setArticleRelated( false );
651 $out->setRobotPolicy( "noindex,nofollow" );
652 $out->setPageTitle( $this->getDescription() );
660 * @param $subPage string|null
662 final public function run( $subPage ) {
664 * Gets called before @see SpecialPage::execute.
668 * @param $special SpecialPage
669 * @param $subPage string|null
671 wfRunHooks( 'SpecialPageBeforeExecute', array( $this, $subPage ) );
673 $this->beforeExecute( $subPage );
674 $this->execute( $subPage );
675 $this->afterExecute( $subPage );
678 * Gets called after @see SpecialPage::execute.
682 * @param $special SpecialPage
683 * @param $subPage string|null
685 wfRunHooks( 'SpecialPageAfterExecute', array( $this, $subPage ) );
689 * Gets called before @see SpecialPage::execute.
693 * @param $subPage string|null
695 protected function beforeExecute( $subPage ) {
700 * Gets called after @see SpecialPage::execute.
704 * @param $subPage string|null
706 protected function afterExecute( $subPage ) {
711 * Default execute method
712 * Checks user permissions, calls the function given in mFunction
714 * This must be overridden by subclasses; it will be made abstract in a future version
716 * @param $subPage string|null
718 public function execute( $subPage ) {
720 $this->checkPermissions();
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
;
727 $this->outputHeader();
728 call_user_func( $func, $subPage, $this );
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'
737 * @param string $summaryMessageKey message key of the summary
739 function outputHeader( $summaryMessageKey = '' ) {
742 if ( $summaryMessageKey == '' ) {
743 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
745 $msg = $summaryMessageKey;
747 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
748 $this->getOutput()->wrapWikiMsg(
749 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
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
758 * Derived classes can override this, but usually it is easier to keep the
763 function getDescription() {
764 return $this->msg( strtolower( $this->mName
) )->text();
768 * Get a self-referential title object
770 * @param $subpage String|Bool
771 * @return Title object
772 * @deprecated in 1.23, use SpecialPage::getPageTitle
774 function getTitle( $subpage = false ) {
775 wfDeprecated( __METHOD__
, '1.23' );
776 return $this->getPageTitle( $subpage );
780 * Get a self-referential title object
782 * @param $subpage String|Bool
783 * @return Title object
785 function getPageTitle( $subpage = false ) {
786 return self
::getTitleFor( $this->mName
, $subpage );
790 * Sets the context this SpecialPage is executed in
792 * @param $context IContextSource
795 public function setContext( $context ) {
796 $this->mContext
= $context;
800 * Gets the context this SpecialPage is executed in
802 * @return IContextSource|RequestContext
805 public function getContext() {
806 if ( $this->mContext
instanceof IContextSource
) {
807 return $this->mContext
;
809 wfDebug( __METHOD__
. " called and \$mContext is null. " .
810 "Return RequestContext::getMain(); for sanity\n" );
811 return RequestContext
::getMain();
816 * Get the WebRequest being used for this instance
821 public function getRequest() {
822 return $this->getContext()->getRequest();
826 * Get the OutputPage being used for this instance
831 public function getOutput() {
832 return $this->getContext()->getOutput();
836 * Shortcut to get the User executing this instance
841 public function getUser() {
842 return $this->getContext()->getUser();
846 * Shortcut to get the skin being used for this instance
851 public function getSkin() {
852 return $this->getContext()->getSkin();
856 * Shortcut to get user's language
858 * @deprecated since 1.19 Use getLanguage instead
862 public function getLang() {
863 wfDeprecated( __METHOD__
, '1.19' );
864 return $this->getLanguage();
868 * Shortcut to get user's language
873 public function getLanguage() {
874 return $this->getContext()->getLanguage();
878 * Return the full title, including $par
883 public function getFullTitle() {
884 return $this->getContext()->getTitle();
888 * Wrapper around wfMessage that sets the current context.
893 public function msg( /* $args */ ) {
894 $message = call_user_func_array(
895 array( $this->getContext(), 'msg' ),
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 );
909 * Adds RSS/atom links
911 * @param $params array
913 protected function addFeedLinks( $params ) {
914 global $wgFeedClasses;
916 $feedTemplate = wfScript( 'api' );
918 foreach ( $wgFeedClasses as $format => $class ) {
919 $theseParams = $params +
array( 'feedformat' => $format );
920 $url = wfAppendQuery( $feedTemplate, $theseParams );
921 $this->getOutput()->addFeedLink( $format, $url );
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
930 * @return string Group of this special page
933 public function getFinalGroupName() {
934 global $wgSpecialPageGroups;
935 $name = $this->getName();
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();
942 // Than use the group from this object
943 $group = $this->getGroupName();
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];
953 // never give '-' back, change to 'other'
954 if ( $group === '-' ) {
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'
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