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.
20 global $wgSpecialPages;
25 $wgSpecialPages = array(
26 'DoubleRedirects' => new UnlistedSpecialPage ( 'DoubleRedirects' ),
27 'BrokenRedirects' => new UnlistedSpecialPage ( 'BrokenRedirects' ),
28 'Disambiguations' => new UnlistedSpecialPage ( 'Disambiguations' ),
30 'Userlogin' => new SpecialPage( 'Userlogin' ),
31 'Userlogout' => new UnlistedSpecialPage( 'Userlogout' ),
32 'Preferences' => new SpecialPage( 'Preferences' ),
33 'Watchlist' => new SpecialPage( 'Watchlist' ),
34 'Recentchanges' => new SpecialPage( 'Recentchanges' ),
35 'Upload' => new SpecialPage( 'Upload' ),
36 'Imagelist' => new SpecialPage( 'Imagelist' ),
37 'Newimages' => new SpecialPage( 'Newimages' ),
38 'Listusers' => new SpecialPage( 'Listusers' ),
39 'Listadmins' => new SpecialPage( 'Listadmins' ),
40 'Statistics' => new SpecialPage( 'Statistics' ),
41 'Randompage' => new SpecialPage( 'Randompage' ),
42 'Lonelypages' => new SpecialPage( 'Lonelypages' ),
43 'Uncategorizedpages'=> new SpecialPage( 'Uncategorizedpages' ),
44 'Uncategorizedcategories'=> new SpecialPage( 'Uncategorizedcategories' ),
45 'Unusedimages' => new SpecialPage( 'Unusedimages' )
48 global $wgDisableCounters;
49 if( !$wgDisableCounters ) {
50 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
55 $wgSpecialPages['Data'] = new SpecialPage( 'Data' );
58 $wgSpecialPages = array_merge($wgSpecialPages, array (
59 'Wantedpages' => new SpecialPage( 'Wantedpages' ),
60 'Shortpages' => new SpecialPage( 'Shortpages' ),
61 'Longpages' => new SpecialPage( 'Longpages' ),
62 'Newpages' => new SpecialPage( 'Newpages' ),
63 'Ancientpages' => new SpecialPage( 'Ancientpages' ),
64 'Deadendpages' => new SpecialPage( 'Deadendpages' ),
65 'Allpages' => new SpecialPage( 'Allpages' ),
66 'Ipblocklist' => new SpecialPage( 'Ipblocklist' ),
67 'Maintenance' => new SpecialPage( 'Maintenance' ),
68 'Specialpages' => new UnlistedSpecialPage( 'Specialpages' ),
69 'Contributions' => new UnlistedSpecialPage( 'Contributions' ),
70 'Emailuser' => new UnlistedSpecialPage( 'Emailuser' ),
71 'Whatlinkshere' => new UnlistedSpecialPage( 'Whatlinkshere' ),
72 'Recentchangeslinked' => new UnlistedSpecialPage( 'Recentchangeslinked' ),
73 'Movepage' => new UnlistedSpecialPage( 'Movepage' ),
74 'Blockme' => new UnlistedSpecialPage( 'Blockme' ),
75 'Geo' => new UnlistedSpecialPage( 'Geo' ),
76 'Validate' => new UnlistedSpecialPage( 'Validate' ),
77 'Booksources' => new SpecialPage( 'Booksources' ),
78 'Categories' => new SpecialPage( 'Categories' ),
79 'Export' => new SpecialPage( 'Export' ),
80 'Version' => new SpecialPage( 'Version' ),
81 'Allmessages' => new SpecialPage( 'Allmessages' ),
82 'Search' => new UnlistedSpecialPage( 'Search' ),
83 'Log' => new SpecialPage( 'Log' ),
84 'Blockip' => new SpecialPage( 'Blockip', 'block' ),
85 'Asksql' => new SpecialPage( 'Asksql', 'asksql' ),
86 'Undelete' => new SpecialPage( 'Undelete', 'delete' ),
87 // Makesysop is obsolete, replaced by Special:Userlevels [av]
88 # 'Makesysop' => new SpecialPage( 'Makesysop', 'userrights' ),
90 # Special:Import is half-written
91 # "Import" => new SpecialPage( "Import", "sysop" ),
92 'Lockdb' => new SpecialPage( 'Lockdb', 'siteadmin' ),
93 'Unlockdb' => new SpecialPage( 'Unlockdb', 'siteadmin' ),
94 'Sitesettings' => new SpecialPage( 'Sitesettings', 'siteadmin' ),
95 'Userlevels' => new SpecialPage( 'Userlevels', 'userrights' ),
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
133 * Add a page to the list of valid special pages
134 * $obj->execute() must send HTML to $wgOut then return
135 * Use this for a special page extension
138 function addPage( &$obj ) {
139 global $wgSpecialPages;
140 $wgSpecialPages[$obj->mName
] = $obj;
144 * Remove a special page from the list
145 * Occasionally used to disable expensive or dangerous special pages
148 function removePage( $name ) {
149 global $wgSpecialPages;
150 unset( $wgSpecialPages[$name] );
154 * Find the object with a given name and return it (or NULL)
156 * @param string $name
158 function &getPage( $name ) {
159 global $wgSpecialPages;
160 if ( array_key_exists( $name, $wgSpecialPages ) ) {
161 return $wgSpecialPages[$name];
168 * Return categorised listable special pages
169 * Returns a 2d array where the first index is the restriction name
172 function getPages() {
173 global $wgSpecialPages;
177 'developer' => array()
180 foreach ( $wgSpecialPages as $name => $page ) {
181 if ( $page->isListed() ) {
182 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
189 * Execute a special page path.
190 * The path may contain parameters, e.g. Special:Name/Params
191 * Extracts the special page name and call the execute method, passing the parameters
193 * @param $title should be a title object
195 function executePath( &$title ) {
196 global $wgSpecialPages, $wgOut, $wgTitle;
198 $bits = split( "/", $title->getDBkey(), 2 );
200 if( empty( $bits[1] ) ) {
206 $page =& SpecialPage
::getPage( $name );
207 if ( is_null( $page ) ) {
208 $wgOut->setArticleRelated( false );
209 $wgOut->setRobotpolicy( "noindex,follow" );
210 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
213 $wgTitle = Title
::makeTitle( NS_SPECIAL
, $name );
218 $page->execute( $par );
223 * Default constructor for special pages
224 * Derivative classes should call this from their constructor
225 * Note that if the user does not have the required level, an error message will
226 * be displayed by the default execute() method, without the global function ever
229 * If you override execute(), you can recover the default behaviour with userCanExecute()
230 * and displayRestrictionError()
232 * @param string $name Name of the special page, as seen in links and URLs
233 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
234 * @param boolean $listed Whether the page is listed in Special:Specialpages
235 * @param string $function Function called by execute(). By default it is constructed from $name
236 * @param string $file File which is included by execute(). It is also constructed from $name by default
238 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default' ) {
239 $this->mName
= $name;
240 $this->mRestriction
= $restriction;
241 $this->mListed
= $listed;
242 if ( $function == false ) {
243 $this->mFunction
= 'wfSpecial'.$name;
245 $this->mFunction
= $function;
247 if ( $file === 'default' ) {
248 $this->mFile
= "Special{$name}.php";
250 $this->mFile
= $file;
254 # Accessor functions, see the descriptions of the associated variables above
255 function getName() { return $this->mName
; }
256 function getRestriction() { return $this->mRestriction
; }
257 function isListed() { return $this->mListed
; }
260 * Checks if the given user (identified by an object) can execute this
261 * special page (as defined by $mRestriction)
263 function userCanExecute( &$user ) {
264 if ( $this->mRestriction
== "" ) {
267 if ( in_array( $this->mRestriction
, $user->getRights() ) ) {
276 * Output an error message telling the user what access level they have to have
278 function displayRestrictionError() {
280 if ( $this->mRestriction
== "developer" ) {
281 $wgOut->developerRequired();
283 $wgOut->sysopRequired();
288 * Sets headers - this should be called from the execute() method of all derived classes!
290 function setHeaders() {
292 $wgOut->setArticleRelated( false );
293 $wgOut->setRobotPolicy( "noindex,follow" );
294 $wgOut->setPageTitle( $this->getDescription() );
298 * Default execute method
299 * Checks user permissions, calls the function given in mFunction
301 function execute( $par ) {
302 global $wgUser, $wgOut, $wgTitle;
306 if ( $this->userCanExecute( $wgUser ) ) {
307 if ( $this->mFile
) {
308 require_once( $this->mFile
);
310 $func = $this->mFunction
;
313 $this->displayRestrictionError();
317 # Returns the name that goes in the <h1> in the special page itself, and also the name that
318 # will be listed in Special:Specialpages
320 # Derived classes can override this, but usually it is easier to keep the default behaviour.
321 # Messages can be added at run-time, see MessageCache.php
322 function getDescription() {
323 return wfMsg( strtolower( $this->mName
) );
327 * Get a self-referential title object
329 function getTitle() {
330 return Title
::makeTitle( NS_SPECIAL
, $this->mName
);
334 * Set whether this page is listed in Special:Specialpages, at run-time
336 function setListed( $listed ) {
337 return wfSetVar( $this->mListed
, $listed );
342 * Shortcut to construct a special page which is unlisted by default
345 class UnlistedSpecialPage
extends SpecialPage
347 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
348 SpecialPage
::SpecialPage( $name, $restriction, false, $function, $file );