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