3 * SpecialPage: handling special pages and lists thereof.
5 * To add a special page in an extension, add to $wgSpecialPages either
6 * an object instance or an array containing the name and constructor
7 * parameters. The latter is preferred for performance reasons.
9 * The object instantiated must be either an instance of SpecialPage or a
10 * sub-class thereof. It must have an execute() method, which sends the HTML
11 * for the special page to $wgOut. The parent class has an execute() method
12 * which distributes the call to the historical global functions. Additionally,
13 * execute() also checks if the user has the necessary access privileges
14 * and bails out if not.
16 * To add a core special page, use the similar static list in
17 * SpecialPage::$mList. To remove a core static special page at runtime, use
18 * a SpecialPage_initList hook.
21 * @ingroup SpecialPage
22 * @defgroup SpecialPage SpecialPage
26 * Parent special page class, also static functions for handling the special
28 * @ingroup SpecialPage
35 * The canonical name of this special page
36 * Also used for the default <h1> heading, @see getDescription()
40 * The local name of this special page
44 * Minimum user level required to access this page, or "" for anyone.
45 * Also used to categorise the pages in Special:Specialpages
49 * Listed in Special:Specialpages?
53 * Function name called by the default execute()
57 * File which needs to be included before the function above can be called
61 * Whether or not this special page is being included from an article
65 * Whether the special page can be included in an article
69 * Query parameters that can be passed through redirects
71 var $mAllowedRedirectParams = array();
73 * Query parameteres added by redirects
75 var $mAddedRedirectParams = array();
77 * Current request context
83 * Initialise the special page list
84 * This must be called before accessing SpecialPage::$mList
85 * @deprecated since 1.18
87 static function initList() {
92 * @deprecated since 1.18
94 static function initAliasList() {
99 * Given a special page alias, return the special page name.
100 * Returns false if there is no such alias.
102 * @param $alias String
103 * @return String or false
104 * @deprecated since 1.18 call SpecialPageFactory method directly
106 static function resolveAlias( $alias ) {
107 list( $name, /*...*/ ) = SpecialPageFactory
::resolveAlias( $alias );
112 * Given a special page name with a possible subpage, return an array
113 * where the first element is the special page name and the second is the
116 * @param $alias String
118 * @deprecated since 1.18 call SpecialPageFactory method directly
120 static function resolveAliasWithSubpage( $alias ) {
121 return SpecialPageFactory
::resolveAlias( $alias );
125 * Add a page to the list of valid special pages. This used to be the preferred
126 * method for adding special pages in extensions. It's now suggested that you add
127 * an associative record to $wgSpecialPages. This avoids autoloading SpecialPage.
129 * @param $page SpecialPage
130 * @deprecated in 1.7, warnings in 1.17, might be removed in 1.20
132 static function addPage( &$page ) {
133 wfDeprecated( __METHOD__
);
134 SpecialPageFactory
::getList()->{$page->mName
} = $page;
138 * Add a page to a certain display group for Special:SpecialPages
140 * @param $page Mixed: SpecialPage or string
141 * @param $group String
142 * @deprecated since 1.18 call SpecialPageFactory method directly
144 static function setGroup( $page, $group ) {
145 return SpecialPageFactory
::setGroup( $page, $group );
149 * Add a page to a certain display group for Special:SpecialPages
151 * @param $page SpecialPage
152 * @deprecated since 1.18 call SpecialPageFactory method directly
154 static function getGroup( &$page ) {
155 return SpecialPageFactory
::getGroup( $page );
159 * Remove a special page from the list
160 * Formerly used to disable expensive or dangerous special pages. The
161 * preferred method is now to add a SpecialPage_initList hook.
162 * @deprecated since 1.18
164 static function removePage( $name ) {
165 unset( SpecialPageFactory
::getList()->$name );
169 * Check if a given name exist as a special page or as a special page alias
171 * @param $name String: name of a special page
172 * @return Boolean: true if a special page exists with this name
173 * @deprecated since 1.18 call SpecialPageFactory method directly
175 static function exists( $name ) {
176 return SpecialPageFactory
::exists( $name );
180 * Find the object with a given name and return it (or NULL)
182 * @param $name String
183 * @return SpecialPage object or null if the page doesn't exist
184 * @deprecated since 1.18 call SpecialPageFactory method directly
186 static function getPage( $name ) {
187 return SpecialPageFactory
::getPage( $name );
191 * Get a special page with a given localised name, or NULL if there
192 * is no such special page.
194 * @return SpecialPage object or null if the page doesn't exist
195 * @deprecated since 1.18 call SpecialPageFactory method directly
197 static function getPageByAlias( $alias ) {
198 return SpecialPageFactory
::getPage( $alias );
202 * Return categorised listable special pages which are available
203 * for the current user, and everyone.
205 * @return Associative array mapping page's name to its SpecialPage object
206 * @deprecated since 1.18 call SpecialPageFactory method directly
208 static function getUsablePages() {
209 return SpecialPageFactory
::getUsablePages();
213 * Return categorised listable special pages for all users
215 * @return Associative array mapping page's name to its SpecialPage object
216 * @deprecated since 1.18 call SpecialPageFactory method directly
218 static function getRegularPages() {
219 return SpecialPageFactory
::getRegularPages();
223 * Return categorised listable special pages which are available
224 * for the current user, but not for everyone
226 * @return Associative array mapping page's name to its SpecialPage object
227 * @deprecated since 1.18 call SpecialPageFactory method directly
229 static function getRestrictedPages() {
230 return SpecialPageFactory
::getRestrictedPages();
234 * Execute a special page path.
235 * The path may contain parameters, e.g. Special:Name/Params
236 * Extracts the special page name and call the execute method, passing the parameters
238 * Returns a title object if the page is redirected, false if there was no such special
239 * page, and true if it was successful.
241 * @param $title Title object
242 * @param $context RequestContext
243 * @param $including Bool output is being captured for use in {{special:whatever}}
244 * @deprecated since 1.18 call SpecialPageFactory method directly
246 public static function executePath( &$title, RequestContext
&$context, $including = false ) {
247 return SpecialPageFactory
::executePath( $title, $context, $including );
251 * Just like executePath() except it returns the HTML instead of outputting it
252 * Returns false if there was no such special page, or a title object if it was
255 * @return String: HTML fragment
256 * @deprecated since 1.18 call SpecialPageFactory method directly
258 static function capturePath( &$title ) {
259 return SpecialPageFactory
::capturePath( $title );
263 * Get the local name for a specified canonical name
265 * @param $name String
266 * @param $subpage Mixed: boolean false, or string
269 * @deprecated since 1.18 call SpecialPageFactory method directly
271 static function getLocalNameFor( $name, $subpage = false ) {
272 return SpecialPageFactory
::getLocalNameFor( $name, $subpage );
276 * Get a localised Title object for a specified special page name
278 * @return Title object
280 public static function getTitleFor( $name, $subpage = false ) {
281 $name = SpecialPageFactory
::getLocalNameFor( $name, $subpage );
283 return Title
::makeTitle( NS_SPECIAL
, $name );
285 throw new MWException( "Invalid special page name \"$name\"" );
290 * Get a localised Title object for a page name with a possibly unvalidated subpage
292 * @return Title object or null if the page doesn't exist
294 public static function getSafeTitleFor( $name, $subpage = false ) {
295 $name = SpecialPageFactory
::getLocalNameFor( $name, $subpage );
297 return Title
::makeTitleSafe( NS_SPECIAL
, $name );
304 * Get a title for a given alias
306 * @return Title or null if there is no such alias
307 * @deprecated since 1.18 call SpecialPageFactory method directly
309 static function getTitleForAlias( $alias ) {
310 return SpecialPageFactory
::getTitleForAlias( $alias );
314 * Default constructor for special pages
315 * Derivative classes should call this from their constructor
316 * Note that if the user does not have the required level, an error message will
317 * be displayed by the default execute() method, without the global function ever
320 * If you override execute(), you can recover the default behaviour with userCanExecute()
321 * and displayRestrictionError()
323 * @param $name String: name of the special page, as seen in links and URLs
324 * @param $restriction String: user right required, e.g. "block" or "delete"
325 * @param $listed Boolean: whether the page is listed in Special:Specialpages
326 * @param $function Callback: function called by execute(). By default it is constructed from $name
327 * @param $file String: file which is included by execute(). It is also constructed from $name by default
328 * @param $includable Boolean: whether the page can be included in normal pages
330 public function __construct( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
331 $this->init( $name, $restriction, $listed, $function, $file, $includable );
335 * Do the real work for the constructor, mainly so __call() can intercept
336 * calls to SpecialPage()
337 * @see __construct() for param docs
339 private function init( $name, $restriction, $listed, $function, $file, $includable ) {
340 $this->mName
= $name;
341 $this->mRestriction
= $restriction;
342 $this->mListed
= $listed;
343 $this->mIncludable
= $includable;
345 $this->mFunction
= 'wfSpecial'.$name;
347 $this->mFunction
= $function;
349 if ( $file === 'default' ) {
350 $this->mFile
= dirname(__FILE__
) . "/specials/Special$name.php";
352 $this->mFile
= $file;
357 * Use PHP's magic __call handler to get calls to the old PHP4 constructor
358 * because PHP E_STRICT yells at you for having __construct() and SpecialPage()
360 * @param $fName String Name of called method
361 * @param $a Array Arguments to the method
362 * @deprecated since 1.17, call parent::__construct()
364 public function __call( $fName, $a ) {
365 // Sometimes $fName is SpecialPage, sometimes it's specialpage. <3 PHP
366 if( strtolower( $fName ) == 'specialpage' ) {
367 // Deprecated messages now, remove in 1.19 or 1.20?
368 wfDeprecated( __METHOD__
);
370 $name = isset( $a[0] ) ?
$a[0] : '';
371 $restriction = isset( $a[1] ) ?
$a[1] : '';
372 $listed = isset( $a[2] ) ?
$a[2] : true;
373 $function = isset( $a[3] ) ?
$a[3] : false;
374 $file = isset( $a[4] ) ?
$a[4] : 'default';
375 $includable = isset( $a[5] ) ?
$a[5] : false;
376 $this->init( $name, $restriction, $listed, $function, $file, $includable );
378 $className = get_class( $this );
379 throw new MWException( "Call to undefined method $className::$fName" );
383 function getName() { return $this->mName
; }
384 function getRestriction() { return $this->mRestriction
; }
385 function getFile() { return $this->mFile
; }
386 function isListed() { return $this->mListed
; }
388 function name( $x = null ) { return wfSetVar( $this->mName
, $x ); }
389 function restrictions( $x = null) {
390 # Use the one below this
391 wfDeprecated( __METHOD__
);
392 return wfSetVar( $this->mRestriction
, $x );
394 function restriction( $x = null) { return wfSetVar( $this->mRestriction
, $x ); }
395 function listed( $x = null) { return wfSetVar( $this->mListed
, $x ); }
396 function func( $x = null) { return wfSetVar( $this->mFunction
, $x ); }
397 function file( $x = null) { return wfSetVar( $this->mFile
, $x ); }
398 function includable( $x = null ) { return wfSetVar( $this->mIncludable
, $x ); }
399 function including( $x = null ) { return wfSetVar( $this->mIncluding
, $x ); }
402 * Get the localised name of the special page
404 function getLocalName() {
405 if ( !isset( $this->mLocalName
) ) {
406 $this->mLocalName
= SpecialPageFactory
::getLocalNameFor( $this->mName
);
408 return $this->mLocalName
;
412 * Is this page expensive (for some definition of expensive)?
413 * Expensive pages are disabled or cached in miser mode. Originally used
414 * (and still overridden) by QueryPage and subclasses, moved here so that
415 * Special:SpecialPages can safely call it for all special pages.
419 public function isExpensive() {
424 * Can be overridden by subclasses with more complicated permissions
427 * @return Boolean: should the page be displayed with the restricted-access
430 public function isRestricted() {
431 global $wgGroupPermissions;
432 // DWIM: If all anons can do something, then it is not restricted
433 return $this->mRestriction
!= '' && empty($wgGroupPermissions['*'][$this->mRestriction
]);
437 * Checks if the given user (identified by an object) can execute this
438 * special page (as defined by $mRestriction). Can be overridden by sub-
439 * classes with more complicated permissions schemes.
441 * @param $user User: the user to check
442 * @return Boolean: does the user have permission to view the page?
444 public function userCanExecute( $user ) {
445 return $user->isAllowed( $this->mRestriction
);
449 * Output an error message telling the user what access level they have to have
451 function displayRestrictionError() {
452 throw new PermissionsError( $this->mRestriction
);
456 * Sets headers - this should be called from the execute() method of all derived classes!
458 function setHeaders() {
459 $out = $this->getOutput();
460 $out->setArticleRelated( false );
461 $out->setRobotPolicy( "noindex,nofollow" );
462 $out->setPageTitle( $this->getDescription() );
466 * Default execute method
467 * Checks user permissions, calls the function given in mFunction
469 * This must be overridden by subclasses; it will be made abstract in a future version
471 function execute( $par ) {
474 if ( $this->userCanExecute( $this->getUser() ) ) {
475 $func = $this->mFunction
;
476 // only load file if the function does not exist
477 if(!is_callable($func) and $this->mFile
) {
478 require_once( $this->mFile
);
480 $this->outputHeader();
481 call_user_func( $func, $par, $this );
483 $this->displayRestrictionError();
488 * Outputs a summary message on top of special pages
489 * Per default the message key is the canonical name of the special page
490 * May be overriden, i.e. by extensions to stick with the naming conventions
491 * for message keys: 'extensionname-xxx'
493 * @param $summaryMessageKey String: message key of the summary
495 function outputHeader( $summaryMessageKey = '' ) {
498 if( $summaryMessageKey == '' ) {
499 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
501 $msg = $summaryMessageKey;
503 if ( !wfMessage( $msg )->isBlank() and ! $this->including() ) {
504 $this->getOutput()->wrapWikiMsg( "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
510 * Returns the name that goes in the \<h1\> in the special page itself, and
511 * also the name that will be listed in Special:Specialpages
513 * Derived classes can override this, but usually it is easier to keep the
514 * default behaviour. Messages can be added at run-time, see
519 function getDescription() {
520 return wfMsg( strtolower( $this->mName
) );
524 * Get a self-referential title object
526 * @return Title object
528 function getTitle( $subpage = false ) {
529 return self
::getTitleFor( $this->mName
, $subpage );
533 * Set whether this page is listed in Special:Specialpages, at run-time
537 function setListed( $listed ) {
538 return wfSetVar( $this->mListed
, $listed );
542 * If the special page is a redirect, then get the Title object it redirects to.
545 * @return Title|false
547 function getRedirect( $subpage ) {
552 * Return part of the request string for a special redirect page
553 * This allows passing, e.g. action=history to Special:Mypage, etc.
557 function getRedirectQuery() {
560 foreach( $this->mAllowedRedirectParams
as $arg ) {
561 if( $this->getContext()->request
->getVal( $arg, null ) !== null ){
562 $params[$arg] = $this->getContext()->request
->getVal( $arg );
566 foreach( $this->mAddedRedirectParams
as $arg => $val ) {
567 $params[$arg] = $val;
570 return count( $params )
576 * Sets the context this SpecialPage is executed in
578 * @param $context RequestContext
581 public function setContext( $context ) {
582 $this->mContext
= $context;
586 * Gets the context this SpecialPage is executed in
588 * @return RequestContext
591 public function getContext() {
592 if ( $this->mContext
instanceof RequestContext
) {
593 return $this->mContext
;
595 wfDebug( __METHOD__
. " called and \$mContext is null. Return RequestContext::getMain(); for sanity\n" );
596 return RequestContext
::getMain();
601 * Get the WebRequest being used for this instance
606 public function getRequest() {
607 return $this->getContext()->getRequest();
611 * Get the OutputPage being used for this instance
616 public function getOutput() {
617 return $this->getContext()->getOutput();
621 * Shortcut to get the skin being used for this instance
626 public function getUser() {
627 return $this->getContext()->getUser();
631 * Shortcut to get the skin being used for this instance
636 public function getSkin() {
637 return $this->getContext()->getSkin();
641 * Return the full title, including $par
646 public function getFullTitle() {
647 return $this->getContext()->getTitle();
651 * Wrapper around wfMessage that sets the current context. Currently this
656 public function msg( /* $args */ ) {
657 return call_user_func_array( 'wfMessage', func_get_args() )->title( $this->getFullTitle() );
662 * Shortcut to construct a special page which is unlisted by default
663 * @ingroup SpecialPage
665 class UnlistedSpecialPage
extends SpecialPage
667 function __construct( $name, $restriction = '', $function = false, $file = 'default' ) {
668 parent
::__construct( $name, $restriction, false, $function, $file );
673 * Shortcut to construct an includable special page
674 * @ingroup SpecialPage
676 class IncludableSpecialPage
extends SpecialPage
678 function __construct( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
679 parent
::__construct( $name, $restriction, $listed, $function, $file, true );
684 * Shortcut to construct a special page alias.
685 * @ingroup SpecialPage
687 abstract class SpecialRedirectToSpecial
extends UnlistedSpecialPage
{
688 var $redirName, $redirSubpage;
690 function __construct( $name, $redirName, $redirSubpage = false, $allowedRedirectParams = array(), $addedRedirectParams = array() ) {
691 parent
::__construct( $name );
692 $this->redirName
= $redirName;
693 $this->redirSubpage
= $redirSubpage;
694 $this->mAllowedRedirectParams
= $allowedRedirectParams;
695 $this->mAddedRedirectParams
= $addedRedirectParams;
698 function getRedirect( $subpage ) {
699 if ( $this->redirSubpage
=== false ) {
700 return SpecialPage
::getTitleFor( $this->redirName
, $subpage );
702 return SpecialPage
::getTitleFor( $this->redirName
, $this->redirSubpage
);
708 * ListAdmins --> ListUsers/admin
710 class SpecialListAdmins
extends SpecialRedirectToSpecial
{
711 function __construct(){
712 parent
::__construct( 'ListAdmins', 'ListUsers', 'sysop' );
717 * ListBots --> ListUsers/admin
719 class SpecialListBots
extends SpecialRedirectToSpecial
{
720 function __construct(){
721 parent
::__construct( 'ListAdmins', 'ListUsers', 'bot' );
726 * CreateAccount --> UserLogin/signup
727 * FIXME: this (and the rest of the login frontend) needs to die a horrible painful death
729 class SpecialCreateAccount
extends SpecialRedirectToSpecial
{
730 function __construct(){
731 parent
::__construct( 'CreateAccount', 'Userlogin', 'signup', array( 'uselang' ) );
735 * SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
736 * are used to get user independant links pointing to the user page, talk
737 * page and list of contributions.
738 * This can let us cache a single copy of any generated content for all
743 * Shortcut to construct a special page pointing to current user user's page.
744 * @ingroup SpecialPage
746 class SpecialMypage
extends UnlistedSpecialPage
{
747 function __construct() {
748 parent
::__construct( 'Mypage' );
749 $this->mAllowedRedirectParams
= array( 'action' , 'preload' , 'editintro',
750 'section', 'oldid', 'diff', 'dir' );
753 function getRedirect( $subpage ) {
755 if ( strval( $subpage ) !== '' ) {
756 return Title
::makeTitle( NS_USER
, $wgUser->getName() . '/' . $subpage );
758 return Title
::makeTitle( NS_USER
, $wgUser->getName() );
764 * Shortcut to construct a special page pointing to current user talk page.
765 * @ingroup SpecialPage
767 class SpecialMytalk
extends UnlistedSpecialPage
{
768 function __construct() {
769 parent
::__construct( 'Mytalk' );
770 $this->mAllowedRedirectParams
= array( 'action' , 'preload' , 'editintro',
771 'section', 'oldid', 'diff', 'dir' );
774 function getRedirect( $subpage ) {
776 if ( strval( $subpage ) !== '' ) {
777 return Title
::makeTitle( NS_USER_TALK
, $wgUser->getName() . '/' . $subpage );
779 return Title
::makeTitle( NS_USER_TALK
, $wgUser->getName() );
785 * Shortcut to construct a special page pointing to current user contributions.
786 * @ingroup SpecialPage
788 class SpecialMycontributions
extends UnlistedSpecialPage
{
789 function __construct() {
790 parent
::__construct( 'Mycontributions' );
791 $this->mAllowedRedirectParams
= array( 'limit', 'namespace', 'tagfilter',
792 'offset', 'dir', 'year', 'month', 'feed' );
795 function getRedirect( $subpage ) {
797 return SpecialPageFactory
::getTitleFor( 'Contributions', $wgUser->getName() );
802 * Redirect to Special:Listfiles?user=$wgUser
804 class SpecialMyuploads
extends UnlistedSpecialPage
{
805 function __construct() {
806 parent
::__construct( 'Myuploads' );
807 $this->mAllowedRedirectParams
= array( 'limit' );
810 function getRedirect( $subpage ) {
812 return SpecialPageFactory
::getTitleFor( 'Listfiles', $wgUser->getName() );
817 * Redirect from Special:PermanentLink/### to index.php?oldid=###
819 class SpecialPermanentLink
extends UnlistedSpecialPage
{
820 function __construct() {
821 parent
::__construct( 'PermanentLink' );
822 $this->mAllowedRedirectParams
= array();
825 function getRedirect( $subpage ) {
826 $subpage = intval( $subpage );
827 $this->mAddedRedirectParams
['oldid'] = $subpage;