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 $page->getFinalGroupName();
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 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 "#")
262 * @throws MWException
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 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
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 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
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 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
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
420 * @param bool $listed
423 function setListed( $listed ) {
424 return wfSetVar( $this->mListed
, $listed );
427 * Get or set whether this special page is listed in Special:SpecialPages
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
517 function getLocalName() {
518 if ( !isset( $this->mLocalName
) ) {
519 $this->mLocalName
= SpecialPageFactory
::getLocalNameFor( $this->mName
);
521 return $this->mLocalName
;
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.
532 public function isExpensive() {
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.
545 public function isCached() {
550 * Can be overridden by subclasses with more complicated permissions
553 * @return bool Should the page be displayed with the restricted-access
556 public function isRestricted() {
557 // DWIM: If anons can do something, then it is not restricted
558 return $this->mRestriction
!= '' && !User
::groupHasPermission( '*', $this->mRestriction
);
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.
566 * @param User $user The user to check
567 * @return bool Does the user have permission to view the page?
569 public function userCanExecute( User
$user ) {
570 return $user->isAllowed( $this->mRestriction
);
574 * Output an error message telling the user what access level they have to have
575 * @throws PermissionsError
577 function displayRestrictionError() {
578 throw new PermissionsError( $this->mRestriction
);
582 * Checks if userCanExecute, and if not throws a PermissionsError
586 * @throws PermissionsError
588 public function checkPermissions() {
589 if ( !$this->userCanExecute( $this->getUser() ) ) {
590 $this->displayRestrictionError();
595 * If the wiki is currently in readonly mode, throws a ReadOnlyError
599 * @throws ReadOnlyError
601 public function checkReadOnly() {
602 if ( wfReadOnly() ) {
603 throw new ReadOnlyError
;
608 * If the user is not logged in, throws UserNotLoggedIn error.
610 * Default error message includes a link to Special:Userlogin with properly set 'returnto' query
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
622 public function requireLogin( $reasonMsg = null, $titleMsg = null ) {
623 if ( $this->getUser()->isAnon() ) {
624 // Use default messages if not given or explicit null passed
626 $reasonMsg = 'exception-nologin-text-manual';
629 $titleMsg = 'exception-nologin';
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(),
638 array( 'returnto' => $this->getPageTitle()->getPrefixedText() )
640 $reasonMsg = $this->msg( $reasonMsg )->rawParams( $loginreqlink );
642 if ( is_string( $titleMsg ) ) {
643 $titleMsg = $this->msg( $titleMsg );
646 throw new UserNotLoggedIn( $reasonMsg, $titleMsg );
651 * Sets headers - this should be called from the execute() method of all derived classes!
653 function setHeaders() {
654 $out = $this->getOutput();
655 $out->setArticleRelated( false );
656 $out->setRobotPolicy( "noindex,nofollow" );
657 $out->setPageTitle( $this->getDescription() );
665 * @param string|null $subPage
667 final public function run( $subPage ) {
669 * Gets called before @see SpecialPage::execute.
673 * @param SpecialPage $this
674 * @param string|null $subPage
676 wfRunHooks( 'SpecialPageBeforeExecute', array( $this, $subPage ) );
678 $this->beforeExecute( $subPage );
679 $this->execute( $subPage );
680 $this->afterExecute( $subPage );
683 * Gets called after @see SpecialPage::execute.
687 * @param SpecialPage $this
688 * @param string|null $subPage
690 wfRunHooks( 'SpecialPageAfterExecute', array( $this, $subPage ) );
694 * Gets called before @see SpecialPage::execute.
698 * @param string|null $subPage
700 protected function beforeExecute( $subPage ) {
705 * Gets called after @see SpecialPage::execute.
709 * @param string|null $subPage
711 protected function afterExecute( $subPage ) {
716 * Default execute method
717 * Checks user permissions, calls the function given in mFunction
719 * This must be overridden by subclasses; it will be made abstract in a future version
721 * @param string|null $subPage
723 public function execute( $subPage ) {
725 $this->checkPermissions();
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
;
732 $this->outputHeader();
733 call_user_func( $func, $subPage, $this );
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'
742 * @param string $summaryMessageKey Message key of the summary
744 function outputHeader( $summaryMessageKey = '' ) {
747 if ( $summaryMessageKey == '' ) {
748 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
750 $msg = $summaryMessageKey;
752 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
753 $this->getOutput()->wrapWikiMsg(
754 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
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
763 * Derived classes can override this, but usually it is easier to keep the
768 function getDescription() {
769 return $this->msg( strtolower( $this->mName
) )->text();
773 * Get a self-referential title object
775 * @param string|bool $subpage
777 * @deprecated in 1.23, use SpecialPage::getPageTitle
779 function getTitle( $subpage = false ) {
780 wfDeprecated( __METHOD__
, '1.23' );
781 return $this->getPageTitle( $subpage );
785 * Get a self-referential title object
787 * @param string|bool $subpage
791 function getPageTitle( $subpage = false ) {
792 return self
::getTitleFor( $this->mName
, $subpage );
796 * Sets the context this SpecialPage is executed in
798 * @param IContextSource $context
801 public function setContext( $context ) {
802 $this->mContext
= $context;
806 * Gets the context this SpecialPage is executed in
808 * @return IContextSource|RequestContext
811 public function getContext() {
812 if ( $this->mContext
instanceof IContextSource
) {
813 return $this->mContext
;
815 wfDebug( __METHOD__
. " called and \$mContext is null. " .
816 "Return RequestContext::getMain(); for sanity\n" );
817 return RequestContext
::getMain();
822 * Get the WebRequest being used for this instance
827 public function getRequest() {
828 return $this->getContext()->getRequest();
832 * Get the OutputPage being used for this instance
837 public function getOutput() {
838 return $this->getContext()->getOutput();
842 * Shortcut to get the User executing this instance
847 public function getUser() {
848 return $this->getContext()->getUser();
852 * Shortcut to get the skin being used for this instance
857 public function getSkin() {
858 return $this->getContext()->getSkin();
862 * Shortcut to get user's language
864 * @deprecated since 1.19 Use getLanguage instead
868 public function getLang() {
869 wfDeprecated( __METHOD__
, '1.19' );
870 return $this->getLanguage();
874 * Shortcut to get user's language
879 public function getLanguage() {
880 return $this->getContext()->getLanguage();
884 * Return the full title, including $par
889 public function getFullTitle() {
890 return $this->getContext()->getTitle();
894 * Wrapper around wfMessage that sets the current context.
899 public function msg( /* $args */ ) {
900 $message = call_user_func_array(
901 array( $this->getContext(), 'msg' ),
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 );
915 * Adds RSS/atom links
917 * @param array $params
919 protected function addFeedLinks( $params ) {
920 global $wgFeedClasses;
922 $feedTemplate = wfScript( 'api' );
924 foreach ( $wgFeedClasses as $format => $class ) {
925 $theseParams = $params +
array( 'feedformat' => $format );
926 $url = wfAppendQuery( $feedTemplate, $theseParams );
927 $this->getOutput()->addFeedLink( $format, $url );
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
936 * @return string Group of this special page
939 public function getFinalGroupName() {
940 global $wgSpecialPageGroups;
941 $name = $this->getName();
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();
948 // Than use the group from this object
949 $group = $this->getGroupName();
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];
959 // never give '-' back, change to 'other'
960 if ( $group === '-' ) {
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'
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