5a9376e7e9ae8675673a16638c9e092e0c7fc49a
[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 global $wgContLang;
284 if ( !self::$mListInitialised ) {
285 self::initList();
286 }
287 if( !self::$mAliases ) {
288 self::initAliasList();
289 }
290
291 # Remove special pages inline parameters:
292 $bits = explode( '/', $name );
293 $name = $wgContLang->caseFold($bits[0]);
294
295 return
296 array_key_exists( $name, self::$mList )
297 or array_key_exists( $name, self::$mAliases )
298 ;
299 }
300
301 /**
302 * Find the object with a given name and return it (or NULL)
303 * @static
304 * @param string $name
305 */
306 static function getPage( $name ) {
307 if ( !self::$mListInitialised ) {
308 self::initList();
309 }
310 if ( array_key_exists( $name, self::$mList ) ) {
311 $rec = self::$mList[$name];
312 if ( is_string( $rec ) ) {
313 $className = $rec;
314 self::$mList[$name] = new $className;
315 } elseif ( is_array( $rec ) ) {
316 $className = array_shift( $rec );
317 self::$mList[$name] = wfCreateObject( $className, $rec );
318 }
319 return self::$mList[$name];
320 } else {
321 return NULL;
322 }
323 }
324
325 /**
326 * Get a special page with a given localised name, or NULL if there
327 * is no such special page.
328 */
329 static function getPageByAlias( $alias ) {
330 $realName = self::resolveAlias( $alias );
331 if ( $realName ) {
332 return self::getPage( $realName );
333 } else {
334 return NULL;
335 }
336 }
337
338 /**
339 * Return categorised listable special pages for all users
340 * @static
341 */
342 static function getRegularPages() {
343 if ( !self::$mListInitialised ) {
344 self::initList();
345 }
346 $pages = array();
347
348 foreach ( self::$mList as $name => $rec ) {
349 $page = self::getPage( $name );
350 if ( $page->isListed() && $page->getRestriction() == '' ) {
351 $pages[$name] = $page;
352 }
353 }
354 return $pages;
355 }
356
357 /**
358 * Return categorised listable special pages which are available
359 * for the current user, but not for everyone
360 * @static
361 */
362 static function getRestrictedPages() {
363 global $wgUser;
364 if ( !self::$mListInitialised ) {
365 self::initList();
366 }
367 $pages = array();
368
369 foreach ( self::$mList as $name => $rec ) {
370 $page = self::getPage( $name );
371 if ( $page->isListed() ) {
372 $restriction = $page->getRestriction();
373 if ( $restriction != '' && $wgUser->isAllowed( $restriction ) ) {
374 $pages[$name] = $page;
375 }
376 }
377 }
378 return $pages;
379 }
380
381 /**
382 * Execute a special page path.
383 * The path may contain parameters, e.g. Special:Name/Params
384 * Extracts the special page name and call the execute method, passing the parameters
385 *
386 * Returns a title object if the page is redirected, false if there was no such special
387 * page, and true if it was successful.
388 *
389 * @param $title a title object
390 * @param $including output is being captured for use in {{special:whatever}}
391 */
392 static function executePath( &$title, $including = false ) {
393 global $wgOut, $wgTitle, $wgRequest;
394 wfProfileIn( __METHOD__ );
395
396 # FIXME: redirects broken due to this call
397 $bits = explode( '/', $title->getDBkey(), 2 );
398 $name = $bits[0];
399 if( !isset( $bits[1] ) ) { // bug 2087
400 $par = NULL;
401 } else {
402 $par = $bits[1];
403 }
404 $page = SpecialPage::getPageByAlias( $name );
405
406 # Nonexistent?
407 if ( !$page ) {
408 if ( !$including ) {
409 $wgOut->setArticleRelated( false );
410 $wgOut->setRobotpolicy( 'noindex,nofollow' );
411 $wgOut->setStatusCode( 404 );
412 $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
413 }
414 wfProfileOut( __METHOD__ );
415 return false;
416 }
417
418 # Check for redirect
419 if ( !$including ) {
420 $redirect = $page->getRedirect( $par );
421 if ( $redirect ) {
422 $query = $page->getRedirectQuery();
423 $url = $redirect->getFullUrl( $query );
424 $wgOut->redirect( $url );
425 wfProfileOut( __METHOD__ );
426 return $redirect;
427 }
428 }
429
430 # Redirect to canonical alias for GET commands
431 # Not for POST, we'd lose the post data, so it's best to just distribute
432 # the request. Such POST requests are possible for old extensions that
433 # generate self-links without being aware that their default name has
434 # changed.
435 if ( !$including && $name != $page->getLocalName() && !$wgRequest->wasPosted() ) {
436 $query = $_GET;
437 unset( $query['title'] );
438 $query = wfArrayToCGI( $query );
439 $title = $page->getTitle( $par );
440 $url = $title->getFullUrl( $query );
441 $wgOut->redirect( $url );
442 wfProfileOut( __METHOD__ );
443 return $redirect;
444 }
445
446 if ( $including && !$page->includable() ) {
447 wfProfileOut( __METHOD__ );
448 return false;
449 } elseif ( !$including ) {
450 $wgTitle = $page->getTitle();
451 }
452 $page->including( $including );
453
454 // Execute special page
455 $profName = 'Special:' . $page->getName();
456 wfProfileIn( $profName );
457 $page->execute( $par );
458 wfProfileOut( $profName );
459 wfProfileOut( __METHOD__ );
460 return true;
461 }
462
463 /**
464 * Just like executePath() except it returns the HTML instead of outputting it
465 * Returns false if there was no such special page, or a title object if it was
466 * a redirect.
467 * @static
468 */
469 static function capturePath( &$title ) {
470 global $wgOut, $wgTitle;
471
472 $oldTitle = $wgTitle;
473 $oldOut = $wgOut;
474 $wgOut = new OutputPage;
475
476 $ret = SpecialPage::executePath( $title, true );
477 if ( $ret === true ) {
478 $ret = $wgOut->getHTML();
479 }
480 $wgTitle = $oldTitle;
481 $wgOut = $oldOut;
482 return $ret;
483 }
484
485 /**
486 * Get the local name for a specified canonical name
487 */
488 static function getLocalNameFor( $name, $subpage = false ) {
489 global $wgContLang;
490 $aliases = $wgContLang->getSpecialPageAliases();
491 if ( isset( $aliases[$name][0] ) ) {
492 $name = $aliases[$name][0];
493 }
494 if ( $subpage !== false && !is_null( $subpage ) ) {
495 $name = "$name/$subpage";
496 }
497 return $name;
498 }
499
500 /**
501 * Get a localised Title object for a specified special page name
502 */
503 static function getTitleFor( $name, $subpage = false ) {
504 $name = self::getLocalNameFor( $name, $subpage );
505 if ( $name ) {
506 return Title::makeTitle( NS_SPECIAL, $name );
507 } else {
508 throw new MWException( "Invalid special page name \"$name\"" );
509 }
510 }
511
512 /**
513 * Get a localised Title object for a page name with a possibly unvalidated subpage
514 */
515 static function getSafeTitleFor( $name, $subpage = false ) {
516 $name = self::getLocalNameFor( $name, $subpage );
517 if ( $name ) {
518 return Title::makeTitleSafe( NS_SPECIAL, $name );
519 } else {
520 return null;
521 }
522 }
523
524 /**
525 * Get a title for a given alias
526 * @return Title or null if there is no such alias
527 */
528 static function getTitleForAlias( $alias ) {
529 $name = self::resolveAlias( $alias );
530 if ( $name ) {
531 return self::getTitleFor( $name );
532 } else {
533 return null;
534 }
535 }
536
537 /**
538 * Default constructor for special pages
539 * Derivative classes should call this from their constructor
540 * Note that if the user does not have the required level, an error message will
541 * be displayed by the default execute() method, without the global function ever
542 * being called.
543 *
544 * If you override execute(), you can recover the default behaviour with userCanExecute()
545 * and displayRestrictionError()
546 *
547 * @param string $name Name of the special page, as seen in links and URLs
548 * @param string $restriction User right required, e.g. "block" or "delete"
549 * @param boolean $listed Whether the page is listed in Special:Specialpages
550 * @param string $function Function called by execute(). By default it is constructed from $name
551 * @param string $file File which is included by execute(). It is also constructed from $name by default
552 */
553 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
554 $this->mName = $name;
555 $this->mRestriction = $restriction;
556 $this->mListed = $listed;
557 $this->mIncludable = $includable;
558 if ( $function == false ) {
559 $this->mFunction = 'wfSpecial'.$name;
560 } else {
561 $this->mFunction = $function;
562 }
563 if ( $file === 'default' ) {
564 $this->mFile = dirname(__FILE__) . "/Special{$name}.php";
565 } else {
566 $this->mFile = $file;
567 }
568 }
569
570 /**#@+
571 * Accessor
572 *
573 * @deprecated
574 */
575 function getName() { return $this->mName; }
576 function getRestriction() { return $this->mRestriction; }
577 function getFile() { return $this->mFile; }
578 function isListed() { return $this->mListed; }
579 /**#@-*/
580
581 /**#@+
582 * Accessor and mutator
583 */
584 function name( $x = NULL ) { return wfSetVar( $this->mName, $x ); }
585 function restrictions( $x = NULL) { return wfSetVar( $this->mRestrictions, $x ); }
586 function listed( $x = NULL) { return wfSetVar( $this->mListed, $x ); }
587 function func( $x = NULL) { return wfSetVar( $this->mFunction, $x ); }
588 function file( $x = NULL) { return wfSetVar( $this->mFile, $x ); }
589 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable, $x ); }
590 function including( $x = NULL ) { return wfSetVar( $this->mIncluding, $x ); }
591 /**#@-*/
592
593 /**
594 * Get the localised name of the special page
595 */
596 function getLocalName() {
597 if ( !isset( $this->mLocalName ) ) {
598 $this->mLocalName = self::getLocalNameFor( $this->mName );
599 }
600 return $this->mLocalName;
601 }
602
603 /**
604 * Checks if the given user (identified by an object) can execute this
605 * special page (as defined by $mRestriction)
606 */
607 function userCanExecute( &$user ) {
608 return $user->isAllowed( $this->mRestriction );
609 }
610
611 /**
612 * Output an error message telling the user what access level they have to have
613 */
614 function displayRestrictionError() {
615 global $wgOut;
616 $wgOut->permissionRequired( $this->mRestriction );
617 }
618
619 /**
620 * Sets headers - this should be called from the execute() method of all derived classes!
621 */
622 function setHeaders() {
623 global $wgOut;
624 $wgOut->setArticleRelated( false );
625 $wgOut->setRobotPolicy( "noindex,nofollow" );
626 $wgOut->setPageTitle( $this->getDescription() );
627 }
628
629 /**
630 * Default execute method
631 * Checks user permissions, calls the function given in mFunction
632 *
633 * This may be overridden by subclasses.
634 */
635 function execute( $par ) {
636 global $wgUser;
637
638 $this->setHeaders();
639
640 if ( $this->userCanExecute( $wgUser ) ) {
641 $func = $this->mFunction;
642 // only load file if the function does not exist
643 if(!function_exists($func) and $this->mFile) {
644 require_once( $this->mFile );
645 }
646 # FIXME: these hooks are broken for extensions and anything else that subclasses SpecialPage.
647 if ( wfRunHooks( 'SpecialPageExecuteBeforeHeader', array( &$this, &$par, &$func ) ) )
648 $this->outputHeader();
649 if ( ! wfRunHooks( 'SpecialPageExecuteBeforePage', array( &$this, &$par, &$func ) ) )
650 return;
651 $func( $par, $this );
652 if ( ! wfRunHooks( 'SpecialPageExecuteAfterPage', array( &$this, &$par, &$func ) ) )
653 return;
654 } else {
655 $this->displayRestrictionError();
656 }
657 }
658
659 function outputHeader() {
660 global $wgOut, $wgContLang;
661
662 $msg = $wgContLang->lc( $this->name() ) . '-summary';
663 $out = wfMsg( $msg );
664 if ( ! wfEmptyMsg( $msg, $out ) and $out !== '' and ! $this->including() )
665 $wgOut->addWikiText( $out );
666
667 }
668
669 # Returns the name that goes in the <h1> in the special page itself, and also the name that
670 # will be listed in Special:Specialpages
671 #
672 # Derived classes can override this, but usually it is easier to keep the default behaviour.
673 # Messages can be added at run-time, see MessageCache.php
674 function getDescription() {
675 return wfMsg( strtolower( $this->mName ) );
676 }
677
678 /**
679 * Get a self-referential title object
680 */
681 function getTitle( $subpage = false) {
682 return self::getTitleFor( $this->mName, $subpage );
683 }
684
685 /**
686 * Set whether this page is listed in Special:Specialpages, at run-time
687 */
688 function setListed( $listed ) {
689 return wfSetVar( $this->mListed, $listed );
690 }
691
692 /**
693 * If the special page is a redirect, then get the Title object it redirects to.
694 * False otherwise.
695 */
696 function getRedirect( $subpage ) {
697 return false;
698 }
699
700 /**
701 * Return part of the request string for a special redirect page
702 * This allows passing, e.g. action=history to Special:Mypage, etc.
703 *
704 * @return string
705 */
706 function getRedirectQuery() {
707 global $wgRequest;
708 $params = array();
709 foreach( $this->mAllowedRedirectParams as $arg ) {
710 if( $val = $wgRequest->getVal( $arg, false ) )
711 $params[] = $arg . '=' . $val;
712 }
713
714 return count( $params ) ? implode( '&', $params ) : false;
715 }
716 }
717
718 /**
719 * Shortcut to construct a special page which is unlisted by default
720 * @addtogroup SpecialPage
721 */
722 class UnlistedSpecialPage extends SpecialPage
723 {
724 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
725 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
726 }
727 }
728
729 /**
730 * Shortcut to construct an includable special page
731 * @addtogroup SpecialPage
732 */
733 class IncludableSpecialPage extends SpecialPage
734 {
735 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
736 SpecialPage::SpecialPage( $name, $restriction, $listed, $function, $file, true );
737 }
738 }
739
740 /**
741 * Shortcut to construct a special page alias.
742 * @addtogroup SpecialPage
743 */
744 class SpecialRedirectToSpecial extends UnlistedSpecialPage {
745 var $redirName, $redirSubpage;
746
747 function __construct( $name, $redirName, $redirSubpage = false, $redirectParams = array() ) {
748 parent::__construct( $name );
749 $this->redirName = $redirName;
750 $this->redirSubpage = $redirSubpage;
751 $this->mAllowedRedirectParams = $redirectParams;
752 }
753
754 function getRedirect( $subpage ) {
755 if ( $this->redirSubpage === false ) {
756 return SpecialPage::getTitleFor( $this->redirName, $subpage );
757 } else {
758 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
759 }
760 }
761 }
762
763 /** SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
764 * are used to get user independant links pointing to the user page, talk
765 * page and list of contributions.
766 * This can let us cache a single copy of any generated content for all
767 * users.
768 */
769
770 /**
771 * Shortcut to construct a special page pointing to current user user's page.
772 * @addtogroup SpecialPage
773 */
774 class SpecialMypage extends UnlistedSpecialPage {
775 function __construct() {
776 parent::__construct( 'Mypage' );
777 $this->mAllowedRedirectParams = array( 'action' );
778 }
779
780 function getRedirect( $subpage ) {
781 global $wgUser;
782 if ( strval( $subpage ) !== '' ) {
783 return Title::makeTitle( NS_USER, $wgUser->getName() . '/' . $subpage );
784 } else {
785 return Title::makeTitle( NS_USER, $wgUser->getName() );
786 }
787 }
788 }
789
790 /**
791 * Shortcut to construct a special page pointing to current user talk page.
792 * @addtogroup SpecialPage
793 */
794 class SpecialMytalk extends UnlistedSpecialPage {
795 function __construct() {
796 parent::__construct( 'Mytalk' );
797 $this->mAllowedRedirectParams = array( 'action' );
798 }
799
800 function getRedirect( $subpage ) {
801 global $wgUser;
802 if ( strval( $subpage ) !== '' ) {
803 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() . '/' . $subpage );
804 } else {
805 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() );
806 }
807 }
808 }
809
810 /**
811 * Shortcut to construct a special page pointing to current user contributions.
812 * @addtogroup SpecialPage
813 */
814 class SpecialMycontributions extends UnlistedSpecialPage {
815 function __construct() {
816 parent::__construct( 'Mycontributions' );
817 }
818
819 function getRedirect( $subpage ) {
820 global $wgUser;
821 return SpecialPage::getTitleFor( 'Contributions', $wgUser->getName() );
822 }
823 }
824
825 ?>