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