Fix #4834: correct XHTML output when using $wgMaxTocLevel
[mediawiki.git] / includes / SpecialPage.php
bloba0eca5a9bc8c23f569108788fc3e69cb432594dd
1 <?php
2 /**
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.
14 * @package MediaWiki
15 * @subpackage SpecialPage
19 /**
20 * @access private
22 $wgSpecialPages = array(
23 'DoubleRedirects' => new SpecialPage ( 'DoubleRedirects' ),
24 'BrokenRedirects' => new SpecialPage ( 'BrokenRedirects' ),
25 'Disambiguations' => new SpecialPage ( 'Disambiguations' ),
27 'Userlogin' => new SpecialPage( 'Userlogin' ),
28 'Userlogout' => new UnlistedSpecialPage( 'Userlogout' ),
29 'Preferences' => new SpecialPage( 'Preferences' ),
30 'Watchlist' => new SpecialPage( 'Watchlist' ),
32 'Recentchanges' => new IncludableSpecialPage( 'Recentchanges' ),
33 'Upload' => new SpecialPage( 'Upload' ),
34 'Imagelist' => new SpecialPage( 'Imagelist' ),
35 'Newimages' => new IncludableSpecialPage( 'Newimages' ),
36 'Listusers' => new SpecialPage( 'Listusers' ),
37 'Statistics' => new SpecialPage( 'Statistics' ),
38 'Random' => new SpecialPage( 'Randompage' ),
39 'Lonelypages' => new SpecialPage( 'Lonelypages' ),
40 'Uncategorizedpages'=> new SpecialPage( 'Uncategorizedpages' ),
41 'Uncategorizedcategories'=> new SpecialPage( 'Uncategorizedcategories' ),
42 'Unusedcategories' => new SpecialPage( 'Unusedcategories' ),
43 'Unusedimages' => new SpecialPage( 'Unusedimages' ),
44 'Wantedpages' => new IncludableSpecialPage( 'Wantedpages' ),
45 'Wantedcategories' => new SpecialPage( 'Wantedcategories' ),
46 'Mostlinked' => new SpecialPage( 'Mostlinked' ),
47 'Mostlinkedcategories' => new SpecialPage( 'Mostlinkedcategories' ),
48 'Mostcategories' => new SpecialPage( 'Mostcategories' ),
49 'Mostimages' => new SpecialPage( 'Mostimages' ),
50 'Mostrevisions' => new SpecialPage( 'Mostrevisions' ),
51 'Shortpages' => new SpecialPage( 'Shortpages' ),
52 'Longpages' => new SpecialPage( 'Longpages' ),
53 'Newpages' => new IncludableSpecialPage( 'Newpages' ),
54 'Ancientpages' => new SpecialPage( 'Ancientpages' ),
55 'Deadendpages' => new SpecialPage( 'Deadendpages' ),
56 'Allpages' => new IncludableSpecialPage( 'Allpages' ),
57 'Prefixindex' => new IncludableSpecialPage( 'Prefixindex' ) ,
58 'Ipblocklist' => new SpecialPage( 'Ipblocklist' ),
59 'Specialpages' => new UnlistedSpecialPage( 'Specialpages' ),
60 'Contributions' => new UnlistedSpecialPage( 'Contributions' ),
61 'Emailuser' => new UnlistedSpecialPage( 'Emailuser' ),
62 'Whatlinkshere' => new UnlistedSpecialPage( 'Whatlinkshere' ),
63 'Recentchangeslinked' => new UnlistedSpecialPage( 'Recentchangeslinked' ),
64 'Movepage' => new UnlistedSpecialPage( 'Movepage' ),
65 'Blockme' => new UnlistedSpecialPage( 'Blockme' ),
66 'Booksources' => new SpecialPage( 'Booksources' ),
67 'Categories' => new SpecialPage( 'Categories' ),
68 'Export' => new SpecialPage( 'Export' ),
69 'Version' => new SpecialPage( 'Version' ),
70 'Allmessages' => new SpecialPage( 'Allmessages' ),
71 'Log' => new SpecialPage( 'Log' ),
72 'Blockip' => new SpecialPage( 'Blockip', 'block' ),
73 'Undelete' => new SpecialPage( 'Undelete', 'deletedhistory' ),
74 "Import" => new SpecialPage( "Import", 'import' ),
75 'Lockdb' => new SpecialPage( 'Lockdb', 'siteadmin' ),
76 'Unlockdb' => new SpecialPage( 'Unlockdb', 'siteadmin' ),
77 'Userrights' => new SpecialPage( 'Userrights', 'userrights' ),
78 'MIMEsearch' => new SpecialPage( 'MIMEsearch' ),
79 'Unwatchedpages' => new SpecialPage( 'Unwatchedpages', 'unwatchedpages' ),
80 'Listredirects' => new SpecialPage( 'Listredirects' ),
81 'Revisiondelete' => new SpecialPage( 'Revisiondelete', 'deleterevision' ),
82 'Unusedtemplates' => new SpecialPage( 'Unusedtemplates' ),
83 'Randomredirect' => new SpecialPage( 'Randomredirect' ),
86 if( !$wgDisableCounters ) {
87 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
90 if( !$wgDisableInternalSearch ) {
91 $wgSpecialPages['Search'] = new SpecialPage( 'Search' );
94 if( $wgEmailAuthentication ) {
95 $wgSpecialPages['Confirmemail'] = new UnlistedSpecialPage( 'Confirmemail' );
98 /**
99 * Parent special page class, also static functions for handling the special
100 * page list
101 * @package MediaWiki
103 class SpecialPage
105 /**#@+
106 * @access private
109 * The name of the class, used in the URL.
110 * Also used for the default <h1> heading, @see getDescription()
112 var $mName;
114 * Minimum user level required to access this page, or "" for anyone.
115 * Also used to categorise the pages in Special:Specialpages
117 var $mRestriction;
119 * Listed in Special:Specialpages?
121 var $mListed;
123 * Function name called by the default execute()
125 var $mFunction;
127 * File which needs to be included before the function above can be called
129 var $mFile;
131 * Whether or not this special page is being included from an article
133 var $mIncluding;
135 * Whether the special page can be included in an article
137 var $mIncludable;
140 /**#@-*/
144 * Add a page to the list of valid special pages
145 * $obj->execute() must send HTML to $wgOut then return
146 * Use this for a special page extension
147 * @static
149 function addPage( &$obj ) {
150 global $wgSpecialPages;
151 $wgSpecialPages[$obj->mName] = $obj;
155 * Remove a special page from the list
156 * Occasionally used to disable expensive or dangerous special pages
157 * @static
159 function removePage( $name ) {
160 global $wgSpecialPages;
161 unset( $wgSpecialPages[$name] );
165 * Find the object with a given name and return it (or NULL)
166 * @static
167 * @param string $name
169 function getPage( $name ) {
170 global $wgSpecialPages;
171 if ( array_key_exists( $name, $wgSpecialPages ) ) {
172 return $wgSpecialPages[$name];
173 } else {
174 return NULL;
179 * @static
180 * @param string $name
181 * @return mixed Title object if the redirect exists, otherwise NULL
183 function getRedirect( $name ) {
184 global $wgUser;
186 $redirects = array(
187 'Mypage' => Title::makeTitle( NS_USER, $wgUser->getName() ),
188 'Mytalk' => Title::makeTitle( NS_USER_TALK, $wgUser->getName() ),
189 'Mycontributions' => Title::makeTitle( NS_SPECIAL, 'Contributions/' . $wgUser->getName() ),
190 'Listadmins' => Title::makeTitle( NS_SPECIAL, 'Listusers/sysop' ), # @bug 2832
191 'Randompage' => Title::makeTitle( NS_SPECIAL, 'Random' )
193 wfRunHooks( 'SpecialPageGetRedirect', array( &$redirects ) );
195 return isset( $redirects[$name] ) ? $redirects[$name] : null;
199 * Return categorised listable special pages
200 * Returns a 2d array where the first index is the restriction name
201 * @static
203 function getPages() {
204 global $wgSpecialPages;
205 $pages = array(
206 '' => array(),
207 'sysop' => array(),
208 'developer' => array()
211 foreach ( $wgSpecialPages as $name => $page ) {
212 if ( $page->isListed() ) {
213 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
216 return $pages;
220 * Execute a special page path.
221 * The path may contain parameters, e.g. Special:Name/Params
222 * Extracts the special page name and call the execute method, passing the parameters
224 * Returns a title object if the page is redirected, false if there was no such special
225 * page, and true if it was successful.
227 * @param $title a title object
228 * @param $including output is being captured for use in {{special:whatever}}
230 function executePath( &$title, $including = false ) {
231 global $wgOut, $wgTitle;
232 $fname = 'SpecialPage::executePath';
233 wfProfileIn( $fname );
235 $bits = split( "/", $title->getDBkey(), 2 );
236 $name = $bits[0];
237 if( !isset( $bits[1] ) ) { // bug 2087
238 $par = NULL;
239 } else {
240 $par = $bits[1];
243 $page = SpecialPage::getPage( $name );
244 if ( is_null( $page ) ) {
245 if ( $including ) {
246 wfProfileOut( $fname );
247 return false;
248 } else {
249 $redir = SpecialPage::getRedirect( $name );
250 if ( isset( $redir ) ) {
251 if ( isset( $par ) )
252 $wgOut->redirect( $redir->getFullURL() . '/' . $par );
253 else
254 $wgOut->redirect( $redir->getFullURL() );
255 $retVal = $redir;
256 } else {
257 $wgOut->setArticleRelated( false );
258 $wgOut->setRobotpolicy( 'noindex,follow' );
259 $wgOut->setStatusCode( 404 );
260 $wgOut->errorpage( 'nosuchspecialpage', 'nospecialpagetext' );
261 $retVal = false;
264 } else {
265 if ( $including && !$page->includable() ) {
266 wfProfileOut( $fname );
267 return false;
269 if($par !== NULL) {
270 $wgTitle = Title::makeTitle( NS_SPECIAL, $name );
271 } else {
272 $wgTitle = $title;
274 $page->including( $including );
276 $profName = 'Special:' . $page->getName();
277 wfProfileIn( $profName );
278 $page->execute( $par );
279 wfProfileOut( $profName );
280 $retVal = true;
282 wfProfileOut( $fname );
283 return $retVal;
287 * Just like executePath() except it returns the HTML instead of outputting it
288 * Returns false if there was no such special page, or a title object if it was
289 * a redirect.
290 * @static
292 function capturePath( &$title ) {
293 global $wgOut, $wgTitle;
295 $oldTitle = $wgTitle;
296 $oldOut = $wgOut;
297 $wgOut = new OutputPage;
299 $ret = SpecialPage::executePath( $title, true );
300 if ( $ret === true ) {
301 $ret = $wgOut->getHTML();
303 $wgTitle = $oldTitle;
304 $wgOut = $oldOut;
305 return $ret;
309 * Default constructor for special pages
310 * Derivative classes should call this from their constructor
311 * Note that if the user does not have the required level, an error message will
312 * be displayed by the default execute() method, without the global function ever
313 * being called.
315 * If you override execute(), you can recover the default behaviour with userCanExecute()
316 * and displayRestrictionError()
318 * @param string $name Name of the special page, as seen in links and URLs
319 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
320 * @param boolean $listed Whether the page is listed in Special:Specialpages
321 * @param string $function Function called by execute(). By default it is constructed from $name
322 * @param string $file File which is included by execute(). It is also constructed from $name by default
324 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
325 $this->mName = $name;
326 $this->mRestriction = $restriction;
327 $this->mListed = $listed;
328 $this->mIncludable = $includable;
329 if ( $function == false ) {
330 $this->mFunction = 'wfSpecial'.$name;
331 } else {
332 $this->mFunction = $function;
334 if ( $file === 'default' ) {
335 $this->mFile = "Special{$name}.php";
336 } else {
337 $this->mFile = $file;
341 /**#@+
342 * Accessor
344 * @deprecated
346 function getName() { return $this->mName; }
347 function getRestriction() { return $this->mRestriction; }
348 function getFile() { return $this->mFile; }
349 function isListed() { return $this->mListed; }
350 /**#@-*/
352 /**#@+
353 * Accessor and mutator
355 function name( $x = NULL ) { return wfSetVar( $this->mName, $x ); }
356 function restrictions( $x = NULL) { return wfSetVar( $this->mRestrictions, $x ); }
357 function listed( $x = NULL) { return wfSetVar( $this->mListed, $x ); }
358 function func( $x = NULL) { return wfSetVar( $this->mFunction, $x ); }
359 function file( $x = NULL) { return wfSetVar( $this->mFile, $x ); }
360 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable, $x ); }
361 function including( $x = NULL ) { return wfSetVar( $this->mIncluding, $x ); }
362 /**#@-*/
365 * Checks if the given user (identified by an object) can execute this
366 * special page (as defined by $mRestriction)
368 function userCanExecute( &$user ) {
369 if ( $this->mRestriction == "" ) {
370 return true;
371 } else {
372 if ( in_array( $this->mRestriction, $user->getRights() ) ) {
373 return true;
374 } else {
375 return false;
381 * Output an error message telling the user what access level they have to have
383 function displayRestrictionError() {
384 global $wgOut;
385 $wgOut->permissionRequired( $this->mRestriction );
389 * Sets headers - this should be called from the execute() method of all derived classes!
391 function setHeaders() {
392 global $wgOut;
393 $wgOut->setArticleRelated( false );
394 $wgOut->setRobotPolicy( "noindex,follow" );
395 $wgOut->setPageTitle( $this->getDescription() );
399 * Default execute method
400 * Checks user permissions, calls the function given in mFunction
402 function execute( $par ) {
403 global $wgUser;
405 $this->setHeaders();
407 if ( $this->userCanExecute( $wgUser ) ) {
408 $func = $this->mFunction;
409 // only load file if the function does not exist
410 if(!function_exists($func) and $this->mFile) {
411 require_once( $this->mFile );
413 if ( wfRunHooks( 'SpecialPageExecuteBeforeHeader', array( &$this, &$par, &$func ) ) )
414 $this->outputHeader();
415 if ( ! wfRunHooks( 'SpecialPageExecuteBeforePage', array( &$this, &$par, &$func ) ) )
416 return;
417 $func( $par, $this );
418 if ( ! wfRunHooks( 'SpecialPageExecuteAfterPage', array( &$this, &$par, &$func ) ) )
419 return;
420 } else {
421 $this->displayRestrictionError();
425 function outputHeader() {
426 global $wgOut, $wgContLang;
428 $msg = $wgContLang->lc( $this->name() ) . '-summary';
429 $out = wfMsg( $msg );
430 if ( ! wfEmptyMsg( $msg, $out ) and $out !== '' and ! $this->including() )
431 $wgOut->addWikiText( $out );
435 # Returns the name that goes in the <h1> in the special page itself, and also the name that
436 # will be listed in Special:Specialpages
438 # Derived classes can override this, but usually it is easier to keep the default behaviour.
439 # Messages can be added at run-time, see MessageCache.php
440 function getDescription() {
441 return wfMsg( strtolower( $this->mName ) );
445 * Get a self-referential title object
447 function getTitle() {
448 return Title::makeTitle( NS_SPECIAL, $this->mName );
452 * Set whether this page is listed in Special:Specialpages, at run-time
454 function setListed( $listed ) {
455 return wfSetVar( $this->mListed, $listed );
461 * Shortcut to construct a special page which is unlisted by default
462 * @package MediaWiki
464 class UnlistedSpecialPage extends SpecialPage
466 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
467 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
472 * Shortcut to construct an includable special page
473 * @package MediaWiki
475 class IncludableSpecialPage extends SpecialPage
477 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
478 SpecialPage::SpecialPage( $name, $restriction, $listed, $function, $file, true );