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 * @subpackage SpecialPage
29 * Parent special page class, also static functions for handling the special
39 * The canonical name of this special page
40 * Also used for the default <h1> heading, @see getDescription()
44 * The local name of this special page
48 * Minimum user level required to access this page, or "" for anyone.
49 * Also used to categorise the pages in Special:Specialpages
53 * Listed in Special:Specialpages?
57 * Function name called by the default execute()
61 * File which needs to be included before the function above can be called
65 * Whether or not this special page is being included from an article
69 * Whether the special page can be included in an article
73 * Query parameters that can be passed through redirects
75 var $mAllowedRedirectParams = array();
77 static public $mList = array(
78 'DoubleRedirects' => array( 'SpecialPage', 'DoubleRedirects' ),
79 'BrokenRedirects' => array( 'SpecialPage', 'BrokenRedirects' ),
80 'Disambiguations' => array( 'SpecialPage', 'Disambiguations' ),
82 'Userlogin' => array( 'SpecialPage', 'Userlogin' ),
83 'Userlogout' => array( 'UnlistedSpecialPage', 'Userlogout' ),
84 'Preferences' => array( 'SpecialPage', 'Preferences' ),
85 'Watchlist' => array( 'SpecialPage', 'Watchlist' ),
87 'Recentchanges' => array( 'IncludableSpecialPage', 'Recentchanges' ),
88 'Upload' => array( 'SpecialPage', 'Upload' ),
89 'Imagelist' => array( 'SpecialPage', 'Imagelist' ),
90 'Newimages' => array( 'IncludableSpecialPage', 'Newimages' ),
91 'Listusers' => array( 'SpecialPage', 'Listusers' ),
92 'Statistics' => array( 'SpecialPage', 'Statistics' ),
93 'Randompage' => array( 'SpecialPage', 'Randompage' ),
94 'Lonelypages' => array( 'SpecialPage', 'Lonelypages' ),
95 'Uncategorizedpages' => array( 'SpecialPage', 'Uncategorizedpages' ),
96 'Uncategorizedcategories' => array( 'SpecialPage', 'Uncategorizedcategories' ),
97 'Uncategorizedimages' => array( 'SpecialPage', 'Uncategorizedimages' ),
98 'Unusedcategories' => array( 'SpecialPage', 'Unusedcategories' ),
99 'Unusedimages' => array( 'SpecialPage', 'Unusedimages' ),
100 'Wantedpages' => array( 'IncludableSpecialPage', 'Wantedpages' ),
101 'Wantedcategories' => array( 'SpecialPage', 'Wantedcategories' ),
102 'Mostlinked' => array( 'SpecialPage', 'Mostlinked' ),
103 'Mostlinkedcategories' => array( 'SpecialPage', 'Mostlinkedcategories' ),
104 'Mostcategories' => array( 'SpecialPage', 'Mostcategories' ),
105 'Mostimages' => array( 'SpecialPage', 'Mostimages' ),
106 'Mostrevisions' => array( 'SpecialPage', 'Mostrevisions' ),
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 'Allpages' => array( 'IncludableSpecialPage', 'Allpages' ),
113 'Prefixindex' => array( 'IncludableSpecialPage', 'Prefixindex' ) ,
114 'Ipblocklist' => array( 'SpecialPage', 'Ipblocklist' ),
115 'Specialpages' => array( 'UnlistedSpecialPage', 'Specialpages' ),
116 'Contributions' => array( 'UnlistedSpecialPage', 'Contributions' ),
117 'Emailuser' => array( 'UnlistedSpecialPage', 'Emailuser' ),
118 'Whatlinkshere' => array( 'UnlistedSpecialPage', 'Whatlinkshere' ),
119 'Recentchangeslinked' => array( 'UnlistedSpecialPage', 'Recentchangeslinked' ),
120 'Movepage' => array( 'UnlistedSpecialPage', 'Movepage' ),
121 'Blockme' => array( 'UnlistedSpecialPage', 'Blockme' ),
122 'Booksources' => array( 'SpecialPage', 'Booksources' ),
123 'Categories' => array( 'SpecialPage', 'Categories' ),
124 'Export' => array( 'SpecialPage', 'Export' ),
125 'Version' => array( 'SpecialPage', 'Version' ),
126 'Allmessages' => array( 'SpecialPage', 'Allmessages' ),
127 'Log' => array( 'SpecialPage', 'Log' ),
128 'Blockip' => array( 'SpecialPage', 'Blockip', 'block' ),
129 'Undelete' => array( 'SpecialPage', 'Undelete', 'deletedhistory' ),
130 'Import' => array( 'SpecialPage', "Import", 'import' ),
131 'Lockdb' => array( 'SpecialPage', 'Lockdb', 'siteadmin' ),
132 'Unlockdb' => array( 'SpecialPage', 'Unlockdb', 'siteadmin' ),
133 'Userrights' => array( 'SpecialPage', 'Userrights', 'userrights' ),
134 'MIMEsearch' => array( 'SpecialPage', 'MIMEsearch' ),
135 'Unwatchedpages' => array( 'SpecialPage', 'Unwatchedpages', 'unwatchedpages' ),
136 'Listredirects' => array( 'SpecialPage', 'Listredirects' ),
137 'Revisiondelete' => array( 'SpecialPage', 'Revisiondelete', 'deleterevision' ),
138 'Unusedtemplates' => array( 'SpecialPage', 'Unusedtemplates' ),
139 'Randomredirect' => array( 'SpecialPage', 'Randomredirect' ),
141 'Mypage' => array( 'SpecialMypage' ),
142 'Mytalk' => array( 'SpecialMytalk' ),
143 'Mycontributions' => array( 'SpecialMycontributions' ),
144 'Listadmins' => array( 'SpecialRedirectToSpecial', 'Listadmins', 'Listusers', 'sysop' ),
147 static public $mAliases;
148 static public $mListInitialised = false;
153 * Initialise the special page list
154 * This must be called before accessing SpecialPage::$mList
156 static function initList() {
157 global $wgSpecialPages;
158 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
160 if ( self
::$mListInitialised ) {
163 wfProfileIn( __METHOD__
);
165 # Better to set this now, to avoid infinite recursion in carelessly written hooks
166 self
::$mListInitialised = true;
168 if( !$wgDisableCounters ) {
169 self
::$mList['Popularpages'] = array( 'SpecialPage', 'Popularpages' );
172 if( !$wgDisableInternalSearch ) {
173 self
::$mList['Search'] = array( 'SpecialPage', 'Search' );
176 if( $wgEmailAuthentication ) {
177 self
::$mList['Confirmemail'] = array( 'UnlistedSpecialPage', 'Confirmemail' );
180 # Add extension special pages
181 self
::$mList = array_merge( self
::$mList, $wgSpecialPages );
184 # This hook can be used to remove undesired built-in special pages
185 wfRunHooks( 'SpecialPage_initList', array( &self
::$mList ) );
186 wfProfileOut( __METHOD__
);
189 static function initAliasList() {
190 if ( !is_null( self
::$mAliases ) ) {
195 $aliases = $wgContLang->getSpecialPageAliases();
196 $missingPages = self
::$mList;
197 self
::$mAliases = array();
198 foreach ( $aliases as $realName => $aliasList ) {
199 foreach ( $aliasList as $alias ) {
200 self
::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
202 unset( $missingPages[$realName] );
204 foreach ( $missingPages as $name => $stuff ) {
205 self
::$mAliases[$wgContLang->caseFold( $name )] = $name;
210 * Given a special page alias, return the special page name.
211 * Returns false if there is no such alias.
213 static function resolveAlias( $alias ) {
216 if ( !self
::$mListInitialised ) self
::initList();
217 if ( is_null( self
::$mAliases ) ) self
::initAliasList();
218 $caseFoldedAlias = $wgContLang->caseFold( $alias );
219 if ( isset( self
::$mAliases[$caseFoldedAlias] ) ) {
220 return self
::$mAliases[$caseFoldedAlias];
227 * Given a special page name with a possible subpage, return an array
228 * where the first element is the special page name and the second is the
231 static function resolveAliasWithSubpage( $alias ) {
232 $bits = explode( '/', $alias, 2 );
233 $name = self
::resolveAlias( $bits[0] );
234 if( !isset( $bits[1] ) ) { // bug 2087
239 return array( $name, $par );
243 * Add a page to the list of valid special pages. This used to be the preferred
244 * method for adding special pages in extensions. It's now suggested that you add
245 * an associative record to $wgSpecialPages. This avoids autoloading SpecialPage.
247 * @param mixed $page Must either be an array specifying a class name and
248 * constructor parameters, or an object. The object,
249 * when constructed, must have an execute() method which
250 * sends HTML to $wgOut.
253 static function addPage( &$page ) {
254 if ( !self
::$mListInitialised ) {
257 self
::$mList[$page->mName
] = $page;
261 * Remove a special page from the list
262 * Formerly used to disable expensive or dangerous special pages. The
263 * preferred method is now to add a SpecialPage_initList hook.
267 static function removePage( $name ) {
268 if ( !self
::$mListInitialised ) {
271 unset( self
::$mList[$name] );
275 * Find the object with a given name and return it (or NULL)
277 * @param string $name
279 static function getPage( $name ) {
280 if ( !self
::$mListInitialised ) {
283 if ( array_key_exists( $name, self
::$mList ) ) {
284 $rec = self
::$mList[$name];
285 if ( is_string( $rec ) ) {
287 self
::$mList[$name] = new $className;
288 } elseif ( is_array( $rec ) ) {
289 $className = array_shift( $rec );
290 self
::$mList[$name] = wfCreateObject( $className, $rec );
292 return self
::$mList[$name];
299 * Get a special page with a given localised name, or NULL if there
300 * is no such special page.
302 static function getPageByAlias( $alias ) {
303 $realName = self
::resolveAlias( $alias );
305 return self
::getPage( $realName );
312 * Return categorised listable special pages for all users
315 static function getRegularPages() {
316 if ( !self
::$mListInitialised ) {
321 foreach ( self
::$mList as $name => $rec ) {
322 $page = self
::getPage( $name );
323 if ( $page->isListed() && $page->getRestriction() == '' ) {
324 $pages[$name] = $page;
331 * Return categorised listable special pages which are available
332 * for the current user, but not for everyone
335 static function getRestrictedPages() {
337 if ( !self
::$mListInitialised ) {
342 foreach ( self
::$mList as $name => $rec ) {
343 $page = self
::getPage( $name );
344 if ( $page->isListed() ) {
345 $restriction = $page->getRestriction();
346 if ( $restriction != '' && $wgUser->isAllowed( $restriction ) ) {
347 $pages[$name] = $page;
355 * Execute a special page path.
356 * The path may contain parameters, e.g. Special:Name/Params
357 * Extracts the special page name and call the execute method, passing the parameters
359 * Returns a title object if the page is redirected, false if there was no such special
360 * page, and true if it was successful.
362 * @param $title a title object
363 * @param $including output is being captured for use in {{special:whatever}}
365 static function executePath( &$title, $including = false ) {
366 global $wgOut, $wgTitle, $wgRequest;
367 wfProfileIn( __METHOD__
);
369 # FIXME: redirects broken due to this call
370 $bits = explode( '/', $title->getDBkey(), 2 );
372 if( !isset( $bits[1] ) ) { // bug 2087
377 $page = SpecialPage
::getPageByAlias( $name );
382 $wgOut->setArticleRelated( false );
383 $wgOut->setRobotpolicy( 'noindex,nofollow' );
384 $wgOut->setStatusCode( 404 );
385 $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
387 wfProfileOut( __METHOD__
);
393 $redirect = $page->getRedirect( $par );
395 $query = $page->getRedirectQuery();
396 $url = $redirect->getFullUrl( $query );
397 $wgOut->redirect( $url );
398 wfProfileOut( __METHOD__
);
403 # Redirect to canonical alias for GET commands
404 # Not for POST, we'd lose the post data, so it's best to just distribute
405 # the request. Such POST requests are possible for old extensions that
406 # generate self-links without being aware that their default name has
408 if ( !$including && $name != $page->getLocalName() && !$wgRequest->wasPosted() ) {
410 unset( $query['title'] );
411 $query = wfArrayToCGI( $query );
412 $title = $page->getTitle();
413 $url = $title->getFullUrl( $query );
414 $wgOut->redirect( $url );
415 wfProfileOut( __METHOD__
);
419 if ( $including && !$page->includable() ) {
420 wfProfileOut( __METHOD__
);
422 } elseif ( !$including ) {
423 $wgTitle = $page->getTitle();
425 $page->including( $including );
427 // Execute special page
428 $profName = 'Special:' . $page->getName();
429 wfProfileIn( $profName );
430 $page->execute( $par );
431 wfProfileOut( $profName );
432 wfProfileOut( __METHOD__
);
437 * Just like executePath() except it returns the HTML instead of outputting it
438 * Returns false if there was no such special page, or a title object if it was
442 static function capturePath( &$title ) {
443 global $wgOut, $wgTitle;
445 $oldTitle = $wgTitle;
447 $wgOut = new OutputPage
;
449 $ret = SpecialPage
::executePath( $title, true );
450 if ( $ret === true ) {
451 $ret = $wgOut->getHTML();
453 $wgTitle = $oldTitle;
459 * Get the local name for a specified canonical name
461 static function getLocalNameFor( $name, $subpage = false ) {
463 $aliases = $wgContLang->getSpecialPageAliases();
464 if ( isset( $aliases[$name][0] ) ) {
465 $name = $aliases[$name][0];
467 if ( $subpage !== false && !is_null( $subpage ) ) {
468 $name = "$name/$subpage";
474 * Get a localised Title object for a specified special page name
476 static function getTitleFor( $name, $subpage = false ) {
477 $name = self
::getLocalNameFor( $name, $subpage );
479 return Title
::makeTitle( NS_SPECIAL
, $name );
481 throw new MWException( "Invalid special page name \"$name\"" );
486 * Get a localised Title object for a page name with a possibly unvalidated subpage
488 static function getSafeTitleFor( $name, $subpage = false ) {
489 $name = self
::getLocalNameFor( $name, $subpage );
491 return Title
::makeTitleSafe( NS_SPECIAL
, $name );
498 * Get a title for a given alias
499 * @return Title or null if there is no such alias
501 static function getTitleForAlias( $alias ) {
502 $name = self
::resolveAlias( $alias );
504 return self
::getTitleFor( $name );
511 * Default constructor for special pages
512 * Derivative classes should call this from their constructor
513 * Note that if the user does not have the required level, an error message will
514 * be displayed by the default execute() method, without the global function ever
517 * If you override execute(), you can recover the default behaviour with userCanExecute()
518 * and displayRestrictionError()
520 * @param string $name Name of the special page, as seen in links and URLs
521 * @param string $restriction User right required, e.g. "block" or "delete"
522 * @param boolean $listed Whether the page is listed in Special:Specialpages
523 * @param string $function Function called by execute(). By default it is constructed from $name
524 * @param string $file File which is included by execute(). It is also constructed from $name by default
526 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
527 $this->mName
= $name;
528 $this->mRestriction
= $restriction;
529 $this->mListed
= $listed;
530 $this->mIncludable
= $includable;
531 if ( $function == false ) {
532 $this->mFunction
= 'wfSpecial'.$name;
534 $this->mFunction
= $function;
536 if ( $file === 'default' ) {
537 $this->mFile
= "Special{$name}.php";
539 $this->mFile
= $file;
548 function getName() { return $this->mName
; }
549 function getRestriction() { return $this->mRestriction
; }
550 function getFile() { return $this->mFile
; }
551 function isListed() { return $this->mListed
; }
555 * Accessor and mutator
557 function name( $x = NULL ) { return wfSetVar( $this->mName
, $x ); }
558 function restrictions( $x = NULL) { return wfSetVar( $this->mRestrictions
, $x ); }
559 function listed( $x = NULL) { return wfSetVar( $this->mListed
, $x ); }
560 function func( $x = NULL) { return wfSetVar( $this->mFunction
, $x ); }
561 function file( $x = NULL) { return wfSetVar( $this->mFile
, $x ); }
562 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable
, $x ); }
563 function including( $x = NULL ) { return wfSetVar( $this->mIncluding
, $x ); }
567 * Get the localised name of the special page
569 function getLocalName() {
570 if ( !isset( $this->mLocalName
) ) {
571 $this->mLocalName
= self
::getLocalNameFor( $this->mName
);
573 return $this->mLocalName
;
577 * Checks if the given user (identified by an object) can execute this
578 * special page (as defined by $mRestriction)
580 function userCanExecute( &$user ) {
581 return $user->isAllowed( $this->mRestriction
);
585 * Output an error message telling the user what access level they have to have
587 function displayRestrictionError() {
589 $wgOut->permissionRequired( $this->mRestriction
);
593 * Sets headers - this should be called from the execute() method of all derived classes!
595 function setHeaders() {
597 $wgOut->setArticleRelated( false );
598 $wgOut->setRobotPolicy( "noindex,nofollow" );
599 $wgOut->setPageTitle( $this->getDescription() );
603 * Default execute method
604 * Checks user permissions, calls the function given in mFunction
606 * This may be overridden by subclasses.
608 function execute( $par ) {
613 if ( $this->userCanExecute( $wgUser ) ) {
614 $func = $this->mFunction
;
615 // only load file if the function does not exist
616 if(!function_exists($func) and $this->mFile
) {
617 require_once( $this->mFile
);
619 # FIXME: these hooks are broken for extensions and anything else that subclasses SpecialPage.
620 if ( wfRunHooks( 'SpecialPageExecuteBeforeHeader', array( &$this, &$par, &$func ) ) )
621 $this->outputHeader();
622 if ( ! wfRunHooks( 'SpecialPageExecuteBeforePage', array( &$this, &$par, &$func ) ) )
624 $func( $par, $this );
625 if ( ! wfRunHooks( 'SpecialPageExecuteAfterPage', array( &$this, &$par, &$func ) ) )
628 $this->displayRestrictionError();
632 function outputHeader() {
633 global $wgOut, $wgContLang;
635 $msg = $wgContLang->lc( $this->name() ) . '-summary';
636 $out = wfMsg( $msg );
637 if ( ! wfEmptyMsg( $msg, $out ) and $out !== '' and ! $this->including() )
638 $wgOut->addWikiText( $out );
642 # Returns the name that goes in the <h1> in the special page itself, and also the name that
643 # will be listed in Special:Specialpages
645 # Derived classes can override this, but usually it is easier to keep the default behaviour.
646 # Messages can be added at run-time, see MessageCache.php
647 function getDescription() {
648 return wfMsg( strtolower( $this->mName
) );
652 * Get a self-referential title object
654 function getTitle( $subpage = false) {
655 return self
::getTitleFor( $this->mName
, $subpage );
659 * Set whether this page is listed in Special:Specialpages, at run-time
661 function setListed( $listed ) {
662 return wfSetVar( $this->mListed
, $listed );
666 * If the special page is a redirect, then get the Title object it redirects to.
669 function getRedirect( $subpage = false ) {
674 * Return part of the request string for a special redirect page
675 * This allows passing, e.g. action=history to Special:Mypage, etc.
679 function getRedirectQuery() {
682 foreach( $this->mAllowedRedirectParams
as $arg ) {
683 if( $val = $wgRequest->getVal( $arg, false ) )
684 $params[] = $arg . '=' . $val;
687 return count( $params ) ?
implode( '&', $params ) : false;
692 * Shortcut to construct a special page which is unlisted by default
695 class UnlistedSpecialPage
extends SpecialPage
697 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
698 SpecialPage
::SpecialPage( $name, $restriction, false, $function, $file );
703 * Shortcut to construct an includable special page
706 class IncludableSpecialPage
extends SpecialPage
708 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
709 SpecialPage
::SpecialPage( $name, $restriction, $listed, $function, $file, true );
713 class SpecialRedirectToSpecial
extends UnlistedSpecialPage
{
714 var $redirName, $redirSubpage;
716 function __construct( $name, $redirName, $redirSubpage = false, $redirectParams = array() ) {
717 parent
::__construct( $name );
718 $this->redirName
= $redirName;
719 $this->redirSubpage
= $redirSubpage;
720 $this->mAllowedRedirectParams
= $redirectParams;
723 function getRedirect( $subpage ) {
724 if ( $this->redirSubpage
=== false ) {
725 return SpecialPage
::getTitleFor( $this->redirName
, $subpage );
727 return SpecialPage
::getTitleFor( $this->redirName
, $this->redirSubpage
);
732 class SpecialMypage
extends UnlistedSpecialPage
{
733 function __construct() {
734 parent
::__construct( 'Mypage' );
735 $this->mAllowedRedirectParams
= array( 'action' );
738 function getRedirect( $subpage ) {
740 if ( strval( $subpage ) !== '' ) {
741 return Title
::makeTitle( NS_USER
, $wgUser->getName() . '/' . $subpage );
743 return Title
::makeTitle( NS_USER
, $wgUser->getName() );
748 class SpecialMytalk
extends UnlistedSpecialPage
{
749 function __construct() {
750 parent
::__construct( 'Mytalk' );
751 $this->mAllowedRedirectParams
= array( 'action' );
754 function getRedirect( $subpage ) {
756 if ( strval( $subpage ) !== '' ) {
757 return Title
::makeTitle( NS_USER_TALK
, $wgUser->getName() . '/' . $subpage );
759 return Title
::makeTitle( NS_USER_TALK
, $wgUser->getName() );
764 class SpecialMycontributions
extends UnlistedSpecialPage
{
765 function __construct() {
766 parent
::__construct( 'Mycontributions' );
769 function getRedirect( $subpage ) {
771 return SpecialPage
::getTitleFor( 'Contributions', $wgUser->getName() );