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 SpecialPage( 'Wantedpages' ),
45 'Mostlinked' => new SpecialPage( 'Mostlinked' ),
46 'Shortpages' => new SpecialPage( 'Shortpages' ),
47 'Longpages' => new SpecialPage( 'Longpages' ),
48 'Newpages' => new IncludableSpecialPage( 'Newpages' ),
49 'Ancientpages' => new SpecialPage( 'Ancientpages' ),
50 'Deadendpages' => new SpecialPage( 'Deadendpages' ),
51 'Allpages' => new IncludableSpecialPage( 'Allpages' ),
52 'Prefixindex' => new IncludableSpecialPage( 'Prefixindex' ) ,
53 'Ipblocklist' => new SpecialPage( 'Ipblocklist' ),
54 'Specialpages' => new UnlistedSpecialPage( 'Specialpages' ),
55 'Contributions' => new UnlistedSpecialPage( 'Contributions' ),
56 'Emailuser' => new UnlistedSpecialPage( 'Emailuser' ),
57 'Whatlinkshere' => new UnlistedSpecialPage( 'Whatlinkshere' ),
58 'Recentchangeslinked' => new UnlistedSpecialPage( 'Recentchangeslinked' ),
59 'Movepage' => new UnlistedSpecialPage( 'Movepage' ),
60 'Blockme' => new UnlistedSpecialPage( 'Blockme' ),
61 'Booksources' => new SpecialPage( 'Booksources' ),
62 'Categories' => new SpecialPage( 'Categories' ),
63 'Export' => new SpecialPage( 'Export' ),
64 'Version' => new SpecialPage( 'Version' ),
65 'Allmessages' => new SpecialPage( 'Allmessages' ),
66 'Log' => new SpecialPage( 'Log' ),
67 'Blockip' => new SpecialPage( 'Blockip', 'block' ),
68 'Undelete' => new SpecialPage( 'Undelete' ),
69 "Import" => new SpecialPage( "Import", 'import' ),
70 'Lockdb' => new SpecialPage( 'Lockdb', 'siteadmin' ),
71 'Unlockdb' => new SpecialPage( 'Unlockdb', 'siteadmin' ),
72 'Userrights' => new SpecialPage( 'Userrights', 'userrights' ),
73 'MIMEsearch' => new SpecialPage( 'MIMEsearch' ),
74 'Unwatchedpages' => new SpecialPage( 'Unwatchedpages' )
77 if ( $wgUseValidation )
78 $wgSpecialPages['Validate'] = new SpecialPage( 'Validate' );
80 if( !$wgDisableCounters ) {
81 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
84 if( !$wgDisableInternalSearch ) {
85 $wgSpecialPages['Search'] = new SpecialPage( 'Search' );
88 if( $wgEmailAuthentication ) {
89 $wgSpecialPages['Confirmemail'] = new UnlistedSpecialPage( 'Confirmemail' );
92 if ( $wgEnableUnwatchedpages )
93 $wgSpecialPages['Unwatchedpages'] = new SpecialPage( 'Unwatchedpages' );
96 * Parent special page class, also static functions for handling the special
106 * The name of the class, used in the URL.
107 * Also used for the default <h1> heading, @see getDescription()
111 * Minimum user level required to access this page, or "" for anyone.
112 * Also used to categorise the pages in Special:Specialpages
116 * Listed in Special:Specialpages?
120 * Function name called by the default execute()
124 * File which needs to be included before the function above can be called
128 * Whether or not this special page is being included from an article
132 * Whether the special page can be included in an article
141 * Add a page to the list of valid special pages
142 * $obj->execute() must send HTML to $wgOut then return
143 * Use this for a special page extension
146 function addPage( &$obj ) {
147 global $wgSpecialPages;
148 $wgSpecialPages[$obj->mName
] = $obj;
152 * Remove a special page from the list
153 * Occasionally used to disable expensive or dangerous special pages
156 function removePage( $name ) {
157 global $wgSpecialPages;
158 unset( $wgSpecialPages[$name] );
162 * Find the object with a given name and return it (or NULL)
164 * @param string $name
166 function getPage( $name ) {
167 global $wgSpecialPages;
168 if ( array_key_exists( $name, $wgSpecialPages ) ) {
169 return $wgSpecialPages[$name];
177 * @param string $name
178 * @return mixed Title object if the redirect exists, otherwise NULL
180 function getRedirect( $name ) {
184 return Title
::makeTitle( NS_USER
, $wgUser->getName() );
186 return Title
::makeTitle( NS_USER_TALK
, $wgUser->getName() );
187 case 'Mycontributions':
188 return Title
::makeTitle( NS_SPECIAL
, 'Contributions/' . $wgUser->getName() );
190 return Title
::makeTitle( NS_SPECIAL
, 'Listusers/sysop' ); # @bug 2832
192 return Title
::makeTitle( NS_SPECIAL
, 'Random' );
199 * Return categorised listable special pages
200 * Returns a 2d array where the first index is the restriction name
203 function getPages() {
204 global $wgSpecialPages;
208 'developer' => array()
211 foreach ( $wgSpecialPages as $name => $page ) {
212 if ( $page->isListed() ) {
213 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
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 $wgSpecialPages, $wgOut, $wgTitle;
232 $fname = 'SpecialPage::executePath';
233 wfProfileIn( $fname );
235 $bits = split( "/", $title->getDBkey(), 2 );
237 if( !isset( $bits[1] ) ) { // bug 2087
243 $page = SpecialPage
::getPage( $name );
244 if ( is_null( $page ) ) {
246 wfProfileOut( $fname );
249 $redir = SpecialPage
::getRedirect( $name );
250 if ( isset( $redir ) ) {
252 $wgOut->redirect( $redir->getFullURL() . '/' . $par );
254 $wgOut->redirect( $redir->getFullURL() );
257 $wgOut->setArticleRelated( false );
258 $wgOut->setRobotpolicy( "noindex,follow" );
259 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
264 if ( $including && !$page->includable() ) {
265 wfProfileOut( $fname );
269 $wgTitle = Title
::makeTitle( NS_SPECIAL
, $name );
273 $page->including( $including );
275 $profName = 'Special:' . $page->getName();
276 wfProfileIn( $profName );
277 $page->execute( $par );
278 wfProfileOut( $profName );
281 wfProfileOut( $fname );
286 * Just like executePath() except it returns the HTML instead of outputting it
287 * Returns false if there was no such special page, or a title object if it was
291 function capturePath( &$title ) {
292 global $wgOut, $wgTitle;
294 $oldTitle = $wgTitle;
296 $wgOut = new OutputPage
;
298 $ret = SpecialPage
::executePath( $title, true );
299 if ( $ret === true ) {
300 $ret = $wgOut->getHTML();
302 $wgTitle = $oldTitle;
308 * Default constructor for special pages
309 * Derivative classes should call this from their constructor
310 * Note that if the user does not have the required level, an error message will
311 * be displayed by the default execute() method, without the global function ever
314 * If you override execute(), you can recover the default behaviour with userCanExecute()
315 * and displayRestrictionError()
317 * @param string $name Name of the special page, as seen in links and URLs
318 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
319 * @param boolean $listed Whether the page is listed in Special:Specialpages
320 * @param string $function Function called by execute(). By default it is constructed from $name
321 * @param string $file File which is included by execute(). It is also constructed from $name by default
323 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
324 $this->mName
= $name;
325 $this->mRestriction
= $restriction;
326 $this->mListed
= $listed;
327 $this->mIncludable
= $includable;
328 if ( $function == false ) {
329 $this->mFunction
= 'wfSpecial'.$name;
331 $this->mFunction
= $function;
333 if ( $file === 'default' ) {
334 $this->mFile
= "Special{$name}.php";
336 $this->mFile
= $file;
340 # Accessor functions, see the descriptions of the associated variables above
341 function getName() { return $this->mName
; }
342 function getRestriction() { return $this->mRestriction
; }
343 function isListed() { return $this->mListed
; }
344 function getFile() { return $this->mFile
; }
345 function including( $x = NULL ) { return wfSetVar( $this->mIncluding
, $x ); }
346 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable
, $x ); }
349 * Checks if the given user (identified by an object) can execute this
350 * special page (as defined by $mRestriction)
352 function userCanExecute( &$user ) {
353 if ( $this->mRestriction
== "" ) {
356 if ( in_array( $this->mRestriction
, $user->getRights() ) ) {
365 * Output an error message telling the user what access level they have to have
367 function displayRestrictionError() {
369 $wgOut->permissionRequired( $this->mRestriction
);
373 * Sets headers - this should be called from the execute() method of all derived classes!
375 function setHeaders() {
377 $wgOut->setArticleRelated( false );
378 $wgOut->setRobotPolicy( "noindex,follow" );
379 $wgOut->setPageTitle( $this->getDescription() );
383 * Default execute method
384 * Checks user permissions, calls the function given in mFunction
386 function execute( $par ) {
387 global $wgUser, $wgOut, $wgTitle;
391 if ( $this->userCanExecute( $wgUser ) ) {
392 $func = $this->mFunction
;
393 // only load file if the function does not exist
394 if(!function_exists($func) and $this->mFile
) {
395 require_once( $this->mFile
);
397 $func( $par, $this );
399 $this->displayRestrictionError();
403 # Returns the name that goes in the <h1> in the special page itself, and also the name that
404 # will be listed in Special:Specialpages
406 # Derived classes can override this, but usually it is easier to keep the default behaviour.
407 # Messages can be added at run-time, see MessageCache.php
408 function getDescription() {
409 return wfMsg( strtolower( $this->mName
) );
413 * Get a self-referential title object
415 function getTitle() {
416 return Title
::makeTitle( NS_SPECIAL
, $this->mName
);
420 * Set whether this page is listed in Special:Specialpages, at run-time
422 function setListed( $listed ) {
423 return wfSetVar( $this->mListed
, $listed );
429 * Shortcut to construct a special page which is unlisted by default
432 class UnlistedSpecialPage
extends SpecialPage
434 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
435 SpecialPage
::SpecialPage( $name, $restriction, false, $function, $file );
440 * Shortcut to construct an includable special page
443 class IncludableSpecialPage
extends SpecialPage
445 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
446 SpecialPage
::SpecialPage( $name, $restriction, $listed, $function, $file, true );