6c0f2e4aadcfcd000aafa6328d98d007da07f520
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
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' );
99 * Parent special page class, also static functions for handling the special
109 * The name of the class, used in the URL.
110 * Also used for the default <h1> heading, @see getDescription()
114 * Minimum user level required to access this page, or "" for anyone.
115 * Also used to categorise the pages in Special:Specialpages
119 * Listed in Special:Specialpages?
123 * Function name called by the default execute()
127 * File which needs to be included before the function above can be called
131 * Whether or not this special page is being included from an article
135 * Whether the special page can be included in an article
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
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
159 function removePage( $name ) {
160 global $wgSpecialPages;
161 unset( $wgSpecialPages[$name] );
165 * Find the object with a given name and return it (or NULL)
167 * @param string $name
169 function getPage( $name ) {
170 global $wgSpecialPages;
171 if ( array_key_exists( $name, $wgSpecialPages ) ) {
172 return $wgSpecialPages[$name];
180 * @param string $name
181 * @return mixed Title object if the redirect exists, otherwise NULL
183 function getRedirect( $name ) {
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' ),
192 'Userlist' => Title
::makeTitle( NS_SPECIAL
, 'Listusers' )
194 wfRunHooks( 'SpecialPageGetRedirect', array( &$redirects ) );
196 return isset( $redirects[$name] ) ?
$redirects[$name] : null;
200 * Return part of the request string for a special redirect page
201 * This allows passing, e.g. action=history to Special:Mypage, etc.
203 * @param $name Name of the redirect page
206 function getRedirectParams( $name ) {
214 $args = array( 'action' );
218 foreach( $args as $arg ) {
219 if( $val = $wgRequest->getVal( $arg, false ) )
220 $params[] = $arg . '=' . $val;
223 return count( $params ) ?
implode( '&', $params ) : false;
227 * Return categorised listable special pages
228 * Returns a 2d array where the first index is the restriction name
231 function getPages() {
232 global $wgSpecialPages;
236 'developer' => array()
239 foreach ( $wgSpecialPages as $name => $page ) {
240 if ( $page->isListed() ) {
241 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
248 * Execute a special page path.
249 * The path may contain parameters, e.g. Special:Name/Params
250 * Extracts the special page name and call the execute method, passing the parameters
252 * Returns a title object if the page is redirected, false if there was no such special
253 * page, and true if it was successful.
255 * @param $title a title object
256 * @param $including output is being captured for use in {{special:whatever}}
258 function executePath( &$title, $including = false ) {
259 global $wgOut, $wgTitle;
260 $fname = 'SpecialPage::executePath';
261 wfProfileIn( $fname );
263 $bits = split( "/", $title->getDBkey(), 2 );
265 if( !isset( $bits[1] ) ) { // bug 2087
271 $page = SpecialPage
::getPage( $name );
272 if ( is_null( $page ) ) {
274 wfProfileOut( $fname );
277 $redir = SpecialPage
::getRedirect( $name );
278 if ( isset( $redir ) ) {
279 $params = SpecialPage
::getRedirectParams( $name );
281 $url = $redir->getFullUrl( $params );
283 $url = $redir->getFullUrl() . '/' . $par;
285 $url = $redir->getFullUrl();
287 $wgOut->redirect( $url );
290 $wgOut->setArticleRelated( false );
291 $wgOut->setRobotpolicy( 'noindex,follow' );
292 $wgOut->setStatusCode( 404 );
293 $wgOut->errorpage( 'nosuchspecialpage', 'nospecialpagetext' );
298 if ( $including && !$page->includable() ) {
299 wfProfileOut( $fname );
303 $wgTitle = Title
::makeTitle( NS_SPECIAL
, $name );
307 $page->including( $including );
309 $profName = 'Special:' . $page->getName();
310 wfProfileIn( $profName );
311 $page->execute( $par );
312 wfProfileOut( $profName );
315 wfProfileOut( $fname );
320 * Just like executePath() except it returns the HTML instead of outputting it
321 * Returns false if there was no such special page, or a title object if it was
325 function capturePath( &$title ) {
326 global $wgOut, $wgTitle;
328 $oldTitle = $wgTitle;
330 $wgOut = new OutputPage
;
332 $ret = SpecialPage
::executePath( $title, true );
333 if ( $ret === true ) {
334 $ret = $wgOut->getHTML();
336 $wgTitle = $oldTitle;
342 * Default constructor for special pages
343 * Derivative classes should call this from their constructor
344 * Note that if the user does not have the required level, an error message will
345 * be displayed by the default execute() method, without the global function ever
348 * If you override execute(), you can recover the default behaviour with userCanExecute()
349 * and displayRestrictionError()
351 * @param string $name Name of the special page, as seen in links and URLs
352 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
353 * @param boolean $listed Whether the page is listed in Special:Specialpages
354 * @param string $function Function called by execute(). By default it is constructed from $name
355 * @param string $file File which is included by execute(). It is also constructed from $name by default
357 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
358 $this->mName
= $name;
359 $this->mRestriction
= $restriction;
360 $this->mListed
= $listed;
361 $this->mIncludable
= $includable;
362 if ( $function == false ) {
363 $this->mFunction
= 'wfSpecial'.$name;
365 $this->mFunction
= $function;
367 if ( $file === 'default' ) {
368 $this->mFile
= "Special{$name}.php";
370 $this->mFile
= $file;
379 function getName() { return $this->mName
; }
380 function getRestriction() { return $this->mRestriction
; }
381 function getFile() { return $this->mFile
; }
382 function isListed() { return $this->mListed
; }
386 * Accessor and mutator
388 function name( $x = NULL ) { return wfSetVar( $this->mName
, $x ); }
389 function restrictions( $x = NULL) { return wfSetVar( $this->mRestrictions
, $x ); }
390 function listed( $x = NULL) { return wfSetVar( $this->mListed
, $x ); }
391 function func( $x = NULL) { return wfSetVar( $this->mFunction
, $x ); }
392 function file( $x = NULL) { return wfSetVar( $this->mFile
, $x ); }
393 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable
, $x ); }
394 function including( $x = NULL ) { return wfSetVar( $this->mIncluding
, $x ); }
398 * Checks if the given user (identified by an object) can execute this
399 * special page (as defined by $mRestriction)
401 function userCanExecute( &$user ) {
402 if ( $this->mRestriction
== "" ) {
405 if ( in_array( $this->mRestriction
, $user->getRights() ) ) {
414 * Output an error message telling the user what access level they have to have
416 function displayRestrictionError() {
418 $wgOut->permissionRequired( $this->mRestriction
);
422 * Sets headers - this should be called from the execute() method of all derived classes!
424 function setHeaders() {
426 $wgOut->setArticleRelated( false );
427 $wgOut->setRobotPolicy( "noindex,follow" );
428 $wgOut->setPageTitle( $this->getDescription() );
432 * Default execute method
433 * Checks user permissions, calls the function given in mFunction
435 function execute( $par ) {
440 if ( $this->userCanExecute( $wgUser ) ) {
441 $func = $this->mFunction
;
442 // only load file if the function does not exist
443 if(!function_exists($func) and $this->mFile
) {
444 require_once( $this->mFile
);
446 if ( wfRunHooks( 'SpecialPageExecuteBeforeHeader', array( &$this, &$par, &$func ) ) )
447 $this->outputHeader();
448 if ( ! wfRunHooks( 'SpecialPageExecuteBeforePage', array( &$this, &$par, &$func ) ) )
450 $func( $par, $this );
451 if ( ! wfRunHooks( 'SpecialPageExecuteAfterPage', array( &$this, &$par, &$func ) ) )
454 $this->displayRestrictionError();
458 function outputHeader() {
459 global $wgOut, $wgContLang;
461 $msg = $wgContLang->lc( $this->name() ) . '-summary';
462 $out = wfMsg( $msg );
463 if ( ! wfEmptyMsg( $msg, $out ) and $out !== '' and ! $this->including() )
464 $wgOut->addWikiText( $out );
468 # Returns the name that goes in the <h1> in the special page itself, and also the name that
469 # will be listed in Special:Specialpages
471 # Derived classes can override this, but usually it is easier to keep the default behaviour.
472 # Messages can be added at run-time, see MessageCache.php
473 function getDescription() {
474 return wfMsg( strtolower( $this->mName
) );
478 * Get a self-referential title object
480 function getTitle() {
481 return Title
::makeTitle( NS_SPECIAL
, $this->mName
);
485 * Set whether this page is listed in Special:Specialpages, at run-time
487 function setListed( $listed ) {
488 return wfSetVar( $this->mListed
, $listed );
494 * Shortcut to construct a special page which is unlisted by default
497 class UnlistedSpecialPage
extends SpecialPage
499 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
500 SpecialPage
::SpecialPage( $name, $restriction, false, $function, $file );
505 * Shortcut to construct an includable special page
508 class IncludableSpecialPage
extends SpecialPage
510 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
511 SpecialPage
::SpecialPage( $name, $restriction, $listed, $function, $file, true );