d5e698565feef594e5dbc4706718e27cc63f389e
[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 * @file
21 * @ingroup SpecialPage
22 * @defgroup SpecialPage SpecialPage
23 */
24
25 /**
26 * Parent special page class, also static functions for handling the special
27 * page list.
28 * @ingroup SpecialPage
29 */
30 class SpecialPage {
31
32 // The canonical name of this special page
33 // Also used for the default <h1> heading, @see getDescription()
34 /*private*/ var $mName;
35
36 // The local name of this special page
37 private $mLocalName;
38
39 // Minimum user level required to access this page, or "" for anyone.
40 // Also used to categorise the pages in Special:Specialpages
41 private $mRestriction;
42
43 // Listed in Special:Specialpages?
44 private $mListed;
45
46 // Function name called by the default execute()
47 private $mFunction;
48
49 // File which needs to be included before the function above can be called
50 private $mFile;
51
52 // Whether or not this special page is being included from an article
53 protected $mIncluding;
54
55 // Whether the special page can be included in an article
56 protected $mIncludable;
57
58 /**
59 * Current request context
60 * @var RequestContext
61 */
62 protected $mContext;
63
64 /**
65 * Initialise the special page list
66 * This must be called before accessing SpecialPage::$mList
67 * @deprecated since 1.18
68 */
69 static function initList() {
70 // Noop
71 }
72
73 /**
74 * @deprecated since 1.18
75 */
76 static function initAliasList() {
77 // Noop
78 }
79
80 /**
81 * Given a special page alias, return the special page name.
82 * Returns false if there is no such alias.
83 *
84 * @param $alias String
85 * @return String or false
86 * @deprecated since 1.18 call SpecialPageFactory method directly
87 */
88 static function resolveAlias( $alias ) {
89 list( $name, /*...*/ ) = SpecialPageFactory::resolveAlias( $alias );
90 return $name;
91 }
92
93 /**
94 * Given a special page name with a possible subpage, return an array
95 * where the first element is the special page name and the second is the
96 * subpage.
97 *
98 * @param $alias String
99 * @return Array
100 * @deprecated since 1.18 call SpecialPageFactory method directly
101 */
102 static function resolveAliasWithSubpage( $alias ) {
103 return SpecialPageFactory::resolveAlias( $alias );
104 }
105
106 /**
107 * Add a page to the list of valid special pages. This used to be the preferred
108 * method for adding special pages in extensions. It's now suggested that you add
109 * an associative record to $wgSpecialPages. This avoids autoloading SpecialPage.
110 *
111 * @param $page SpecialPage
112 * @deprecated since 1.7, warnings in 1.17, might be removed in 1.20
113 */
114 static function addPage( &$page ) {
115 wfDeprecated( __METHOD__ );
116 SpecialPageFactory::getList()->{$page->mName} = $page;
117 }
118
119 /**
120 * Add a page to a certain display group for Special:SpecialPages
121 *
122 * @param $page Mixed: SpecialPage or string
123 * @param $group String
124 * @deprecated since 1.18 call SpecialPageFactory method directly
125 */
126 static function setGroup( $page, $group ) {
127 return SpecialPageFactory::setGroup( $page, $group );
128 }
129
130 /**
131 * Add a page to a certain display group for Special:SpecialPages
132 *
133 * @param $page SpecialPage
134 * @deprecated since 1.18 call SpecialPageFactory method directly
135 */
136 static function getGroup( &$page ) {
137 return SpecialPageFactory::getGroup( $page );
138 }
139
140 /**
141 * Remove a special page from the list
142 * Formerly used to disable expensive or dangerous special pages. The
143 * preferred method is now to add a SpecialPage_initList hook.
144 * @deprecated since 1.18
145 */
146 static function removePage( $name ) {
147 unset( SpecialPageFactory::getList()->$name );
148 }
149
150 /**
151 * Check if a given name exist as a special page or as a special page alias
152 *
153 * @param $name String: name of a special page
154 * @return Boolean: true if a special page exists with this name
155 * @deprecated since 1.18 call SpecialPageFactory method directly
156 */
157 static function exists( $name ) {
158 return SpecialPageFactory::exists( $name );
159 }
160
161 /**
162 * Find the object with a given name and return it (or NULL)
163 *
164 * @param $name String
165 * @return SpecialPage object or null if the page doesn't exist
166 * @deprecated since 1.18 call SpecialPageFactory method directly
167 */
168 static function getPage( $name ) {
169 return SpecialPageFactory::getPage( $name );
170 }
171
172 /**
173 * Get a special page with a given localised name, or NULL if there
174 * is no such special page.
175 *
176 * @return SpecialPage object or null if the page doesn't exist
177 * @deprecated since 1.18 call SpecialPageFactory method directly
178 */
179 static function getPageByAlias( $alias ) {
180 return SpecialPageFactory::getPage( $alias );
181 }
182
183 /**
184 * Return categorised listable special pages which are available
185 * for the current user, and everyone.
186 *
187 * @return Associative array mapping page's name to its SpecialPage object
188 * @deprecated since 1.18 call SpecialPageFactory method directly
189 */
190 static function getUsablePages() {
191 return SpecialPageFactory::getUsablePages();
192 }
193
194 /**
195 * Return categorised listable special pages for all users
196 *
197 * @return Associative array mapping page's name to its SpecialPage object
198 * @deprecated since 1.18 call SpecialPageFactory method directly
199 */
200 static function getRegularPages() {
201 return SpecialPageFactory::getRegularPages();
202 }
203
204 /**
205 * Return categorised listable special pages which are available
206 * for the current user, but not for everyone
207 *
208 * @return Associative array mapping page's name to its SpecialPage object
209 * @deprecated since 1.18 call SpecialPageFactory method directly
210 */
211 static function getRestrictedPages() {
212 return SpecialPageFactory::getRestrictedPages();
213 }
214
215 /**
216 * Execute a special page path.
217 * The path may contain parameters, e.g. Special:Name/Params
218 * Extracts the special page name and call the execute method, passing the parameters
219 *
220 * Returns a title object if the page is redirected, false if there was no such special
221 * page, and true if it was successful.
222 *
223 * @param $title Title object
224 * @param $context RequestContext
225 * @param $including Bool output is being captured for use in {{special:whatever}}
226 * @deprecated since 1.18 call SpecialPageFactory method directly
227 */
228 public static function executePath( &$title, RequestContext &$context, $including = false ) {
229 return SpecialPageFactory::executePath( $title, $context, $including );
230 }
231
232 /**
233 * Just like executePath() except it returns the HTML instead of outputting it
234 * Returns false if there was no such special page, or a title object if it was
235 * a redirect.
236 *
237 * @return String: HTML fragment
238 * @deprecated since 1.18 call SpecialPageFactory method directly
239 */
240 static function capturePath( &$title ) {
241 return SpecialPageFactory::capturePath( $title );
242 }
243
244 /**
245 * Get the local name for a specified canonical name
246 *
247 * @param $name String
248 * @param $subpage Mixed: boolean false, or string
249 *
250 * @return String
251 * @deprecated since 1.18 call SpecialPageFactory method directly
252 */
253 static function getLocalNameFor( $name, $subpage = false ) {
254 return SpecialPageFactory::getLocalNameFor( $name, $subpage );
255 }
256
257 /**
258 * Get a localised Title object for a specified special page name
259 *
260 * @return Title object
261 */
262 public static function getTitleFor( $name, $subpage = false ) {
263 $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
264 if ( $name ) {
265 return Title::makeTitle( NS_SPECIAL, $name );
266 } else {
267 throw new MWException( "Invalid special page name \"$name\"" );
268 }
269 }
270
271 /**
272 * Get a localised Title object for a page name with a possibly unvalidated subpage
273 *
274 * @return Title object or null if the page doesn't exist
275 */
276 public static function getSafeTitleFor( $name, $subpage = false ) {
277 $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
278 if ( $name ) {
279 return Title::makeTitleSafe( NS_SPECIAL, $name );
280 } else {
281 return null;
282 }
283 }
284
285 /**
286 * Get a title for a given alias
287 *
288 * @return Title or null if there is no such alias
289 * @deprecated since 1.18 call SpecialPageFactory method directly
290 */
291 static function getTitleForAlias( $alias ) {
292 return SpecialPageFactory::getTitleForAlias( $alias );
293 }
294
295 /**
296 * Default constructor for special pages
297 * Derivative classes should call this from their constructor
298 * Note that if the user does not have the required level, an error message will
299 * be displayed by the default execute() method, without the global function ever
300 * being called.
301 *
302 * If you override execute(), you can recover the default behaviour with userCanExecute()
303 * and displayRestrictionError()
304 *
305 * @param $name String: name of the special page, as seen in links and URLs
306 * @param $restriction String: user right required, e.g. "block" or "delete"
307 * @param $listed Boolean: whether the page is listed in Special:Specialpages
308 * @param $function Callback: function called by execute(). By default it is constructed from $name
309 * @param $file String: file which is included by execute(). It is also constructed from $name by default
310 * @param $includable Boolean: whether the page can be included in normal pages
311 */
312 public function __construct( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
313 $this->init( $name, $restriction, $listed, $function, $file, $includable );
314 }
315
316 /**
317 * Do the real work for the constructor, mainly so __call() can intercept
318 * calls to SpecialPage()
319 * @see __construct() for param docs
320 */
321 private function init( $name, $restriction, $listed, $function, $file, $includable ) {
322 $this->mName = $name;
323 $this->mRestriction = $restriction;
324 $this->mListed = $listed;
325 $this->mIncludable = $includable;
326 if ( !$function ) {
327 $this->mFunction = 'wfSpecial'.$name;
328 } else {
329 $this->mFunction = $function;
330 }
331 if ( $file === 'default' ) {
332 $this->mFile = dirname(__FILE__) . "/specials/Special$name.php";
333 } else {
334 $this->mFile = $file;
335 }
336 }
337
338 /**
339 * Use PHP's magic __call handler to get calls to the old PHP4 constructor
340 * because PHP E_STRICT yells at you for having __construct() and SpecialPage()
341 *
342 * @param $fName String Name of called method
343 * @param $a Array Arguments to the method
344 * @deprecated since 1.17, call parent::__construct()
345 */
346 public function __call( $fName, $a ) {
347 // Sometimes $fName is SpecialPage, sometimes it's specialpage. <3 PHP
348 if( strtolower( $fName ) == 'specialpage' ) {
349 // Deprecated messages now, remove in 1.19 or 1.20?
350 wfDeprecated( __METHOD__ );
351
352 $name = isset( $a[0] ) ? $a[0] : '';
353 $restriction = isset( $a[1] ) ? $a[1] : '';
354 $listed = isset( $a[2] ) ? $a[2] : true;
355 $function = isset( $a[3] ) ? $a[3] : false;
356 $file = isset( $a[4] ) ? $a[4] : 'default';
357 $includable = isset( $a[5] ) ? $a[5] : false;
358 $this->init( $name, $restriction, $listed, $function, $file, $includable );
359 } else {
360 $className = get_class( $this );
361 throw new MWException( "Call to undefined method $className::$fName" );
362 }
363 }
364
365 /**
366 * Get the name of this Special Page.
367 * @return String
368 */
369 function getName() {
370 return $this->mName;
371 }
372
373 /**
374 * Get the permission that a user must have to execute this page
375 * @return String
376 */
377 function getRestriction() {
378 return $this->mRestriction;
379 }
380
381 /**
382 * Get the file which will be included by SpecialPage::execute() if your extension is
383 * still stuck in the past and hasn't overridden the execute() method. No modern code
384 * should want or need to know this.
385 * @return String
386 * @deprecated since 1.18
387 */
388 function getFile() {
389 return $this->mFile;
390 }
391
392 // @todo FIXME: Decide which syntax to use for this, and stick to it
393 /**
394 * Whether this special page is listed in Special:SpecialPages
395 * @since r3583 (v1.3)
396 * @return Bool
397 */
398 function isListed() {
399 return $this->mListed;
400 }
401 /**
402 * Set whether this page is listed in Special:Specialpages, at run-time
403 * @since r3583 (v1.3)
404 * @return Bool
405 */
406 function setListed( $listed ) {
407 return wfSetVar( $this->mListed, $listed );
408 }
409 /**
410 * Get or set whether this special page is listed in Special:SpecialPages
411 * @since r11308 (v1.6)
412 * @return Bool
413 */
414 function listed( $x = null) {
415 return wfSetVar( $this->mListed, $x );
416 }
417
418 /**
419 * Whether it's allowed to transclude the special page via {{Special:Foo/params}}
420 * @return Bool
421 */
422 public function isIncludable(){
423 return $this->mIncludable;
424 }
425
426 /**
427 * These mutators are very evil, as the relevant variables should not mutate. So
428 * don't use them.
429 * @deprecated since 1.18
430 */
431 function name( $x = null ) { return wfSetVar( $this->mName, $x ); }
432 function restriction( $x = null) { return wfSetVar( $this->mRestriction, $x ); }
433 function func( $x = null) { return wfSetVar( $this->mFunction, $x ); }
434 function file( $x = null) { return wfSetVar( $this->mFile, $x ); }
435 function includable( $x = null ) { return wfSetVar( $this->mIncludable, $x ); }
436
437 /**
438 * Whether the special page is being evaluated via transclusion
439 * @return Bool
440 */
441 function including( $x = null ) {
442 return wfSetVar( $this->mIncluding, $x );
443 }
444
445 /**
446 * Get the localised name of the special page
447 */
448 function getLocalName() {
449 if ( !isset( $this->mLocalName ) ) {
450 $this->mLocalName = SpecialPageFactory::getLocalNameFor( $this->mName );
451 }
452 return $this->mLocalName;
453 }
454
455 /**
456 * Is this page expensive (for some definition of expensive)?
457 * Expensive pages are disabled or cached in miser mode. Originally used
458 * (and still overridden) by QueryPage and subclasses, moved here so that
459 * Special:SpecialPages can safely call it for all special pages.
460 *
461 * @return Boolean
462 */
463 public function isExpensive() {
464 return false;
465 }
466
467 /**
468 * Can be overridden by subclasses with more complicated permissions
469 * schemes.
470 *
471 * @return Boolean: should the page be displayed with the restricted-access
472 * pages?
473 */
474 public function isRestricted() {
475 global $wgGroupPermissions;
476 // DWIM: If all anons can do something, then it is not restricted
477 return $this->mRestriction != '' && empty($wgGroupPermissions['*'][$this->mRestriction]);
478 }
479
480 /**
481 * Checks if the given user (identified by an object) can execute this
482 * special page (as defined by $mRestriction). Can be overridden by sub-
483 * classes with more complicated permissions schemes.
484 *
485 * @param $user User: the user to check
486 * @return Boolean: does the user have permission to view the page?
487 */
488 public function userCanExecute( User $user ) {
489 return $user->isAllowed( $this->mRestriction );
490 }
491
492 /**
493 * Output an error message telling the user what access level they have to have
494 */
495 function displayRestrictionError() {
496 throw new PermissionsError( $this->mRestriction );
497 }
498
499 /**
500 * Sets headers - this should be called from the execute() method of all derived classes!
501 */
502 function setHeaders() {
503 $out = $this->getOutput();
504 $out->setArticleRelated( false );
505 $out->setRobotPolicy( "noindex,nofollow" );
506 $out->setPageTitle( $this->getDescription() );
507 }
508
509 /**
510 * Default execute method
511 * Checks user permissions, calls the function given in mFunction
512 *
513 * This must be overridden by subclasses; it will be made abstract in a future version
514 */
515 function execute( $par ) {
516 $this->setHeaders();
517
518 if ( $this->userCanExecute( $this->getUser() ) ) {
519 $func = $this->mFunction;
520 // only load file if the function does not exist
521 if(!is_callable($func) and $this->mFile) {
522 require_once( $this->mFile );
523 }
524 $this->outputHeader();
525 call_user_func( $func, $par, $this );
526 } else {
527 $this->displayRestrictionError();
528 }
529 }
530
531 /**
532 * Outputs a summary message on top of special pages
533 * Per default the message key is the canonical name of the special page
534 * May be overriden, i.e. by extensions to stick with the naming conventions
535 * for message keys: 'extensionname-xxx'
536 *
537 * @param $summaryMessageKey String: message key of the summary
538 */
539 function outputHeader( $summaryMessageKey = '' ) {
540 global $wgContLang;
541
542 if( $summaryMessageKey == '' ) {
543 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
544 } else {
545 $msg = $summaryMessageKey;
546 }
547 if ( !wfMessage( $msg )->isBlank() and ! $this->including() ) {
548 $this->getOutput()->wrapWikiMsg( "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
549 }
550
551 }
552
553 /**
554 * Returns the name that goes in the \<h1\> in the special page itself, and
555 * also the name that will be listed in Special:Specialpages
556 *
557 * Derived classes can override this, but usually it is easier to keep the
558 * default behaviour. Messages can be added at run-time, see
559 * MessageCache.php.
560 *
561 * @return String
562 */
563 function getDescription() {
564 return wfMsg( strtolower( $this->mName ) );
565 }
566
567 /**
568 * Get a self-referential title object
569 *
570 * @return Title object
571 */
572 function getTitle( $subpage = false ) {
573 return self::getTitleFor( $this->mName, $subpage );
574 }
575
576 /**
577 * Sets the context this SpecialPage is executed in
578 *
579 * @param $context RequestContext
580 * @since 1.18
581 */
582 public function setContext( $context ) {
583 $this->mContext = $context;
584 }
585
586 /**
587 * Gets the context this SpecialPage is executed in
588 *
589 * @return RequestContext
590 * @since 1.18
591 */
592 public function getContext() {
593 if ( $this->mContext instanceof RequestContext ) {
594 return $this->mContext;
595 } else {
596 wfDebug( __METHOD__ . " called and \$mContext is null. Return RequestContext::getMain(); for sanity\n" );
597 return RequestContext::getMain();
598 }
599 }
600
601 /**
602 * Get the WebRequest being used for this instance
603 *
604 * @return WebRequest
605 * @since 1.18
606 */
607 public function getRequest() {
608 return $this->getContext()->getRequest();
609 }
610
611 /**
612 * Get the OutputPage being used for this instance
613 *
614 * @return OutputPage
615 * @since 1.18
616 */
617 public function getOutput() {
618 return $this->getContext()->getOutput();
619 }
620
621 /**
622 * Shortcut to get the skin being used for this instance
623 *
624 * @return User
625 * @since 1.18
626 */
627 public function getUser() {
628 return $this->getContext()->getUser();
629 }
630
631 /**
632 * Shortcut to get the skin being used for this instance
633 *
634 * @return Skin
635 * @since 1.18
636 */
637 public function getSkin() {
638 return $this->getContext()->getSkin();
639 }
640
641 /**
642 * Return the full title, including $par
643 *
644 * @return Title
645 * @since 1.18
646 */
647 public function getFullTitle() {
648 return $this->getContext()->getTitle();
649 }
650
651 /**
652 * Wrapper around wfMessage that sets the current context. Currently this
653 * is only the title.
654 *
655 * @see wfMessage
656 */
657 public function msg( /* $args */ ) {
658 return call_user_func_array( 'wfMessage', func_get_args() )->title( $this->getFullTitle() );
659 }
660
661 /**
662 * Adds RSS/atom links
663 *
664 * @param $params array
665 */
666 protected function addFeedLinks( $params ) {
667 global $wgFeedClasses, $wgOut;
668
669 $feedTemplate = wfScript( 'api' ) . '?';
670
671 foreach( $wgFeedClasses as $format => $class ) {
672 $theseParams = $params + array( 'feedformat' => $format );
673 $url = $feedTemplate . wfArrayToCGI( $theseParams );
674 $wgOut->addFeedLink( $format, $url );
675 }
676 }
677 }
678
679 /**
680 * Special page which uses an HTMLForm to handle processing. This is mostly a
681 * clone of FormAction. More special pages should be built this way; maybe this could be
682 * a new structure for SpecialPages
683 */
684 abstract class FormSpecialPage extends SpecialPage {
685
686 /**
687 * Get an HTMLForm descriptor array
688 * @return Array
689 */
690 protected abstract function getFormFields();
691
692 /**
693 * Add pre- or post-text to the form
694 * @return String HTML which will be sent to $form->addPreText()
695 */
696 protected function preText() { return ''; }
697 protected function postText() { return ''; }
698
699 /**
700 * Play with the HTMLForm if you need to more substantially
701 * @param $form HTMLForm
702 */
703 protected function alterForm( HTMLForm $form ) {}
704
705 /**
706 * Get the HTMLForm to control behaviour
707 * @return HTMLForm|null
708 */
709 protected function getForm() {
710 $this->fields = $this->getFormFields();
711
712 // Give hooks a chance to alter the form, adding extra fields or text etc
713 wfRunHooks( "Special{$this->getName()}ModifyFormFields", array( &$this->fields ) );
714
715 $form = new HTMLForm( $this->fields, $this->getContext() );
716 $form->setSubmitCallback( array( $this, 'onSubmit' ) );
717 $form->setWrapperLegend( wfMessage( strtolower( $this->getName() ) . '-legend' ) );
718 $form->addHeaderText( wfMessage( strtolower( $this->getName() ) . '-text' )->parseAsBlock() );
719
720 // Retain query parameters (uselang etc)
721 $params = array_diff_key( $this->getRequest()->getQueryValues(), array( 'title' => null ) );
722 $form->addHiddenField( 'redirectparams', wfArrayToCGI( $params ) );
723
724 $form->addPreText( $this->preText() );
725 $form->addPostText( $this->postText() );
726 $this->alterForm( $form );
727
728 // Give hooks a chance to alter the form, adding extra fields or text etc
729 wfRunHooks( "Special{$this->getName()}BeforeFormDisplay", array( &$form ) );
730
731 return $form;
732 }
733
734 /**
735 * Process the form on POST submission.
736 * @param $data Array
737 * @return Bool|Array true for success, false for didn't-try, array of errors on failure
738 */
739 public abstract function onSubmit( array $data );
740
741 /**
742 * Do something exciting on successful processing of the form, most likely to show a
743 * confirmation message
744 */
745 public abstract function onSuccess();
746
747 /**
748 * Basic SpecialPage workflow: get a form, send it to the user; get some data back,
749 */
750 public function execute( $par ) {
751 $this->setParameter( $par );
752 $this->setHeaders();
753
754 // This will throw exceptions if there's a problem
755 $this->userCanExecute( $this->getUser() );
756
757 $form = $this->getForm();
758 if ( $form->show() ) {
759 $this->onSuccess();
760 }
761 }
762
763 /**
764 * Maybe do something interesting with the subpage parameter
765 * @param $par String
766 */
767 protected function setParameter( $par ){}
768
769 /**
770 * Checks if the given user (identified by an object) can perform this action. Can be
771 * overridden by sub-classes with more complicated permissions schemes. Failures here
772 * must throw subclasses of ErrorPageError
773 *
774 * @param $user User: the user to check, or null to use the context user
775 * @throws ErrorPageError
776 */
777 public function userCanExecute( User $user ) {
778 if ( $this->requiresWrite() && wfReadOnly() ) {
779 throw new ReadOnlyError();
780 }
781
782 if ( $this->getRestriction() !== null && !$user->isAllowed( $this->getRestriction() ) ) {
783 throw new PermissionsError( $this->getRestriction() );
784 }
785
786 if ( $this->requiresUnblock() && $user->isBlocked() ) {
787 $block = $user->mBlock;
788 throw new UserBlockedError( $block );
789 }
790
791 return true;
792 }
793
794 /**
795 * Whether this action requires the wiki not to be locked
796 * @return Bool
797 */
798 public function requiresWrite() {
799 return true;
800 }
801
802 /**
803 * Whether this action cannot be executed by a blocked user
804 * @return Bool
805 */
806 public function requiresUnblock() {
807 return true;
808 }
809 }
810
811 /**
812 * Shortcut to construct a special page which is unlisted by default
813 * @ingroup SpecialPage
814 */
815 class UnlistedSpecialPage extends SpecialPage
816 {
817 function __construct( $name, $restriction = '', $function = false, $file = 'default' ) {
818 parent::__construct( $name, $restriction, false, $function, $file );
819 }
820
821 public function isListed(){
822 return false;
823 }
824 }
825
826 /**
827 * Shortcut to construct an includable special page
828 * @ingroup SpecialPage
829 */
830 class IncludableSpecialPage extends SpecialPage
831 {
832 function __construct( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
833 parent::__construct( $name, $restriction, $listed, $function, $file, true );
834 }
835
836 public function isIncludable(){
837 return true;
838 }
839 }
840
841 /**
842 * Shortcut to construct a special page alias.
843 * @ingroup SpecialPage
844 */
845 abstract class RedirectSpecialPage extends UnlistedSpecialPage {
846
847 // Query parameters that can be passed through redirects
848 protected $mAllowedRedirectParams = array();
849
850 // Query parameteres added by redirects
851 protected $mAddedRedirectParams = array();
852
853 public function execute( $par ){
854 $redirect = $this->getRedirect( $par );
855 $query = $this->getRedirectQuery();
856 // Redirect to a page title with possible query parameters
857 if ( $redirect instanceof Title ) {
858 $url = $redirect->getFullUrl( $query );
859 $this->getContext()->output->redirect( $url );
860 wfProfileOut( __METHOD__ );
861 return $redirect;
862 // Redirect to index.php with query parameters
863 } elseif ( $redirect === true ) {
864 global $wgScript;
865 $url = $wgScript . '?' . wfArrayToCGI( $query );
866 $this->getContext()->output->redirect( $url );
867 wfProfileOut( __METHOD__ );
868 return $redirect;
869 } else {
870 $class = __CLASS__;
871 throw new MWException( "RedirectSpecialPage $class doesn't redirect!" );
872 }
873 }
874
875 /**
876 * If the special page is a redirect, then get the Title object it redirects to.
877 * False otherwise.
878 *
879 * @return Title|false
880 */
881 abstract public function getRedirect( $par );
882
883 /**
884 * Return part of the request string for a special redirect page
885 * This allows passing, e.g. action=history to Special:Mypage, etc.
886 *
887 * @return String
888 */
889 public function getRedirectQuery() {
890 $params = array();
891
892 foreach( $this->mAllowedRedirectParams as $arg ) {
893 if( $this->getRequest()->getVal( $arg, null ) !== null ){
894 $params[$arg] = $this->getRequest()->getVal( $arg );
895 }
896 }
897
898 foreach( $this->mAddedRedirectParams as $arg => $val ) {
899 $params[$arg] = $val;
900 }
901
902 return count( $params )
903 ? $params
904 : false;
905 }
906 }
907
908 abstract class SpecialRedirectToSpecial extends RedirectSpecialPage {
909
910 var $redirName, $redirSubpage;
911
912 function __construct( $name, $redirName, $redirSubpage = false, $allowedRedirectParams = array(), $addedRedirectParams = array() ) {
913 parent::__construct( $name );
914 $this->redirName = $redirName;
915 $this->redirSubpage = $redirSubpage;
916 $this->mAllowedRedirectParams = $allowedRedirectParams;
917 $this->mAddedRedirectParams = $addedRedirectParams;
918 }
919
920 public function getRedirect( $subpage ) {
921 if ( $this->redirSubpage === false ) {
922 return SpecialPage::getTitleFor( $this->redirName, $subpage );
923 } else {
924 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
925 }
926 }
927 }
928
929 /**
930 * ListAdmins --> ListUsers/admin
931 */
932 class SpecialListAdmins extends SpecialRedirectToSpecial {
933 function __construct(){
934 parent::__construct( 'ListAdmins', 'ListUsers', 'sysop' );
935 }
936 }
937
938 /**
939 * ListBots --> ListUsers/admin
940 */
941 class SpecialListBots extends SpecialRedirectToSpecial {
942 function __construct(){
943 parent::__construct( 'ListAdmins', 'ListUsers', 'bot' );
944 }
945 }
946
947 /**
948 * CreateAccount --> UserLogin/signup
949 * @todo FIXME: This (and the rest of the login frontend) needs to die a horrible painful death
950 */
951 class SpecialCreateAccount extends SpecialRedirectToSpecial {
952 function __construct(){
953 parent::__construct( 'CreateAccount', 'Userlogin', 'signup', array( 'uselang' ) );
954 }
955 }
956 /**
957 * SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
958 * are used to get user independant links pointing to the user page, talk
959 * page and list of contributions.
960 * This can let us cache a single copy of any generated content for all
961 * users.
962 */
963
964 /**
965 * Shortcut to construct a special page pointing to current user user's page.
966 * @ingroup SpecialPage
967 */
968 class SpecialMypage extends RedirectSpecialPage {
969 function __construct() {
970 parent::__construct( 'Mypage' );
971 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro',
972 'section', 'oldid', 'diff', 'dir' );
973 }
974
975 function getRedirect( $subpage ) {
976 global $wgUser;
977 if ( strval( $subpage ) !== '' ) {
978 return Title::makeTitle( NS_USER, $wgUser->getName() . '/' . $subpage );
979 } else {
980 return Title::makeTitle( NS_USER, $wgUser->getName() );
981 }
982 }
983 }
984
985 /**
986 * Shortcut to construct a special page pointing to current user talk page.
987 * @ingroup SpecialPage
988 */
989 class SpecialMytalk extends RedirectSpecialPage {
990 function __construct() {
991 parent::__construct( 'Mytalk' );
992 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro',
993 'section', 'oldid', 'diff', 'dir' );
994 }
995
996 function getRedirect( $subpage ) {
997 global $wgUser;
998 if ( strval( $subpage ) !== '' ) {
999 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() . '/' . $subpage );
1000 } else {
1001 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() );
1002 }
1003 }
1004 }
1005
1006 /**
1007 * Shortcut to construct a special page pointing to current user contributions.
1008 * @ingroup SpecialPage
1009 */
1010 class SpecialMycontributions extends RedirectSpecialPage {
1011 function __construct() {
1012 parent::__construct( 'Mycontributions' );
1013 $this->mAllowedRedirectParams = array( 'limit', 'namespace', 'tagfilter',
1014 'offset', 'dir', 'year', 'month', 'feed' );
1015 }
1016
1017 function getRedirect( $subpage ) {
1018 global $wgUser;
1019 return SpecialPage::getTitleFor( 'Contributions', $wgUser->getName() );
1020 }
1021 }
1022
1023 /**
1024 * Redirect to Special:Listfiles?user=$wgUser
1025 */
1026 class SpecialMyuploads extends RedirectSpecialPage {
1027 function __construct() {
1028 parent::__construct( 'Myuploads' );
1029 $this->mAllowedRedirectParams = array( 'limit' );
1030 }
1031
1032 function getRedirect( $subpage ) {
1033 global $wgUser;
1034 return SpecialPage::getTitleFor( 'Listfiles', $wgUser->getName() );
1035 }
1036 }
1037
1038 /**
1039 * Redirect from Special:PermanentLink/### to index.php?oldid=###
1040 */
1041 class SpecialPermanentLink extends RedirectSpecialPage {
1042 function __construct() {
1043 parent::__construct( 'PermanentLink' );
1044 $this->mAllowedRedirectParams = array();
1045 }
1046
1047 function getRedirect( $subpage ) {
1048 $subpage = intval( $subpage );
1049 $this->mAddedRedirectParams['oldid'] = $subpage;
1050 return true;
1051 }
1052 }