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.
20 * @addtogroup SpecialPage
28 * Parent special page class, also static functions for handling the special
30 * @addtogroup SpecialPage
38 * The canonical name of this special page
39 * Also used for the default <h1> heading, @see getDescription()
43 * The local name of this special page
47 * Minimum user level required to access this page, or "" for anyone.
48 * Also used to categorise the pages in Special:Specialpages
52 * Listed in Special:Specialpages?
56 * Function name called by the default execute()
60 * File which needs to be included before the function above can be called
64 * Whether or not this special page is being included from an article
68 * Whether the special page can be included in an article
72 * Query parameters that can be passed through redirects
74 var $mAllowedRedirectParams = array();
76 static public $mList = array(
77 'DoubleRedirects' => array( 'SpecialPage', 'DoubleRedirects' ),
78 'BrokenRedirects' => array( 'SpecialPage', 'BrokenRedirects' ),
79 'Disambiguations' => array( 'SpecialPage', 'Disambiguations' ),
81 'Userlogin' => array( 'SpecialPage', 'Userlogin' ),
82 'Userlogout' => array( 'UnlistedSpecialPage', 'Userlogout' ),
83 'Preferences' => array( 'SpecialPage', 'Preferences' ),
84 'Watchlist' => array( 'SpecialPage', 'Watchlist' ),
86 'Recentchanges' => array( 'IncludableSpecialPage', 'Recentchanges' ),
87 'Upload' => array( 'SpecialPage', 'Upload' ),
88 'Imagelist' => array( 'SpecialPage', 'Imagelist' ),
89 'Newimages' => array( 'IncludableSpecialPage', 'Newimages' ),
90 'Listusers' => array( 'SpecialPage', 'Listusers' ),
91 'Statistics' => array( 'SpecialPage', 'Statistics' ),
92 'Randompage' => array( 'SpecialPage', 'Randompage' ),
93 'Lonelypages' => array( 'SpecialPage', 'Lonelypages' ),
94 'Uncategorizedpages' => array( 'SpecialPage', 'Uncategorizedpages' ),
95 'Uncategorizedcategories' => array( 'SpecialPage', 'Uncategorizedcategories' ),
96 'Uncategorizedimages' => array( 'SpecialPage', 'Uncategorizedimages' ),
97 'Unusedcategories' => array( 'SpecialPage', 'Unusedcategories' ),
98 'Unusedimages' => array( 'SpecialPage', 'Unusedimages' ),
99 'Wantedpages' => array( 'IncludableSpecialPage', 'Wantedpages' ),
100 'Wantedcategories' => array( 'SpecialPage', 'Wantedcategories' ),
101 'Mostlinked' => array( 'SpecialPage', 'Mostlinked' ),
102 'Mostlinkedcategories' => array( 'SpecialPage', 'Mostlinkedcategories' ),
103 'Mostcategories' => array( 'SpecialPage', 'Mostcategories' ),
104 'Mostimages' => array( 'SpecialPage', 'Mostimages' ),
105 'Mostrevisions' => array( 'SpecialPage', 'Mostrevisions' ),
106 'Fewestrevisions' => array( 'SpecialPage', 'Fewestrevisions' ),
107 'Shortpages' => array( 'SpecialPage', 'Shortpages' ),
108 'Longpages' => array( 'SpecialPage', 'Longpages' ),
109 'Newpages' => array( 'IncludableSpecialPage', 'Newpages' ),
110 'Ancientpages' => array( 'SpecialPage', 'Ancientpages' ),
111 'Deadendpages' => array( 'SpecialPage', 'Deadendpages' ),
112 'Protectedpages' => array( 'SpecialPage', 'Protectedpages' ),
113 'Allpages' => array( 'IncludableSpecialPage', 'Allpages' ),
114 'Prefixindex' => array( 'IncludableSpecialPage', 'Prefixindex' ) ,
115 'Ipblocklist' => array( 'SpecialPage', 'Ipblocklist' ),
116 'Specialpages' => array( 'UnlistedSpecialPage', 'Specialpages' ),
117 'Contributions' => array( 'SpecialPage', 'Contributions' ),
118 'Emailuser' => array( 'UnlistedSpecialPage', 'Emailuser' ),
119 'Whatlinkshere' => array( 'UnlistedSpecialPage', 'Whatlinkshere' ),
120 'Recentchangeslinked' => array( 'UnlistedSpecialPage', 'Recentchangeslinked' ),
121 'Movepage' => array( 'UnlistedSpecialPage', 'Movepage' ),
122 'Blockme' => array( 'UnlistedSpecialPage', 'Blockme' ),
123 'Resetpass' => array( 'UnlistedSpecialPage', 'Resetpass' ),
124 'Booksources' => 'SpecialBookSources',
125 'Categories' => array( 'SpecialPage', 'Categories' ),
126 'Export' => array( 'SpecialPage', 'Export' ),
127 'Version' => array( 'SpecialPage', 'Version' ),
128 'Allmessages' => array( 'SpecialPage', 'Allmessages' ),
129 'Log' => array( 'SpecialPage', 'Log' ),
130 'Blockip' => array( 'SpecialPage', 'Blockip', 'block' ),
131 'Undelete' => array( 'SpecialPage', 'Undelete', 'deletedhistory' ),
132 'Import' => array( 'SpecialPage', "Import", 'import' ),
133 'Lockdb' => array( 'SpecialPage', 'Lockdb', 'siteadmin' ),
134 'Unlockdb' => array( 'SpecialPage', 'Unlockdb', 'siteadmin' ),
135 'Userrights' => array( 'SpecialPage', 'Userrights', 'userrights' ),
136 'MIMEsearch' => array( 'SpecialPage', 'MIMEsearch' ),
137 'Unwatchedpages' => array( 'SpecialPage', 'Unwatchedpages', 'unwatchedpages' ),
138 'Listredirects' => array( 'SpecialPage', 'Listredirects' ),
139 'Revisiondelete' => array( 'SpecialPage', 'Revisiondelete', 'deleterevision' ),
140 'Unusedtemplates' => array( 'SpecialPage', 'Unusedtemplates' ),
141 'Randomredirect' => array( 'SpecialPage', 'Randomredirect' ),
142 'Withoutinterwiki' => array( 'SpecialPage', 'Withoutinterwiki' ),
144 'Mypage' => array( 'SpecialMypage' ),
145 'Mytalk' => array( 'SpecialMytalk' ),
146 'Mycontributions' => array( 'SpecialMycontributions' ),
147 'Listadmins' => array( 'SpecialRedirectToSpecial', 'Listadmins', 'Listusers', 'sysop' ),
150 static public $mAliases;
151 static public $mListInitialised = false;
156 * Initialise the special page list
157 * This must be called before accessing SpecialPage::$mList
159 static function initList() {
160 global $wgSpecialPages;
161 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
163 if ( self
::$mListInitialised ) {
166 wfProfileIn( __METHOD__
);
168 # Better to set this now, to avoid infinite recursion in carelessly written hooks
169 self
::$mListInitialised = true;
171 if( !$wgDisableCounters ) {
172 self
::$mList['Popularpages'] = array( 'SpecialPage', 'Popularpages' );
175 if( !$wgDisableInternalSearch ) {
176 self
::$mList['Search'] = array( 'SpecialPage', 'Search' );
179 if( $wgEmailAuthentication ) {
180 self
::$mList['Confirmemail'] = array( 'UnlistedSpecialPage', 'Confirmemail' );
183 # Add extension special pages
184 self
::$mList = array_merge( self
::$mList, $wgSpecialPages );
187 # This hook can be used to remove undesired built-in special pages
188 wfRunHooks( 'SpecialPage_initList', array( &self
::$mList ) );
189 wfProfileOut( __METHOD__
);
192 static function initAliasList() {
193 if ( !is_null( self
::$mAliases ) ) {
198 $aliases = $wgContLang->getSpecialPageAliases();
199 $missingPages = self
::$mList;
200 self
::$mAliases = array();
201 foreach ( $aliases as $realName => $aliasList ) {
202 foreach ( $aliasList as $alias ) {
203 self
::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
205 unset( $missingPages[$realName] );
207 foreach ( $missingPages as $name => $stuff ) {
208 self
::$mAliases[$wgContLang->caseFold( $name )] = $name;
213 * Given a special page alias, return the special page name.
214 * Returns false if there is no such alias.
216 static function resolveAlias( $alias ) {
219 if ( !self
::$mListInitialised ) self
::initList();
220 if ( is_null( self
::$mAliases ) ) self
::initAliasList();
221 $caseFoldedAlias = $wgContLang->caseFold( $alias );
222 if ( isset( self
::$mAliases[$caseFoldedAlias] ) ) {
223 return self
::$mAliases[$caseFoldedAlias];
230 * Given a special page name with a possible subpage, return an array
231 * where the first element is the special page name and the second is the
234 static function resolveAliasWithSubpage( $alias ) {
235 $bits = explode( '/', $alias, 2 );
236 $name = self
::resolveAlias( $bits[0] );
237 if( !isset( $bits[1] ) ) { // bug 2087
242 return array( $name, $par );
246 * Add a page to the list of valid special pages. This used to be the preferred
247 * method for adding special pages in extensions. It's now suggested that you add
248 * an associative record to $wgSpecialPages. This avoids autoloading SpecialPage.
250 * @param mixed $page Must either be an array specifying a class name and
251 * constructor parameters, or an object. The object,
252 * when constructed, must have an execute() method which
253 * sends HTML to $wgOut.
256 static function addPage( &$page ) {
257 if ( !self
::$mListInitialised ) {
260 self
::$mList[$page->mName
] = $page;
264 * Remove a special page from the list
265 * Formerly used to disable expensive or dangerous special pages. The
266 * preferred method is now to add a SpecialPage_initList hook.
270 static function removePage( $name ) {
271 if ( !self
::$mListInitialised ) {
274 unset( self
::$mList[$name] );
278 * Find the object with a given name and return it (or NULL)
280 * @param string $name
282 static function getPage( $name ) {
283 if ( !self
::$mListInitialised ) {
286 if ( array_key_exists( $name, self
::$mList ) ) {
287 $rec = self
::$mList[$name];
288 if ( is_string( $rec ) ) {
290 self
::$mList[$name] = new $className;
291 } elseif ( is_array( $rec ) ) {
292 $className = array_shift( $rec );
293 self
::$mList[$name] = wfCreateObject( $className, $rec );
295 return self
::$mList[$name];
302 * Get a special page with a given localised name, or NULL if there
303 * is no such special page.
305 static function getPageByAlias( $alias ) {
306 $realName = self
::resolveAlias( $alias );
308 return self
::getPage( $realName );
315 * Return categorised listable special pages for all users
318 static function getRegularPages() {
319 if ( !self
::$mListInitialised ) {
324 foreach ( self
::$mList as $name => $rec ) {
325 $page = self
::getPage( $name );
326 if ( $page->isListed() && $page->getRestriction() == '' ) {
327 $pages[$name] = $page;
334 * Return categorised listable special pages which are available
335 * for the current user, but not for everyone
338 static function getRestrictedPages() {
340 if ( !self
::$mListInitialised ) {
345 foreach ( self
::$mList as $name => $rec ) {
346 $page = self
::getPage( $name );
347 if ( $page->isListed() ) {
348 $restriction = $page->getRestriction();
349 if ( $restriction != '' && $wgUser->isAllowed( $restriction ) ) {
350 $pages[$name] = $page;
358 * Execute a special page path.
359 * The path may contain parameters, e.g. Special:Name/Params
360 * Extracts the special page name and call the execute method, passing the parameters
362 * Returns a title object if the page is redirected, false if there was no such special
363 * page, and true if it was successful.
365 * @param $title a title object
366 * @param $including output is being captured for use in {{special:whatever}}
368 static function executePath( &$title, $including = false ) {
369 global $wgOut, $wgTitle, $wgRequest;
370 wfProfileIn( __METHOD__
);
372 # FIXME: redirects broken due to this call
373 $bits = explode( '/', $title->getDBkey(), 2 );
375 if( !isset( $bits[1] ) ) { // bug 2087
380 $page = SpecialPage
::getPageByAlias( $name );
385 $wgOut->setArticleRelated( false );
386 $wgOut->setRobotpolicy( 'noindex,nofollow' );
387 $wgOut->setStatusCode( 404 );
388 $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
390 wfProfileOut( __METHOD__
);
396 $redirect = $page->getRedirect( $par );
398 $query = $page->getRedirectQuery();
399 $url = $redirect->getFullUrl( $query );
400 $wgOut->redirect( $url );
401 wfProfileOut( __METHOD__
);
406 # Redirect to canonical alias for GET commands
407 # Not for POST, we'd lose the post data, so it's best to just distribute
408 # the request. Such POST requests are possible for old extensions that
409 # generate self-links without being aware that their default name has
411 if ( !$including && $name != $page->getLocalName() && !$wgRequest->wasPosted() ) {
413 unset( $query['title'] );
414 $query = wfArrayToCGI( $query );
415 $title = $page->getTitle( $par );
416 $url = $title->getFullUrl( $query );
417 $wgOut->redirect( $url );
418 wfProfileOut( __METHOD__
);
422 if ( $including && !$page->includable() ) {
423 wfProfileOut( __METHOD__
);
425 } elseif ( !$including ) {
426 $wgTitle = $page->getTitle();
428 $page->including( $including );
430 // Execute special page
431 $profName = 'Special:' . $page->getName();
432 wfProfileIn( $profName );
433 $page->execute( $par );
434 wfProfileOut( $profName );
435 wfProfileOut( __METHOD__
);
440 * Just like executePath() except it returns the HTML instead of outputting it
441 * Returns false if there was no such special page, or a title object if it was
445 static function capturePath( &$title ) {
446 global $wgOut, $wgTitle;
448 $oldTitle = $wgTitle;
450 $wgOut = new OutputPage
;
452 $ret = SpecialPage
::executePath( $title, true );
453 if ( $ret === true ) {
454 $ret = $wgOut->getHTML();
456 $wgTitle = $oldTitle;
462 * Get the local name for a specified canonical name
464 static function getLocalNameFor( $name, $subpage = false ) {
466 $aliases = $wgContLang->getSpecialPageAliases();
467 if ( isset( $aliases[$name][0] ) ) {
468 $name = $aliases[$name][0];
470 if ( $subpage !== false && !is_null( $subpage ) ) {
471 $name = "$name/$subpage";
477 * Get a localised Title object for a specified special page name
479 static function getTitleFor( $name, $subpage = false ) {
480 $name = self
::getLocalNameFor( $name, $subpage );
482 return Title
::makeTitle( NS_SPECIAL
, $name );
484 throw new MWException( "Invalid special page name \"$name\"" );
489 * Get a localised Title object for a page name with a possibly unvalidated subpage
491 static function getSafeTitleFor( $name, $subpage = false ) {
492 $name = self
::getLocalNameFor( $name, $subpage );
494 return Title
::makeTitleSafe( NS_SPECIAL
, $name );
501 * Get a title for a given alias
502 * @return Title or null if there is no such alias
504 static function getTitleForAlias( $alias ) {
505 $name = self
::resolveAlias( $alias );
507 return self
::getTitleFor( $name );
514 * Default constructor for special pages
515 * Derivative classes should call this from their constructor
516 * Note that if the user does not have the required level, an error message will
517 * be displayed by the default execute() method, without the global function ever
520 * If you override execute(), you can recover the default behaviour with userCanExecute()
521 * and displayRestrictionError()
523 * @param string $name Name of the special page, as seen in links and URLs
524 * @param string $restriction User right required, e.g. "block" or "delete"
525 * @param boolean $listed Whether the page is listed in Special:Specialpages
526 * @param string $function Function called by execute(). By default it is constructed from $name
527 * @param string $file File which is included by execute(). It is also constructed from $name by default
529 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
530 $this->mName
= $name;
531 $this->mRestriction
= $restriction;
532 $this->mListed
= $listed;
533 $this->mIncludable
= $includable;
534 if ( $function == false ) {
535 $this->mFunction
= 'wfSpecial'.$name;
537 $this->mFunction
= $function;
539 if ( $file === 'default' ) {
540 $this->mFile
= dirname(__FILE__
) . "/Special{$name}.php";
542 $this->mFile
= $file;
551 function getName() { return $this->mName
; }
552 function getRestriction() { return $this->mRestriction
; }
553 function getFile() { return $this->mFile
; }
554 function isListed() { return $this->mListed
; }
558 * Accessor and mutator
560 function name( $x = NULL ) { return wfSetVar( $this->mName
, $x ); }
561 function restrictions( $x = NULL) { return wfSetVar( $this->mRestrictions
, $x ); }
562 function listed( $x = NULL) { return wfSetVar( $this->mListed
, $x ); }
563 function func( $x = NULL) { return wfSetVar( $this->mFunction
, $x ); }
564 function file( $x = NULL) { return wfSetVar( $this->mFile
, $x ); }
565 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable
, $x ); }
566 function including( $x = NULL ) { return wfSetVar( $this->mIncluding
, $x ); }
570 * Get the localised name of the special page
572 function getLocalName() {
573 if ( !isset( $this->mLocalName
) ) {
574 $this->mLocalName
= self
::getLocalNameFor( $this->mName
);
576 return $this->mLocalName
;
580 * Checks if the given user (identified by an object) can execute this
581 * special page (as defined by $mRestriction)
583 function userCanExecute( &$user ) {
584 return $user->isAllowed( $this->mRestriction
);
588 * Output an error message telling the user what access level they have to have
590 function displayRestrictionError() {
592 $wgOut->permissionRequired( $this->mRestriction
);
596 * Sets headers - this should be called from the execute() method of all derived classes!
598 function setHeaders() {
600 $wgOut->setArticleRelated( false );
601 $wgOut->setRobotPolicy( "noindex,nofollow" );
602 $wgOut->setPageTitle( $this->getDescription() );
606 * Default execute method
607 * Checks user permissions, calls the function given in mFunction
609 * This may be overridden by subclasses.
611 function execute( $par ) {
616 if ( $this->userCanExecute( $wgUser ) ) {
617 $func = $this->mFunction
;
618 // only load file if the function does not exist
619 if(!function_exists($func) and $this->mFile
) {
620 require_once( $this->mFile
);
622 # FIXME: these hooks are broken for extensions and anything else that subclasses SpecialPage.
623 if ( wfRunHooks( 'SpecialPageExecuteBeforeHeader', array( &$this, &$par, &$func ) ) )
624 $this->outputHeader();
625 if ( ! wfRunHooks( 'SpecialPageExecuteBeforePage', array( &$this, &$par, &$func ) ) )
627 $func( $par, $this );
628 if ( ! wfRunHooks( 'SpecialPageExecuteAfterPage', array( &$this, &$par, &$func ) ) )
631 $this->displayRestrictionError();
635 function outputHeader() {
636 global $wgOut, $wgContLang;
638 $msg = $wgContLang->lc( $this->name() ) . '-summary';
639 $out = wfMsg( $msg );
640 if ( ! wfEmptyMsg( $msg, $out ) and $out !== '' and ! $this->including() )
641 $wgOut->addWikiText( $out );
645 # Returns the name that goes in the <h1> in the special page itself, and also the name that
646 # will be listed in Special:Specialpages
648 # Derived classes can override this, but usually it is easier to keep the default behaviour.
649 # Messages can be added at run-time, see MessageCache.php
650 function getDescription() {
651 return wfMsg( strtolower( $this->mName
) );
655 * Get a self-referential title object
657 function getTitle( $subpage = false) {
658 return self
::getTitleFor( $this->mName
, $subpage );
662 * Set whether this page is listed in Special:Specialpages, at run-time
664 function setListed( $listed ) {
665 return wfSetVar( $this->mListed
, $listed );
669 * If the special page is a redirect, then get the Title object it redirects to.
672 function getRedirect( $subpage ) {
677 * Return part of the request string for a special redirect page
678 * This allows passing, e.g. action=history to Special:Mypage, etc.
682 function getRedirectQuery() {
685 foreach( $this->mAllowedRedirectParams
as $arg ) {
686 if( $val = $wgRequest->getVal( $arg, false ) )
687 $params[] = $arg . '=' . $val;
690 return count( $params ) ?
implode( '&', $params ) : false;
695 * Shortcut to construct a special page which is unlisted by default
696 * @addtogroup SpecialPage
698 class UnlistedSpecialPage
extends SpecialPage
700 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
701 SpecialPage
::SpecialPage( $name, $restriction, false, $function, $file );
706 * Shortcut to construct an includable special page
707 * @addtogroup SpecialPage
709 class IncludableSpecialPage
extends SpecialPage
711 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
712 SpecialPage
::SpecialPage( $name, $restriction, $listed, $function, $file, true );
717 * Shortcut to construct a special page alias.
718 * @addtogroup SpecialPage
720 class SpecialRedirectToSpecial
extends UnlistedSpecialPage
{
721 var $redirName, $redirSubpage;
723 function __construct( $name, $redirName, $redirSubpage = false, $redirectParams = array() ) {
724 parent
::__construct( $name );
725 $this->redirName
= $redirName;
726 $this->redirSubpage
= $redirSubpage;
727 $this->mAllowedRedirectParams
= $redirectParams;
730 function getRedirect( $subpage ) {
731 if ( $this->redirSubpage
=== false ) {
732 return SpecialPage
::getTitleFor( $this->redirName
, $subpage );
734 return SpecialPage
::getTitleFor( $this->redirName
, $this->redirSubpage
);
739 /** SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
740 * are used to get user independant links pointing to the user page, talk
741 * page and list of contributions.
742 * This can let us cache a single copy of any generated content for all
747 * Shortcut to construct a special page pointing to current user user's page.
748 * @addtogroup SpecialPage
750 class SpecialMypage
extends UnlistedSpecialPage
{
751 function __construct() {
752 parent
::__construct( 'Mypage' );
753 $this->mAllowedRedirectParams
= array( 'action' );
756 function getRedirect( $subpage ) {
758 if ( strval( $subpage ) !== '' ) {
759 return Title
::makeTitle( NS_USER
, $wgUser->getName() . '/' . $subpage );
761 return Title
::makeTitle( NS_USER
, $wgUser->getName() );
767 * Shortcut to construct a special page pointing to current user talk page.
768 * @addtogroup SpecialPage
770 class SpecialMytalk
extends UnlistedSpecialPage
{
771 function __construct() {
772 parent
::__construct( 'Mytalk' );
773 $this->mAllowedRedirectParams
= array( 'action' );
776 function getRedirect( $subpage ) {
778 if ( strval( $subpage ) !== '' ) {
779 return Title
::makeTitle( NS_USER_TALK
, $wgUser->getName() . '/' . $subpage );
781 return Title
::makeTitle( NS_USER_TALK
, $wgUser->getName() );
787 * Shortcut to construct a special page pointing to current user contributions.
788 * @addtogroup SpecialPage
790 class SpecialMycontributions
extends UnlistedSpecialPage
{
791 function __construct() {
792 parent
::__construct( 'Mycontributions' );
795 function getRedirect( $subpage ) {
797 return SpecialPage
::getTitleFor( 'Contributions', $wgUser->getName() );