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