Deprecating ; reordering the special pages array (whose the special page list is...
[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 if ( self::$mListInitialised ) {
147 return;
148 }
149 wfProfileIn( __METHOD__ );
150
151 if( !$wgDisableCounters ) {
152 self::$mList['Popularpages'] = array( 'SpecialPage', 'Popularpages' );
153 }
154
155 if( !$wgDisableInternalSearch ) {
156 self::$mList['Search'] = array( 'SpecialPage', 'Search' );
157 }
158
159 if( $wgEmailAuthentication ) {
160 self::$mList['Confirmemail'] = array( 'UnlistedSpecialPage', 'Confirmemail' );
161 }
162
163 # Add extension special pages
164 self::$mList = array_merge( self::$mList, $wgSpecialPages );
165
166 # Better to set this now, to avoid infinite recursion in carelessly written hooks
167 self::$mListInitialised = true;
168
169 # Run hooks
170 # This hook can be used to remove undesired built-in special pages
171 wfRunHooks( 'SpecialPage_initList', array( &self::$mList ) );
172 wfProfileOut( __METHOD__ );
173 }
174
175 /**
176 * Add a page to the list of valid special pages. This used to be the preferred
177 * method for adding special pages in extensions. It's now suggested that you add
178 * an associative record to $wgSpecialPages. This avoids autoloading SpecialPage.
179 *
180 * @param mixed $page Must either be an array specifying a class name and
181 * constructor parameters, or an object. The object,
182 * when constructed, must have an execute() method which
183 * sends HTML to $wgOut.
184 * @static
185 */
186 static function addPage( &$page ) {
187 if ( !self::$mListInitialised ) {
188 self::initList();
189 }
190 self::$mList[$page->mName] = $page;
191 }
192
193 /**
194 * Remove a special page from the list
195 * Formerly used to disable expensive or dangerous special pages. The
196 * preferred method is now to add a SpecialPage_initList hook.
197 *
198 * @static
199 */
200 static function removePage( $name ) {
201 if ( !self::$mListInitialised ) {
202 self::initList();
203 }
204 unset( self::$mList[$name] );
205 }
206
207 /**
208 * Find the object with a given name and return it (or NULL)
209 * @static
210 * @param string $name
211 */
212 static function getPage( $name ) {
213 if ( !self::$mListInitialised ) {
214 self::initList();
215 }
216 if ( array_key_exists( $name, self::$mList ) ) {
217 $rec = self::$mList[$name];
218 if ( is_string( $rec ) ) {
219 $className = $rec;
220 self::$mList[$name] = new $className;
221 } elseif ( is_array( $rec ) ) {
222 $className = array_shift( $rec );
223 self::$mList[$name] = wfCreateObject( $className, $rec );
224 }
225 return self::$mList[$name];
226 } else {
227 return NULL;
228 }
229 }
230
231
232 /**
233 * @static
234 * @param string $name
235 * @return mixed Title object if the redirect exists, otherwise NULL
236 */
237 static function getRedirect( $name ) {
238 global $wgUser;
239
240 $redirects = array(
241 'Mypage' => Title::makeTitle( NS_USER, $wgUser->getName() ),
242 'Mytalk' => Title::makeTitle( NS_USER_TALK, $wgUser->getName() ),
243 'Mycontributions' => Title::makeTitle( NS_SPECIAL, 'Contributions/' . $wgUser->getName() ),
244 'Listadmins' => Title::makeTitle( NS_SPECIAL, 'Listusers/sysop' ), # @bug 2832
245 'Logs' => Title::makeTitle( NS_SPECIAL, 'Log' ),
246 'Randompage' => Title::makeTitle( NS_SPECIAL, 'Random' ),
247 'Userlist' => Title::makeTitle( NS_SPECIAL, 'Listusers' )
248 );
249 wfRunHooks( 'SpecialPageGetRedirect', array( &$redirects ) );
250
251 return isset( $redirects[$name] ) ? $redirects[$name] : null;
252 }
253
254 /**
255 * Return part of the request string for a special redirect page
256 * This allows passing, e.g. action=history to Special:Mypage, etc.
257 *
258 * @param $name Name of the redirect page
259 * @return string
260 */
261 function getRedirectParams( $name ) {
262 global $wgRequest;
263
264 $args = array();
265 switch( $name ) {
266 case 'Mypage':
267 case 'Mytalk':
268 case 'Randompage':
269 $args = array( 'action' );
270 }
271
272 $params = array();
273 foreach( $args as $arg ) {
274 if( $val = $wgRequest->getVal( $arg, false ) )
275 $params[] = $arg . '=' . $val;
276 }
277
278 return count( $params ) ? implode( '&', $params ) : false;
279 }
280
281 /**
282 * Return categorised listable special pages
283 * Returns a 2d array where the first index is the restriction name
284 * @static
285 */
286 static function getPages() {
287 if ( !self::$mListInitialised ) {
288 self::initList();
289 }
290 $pages = array();
291
292 foreach ( self::$mList as $name => $rec ) {
293 $page = self::getPage( $name );
294 if ( $page->isListed() ) {
295 $restricted = $page->getRestriction() == '' ? '' : 'restricted';
296 $pages[$restricted][$page->getName()] = $page;
297 }
298 }
299 return $pages;
300 }
301
302 /**
303 * Execute a special page path.
304 * The path may contain parameters, e.g. Special:Name/Params
305 * Extracts the special page name and call the execute method, passing the parameters
306 *
307 * Returns a title object if the page is redirected, false if there was no such special
308 * page, and true if it was successful.
309 *
310 * @param $title a title object
311 * @param $including output is being captured for use in {{special:whatever}}
312 */
313 static function executePath( &$title, $including = false ) {
314 global $wgOut, $wgTitle;
315 $fname = 'SpecialPage::executePath';
316 wfProfileIn( $fname );
317
318 $bits = split( "/", $title->getDBkey(), 2 );
319 $name = $bits[0];
320 if( !isset( $bits[1] ) ) { // bug 2087
321 $par = NULL;
322 } else {
323 $par = $bits[1];
324 }
325
326 $page = SpecialPage::getPage( $name );
327 if ( is_null( $page ) ) {
328 if ( $including ) {
329 wfProfileOut( $fname );
330 return false;
331 } else {
332 $redir = SpecialPage::getRedirect( $name );
333 if ( isset( $redir ) ) {
334 if( $par )
335 $redir = Title::makeTitle( $redir->getNamespace(), $redir->getText() . '/' . $par );
336 $params = SpecialPage::getRedirectParams( $name );
337 if( $params ) {
338 $url = $redir->getFullUrl( $params );
339 } else {
340 $url = $redir->getFullUrl();
341 }
342 $wgOut->redirect( $url );
343 $retVal = $redir;
344 $wgOut->redirect( $url );
345 $retVal = $redir;
346 } else {
347 $wgOut->setArticleRelated( false );
348 $wgOut->setRobotpolicy( 'noindex,nofollow' );
349 $wgOut->setStatusCode( 404 );
350 $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
351 $retVal = false;
352 }
353 }
354 } else {
355 if ( $including && !$page->includable() ) {
356 wfProfileOut( $fname );
357 return false;
358 } elseif ( !$including ) {
359 if($par !== NULL) {
360 $wgTitle = Title::makeTitle( NS_SPECIAL, $name );
361 } else {
362 $wgTitle = $title;
363 }
364 }
365 $page->including( $including );
366
367 $profName = 'Special:' . $page->getName();
368 wfProfileIn( $profName );
369 $page->execute( $par );
370 wfProfileOut( $profName );
371 $retVal = true;
372 }
373 wfProfileOut( $fname );
374 return $retVal;
375 }
376
377 /**
378 * Just like executePath() except it returns the HTML instead of outputting it
379 * Returns false if there was no such special page, or a title object if it was
380 * a redirect.
381 * @static
382 */
383 static function capturePath( &$title ) {
384 global $wgOut, $wgTitle;
385
386 $oldTitle = $wgTitle;
387 $oldOut = $wgOut;
388 $wgOut = new OutputPage;
389
390 $ret = SpecialPage::executePath( $title, true );
391 if ( $ret === true ) {
392 $ret = $wgOut->getHTML();
393 }
394 $wgTitle = $oldTitle;
395 $wgOut = $oldOut;
396 return $ret;
397 }
398
399 /**
400 * Default constructor for special pages
401 * Derivative classes should call this from their constructor
402 * Note that if the user does not have the required level, an error message will
403 * be displayed by the default execute() method, without the global function ever
404 * being called.
405 *
406 * If you override execute(), you can recover the default behaviour with userCanExecute()
407 * and displayRestrictionError()
408 *
409 * @param string $name Name of the special page, as seen in links and URLs
410 * @param string $restriction User right required, e.g. "block" or "delete"
411 * @param boolean $listed Whether the page is listed in Special:Specialpages
412 * @param string $function Function called by execute(). By default it is constructed from $name
413 * @param string $file File which is included by execute(). It is also constructed from $name by default
414 */
415 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
416 $this->mName = $name;
417 $this->mRestriction = $restriction;
418 $this->mListed = $listed;
419 $this->mIncludable = $includable;
420 if ( $function == false ) {
421 $this->mFunction = 'wfSpecial'.$name;
422 } else {
423 $this->mFunction = $function;
424 }
425 if ( $file === 'default' ) {
426 $this->mFile = "Special{$name}.php";
427 } else {
428 $this->mFile = $file;
429 }
430 }
431
432 /**#@+
433 * Accessor
434 *
435 * @deprecated
436 */
437 function getName() { return $this->mName; }
438 function getRestriction() { return $this->mRestriction; }
439 function getFile() { return $this->mFile; }
440 function isListed() { return $this->mListed; }
441 /**#@-*/
442
443 /**#@+
444 * Accessor and mutator
445 */
446 function name( $x = NULL ) { return wfSetVar( $this->mName, $x ); }
447 function restrictions( $x = NULL) { return wfSetVar( $this->mRestrictions, $x ); }
448 function listed( $x = NULL) { return wfSetVar( $this->mListed, $x ); }
449 function func( $x = NULL) { return wfSetVar( $this->mFunction, $x ); }
450 function file( $x = NULL) { return wfSetVar( $this->mFile, $x ); }
451 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable, $x ); }
452 function including( $x = NULL ) { return wfSetVar( $this->mIncluding, $x ); }
453 /**#@-*/
454
455 /**
456 * Checks if the given user (identified by an object) can execute this
457 * special page (as defined by $mRestriction)
458 */
459 function userCanExecute( &$user ) {
460 return $user->isAllowed( $this->mRestriction );
461 }
462
463 /**
464 * Output an error message telling the user what access level they have to have
465 */
466 function displayRestrictionError() {
467 global $wgOut;
468 $wgOut->permissionRequired( $this->mRestriction );
469 }
470
471 /**
472 * Sets headers - this should be called from the execute() method of all derived classes!
473 */
474 function setHeaders() {
475 global $wgOut;
476 $wgOut->setArticleRelated( false );
477 $wgOut->setRobotPolicy( "noindex,nofollow" );
478 $wgOut->setPageTitle( $this->getDescription() );
479 }
480
481 /**
482 * Default execute method
483 * Checks user permissions, calls the function given in mFunction
484 */
485 function execute( $par ) {
486 global $wgUser;
487
488 $this->setHeaders();
489
490 if ( $this->userCanExecute( $wgUser ) ) {
491 $func = $this->mFunction;
492 // only load file if the function does not exist
493 if(!function_exists($func) and $this->mFile) {
494 require_once( $this->mFile );
495 }
496 if ( wfRunHooks( 'SpecialPageExecuteBeforeHeader', array( &$this, &$par, &$func ) ) )
497 $this->outputHeader();
498 if ( ! wfRunHooks( 'SpecialPageExecuteBeforePage', array( &$this, &$par, &$func ) ) )
499 return;
500 $func( $par, $this );
501 if ( ! wfRunHooks( 'SpecialPageExecuteAfterPage', array( &$this, &$par, &$func ) ) )
502 return;
503 } else {
504 $this->displayRestrictionError();
505 }
506 }
507
508 function outputHeader() {
509 global $wgOut, $wgContLang;
510
511 $msg = $wgContLang->lc( $this->name() ) . '-summary';
512 $out = wfMsg( $msg );
513 if ( ! wfEmptyMsg( $msg, $out ) and $out !== '' and ! $this->including() )
514 $wgOut->addWikiText( $out );
515
516 }
517
518 # Returns the name that goes in the <h1> in the special page itself, and also the name that
519 # will be listed in Special:Specialpages
520 #
521 # Derived classes can override this, but usually it is easier to keep the default behaviour.
522 # Messages can be added at run-time, see MessageCache.php
523 function getDescription() {
524 return wfMsg( strtolower( $this->mName ) );
525 }
526
527 /**
528 * Get a self-referential title object
529 */
530 function getTitle() {
531 return Title::makeTitle( NS_SPECIAL, $this->mName );
532 }
533
534 /**
535 * Set whether this page is listed in Special:Specialpages, at run-time
536 */
537 function setListed( $listed ) {
538 return wfSetVar( $this->mListed, $listed );
539 }
540
541 }
542
543 /**
544 * Shortcut to construct a special page which is unlisted by default
545 * @package MediaWiki
546 */
547 class UnlistedSpecialPage extends SpecialPage
548 {
549 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
550 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
551 }
552 }
553
554 /**
555 * Shortcut to construct an includable special page
556 * @package MediaWiki
557 */
558 class IncludableSpecialPage extends SpecialPage
559 {
560 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
561 SpecialPage::SpecialPage( $name, $restriction, $listed, $function, $file, true );
562 }
563 }
564 ?>