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