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