3 * SpecialPage: handling special pages and lists thereof
4 * $wgSpecialPages is a list of all SpecialPage objects. These objects are
5 * either instances of SpecialPage or a sub-class thereof. They have an
6 * execute() method, which sends the HTML for the special page to $wgOut.
7 * The parent class has an execute() method which distributes the call to
8 * the historical global functions. Additionally, execute() also checks if the
9 * user has the necessary access privileges and bails out if not.
11 * To add a special page at run-time, use SpecialPage::addPage().
12 * DO NOT manipulate this array at run-time.
15 * @subpackage SpecialPage
19 * @todo put all global specialpages stuff into class
21 global $wgSpecialPages, $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
26 $wgSpecialPages = array(
27 'DoubleRedirects' => new SpecialPage ( 'DoubleRedirects' ),
28 'BrokenRedirects' => new SpecialPage ( 'BrokenRedirects' ),
29 'Disambiguations' => new SpecialPage ( 'Disambiguations' ),
31 'Userlogin' => new SpecialPage( 'Userlogin' ),
32 'Userlogout' => new UnlistedSpecialPage( 'Userlogout' ),
33 'Preferences' => new SpecialPage( 'Preferences' ),
34 'Watchlist' => new SpecialPage( 'Watchlist' ),
36 'Recentchanges' => new IncludableSpecialPage( 'Recentchanges' ),
37 'Upload' => new SpecialPage( 'Upload' ),
38 'Imagelist' => new SpecialPage( 'Imagelist' ),
39 'Newimages' => new IncludableSpecialPage( 'Newimages' ),
40 'Listusers' => new SpecialPage( 'Listusers' ),
41 'Statistics' => new SpecialPage( 'Statistics' ),
42 'Random' => new SpecialPage( 'Randompage' ),
43 'Lonelypages' => new SpecialPage( 'Lonelypages' ),
44 'Uncategorizedpages'=> new SpecialPage( 'Uncategorizedpages' ),
45 'Uncategorizedcategories'=> new SpecialPage( 'Uncategorizedcategories' ),
46 'Unusedcategories' => new SpecialPage( 'Unusedcategories' ),
47 'Unusedimages' => new SpecialPage( 'Unusedimages' ),
48 'Wantedpages' => new IncludableSpecialPage( 'Wantedpages' ),
49 'Wantedcategories' => new SpecialPage( 'Wantedcategories' ),
50 'Mostlinked' => new SpecialPage( 'Mostlinked' ),
51 'Mostlinkedcategories' => new SpecialPage( 'Mostlinkedcategories' ),
52 'Mostcategories' => new SpecialPage( 'Mostcategories' ),
53 'Mostimages' => new SpecialPage( 'Mostimages' ),
54 'Mostrevisions' => new SpecialPage( 'Mostrevisions' ),
55 'Shortpages' => new SpecialPage( 'Shortpages' ),
56 'Longpages' => new SpecialPage( 'Longpages' ),
57 'Newpages' => new IncludableSpecialPage( 'Newpages' ),
58 'Ancientpages' => new SpecialPage( 'Ancientpages' ),
59 'Deadendpages' => new SpecialPage( 'Deadendpages' ),
60 'Allpages' => new IncludableSpecialPage( 'Allpages' ),
61 'Prefixindex' => new IncludableSpecialPage( 'Prefixindex' ) ,
62 'Ipblocklist' => new SpecialPage( 'Ipblocklist' ),
63 'Specialpages' => new UnlistedSpecialPage( 'Specialpages' ),
64 'Contributions' => new UnlistedSpecialPage( 'Contributions' ),
65 'Emailuser' => new UnlistedSpecialPage( 'Emailuser' ),
66 'Whatlinkshere' => new UnlistedSpecialPage( 'Whatlinkshere' ),
67 'Recentchangeslinked' => new UnlistedSpecialPage( 'Recentchangeslinked' ),
68 'Movepage' => new UnlistedSpecialPage( 'Movepage' ),
69 'Blockme' => new UnlistedSpecialPage( 'Blockme' ),
70 'Booksources' => new SpecialPage( 'Booksources' ),
71 'Categories' => new SpecialPage( 'Categories' ),
72 'Export' => new SpecialPage( 'Export' ),
73 'Version' => new SpecialPage( 'Version' ),
74 'Allmessages' => new SpecialPage( 'Allmessages' ),
75 'Log' => new SpecialPage( 'Log' ),
76 'Blockip' => new SpecialPage( 'Blockip', 'block' ),
77 'Undelete' => new SpecialPage( 'Undelete', 'deletedhistory' ),
78 "Import" => new SpecialPage( "Import", 'import' ),
79 'Lockdb' => new SpecialPage( 'Lockdb', 'siteadmin' ),
80 'Unlockdb' => new SpecialPage( 'Unlockdb', 'siteadmin' ),
81 'Userrights' => new SpecialPage( 'Userrights', 'userrights' ),
82 'MIMEsearch' => new SpecialPage( 'MIMEsearch' ),
83 'Unwatchedpages' => new SpecialPage( 'Unwatchedpages', 'unwatchedpages' ),
84 'Listredirects' => new SpecialPage( 'Listredirects' ),
85 'Revisiondelete' => new SpecialPage( 'Revisiondelete', 'deleterevision' ),
86 'Unusedtemplates' => new SpecialPage( 'Unusedtemplates' ),
87 'Randomredirect' => new SpecialPage( 'Randomredirect' ),
90 if( !$wgDisableCounters ) {
91 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
94 if( !$wgDisableInternalSearch ) {
95 $wgSpecialPages['Search'] = new SpecialPage( 'Search' );
98 if( $wgEmailAuthentication ) {
99 $wgSpecialPages['Confirmemail'] = new UnlistedSpecialPage( 'Confirmemail' );
103 * Parent special page class, also static functions for handling the special
113 * The name of the class, used in the URL.
114 * Also used for the default <h1> heading, @see getDescription()
118 * Minimum user level required to access this page, or "" for anyone.
119 * Also used to categorise the pages in Special:Specialpages
123 * Listed in Special:Specialpages?
127 * Function name called by the default execute()
131 * File which needs to be included before the function above can be called
135 * Whether or not this special page is being included from an article
139 * Whether the special page can be included in an article
148 * Add a page to the list of valid special pages
149 * $obj->execute() must send HTML to $wgOut then return
150 * Use this for a special page extension
153 static function addPage( &$obj ) {
154 global $wgSpecialPages;
155 $wgSpecialPages[$obj->mName
] = $obj;
159 * Remove a special page from the list
160 * Occasionally used to disable expensive or dangerous special pages
163 static function removePage( $name ) {
164 global $wgSpecialPages;
165 unset( $wgSpecialPages[$name] );
169 * Find the object with a given name and return it (or NULL)
171 * @param string $name
173 static function getPage( $name ) {
174 global $wgSpecialPages;
175 if ( array_key_exists( $name, $wgSpecialPages ) ) {
176 return $wgSpecialPages[$name];
184 * @param string $name
185 * @return mixed Title object if the redirect exists, otherwise NULL
187 static function getRedirect( $name ) {
191 'Mypage' => Title
::makeTitle( NS_USER
, $wgUser->getName() ),
192 'Mytalk' => Title
::makeTitle( NS_USER_TALK
, $wgUser->getName() ),
193 'Mycontributions' => Title
::makeTitle( NS_SPECIAL
, 'Contributions/' . $wgUser->getName() ),
194 'Listadmins' => Title
::makeTitle( NS_SPECIAL
, 'Listusers/sysop' ), # @bug 2832
195 'Randompage' => Title
::makeTitle( NS_SPECIAL
, 'Random' ),
196 'Userlist' => Title
::makeTitle( NS_SPECIAL
, 'Listusers' )
198 wfRunHooks( 'SpecialPageGetRedirect', array( &$redirects ) );
200 return isset( $redirects[$name] ) ?
$redirects[$name] : null;
204 * Return part of the request string for a special redirect page
205 * This allows passing, e.g. action=history to Special:Mypage, etc.
207 * @param $name Name of the redirect page
210 function getRedirectParams( $name ) {
218 $args = array( 'action' );
222 foreach( $args as $arg ) {
223 if( $val = $wgRequest->getVal( $arg, false ) )
224 $params[] = $arg . '=' . $val;
227 return count( $params ) ?
implode( '&', $params ) : false;
231 * Return categorised listable special pages
232 * Returns a 2d array where the first index is the restriction name
235 static function getPages() {
236 global $wgSpecialPages;
240 'developer' => array()
243 foreach ( $wgSpecialPages as $name => $page ) {
244 if ( $page->isListed() ) {
245 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
252 * Execute a special page path.
253 * The path may contain parameters, e.g. Special:Name/Params
254 * Extracts the special page name and call the execute method, passing the parameters
256 * Returns a title object if the page is redirected, false if there was no such special
257 * page, and true if it was successful.
259 * @param $title a title object
260 * @param $including output is being captured for use in {{special:whatever}}
262 function executePath( &$title, $including = false ) {
263 global $wgOut, $wgTitle;
264 $fname = 'SpecialPage::executePath';
265 wfProfileIn( $fname );
267 $bits = split( "/", $title->getDBkey(), 2 );
269 if( !isset( $bits[1] ) ) { // bug 2087
275 $page = SpecialPage
::getPage( $name );
276 if ( is_null( $page ) ) {
278 wfProfileOut( $fname );
281 $redir = SpecialPage
::getRedirect( $name );
282 if ( isset( $redir ) ) {
284 $redir = Title
::makeTitle( $redir->getNamespace(), $redir->getText() . '/' . $par );
285 $params = SpecialPage
::getRedirectParams( $name );
287 $url = $redir->getFullUrl( $params );
289 $url = $redir->getFullUrl();
291 $wgOut->redirect( $url );
293 $wgOut->redirect( $url );
296 $wgOut->setArticleRelated( false );
297 $wgOut->setRobotpolicy( 'noindex,follow' );
298 $wgOut->setStatusCode( 404 );
299 $wgOut->errorpage( 'nosuchspecialpage', 'nospecialpagetext' );
304 if ( $including && !$page->includable() ) {
305 wfProfileOut( $fname );
307 } elseif ( !$including ) {
309 $wgTitle = Title
::makeTitle( NS_SPECIAL
, $name );
314 $page->including( $including );
316 $profName = 'Special:' . $page->getName();
317 wfProfileIn( $profName );
318 $page->execute( $par );
319 wfProfileOut( $profName );
322 wfProfileOut( $fname );
327 * Just like executePath() except it returns the HTML instead of outputting it
328 * Returns false if there was no such special page, or a title object if it was
332 static function capturePath( &$title ) {
333 global $wgOut, $wgTitle;
335 $oldTitle = $wgTitle;
337 $wgOut = new OutputPage
;
339 $ret = SpecialPage
::executePath( $title, true );
340 if ( $ret === true ) {
341 $ret = $wgOut->getHTML();
343 $wgTitle = $oldTitle;
349 * Default constructor for special pages
350 * Derivative classes should call this from their constructor
351 * Note that if the user does not have the required level, an error message will
352 * be displayed by the default execute() method, without the global function ever
355 * If you override execute(), you can recover the default behaviour with userCanExecute()
356 * and displayRestrictionError()
358 * @param string $name Name of the special page, as seen in links and URLs
359 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
360 * @param boolean $listed Whether the page is listed in Special:Specialpages
361 * @param string $function Function called by execute(). By default it is constructed from $name
362 * @param string $file File which is included by execute(). It is also constructed from $name by default
364 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
365 $this->mName
= $name;
366 $this->mRestriction
= $restriction;
367 $this->mListed
= $listed;
368 $this->mIncludable
= $includable;
369 if ( $function == false ) {
370 $this->mFunction
= 'wfSpecial'.$name;
372 $this->mFunction
= $function;
374 if ( $file === 'default' ) {
375 $this->mFile
= "Special{$name}.php";
377 $this->mFile
= $file;
386 function getName() { return $this->mName
; }
387 function getRestriction() { return $this->mRestriction
; }
388 function getFile() { return $this->mFile
; }
389 function isListed() { return $this->mListed
; }
393 * Accessor and mutator
395 function name( $x = NULL ) { return wfSetVar( $this->mName
, $x ); }
396 function restrictions( $x = NULL) { return wfSetVar( $this->mRestrictions
, $x ); }
397 function listed( $x = NULL) { return wfSetVar( $this->mListed
, $x ); }
398 function func( $x = NULL) { return wfSetVar( $this->mFunction
, $x ); }
399 function file( $x = NULL) { return wfSetVar( $this->mFile
, $x ); }
400 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable
, $x ); }
401 function including( $x = NULL ) { return wfSetVar( $this->mIncluding
, $x ); }
405 * Checks if the given user (identified by an object) can execute this
406 * special page (as defined by $mRestriction)
408 function userCanExecute( &$user ) {
409 if ( $this->mRestriction
== "" ) {
412 if ( in_array( $this->mRestriction
, $user->getRights() ) ) {
421 * Output an error message telling the user what access level they have to have
423 function displayRestrictionError() {
425 $wgOut->permissionRequired( $this->mRestriction
);
429 * Sets headers - this should be called from the execute() method of all derived classes!
431 function setHeaders() {
433 $wgOut->setArticleRelated( false );
434 $wgOut->setRobotPolicy( "noindex,follow" );
435 $wgOut->setPageTitle( $this->getDescription() );
439 * Default execute method
440 * Checks user permissions, calls the function given in mFunction
442 function execute( $par ) {
447 if ( $this->userCanExecute( $wgUser ) ) {
448 $func = $this->mFunction
;
449 // only load file if the function does not exist
450 if(!function_exists($func) and $this->mFile
) {
451 require_once( $this->mFile
);
453 if ( wfRunHooks( 'SpecialPageExecuteBeforeHeader', array( &$this, &$par, &$func ) ) )
454 $this->outputHeader();
455 if ( ! wfRunHooks( 'SpecialPageExecuteBeforePage', array( &$this, &$par, &$func ) ) )
457 $func( $par, $this );
458 if ( ! wfRunHooks( 'SpecialPageExecuteAfterPage', array( &$this, &$par, &$func ) ) )
461 $this->displayRestrictionError();
465 function outputHeader() {
466 global $wgOut, $wgContLang;
468 $msg = $wgContLang->lc( $this->name() ) . '-summary';
469 $out = wfMsg( $msg );
470 if ( ! wfEmptyMsg( $msg, $out ) and $out !== '' and ! $this->including() )
471 $wgOut->addWikiText( $out );
475 # Returns the name that goes in the <h1> in the special page itself, and also the name that
476 # will be listed in Special:Specialpages
478 # Derived classes can override this, but usually it is easier to keep the default behaviour.
479 # Messages can be added at run-time, see MessageCache.php
480 function getDescription() {
481 return wfMsg( strtolower( $this->mName
) );
485 * Get a self-referential title object
487 function getTitle() {
488 return Title
::makeTitle( NS_SPECIAL
, $this->mName
);
492 * Set whether this page is listed in Special:Specialpages, at run-time
494 function setListed( $listed ) {
495 return wfSetVar( $this->mListed
, $listed );
501 * Shortcut to construct a special page which is unlisted by default
504 class UnlistedSpecialPage
extends SpecialPage
506 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
507 SpecialPage
::SpecialPage( $name, $restriction, false, $function, $file );
512 * Shortcut to construct an includable special page
515 class IncludableSpecialPage
extends SpecialPage
517 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
518 SpecialPage
::SpecialPage( $name, $restriction, $listed, $function, $file, true );