* Fix #908: links to Special: pages should check for existence,
[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 * @addtogroup SpecialPage
21 */
22
23 /**
24 * @access private
25 */
26
27 /**
28 * Parent special page class, also static functions for handling the special
29 * page list.
30 * @addtogroup SpecialPage
31 */
32 class SpecialPage
33 {
34 /**#@+
35 * @access private
36 */
37 /**
38 * The canonical name of this special page
39 * Also used for the default <h1> heading, @see getDescription()
40 */
41 var $mName;
42 /**
43 * The local name of this special page
44 */
45 var $mLocalName;
46 /**
47 * Minimum user level required to access this page, or "" for anyone.
48 * Also used to categorise the pages in Special:Specialpages
49 */
50 var $mRestriction;
51 /**
52 * Listed in Special:Specialpages?
53 */
54 var $mListed;
55 /**
56 * Function name called by the default execute()
57 */
58 var $mFunction;
59 /**
60 * File which needs to be included before the function above can be called
61 */
62 var $mFile;
63 /**
64 * Whether or not this special page is being included from an article
65 */
66 var $mIncluding;
67 /**
68 * Whether the special page can be included in an article
69 */
70 var $mIncludable;
71 /**
72 * Query parameters that can be passed through redirects
73 */
74 var $mAllowedRedirectParams = array();
75
76 static public $mList = array(
77 'DoubleRedirects' => array( 'SpecialPage', 'DoubleRedirects' ),
78 'BrokenRedirects' => array( 'SpecialPage', 'BrokenRedirects' ),
79 'Disambiguations' => array( 'SpecialPage', 'Disambiguations' ),
80
81 'Userlogin' => array( 'SpecialPage', 'Userlogin' ),
82 'Userlogout' => array( 'UnlistedSpecialPage', 'Userlogout' ),
83 'Preferences' => array( 'SpecialPage', 'Preferences' ),
84 'Watchlist' => array( 'SpecialPage', 'Watchlist' ),
85
86 'Recentchanges' => array( 'IncludableSpecialPage', 'Recentchanges' ),
87 'Upload' => array( 'SpecialPage', 'Upload' ),
88 'Imagelist' => array( 'SpecialPage', 'Imagelist' ),
89 'Newimages' => array( 'IncludableSpecialPage', 'Newimages' ),
90 'Listusers' => array( 'SpecialPage', 'Listusers' ),
91 'Statistics' => array( 'SpecialPage', 'Statistics' ),
92 'Randompage' => array( 'SpecialPage', 'Randompage' ),
93 'Lonelypages' => array( 'SpecialPage', 'Lonelypages' ),
94 'Uncategorizedpages' => array( 'SpecialPage', 'Uncategorizedpages' ),
95 'Uncategorizedcategories' => array( 'SpecialPage', 'Uncategorizedcategories' ),
96 'Uncategorizedimages' => array( 'SpecialPage', 'Uncategorizedimages' ),
97 'Unusedcategories' => array( 'SpecialPage', 'Unusedcategories' ),
98 'Unusedimages' => array( 'SpecialPage', 'Unusedimages' ),
99 'Wantedpages' => array( 'IncludableSpecialPage', 'Wantedpages' ),
100 'Wantedcategories' => array( 'SpecialPage', 'Wantedcategories' ),
101 'Mostlinked' => array( 'SpecialPage', 'Mostlinked' ),
102 'Mostlinkedcategories' => array( 'SpecialPage', 'Mostlinkedcategories' ),
103 'Mostcategories' => array( 'SpecialPage', 'Mostcategories' ),
104 'Mostimages' => array( 'SpecialPage', 'Mostimages' ),
105 'Mostrevisions' => array( 'SpecialPage', 'Mostrevisions' ),
106 'Fewestrevisions' => array( 'SpecialPage', 'Fewestrevisions' ),
107 'Shortpages' => array( 'SpecialPage', 'Shortpages' ),
108 'Longpages' => array( 'SpecialPage', 'Longpages' ),
109 'Newpages' => array( 'IncludableSpecialPage', 'Newpages' ),
110 'Ancientpages' => array( 'SpecialPage', 'Ancientpages' ),
111 'Deadendpages' => array( 'SpecialPage', 'Deadendpages' ),
112 'Protectedpages' => array( 'SpecialPage', 'Protectedpages' ),
113 'Allpages' => array( 'IncludableSpecialPage', 'Allpages' ),
114 'Prefixindex' => array( 'IncludableSpecialPage', 'Prefixindex' ) ,
115 'Ipblocklist' => array( 'SpecialPage', 'Ipblocklist' ),
116 'Specialpages' => array( 'UnlistedSpecialPage', 'Specialpages' ),
117 'Contributions' => array( 'SpecialPage', 'Contributions' ),
118 'Emailuser' => array( 'UnlistedSpecialPage', 'Emailuser' ),
119 'Whatlinkshere' => array( 'UnlistedSpecialPage', 'Whatlinkshere' ),
120 'Recentchangeslinked' => array( 'UnlistedSpecialPage', 'Recentchangeslinked' ),
121 'Movepage' => array( 'UnlistedSpecialPage', 'Movepage' ),
122 'Blockme' => array( 'UnlistedSpecialPage', 'Blockme' ),
123 'Resetpass' => array( 'UnlistedSpecialPage', 'Resetpass' ),
124 'Booksources' => 'SpecialBookSources',
125 'Categories' => array( 'SpecialPage', 'Categories' ),
126 'Export' => array( 'SpecialPage', 'Export' ),
127 'Version' => array( 'SpecialPage', 'Version' ),
128 'Allmessages' => array( 'SpecialPage', 'Allmessages' ),
129 'Log' => array( 'SpecialPage', 'Log' ),
130 'Blockip' => array( 'SpecialPage', 'Blockip', 'block' ),
131 'Undelete' => array( 'SpecialPage', 'Undelete', 'deletedhistory' ),
132 'Import' => array( 'SpecialPage', "Import", 'import' ),
133 'Lockdb' => array( 'SpecialPage', 'Lockdb', 'siteadmin' ),
134 'Unlockdb' => array( 'SpecialPage', 'Unlockdb', 'siteadmin' ),
135 'Userrights' => array( 'SpecialPage', 'Userrights', 'userrights' ),
136 'MIMEsearch' => array( 'SpecialPage', 'MIMEsearch' ),
137 'Unwatchedpages' => array( 'SpecialPage', 'Unwatchedpages', 'unwatchedpages' ),
138 'Listredirects' => array( 'SpecialPage', 'Listredirects' ),
139 'Revisiondelete' => array( 'SpecialPage', 'Revisiondelete', 'deleterevision' ),
140 'Unusedtemplates' => array( 'SpecialPage', 'Unusedtemplates' ),
141 'Randomredirect' => array( 'SpecialPage', 'Randomredirect' ),
142 'Withoutinterwiki' => array( 'SpecialPage', 'Withoutinterwiki' ),
143
144 'Mypage' => array( 'SpecialMypage' ),
145 'Mytalk' => array( 'SpecialMytalk' ),
146 'Mycontributions' => array( 'SpecialMycontributions' ),
147 'Listadmins' => array( 'SpecialRedirectToSpecial', 'Listadmins', 'Listusers', 'sysop' ),
148 );
149
150 static public $mAliases;
151 static public $mListInitialised = false;
152
153 /**#@-*/
154
155 /**
156 * Initialise the special page list
157 * This must be called before accessing SpecialPage::$mList
158 */
159 static function initList() {
160 global $wgSpecialPages;
161 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
162
163 if ( self::$mListInitialised ) {
164 return;
165 }
166 wfProfileIn( __METHOD__ );
167
168 # Better to set this now, to avoid infinite recursion in carelessly written hooks
169 self::$mListInitialised = true;
170
171 if( !$wgDisableCounters ) {
172 self::$mList['Popularpages'] = array( 'SpecialPage', 'Popularpages' );
173 }
174
175 if( !$wgDisableInternalSearch ) {
176 self::$mList['Search'] = array( 'SpecialPage', 'Search' );
177 }
178
179 if( $wgEmailAuthentication ) {
180 self::$mList['Confirmemail'] = array( 'UnlistedSpecialPage', 'Confirmemail' );
181 }
182
183 # Add extension special pages
184 self::$mList = array_merge( self::$mList, $wgSpecialPages );
185
186 # Run hooks
187 # This hook can be used to remove undesired built-in special pages
188 wfRunHooks( 'SpecialPage_initList', array( &self::$mList ) );
189 wfProfileOut( __METHOD__ );
190 }
191
192 static function initAliasList() {
193 if ( !is_null( self::$mAliases ) ) {
194 return;
195 }
196
197 global $wgContLang;
198 $aliases = $wgContLang->getSpecialPageAliases();
199 $missingPages = self::$mList;
200 self::$mAliases = array();
201 foreach ( $aliases as $realName => $aliasList ) {
202 foreach ( $aliasList as $alias ) {
203 self::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
204 }
205 unset( $missingPages[$realName] );
206 }
207 foreach ( $missingPages as $name => $stuff ) {
208 self::$mAliases[$wgContLang->caseFold( $name )] = $name;
209 }
210 }
211
212 /**
213 * Given a special page alias, return the special page name.
214 * Returns false if there is no such alias.
215 */
216 static function resolveAlias( $alias ) {
217 global $wgContLang;
218
219 if ( !self::$mListInitialised ) self::initList();
220 if ( is_null( self::$mAliases ) ) self::initAliasList();
221 $caseFoldedAlias = $wgContLang->caseFold( $alias );
222 if ( isset( self::$mAliases[$caseFoldedAlias] ) ) {
223 return self::$mAliases[$caseFoldedAlias];
224 } else {
225 return false;
226 }
227 }
228
229 /**
230 * Given a special page name with a possible subpage, return an array
231 * where the first element is the special page name and the second is the
232 * subpage.
233 */
234 static function resolveAliasWithSubpage( $alias ) {
235 $bits = explode( '/', $alias, 2 );
236 $name = self::resolveAlias( $bits[0] );
237 if( !isset( $bits[1] ) ) { // bug 2087
238 $par = NULL;
239 } else {
240 $par = $bits[1];
241 }
242 return array( $name, $par );
243 }
244
245 /**
246 * Add a page to the list of valid special pages. This used to be the preferred
247 * method for adding special pages in extensions. It's now suggested that you add
248 * an associative record to $wgSpecialPages. This avoids autoloading SpecialPage.
249 *
250 * @param mixed $page Must either be an array specifying a class name and
251 * constructor parameters, or an object. The object,
252 * when constructed, must have an execute() method which
253 * sends HTML to $wgOut.
254 * @static
255 */
256 static function addPage( &$page ) {
257 if ( !self::$mListInitialised ) {
258 self::initList();
259 }
260 self::$mList[$page->mName] = $page;
261 }
262
263 /**
264 * Remove a special page from the list
265 * Formerly used to disable expensive or dangerous special pages. The
266 * preferred method is now to add a SpecialPage_initList hook.
267 *
268 * @static
269 */
270 static function removePage( $name ) {
271 if ( !self::$mListInitialised ) {
272 self::initList();
273 }
274 unset( self::$mList[$name] );
275 }
276
277 /**
278 * Check if a given name exist as a special page or as a special page alias
279 * @param $name string: name of a special page
280 * @return boolean: true if a special page exists with this name
281 */
282 static function exists( $name ) {
283 if ( !self::$mListInitialised ) {
284 self::initList();
285 }
286 if( !self::$mAliases ) {
287 self::initAliasList();
288 }
289
290 # Remove special pages inline parameters:
291 $bits = explode( '/', $name );
292 $name = $bits[0];
293
294 return
295 array_key_exists( $name, self::$mList )
296 or array_key_exists( $name, self::$mAliases )
297 ;
298 }
299
300 /**
301 * Find the object with a given name and return it (or NULL)
302 * @static
303 * @param string $name
304 */
305 static function getPage( $name ) {
306 if ( !self::$mListInitialised ) {
307 self::initList();
308 }
309 if ( array_key_exists( $name, self::$mList ) ) {
310 $rec = self::$mList[$name];
311 if ( is_string( $rec ) ) {
312 $className = $rec;
313 self::$mList[$name] = new $className;
314 } elseif ( is_array( $rec ) ) {
315 $className = array_shift( $rec );
316 self::$mList[$name] = wfCreateObject( $className, $rec );
317 }
318 return self::$mList[$name];
319 } else {
320 return NULL;
321 }
322 }
323
324 /**
325 * Get a special page with a given localised name, or NULL if there
326 * is no such special page.
327 */
328 static function getPageByAlias( $alias ) {
329 $realName = self::resolveAlias( $alias );
330 if ( $realName ) {
331 return self::getPage( $realName );
332 } else {
333 return NULL;
334 }
335 }
336
337 /**
338 * Return categorised listable special pages for all users
339 * @static
340 */
341 static function getRegularPages() {
342 if ( !self::$mListInitialised ) {
343 self::initList();
344 }
345 $pages = array();
346
347 foreach ( self::$mList as $name => $rec ) {
348 $page = self::getPage( $name );
349 if ( $page->isListed() && $page->getRestriction() == '' ) {
350 $pages[$name] = $page;
351 }
352 }
353 return $pages;
354 }
355
356 /**
357 * Return categorised listable special pages which are available
358 * for the current user, but not for everyone
359 * @static
360 */
361 static function getRestrictedPages() {
362 global $wgUser;
363 if ( !self::$mListInitialised ) {
364 self::initList();
365 }
366 $pages = array();
367
368 foreach ( self::$mList as $name => $rec ) {
369 $page = self::getPage( $name );
370 if ( $page->isListed() ) {
371 $restriction = $page->getRestriction();
372 if ( $restriction != '' && $wgUser->isAllowed( $restriction ) ) {
373 $pages[$name] = $page;
374 }
375 }
376 }
377 return $pages;
378 }
379
380 /**
381 * Execute a special page path.
382 * The path may contain parameters, e.g. Special:Name/Params
383 * Extracts the special page name and call the execute method, passing the parameters
384 *
385 * Returns a title object if the page is redirected, false if there was no such special
386 * page, and true if it was successful.
387 *
388 * @param $title a title object
389 * @param $including output is being captured for use in {{special:whatever}}
390 */
391 static function executePath( &$title, $including = false ) {
392 global $wgOut, $wgTitle, $wgRequest;
393 wfProfileIn( __METHOD__ );
394
395 # FIXME: redirects broken due to this call
396 $bits = explode( '/', $title->getDBkey(), 2 );
397 $name = $bits[0];
398 if( !isset( $bits[1] ) ) { // bug 2087
399 $par = NULL;
400 } else {
401 $par = $bits[1];
402 }
403 $page = SpecialPage::getPageByAlias( $name );
404
405 # Nonexistent?
406 if ( !$page ) {
407 if ( !$including ) {
408 $wgOut->setArticleRelated( false );
409 $wgOut->setRobotpolicy( 'noindex,nofollow' );
410 $wgOut->setStatusCode( 404 );
411 $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
412 }
413 wfProfileOut( __METHOD__ );
414 return false;
415 }
416
417 # Check for redirect
418 if ( !$including ) {
419 $redirect = $page->getRedirect( $par );
420 if ( $redirect ) {
421 $query = $page->getRedirectQuery();
422 $url = $redirect->getFullUrl( $query );
423 $wgOut->redirect( $url );
424 wfProfileOut( __METHOD__ );
425 return $redirect;
426 }
427 }
428
429 # Redirect to canonical alias for GET commands
430 # Not for POST, we'd lose the post data, so it's best to just distribute
431 # the request. Such POST requests are possible for old extensions that
432 # generate self-links without being aware that their default name has
433 # changed.
434 if ( !$including && $name != $page->getLocalName() && !$wgRequest->wasPosted() ) {
435 $query = $_GET;
436 unset( $query['title'] );
437 $query = wfArrayToCGI( $query );
438 $title = $page->getTitle( $par );
439 $url = $title->getFullUrl( $query );
440 $wgOut->redirect( $url );
441 wfProfileOut( __METHOD__ );
442 return $redirect;
443 }
444
445 if ( $including && !$page->includable() ) {
446 wfProfileOut( __METHOD__ );
447 return false;
448 } elseif ( !$including ) {
449 $wgTitle = $page->getTitle();
450 }
451 $page->including( $including );
452
453 // Execute special page
454 $profName = 'Special:' . $page->getName();
455 wfProfileIn( $profName );
456 $page->execute( $par );
457 wfProfileOut( $profName );
458 wfProfileOut( __METHOD__ );
459 return true;
460 }
461
462 /**
463 * Just like executePath() except it returns the HTML instead of outputting it
464 * Returns false if there was no such special page, or a title object if it was
465 * a redirect.
466 * @static
467 */
468 static function capturePath( &$title ) {
469 global $wgOut, $wgTitle;
470
471 $oldTitle = $wgTitle;
472 $oldOut = $wgOut;
473 $wgOut = new OutputPage;
474
475 $ret = SpecialPage::executePath( $title, true );
476 if ( $ret === true ) {
477 $ret = $wgOut->getHTML();
478 }
479 $wgTitle = $oldTitle;
480 $wgOut = $oldOut;
481 return $ret;
482 }
483
484 /**
485 * Get the local name for a specified canonical name
486 */
487 static function getLocalNameFor( $name, $subpage = false ) {
488 global $wgContLang;
489 $aliases = $wgContLang->getSpecialPageAliases();
490 if ( isset( $aliases[$name][0] ) ) {
491 $name = $aliases[$name][0];
492 }
493 if ( $subpage !== false && !is_null( $subpage ) ) {
494 $name = "$name/$subpage";
495 }
496 return $name;
497 }
498
499 /**
500 * Get a localised Title object for a specified special page name
501 */
502 static function getTitleFor( $name, $subpage = false ) {
503 $name = self::getLocalNameFor( $name, $subpage );
504 if ( $name ) {
505 return Title::makeTitle( NS_SPECIAL, $name );
506 } else {
507 throw new MWException( "Invalid special page name \"$name\"" );
508 }
509 }
510
511 /**
512 * Get a localised Title object for a page name with a possibly unvalidated subpage
513 */
514 static function getSafeTitleFor( $name, $subpage = false ) {
515 $name = self::getLocalNameFor( $name, $subpage );
516 if ( $name ) {
517 return Title::makeTitleSafe( NS_SPECIAL, $name );
518 } else {
519 return null;
520 }
521 }
522
523 /**
524 * Get a title for a given alias
525 * @return Title or null if there is no such alias
526 */
527 static function getTitleForAlias( $alias ) {
528 $name = self::resolveAlias( $alias );
529 if ( $name ) {
530 return self::getTitleFor( $name );
531 } else {
532 return null;
533 }
534 }
535
536 /**
537 * Default constructor for special pages
538 * Derivative classes should call this from their constructor
539 * Note that if the user does not have the required level, an error message will
540 * be displayed by the default execute() method, without the global function ever
541 * being called.
542 *
543 * If you override execute(), you can recover the default behaviour with userCanExecute()
544 * and displayRestrictionError()
545 *
546 * @param string $name Name of the special page, as seen in links and URLs
547 * @param string $restriction User right required, e.g. "block" or "delete"
548 * @param boolean $listed Whether the page is listed in Special:Specialpages
549 * @param string $function Function called by execute(). By default it is constructed from $name
550 * @param string $file File which is included by execute(). It is also constructed from $name by default
551 */
552 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
553 $this->mName = $name;
554 $this->mRestriction = $restriction;
555 $this->mListed = $listed;
556 $this->mIncludable = $includable;
557 if ( $function == false ) {
558 $this->mFunction = 'wfSpecial'.$name;
559 } else {
560 $this->mFunction = $function;
561 }
562 if ( $file === 'default' ) {
563 $this->mFile = dirname(__FILE__) . "/Special{$name}.php";
564 } else {
565 $this->mFile = $file;
566 }
567 }
568
569 /**#@+
570 * Accessor
571 *
572 * @deprecated
573 */
574 function getName() { return $this->mName; }
575 function getRestriction() { return $this->mRestriction; }
576 function getFile() { return $this->mFile; }
577 function isListed() { return $this->mListed; }
578 /**#@-*/
579
580 /**#@+
581 * Accessor and mutator
582 */
583 function name( $x = NULL ) { return wfSetVar( $this->mName, $x ); }
584 function restrictions( $x = NULL) { return wfSetVar( $this->mRestrictions, $x ); }
585 function listed( $x = NULL) { return wfSetVar( $this->mListed, $x ); }
586 function func( $x = NULL) { return wfSetVar( $this->mFunction, $x ); }
587 function file( $x = NULL) { return wfSetVar( $this->mFile, $x ); }
588 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable, $x ); }
589 function including( $x = NULL ) { return wfSetVar( $this->mIncluding, $x ); }
590 /**#@-*/
591
592 /**
593 * Get the localised name of the special page
594 */
595 function getLocalName() {
596 if ( !isset( $this->mLocalName ) ) {
597 $this->mLocalName = self::getLocalNameFor( $this->mName );
598 }
599 return $this->mLocalName;
600 }
601
602 /**
603 * Checks if the given user (identified by an object) can execute this
604 * special page (as defined by $mRestriction)
605 */
606 function userCanExecute( &$user ) {
607 return $user->isAllowed( $this->mRestriction );
608 }
609
610 /**
611 * Output an error message telling the user what access level they have to have
612 */
613 function displayRestrictionError() {
614 global $wgOut;
615 $wgOut->permissionRequired( $this->mRestriction );
616 }
617
618 /**
619 * Sets headers - this should be called from the execute() method of all derived classes!
620 */
621 function setHeaders() {
622 global $wgOut;
623 $wgOut->setArticleRelated( false );
624 $wgOut->setRobotPolicy( "noindex,nofollow" );
625 $wgOut->setPageTitle( $this->getDescription() );
626 }
627
628 /**
629 * Default execute method
630 * Checks user permissions, calls the function given in mFunction
631 *
632 * This may be overridden by subclasses.
633 */
634 function execute( $par ) {
635 global $wgUser;
636
637 $this->setHeaders();
638
639 if ( $this->userCanExecute( $wgUser ) ) {
640 $func = $this->mFunction;
641 // only load file if the function does not exist
642 if(!function_exists($func) and $this->mFile) {
643 require_once( $this->mFile );
644 }
645 # FIXME: these hooks are broken for extensions and anything else that subclasses SpecialPage.
646 if ( wfRunHooks( 'SpecialPageExecuteBeforeHeader', array( &$this, &$par, &$func ) ) )
647 $this->outputHeader();
648 if ( ! wfRunHooks( 'SpecialPageExecuteBeforePage', array( &$this, &$par, &$func ) ) )
649 return;
650 $func( $par, $this );
651 if ( ! wfRunHooks( 'SpecialPageExecuteAfterPage', array( &$this, &$par, &$func ) ) )
652 return;
653 } else {
654 $this->displayRestrictionError();
655 }
656 }
657
658 function outputHeader() {
659 global $wgOut, $wgContLang;
660
661 $msg = $wgContLang->lc( $this->name() ) . '-summary';
662 $out = wfMsg( $msg );
663 if ( ! wfEmptyMsg( $msg, $out ) and $out !== '' and ! $this->including() )
664 $wgOut->addWikiText( $out );
665
666 }
667
668 # Returns the name that goes in the <h1> in the special page itself, and also the name that
669 # will be listed in Special:Specialpages
670 #
671 # Derived classes can override this, but usually it is easier to keep the default behaviour.
672 # Messages can be added at run-time, see MessageCache.php
673 function getDescription() {
674 return wfMsg( strtolower( $this->mName ) );
675 }
676
677 /**
678 * Get a self-referential title object
679 */
680 function getTitle( $subpage = false) {
681 return self::getTitleFor( $this->mName, $subpage );
682 }
683
684 /**
685 * Set whether this page is listed in Special:Specialpages, at run-time
686 */
687 function setListed( $listed ) {
688 return wfSetVar( $this->mListed, $listed );
689 }
690
691 /**
692 * If the special page is a redirect, then get the Title object it redirects to.
693 * False otherwise.
694 */
695 function getRedirect( $subpage ) {
696 return false;
697 }
698
699 /**
700 * Return part of the request string for a special redirect page
701 * This allows passing, e.g. action=history to Special:Mypage, etc.
702 *
703 * @return string
704 */
705 function getRedirectQuery() {
706 global $wgRequest;
707 $params = array();
708 foreach( $this->mAllowedRedirectParams as $arg ) {
709 if( $val = $wgRequest->getVal( $arg, false ) )
710 $params[] = $arg . '=' . $val;
711 }
712
713 return count( $params ) ? implode( '&', $params ) : false;
714 }
715 }
716
717 /**
718 * Shortcut to construct a special page which is unlisted by default
719 * @addtogroup SpecialPage
720 */
721 class UnlistedSpecialPage extends SpecialPage
722 {
723 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
724 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
725 }
726 }
727
728 /**
729 * Shortcut to construct an includable special page
730 * @addtogroup SpecialPage
731 */
732 class IncludableSpecialPage extends SpecialPage
733 {
734 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
735 SpecialPage::SpecialPage( $name, $restriction, $listed, $function, $file, true );
736 }
737 }
738
739 /**
740 * Shortcut to construct a special page alias.
741 * @addtogroup SpecialPage
742 */
743 class SpecialRedirectToSpecial extends UnlistedSpecialPage {
744 var $redirName, $redirSubpage;
745
746 function __construct( $name, $redirName, $redirSubpage = false, $redirectParams = array() ) {
747 parent::__construct( $name );
748 $this->redirName = $redirName;
749 $this->redirSubpage = $redirSubpage;
750 $this->mAllowedRedirectParams = $redirectParams;
751 }
752
753 function getRedirect( $subpage ) {
754 if ( $this->redirSubpage === false ) {
755 return SpecialPage::getTitleFor( $this->redirName, $subpage );
756 } else {
757 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
758 }
759 }
760 }
761
762 /** SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
763 * are used to get user independant links pointing to the user page, talk
764 * page and list of contributions.
765 * This can let us cache a single copy of any generated content for all
766 * users.
767 */
768
769 /**
770 * Shortcut to construct a special page pointing to current user user's page.
771 * @addtogroup SpecialPage
772 */
773 class SpecialMypage extends UnlistedSpecialPage {
774 function __construct() {
775 parent::__construct( 'Mypage' );
776 $this->mAllowedRedirectParams = array( 'action' );
777 }
778
779 function getRedirect( $subpage ) {
780 global $wgUser;
781 if ( strval( $subpage ) !== '' ) {
782 return Title::makeTitle( NS_USER, $wgUser->getName() . '/' . $subpage );
783 } else {
784 return Title::makeTitle( NS_USER, $wgUser->getName() );
785 }
786 }
787 }
788
789 /**
790 * Shortcut to construct a special page pointing to current user talk page.
791 * @addtogroup SpecialPage
792 */
793 class SpecialMytalk extends UnlistedSpecialPage {
794 function __construct() {
795 parent::__construct( 'Mytalk' );
796 $this->mAllowedRedirectParams = array( 'action' );
797 }
798
799 function getRedirect( $subpage ) {
800 global $wgUser;
801 if ( strval( $subpage ) !== '' ) {
802 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() . '/' . $subpage );
803 } else {
804 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() );
805 }
806 }
807 }
808
809 /**
810 * Shortcut to construct a special page pointing to current user contributions.
811 * @addtogroup SpecialPage
812 */
813 class SpecialMycontributions extends UnlistedSpecialPage {
814 function __construct() {
815 parent::__construct( 'Mycontributions' );
816 }
817
818 function getRedirect( $subpage ) {
819 global $wgUser;
820 return SpecialPage::getTitleFor( 'Contributions', $wgUser->getName() );
821 }
822 }
823
824 ?>