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.
18 global $wgSpecialPages;
23 $wgSpecialPages = array(
24 'DoubleRedirects' => new UnlistedSpecialPage ( 'DoubleRedirects' ),
25 'BrokenRedirects' => new UnlistedSpecialPage ( 'BrokenRedirects' ),
26 'Disambiguations' => new UnlistedSpecialPage ( 'Disambiguations' ),
28 "Userlogin" => new SpecialPage( "Userlogin" ),
29 "Userlogout" => new UnlistedSpecialPage( "Userlogout" ),
30 "Preferences" => new SpecialPage( "Preferences" ),
31 "Watchlist" => new SpecialPage( "Watchlist" ),
32 "Recentchanges" => new SpecialPage( "Recentchanges" ),
33 "Upload" => new SpecialPage( "Upload" ),
34 "Imagelist" => new SpecialPage( "Imagelist" ),
35 "Listusers" => new SpecialPage( "Listusers" ),
36 "Listadmins" => new SpecialPage( "Listadmins" ),
37 "Statistics" => new SpecialPage( "Statistics" ),
38 "Randompage" => new SpecialPage( "Randompage" ),
39 "Lonelypages" => new SpecialPage( "Lonelypages" ),
40 "Uncategorizedpages"=> new SpecialPage( "Uncategorizedpages" ),
41 "Unusedimages" => new SpecialPage( "Unusedimages" )
43 global $wgDisableCounters;
44 if( !$wgDisableCounters ) {
45 $wgSpecialPages["Popularpages"] = new SpecialPage( "Popularpages" );
47 $wgSpecialPages = array_merge($wgSpecialPages, array (
48 "Wantedpages" => new SpecialPage( "Wantedpages" ),
49 "Shortpages" => new SpecialPage( "Shortpages" ),
50 "Longpages" => new SpecialPage( "Longpages" ),
51 "Newpages" => new SpecialPage( "Newpages" ),
52 "Ancientpages" => new SpecialPage( "Ancientpages" ),
53 "Deadendpages" => new SpecialPage( "Deadendpages" ),
54 "Allpages" => new SpecialPage( "Allpages" ),
55 "Ipblocklist" => new SpecialPage( "Ipblocklist" ),
56 "Maintenance" => new SpecialPage( "Maintenance" ),
57 "Specialpages" => new UnlistedSpecialPage( "Specialpages" ),
58 "Contributions" => new UnlistedSpecialPage( "Contributions" ),
59 "Emailuser" => new UnlistedSpecialPage( "Emailuser" ),
60 "Whatlinkshere" => new UnlistedSpecialPage( "Whatlinkshere" ),
61 "Recentchangeslinked" => new UnlistedSpecialPage( "Recentchangeslinked" ),
62 "Movepage" => new UnlistedSpecialPage( "Movepage" ),
63 "Blockme" => new UnlistedSpecialPage( "Blockme" ),
64 "Geo" => new UnlistedSpecialPage( "Geo" ),
65 "Validate" => new UnlistedSpecialPage( "Validate" ),
66 "Booksources" => new SpecialPage( "Booksources" ),
67 "Categories" => new SpecialPage( "Categories" ),
68 "Export" => new SpecialPage( "Export" ),
69 "Version" => new SpecialPage( "Version" ),
70 "Allmessages" => new SpecialPage( "Allmessages" ),
71 "Search" => new UnlistedSpecialPage( "Search" ),
72 "Log" => new SpecialPage( "Log" ),
73 "Blockip" => new SpecialPage( "Blockip", "sysop" ),
74 "Asksql" => new SpecialPage( "Asksql", "sysop" ),
75 "Undelete" => new SpecialPage( "Undelete", "sysop" ),
76 "Makesysop" => new SpecialPage( "Makesysop", "sysop" ),
78 # Special:Import is half-written
79 # "Import" => new SpecialPage( "Import", "sysop" ),
81 "Lockdb" => new SpecialPage( "Lockdb", "developer" ),
82 "Unlockdb" => new SpecialPage( "Unlockdb", "developer" )
86 * Parent special page class, also static functions for handling the special
91 /* private */ var $mName; # The name of the class, used in the URL. Also used for the default
92 # <h1> heading, see getDescription()
93 /* private */ var $mRestriction; # Minimum user level required to access this page, or ""
94 # for anyone. Also used to categorise the pages in
95 # Special:Specialpages
96 /* private */ var $mListed; # Listed in Special:Specialpages?
97 /* private */ var $mFunction; # Function name called by the default execute()
98 /* private */ var $mFile; # File which needs to be included before the function above can be called
100 # Add a page to the list of valid special pages
101 # $obj->execute() must send HTML to $wgOut then return
102 # Use this for a special page extension
103 /* static */ function addPage( &$obj ) {
104 global $wgSpecialPages;
105 $wgSpecialPages[$obj->mName
] = $obj;
108 # Remove a special page from the list
109 # Occasionally used to disable expensive or dangerous special pages
110 /* static */ function removePage( $name ) {
111 global $wgSpecialPages;
112 unset( $wgSpecialPages[$name] );
115 # Find the object with a given name and return it (or NULL)
116 /* static */ function &getPage( $name ) {
117 global $wgSpecialPages;
118 if ( array_key_exists( $name, $wgSpecialPages ) ) {
119 return $wgSpecialPages[$name];
125 # Return categorised listable special pages
126 # Returns a 2d array where the first index is the restriction name
127 /* static */ function getPages() {
128 global $wgSpecialPages;
132 "developer" => array()
135 foreach ( $wgSpecialPages as $name => $page ) {
136 if ( $page->isListed() ) {
137 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
143 # Execute a special page path, which may contain parameters, e.g. Special:Name/Params
144 # $title should be a title object
145 # Extracts the special page name and call the execute method, passing the parameters
146 /* static */ function executePath( &$title ) {
147 global $wgSpecialPages, $wgOut, $wgTitle;
149 $bits = split( "/", $title->getDBkey(), 2 );
151 if( empty( $bits[1] ) ) {
157 $page =& SpecialPage
::getPage( $name );
158 if ( is_null( $page ) ) {
159 $wgOut->setArticleRelated( false );
160 $wgOut->setRobotpolicy( "noindex,follow" );
161 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
164 $wgTitle = Title
::makeTitle( NS_SPECIAL
, $name );
169 $page->execute( $par );
173 # Default constructor for special pages
174 # Derivative classes should call this from their constructor
175 # $name - the name of the special page, as seen in links and URLs
176 # $restriction - the minimum user level required, e.g. "sysop" or "developer".
178 # Note that if the user does not have the required level, an error message will
179 # be displayed by the default execute() method, without the global function ever
182 # If you override execute(), you can recover the default behaviour with userCanExecute()
183 # and displayRestrictionError()
185 # $listed - whether the page is listed in Special:Specialpages
186 # $function - the function called by execute(). By default it is constructed from $name
187 # $file - the file which is included by execute(). It is also constructed from $name by default
189 function SpecialPage( $name = "", $restriction = "", $listed = true, $function = false, $file = "default" ) {
190 $this->mName
= $name;
191 $this->mRestriction
= $restriction;
192 $this->mListed
= $listed;
193 if ( $function == false ) {
194 $this->mFunction
= "wfSpecial{$name}";
196 $this->mFunction
= $function;
198 if ( $file === "default" ) {
199 $this->mFile
= "Special{$name}.php";
201 $this->mFile
= $file;
205 # Accessor functions, see the descriptions of the associated variables above
206 function getName() { return $this->mName
; }
207 function getRestriction() { return $this->mRestriction
; }
208 function isListed() { return $this->mListed
; }
210 # Checks if the given user (identified by an object) can execute this special page (as
211 # defined by $mRestriction)
212 function userCanExecute( &$user ) {
213 if ( $this->mRestriction
== "" ) {
216 if ( in_array( $this->mRestriction
, $user->getRights() ) ) {
224 # Output an error message telling the user what access level they have to have
225 function displayRestrictionError() {
227 if ( $this->mRestriction
== "developer" ) {
228 $wgOut->developerRequired();
230 $wgOut->sysopRequired();
234 # Sets headers - this should be called from the execute() method of all derived classes!
235 function setHeaders() {
237 $wgOut->setArticleRelated( false );
238 $wgOut->setRobotPolicy( "noindex,follow" );
239 $wgOut->setPageTitle( $this->getDescription() );
242 # Default execute method
243 # Checks user permissions, calls the function given in mFunction
244 function execute( $par ) {
245 global $wgUser, $wgOut, $wgTitle;
249 if ( $this->userCanExecute( $wgUser ) ) {
250 if ( $this->mFile
) {
251 require_once( $this->mFile
);
253 $func = $this->mFunction
;
256 $this->displayRestrictionError();
260 # Returns the name that goes in the <h1> in the special page itself, and also the name that
261 # will be listed in Special:Specialpages
263 # Derived classes can override this, but usually it is easier to keep the default behaviour.
264 # Messages can be added at run-time, see MessageCache.php
265 function getDescription() {
266 return wfMsg( strtolower( $this->mName
) );
269 # Get a self-referential title object
270 function getTitle() {
271 return Title
::makeTitle( NS_SPECIAL
, $this->mName
);
274 # Set whether this page is listed in Special:Specialpages, at run-time
275 function setListed( $listed ) {
276 return wfSetVar( $this->mListed
, $listed );
281 * Shortcut to construct a special page which is unlisted by default
283 class UnlistedSpecialPage
extends SpecialPage
285 function UnlistedSpecialPage( $name, $restriction = "", $function = false, $file = "default" ) {
286 SpecialPage
::SpecialPage( $name, $restriction, false, $function, $file );