Misc EOL w/s and spaces-instead-of-tabs fixes. One day I'll get around to nagging...
[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 /**
663 * Special page which uses an HTMLForm to handle processing. This is mostly a
664 * clone of FormAction. More special pages should be built this way; maybe this could be
665 * a new structure for SpecialPages
666 */
667 abstract class FormSpecialPage extends SpecialPage {
668
669 /**
670 * Get an HTMLForm descriptor array
671 * @return Array
672 */
673 protected abstract function getFormFields();
674
675 /**
676 * Add pre- or post-text to the form
677 * @return String HTML which will be sent to $form->addPreText()
678 */
679 protected function preText() { return ''; }
680 protected function postText() { return ''; }
681
682 /**
683 * Play with the HTMLForm if you need to more substantially
684 * @param $form HTMLForm
685 */
686 protected function alterForm( HTMLForm $form ) {}
687
688 /**
689 * Get the HTMLForm to control behaviour
690 * @return HTMLForm|null
691 */
692 protected function getForm() {
693 $this->fields = $this->getFormFields();
694
695 // Give hooks a chance to alter the form, adding extra fields or text etc
696 wfRunHooks( "Special{$this->getName()}ModifyFormFields", array( &$this->fields ) );
697
698 $form = new HTMLForm( $this->fields, $this->getContext() );
699 $form->setSubmitCallback( array( $this, 'onSubmit' ) );
700 $form->setWrapperLegend( wfMessage( strtolower( $this->getName() ) . '-legend' ) );
701 $form->addHeaderText( wfMessage( strtolower( $this->getName() ) . '-text' )->parseAsBlock() );
702
703 // Retain query parameters (uselang etc)
704 $params = array_diff_key( $this->getRequest()->getQueryValues(), array( 'title' => null ) );
705 $form->addHiddenField( 'redirectparams', wfArrayToCGI( $params ) );
706
707 $form->addPreText( $this->preText() );
708 $form->addPostText( $this->postText() );
709 $this->alterForm( $form );
710
711 // Give hooks a chance to alter the form, adding extra fields or text etc
712 wfRunHooks( "Special{$this->getName()}BeforeFormDisplay", array( &$form ) );
713
714 return $form;
715 }
716
717 /**
718 * Process the form on POST submission.
719 * @param $data Array
720 * @return Bool|Array true for success, false for didn't-try, array of errors on failure
721 */
722 public abstract function onSubmit( array $data );
723
724 /**
725 * Do something exciting on successful processing of the form, most likely to show a
726 * confirmation message
727 */
728 public abstract function onSuccess();
729
730 /**
731 * Basic SpecialPage workflow: get a form, send it to the user; get some data back,
732 */
733 public function execute( $par ) {
734 $this->setParameter( $par );
735 $this->setHeaders();
736
737 // This will throw exceptions if there's a problem
738 $this->userCanExecute( $this->getUser() );
739
740 $form = $this->getForm();
741 if ( $form->show() ) {
742 $this->onSuccess();
743 }
744 }
745
746 /**
747 * Maybe do something interesting with the subpage parameter
748 * @param $par String
749 */
750 protected function setParameter( $par ){}
751
752 /**
753 * Checks if the given user (identified by an object) can perform this action. Can be
754 * overridden by sub-classes with more complicated permissions schemes. Failures here
755 * must throw subclasses of ErrorPageError
756 *
757 * @param $user User: the user to check, or null to use the context user
758 * @throws ErrorPageError
759 */
760 public function userCanExecute( User $user ) {
761 if ( $this->requiresWrite() && wfReadOnly() ) {
762 throw new ReadOnlyError();
763 }
764
765 if ( $this->getRestriction() !== null && !$user->isAllowed( $this->getRestriction() ) ) {
766 throw new PermissionsError( $this->getRestriction() );
767 }
768
769 if ( $this->requiresUnblock() && $user->isBlocked() ) {
770 $block = $user->mBlock;
771 throw new UserBlockedError( $block );
772 }
773
774 return true;
775 }
776
777 /**
778 * Whether this action requires the wiki not to be locked
779 * @return Bool
780 */
781 public function requiresWrite() {
782 return true;
783 }
784
785 /**
786 * Whether this action cannot be executed by a blocked user
787 * @return Bool
788 */
789 public function requiresUnblock() {
790 return true;
791 }
792 }
793
794 /**
795 * Shortcut to construct a special page which is unlisted by default
796 * @ingroup SpecialPage
797 */
798 class UnlistedSpecialPage extends SpecialPage
799 {
800 function __construct( $name, $restriction = '', $function = false, $file = 'default' ) {
801 parent::__construct( $name, $restriction, false, $function, $file );
802 }
803
804 public function isListed(){
805 return false;
806 }
807 }
808
809 /**
810 * Shortcut to construct an includable special page
811 * @ingroup SpecialPage
812 */
813 class IncludableSpecialPage extends SpecialPage
814 {
815 function __construct( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
816 parent::__construct( $name, $restriction, $listed, $function, $file, true );
817 }
818
819 public function isIncludable(){
820 return true;
821 }
822 }
823
824 /**
825 * Shortcut to construct a special page alias.
826 * @ingroup SpecialPage
827 */
828 abstract class RedirectSpecialPage extends UnlistedSpecialPage {
829
830 // Query parameters that can be passed through redirects
831 protected $mAllowedRedirectParams = array();
832
833 // Query parameteres added by redirects
834 protected $mAddedRedirectParams = array();
835
836 public function execute( $par ){
837 $redirect = $this->getRedirect( $par );
838 $query = $this->getRedirectQuery();
839 // Redirect to a page title with possible query parameters
840 if ( $redirect instanceof Title ) {
841 $url = $redirect->getFullUrl( $query );
842 $this->getContext()->output->redirect( $url );
843 wfProfileOut( __METHOD__ );
844 return $redirect;
845 // Redirect to index.php with query parameters
846 } elseif ( $redirect === true ) {
847 global $wgScript;
848 $url = $wgScript . '?' . wfArrayToCGI( $query );
849 $this->getContext()->output->redirect( $url );
850 wfProfileOut( __METHOD__ );
851 return $redirect;
852 } else {
853 $class = __CLASS__;
854 throw new MWException( "RedirectSpecialPage $class doesn't redirect!" );
855 }
856 }
857
858 /**
859 * If the special page is a redirect, then get the Title object it redirects to.
860 * False otherwise.
861 *
862 * @return Title|false
863 */
864 abstract public function getRedirect( $par );
865
866 /**
867 * Return part of the request string for a special redirect page
868 * This allows passing, e.g. action=history to Special:Mypage, etc.
869 *
870 * @return String
871 */
872 public function getRedirectQuery() {
873 $params = array();
874
875 foreach( $this->mAllowedRedirectParams as $arg ) {
876 if( $this->getContext()->request->getVal( $arg, null ) !== null ){
877 $params[$arg] = $this->getContext()->request->getVal( $arg );
878 }
879 }
880
881 foreach( $this->mAddedRedirectParams as $arg => $val ) {
882 $params[$arg] = $val;
883 }
884
885 return count( $params )
886 ? $params
887 : false;
888 }
889 }
890
891 abstract class SpecialRedirectToSpecial extends RedirectSpecialPage {
892
893 var $redirName, $redirSubpage;
894
895 function __construct( $name, $redirName, $redirSubpage = false, $allowedRedirectParams = array(), $addedRedirectParams = array() ) {
896 parent::__construct( $name );
897 $this->redirName = $redirName;
898 $this->redirSubpage = $redirSubpage;
899 $this->mAllowedRedirectParams = $allowedRedirectParams;
900 $this->mAddedRedirectParams = $addedRedirectParams;
901 }
902
903 public function getRedirect( $subpage ) {
904 if ( $this->redirSubpage === false ) {
905 return SpecialPage::getTitleFor( $this->redirName, $subpage );
906 } else {
907 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
908 }
909 }
910 }
911
912 /**
913 * ListAdmins --> ListUsers/admin
914 */
915 class SpecialListAdmins extends SpecialRedirectToSpecial {
916 function __construct(){
917 parent::__construct( 'ListAdmins', 'ListUsers', 'sysop' );
918 }
919 }
920
921 /**
922 * ListBots --> ListUsers/admin
923 */
924 class SpecialListBots extends SpecialRedirectToSpecial {
925 function __construct(){
926 parent::__construct( 'ListAdmins', 'ListUsers', 'bot' );
927 }
928 }
929
930 /**
931 * CreateAccount --> UserLogin/signup
932 * @todo FIXME: This (and the rest of the login frontend) needs to die a horrible painful death
933 */
934 class SpecialCreateAccount extends SpecialRedirectToSpecial {
935 function __construct(){
936 parent::__construct( 'CreateAccount', 'Userlogin', 'signup', array( 'uselang' ) );
937 }
938 }
939 /**
940 * SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
941 * are used to get user independant links pointing to the user page, talk
942 * page and list of contributions.
943 * This can let us cache a single copy of any generated content for all
944 * users.
945 */
946
947 /**
948 * Shortcut to construct a special page pointing to current user user's page.
949 * @ingroup SpecialPage
950 */
951 class SpecialMypage extends RedirectSpecialPage {
952 function __construct() {
953 parent::__construct( 'Mypage' );
954 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro',
955 'section', 'oldid', 'diff', 'dir' );
956 }
957
958 function getRedirect( $subpage ) {
959 global $wgUser;
960 if ( strval( $subpage ) !== '' ) {
961 return Title::makeTitle( NS_USER, $wgUser->getName() . '/' . $subpage );
962 } else {
963 return Title::makeTitle( NS_USER, $wgUser->getName() );
964 }
965 }
966 }
967
968 /**
969 * Shortcut to construct a special page pointing to current user talk page.
970 * @ingroup SpecialPage
971 */
972 class SpecialMytalk extends RedirectSpecialPage {
973 function __construct() {
974 parent::__construct( 'Mytalk' );
975 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro',
976 'section', 'oldid', 'diff', 'dir' );
977 }
978
979 function getRedirect( $subpage ) {
980 global $wgUser;
981 if ( strval( $subpage ) !== '' ) {
982 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() . '/' . $subpage );
983 } else {
984 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() );
985 }
986 }
987 }
988
989 /**
990 * Shortcut to construct a special page pointing to current user contributions.
991 * @ingroup SpecialPage
992 */
993 class SpecialMycontributions extends RedirectSpecialPage {
994 function __construct() {
995 parent::__construct( 'Mycontributions' );
996 $this->mAllowedRedirectParams = array( 'limit', 'namespace', 'tagfilter',
997 'offset', 'dir', 'year', 'month', 'feed' );
998 }
999
1000 function getRedirect( $subpage ) {
1001 global $wgUser;
1002 return SpecialPage::getTitleFor( 'Contributions', $wgUser->getName() );
1003 }
1004 }
1005
1006 /**
1007 * Redirect to Special:Listfiles?user=$wgUser
1008 */
1009 class SpecialMyuploads extends RedirectSpecialPage {
1010 function __construct() {
1011 parent::__construct( 'Myuploads' );
1012 $this->mAllowedRedirectParams = array( 'limit' );
1013 }
1014
1015 function getRedirect( $subpage ) {
1016 global $wgUser;
1017 return SpecialPage::getTitleFor( 'Listfiles', $wgUser->getName() );
1018 }
1019 }
1020
1021 /**
1022 * Redirect from Special:PermanentLink/### to index.php?oldid=###
1023 */
1024 class SpecialPermanentLink extends RedirectSpecialPage {
1025 function __construct() {
1026 parent::__construct( 'PermanentLink' );
1027 $this->mAllowedRedirectParams = array();
1028 }
1029
1030 function getRedirect( $subpage ) {
1031 $subpage = intval( $subpage );
1032 $this->mAddedRedirectParams['oldid'] = $subpage;
1033 return true;
1034 }
1035 }