(reopened bug 5284) Handle cases like Special:Mypage/monobook.js&action=edit
[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 * @access private
21 */
22 $wgSpecialPages = array(
23 'DoubleRedirects' => new SpecialPage ( 'DoubleRedirects' ),
24 'BrokenRedirects' => new SpecialPage ( 'BrokenRedirects' ),
25 'Disambiguations' => new SpecialPage ( 'Disambiguations' ),
26
27 'Userlogin' => new SpecialPage( 'Userlogin' ),
28 'Userlogout' => new UnlistedSpecialPage( 'Userlogout' ),
29 'Preferences' => new SpecialPage( 'Preferences' ),
30 'Watchlist' => new SpecialPage( 'Watchlist' ),
31
32 'Recentchanges' => new IncludableSpecialPage( 'Recentchanges' ),
33 'Upload' => new SpecialPage( 'Upload' ),
34 'Imagelist' => new SpecialPage( 'Imagelist' ),
35 'Newimages' => new IncludableSpecialPage( 'Newimages' ),
36 'Listusers' => new SpecialPage( 'Listusers' ),
37 'Statistics' => new SpecialPage( 'Statistics' ),
38 'Random' => new SpecialPage( 'Randompage' ),
39 'Lonelypages' => new SpecialPage( 'Lonelypages' ),
40 'Uncategorizedpages'=> new SpecialPage( 'Uncategorizedpages' ),
41 'Uncategorizedcategories'=> new SpecialPage( 'Uncategorizedcategories' ),
42 'Unusedcategories' => new SpecialPage( 'Unusedcategories' ),
43 'Unusedimages' => new SpecialPage( 'Unusedimages' ),
44 'Wantedpages' => new IncludableSpecialPage( 'Wantedpages' ),
45 'Wantedcategories' => new SpecialPage( 'Wantedcategories' ),
46 'Mostlinked' => new SpecialPage( 'Mostlinked' ),
47 'Mostlinkedcategories' => new SpecialPage( 'Mostlinkedcategories' ),
48 'Mostcategories' => new SpecialPage( 'Mostcategories' ),
49 'Mostimages' => new SpecialPage( 'Mostimages' ),
50 'Mostrevisions' => new SpecialPage( 'Mostrevisions' ),
51 'Shortpages' => new SpecialPage( 'Shortpages' ),
52 'Longpages' => new SpecialPage( 'Longpages' ),
53 'Newpages' => new IncludableSpecialPage( 'Newpages' ),
54 'Ancientpages' => new SpecialPage( 'Ancientpages' ),
55 'Deadendpages' => new SpecialPage( 'Deadendpages' ),
56 'Allpages' => new IncludableSpecialPage( 'Allpages' ),
57 'Prefixindex' => new IncludableSpecialPage( 'Prefixindex' ) ,
58 'Ipblocklist' => new SpecialPage( 'Ipblocklist' ),
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 '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 'Log' => new SpecialPage( 'Log' ),
72 'Blockip' => new SpecialPage( 'Blockip', 'block' ),
73 'Undelete' => new SpecialPage( 'Undelete', 'deletedhistory' ),
74 "Import" => new SpecialPage( "Import", 'import' ),
75 'Lockdb' => new SpecialPage( 'Lockdb', 'siteadmin' ),
76 'Unlockdb' => new SpecialPage( 'Unlockdb', 'siteadmin' ),
77 'Userrights' => new SpecialPage( 'Userrights', 'userrights' ),
78 'MIMEsearch' => new SpecialPage( 'MIMEsearch' ),
79 'Unwatchedpages' => new SpecialPage( 'Unwatchedpages', 'unwatchedpages' ),
80 'Listredirects' => new SpecialPage( 'Listredirects' ),
81 'Revisiondelete' => new SpecialPage( 'Revisiondelete', 'deleterevision' ),
82 'Unusedtemplates' => new SpecialPage( 'Unusedtemplates' ),
83 'Randomredirect' => new SpecialPage( 'Randomredirect' ),
84 );
85
86 if( !$wgDisableCounters ) {
87 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
88 }
89
90 if( !$wgDisableInternalSearch ) {
91 $wgSpecialPages['Search'] = new SpecialPage( 'Search' );
92 }
93
94 if( $wgEmailAuthentication ) {
95 $wgSpecialPages['Confirmemail'] = new UnlistedSpecialPage( 'Confirmemail' );
96 }
97
98 /**
99 * Parent special page class, also static functions for handling the special
100 * page list
101 * @package MediaWiki
102 */
103 class SpecialPage
104 {
105 /**#@+
106 * @access private
107 */
108 /**
109 * The name of the class, used in the URL.
110 * Also used for the default <h1> heading, @see getDescription()
111 */
112 var $mName;
113 /**
114 * Minimum user level required to access this page, or "" for anyone.
115 * Also used to categorise the pages in Special:Specialpages
116 */
117 var $mRestriction;
118 /**
119 * Listed in Special:Specialpages?
120 */
121 var $mListed;
122 /**
123 * Function name called by the default execute()
124 */
125 var $mFunction;
126 /**
127 * File which needs to be included before the function above can be called
128 */
129 var $mFile;
130 /**
131 * Whether or not this special page is being included from an article
132 */
133 var $mIncluding;
134 /**
135 * Whether the special page can be included in an article
136 */
137 var $mIncludable;
138
139
140 /**#@-*/
141
142
143 /**
144 * Add a page to the list of valid special pages
145 * $obj->execute() must send HTML to $wgOut then return
146 * Use this for a special page extension
147 * @static
148 */
149 function addPage( &$obj ) {
150 global $wgSpecialPages;
151 $wgSpecialPages[$obj->mName] = $obj;
152 }
153
154 /**
155 * Remove a special page from the list
156 * Occasionally used to disable expensive or dangerous special pages
157 * @static
158 */
159 function removePage( $name ) {
160 global $wgSpecialPages;
161 unset( $wgSpecialPages[$name] );
162 }
163
164 /**
165 * Find the object with a given name and return it (or NULL)
166 * @static
167 * @param string $name
168 */
169 function getPage( $name ) {
170 global $wgSpecialPages;
171 if ( array_key_exists( $name, $wgSpecialPages ) ) {
172 return $wgSpecialPages[$name];
173 } else {
174 return NULL;
175 }
176 }
177
178 /**
179 * @static
180 * @param string $name
181 * @return mixed Title object if the redirect exists, otherwise NULL
182 */
183 function getRedirect( $name ) {
184 global $wgUser;
185
186 $redirects = array(
187 'Mypage' => Title::makeTitle( NS_USER, $wgUser->getName() ),
188 'Mytalk' => Title::makeTitle( NS_USER_TALK, $wgUser->getName() ),
189 'Mycontributions' => Title::makeTitle( NS_SPECIAL, 'Contributions/' . $wgUser->getName() ),
190 'Listadmins' => Title::makeTitle( NS_SPECIAL, 'Listusers/sysop' ), # @bug 2832
191 'Randompage' => Title::makeTitle( NS_SPECIAL, 'Random' ),
192 'Userlist' => Title::makeTitle( NS_SPECIAL, 'Listusers' )
193 );
194 wfRunHooks( 'SpecialPageGetRedirect', array( &$redirects ) );
195
196 return isset( $redirects[$name] ) ? $redirects[$name] : null;
197 }
198
199 /**
200 * Return part of the request string for a special redirect page
201 * This allows passing, e.g. action=history to Special:Mypage, etc.
202 *
203 * @param $name Name of the redirect page
204 * @return string
205 */
206 function getRedirectParams( $name ) {
207 global $wgRequest;
208
209 $args = array();
210 switch( $name ) {
211 case 'Mypage':
212 case 'Mytalk':
213 case 'Randompage':
214 $args = array( 'action' );
215 }
216
217 $params = array();
218 foreach( $args as $arg ) {
219 if( $val = $wgRequest->getVal( $arg, false ) )
220 $params[] = $arg . '=' . $val;
221 }
222
223 return count( $params ) ? implode( '&', $params ) : false;
224 }
225
226 /**
227 * Return categorised listable special pages
228 * Returns a 2d array where the first index is the restriction name
229 * @static
230 */
231 function getPages() {
232 global $wgSpecialPages;
233 $pages = array(
234 '' => array(),
235 'sysop' => array(),
236 'developer' => array()
237 );
238
239 foreach ( $wgSpecialPages as $name => $page ) {
240 if ( $page->isListed() ) {
241 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
242 }
243 }
244 return $pages;
245 }
246
247 /**
248 * Execute a special page path.
249 * The path may contain parameters, e.g. Special:Name/Params
250 * Extracts the special page name and call the execute method, passing the parameters
251 *
252 * Returns a title object if the page is redirected, false if there was no such special
253 * page, and true if it was successful.
254 *
255 * @param $title a title object
256 * @param $including output is being captured for use in {{special:whatever}}
257 */
258 function executePath( &$title, $including = false ) {
259 global $wgOut, $wgTitle;
260 $fname = 'SpecialPage::executePath';
261 wfProfileIn( $fname );
262
263 $bits = split( "/", $title->getDBkey(), 2 );
264 $name = $bits[0];
265 if( !isset( $bits[1] ) ) { // bug 2087
266 $par = NULL;
267 } else {
268 $par = $bits[1];
269 }
270
271 $page = SpecialPage::getPage( $name );
272 if ( is_null( $page ) ) {
273 if ( $including ) {
274 wfProfileOut( $fname );
275 return false;
276 } else {
277 $redir = SpecialPage::getRedirect( $name );
278 if ( isset( $redir ) ) {
279 if( $par )
280 $redir = Title::makeTitle( $redir->getNamespace(), $redir->getText() . '/' . $par );
281 $params = SpecialPage::getRedirectParams( $name );
282 if( $params ) {
283 $url = $redir->getFullUrl( $params );
284 } else {
285 $url = $redir->getFullUrl();
286 }
287 $wgOut->redirect( $url );
288 $retVal = $redir;
289 $wgOut->redirect( $url );
290 $retVal = $redir;
291 } else {
292 $wgOut->setArticleRelated( false );
293 $wgOut->setRobotpolicy( 'noindex,follow' );
294 $wgOut->setStatusCode( 404 );
295 $wgOut->errorpage( 'nosuchspecialpage', 'nospecialpagetext' );
296 $retVal = false;
297 }
298 }
299 } else {
300 if ( $including && !$page->includable() ) {
301 wfProfileOut( $fname );
302 return false;
303 } elseif ( !$including ) {
304 if($par !== NULL) {
305 $wgTitle = Title::makeTitle( NS_SPECIAL, $name );
306 } else {
307 $wgTitle = $title;
308 }
309 }
310 $page->including( $including );
311
312 $profName = 'Special:' . $page->getName();
313 wfProfileIn( $profName );
314 $page->execute( $par );
315 wfProfileOut( $profName );
316 $retVal = true;
317 }
318 wfProfileOut( $fname );
319 return $retVal;
320 }
321
322 /**
323 * Just like executePath() except it returns the HTML instead of outputting it
324 * Returns false if there was no such special page, or a title object if it was
325 * a redirect.
326 * @static
327 */
328 function capturePath( &$title ) {
329 global $wgOut, $wgTitle;
330
331 $oldTitle = $wgTitle;
332 $oldOut = $wgOut;
333 $wgOut = new OutputPage;
334
335 $ret = SpecialPage::executePath( $title, true );
336 if ( $ret === true ) {
337 $ret = $wgOut->getHTML();
338 }
339 $wgTitle = $oldTitle;
340 $wgOut = $oldOut;
341 return $ret;
342 }
343
344 /**
345 * Default constructor for special pages
346 * Derivative classes should call this from their constructor
347 * Note that if the user does not have the required level, an error message will
348 * be displayed by the default execute() method, without the global function ever
349 * being called.
350 *
351 * If you override execute(), you can recover the default behaviour with userCanExecute()
352 * and displayRestrictionError()
353 *
354 * @param string $name Name of the special page, as seen in links and URLs
355 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
356 * @param boolean $listed Whether the page is listed in Special:Specialpages
357 * @param string $function Function called by execute(). By default it is constructed from $name
358 * @param string $file File which is included by execute(). It is also constructed from $name by default
359 */
360 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
361 $this->mName = $name;
362 $this->mRestriction = $restriction;
363 $this->mListed = $listed;
364 $this->mIncludable = $includable;
365 if ( $function == false ) {
366 $this->mFunction = 'wfSpecial'.$name;
367 } else {
368 $this->mFunction = $function;
369 }
370 if ( $file === 'default' ) {
371 $this->mFile = "Special{$name}.php";
372 } else {
373 $this->mFile = $file;
374 }
375 }
376
377 /**#@+
378 * Accessor
379 *
380 * @deprecated
381 */
382 function getName() { return $this->mName; }
383 function getRestriction() { return $this->mRestriction; }
384 function getFile() { return $this->mFile; }
385 function isListed() { return $this->mListed; }
386 /**#@-*/
387
388 /**#@+
389 * Accessor and mutator
390 */
391 function name( $x = NULL ) { return wfSetVar( $this->mName, $x ); }
392 function restrictions( $x = NULL) { return wfSetVar( $this->mRestrictions, $x ); }
393 function listed( $x = NULL) { return wfSetVar( $this->mListed, $x ); }
394 function func( $x = NULL) { return wfSetVar( $this->mFunction, $x ); }
395 function file( $x = NULL) { return wfSetVar( $this->mFile, $x ); }
396 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable, $x ); }
397 function including( $x = NULL ) { return wfSetVar( $this->mIncluding, $x ); }
398 /**#@-*/
399
400 /**
401 * Checks if the given user (identified by an object) can execute this
402 * special page (as defined by $mRestriction)
403 */
404 function userCanExecute( &$user ) {
405 if ( $this->mRestriction == "" ) {
406 return true;
407 } else {
408 if ( in_array( $this->mRestriction, $user->getRights() ) ) {
409 return true;
410 } else {
411 return false;
412 }
413 }
414 }
415
416 /**
417 * Output an error message telling the user what access level they have to have
418 */
419 function displayRestrictionError() {
420 global $wgOut;
421 $wgOut->permissionRequired( $this->mRestriction );
422 }
423
424 /**
425 * Sets headers - this should be called from the execute() method of all derived classes!
426 */
427 function setHeaders() {
428 global $wgOut;
429 $wgOut->setArticleRelated( false );
430 $wgOut->setRobotPolicy( "noindex,follow" );
431 $wgOut->setPageTitle( $this->getDescription() );
432 }
433
434 /**
435 * Default execute method
436 * Checks user permissions, calls the function given in mFunction
437 */
438 function execute( $par ) {
439 global $wgUser;
440
441 $this->setHeaders();
442
443 if ( $this->userCanExecute( $wgUser ) ) {
444 $func = $this->mFunction;
445 // only load file if the function does not exist
446 if(!function_exists($func) and $this->mFile) {
447 require_once( $this->mFile );
448 }
449 if ( wfRunHooks( 'SpecialPageExecuteBeforeHeader', array( &$this, &$par, &$func ) ) )
450 $this->outputHeader();
451 if ( ! wfRunHooks( 'SpecialPageExecuteBeforePage', array( &$this, &$par, &$func ) ) )
452 return;
453 $func( $par, $this );
454 if ( ! wfRunHooks( 'SpecialPageExecuteAfterPage', array( &$this, &$par, &$func ) ) )
455 return;
456 } else {
457 $this->displayRestrictionError();
458 }
459 }
460
461 function outputHeader() {
462 global $wgOut, $wgContLang;
463
464 $msg = $wgContLang->lc( $this->name() ) . '-summary';
465 $out = wfMsg( $msg );
466 if ( ! wfEmptyMsg( $msg, $out ) and $out !== '' and ! $this->including() )
467 $wgOut->addWikiText( $out );
468
469 }
470
471 # Returns the name that goes in the <h1> in the special page itself, and also the name that
472 # will be listed in Special:Specialpages
473 #
474 # Derived classes can override this, but usually it is easier to keep the default behaviour.
475 # Messages can be added at run-time, see MessageCache.php
476 function getDescription() {
477 return wfMsg( strtolower( $this->mName ) );
478 }
479
480 /**
481 * Get a self-referential title object
482 */
483 function getTitle() {
484 return Title::makeTitle( NS_SPECIAL, $this->mName );
485 }
486
487 /**
488 * Set whether this page is listed in Special:Specialpages, at run-time
489 */
490 function setListed( $listed ) {
491 return wfSetVar( $this->mListed, $listed );
492 }
493
494 }
495
496 /**
497 * Shortcut to construct a special page which is unlisted by default
498 * @package MediaWiki
499 */
500 class UnlistedSpecialPage extends SpecialPage
501 {
502 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
503 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
504 }
505 }
506
507 /**
508 * Shortcut to construct an includable special page
509 * @package MediaWiki
510 */
511 class IncludableSpecialPage extends SpecialPage
512 {
513 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
514 SpecialPage::SpecialPage( $name, $restriction, $listed, $function, $file, true );
515 }
516 }
517 ?>