Migrate doubleredirect and brokenredirect subpages from SpecialMaintenance.php to...
[lhc/web/wiklou.git] / includes / SpecialPage.php
1 <?php
2 # SpecialPage: handling special pages and lists thereof
3
4 # $wgSpecialPages is a list of all SpecialPage objects. These objects are either instances of
5 # SpecialPage or a sub-class thereof. They have an execute() method, which sends the HTML for the
6 # special page to $wgOut. The parent class has an execute() method which distributes the call to
7 # the historical global functions. Additionally, execute() also checks if the user has the
8 # necessary access privileges and bails out if not.
9
10 # To add a special page at run-time, use SpecialPage::addPage().
11 # DO NOT manipulate this array at run-time.
12
13 global $wgSpecialPages;
14
15 /* private */ $wgSpecialPages = array(
16 'DoubleRedirects' => new UnlistedSpecialPage ( 'DoubleRedirects' ),
17 'BrokenRedirects' => new UnlistedSpecialPage ( 'BrokenRedirects' ),
18
19 "Userlogin" => new SpecialPage( "Userlogin" ),
20 "Userlogout" => new UnlistedSpecialPage( "Userlogout" ),
21 "Preferences" => new SpecialPage( "Preferences" ),
22 "Watchlist" => new SpecialPage( "Watchlist" ),
23 "Recentchanges" => new SpecialPage( "Recentchanges" ),
24 "Upload" => new SpecialPage( "Upload" ),
25 "Imagelist" => new SpecialPage( "Imagelist" ),
26 "Listusers" => new SpecialPage( "Listusers" ),
27 "Listadmins" => new SpecialPage( "Listadmins" ),
28 "Statistics" => new SpecialPage( "Statistics" ),
29 "Randompage" => new SpecialPage( "Randompage" ),
30 "Lonelypages" => new SpecialPage( "Lonelypages" ),
31 "Uncategorizedpages"=> new SpecialPage( "Uncategorizedpages" ),
32 "Unusedimages" => new SpecialPage( "Unusedimages" )
33 );
34 global $wgDisableCounters;
35 if( !$wgDisableCounters )
36 {
37 $wgSpecialPages["Popularpages"] = new SpecialPage( "Popularpages" );
38 }
39 $wgSpecialPages = array_merge($wgSpecialPages, array (
40 "Wantedpages" => new SpecialPage( "Wantedpages" ),
41 "Shortpages" => new SpecialPage( "Shortpages" ),
42 "Longpages" => new SpecialPage( "Longpages" ),
43 "Newpages" => new SpecialPage( "Newpages" ),
44 "Ancientpages" => new SpecialPage( "Ancientpages" ),
45 "Deadendpages" => new SpecialPage( "Deadendpages" ),
46 "Allpages" => new SpecialPage( "Allpages" ),
47 "Ipblocklist" => new SpecialPage( "Ipblocklist" ),
48 "Maintenance" => new SpecialPage( "Maintenance" ),
49 "Specialpages" => new UnlistedSpecialPage( "Specialpages" ),
50 "Contributions" => new UnlistedSpecialPage( "Contributions" ),
51 "Emailuser" => new UnlistedSpecialPage( "Emailuser" ),
52 "Whatlinkshere" => new UnlistedSpecialPage( "Whatlinkshere" ),
53 "Recentchangeslinked" => new UnlistedSpecialPage( "Recentchangeslinked" ),
54 "Movepage" => new UnlistedSpecialPage( "Movepage" ),
55 "Blockme" => new UnlistedSpecialPage( "Blockme" ),
56 "Geo" => new UnlistedSpecialPage( "Geo" ),
57 "Validate" => new UnlistedSpecialPage( "Validate" ),
58 "Booksources" => new SpecialPage( "Booksources" ),
59 "Categories" => new SpecialPage( "Categories" ),
60 "Export" => new SpecialPage( "Export" ),
61 "Version" => new SpecialPage( "Version" ),
62 "Allmessages" => new SpecialPage( "Allmessages" ),
63 "Search" => new UnlistedSpecialPage( "Search" ),
64 "Blockip" => new SpecialPage( "Blockip", "sysop" ),
65 "Asksql" => new SpecialPage( "Asksql", "sysop" ),
66 "Undelete" => new SpecialPage( "Undelete", "sysop" ),
67 "Makesysop" => new SpecialPage( "Makesysop", "sysop" ),
68
69 # Special:Import is half-written
70 # "Import" => new SpecialPage( "Import", "sysop" ),
71
72 "Lockdb" => new SpecialPage( "Lockdb", "developer" ),
73 "Unlockdb" => new SpecialPage( "Unlockdb", "developer" )
74 ));
75
76 # Parent special page class, also static functions for handling the special page list
77 class SpecialPage
78 {
79 /* private */ var $mName; # The name of the class, used in the URL. Also used for the default
80 # <h1> heading, see getDescription()
81 /* private */ var $mRestriction; # Minimum user level required to access this page, or ""
82 # for anyone. Also used to categorise the pages in
83 # Special:Specialpages
84 /* private */ var $mListed; # Listed in Special:Specialpages?
85 /* private */ var $mFunction; # Function name called by the default execute()
86 /* private */ var $mFile; # File which needs to be included before the function above can be called
87
88 # Add a page to the list of valid special pages
89 # $obj->execute() must send HTML to $wgOut then return
90 # Use this for a special page extension
91 /* static */ function addPage( &$obj ) {
92 global $wgSpecialPages;
93 $wgSpecialPages[$obj->mName] = $obj;
94 }
95
96 # Remove a special page from the list
97 # Occasionally used to disable expensive or dangerous special pages
98 /* static */ function removePage( $name ) {
99 global $wgSpecialPages;
100 unset( $wgSpecialPages[$name] );
101 }
102
103 # Find the object with a given name and return it (or NULL)
104 /* static */ function &getPage( $name ) {
105 global $wgSpecialPages;
106 if ( array_key_exists( $name, $wgSpecialPages ) ) {
107 return $wgSpecialPages[$name];
108 } else {
109 return NULL;
110 }
111 }
112
113 # Return categorised listable special pages
114 # Returns a 2d array where the first index is the restriction name
115 /* static */ function getPages() {
116 global $wgSpecialPages;
117 $pages = array(
118 "" => array(),
119 "sysop" => array(),
120 "developer" => array()
121 );
122
123 foreach ( $wgSpecialPages as $name => $page ) {
124 if ( $page->isListed() ) {
125 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
126 }
127 }
128 return $pages;
129 }
130
131 # Execute a special page path, which may contain parameters, e.g. Special:Name/Params
132 # $title should be a title object
133 # Extracts the special page name and call the execute method, passing the parameters
134 /* static */ function executePath( &$title ) {
135 global $wgSpecialPages, $wgOut, $wgTitle;
136
137 $bits = split( "/", $title->getDBkey(), 2 );
138 $name = $bits[0];
139 if( empty( $bits[1] ) ) {
140 $par = NULL;
141 } else {
142 $par = $bits[1];
143 }
144
145 $page =& SpecialPage::getPage( $name );
146 if ( is_null( $page ) ) {
147 $wgOut->setArticleRelated( false );
148 $wgOut->setRobotpolicy( "noindex,follow" );
149 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
150 } else {
151 if($par !== NULL) {
152 $wgTitle = Title::makeTitle( NS_SPECIAL, $name );
153 } else {
154 $wgTitle = $title;
155 }
156
157 $page->execute( $par );
158 }
159 }
160
161 # Default constructor for special pages
162 # Derivative classes should call this from their constructor
163 # $name - the name of the special page, as seen in links and URLs
164 # $restriction - the minimum user level required, e.g. "sysop" or "developer".
165 #
166 # Note that if the user does not have the required level, an error message will
167 # be displayed by the default execute() method, without the global function ever
168 # being called.
169 #
170 # If you override execute(), you can recover the default behaviour with userCanExecute()
171 # and displayRestrictionError()
172 #
173 # $listed - whether the page is listed in Special:Specialpages
174 # $function - the function called by execute(). By default it is constructed from $name
175 # $file - the file which is included by execute(). It is also constructed from $name by default
176 #
177 function SpecialPage( $name = "", $restriction = "", $listed = true, $function = false, $file = "default" ) {
178 $this->mName = $name;
179 $this->mRestriction = $restriction;
180 $this->mListed = $listed;
181 if ( $function == false ) {
182 $this->mFunction = "wfSpecial{$name}";
183 } else {
184 $this->mFunction = $function;
185 }
186 if ( $file === "default" ) {
187 $this->mFile = "Special{$name}.php";
188 } else {
189 $this->mFile = $file;
190 }
191 }
192
193 # Accessor functions, see the descriptions of the associated variables above
194 function getName() { return $this->mName; }
195 function getRestriction() { return $this->mRestriction; }
196 function isListed() { return $this->mListed; }
197
198 # Checks if the given user (identified by an object) can execute this special page (as
199 # defined by $mRestriction)
200 function userCanExecute( &$user ) {
201 if ( $this->mRestriction == "" ) {
202 return true;
203 } else {
204 if ( in_array( $this->mRestriction, $user->getRights() ) ) {
205 return true;
206 } else {
207 return false;
208 }
209 }
210 }
211
212 # Output an error message telling the user what access level they have to have
213 function displayRestrictionError() {
214 global $wgOut;
215 if ( $this->mRestriction == "developer" ) {
216 $wgOut->developerRequired();
217 } else {
218 $wgOut->sysopRequired();
219 }
220 }
221
222 # Sets headers - this should be called from the execute() method of all derived classes!
223 function setHeaders() {
224 global $wgOut;
225 $wgOut->setArticleRelated( false );
226 $wgOut->setRobotPolicy( "noindex,follow" );
227 $wgOut->setPageTitle( $this->getDescription() );
228 }
229
230 # Default execute method
231 # Checks user permissions, calls the function given in mFunction
232 function execute( $par ) {
233 global $wgUser, $wgOut, $wgTitle;
234
235 $this->setHeaders();
236
237 if ( $this->userCanExecute( $wgUser ) ) {
238 if ( $this->mFile ) {
239 require_once( $this->mFile );
240 }
241 $func = $this->mFunction;
242 $func( $par );
243 } else {
244 $this->displayRestrictionError();
245 }
246 }
247
248 # Returns the name that goes in the <h1> in the special page itself, and also the name that
249 # will be listed in Special:Specialpages
250 #
251 # Derived classes can override this, but usually it is easier to keep the default behaviour.
252 # Messages can be added at run-time, see MessageCache.php
253 function getDescription() {
254 return wfMsg( strtolower( $this->mName ) );
255 }
256
257 # Get a self-referential title object
258 function getTitle() {
259 return Title::makeTitle( NS_SPECIAL, $this->mName );
260 }
261
262 # Set whether this page is listed in Special:Specialpages, at run-time
263 function setListed( $listed ) {
264 return wfSetVar( $this->mListed, $listed );
265 }
266 }
267
268 # Shortcut to construct a special page which is unlisted by default
269 class UnlistedSpecialPage extends SpecialPage
270 {
271 function UnlistedSpecialPage( $name, $restriction = "", $function = false, $file = "default" ) {
272 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
273 }
274 }