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 'Licensesearch' => new SpecialPage( 'Licensesearch' ),
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' );
93 * Parent special page class, also static functions for handling the special
103 * The name of the class, used in the URL.
104 * Also used for the default <h1> heading, @see getDescription()
108 * Minimum user level required to access this page, or "" for anyone.
109 * Also used to categorise the pages in Special:Specialpages
113 * Listed in Special:Specialpages?
117 * Function name called by the default execute()
121 * File which needs to be included before the function above can be called
125 * Whether or not this special page is being included from an article
129 * Whether the special page can be included in an article
138 * Add a page to the list of valid special pages
139 * $obj->execute() must send HTML to $wgOut then return
140 * Use this for a special page extension
143 function addPage( &$obj ) {
144 global $wgSpecialPages;
145 $wgSpecialPages[$obj->mName
] = $obj;
149 * Remove a special page from the list
150 * Occasionally used to disable expensive or dangerous special pages
153 function removePage( $name ) {
154 global $wgSpecialPages;
155 unset( $wgSpecialPages[$name] );
159 * Find the object with a given name and return it (or NULL)
161 * @param string $name
163 function getPage( $name ) {
164 global $wgSpecialPages;
165 if ( array_key_exists( $name, $wgSpecialPages ) ) {
166 return $wgSpecialPages[$name];
174 * @param string $name
175 * @return mixed Title object if the redirect exists, otherwise NULL
177 function getRedirect( $name ) {
181 return Title
::makeTitle( NS_USER
, $wgUser->getName() );
183 return Title
::makeTitle( NS_USER_TALK
, $wgUser->getName() );
184 case 'Mycontributions':
185 return Title
::makeTitle( NS_SPECIAL
, 'Contributions/' . $wgUser->getName() );
187 return Title
::makeTitle( NS_SPECIAL
, 'Listusers/sysop' ); # @bug 2832
189 return Title
::makeTitle( NS_SPECIAL
, 'Random' );
196 * Return categorised listable special pages
197 * Returns a 2d array where the first index is the restriction name
200 function getPages() {
201 global $wgSpecialPages;
205 'developer' => array()
208 foreach ( $wgSpecialPages as $name => $page ) {
209 if ( $page->isListed() ) {
210 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
217 * Execute a special page path.
218 * The path may contain parameters, e.g. Special:Name/Params
219 * Extracts the special page name and call the execute method, passing the parameters
221 * Returns a title object if the page is redirected, false if there was no such special
222 * page, and true if it was successful.
224 * @param $title a title object
225 * @param $including output is being captured for use in {{special:whatever}}
227 function executePath( &$title, $including = false ) {
228 global $wgSpecialPages, $wgOut, $wgTitle;
229 $fname = 'SpecialPage::executePath';
230 wfProfileIn( $fname );
232 $bits = split( "/", $title->getDBkey(), 2 );
234 if( !isset( $bits[1] ) ) { // bug 2087
240 $page = SpecialPage
::getPage( $name );
241 if ( is_null( $page ) ) {
243 wfProfileOut( $fname );
246 $redir = SpecialPage
::getRedirect( $name );
247 if ( isset( $redir ) ) {
249 $wgOut->redirect( $redir->getFullURL() . '/' . $par );
251 $wgOut->redirect( $redir->getFullURL() );
254 $wgOut->setArticleRelated( false );
255 $wgOut->setRobotpolicy( "noindex,follow" );
256 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
261 if ( $including && !$page->includable() ) {
262 wfProfileOut( $fname );
266 $wgTitle = Title
::makeTitle( NS_SPECIAL
, $name );
270 $page->including( $including );
272 $profName = 'Special:' . $page->getName();
273 wfProfileIn( $profName );
274 $page->execute( $par );
275 wfProfileOut( $profName );
278 wfProfileOut( $fname );
283 * Just like executePath() except it returns the HTML instead of outputting it
284 * Returns false if there was no such special page, or a title object if it was
288 function capturePath( &$title ) {
289 global $wgOut, $wgTitle;
291 $oldTitle = $wgTitle;
293 $wgOut = new OutputPage
;
295 $ret = SpecialPage
::executePath( $title, true );
296 if ( $ret === true ) {
297 $ret = $wgOut->getHTML();
299 $wgTitle = $oldTitle;
305 * Default constructor for special pages
306 * Derivative classes should call this from their constructor
307 * Note that if the user does not have the required level, an error message will
308 * be displayed by the default execute() method, without the global function ever
311 * If you override execute(), you can recover the default behaviour with userCanExecute()
312 * and displayRestrictionError()
314 * @param string $name Name of the special page, as seen in links and URLs
315 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
316 * @param boolean $listed Whether the page is listed in Special:Specialpages
317 * @param string $function Function called by execute(). By default it is constructed from $name
318 * @param string $file File which is included by execute(). It is also constructed from $name by default
320 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
321 $this->mName
= $name;
322 $this->mRestriction
= $restriction;
323 $this->mListed
= $listed;
324 $this->mIncludable
= $includable;
325 if ( $function == false ) {
326 $this->mFunction
= 'wfSpecial'.$name;
328 $this->mFunction
= $function;
330 if ( $file === 'default' ) {
331 $this->mFile
= "Special{$name}.php";
333 $this->mFile
= $file;
337 # Accessor functions, see the descriptions of the associated variables above
338 function getName() { return $this->mName
; }
339 function getRestriction() { return $this->mRestriction
; }
340 function isListed() { return $this->mListed
; }
341 function getFile() { return $this->mFile
; }
342 function including( $x = NULL ) { return wfSetVar( $this->mIncluding
, $x ); }
343 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable
, $x ); }
346 * Checks if the given user (identified by an object) can execute this
347 * special page (as defined by $mRestriction)
349 function userCanExecute( &$user ) {
350 if ( $this->mRestriction
== "" ) {
353 if ( in_array( $this->mRestriction
, $user->getRights() ) ) {
362 * Output an error message telling the user what access level they have to have
364 function displayRestrictionError() {
366 $wgOut->permissionRequired( $this->mRestriction
);
370 * Sets headers - this should be called from the execute() method of all derived classes!
372 function setHeaders() {
374 $wgOut->setArticleRelated( false );
375 $wgOut->setRobotPolicy( "noindex,follow" );
376 $wgOut->setPageTitle( $this->getDescription() );
380 * Default execute method
381 * Checks user permissions, calls the function given in mFunction
383 function execute( $par ) {
384 global $wgUser, $wgOut, $wgTitle;
388 if ( $this->userCanExecute( $wgUser ) ) {
389 $func = $this->mFunction
;
390 // only load file if the function does not exist
391 if(!function_exists($func) and $this->mFile
) {
392 require_once( $this->mFile
);
394 $func( $par, $this );
396 $this->displayRestrictionError();
400 # Returns the name that goes in the <h1> in the special page itself, and also the name that
401 # will be listed in Special:Specialpages
403 # Derived classes can override this, but usually it is easier to keep the default behaviour.
404 # Messages can be added at run-time, see MessageCache.php
405 function getDescription() {
406 return wfMsg( strtolower( $this->mName
) );
410 * Get a self-referential title object
412 function getTitle() {
413 return Title
::makeTitle( NS_SPECIAL
, $this->mName
);
417 * Set whether this page is listed in Special:Specialpages, at run-time
419 function setListed( $listed ) {
420 return wfSetVar( $this->mListed
, $listed );
426 * Shortcut to construct a special page which is unlisted by default
429 class UnlistedSpecialPage
extends SpecialPage
431 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
432 SpecialPage
::SpecialPage( $name, $restriction, false, $function, $file );
437 * Shortcut to construct an includable special page
440 class IncludableSpecialPage
extends SpecialPage
442 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
443 SpecialPage
::SpecialPage( $name, $restriction, $listed, $function, $file, true );