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
37 * The canonical name of this special page
38 * Also used for the default <h1> heading, @see getDescription()
42 * The local name of this special page
46 * Minimum user level required to access this page, or "" for anyone.
47 * Also used to categorise the pages in Special:Specialpages
51 * Listed in Special:Specialpages?
55 * Function name called by the default execute()
59 * File which needs to be included before the function above can be called
63 * Whether or not this special page is being included from an article
67 * Whether the special page can be included in an article
71 * Query parameters that can be passed through redirects
73 var $mAllowedRedirectParams = array();
75 static public $mList = array(
76 'DoubleRedirects' => array( 'SpecialPage', 'DoubleRedirects' ),
77 'BrokenRedirects' => array( 'SpecialPage', 'BrokenRedirects' ),
78 'Disambiguations' => array( 'SpecialPage', 'Disambiguations' ),
80 'Userlogin' => array( 'SpecialPage', 'Userlogin' ),
81 'Userlogout' => array( 'UnlistedSpecialPage', 'Userlogout' ),
82 'Preferences' => array( 'SpecialPage', 'Preferences' ),
83 'Watchlist' => array( 'SpecialPage', 'Watchlist' ),
85 'Recentchanges' => array( 'IncludableSpecialPage', 'Recentchanges' ),
86 'Upload' => array( 'SpecialPage', 'Upload' ),
87 'Imagelist' => array( 'SpecialPage', 'Imagelist' ),
88 'Newimages' => array( 'IncludableSpecialPage', 'Newimages' ),
89 'Listusers' => array( 'SpecialPage', 'Listusers' ),
90 'Statistics' => array( 'SpecialPage', 'Statistics' ),
91 'Randompage' => array( 'SpecialPage', 'Randompage' ),
92 'Lonelypages' => array( 'SpecialPage', 'Lonelypages' ),
93 'Uncategorizedpages' => array( 'SpecialPage', 'Uncategorizedpages' ),
94 'Uncategorizedcategories' => array( 'SpecialPage', 'Uncategorizedcategories' ),
95 'Uncategorizedimages' => array( 'SpecialPage', 'Uncategorizedimages' ),
96 'Unusedcategories' => array( 'SpecialPage', 'Unusedcategories' ),
97 'Unusedimages' => array( 'SpecialPage', 'Unusedimages' ),
98 'Wantedpages' => array( 'IncludableSpecialPage', 'Wantedpages' ),
99 'Wantedcategories' => array( 'SpecialPage', 'Wantedcategories' ),
100 'Mostlinked' => array( 'SpecialPage', 'Mostlinked' ),
101 'Mostlinkedcategories' => array( 'SpecialPage', 'Mostlinkedcategories' ),
102 'Mostcategories' => array( 'SpecialPage', 'Mostcategories' ),
103 'Mostimages' => array( 'SpecialPage', 'Mostimages' ),
104 'Mostrevisions' => array( 'SpecialPage', 'Mostrevisions' ),
105 'Shortpages' => array( 'SpecialPage', 'Shortpages' ),
106 'Longpages' => array( 'SpecialPage', 'Longpages' ),
107 'Newpages' => array( 'IncludableSpecialPage', 'Newpages' ),
108 'Ancientpages' => array( 'SpecialPage', 'Ancientpages' ),
109 'Deadendpages' => array( 'SpecialPage', 'Deadendpages' ),
110 'Allpages' => array( 'IncludableSpecialPage', 'Allpages' ),
111 'Prefixindex' => array( 'IncludableSpecialPage', 'Prefixindex' ) ,
112 'Ipblocklist' => array( 'SpecialPage', 'Ipblocklist' ),
113 'Specialpages' => array( 'UnlistedSpecialPage', 'Specialpages' ),
114 'Contributions' => array( 'UnlistedSpecialPage', 'Contributions' ),
115 'Emailuser' => array( 'UnlistedSpecialPage', 'Emailuser' ),
116 'Whatlinkshere' => array( 'UnlistedSpecialPage', 'Whatlinkshere' ),
117 'Recentchangeslinked' => array( 'UnlistedSpecialPage', 'Recentchangeslinked' ),
118 'Movepage' => array( 'UnlistedSpecialPage', 'Movepage' ),
119 'Blockme' => array( 'UnlistedSpecialPage', 'Blockme' ),
120 'Resetpass' => array( 'UnlistedSpecialPage', 'Resetpass' ),
121 'Booksources' => 'SpecialBookSources',
122 'Categories' => array( 'SpecialPage', 'Categories' ),
123 'Export' => array( 'SpecialPage', 'Export' ),
124 'Version' => array( 'SpecialPage', 'Version' ),
125 'Allmessages' => array( 'SpecialPage', 'Allmessages' ),
126 'Log' => array( 'SpecialPage', 'Log' ),
127 'Blockip' => array( 'SpecialPage', 'Blockip', 'block' ),
128 'Undelete' => array( 'SpecialPage', 'Undelete', 'deletedhistory' ),
129 'Import' => array( 'SpecialPage', "Import", 'import' ),
130 'Lockdb' => array( 'SpecialPage', 'Lockdb', 'siteadmin' ),
131 'Unlockdb' => array( 'SpecialPage', 'Unlockdb', 'siteadmin' ),
132 'Userrights' => array( 'SpecialPage', 'Userrights', 'userrights' ),
133 'MIMEsearch' => array( 'SpecialPage', 'MIMEsearch' ),
134 'Unwatchedpages' => array( 'SpecialPage', 'Unwatchedpages', 'unwatchedpages' ),
135 'Listredirects' => array( 'SpecialPage', 'Listredirects' ),
136 'Revisiondelete' => array( 'SpecialPage', 'Revisiondelete', 'deleterevision' ),
137 'Unusedtemplates' => array( 'SpecialPage', 'Unusedtemplates' ),
138 'Randomredirect' => array( 'SpecialPage', 'Randomredirect' ),
140 'Mypage' => array( 'SpecialMypage' ),
141 'Mytalk' => array( 'SpecialMytalk' ),
142 'Mycontributions' => array( 'SpecialMycontributions' ),
143 'Listadmins' => array( 'SpecialRedirectToSpecial', 'Listadmins', 'Listusers', 'sysop' ),
146 static public $mAliases;
147 static public $mListInitialised = false;
152 * Initialise the special page list
153 * This must be called before accessing SpecialPage::$mList
155 static function initList() {
156 global $wgSpecialPages;
157 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
159 if ( self
::$mListInitialised ) {
162 wfProfileIn( __METHOD__
);
164 # Better to set this now, to avoid infinite recursion in carelessly written hooks
165 self
::$mListInitialised = true;
167 if( !$wgDisableCounters ) {
168 self
::$mList['Popularpages'] = array( 'SpecialPage', 'Popularpages' );
171 if( !$wgDisableInternalSearch ) {
172 self
::$mList['Search'] = array( 'SpecialPage', 'Search' );
175 if( $wgEmailAuthentication ) {
176 self
::$mList['Confirmemail'] = array( 'UnlistedSpecialPage', 'Confirmemail' );
179 # Add extension special pages
180 self
::$mList = array_merge( self
::$mList, $wgSpecialPages );
183 # This hook can be used to remove undesired built-in special pages
184 wfRunHooks( 'SpecialPage_initList', array( &self
::$mList ) );
185 wfProfileOut( __METHOD__
);
188 static function initAliasList() {
189 if ( !is_null( self
::$mAliases ) ) {
194 $aliases = $wgContLang->getSpecialPageAliases();
195 $missingPages = self
::$mList;
196 self
::$mAliases = array();
197 foreach ( $aliases as $realName => $aliasList ) {
198 foreach ( $aliasList as $alias ) {
199 self
::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
201 unset( $missingPages[$realName] );
203 foreach ( $missingPages as $name => $stuff ) {
204 self
::$mAliases[$wgContLang->caseFold( $name )] = $name;
209 * Given a special page alias, return the special page name.
210 * Returns false if there is no such alias.
212 static function resolveAlias( $alias ) {
215 if ( !self
::$mListInitialised ) self
::initList();
216 if ( is_null( self
::$mAliases ) ) self
::initAliasList();
217 $caseFoldedAlias = $wgContLang->caseFold( $alias );
218 if ( isset( self
::$mAliases[$caseFoldedAlias] ) ) {
219 return self
::$mAliases[$caseFoldedAlias];
226 * Given a special page name with a possible subpage, return an array
227 * where the first element is the special page name and the second is the
230 static function resolveAliasWithSubpage( $alias ) {
231 $bits = explode( '/', $alias, 2 );
232 $name = self
::resolveAlias( $bits[0] );
233 if( !isset( $bits[1] ) ) { // bug 2087
238 return array( $name, $par );
242 * Add a page to the list of valid special pages. This used to be the preferred
243 * method for adding special pages in extensions. It's now suggested that you add
244 * an associative record to $wgSpecialPages. This avoids autoloading SpecialPage.
246 * @param mixed $page Must either be an array specifying a class name and
247 * constructor parameters, or an object. The object,
248 * when constructed, must have an execute() method which
249 * sends HTML to $wgOut.
252 static function addPage( &$page ) {
253 if ( !self
::$mListInitialised ) {
256 self
::$mList[$page->mName
] = $page;
260 * Remove a special page from the list
261 * Formerly used to disable expensive or dangerous special pages. The
262 * preferred method is now to add a SpecialPage_initList hook.
266 static function removePage( $name ) {
267 if ( !self
::$mListInitialised ) {
270 unset( self
::$mList[$name] );
274 * Find the object with a given name and return it (or NULL)
276 * @param string $name
278 static function getPage( $name ) {
279 if ( !self
::$mListInitialised ) {
282 if ( array_key_exists( $name, self
::$mList ) ) {
283 $rec = self
::$mList[$name];
284 if ( is_string( $rec ) ) {
286 self
::$mList[$name] = new $className;
287 } elseif ( is_array( $rec ) ) {
288 $className = array_shift( $rec );
289 self
::$mList[$name] = wfCreateObject( $className, $rec );
291 return self
::$mList[$name];
298 * Get a special page with a given localised name, or NULL if there
299 * is no such special page.
301 static function getPageByAlias( $alias ) {
302 $realName = self
::resolveAlias( $alias );
304 return self
::getPage( $realName );
311 * Return categorised listable special pages for all users
314 static function getRegularPages() {
315 if ( !self
::$mListInitialised ) {
320 foreach ( self
::$mList as $name => $rec ) {
321 $page = self
::getPage( $name );
322 if ( $page->isListed() && $page->getRestriction() == '' ) {
323 $pages[$name] = $page;
330 * Return categorised listable special pages which are available
331 * for the current user, but not for everyone
334 static function getRestrictedPages() {
336 if ( !self
::$mListInitialised ) {
341 foreach ( self
::$mList as $name => $rec ) {
342 $page = self
::getPage( $name );
343 if ( $page->isListed() ) {
344 $restriction = $page->getRestriction();
345 if ( $restriction != '' && $wgUser->isAllowed( $restriction ) ) {
346 $pages[$name] = $page;
354 * Execute a special page path.
355 * The path may contain parameters, e.g. Special:Name/Params
356 * Extracts the special page name and call the execute method, passing the parameters
358 * Returns a title object if the page is redirected, false if there was no such special
359 * page, and true if it was successful.
361 * @param $title a title object
362 * @param $including output is being captured for use in {{special:whatever}}
364 static function executePath( &$title, $including = false ) {
365 global $wgOut, $wgTitle, $wgRequest;
366 wfProfileIn( __METHOD__
);
368 # FIXME: redirects broken due to this call
369 $bits = explode( '/', $title->getDBkey(), 2 );
371 if( !isset( $bits[1] ) ) { // bug 2087
376 $page = SpecialPage
::getPageByAlias( $name );
381 $wgOut->setArticleRelated( false );
382 $wgOut->setRobotpolicy( 'noindex,nofollow' );
383 $wgOut->setStatusCode( 404 );
384 $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
386 wfProfileOut( __METHOD__
);
392 $redirect = $page->getRedirect( $par );
394 $query = $page->getRedirectQuery();
395 $url = $redirect->getFullUrl( $query );
396 $wgOut->redirect( $url );
397 wfProfileOut( __METHOD__
);
402 # Redirect to canonical alias for GET commands
403 # Not for POST, we'd lose the post data, so it's best to just distribute
404 # the request. Such POST requests are possible for old extensions that
405 # generate self-links without being aware that their default name has
407 if ( !$including && $name != $page->getLocalName() && !$wgRequest->wasPosted() ) {
409 unset( $query['title'] );
410 $query = wfArrayToCGI( $query );
411 $title = $page->getTitle( $par );
412 $url = $title->getFullUrl( $query );
413 $wgOut->redirect( $url );
414 wfProfileOut( __METHOD__
);
418 if ( $including && !$page->includable() ) {
419 wfProfileOut( __METHOD__
);
421 } elseif ( !$including ) {
422 $wgTitle = $page->getTitle();
424 $page->including( $including );
426 // Execute special page
427 $profName = 'Special:' . $page->getName();
428 wfProfileIn( $profName );
429 $page->execute( $par );
430 wfProfileOut( $profName );
431 wfProfileOut( __METHOD__
);
436 * Just like executePath() except it returns the HTML instead of outputting it
437 * Returns false if there was no such special page, or a title object if it was
441 static function capturePath( &$title ) {
442 global $wgOut, $wgTitle;
444 $oldTitle = $wgTitle;
446 $wgOut = new OutputPage
;
448 $ret = SpecialPage
::executePath( $title, true );
449 if ( $ret === true ) {
450 $ret = $wgOut->getHTML();
452 $wgTitle = $oldTitle;
458 * Get the local name for a specified canonical name
460 static function getLocalNameFor( $name, $subpage = false ) {
462 $aliases = $wgContLang->getSpecialPageAliases();
463 if ( isset( $aliases[$name][0] ) ) {
464 $name = $aliases[$name][0];
466 if ( $subpage !== false && !is_null( $subpage ) ) {
467 $name = "$name/$subpage";
473 * Get a localised Title object for a specified special page name
475 static function getTitleFor( $name, $subpage = false ) {
476 $name = self
::getLocalNameFor( $name, $subpage );
478 return Title
::makeTitle( NS_SPECIAL
, $name );
480 throw new MWException( "Invalid special page name \"$name\"" );
485 * Get a localised Title object for a page name with a possibly unvalidated subpage
487 static function getSafeTitleFor( $name, $subpage = false ) {
488 $name = self
::getLocalNameFor( $name, $subpage );
490 return Title
::makeTitleSafe( NS_SPECIAL
, $name );
497 * Get a title for a given alias
498 * @return Title or null if there is no such alias
500 static function getTitleForAlias( $alias ) {
501 $name = self
::resolveAlias( $alias );
503 return self
::getTitleFor( $name );
510 * Default constructor for special pages
511 * Derivative classes should call this from their constructor
512 * Note that if the user does not have the required level, an error message will
513 * be displayed by the default execute() method, without the global function ever
516 * If you override execute(), you can recover the default behaviour with userCanExecute()
517 * and displayRestrictionError()
519 * @param string $name Name of the special page, as seen in links and URLs
520 * @param string $restriction User right required, e.g. "block" or "delete"
521 * @param boolean $listed Whether the page is listed in Special:Specialpages
522 * @param string $function Function called by execute(). By default it is constructed from $name
523 * @param string $file File which is included by execute(). It is also constructed from $name by default
525 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
526 $this->mName
= $name;
527 $this->mRestriction
= $restriction;
528 $this->mListed
= $listed;
529 $this->mIncludable
= $includable;
530 if ( $function == false ) {
531 $this->mFunction
= 'wfSpecial'.$name;
533 $this->mFunction
= $function;
535 if ( $file === 'default' ) {
536 $this->mFile
= "Special{$name}.php";
538 $this->mFile
= $file;
547 function getName() { return $this->mName
; }
548 function getRestriction() { return $this->mRestriction
; }
549 function getFile() { return $this->mFile
; }
550 function isListed() { return $this->mListed
; }
554 * Accessor and mutator
556 function name( $x = NULL ) { return wfSetVar( $this->mName
, $x ); }
557 function restrictions( $x = NULL) { return wfSetVar( $this->mRestrictions
, $x ); }
558 function listed( $x = NULL) { return wfSetVar( $this->mListed
, $x ); }
559 function func( $x = NULL) { return wfSetVar( $this->mFunction
, $x ); }
560 function file( $x = NULL) { return wfSetVar( $this->mFile
, $x ); }
561 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable
, $x ); }
562 function including( $x = NULL ) { return wfSetVar( $this->mIncluding
, $x ); }
566 * Get the localised name of the special page
568 function getLocalName() {
569 if ( !isset( $this->mLocalName
) ) {
570 $this->mLocalName
= self
::getLocalNameFor( $this->mName
);
572 return $this->mLocalName
;
576 * Checks if the given user (identified by an object) can execute this
577 * special page (as defined by $mRestriction)
579 function userCanExecute( &$user ) {
580 return $user->isAllowed( $this->mRestriction
);
584 * Output an error message telling the user what access level they have to have
586 function displayRestrictionError() {
588 $wgOut->permissionRequired( $this->mRestriction
);
592 * Sets headers - this should be called from the execute() method of all derived classes!
594 function setHeaders() {
596 $wgOut->setArticleRelated( false );
597 $wgOut->setRobotPolicy( "noindex,nofollow" );
598 $wgOut->setPageTitle( $this->getDescription() );
602 * Default execute method
603 * Checks user permissions, calls the function given in mFunction
605 * This may be overridden by subclasses.
607 function execute( $par ) {
612 if ( $this->userCanExecute( $wgUser ) ) {
613 $func = $this->mFunction
;
614 // only load file if the function does not exist
615 if(!function_exists($func) and $this->mFile
) {
616 require_once( $this->mFile
);
618 # FIXME: these hooks are broken for extensions and anything else that subclasses SpecialPage.
619 if ( wfRunHooks( 'SpecialPageExecuteBeforeHeader', array( &$this, &$par, &$func ) ) )
620 $this->outputHeader();
621 if ( ! wfRunHooks( 'SpecialPageExecuteBeforePage', array( &$this, &$par, &$func ) ) )
623 $func( $par, $this );
624 if ( ! wfRunHooks( 'SpecialPageExecuteAfterPage', array( &$this, &$par, &$func ) ) )
627 $this->displayRestrictionError();
631 function outputHeader() {
632 global $wgOut, $wgContLang;
634 $msg = $wgContLang->lc( $this->name() ) . '-summary';
635 $out = wfMsg( $msg );
636 if ( ! wfEmptyMsg( $msg, $out ) and $out !== '' and ! $this->including() )
637 $wgOut->addWikiText( $out );
641 # Returns the name that goes in the <h1> in the special page itself, and also the name that
642 # will be listed in Special:Specialpages
644 # Derived classes can override this, but usually it is easier to keep the default behaviour.
645 # Messages can be added at run-time, see MessageCache.php
646 function getDescription() {
647 return wfMsg( strtolower( $this->mName
) );
651 * Get a self-referential title object
653 function getTitle( $subpage = false) {
654 return self
::getTitleFor( $this->mName
, $subpage );
658 * Set whether this page is listed in Special:Specialpages, at run-time
660 function setListed( $listed ) {
661 return wfSetVar( $this->mListed
, $listed );
665 * If the special page is a redirect, then get the Title object it redirects to.
668 function getRedirect( $subpage ) {
673 * Return part of the request string for a special redirect page
674 * This allows passing, e.g. action=history to Special:Mypage, etc.
678 function getRedirectQuery() {
681 foreach( $this->mAllowedRedirectParams
as $arg ) {
682 if( $val = $wgRequest->getVal( $arg, false ) )
683 $params[] = $arg . '=' . $val;
686 return count( $params ) ?
implode( '&', $params ) : false;
691 * Shortcut to construct a special page which is unlisted by default
693 class UnlistedSpecialPage
extends SpecialPage
695 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
696 SpecialPage
::SpecialPage( $name, $restriction, false, $function, $file );
701 * Shortcut to construct an includable special page
703 class IncludableSpecialPage
extends SpecialPage
705 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
706 SpecialPage
::SpecialPage( $name, $restriction, $listed, $function, $file, true );
710 class SpecialRedirectToSpecial
extends UnlistedSpecialPage
{
711 var $redirName, $redirSubpage;
713 function __construct( $name, $redirName, $redirSubpage = false, $redirectParams = array() ) {
714 parent
::__construct( $name );
715 $this->redirName
= $redirName;
716 $this->redirSubpage
= $redirSubpage;
717 $this->mAllowedRedirectParams
= $redirectParams;
720 function getRedirect( $subpage ) {
721 if ( $this->redirSubpage
=== false ) {
722 return SpecialPage
::getTitleFor( $this->redirName
, $subpage );
724 return SpecialPage
::getTitleFor( $this->redirName
, $this->redirSubpage
);
729 class SpecialMypage
extends UnlistedSpecialPage
{
730 function __construct() {
731 parent
::__construct( 'Mypage' );
732 $this->mAllowedRedirectParams
= array( 'action' );
735 function getRedirect( $subpage ) {
737 if ( strval( $subpage ) !== '' ) {
738 return Title
::makeTitle( NS_USER
, $wgUser->getName() . '/' . $subpage );
740 return Title
::makeTitle( NS_USER
, $wgUser->getName() );
745 class SpecialMytalk
extends UnlistedSpecialPage
{
746 function __construct() {
747 parent
::__construct( 'Mytalk' );
748 $this->mAllowedRedirectParams
= array( 'action' );
751 function getRedirect( $subpage ) {
753 if ( strval( $subpage ) !== '' ) {
754 return Title
::makeTitle( NS_USER_TALK
, $wgUser->getName() . '/' . $subpage );
756 return Title
::makeTitle( NS_USER_TALK
, $wgUser->getName() );
761 class SpecialMycontributions
extends UnlistedSpecialPage
{
762 function __construct() {
763 parent
::__construct( 'Mycontributions' );
766 function getRedirect( $subpage ) {
768 return SpecialPage
::getTitleFor( 'Contributions', $wgUser->getName() );