[FileBackend] Added back ability to delete file HTTP headers.
[lhc/web/wiklou.git] / includes / SpecialPage.php
1 <?php
2 /**
3 * Parent class for all special pages.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Parent special page class, also static functions for handling the special
26 * page list.
27 * @ingroup SpecialPage
28 */
29 class SpecialPage {
30
31 // The canonical name of this special page
32 // Also used for the default <h1> heading, @see getDescription()
33 protected $mName;
34
35 // The local name of this special page
36 private $mLocalName;
37
38 // Minimum user level required to access this page, or "" for anyone.
39 // Also used to categorise the pages in Special:Specialpages
40 private $mRestriction;
41
42 // Listed in Special:Specialpages?
43 private $mListed;
44
45 // Function name called by the default execute()
46 private $mFunction;
47
48 // File which needs to be included before the function above can be called
49 private $mFile;
50
51 // Whether or not this special page is being included from an article
52 protected $mIncluding;
53
54 // Whether the special page can be included in an article
55 protected $mIncludable;
56
57 /**
58 * Current request context
59 * @var IContextSource
60 */
61 protected $mContext;
62
63 /**
64 * Initialise the special page list
65 * This must be called before accessing SpecialPage::$mList
66 * @deprecated since 1.18
67 */
68 static function initList() {
69 wfDeprecated( __METHOD__, '1.18' );
70 // Noop
71 }
72
73 /**
74 * @deprecated since 1.18
75 */
76 static function initAliasList() {
77 wfDeprecated( __METHOD__, '1.18' );
78 // Noop
79 }
80
81 /**
82 * Given a special page alias, return the special page name.
83 * Returns false if there is no such alias.
84 *
85 * @param $alias String
86 * @return String or false
87 * @deprecated since 1.18 call SpecialPageFactory method directly
88 */
89 static function resolveAlias( $alias ) {
90 wfDeprecated( __METHOD__, '1.18' );
91 list( $name, /*...*/ ) = SpecialPageFactory::resolveAlias( $alias );
92 return $name;
93 }
94
95 /**
96 * Given a special page name with a possible subpage, return an array
97 * where the first element is the special page name and the second is the
98 * subpage.
99 *
100 * @param $alias String
101 * @return Array
102 * @deprecated since 1.18 call SpecialPageFactory method directly
103 */
104 static function resolveAliasWithSubpage( $alias ) {
105 return SpecialPageFactory::resolveAlias( $alias );
106 }
107
108 /**
109 * Add a page to a certain display group for Special:SpecialPages
110 *
111 * @param $page Mixed: SpecialPage or string
112 * @param $group String
113 * @deprecated since 1.18 call SpecialPageFactory method directly
114 */
115 static function setGroup( $page, $group ) {
116 wfDeprecated( __METHOD__, '1.18' );
117 SpecialPageFactory::setGroup( $page, $group );
118 }
119
120 /**
121 * Get the group that the special page belongs in on Special:SpecialPage
122 *
123 * @param $page SpecialPage
124 * @return string
125 * @deprecated since 1.18 call SpecialPageFactory method directly
126 */
127 static function getGroup( &$page ) {
128 wfDeprecated( __METHOD__, '1.18' );
129 return SpecialPageFactory::getGroup( $page );
130 }
131
132 /**
133 * Remove a special page from the list
134 * Formerly used to disable expensive or dangerous special pages. The
135 * preferred method is now to add a SpecialPage_initList hook.
136 * @deprecated since 1.18
137 *
138 * @param $name String the page to remove
139 */
140 static function removePage( $name ) {
141 wfDeprecated( __METHOD__, '1.18' );
142 unset( SpecialPageFactory::getList()->$name );
143 }
144
145 /**
146 * Check if a given name exist as a special page or as a special page alias
147 *
148 * @param $name String: name of a special page
149 * @return Boolean: true if a special page exists with this name
150 * @deprecated since 1.18 call SpecialPageFactory method directly
151 */
152 static function exists( $name ) {
153 wfDeprecated( __METHOD__, '1.18' );
154 return SpecialPageFactory::exists( $name );
155 }
156
157 /**
158 * Find the object with a given name and return it (or NULL)
159 *
160 * @param $name String
161 * @return SpecialPage object or null if the page doesn't exist
162 * @deprecated since 1.18 call SpecialPageFactory method directly
163 */
164 static function getPage( $name ) {
165 wfDeprecated( __METHOD__, '1.18' );
166 return SpecialPageFactory::getPage( $name );
167 }
168
169 /**
170 * Get a special page with a given localised name, or NULL if there
171 * is no such special page.
172 *
173 * @param $alias String
174 * @return SpecialPage object or null if the page doesn't exist
175 * @deprecated since 1.18 call SpecialPageFactory method directly
176 */
177 static function getPageByAlias( $alias ) {
178 wfDeprecated( __METHOD__, '1.18' );
179 return SpecialPageFactory::getPage( $alias );
180 }
181
182 /**
183 * Return categorised listable special pages which are available
184 * for the current user, and everyone.
185 *
186 * @param $user User object to check permissions, $wgUser will be used
187 * if not provided
188 * @return array Associative array mapping page's name to its SpecialPage object
189 * @deprecated since 1.18 call SpecialPageFactory method directly
190 */
191 static function getUsablePages( User $user = null ) {
192 wfDeprecated( __METHOD__, '1.18' );
193 return SpecialPageFactory::getUsablePages( $user );
194 }
195
196 /**
197 * Return categorised listable special pages for all users
198 *
199 * @return array Associative array mapping page's name to its SpecialPage object
200 * @deprecated since 1.18 call SpecialPageFactory method directly
201 */
202 static function getRegularPages() {
203 wfDeprecated( __METHOD__, '1.18' );
204 return SpecialPageFactory::getRegularPages();
205 }
206
207 /**
208 * Return categorised listable special pages which are available
209 * for the current user, but not for everyone
210 *
211 * @return array Associative array mapping page's name to its SpecialPage object
212 * @deprecated since 1.18 call SpecialPageFactory method directly
213 */
214 static function getRestrictedPages() {
215 wfDeprecated( __METHOD__, '1.18' );
216 return SpecialPageFactory::getRestrictedPages();
217 }
218
219 /**
220 * Execute a special page path.
221 * The path may contain parameters, e.g. Special:Name/Params
222 * Extracts the special page name and call the execute method, passing the parameters
223 *
224 * Returns a title object if the page is redirected, false if there was no such special
225 * page, and true if it was successful.
226 *
227 * @param $title Title object
228 * @param $context IContextSource
229 * @param $including Bool output is being captured for use in {{special:whatever}}
230 * @return Bool
231 * @deprecated since 1.18 call SpecialPageFactory method directly
232 */
233 public static function executePath( &$title, IContextSource &$context, $including = false ) {
234 wfDeprecated( __METHOD__, '1.18' );
235 return SpecialPageFactory::executePath( $title, $context, $including );
236 }
237
238 /**
239 * Get the local name for a specified canonical name
240 *
241 * @param $name String
242 * @param $subpage Mixed: boolean false, or string
243 *
244 * @return String
245 * @deprecated since 1.18 call SpecialPageFactory method directly
246 */
247 static function getLocalNameFor( $name, $subpage = false ) {
248 wfDeprecated( __METHOD__, '1.18' );
249 return SpecialPageFactory::getLocalNameFor( $name, $subpage );
250 }
251
252 /**
253 * Get a localised Title object for a specified special page name
254 *
255 * @param $name String
256 * @param $subpage String|Bool subpage string, or false to not use a subpage
257 * @throws MWException
258 * @return Title object
259 */
260 public static function getTitleFor( $name, $subpage = false ) {
261 $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
262 if ( $name ) {
263 return Title::makeTitle( NS_SPECIAL, $name );
264 } else {
265 throw new MWException( "Invalid special page name \"$name\"" );
266 }
267 }
268
269 /**
270 * Get a localised Title object for a page name with a possibly unvalidated subpage
271 *
272 * @param $name String
273 * @param $subpage String|Bool subpage string, or false to not use a subpage
274 * @return Title object or null if the page doesn't exist
275 */
276 public static function getSafeTitleFor( $name, $subpage = false ) {
277 $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
278 if ( $name ) {
279 return Title::makeTitleSafe( NS_SPECIAL, $name );
280 } else {
281 return null;
282 }
283 }
284
285 /**
286 * Get a title for a given alias
287 *
288 * @param $alias String
289 * @return Title or null if there is no such alias
290 * @deprecated since 1.18 call SpecialPageFactory method directly
291 */
292 static function getTitleForAlias( $alias ) {
293 wfDeprecated( __METHOD__, '1.18' );
294 return SpecialPageFactory::getTitleForAlias( $alias );
295 }
296
297 /**
298 * Default constructor for special pages
299 * Derivative classes should call this from their constructor
300 * Note that if the user does not have the required level, an error message will
301 * be displayed by the default execute() method, without the global function ever
302 * being called.
303 *
304 * If you override execute(), you can recover the default behaviour with userCanExecute()
305 * and displayRestrictionError()
306 *
307 * @param $name String: name of the special page, as seen in links and URLs
308 * @param $restriction String: user right required, e.g. "block" or "delete"
309 * @param $listed Bool: whether the page is listed in Special:Specialpages
310 * @param $function Callback|Bool: function called by execute(). By default it is constructed from $name
311 * @param $file String: file which is included by execute(). It is also constructed from $name by default
312 * @param $includable Bool: whether the page can be included in normal pages
313 */
314 public function __construct(
315 $name = '', $restriction = '', $listed = true,
316 $function = false, $file = 'default', $includable = false
317 ) {
318 $this->init( $name, $restriction, $listed, $function, $file, $includable );
319 }
320
321 /**
322 * Do the real work for the constructor, mainly so __call() can intercept
323 * calls to SpecialPage()
324 * @param $name String: name of the special page, as seen in links and URLs
325 * @param $restriction String: user right required, e.g. "block" or "delete"
326 * @param $listed Bool: whether the page is listed in Special:Specialpages
327 * @param $function Callback|Bool: function called by execute(). By default it is constructed from $name
328 * @param $file String: file which is included by execute(). It is also constructed from $name by default
329 * @param $includable Bool: whether the page can be included in normal pages
330 */
331 private function init( $name, $restriction, $listed, $function, $file, $includable ) {
332 $this->mName = $name;
333 $this->mRestriction = $restriction;
334 $this->mListed = $listed;
335 $this->mIncludable = $includable;
336 if ( !$function ) {
337 $this->mFunction = 'wfSpecial' . $name;
338 } else {
339 $this->mFunction = $function;
340 }
341 if ( $file === 'default' ) {
342 $this->mFile = __DIR__ . "/specials/Special$name.php";
343 } else {
344 $this->mFile = $file;
345 }
346 }
347
348 /**
349 * Use PHP's magic __call handler to get calls to the old PHP4 constructor
350 * because PHP E_STRICT yells at you for having __construct() and SpecialPage()
351 *
352 * @param $fName String Name of called method
353 * @param $a Array Arguments to the method
354 * @throws MWException
355 * @deprecated since 1.17, call parent::__construct()
356 */
357 public function __call( $fName, $a ) {
358 // Deprecated messages now, remove in 1.19 or 1.20?
359 wfDeprecated( __METHOD__, '1.17' );
360
361 // Sometimes $fName is SpecialPage, sometimes it's specialpage. <3 PHP
362 if ( strtolower( $fName ) == 'specialpage' ) {
363 $name = isset( $a[0] ) ? $a[0] : '';
364 $restriction = isset( $a[1] ) ? $a[1] : '';
365 $listed = isset( $a[2] ) ? $a[2] : true;
366 $function = isset( $a[3] ) ? $a[3] : false;
367 $file = isset( $a[4] ) ? $a[4] : 'default';
368 $includable = isset( $a[5] ) ? $a[5] : false;
369 $this->init( $name, $restriction, $listed, $function, $file, $includable );
370 } else {
371 $className = get_class( $this );
372 throw new MWException( "Call to undefined method $className::$fName" );
373 }
374 }
375
376 /**
377 * Get the name of this Special Page.
378 * @return String
379 */
380 function getName() {
381 return $this->mName;
382 }
383
384 /**
385 * Get the permission that a user must have to execute this page
386 * @return String
387 */
388 function getRestriction() {
389 return $this->mRestriction;
390 }
391
392 /**
393 * Get the file which will be included by SpecialPage::execute() if your extension is
394 * still stuck in the past and hasn't overridden the execute() method. No modern code
395 * should want or need to know this.
396 * @return String
397 * @deprecated since 1.18
398 */
399 function getFile() {
400 wfDeprecated( __METHOD__, '1.18' );
401 return $this->mFile;
402 }
403
404 // @todo FIXME: Decide which syntax to use for this, and stick to it
405 /**
406 * Whether this special page is listed in Special:SpecialPages
407 * @since r3583 (v1.3)
408 * @return Bool
409 */
410 function isListed() {
411 return $this->mListed;
412 }
413 /**
414 * Set whether this page is listed in Special:Specialpages, at run-time
415 * @since r3583 (v1.3)
416 * @param $listed Bool
417 * @return Bool
418 */
419 function setListed( $listed ) {
420 return wfSetVar( $this->mListed, $listed );
421 }
422 /**
423 * Get or set whether this special page is listed in Special:SpecialPages
424 * @since r11308 (v1.6)
425 * @param $x Bool
426 * @return Bool
427 */
428 function listed( $x = null ) {
429 return wfSetVar( $this->mListed, $x );
430 }
431
432 /**
433 * Whether it's allowed to transclude the special page via {{Special:Foo/params}}
434 * @return Bool
435 */
436 public function isIncludable() {
437 return $this->mIncludable;
438 }
439
440 /**
441 * These mutators are very evil, as the relevant variables should not mutate. So
442 * don't use them.
443 * @param $x Mixed
444 * @return Mixed
445 * @deprecated since 1.18
446 */
447 function name( $x = null ) { wfDeprecated( __METHOD__, '1.18' ); return wfSetVar( $this->mName, $x ); }
448
449 /**
450 * These mutators are very evil, as the relevant variables should not mutate. So
451 * don't use them.
452 * @param $x Mixed
453 * @return Mixed
454 * @deprecated since 1.18
455 */
456 function restriction( $x = null ) { wfDeprecated( __METHOD__, '1.18' ); return wfSetVar( $this->mRestriction, $x ); }
457
458 /**
459 * These mutators are very evil, as the relevant variables should not mutate. So
460 * don't use them.
461 * @param $x Mixed
462 * @return Mixed
463 * @deprecated since 1.18
464 */
465 function func( $x = null ) { wfDeprecated( __METHOD__, '1.18' ); return wfSetVar( $this->mFunction, $x ); }
466
467 /**
468 * These mutators are very evil, as the relevant variables should not mutate. So
469 * don't use them.
470 * @param $x Mixed
471 * @return Mixed
472 * @deprecated since 1.18
473 */
474 function file( $x = null ) { wfDeprecated( __METHOD__, '1.18' ); return wfSetVar( $this->mFile, $x ); }
475
476 /**
477 * These mutators are very evil, as the relevant variables should not mutate. So
478 * don't use them.
479 * @param $x Mixed
480 * @return Mixed
481 * @deprecated since 1.18
482 */
483 function includable( $x = null ) { wfDeprecated( __METHOD__, '1.18' ); return wfSetVar( $this->mIncludable, $x ); }
484
485 /**
486 * Whether the special page is being evaluated via transclusion
487 * @param $x Bool
488 * @return Bool
489 */
490 function including( $x = null ) {
491 return wfSetVar( $this->mIncluding, $x );
492 }
493
494 /**
495 * Get the localised name of the special page
496 */
497 function getLocalName() {
498 if ( !isset( $this->mLocalName ) ) {
499 $this->mLocalName = SpecialPageFactory::getLocalNameFor( $this->mName );
500 }
501 return $this->mLocalName;
502 }
503
504 /**
505 * Is this page expensive (for some definition of expensive)?
506 * Expensive pages are disabled or cached in miser mode. Originally used
507 * (and still overridden) by QueryPage and subclasses, moved here so that
508 * Special:SpecialPages can safely call it for all special pages.
509 *
510 * @return Boolean
511 */
512 public function isExpensive() {
513 return false;
514 }
515
516 /**
517 * Is this page cached?
518 * Expensive pages are cached or disabled in miser mode.
519 * Used by QueryPage and subclasses, moved here so that
520 * Special:SpecialPages can safely call it for all special pages.
521 *
522 * @return Boolean
523 * @since 1.21
524 */
525 public function isCached() {
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 // DWIM: If all anons can do something, then it is not restricted
538 return $this->mRestriction != '' && !User::groupHasPermission( '*', $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 * Entry point.
595 *
596 * @since 1.20
597 *
598 * @param $subPage string|null
599 */
600 public final function run( $subPage ) {
601 /**
602 * Gets called before @see SpecialPage::execute.
603 *
604 * @since 1.20
605 *
606 * @param $special SpecialPage
607 * @param $subPage string|null
608 */
609 wfRunHooks( 'SpecialPageBeforeExecute', array( $this, $subPage ) );
610
611 $this->beforeExecute( $subPage );
612 $this->execute( $subPage );
613 $this->afterExecute( $subPage );
614
615 /**
616 * Gets called after @see SpecialPage::execute.
617 *
618 * @since 1.20
619 *
620 * @param $special SpecialPage
621 * @param $subPage string|null
622 */
623 wfRunHooks( 'SpecialPageAfterExecute', array( $this, $subPage ) );
624 }
625
626 /**
627 * Gets called before @see SpecialPage::execute.
628 *
629 * @since 1.20
630 *
631 * @param $subPage string|null
632 */
633 protected function beforeExecute( $subPage ) {
634 // No-op
635 }
636
637 /**
638 * Gets called after @see SpecialPage::execute.
639 *
640 * @since 1.20
641 *
642 * @param $subPage string|null
643 */
644 protected function afterExecute( $subPage ) {
645 // No-op
646 }
647
648 /**
649 * Default execute method
650 * Checks user permissions, calls the function given in mFunction
651 *
652 * This must be overridden by subclasses; it will be made abstract in a future version
653 *
654 * @param $subPage string|null
655 */
656 public function execute( $subPage ) {
657 $this->setHeaders();
658 $this->checkPermissions();
659
660 $func = $this->mFunction;
661 // only load file if the function does not exist
662 if ( !is_callable( $func ) && $this->mFile ) {
663 require_once( $this->mFile );
664 }
665 $this->outputHeader();
666 call_user_func( $func, $subPage, $this );
667 }
668
669 /**
670 * Outputs a summary message on top of special pages
671 * Per default the message key is the canonical name of the special page
672 * May be overriden, i.e. by extensions to stick with the naming conventions
673 * for message keys: 'extensionname-xxx'
674 *
675 * @param $summaryMessageKey String: message key of the summary
676 */
677 function outputHeader( $summaryMessageKey = '' ) {
678 global $wgContLang;
679
680 if ( $summaryMessageKey == '' ) {
681 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
682 } else {
683 $msg = $summaryMessageKey;
684 }
685 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
686 $this->getOutput()->wrapWikiMsg(
687 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
688 }
689
690 }
691
692 /**
693 * Returns the name that goes in the \<h1\> in the special page itself, and
694 * also the name that will be listed in Special:Specialpages
695 *
696 * Derived classes can override this, but usually it is easier to keep the
697 * default behaviour. Messages can be added at run-time, see
698 * MessageCache.php.
699 *
700 * @return String
701 */
702 function getDescription() {
703 return $this->msg( strtolower( $this->mName ) )->text();
704 }
705
706 /**
707 * Get a self-referential title object
708 *
709 * @param $subpage String|Bool
710 * @return Title object
711 */
712 function getTitle( $subpage = false ) {
713 return self::getTitleFor( $this->mName, $subpage );
714 }
715
716 /**
717 * Sets the context this SpecialPage is executed in
718 *
719 * @param $context IContextSource
720 * @since 1.18
721 */
722 public function setContext( $context ) {
723 $this->mContext = $context;
724 }
725
726 /**
727 * Gets the context this SpecialPage is executed in
728 *
729 * @return IContextSource|RequestContext
730 * @since 1.18
731 */
732 public function getContext() {
733 if ( $this->mContext instanceof IContextSource ) {
734 return $this->mContext;
735 } else {
736 wfDebug( __METHOD__ . " called and \$mContext is null. Return RequestContext::getMain(); for sanity\n" );
737 return RequestContext::getMain();
738 }
739 }
740
741 /**
742 * Get the WebRequest being used for this instance
743 *
744 * @return WebRequest
745 * @since 1.18
746 */
747 public function getRequest() {
748 return $this->getContext()->getRequest();
749 }
750
751 /**
752 * Get the OutputPage being used for this instance
753 *
754 * @return OutputPage
755 * @since 1.18
756 */
757 public function getOutput() {
758 return $this->getContext()->getOutput();
759 }
760
761 /**
762 * Shortcut to get the User executing this instance
763 *
764 * @return User
765 * @since 1.18
766 */
767 public function getUser() {
768 return $this->getContext()->getUser();
769 }
770
771 /**
772 * Shortcut to get the skin being used for this instance
773 *
774 * @return Skin
775 * @since 1.18
776 */
777 public function getSkin() {
778 return $this->getContext()->getSkin();
779 }
780
781 /**
782 * Shortcut to get user's language
783 *
784 * @deprecated 1.19 Use getLanguage instead
785 * @return Language
786 * @since 1.18
787 */
788 public function getLang() {
789 wfDeprecated( __METHOD__, '1.19' );
790 return $this->getLanguage();
791 }
792
793 /**
794 * Shortcut to get user's language
795 *
796 * @return Language
797 * @since 1.19
798 */
799 public function getLanguage() {
800 return $this->getContext()->getLanguage();
801 }
802
803 /**
804 * Return the full title, including $par
805 *
806 * @return Title
807 * @since 1.18
808 */
809 public function getFullTitle() {
810 return $this->getContext()->getTitle();
811 }
812
813 /**
814 * Wrapper around wfMessage that sets the current context.
815 *
816 * @return Message
817 * @see wfMessage
818 */
819 public function msg( /* $args */ ) {
820 // Note: can't use func_get_args() directly as second or later item in
821 // a parameter list until PHP 5.3 or you get a fatal error.
822 // Works fine as the first parameter, which appears elsewhere in the
823 // code base. Sighhhh.
824 $args = func_get_args();
825 $message = call_user_func_array( array( $this->getContext(), 'msg' ), $args );
826 // RequestContext passes context to wfMessage, and the language is set from
827 // the context, but setting the language for Message class removes the
828 // interface message status, which breaks for example usernameless gender
829 // invokations. Restore the flag when not including special page in content.
830 if ( $this->including() ) {
831 $message->setInterfaceMessageFlag( false );
832 }
833 return $message;
834 }
835
836 /**
837 * Adds RSS/atom links
838 *
839 * @param $params array
840 */
841 protected function addFeedLinks( $params ) {
842 global $wgFeedClasses;
843
844 $feedTemplate = wfScript( 'api' ) . '?';
845
846 foreach ( $wgFeedClasses as $format => $class ) {
847 $theseParams = $params + array( 'feedformat' => $format );
848 $url = $feedTemplate . wfArrayToCGI( $theseParams );
849 $this->getOutput()->addFeedLink( $format, $url );
850 }
851 }
852 }
853
854 /**
855 * Special page which uses an HTMLForm to handle processing. This is mostly a
856 * clone of FormAction. More special pages should be built this way; maybe this could be
857 * a new structure for SpecialPages
858 */
859 abstract class FormSpecialPage extends SpecialPage {
860
861 /**
862 * Get an HTMLForm descriptor array
863 * @return Array
864 */
865 protected abstract function getFormFields();
866
867 /**
868 * Add pre- or post-text to the form
869 * @return String HTML which will be sent to $form->addPreText()
870 */
871 protected function preText() { return ''; }
872 protected function postText() { return ''; }
873
874 /**
875 * Play with the HTMLForm if you need to more substantially
876 * @param $form HTMLForm
877 */
878 protected function alterForm( HTMLForm $form ) {}
879
880 /**
881 * Get the HTMLForm to control behaviour
882 * @return HTMLForm|null
883 */
884 protected function getForm() {
885 $this->fields = $this->getFormFields();
886
887 $form = new HTMLForm( $this->fields, $this->getContext() );
888 $form->setSubmitCallback( array( $this, 'onSubmit' ) );
889 $form->setWrapperLegend( $this->msg( strtolower( $this->getName() ) . '-legend' ) );
890 $form->addHeaderText(
891 $this->msg( strtolower( $this->getName() ) . '-text' )->parseAsBlock() );
892
893 // Retain query parameters (uselang etc)
894 $params = array_diff_key(
895 $this->getRequest()->getQueryValues(), array( 'title' => null ) );
896 $form->addHiddenField( 'redirectparams', wfArrayToCGI( $params ) );
897
898 $form->addPreText( $this->preText() );
899 $form->addPostText( $this->postText() );
900 $this->alterForm( $form );
901
902 // Give hooks a chance to alter the form, adding extra fields or text etc
903 wfRunHooks( "Special{$this->getName()}BeforeFormDisplay", array( &$form ) );
904
905 return $form;
906 }
907
908 /**
909 * Process the form on POST submission.
910 * @param $data Array
911 * @return Bool|Array true for success, false for didn't-try, array of errors on failure
912 */
913 public abstract function onSubmit( array $data );
914
915 /**
916 * Do something exciting on successful processing of the form, most likely to show a
917 * confirmation message
918 */
919 public abstract function onSuccess();
920
921 /**
922 * Basic SpecialPage workflow: get a form, send it to the user; get some data back,
923 *
924 * @param $par String Subpage string if one was specified
925 */
926 public function execute( $par ) {
927 $this->setParameter( $par );
928 $this->setHeaders();
929
930 // This will throw exceptions if there's a problem
931 $this->checkExecutePermissions( $this->getUser() );
932
933 $form = $this->getForm();
934 if ( $form->show() ) {
935 $this->onSuccess();
936 }
937 }
938
939 /**
940 * Maybe do something interesting with the subpage parameter
941 * @param $par String
942 */
943 protected function setParameter( $par ) {}
944
945 /**
946 * Called from execute() to check if the given user can perform this action.
947 * Failures here must throw subclasses of ErrorPageError.
948 * @param $user User
949 * @throws UserBlockedError
950 * @return Bool true
951 */
952 protected function checkExecutePermissions( User $user ) {
953 $this->checkPermissions();
954
955 if ( $this->requiresUnblock() && $user->isBlocked() ) {
956 $block = $user->getBlock();
957 throw new UserBlockedError( $block );
958 }
959
960 if ( $this->requiresWrite() ) {
961 $this->checkReadOnly();
962 }
963
964 return true;
965 }
966
967 /**
968 * Whether this action requires the wiki not to be locked
969 * @return Bool
970 */
971 public function requiresWrite() {
972 return true;
973 }
974
975 /**
976 * Whether this action cannot be executed by a blocked user
977 * @return Bool
978 */
979 public function requiresUnblock() {
980 return true;
981 }
982 }
983
984 /**
985 * Shortcut to construct a special page which is unlisted by default
986 * @ingroup SpecialPage
987 */
988 class UnlistedSpecialPage extends SpecialPage {
989 function __construct( $name, $restriction = '', $function = false, $file = 'default' ) {
990 parent::__construct( $name, $restriction, false, $function, $file );
991 }
992
993 public function isListed() {
994 return false;
995 }
996 }
997
998 /**
999 * Shortcut to construct an includable special page
1000 * @ingroup SpecialPage
1001 */
1002 class IncludableSpecialPage extends SpecialPage {
1003 function __construct(
1004 $name, $restriction = '', $listed = true, $function = false, $file = 'default'
1005 ) {
1006 parent::__construct( $name, $restriction, $listed, $function, $file, true );
1007 }
1008
1009 public function isIncludable() {
1010 return true;
1011 }
1012 }
1013
1014 /**
1015 * Shortcut to construct a special page alias.
1016 * @ingroup SpecialPage
1017 */
1018 abstract class RedirectSpecialPage extends UnlistedSpecialPage {
1019
1020 // Query parameters that can be passed through redirects
1021 protected $mAllowedRedirectParams = array();
1022
1023 // Query parameteres added by redirects
1024 protected $mAddedRedirectParams = array();
1025
1026 public function execute( $par ) {
1027 $redirect = $this->getRedirect( $par );
1028 $query = $this->getRedirectQuery();
1029 // Redirect to a page title with possible query parameters
1030 if ( $redirect instanceof Title ) {
1031 $url = $redirect->getFullUrl( $query );
1032 $this->getOutput()->redirect( $url );
1033 return $redirect;
1034 // Redirect to index.php with query parameters
1035 } elseif ( $redirect === true ) {
1036 global $wgScript;
1037 $url = $wgScript . '?' . wfArrayToCGI( $query );
1038 $this->getOutput()->redirect( $url );
1039 return $redirect;
1040 } else {
1041 $class = __CLASS__;
1042 throw new MWException( "RedirectSpecialPage $class doesn't redirect!" );
1043 }
1044 }
1045
1046 /**
1047 * If the special page is a redirect, then get the Title object it redirects to.
1048 * False otherwise.
1049 *
1050 * @param $par String Subpage string
1051 * @return Title|bool
1052 */
1053 abstract public function getRedirect( $par );
1054
1055 /**
1056 * Return part of the request string for a special redirect page
1057 * This allows passing, e.g. action=history to Special:Mypage, etc.
1058 *
1059 * @return String
1060 */
1061 public function getRedirectQuery() {
1062 $params = array();
1063
1064 foreach ( $this->mAllowedRedirectParams as $arg ) {
1065 if ( $this->getRequest()->getVal( $arg, null ) !== null ) {
1066 $params[$arg] = $this->getRequest()->getVal( $arg );
1067 }
1068 }
1069
1070 foreach ( $this->mAddedRedirectParams as $arg => $val ) {
1071 $params[$arg] = $val;
1072 }
1073
1074 return count( $params )
1075 ? $params
1076 : false;
1077 }
1078 }
1079
1080 abstract class SpecialRedirectToSpecial extends RedirectSpecialPage {
1081 var $redirName, $redirSubpage;
1082
1083 function __construct(
1084 $name, $redirName, $redirSubpage = false,
1085 $allowedRedirectParams = array(), $addedRedirectParams = array()
1086 ) {
1087 parent::__construct( $name );
1088 $this->redirName = $redirName;
1089 $this->redirSubpage = $redirSubpage;
1090 $this->mAllowedRedirectParams = $allowedRedirectParams;
1091 $this->mAddedRedirectParams = $addedRedirectParams;
1092 }
1093
1094 public function getRedirect( $subpage ) {
1095 if ( $this->redirSubpage === false ) {
1096 return SpecialPage::getTitleFor( $this->redirName, $subpage );
1097 } else {
1098 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
1099 }
1100 }
1101 }
1102
1103 /**
1104 * ListAdmins --> ListUsers/sysop
1105 */
1106 class SpecialListAdmins extends SpecialRedirectToSpecial {
1107 function __construct() {
1108 parent::__construct( 'Listadmins', 'Listusers', 'sysop' );
1109 }
1110 }
1111
1112 /**
1113 * ListBots --> ListUsers/bot
1114 */
1115 class SpecialListBots extends SpecialRedirectToSpecial {
1116 function __construct() {
1117 parent::__construct( 'Listbots', 'Listusers', 'bot' );
1118 }
1119 }
1120
1121 /**
1122 * CreateAccount --> UserLogin/signup
1123 * @todo FIXME: This (and the rest of the login frontend) needs to die a horrible painful death
1124 */
1125 class SpecialCreateAccount extends SpecialRedirectToSpecial {
1126 function __construct() {
1127 parent::__construct( 'CreateAccount', 'Userlogin', 'signup', array( 'uselang' ) );
1128 }
1129 }
1130 /**
1131 * SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
1132 * are used to get user independant links pointing to the user page, talk
1133 * page and list of contributions.
1134 * This can let us cache a single copy of any generated content for all
1135 * users.
1136 */
1137
1138 /**
1139 * Superclass for any RedirectSpecialPage which redirects the user
1140 * to a particular article (as opposed to user contributions, logs, etc.).
1141 *
1142 * For security reasons these special pages are restricted to pass on
1143 * the following subset of GET parameters to the target page while
1144 * removing all others:
1145 *
1146 * - useskin, uselang, printable: to alter the appearance of the resulting page
1147 *
1148 * - redirect: allows viewing one's user page or talk page even if it is a
1149 * redirect.
1150 *
1151 * - rdfrom: allows redirecting to one's user page or talk page from an
1152 * external wiki with the "Redirect from..." notice.
1153 *
1154 * - limit, offset: Useful for linking to history of one's own user page or
1155 * user talk page. For example, this would be a link to "the last edit to your
1156 * user talk page in the year 2010":
1157 * http://en.wikipedia.org/w/index.php?title=Special:MyPage&offset=20110000000000&limit=1&action=history
1158 *
1159 * - feed: would allow linking to the current user's RSS feed for their user
1160 * talk page:
1161 * http://en.wikipedia.org/w/index.php?title=Special:MyTalk&action=history&feed=rss
1162 *
1163 * - preloadtitle: Can be used to provide a default section title for a
1164 * preloaded new comment on one's own talk page.
1165 *
1166 * - summary : Can be used to provide a default edit summary for a preloaded
1167 * edit to one's own user page or talk page.
1168 *
1169 * - preview: Allows showing/hiding preview on first edit regardless of user
1170 * preference, useful for preloaded edits where you know preview wouldn't be
1171 * useful.
1172 *
1173 * - internaledit, externaledit, mode: Allows forcing the use of the
1174 * internal/external editor, e.g. to force the internal editor for
1175 * short/simple preloaded edits.
1176 *
1177 * - redlink: Affects the message the user sees if their talk page/user talk
1178 * page does not currently exist. Avoids confusion for newbies with no user
1179 * pages over why they got a "permission error" following this link:
1180 * http://en.wikipedia.org/w/index.php?title=Special:MyPage&redlink=1
1181 *
1182 * - debug: determines whether the debug parameter is passed to load.php,
1183 * which disables reformatting and allows scripts to be debugged. Useful
1184 * when debugging scripts that manipulate one's own user page or talk page.
1185 *
1186 * @par Hook extension:
1187 * Extensions can add to the redirect parameters list by using the hook
1188 * RedirectSpecialArticleRedirectParams
1189 *
1190 * This hook allows extensions which add GET parameters like FlaggedRevs to
1191 * retain those parameters when redirecting using special pages.
1192 *
1193 * @par Hook extension example:
1194 * @code
1195 * $wgHooks['RedirectSpecialArticleRedirectParams'][] =
1196 * 'MyExtensionHooks::onRedirectSpecialArticleRedirectParams';
1197 * public static function onRedirectSpecialArticleRedirectParams( &$redirectParams ) {
1198 * $redirectParams[] = 'stable';
1199 * return true;
1200 * }
1201 * @endcode
1202 * @ingroup SpecialPage
1203 */
1204 abstract class RedirectSpecialArticle extends RedirectSpecialPage {
1205 function __construct( $name ) {
1206 parent::__construct( $name );
1207 $redirectParams = array(
1208 'action',
1209 'redirect', 'rdfrom',
1210 # Options for preloaded edits
1211 'preload', 'editintro', 'preloadtitle', 'summary',
1212 # Options for overriding user settings
1213 'preview', 'internaledit', 'externaledit', 'mode',
1214 # Options for history/diffs
1215 'section', 'oldid', 'diff', 'dir',
1216 'limit', 'offset', 'feed',
1217 # Misc options
1218 'redlink', 'debug',
1219 # Options for action=raw; missing ctype can break JS or CSS in some browsers
1220 'ctype', 'maxage', 'smaxage',
1221 );
1222
1223 wfRunHooks( "RedirectSpecialArticleRedirectParams", array( &$redirectParams ) );
1224 $this->mAllowedRedirectParams = $redirectParams;
1225 }
1226 }
1227
1228 /**
1229 * Shortcut to construct a special page pointing to current user user's page.
1230 * @ingroup SpecialPage
1231 */
1232 class SpecialMypage extends RedirectSpecialArticle {
1233 function __construct() {
1234 parent::__construct( 'Mypage' );
1235 }
1236
1237 function getRedirect( $subpage ) {
1238 if ( strval( $subpage ) !== '' ) {
1239 return Title::makeTitle( NS_USER, $this->getUser()->getName() . '/' . $subpage );
1240 } else {
1241 return Title::makeTitle( NS_USER, $this->getUser()->getName() );
1242 }
1243 }
1244 }
1245
1246 /**
1247 * Shortcut to construct a special page pointing to current user talk page.
1248 * @ingroup SpecialPage
1249 */
1250 class SpecialMytalk extends RedirectSpecialArticle {
1251 function __construct() {
1252 parent::__construct( 'Mytalk' );
1253 }
1254
1255 function getRedirect( $subpage ) {
1256 if ( strval( $subpage ) !== '' ) {
1257 return Title::makeTitle( NS_USER_TALK, $this->getUser()->getName() . '/' . $subpage );
1258 } else {
1259 return Title::makeTitle( NS_USER_TALK, $this->getUser()->getName() );
1260 }
1261 }
1262 }
1263
1264 /**
1265 * Shortcut to construct a special page pointing to current user contributions.
1266 * @ingroup SpecialPage
1267 */
1268 class SpecialMycontributions extends RedirectSpecialPage {
1269 function __construct() {
1270 parent::__construct( 'Mycontributions' );
1271 $this->mAllowedRedirectParams = array( 'limit', 'namespace', 'tagfilter',
1272 'offset', 'dir', 'year', 'month', 'feed' );
1273 }
1274
1275 function getRedirect( $subpage ) {
1276 return SpecialPage::getTitleFor( 'Contributions', $this->getUser()->getName() );
1277 }
1278 }
1279
1280 /**
1281 * Redirect to Special:Listfiles?user=$wgUser
1282 */
1283 class SpecialMyuploads extends RedirectSpecialPage {
1284 function __construct() {
1285 parent::__construct( 'Myuploads' );
1286 $this->mAllowedRedirectParams = array( 'limit' );
1287 }
1288
1289 function getRedirect( $subpage ) {
1290 return SpecialPage::getTitleFor( 'Listfiles', $this->getUser()->getName() );
1291 }
1292 }
1293
1294 /**
1295 * Redirect from Special:PermanentLink/### to index.php?oldid=###
1296 */
1297 class SpecialPermanentLink extends RedirectSpecialPage {
1298 function __construct() {
1299 parent::__construct( 'PermanentLink' );
1300 $this->mAllowedRedirectParams = array();
1301 }
1302
1303 function getRedirect( $subpage ) {
1304 $subpage = intval( $subpage );
1305 if ( $subpage === 0 ) {
1306 # throw an error page when no subpage was given
1307 throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
1308 }
1309 $this->mAddedRedirectParams['oldid'] = $subpage;
1310 return true;
1311 }
1312 }