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 "Listusers" => new SpecialPage( "Listusers" ),
38 "Listadmins" => new SpecialPage( "Listadmins" ),
39 "Statistics" => new SpecialPage( "Statistics" ),
40 "Randompage" => new SpecialPage( "Randompage" ),
41 "Lonelypages" => new SpecialPage( "Lonelypages" ),
42 "Uncategorizedpages"=> new SpecialPage( "Uncategorizedpages" ),
43 "Unusedimages" => new SpecialPage( "Unusedimages" )
45 global $wgDisableCounters;
46 if( !$wgDisableCounters ) {
47 $wgSpecialPages["Popularpages"] = new SpecialPage( "Popularpages" );
49 $wgSpecialPages = array_merge($wgSpecialPages, array (
50 "Wantedpages" => new SpecialPage( "Wantedpages" ),
51 "Shortpages" => new SpecialPage( "Shortpages" ),
52 "Longpages" => new SpecialPage( "Longpages" ),
53 "Newpages" => new SpecialPage( "Newpages" ),
54 "Ancientpages" => new SpecialPage( "Ancientpages" ),
55 "Deadendpages" => new SpecialPage( "Deadendpages" ),
56 "Allpages" => new SpecialPage( "Allpages" ),
57 "Ipblocklist" => new SpecialPage( "Ipblocklist" ),
58 "Maintenance" => new SpecialPage( "Maintenance" ),
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 "Geo" => new UnlistedSpecialPage( "Geo" ),
67 "Validate" => new UnlistedSpecialPage( "Validate" ),
68 "Booksources" => new SpecialPage( "Booksources" ),
69 "Categories" => new SpecialPage( "Categories" ),
70 "Export" => new SpecialPage( "Export" ),
71 "Version" => new SpecialPage( "Version" ),
72 "Allmessages" => new SpecialPage( "Allmessages" ),
73 "Search" => new UnlistedSpecialPage( "Search" ),
74 "Log" => new SpecialPage( "Log" ),
75 "Blockip" => new SpecialPage( "Blockip", "sysop" ),
76 "Asksql" => new SpecialPage( "Asksql", "sysop" ),
77 "Undelete" => new SpecialPage( "Undelete", "sysop" ),
78 "Makesysop" => new SpecialPage( "Makesysop", "sysop" ),
80 # Special:Import is half-written
81 # "Import" => new SpecialPage( "Import", "sysop" ),
83 "Lockdb" => new SpecialPage( "Lockdb", "developer" ),
84 "Unlockdb" => new SpecialPage( "Unlockdb", "developer" )
88 * Parent special page class, also static functions for handling the special
94 /* private */ var $mName; # The name of the class, used in the URL. Also used for the default
95 # <h1> heading, see getDescription()
96 /* private */ var $mRestriction; # Minimum user level required to access this page, or ""
97 # for anyone. Also used to categorise the pages in
98 # Special:Specialpages
99 /* private */ var $mListed; # Listed in Special:Specialpages?
100 /* private */ var $mFunction; # Function name called by the default execute()
101 /* private */ var $mFile; # File which needs to be included before the function above can be called
103 # Add a page to the list of valid special pages
104 # $obj->execute() must send HTML to $wgOut then return
105 # Use this for a special page extension
106 /* static */ function addPage( &$obj ) {
107 global $wgSpecialPages;
108 $wgSpecialPages[$obj->mName
] = $obj;
111 # Remove a special page from the list
112 # Occasionally used to disable expensive or dangerous special pages
113 /* static */ function removePage( $name ) {
114 global $wgSpecialPages;
115 unset( $wgSpecialPages[$name] );
118 # Find the object with a given name and return it (or NULL)
119 /* static */ function &getPage( $name ) {
120 global $wgSpecialPages;
121 if ( array_key_exists( $name, $wgSpecialPages ) ) {
122 return $wgSpecialPages[$name];
128 # Return categorised listable special pages
129 # Returns a 2d array where the first index is the restriction name
130 /* static */ function getPages() {
131 global $wgSpecialPages;
135 "developer" => array()
138 foreach ( $wgSpecialPages as $name => $page ) {
139 if ( $page->isListed() ) {
140 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
146 # Execute a special page path, which may contain parameters, e.g. Special:Name/Params
147 # $title should be a title object
148 # Extracts the special page name and call the execute method, passing the parameters
149 /* static */ function executePath( &$title ) {
150 global $wgSpecialPages, $wgOut, $wgTitle;
152 $bits = split( "/", $title->getDBkey(), 2 );
154 if( empty( $bits[1] ) ) {
160 $page =& SpecialPage
::getPage( $name );
161 if ( is_null( $page ) ) {
162 $wgOut->setArticleRelated( false );
163 $wgOut->setRobotpolicy( "noindex,follow" );
164 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
167 $wgTitle = Title
::makeTitle( NS_SPECIAL
, $name );
172 $page->execute( $par );
176 # Default constructor for special pages
177 # Derivative classes should call this from their constructor
178 # $name - the name of the special page, as seen in links and URLs
179 # $restriction - the minimum user level required, e.g. "sysop" or "developer".
181 # Note that if the user does not have the required level, an error message will
182 # be displayed by the default execute() method, without the global function ever
185 # If you override execute(), you can recover the default behaviour with userCanExecute()
186 # and displayRestrictionError()
188 # $listed - whether the page is listed in Special:Specialpages
189 # $function - the function called by execute(). By default it is constructed from $name
190 # $file - the file which is included by execute(). It is also constructed from $name by default
192 function SpecialPage( $name = "", $restriction = "", $listed = true, $function = false, $file = "default" ) {
193 $this->mName
= $name;
194 $this->mRestriction
= $restriction;
195 $this->mListed
= $listed;
196 if ( $function == false ) {
197 $this->mFunction
= "wfSpecial{$name}";
199 $this->mFunction
= $function;
201 if ( $file === "default" ) {
202 $this->mFile
= "Special{$name}.php";
204 $this->mFile
= $file;
208 # Accessor functions, see the descriptions of the associated variables above
209 function getName() { return $this->mName
; }
210 function getRestriction() { return $this->mRestriction
; }
211 function isListed() { return $this->mListed
; }
213 # Checks if the given user (identified by an object) can execute this special page (as
214 # defined by $mRestriction)
215 function userCanExecute( &$user ) {
216 if ( $this->mRestriction
== "" ) {
219 if ( in_array( $this->mRestriction
, $user->getRights() ) ) {
227 # Output an error message telling the user what access level they have to have
228 function displayRestrictionError() {
230 if ( $this->mRestriction
== "developer" ) {
231 $wgOut->developerRequired();
233 $wgOut->sysopRequired();
237 # Sets headers - this should be called from the execute() method of all derived classes!
238 function setHeaders() {
240 $wgOut->setArticleRelated( false );
241 $wgOut->setRobotPolicy( "noindex,follow" );
242 $wgOut->setPageTitle( $this->getDescription() );
245 # Default execute method
246 # Checks user permissions, calls the function given in mFunction
247 function execute( $par ) {
248 global $wgUser, $wgOut, $wgTitle;
252 if ( $this->userCanExecute( $wgUser ) ) {
253 if ( $this->mFile
) {
254 require_once( $this->mFile
);
256 $func = $this->mFunction
;
259 $this->displayRestrictionError();
263 # Returns the name that goes in the <h1> in the special page itself, and also the name that
264 # will be listed in Special:Specialpages
266 # Derived classes can override this, but usually it is easier to keep the default behaviour.
267 # Messages can be added at run-time, see MessageCache.php
268 function getDescription() {
269 return wfMsg( strtolower( $this->mName
) );
272 # Get a self-referential title object
273 function getTitle() {
274 return Title
::makeTitle( NS_SPECIAL
, $this->mName
);
277 # Set whether this page is listed in Special:Specialpages, at run-time
278 function setListed( $listed ) {
279 return wfSetVar( $this->mListed
, $listed );
284 * Shortcut to construct a special page which is unlisted by default
287 class UnlistedSpecialPage
extends SpecialPage
289 function UnlistedSpecialPage( $name, $restriction = "", $function = false, $file = "default" ) {
290 SpecialPage
::SpecialPage( $name, $restriction, false, $function, $file );