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