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