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 global $wgSpecialPages, $wgUser;
27 $wgSpecialPages = array(
28 'DoubleRedirects' => new SpecialPage ( 'DoubleRedirects' ),
29 'BrokenRedirects' => new SpecialPage ( 'BrokenRedirects' ),
30 'Disambiguations' => new SpecialPage ( 'Disambiguations' ),
32 'Userlogin' => new SpecialPage( 'Userlogin' ),
33 'Userlogout' => new UnlistedSpecialPage( 'Userlogout' ),
34 'Preferences' => new SpecialPage( 'Preferences' ),
35 'Watchlist' => new SpecialPage( 'Watchlist' ),
37 'Recentchanges' => new IncludableSpecialPage( 'Recentchanges' ),
38 'Upload' => new SpecialPage( 'Upload' ),
39 'Imagelist' => new SpecialPage( 'Imagelist' ),
40 'Newimages' => new SpecialPage( 'Newimages' ),
41 'Listusers' => new SpecialPage( 'Listusers' ),
42 'Statistics' => new SpecialPage( 'Statistics' ),
43 'Random' => new SpecialPage( 'Randompage' ),
44 'Lonelypages' => new SpecialPage( 'Lonelypages' ),
45 'Uncategorizedpages'=> new SpecialPage( 'Uncategorizedpages' ),
46 'Uncategorizedcategories'=> new SpecialPage( 'Uncategorizedcategories' ),
47 'Unusedcategories' => new SpecialPage( 'Unusedcategories' ),
48 'Unusedimages' => new SpecialPage( 'Unusedimages' ),
49 'Wantedpages' => new SpecialPage( 'Wantedpages' ),
50 'Shortpages' => new SpecialPage( 'Shortpages' ),
51 'Longpages' => new SpecialPage( 'Longpages' ),
52 'Newpages' => new IncludableSpecialPage( 'Newpages' ),
53 'Ancientpages' => new SpecialPage( 'Ancientpages' ),
54 'Deadendpages' => new SpecialPage( 'Deadendpages' ),
55 'Allpages' => new IncludableSpecialPage( 'Allpages' ),
56 'Prefixindex' => new IncludableSpecialPage( 'Prefixindex' ) ,
57 'Ipblocklist' => new SpecialPage( 'Ipblocklist' ),
58 'Specialpages' => new UnlistedSpecialPage( 'Specialpages' ),
59 'Contributions' => new UnlistedSpecialPage( 'Contributions' ),
60 'Emailuser' => new UnlistedSpecialPage( 'Emailuser' ),
61 'Whatlinkshere' => new UnlistedSpecialPage( 'Whatlinkshere' ),
62 'Recentchangeslinked' => new UnlistedSpecialPage( 'Recentchangeslinked' ),
63 'Movepage' => new UnlistedSpecialPage( 'Movepage' ),
64 'Blockme' => new UnlistedSpecialPage( 'Blockme' ),
65 'Booksources' => new SpecialPage( 'Booksources' ),
66 'Categories' => new SpecialPage( 'Categories' ),
67 'Export' => new SpecialPage( 'Export' ),
68 'Version' => new SpecialPage( 'Version' ),
69 'Allmessages' => new SpecialPage( 'Allmessages' ),
70 'Log' => new SpecialPage( 'Log' ),
71 'Blockip' => new SpecialPage( 'Blockip', 'block' ),
72 'Undelete' => new SpecialPage( 'Undelete' ),
73 "Import" => new SpecialPage( "Import", 'import' ),
74 'Lockdb' => new SpecialPage( 'Lockdb', 'siteadmin' ),
75 'Unlockdb' => new SpecialPage( 'Unlockdb', 'siteadmin' ),
76 'Userrights' => new SpecialPage( 'Userrights', 'userrights' ),
79 global $wgUseValidation ;
80 if ( $wgUseValidation )
81 $wgSpecialPages['Validate'] = new SpecialPage( 'Validate' );
83 global $wgDisableCounters;
84 if( !$wgDisableCounters ) {
85 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
88 global $wgDisableInternalSearch;
89 if( !$wgDisableInternalSearch ) {
90 $wgSpecialPages['Search'] = new UnlistedSpecialPage( 'Search' );
93 global $wgEmailAuthentication;
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 return Title
::makeTitle( NS_USER
, $wgUser->getName() );
189 return Title
::makeTitle( NS_USER_TALK
, $wgUser->getName() );
190 case 'Mycontributions':
191 return Title
::makeTitle( NS_SPECIAL
, 'Contributions/' . $wgUser->getName() );
193 return Title
::makeTitle( NS_SPECIAL
, 'Listusers/sysop' ); # @bug 2832
195 return Title
::makeTitle( NS_SPECIAL
, 'Random' );
202 * Return categorised listable special pages
203 * Returns a 2d array where the first index is the restriction name
206 function getPages() {
207 global $wgSpecialPages;
211 'developer' => array()
214 foreach ( $wgSpecialPages as $name => $page ) {
215 if ( $page->isListed() ) {
216 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
223 * Execute a special page path.
224 * The path may contain parameters, e.g. Special:Name/Params
225 * Extracts the special page name and call the execute method, passing the parameters
227 * Returns a title object if the page is redirected, false if there was no such special
228 * page, and true if it was successful.
230 * @param $title a title object
231 * @param $including output is being captured for use in {{special:whatever}}
233 function executePath( &$title, $including = false ) {
234 global $wgSpecialPages, $wgOut, $wgTitle;
236 $bits = split( "/", $title->getDBkey(), 2 );
238 if( !isset( $bits[1] ) ) { // bug 2087
244 $page =& SpecialPage
::getPage( $name );
245 if ( is_null( $page ) ) {
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() ) {
268 $wgTitle = Title
::makeTitle( NS_SPECIAL
, $name );
272 $page->including( $including );
274 $page->execute( $par );
281 * Just like executePath() except it returns the HTML instead of outputting it
282 * Returns false if there was no such special page, or a title object if it was
286 function capturePath( &$title ) {
287 global $wgOut, $wgTitle;
289 $oldTitle = $wgTitle;
291 $wgOut = new OutputPage
;
293 $ret = SpecialPage
::executePath( $title, true );
294 if ( $ret === true ) {
295 $ret = $wgOut->getHTML();
297 $wgTitle = $oldTitle;
303 * Default constructor for special pages
304 * Derivative classes should call this from their constructor
305 * Note that if the user does not have the required level, an error message will
306 * be displayed by the default execute() method, without the global function ever
309 * If you override execute(), you can recover the default behaviour with userCanExecute()
310 * and displayRestrictionError()
312 * @param string $name Name of the special page, as seen in links and URLs
313 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
314 * @param boolean $listed Whether the page is listed in Special:Specialpages
315 * @param string $function Function called by execute(). By default it is constructed from $name
316 * @param string $file File which is included by execute(). It is also constructed from $name by default
318 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
319 $this->mName
= $name;
320 $this->mRestriction
= $restriction;
321 $this->mListed
= $listed;
322 $this->mIncludable
= $includable;
323 if ( $function == false ) {
324 $this->mFunction
= 'wfSpecial'.$name;
326 $this->mFunction
= $function;
328 if ( $file === 'default' ) {
329 $this->mFile
= "Special{$name}.php";
331 $this->mFile
= $file;
335 # Accessor functions, see the descriptions of the associated variables above
336 function getName() { return $this->mName
; }
337 function getRestriction() { return $this->mRestriction
; }
338 function isListed() { return $this->mListed
; }
339 function getFile() { return $this->mFile
; }
340 function including( $x = NULL ) { return wfSetVar( $this->mIncluding
, $x ); }
341 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable
, $x ); }
344 * Checks if the given user (identified by an object) can execute this
345 * special page (as defined by $mRestriction)
347 function userCanExecute( &$user ) {
348 if ( $this->mRestriction
== "" ) {
351 if ( in_array( $this->mRestriction
, $user->getRights() ) ) {
360 * Output an error message telling the user what access level they have to have
362 function displayRestrictionError() {
364 $wgOut->permissionRequired( $this->mRestriction
);
368 * Sets headers - this should be called from the execute() method of all derived classes!
370 function setHeaders() {
372 $wgOut->setArticleRelated( false );
373 $wgOut->setRobotPolicy( "noindex,follow" );
374 $wgOut->setPageTitle( $this->getDescription() );
378 * Default execute method
379 * Checks user permissions, calls the function given in mFunction
381 function execute( $par ) {
382 global $wgUser, $wgOut, $wgTitle;
386 if ( $this->userCanExecute( $wgUser ) ) {
387 if ( $this->mFile
) {
388 require_once( $this->mFile
);
390 $func = $this->mFunction
;
391 $func( $par, $this );
393 $this->displayRestrictionError();
397 # Returns the name that goes in the <h1> in the special page itself, and also the name that
398 # will be listed in Special:Specialpages
400 # Derived classes can override this, but usually it is easier to keep the default behaviour.
401 # Messages can be added at run-time, see MessageCache.php
402 function getDescription() {
403 return wfMsg( strtolower( $this->mName
) );
407 * Get a self-referential title object
409 function getTitle() {
410 return Title
::makeTitle( NS_SPECIAL
, $this->mName
);
414 * Set whether this page is listed in Special:Specialpages, at run-time
416 function setListed( $listed ) {
417 return wfSetVar( $this->mListed
, $listed );
423 * Shortcut to construct a special page which is unlisted by default
426 class UnlistedSpecialPage
extends SpecialPage
428 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
429 SpecialPage
::SpecialPage( $name, $restriction, false, $function, $file );
434 * Shortcut to construct an includable special page
437 class IncludableSpecialPage
extends SpecialPage
439 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
440 SpecialPage
::SpecialPage( $name, $restriction, $listed, $function, $file, true );