2 # SpecialPage: handling special pages and lists thereof
4 # $wgSpecialPages is a list of all SpecialPage objects. These objects should be derived from
5 # SpecialPage. At a minimum, they must have an execute() method, which sends the HTML for the
6 # special page to $wgOut. The parent object has an execute() method which distributes the call
7 # to the historical global functions.
9 # To add a special page at run-time, use SpecialPage::addPage(). DO NOT add objects directly to
12 global $wgSpecialPages;
14 /* private */ $wgSpecialPages = array(
15 "Userlogin" => new SpecialPage( "Userlogin" ),
16 "Userlogout" => new UnlistedSpecialPage( "Userlogout" ),
17 "Preferences" => new SpecialPage( "Preferences" ),
18 "Watchlist" => new SpecialPage( "Watchlist" ),
19 "Recentchanges" => new SpecialPage( "Recentchanges" ),
20 "Upload" => new SpecialPage( "Upload" ),
21 "Imagelist" => new SpecialPage( "Imagelist" ),
22 "Listusers" => new SpecialPage( "Listusers" ),
23 "Listadmins" => new SpecialPage( "Listadmins" ),
24 "Statistics" => new SpecialPage( "Statistics" ),
25 "Randompage" => new SpecialPage( "Randompage" ),
26 "Lonelypages" => new SpecialPage( "Lonelypages" ),
27 "Uncategorizedpages"=> new SpecialPage( "Uncategorizedpages" ),
28 "Unusedimages" => new SpecialPage( "Unusedimages" )
30 global $wgDisableCounters;
31 if( !$wgDisableCounters )
33 $wgSpecialPages["Popularpages"] = new SpecialPage( "Popularpages" );
35 $wgSpecialPages = array_merge($wgSpecialPages, array (
36 "Wantedpages" => new SpecialPage( "Wantedpages" ),
37 "Shortpages" => new SpecialPage( "Shortpages" ),
38 "Longpages" => new SpecialPage( "Longpages" ),
39 "Newpages" => new SpecialPage( "Newpages" ),
40 "Ancientpages" => new SpecialPage( "Ancientpages" ),
41 "Deadendpages" => new SpecialPage( "Deadendpages" ),
42 "Allpages" => new SpecialPage( "Allpages" ),
43 "Ipblocklist" => new SpecialPage( "Ipblocklist" ),
44 "Maintenance" => new SpecialPage( "Maintenance" ),
45 "Specialpages" => new UnlistedSpecialPage( "Specialpages" ),
46 "Contributions" => new UnlistedSpecialPage( "Contributions" ),
47 "Emailuser" => new UnlistedSpecialPage( "Emailuser" ),
48 "Whatlinkshere" => new UnlistedSpecialPage( "Whatlinkshere" ),
49 "Recentchangeslinked" => new UnlistedSpecialPage( "Recentchangeslinked" ),
50 "Movepage" => new UnlistedSpecialPage( "Movepage" ),
51 "Blockme" => new UnlistedSpecialPage( "Blockme" ),
52 "Geo" => new UnlistedSpecialPage( "Geo" ),
53 "Validate" => new UnlistedSpecialPage( "Validate" ),
54 "Booksources" => new SpecialPage( "Booksources" ),
55 "Categories" => new SpecialPage( "Categories" ),
56 "Export" => new SpecialPage( "Export" ),
57 "Version" => new SpecialPage( "Version" ),
58 "Allmessages" => new SpecialPage( "Allmessages" ),
59 "Search" => new UnlistedSpecialPage( "Search" ),
60 "Blockip" => new SpecialPage( "Blockip", "sysop" ),
61 "Asksql" => new SpecialPage( "Asksql", "sysop" ),
62 "Undelete" => new SpecialPage( "Undelete", "sysop" ),
63 "Makesysop" => new SpecialPage( "Makesysop", "sysop" ),
65 # Special:Import is half-written
66 # "Import" => new SpecialPage( "Import", "sysop" ),
68 "Lockdb" => new SpecialPage( "Lockdb", "developer" ),
69 "Unlockdb" => new SpecialPage( "Unlockdb", "developer" )
72 # Parent special page class, also static functions for handling the special page list
75 /* private */ var $mName; # The name of the class, used in the URL. Also used for the default
76 # <h1> heading, see getDescription()
77 /* private */ var $mRestriction; # Minimum user level required to access this page, or ""
78 # for anyone. Also used to categorise the pages in
79 # Special:Specialpages
80 /* private */ var $mListed; # Listed in Special:Specialpages?
81 /* private */ var $mFunction; # Function name called by the default execute()
82 /* private */ var $mFile; # File which needs to be included before the function above can be called
84 # Add a page to the list of valid special pages
85 # $obj->execute() must send HTML to $wgOut then return
86 # Use this for a special page extension
87 /* static */ function addPage( &$obj ) {
88 global $wgSpecialPages;
89 $wgSpecialPages[$obj->mName
] = $obj;
92 # Remove a special page from the list
93 # Occasionally used to disable expensive or dangerous special pages
94 /* static */ function removePage( $name ) {
95 global $wgSpecialPages;
96 unset( $wgSpecialPages[$name] );
99 # Find the object with a given name and return it (or NULL)
100 /* static */ function &getPage( $name ) {
101 global $wgSpecialPages;
102 if ( array_key_exists( $name, $wgSpecialPages ) ) {
103 return $wgSpecialPages[$name];
109 # Return categorised listable special pages
110 # Returns a 2d array where the first index is the restriction name
111 /* static */ function getPages() {
112 global $wgSpecialPages;
116 "developer" => array()
119 foreach ( $wgSpecialPages as $name => $page ) {
120 if ( $page->isListed() ) {
121 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
127 # Execute a special page path, which may contain parameters, e.g. Special:Name/Params
128 # $title should be a title object
129 # Extracts the special page name and call the execute method, passing the parameters
130 /* static */ function executePath( &$title ) {
131 global $wgSpecialPages, $wgOut, $wgTitle;
133 $bits = split( "/", $title->getDBkey(), 2 );
135 if( empty( $bits[1] ) ) {
141 $page =& SpecialPage
::getPage( $name );
142 if ( is_null( $page ) ) {
143 $wgOut->setArticleRelated( false );
144 $wgOut->setRobotpolicy( "noindex,follow" );
145 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
148 $wgTitle = Title
::makeTitle( NS_SPECIAL
, $name );
153 $page->execute( $par );
157 # Default constructor for special pages
158 # Derivative classes should call this from their constructor
159 # $name - the name of the special page, as seen in links and URLs
160 # $restriction - the minimum user level required, e.g. "sysop" or "developer".
162 # Note that if the user does not have the required level, an error message will
163 # be displayed by the default execute() method, without the global function ever
166 # If you override execute(), you can recover the default behaviour with userCanExecute()
167 # and displayRestrictionError()
169 # $listed - whether the page is listed in Special:Specialpages
170 # $function - the function called by execute(). By default it is constructed from $name
171 # $file - the file which is included by execute(). It is also constructed from $name by default
173 function SpecialPage( $name = "", $restriction = "", $listed = true, $function = false, $file = "default" ) {
174 $this->mName
= $name;
175 $this->mRestriction
= $restriction;
176 $this->mListed
= $listed;
177 if ( $function == false ) {
178 $this->mFunction
= "wfSpecial{$name}";
180 $this->mFunction
= $function;
182 if ( $file === "default" ) {
183 $this->mFile
= "Special{$name}.php";
185 $this->mFile
= $file;
189 # Accessor functions, see the descriptions of the associated variables above
190 function getName() { return $this->mName
; }
191 function getRestriction() { return $this->mRestriction
; }
192 function isListed() { return $this->mListed
; }
194 # Checks if the given user (identified by an object) can execute this special page (as
195 # defined by $mRestriction)
196 function userCanExecute( &$user ) {
197 if ( $this->mRestriction
== "" ) {
200 if ( in_array( $this->mRestriction
, $user->getRights() ) ) {
208 # Output an error message telling the user what access level they have to have
209 function displayRestrictionError() {
211 if ( $this->mRestriction
== "developer" ) {
212 $wgOut->developerRequired();
214 $wgOut->sysopRequired();
218 # Sets headers - this should be called from the execute() method of all derived classes!
219 function setHeaders() {
221 $wgOut->setArticleRelated( false );
222 $wgOut->setRobotPolicy( "noindex,follow" );
223 $wgOut->setPageTitle( $this->getDescription() );
226 # Default execute method
227 # Checks user permissions, calls the function given in mFunction
228 function execute( $par ) {
229 global $wgUser, $wgOut, $wgTitle;
233 if ( $this->userCanExecute( $wgUser ) ) {
234 if ( $this->mFile
) {
235 require_once( $this->mFile
);
237 $func = $this->mFunction
;
240 $this->displayRestrictionError();
244 # Returns the name that goes in the <h1> in the special page itself, and also the name that
245 # will be listed in Special:Specialpages
247 # Derived classes can override this, but usually it is easier to keep the default behaviour.
248 # Messages can be added at run-time, see MessageCache.php
249 function getDescription() {
250 return wfMsg( strtolower( $this->mName
) );
253 # Get a self-referential title object
254 function getTitle() {
255 return Title
::makeTitle( NS_SPECIAL
, $this->mName
);
258 # Set whether this page is listed in Special:Specialpages, at run-time
259 function setListed( $listed ) {
260 return wfSetVar( $this->mListed
, $listed );
264 # Shortcut to construct a special page which is unlisted by default
265 class UnlistedSpecialPage
extends SpecialPage
267 function UnlistedSpecialPage( $name, $restriction = "", $function = false, $file = "default" ) {
268 SpecialPage
::SpecialPage( $name, $restriction, false, $function, $file );