097c5957ce5d9f7a6d806fd794a582d220297403
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" );
388 function getName() { return $this->mName
; }
389 function getRestriction() { return $this->mRestriction
; }
390 function getFile() { return $this->mFile
; }
391 function isListed() { return $this->mListed
; }
395 * Accessor and mutator
397 function name( $x = null ) { return wfSetVar( $this->mName
, $x ); }
398 function restrictions( $x = null) {
399 # Use the one below this
400 wfDeprecated( __METHOD__
);
401 return wfSetVar( $this->mRestriction
, $x );
403 function restriction( $x = null) { return wfSetVar( $this->mRestriction
, $x ); }
404 function listed( $x = null) { return wfSetVar( $this->mListed
, $x ); }
405 function func( $x = null) { return wfSetVar( $this->mFunction
, $x ); }
406 function file( $x = null) { return wfSetVar( $this->mFile
, $x ); }
407 function includable( $x = null ) { return wfSetVar( $this->mIncludable
, $x ); }
408 function including( $x = null ) { return wfSetVar( $this->mIncluding
, $x ); }
412 * Get the localised name of the special page
414 function getLocalName() {
415 if ( !isset( $this->mLocalName
) ) {
416 $this->mLocalName
= SpecialPageFactory
::getLocalNameFor( $this->mName
);
418 return $this->mLocalName
;
422 * Is this page expensive (for some definition of expensive)?
423 * Expensive pages are disabled or cached in miser mode. Originally used
424 * (and still overridden) by QueryPage and subclasses, moved here so that
425 * Special:SpecialPages can safely call it for all special pages.
429 public function isExpensive() {
434 * Can be overridden by subclasses with more complicated permissions
437 * @return Boolean: should the page be displayed with the restricted-access
440 public function isRestricted() {
441 global $wgGroupPermissions;
442 // DWIM: If all anons can do something, then it is not restricted
443 return $this->mRestriction
!= '' && empty($wgGroupPermissions['*'][$this->mRestriction
]);
447 * Checks if the given user (identified by an object) can execute this
448 * special page (as defined by $mRestriction). Can be overridden by sub-
449 * classes with more complicated permissions schemes.
451 * @param $user User: the user to check
452 * @return Boolean: does the user have permission to view the page?
454 public function userCanExecute( $user ) {
455 return $user->isAllowed( $this->mRestriction
);
459 * Output an error message telling the user what access level they have to have
461 function displayRestrictionError() {
462 throw new PermissionsError( $this->mRestriction
);
466 * Sets headers - this should be called from the execute() method of all derived classes!
468 function setHeaders() {
469 $out = $this->getOutput();
470 $out->setArticleRelated( false );
471 $out->setRobotPolicy( "noindex,nofollow" );
472 $out->setPageTitle( $this->getDescription() );
476 * Default execute method
477 * Checks user permissions, calls the function given in mFunction
479 * This may be overridden by subclasses.
481 function execute( $par ) {
484 if ( $this->userCanExecute( $this->getUser() ) ) {
485 $func = $this->mFunction
;
486 // only load file if the function does not exist
487 if(!is_callable($func) and $this->mFile
) {
488 require_once( $this->mFile
);
490 $this->outputHeader();
491 call_user_func( $func, $par, $this );
493 $this->displayRestrictionError();
498 * Outputs a summary message on top of special pages
499 * Per default the message key is the canonical name of the special page
500 * May be overriden, i.e. by extensions to stick with the naming conventions
501 * for message keys: 'extensionname-xxx'
503 * @param $summaryMessageKey String: message key of the summary
505 function outputHeader( $summaryMessageKey = '' ) {
508 if( $summaryMessageKey == '' ) {
509 $msg = $wgContLang->lc( $this->name() ) . '-summary';
511 $msg = $summaryMessageKey;
513 if ( !wfMessage( $msg )->isBlank() and ! $this->including() ) {
514 $this->getOutput()->wrapWikiMsg( "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
520 * Returns the name that goes in the \<h1\> in the special page itself, and
521 * also the name that will be listed in Special:Specialpages
523 * Derived classes can override this, but usually it is easier to keep the
524 * default behaviour. Messages can be added at run-time, see
529 function getDescription() {
530 return wfMsg( strtolower( $this->mName
) );
534 * Get a self-referential title object
536 * @return Title object
538 function getTitle( $subpage = false ) {
539 return self
::getTitleFor( $this->mName
, $subpage );
543 * Set whether this page is listed in Special:Specialpages, at run-time
547 function setListed( $listed ) {
548 return wfSetVar( $this->mListed
, $listed );
552 * If the special page is a redirect, then get the Title object it redirects to.
555 * @return Title|false
557 function getRedirect( $subpage ) {
562 * Return part of the request string for a special redirect page
563 * This allows passing, e.g. action=history to Special:Mypage, etc.
567 function getRedirectQuery() {
570 foreach( $this->mAllowedRedirectParams
as $arg ) {
571 if( $this->getContext()->request
->getVal( $arg, null ) !== null ){
572 $params[$arg] = $this->getContext()->request
->getVal( $arg );
576 foreach( $this->mAddedRedirectParams
as $arg => $val ) {
577 $params[$arg] = $val;
580 return count( $params )
586 * Sets the context this SpecialPage is executed in
588 * @param $context RequestContext
591 public function setContext( $context ) {
592 $this->mContext
= $context;
596 * Gets the context this SpecialPage is executed in
598 * @return RequestContext
601 public function getContext() {
602 if ( $this->mContext
instanceof RequestContext
) {
603 return $this->mContext
;
605 wfDebug( __METHOD__
. " called and \$mContext is null. Return RequestContext::getMain(); for sanity\n" );
606 return RequestContext
::getMain();
611 * Get the WebRequest being used for this instance
616 public function getRequest() {
617 return $this->getContext()->getRequest();
621 * Get the OutputPage being used for this instance
626 public function getOutput() {
627 return $this->getContext()->getOutput();
631 * Shortcut to get the skin being used for this instance
636 public function getUser() {
637 return $this->getContext()->getUser();
641 * Shortcut to get the skin being used for this instance
646 public function getSkin() {
647 return $this->getContext()->getSkin();
651 * Return the full title, including $par
656 public function getFullTitle() {
657 return $this->getContext()->getTitle();
661 * Wrapper around wfMessage that sets the current context. Currently this
666 public function msg( /* $args */ ) {
667 return call_user_func_array( 'wfMessage', func_get_args() )->title( $this->getFullTitle() );
672 * Shortcut to construct a special page which is unlisted by default
673 * @ingroup SpecialPage
675 class UnlistedSpecialPage
extends SpecialPage
677 function __construct( $name, $restriction = '', $function = false, $file = 'default' ) {
678 parent
::__construct( $name, $restriction, false, $function, $file );
683 * Shortcut to construct an includable special page
684 * @ingroup SpecialPage
686 class IncludableSpecialPage
extends SpecialPage
688 function __construct( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
689 parent
::__construct( $name, $restriction, $listed, $function, $file, true );
694 * Shortcut to construct a special page alias.
695 * @ingroup SpecialPage
697 abstract class SpecialRedirectToSpecial
extends UnlistedSpecialPage
{
698 var $redirName, $redirSubpage;
700 function __construct( $name, $redirName, $redirSubpage = false, $allowedRedirectParams = array(), $addedRedirectParams = array() ) {
701 parent
::__construct( $name );
702 $this->redirName
= $redirName;
703 $this->redirSubpage
= $redirSubpage;
704 $this->mAllowedRedirectParams
= $allowedRedirectParams;
705 $this->mAddedRedirectParams
= $addedRedirectParams;
708 function getRedirect( $subpage ) {
709 if ( $this->redirSubpage
=== false ) {
710 return SpecialPageFactory
::getTitleFor( $this->redirName
, $subpage );
712 return SpecialPageFactory
::getTitleFor( $this->redirName
, $this->redirSubpage
);
718 * ListAdmins --> ListUsers/admin
720 class SpecialListAdmins
extends SpecialRedirectToSpecial
{
721 function __construct(){
722 parent
::__construct( 'ListAdmins', 'ListUsers', 'sysop' );
727 * ListBots --> ListUsers/admin
729 class SpecialListBots
extends SpecialRedirectToSpecial
{
730 function __construct(){
731 parent
::__construct( 'ListAdmins', 'ListUsers', 'bot' );
736 * CreateAccount --> UserLogin/signup
737 * FIXME: this (and the rest of the login frontend) needs to die a horrible painful death
739 class SpecialCreateAccount
extends SpecialRedirectToSpecial
{
740 function __construct(){
741 parent
::__construct( 'CreateAccount', 'Userlogin', 'signup', array( 'uselang' ) );
745 * SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
746 * are used to get user independant links pointing to the user page, talk
747 * page and list of contributions.
748 * This can let us cache a single copy of any generated content for all
753 * Shortcut to construct a special page pointing to current user user's page.
754 * @ingroup SpecialPage
756 class SpecialMypage
extends UnlistedSpecialPage
{
757 function __construct() {
758 parent
::__construct( 'Mypage' );
759 $this->mAllowedRedirectParams
= array( 'action' , 'preload' , 'editintro',
760 'section', 'oldid', 'diff', 'dir' );
763 function getRedirect( $subpage ) {
765 if ( strval( $subpage ) !== '' ) {
766 return Title
::makeTitle( NS_USER
, $wgUser->getName() . '/' . $subpage );
768 return Title
::makeTitle( NS_USER
, $wgUser->getName() );
774 * Shortcut to construct a special page pointing to current user talk page.
775 * @ingroup SpecialPage
777 class SpecialMytalk
extends UnlistedSpecialPage
{
778 function __construct() {
779 parent
::__construct( 'Mytalk' );
780 $this->mAllowedRedirectParams
= array( 'action' , 'preload' , 'editintro',
781 'section', 'oldid', 'diff', 'dir' );
784 function getRedirect( $subpage ) {
786 if ( strval( $subpage ) !== '' ) {
787 return Title
::makeTitle( NS_USER_TALK
, $wgUser->getName() . '/' . $subpage );
789 return Title
::makeTitle( NS_USER_TALK
, $wgUser->getName() );
795 * Shortcut to construct a special page pointing to current user contributions.
796 * @ingroup SpecialPage
798 class SpecialMycontributions
extends UnlistedSpecialPage
{
799 function __construct() {
800 parent
::__construct( 'Mycontributions' );
801 $this->mAllowedRedirectParams
= array( 'limit', 'namespace', 'tagfilter',
802 'offset', 'dir', 'year', 'month', 'feed' );
805 function getRedirect( $subpage ) {
807 return SpecialPageFactory
::getTitleFor( 'Contributions', $wgUser->getName() );
812 * Redirect to Special:Listfiles?user=$wgUser
814 class SpecialMyuploads
extends UnlistedSpecialPage
{
815 function __construct() {
816 parent
::__construct( 'Myuploads' );
817 $this->mAllowedRedirectParams
= array( 'limit' );
820 function getRedirect( $subpage ) {
822 return SpecialPageFactory
::getTitleFor( 'Listfiles', $wgUser->getName() );
827 * Redirect from Special:PermanentLink/### to index.php?oldid=###
829 class SpecialPermanentLink
extends UnlistedSpecialPage
{
830 function __construct() {
831 parent
::__construct( 'PermanentLink' );
832 $this->mAllowedRedirectParams
= array();
835 function getRedirect( $subpage ) {
836 $subpage = intval( $subpage );
837 $this->mAddedRedirectParams
['oldid'] = $subpage;