* Added a new associative array ($wgSpecialPageRedirects) that holds
[lhc/web/wiklou.git] / includes / SpecialPage.php
1 <?php
2 /**
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.
10 *
11 * To add a special page at run-time, use SpecialPage::addPage().
12 * DO NOT manipulate this array at run-time.
13 *
14 * @package MediaWiki
15 * @subpackage SpecialPage
16 */
17
18
19 /**
20 *
21 */
22 global $wgSpecialPages, $wgSpecialPageRedirects;
23
24 /**
25 * @access private
26 */
27 $wgSpecialPages = array(
28 'DoubleRedirects' => new SpecialPage ( 'DoubleRedirects' ),
29 'BrokenRedirects' => new SpecialPage ( 'BrokenRedirects' ),
30 'Disambiguations' => new SpecialPage ( 'Disambiguations' ),
31
32 'Userlogin' => new SpecialPage( 'Userlogin' ),
33 'Userlogout' => new UnlistedSpecialPage( 'Userlogout' ),
34 'Preferences' => new SpecialPage( 'Preferences' ),
35 'Watchlist' => new SpecialPage( 'Watchlist' ),
36
37 'Mytalk' => new UnlistedSpecialPage( 'Mytalk'),
38 'Mycontributions' => new UnlistedSpecialPage( 'Mycontributions'),
39 'Mypage' => new UnlistedSpecialPage( 'Mypage'),
40
41 'Recentchanges' => new SpecialPage( 'Recentchanges' ),
42 'Upload' => new SpecialPage( 'Upload' ),
43 'Imagelist' => new SpecialPage( 'Imagelist' ),
44 'Newimages' => new SpecialPage( 'Newimages' ),
45 'Listusers' => new SpecialPage( 'Listusers' ),
46 'Statistics' => new SpecialPage( 'Statistics' ),
47 'Randompage' => new SpecialPage( 'Randompage' ),
48 'Lonelypages' => new SpecialPage( 'Lonelypages' ),
49 'Uncategorizedpages'=> new SpecialPage( 'Uncategorizedpages' ),
50 'Uncategorizedcategories'=> new SpecialPage( 'Uncategorizedcategories' ),
51 'Unusedimages' => new SpecialPage( 'Unusedimages' ),
52 'Wantedpages' => new SpecialPage( 'Wantedpages' ),
53 'Shortpages' => new SpecialPage( 'Shortpages' ),
54 'Longpages' => new SpecialPage( 'Longpages' ),
55 'Newpages' => new SpecialPage( 'Newpages' ),
56 'Ancientpages' => new SpecialPage( 'Ancientpages' ),
57 'Deadendpages' => new SpecialPage( 'Deadendpages' ),
58 'Allpages' => new SpecialPage( 'Allpages' ),
59 'Ipblocklist' => new SpecialPage( 'Ipblocklist' ),
60 'Specialpages' => new UnlistedSpecialPage( 'Specialpages' ),
61 'Contributions' => new UnlistedSpecialPage( 'Contributions' ),
62 'Emailuser' => new UnlistedSpecialPage( 'Emailuser' ),
63 'Whatlinkshere' => new UnlistedSpecialPage( 'Whatlinkshere' ),
64 'Recentchangeslinked' => new UnlistedSpecialPage( 'Recentchangeslinked' ),
65 'Movepage' => new UnlistedSpecialPage( 'Movepage' ),
66 'Blockme' => new UnlistedSpecialPage( 'Blockme' ),
67 'Booksources' => new SpecialPage( 'Booksources' ),
68 'Categories' => new SpecialPage( 'Categories' ),
69 'Export' => new SpecialPage( 'Export' ),
70 'Version' => new SpecialPage( 'Version' ),
71 'Allmessages' => new SpecialPage( 'Allmessages' ),
72 'Log' => new SpecialPage( 'Log' ),
73 'Blockip' => new SpecialPage( 'Blockip', 'block' ),
74 'Undelete' => new SpecialPage( 'Undelete', 'delete' ),
75 "Import" => new SpecialPage( "Import", 'import' ),
76 'Lockdb' => new SpecialPage( 'Lockdb', 'siteadmin' ),
77 'Unlockdb' => new SpecialPage( 'Unlockdb', 'siteadmin' ),
78 'Userrights' => new SpecialPage( 'Userrights', 'userrights' ),
79 'Groups' => new SpecialPage( 'Groups' ),
80 );
81
82 /**
83 * Sometimes the functionality of a specialpage is merged into the
84 * functionality of another or its simply renamed, this allows us to redirect
85 * the request to the proper place.
86 *
87 * @access private
88 */
89 $wgSpecialPageRedirects = array(
90 'Listadmins' => 'Listusers'
91 );
92
93 global $wgUseValidation ;
94 if ( $wgUseValidation )
95 $wgSpecialPages['Validate'] = new SpecialPage( 'Validate' );
96
97 global $wgDisableCounters;
98 if( !$wgDisableCounters ) {
99 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
100 }
101
102 global $wgDisableInternalSearch;
103 if( !$wgDisableInternalSearch ) {
104 $wgSpecialPages['Search'] = new UnlistedSpecialPage( 'Search' );
105 }
106
107 global $wgEmailAuthentication;
108 if( $wgEmailAuthentication ) {
109 $wgSpecialPages['Confirmemail'] = new UnlistedSpecialPage( 'Confirmemail' );
110 }
111
112 /**
113 * Parent special page class, also static functions for handling the special
114 * page list
115 * @package MediaWiki
116 */
117 class SpecialPage
118 {
119 /**#@+
120 * @access private
121 */
122 /**
123 * The name of the class, used in the URL.
124 * Also used for the default <h1> heading, @see getDescription()
125 */
126 var $mName;
127 /**
128 * Minimum user level required to access this page, or "" for anyone.
129 * Also used to categorise the pages in Special:Specialpages
130 */
131 var $mRestriction;
132 /**
133 * Listed in Special:Specialpages?
134 */
135 var $mListed;
136 /**
137 * Function name called by the default execute()
138 */
139 var $mFunction;
140 /**
141 * File which needs to be included before the function above can be called
142 */
143 var $mFile;
144 /**#@-*/
145
146 /**
147 * Add a page to the list of valid special pages
148 * $obj->execute() must send HTML to $wgOut then return
149 * Use this for a special page extension
150 * @static
151 */
152 function addPage( &$obj ) {
153 global $wgSpecialPages;
154 $wgSpecialPages[$obj->mName] = $obj;
155 }
156
157 /**
158 * Remove a special page from the list
159 * Occasionally used to disable expensive or dangerous special pages
160 * @static
161 */
162 function removePage( $name ) {
163 global $wgSpecialPages;
164 unset( $wgSpecialPages[$name] );
165 }
166
167 /**
168 * Find the object with a given name and return it (or NULL)
169 * @static
170 * @param string $name
171 */
172 function &getPage( $name ) {
173 global $wgSpecialPages;
174 if ( array_key_exists( $name, $wgSpecialPages ) ) {
175 return $wgSpecialPages[$name];
176 } else {
177 return NULL;
178 }
179 }
180
181 /**
182 *
183 * @static
184 * @param string $nam
185 * @return mixed string if the redirect exists, otherwise NULL
186 */
187 function &getRedirect( $name ) {
188 global $wgSpecialPageRedirects;
189 if ( array_key_exists( $name, $wgSpecialPageRedirects ) ) {
190 return $wgSpecialPageRedirects[$name];
191 } else {
192 return NULL;
193 }
194 }
195
196 /**
197 * Return categorised listable special pages
198 * Returns a 2d array where the first index is the restriction name
199 * @static
200 */
201 function getPages() {
202 global $wgSpecialPages;
203 $pages = array(
204 '' => array(),
205 'sysop' => array(),
206 'developer' => array()
207 );
208
209 foreach ( $wgSpecialPages as $name => $page ) {
210 if ( $page->isListed() ) {
211 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
212 }
213 }
214 return $pages;
215 }
216
217 /**
218 * Execute a special page path.
219 * The path may contain parameters, e.g. Special:Name/Params
220 * Extracts the special page name and call the execute method, passing the parameters
221 *
222 * @param $title should be a title object
223 */
224 function executePath( &$title ) {
225 global $wgSpecialPages, $wgOut, $wgTitle;
226
227 $bits = split( "/", $title->getDBkey(), 2 );
228 $name = $bits[0];
229 if( !isset( $bits[1] ) ) { // bug 2087
230 $par = NULL;
231 } else {
232 $par = $bits[1];
233 }
234
235 $page =& SpecialPage::getPage( $name );
236 if ( is_null( $page ) ) {
237 $redir =& SpecialPage::getRedirect( $name );
238 if ( isset( $redir ) ) {
239 $t = Title::makeTitle( NS_SPECIAL, "Listusers" );
240 $wgOut->redirect ($t->getFullURL());
241 } else {
242 $wgOut->setArticleRelated( false );
243 $wgOut->setRobotpolicy( "noindex,follow" );
244 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
245 }
246 } else {
247 if($par !== NULL) {
248 $wgTitle = Title::makeTitle( NS_SPECIAL, $name );
249 } else {
250 $wgTitle = $title;
251 }
252
253 $page->execute( $par );
254 }
255 }
256
257 /**
258 * Default constructor for special pages
259 * Derivative classes should call this from their constructor
260 * Note that if the user does not have the required level, an error message will
261 * be displayed by the default execute() method, without the global function ever
262 * being called.
263 *
264 * If you override execute(), you can recover the default behaviour with userCanExecute()
265 * and displayRestrictionError()
266 *
267 * @param string $name Name of the special page, as seen in links and URLs
268 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
269 * @param boolean $listed Whether the page is listed in Special:Specialpages
270 * @param string $function Function called by execute(). By default it is constructed from $name
271 * @param string $file File which is included by execute(). It is also constructed from $name by default
272 */
273 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default' ) {
274 $this->mName = $name;
275 $this->mRestriction = $restriction;
276 $this->mListed = $listed;
277 if ( $function == false ) {
278 $this->mFunction = 'wfSpecial'.$name;
279 } else {
280 $this->mFunction = $function;
281 }
282 if ( $file === 'default' ) {
283 $this->mFile = "Special{$name}.php";
284 } else {
285 $this->mFile = $file;
286 }
287 }
288
289 # Accessor functions, see the descriptions of the associated variables above
290 function getName() { return $this->mName; }
291 function getRestriction() { return $this->mRestriction; }
292 function isListed() { return $this->mListed; }
293 function getFile() { return $this->mFile; }
294
295 /**
296 * Checks if the given user (identified by an object) can execute this
297 * special page (as defined by $mRestriction)
298 */
299 function userCanExecute( &$user ) {
300 if ( $this->mRestriction == "" ) {
301 return true;
302 } else {
303 if ( in_array( $this->mRestriction, $user->getRights() ) ) {
304 return true;
305 } else {
306 return false;
307 }
308 }
309 }
310
311 /**
312 * Output an error message telling the user what access level they have to have
313 */
314 function displayRestrictionError() {
315 global $wgOut;
316 if ( $this->mRestriction == "developer" ) {
317 $wgOut->developerRequired();
318 } else {
319 $wgOut->sysopRequired();
320 }
321 }
322
323 /**
324 * Sets headers - this should be called from the execute() method of all derived classes!
325 */
326 function setHeaders() {
327 global $wgOut;
328 $wgOut->setArticleRelated( false );
329 $wgOut->setRobotPolicy( "noindex,follow" );
330 $wgOut->setPageTitle( $this->getDescription() );
331 }
332
333 /**
334 * Default execute method
335 * Checks user permissions, calls the function given in mFunction
336 */
337 function execute( $par ) {
338 global $wgUser, $wgOut, $wgTitle;
339
340 $this->setHeaders();
341
342 if ( $this->userCanExecute( $wgUser ) ) {
343 if ( $this->mFile ) {
344 require_once( $this->mFile );
345 }
346 $func = $this->mFunction;
347 $func( $par );
348 } else {
349 $this->displayRestrictionError();
350 }
351 }
352
353 # Returns the name that goes in the <h1> in the special page itself, and also the name that
354 # will be listed in Special:Specialpages
355 #
356 # Derived classes can override this, but usually it is easier to keep the default behaviour.
357 # Messages can be added at run-time, see MessageCache.php
358 function getDescription() {
359 return wfMsg( strtolower( $this->mName ) );
360 }
361
362 /**
363 * Get a self-referential title object
364 */
365 function getTitle() {
366 return Title::makeTitle( NS_SPECIAL, $this->mName );
367 }
368
369 /**
370 * Set whether this page is listed in Special:Specialpages, at run-time
371 */
372 function setListed( $listed ) {
373 return wfSetVar( $this->mListed, $listed );
374 }
375 }
376
377 /**
378 * Shortcut to construct a special page which is unlisted by default
379 * @package MediaWiki
380 */
381 class UnlistedSpecialPage extends SpecialPage
382 {
383 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
384 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
385 }
386 }
387 ?>