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 'Fewestrevisions' => array( 'SpecialPage', 'Fewestrevisions' ),
106 'Shortpages' => array( 'SpecialPage', 'Shortpages' ),
107 'Longpages' => array( 'SpecialPage', 'Longpages' ),
108 'Newpages' => array( 'IncludableSpecialPage', 'Newpages' ),
109 'Ancientpages' => array( 'SpecialPage', 'Ancientpages' ),
110 'Deadendpages' => array( 'SpecialPage', 'Deadendpages' ),
111 'Protectedpages' => array( 'SpecialPage', 'Protectedpages' ),
112 'Allpages' => array( 'IncludableSpecialPage', 'Allpages' ),
113 'Prefixindex' => array( 'IncludableSpecialPage', 'Prefixindex' ) ,
114 'Ipblocklist' => array( 'SpecialPage', 'Ipblocklist' ),
115 'Specialpages' => array( 'UnlistedSpecialPage', 'Specialpages' ),
116 'Contributions' => array( 'SpecialPage', '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 'Resetpass' => array( 'UnlistedSpecialPage', 'Resetpass' ),
123 'Booksources' => 'SpecialBookSources',
124 'Categories' => array( 'SpecialPage', 'Categories' ),
125 'Export' => array( 'SpecialPage', 'Export' ),
126 'Version' => array( 'SpecialPage', 'Version' ),
127 'Allmessages' => array( 'SpecialPage', 'Allmessages' ),
128 'Log' => array( 'SpecialPage', 'Log' ),
129 'Blockip' => array( 'SpecialPage', 'Blockip', 'block' ),
130 'Undelete' => array( 'SpecialPage', 'Undelete', 'deletedhistory' ),
131 'Import' => array( 'SpecialPage', "Import", 'import' ),
132 'Lockdb' => array( 'SpecialPage', 'Lockdb', 'siteadmin' ),
133 'Unlockdb' => array( 'SpecialPage', 'Unlockdb', 'siteadmin' ),
134 'Userrights' => array( 'SpecialPage', 'Userrights', 'userrights' ),
135 'MIMEsearch' => array( 'SpecialPage', 'MIMEsearch' ),
136 'Unwatchedpages' => array( 'SpecialPage', 'Unwatchedpages', 'unwatchedpages' ),
137 'Listredirects' => array( 'SpecialPage', 'Listredirects' ),
138 'Revisiondelete' => array( 'SpecialPage', 'Revisiondelete', 'deleterevision' ),
139 'Unusedtemplates' => array( 'SpecialPage', 'Unusedtemplates' ),
140 'Randomredirect' => array( 'SpecialPage', 'Randomredirect' ),
141 'Withoutinterwiki' => array( 'SpecialPage', 'Withoutinterwiki' ),
143 'Mypage' => array( 'SpecialMypage' ),
144 'Mytalk' => array( 'SpecialMytalk' ),
145 'Mycontributions' => array( 'SpecialMycontributions' ),
146 'Listadmins' => array( 'SpecialRedirectToSpecial', 'Listadmins', 'Listusers', 'sysop' ),
149 static public $mAliases;
150 static public $mListInitialised = false;
155 * Initialise the special page list
156 * This must be called before accessing SpecialPage::$mList
158 static function initList() {
159 global $wgSpecialPages;
160 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
162 if ( self
::$mListInitialised ) {
165 wfProfileIn( __METHOD__
);
167 # Better to set this now, to avoid infinite recursion in carelessly written hooks
168 self
::$mListInitialised = true;
170 if( !$wgDisableCounters ) {
171 self
::$mList['Popularpages'] = array( 'SpecialPage', 'Popularpages' );
174 if( !$wgDisableInternalSearch ) {
175 self
::$mList['Search'] = array( 'SpecialPage', 'Search' );
178 if( $wgEmailAuthentication ) {
179 self
::$mList['Confirmemail'] = array( 'UnlistedSpecialPage', 'Confirmemail' );
182 # Add extension special pages
183 self
::$mList = array_merge( self
::$mList, $wgSpecialPages );
186 # This hook can be used to remove undesired built-in special pages
187 wfRunHooks( 'SpecialPage_initList', array( &self
::$mList ) );
188 wfProfileOut( __METHOD__
);
191 static function initAliasList() {
192 if ( !is_null( self
::$mAliases ) ) {
197 $aliases = $wgContLang->getSpecialPageAliases();
198 $missingPages = self
::$mList;
199 self
::$mAliases = array();
200 foreach ( $aliases as $realName => $aliasList ) {
201 foreach ( $aliasList as $alias ) {
202 self
::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
204 unset( $missingPages[$realName] );
206 foreach ( $missingPages as $name => $stuff ) {
207 self
::$mAliases[$wgContLang->caseFold( $name )] = $name;
212 * Given a special page alias, return the special page name.
213 * Returns false if there is no such alias.
215 static function resolveAlias( $alias ) {
218 if ( !self
::$mListInitialised ) self
::initList();
219 if ( is_null( self
::$mAliases ) ) self
::initAliasList();
220 $caseFoldedAlias = $wgContLang->caseFold( $alias );
221 if ( isset( self
::$mAliases[$caseFoldedAlias] ) ) {
222 return self
::$mAliases[$caseFoldedAlias];
229 * Given a special page name with a possible subpage, return an array
230 * where the first element is the special page name and the second is the
233 static function resolveAliasWithSubpage( $alias ) {
234 $bits = explode( '/', $alias, 2 );
235 $name = self
::resolveAlias( $bits[0] );
236 if( !isset( $bits[1] ) ) { // bug 2087
241 return array( $name, $par );
245 * Add a page to the list of valid special pages. This used to be the preferred
246 * method for adding special pages in extensions. It's now suggested that you add
247 * an associative record to $wgSpecialPages. This avoids autoloading SpecialPage.
249 * @param mixed $page Must either be an array specifying a class name and
250 * constructor parameters, or an object. The object,
251 * when constructed, must have an execute() method which
252 * sends HTML to $wgOut.
255 static function addPage( &$page ) {
256 if ( !self
::$mListInitialised ) {
259 self
::$mList[$page->mName
] = $page;
263 * Remove a special page from the list
264 * Formerly used to disable expensive or dangerous special pages. The
265 * preferred method is now to add a SpecialPage_initList hook.
269 static function removePage( $name ) {
270 if ( !self
::$mListInitialised ) {
273 unset( self
::$mList[$name] );
277 * Find the object with a given name and return it (or NULL)
279 * @param string $name
281 static function getPage( $name ) {
282 if ( !self
::$mListInitialised ) {
285 if ( array_key_exists( $name, self
::$mList ) ) {
286 $rec = self
::$mList[$name];
287 if ( is_string( $rec ) ) {
289 self
::$mList[$name] = new $className;
290 } elseif ( is_array( $rec ) ) {
291 $className = array_shift( $rec );
292 self
::$mList[$name] = wfCreateObject( $className, $rec );
294 return self
::$mList[$name];
301 * Get a special page with a given localised name, or NULL if there
302 * is no such special page.
304 static function getPageByAlias( $alias ) {
305 $realName = self
::resolveAlias( $alias );
307 return self
::getPage( $realName );
314 * Return categorised listable special pages for all users
317 static function getRegularPages() {
318 if ( !self
::$mListInitialised ) {
323 foreach ( self
::$mList as $name => $rec ) {
324 $page = self
::getPage( $name );
325 if ( $page->isListed() && $page->getRestriction() == '' ) {
326 $pages[$name] = $page;
333 * Return categorised listable special pages which are available
334 * for the current user, but not for everyone
337 static function getRestrictedPages() {
339 if ( !self
::$mListInitialised ) {
344 foreach ( self
::$mList as $name => $rec ) {
345 $page = self
::getPage( $name );
346 if ( $page->isListed() ) {
347 $restriction = $page->getRestriction();
348 if ( $restriction != '' && $wgUser->isAllowed( $restriction ) ) {
349 $pages[$name] = $page;
357 * Execute a special page path.
358 * The path may contain parameters, e.g. Special:Name/Params
359 * Extracts the special page name and call the execute method, passing the parameters
361 * Returns a title object if the page is redirected, false if there was no such special
362 * page, and true if it was successful.
364 * @param $title a title object
365 * @param $including output is being captured for use in {{special:whatever}}
367 static function executePath( &$title, $including = false ) {
368 global $wgOut, $wgTitle, $wgRequest;
369 wfProfileIn( __METHOD__
);
371 # FIXME: redirects broken due to this call
372 $bits = explode( '/', $title->getDBkey(), 2 );
374 if( !isset( $bits[1] ) ) { // bug 2087
379 $page = SpecialPage
::getPageByAlias( $name );
384 $wgOut->setArticleRelated( false );
385 $wgOut->setRobotpolicy( 'noindex,nofollow' );
386 $wgOut->setStatusCode( 404 );
387 $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
389 wfProfileOut( __METHOD__
);
395 $redirect = $page->getRedirect( $par );
397 $query = $page->getRedirectQuery();
398 $url = $redirect->getFullUrl( $query );
399 $wgOut->redirect( $url );
400 wfProfileOut( __METHOD__
);
405 # Redirect to canonical alias for GET commands
406 # Not for POST, we'd lose the post data, so it's best to just distribute
407 # the request. Such POST requests are possible for old extensions that
408 # generate self-links without being aware that their default name has
410 if ( !$including && $name != $page->getLocalName() && !$wgRequest->wasPosted() ) {
412 unset( $query['title'] );
413 $query = wfArrayToCGI( $query );
414 $title = $page->getTitle( $par );
415 $url = $title->getFullUrl( $query );
416 $wgOut->redirect( $url );
417 wfProfileOut( __METHOD__
);
421 if ( $including && !$page->includable() ) {
422 wfProfileOut( __METHOD__
);
424 } elseif ( !$including ) {
425 $wgTitle = $page->getTitle();
427 $page->including( $including );
429 // Execute special page
430 $profName = 'Special:' . $page->getName();
431 wfProfileIn( $profName );
432 $page->execute( $par );
433 wfProfileOut( $profName );
434 wfProfileOut( __METHOD__
);
439 * Just like executePath() except it returns the HTML instead of outputting it
440 * Returns false if there was no such special page, or a title object if it was
444 static function capturePath( &$title ) {
445 global $wgOut, $wgTitle;
447 $oldTitle = $wgTitle;
449 $wgOut = new OutputPage
;
451 $ret = SpecialPage
::executePath( $title, true );
452 if ( $ret === true ) {
453 $ret = $wgOut->getHTML();
455 $wgTitle = $oldTitle;
461 * Get the local name for a specified canonical name
463 static function getLocalNameFor( $name, $subpage = false ) {
465 $aliases = $wgContLang->getSpecialPageAliases();
466 if ( isset( $aliases[$name][0] ) ) {
467 $name = $aliases[$name][0];
469 if ( $subpage !== false && !is_null( $subpage ) ) {
470 $name = "$name/$subpage";
476 * Get a localised Title object for a specified special page name
478 static function getTitleFor( $name, $subpage = false ) {
479 $name = self
::getLocalNameFor( $name, $subpage );
481 return Title
::makeTitle( NS_SPECIAL
, $name );
483 throw new MWException( "Invalid special page name \"$name\"" );
488 * Get a localised Title object for a page name with a possibly unvalidated subpage
490 static function getSafeTitleFor( $name, $subpage = false ) {
491 $name = self
::getLocalNameFor( $name, $subpage );
493 return Title
::makeTitleSafe( NS_SPECIAL
, $name );
500 * Get a title for a given alias
501 * @return Title or null if there is no such alias
503 static function getTitleForAlias( $alias ) {
504 $name = self
::resolveAlias( $alias );
506 return self
::getTitleFor( $name );
513 * Default constructor for special pages
514 * Derivative classes should call this from their constructor
515 * Note that if the user does not have the required level, an error message will
516 * be displayed by the default execute() method, without the global function ever
519 * If you override execute(), you can recover the default behaviour with userCanExecute()
520 * and displayRestrictionError()
522 * @param string $name Name of the special page, as seen in links and URLs
523 * @param string $restriction User right required, e.g. "block" or "delete"
524 * @param boolean $listed Whether the page is listed in Special:Specialpages
525 * @param string $function Function called by execute(). By default it is constructed from $name
526 * @param string $file File which is included by execute(). It is also constructed from $name by default
528 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
529 $this->mName
= $name;
530 $this->mRestriction
= $restriction;
531 $this->mListed
= $listed;
532 $this->mIncludable
= $includable;
533 if ( $function == false ) {
534 $this->mFunction
= 'wfSpecial'.$name;
536 $this->mFunction
= $function;
538 if ( $file === 'default' ) {
539 $this->mFile
= dirname(__FILE__
) . "/Special{$name}.php";
541 $this->mFile
= $file;
550 function getName() { return $this->mName
; }
551 function getRestriction() { return $this->mRestriction
; }
552 function getFile() { return $this->mFile
; }
553 function isListed() { return $this->mListed
; }
557 * Accessor and mutator
559 function name( $x = NULL ) { return wfSetVar( $this->mName
, $x ); }
560 function restrictions( $x = NULL) { return wfSetVar( $this->mRestrictions
, $x ); }
561 function listed( $x = NULL) { return wfSetVar( $this->mListed
, $x ); }
562 function func( $x = NULL) { return wfSetVar( $this->mFunction
, $x ); }
563 function file( $x = NULL) { return wfSetVar( $this->mFile
, $x ); }
564 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable
, $x ); }
565 function including( $x = NULL ) { return wfSetVar( $this->mIncluding
, $x ); }
569 * Get the localised name of the special page
571 function getLocalName() {
572 if ( !isset( $this->mLocalName
) ) {
573 $this->mLocalName
= self
::getLocalNameFor( $this->mName
);
575 return $this->mLocalName
;
579 * Checks if the given user (identified by an object) can execute this
580 * special page (as defined by $mRestriction)
582 function userCanExecute( &$user ) {
583 return $user->isAllowed( $this->mRestriction
);
587 * Output an error message telling the user what access level they have to have
589 function displayRestrictionError() {
591 $wgOut->permissionRequired( $this->mRestriction
);
595 * Sets headers - this should be called from the execute() method of all derived classes!
597 function setHeaders() {
599 $wgOut->setArticleRelated( false );
600 $wgOut->setRobotPolicy( "noindex,nofollow" );
601 $wgOut->setPageTitle( $this->getDescription() );
605 * Default execute method
606 * Checks user permissions, calls the function given in mFunction
608 * This may be overridden by subclasses.
610 function execute( $par ) {
615 if ( $this->userCanExecute( $wgUser ) ) {
616 $func = $this->mFunction
;
617 // only load file if the function does not exist
618 if(!function_exists($func) and $this->mFile
) {
619 require_once( $this->mFile
);
621 # FIXME: these hooks are broken for extensions and anything else that subclasses SpecialPage.
622 if ( wfRunHooks( 'SpecialPageExecuteBeforeHeader', array( &$this, &$par, &$func ) ) )
623 $this->outputHeader();
624 if ( ! wfRunHooks( 'SpecialPageExecuteBeforePage', array( &$this, &$par, &$func ) ) )
626 $func( $par, $this );
627 if ( ! wfRunHooks( 'SpecialPageExecuteAfterPage', array( &$this, &$par, &$func ) ) )
630 $this->displayRestrictionError();
634 function outputHeader() {
635 global $wgOut, $wgContLang;
637 $msg = $wgContLang->lc( $this->name() ) . '-summary';
638 $out = wfMsg( $msg );
639 if ( ! wfEmptyMsg( $msg, $out ) and $out !== '' and ! $this->including() )
640 $wgOut->addWikiText( $out );
644 # Returns the name that goes in the <h1> in the special page itself, and also the name that
645 # will be listed in Special:Specialpages
647 # Derived classes can override this, but usually it is easier to keep the default behaviour.
648 # Messages can be added at run-time, see MessageCache.php
649 function getDescription() {
650 return wfMsg( strtolower( $this->mName
) );
654 * Get a self-referential title object
656 function getTitle( $subpage = false) {
657 return self
::getTitleFor( $this->mName
, $subpage );
661 * Set whether this page is listed in Special:Specialpages, at run-time
663 function setListed( $listed ) {
664 return wfSetVar( $this->mListed
, $listed );
668 * If the special page is a redirect, then get the Title object it redirects to.
671 function getRedirect( $subpage ) {
676 * Return part of the request string for a special redirect page
677 * This allows passing, e.g. action=history to Special:Mypage, etc.
681 function getRedirectQuery() {
684 foreach( $this->mAllowedRedirectParams
as $arg ) {
685 if( $val = $wgRequest->getVal( $arg, false ) )
686 $params[] = $arg . '=' . $val;
689 return count( $params ) ?
implode( '&', $params ) : false;
694 * Shortcut to construct a special page which is unlisted by default
696 class UnlistedSpecialPage
extends SpecialPage
698 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
699 SpecialPage
::SpecialPage( $name, $restriction, false, $function, $file );
704 * 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() );