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