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