Merge "Fix behavior of Hooks class."
[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 * @return Title object
258 */
259 public static function getTitleFor( $name, $subpage = false ) {
260 $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
261 if ( $name ) {
262 return Title::makeTitle( NS_SPECIAL, $name );
263 } else {
264 throw new MWException( "Invalid special page name \"$name\"" );
265 }
266 }
267
268 /**
269 * Get a localised Title object for a page name with a possibly unvalidated subpage
270 *
271 * @param $name String
272 * @param $subpage String|Bool subpage string, or false to not use a subpage
273 * @return Title object or null if the page doesn't exist
274 */
275 public static function getSafeTitleFor( $name, $subpage = false ) {
276 $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
277 if ( $name ) {
278 return Title::makeTitleSafe( NS_SPECIAL, $name );
279 } else {
280 return null;
281 }
282 }
283
284 /**
285 * Get a title for a given alias
286 *
287 * @param $alias String
288 * @return Title or null if there is no such alias
289 * @deprecated since 1.18 call SpecialPageFactory method directly
290 */
291 static function getTitleForAlias( $alias ) {
292 wfDeprecated( __METHOD__, '1.18' );
293 return SpecialPageFactory::getTitleForAlias( $alias );
294 }
295
296 /**
297 * Default constructor for special pages
298 * Derivative classes should call this from their constructor
299 * Note that if the user does not have the required level, an error message will
300 * be displayed by the default execute() method, without the global function ever
301 * being called.
302 *
303 * If you override execute(), you can recover the default behaviour with userCanExecute()
304 * and displayRestrictionError()
305 *
306 * @param $name String: name of the special page, as seen in links and URLs
307 * @param $restriction String: user right required, e.g. "block" or "delete"
308 * @param $listed Bool: whether the page is listed in Special:Specialpages
309 * @param $function Callback|Bool: function called by execute(). By default it is constructed from $name
310 * @param $file String: file which is included by execute(). It is also constructed from $name by default
311 * @param $includable Bool: whether the page can be included in normal pages
312 */
313 public function __construct(
314 $name = '', $restriction = '', $listed = true,
315 $function = false, $file = 'default', $includable = false
316 ) {
317 $this->init( $name, $restriction, $listed, $function, $file, $includable );
318 }
319
320 /**
321 * Do the real work for the constructor, mainly so __call() can intercept
322 * calls to SpecialPage()
323 * @param $name String: name of the special page, as seen in links and URLs
324 * @param $restriction String: user right required, e.g. "block" or "delete"
325 * @param $listed Bool: whether the page is listed in Special:Specialpages
326 * @param $function Callback|Bool: function called by execute(). By default it is constructed from $name
327 * @param $file String: file which is included by execute(). It is also constructed from $name by default
328 * @param $includable Bool: whether the page can be included in normal pages
329 */
330 private function init( $name, $restriction, $listed, $function, $file, $includable ) {
331 $this->mName = $name;
332 $this->mRestriction = $restriction;
333 $this->mListed = $listed;
334 $this->mIncludable = $includable;
335 if ( !$function ) {
336 $this->mFunction = 'wfSpecial' . $name;
337 } else {
338 $this->mFunction = $function;
339 }
340 if ( $file === 'default' ) {
341 $this->mFile = __DIR__ . "/specials/Special$name.php";
342 } else {
343 $this->mFile = $file;
344 }
345 }
346
347 /**
348 * Use PHP's magic __call handler to get calls to the old PHP4 constructor
349 * because PHP E_STRICT yells at you for having __construct() and SpecialPage()
350 *
351 * @param $fName String Name of called method
352 * @param $a Array Arguments to the method
353 * @deprecated since 1.17, call parent::__construct()
354 */
355 public function __call( $fName, $a ) {
356 // Deprecated messages now, remove in 1.19 or 1.20?
357 wfDeprecated( __METHOD__, '1.17' );
358
359 // Sometimes $fName is SpecialPage, sometimes it's specialpage. <3 PHP
360 if ( strtolower( $fName ) == 'specialpage' ) {
361 $name = isset( $a[0] ) ? $a[0] : '';
362 $restriction = isset( $a[1] ) ? $a[1] : '';
363 $listed = isset( $a[2] ) ? $a[2] : true;
364 $function = isset( $a[3] ) ? $a[3] : false;
365 $file = isset( $a[4] ) ? $a[4] : 'default';
366 $includable = isset( $a[5] ) ? $a[5] : false;
367 $this->init( $name, $restriction, $listed, $function, $file, $includable );
368 } else {
369 $className = get_class( $this );
370 throw new MWException( "Call to undefined method $className::$fName" );
371 }
372 }
373
374 /**
375 * Get the name of this Special Page.
376 * @return String
377 */
378 function getName() {
379 return $this->mName;
380 }
381
382 /**
383 * Get the permission that a user must have to execute this page
384 * @return String
385 */
386 function getRestriction() {
387 return $this->mRestriction;
388 }
389
390 /**
391 * Get the file which will be included by SpecialPage::execute() if your extension is
392 * still stuck in the past and hasn't overridden the execute() method. No modern code
393 * should want or need to know this.
394 * @return String
395 * @deprecated since 1.18
396 */
397 function getFile() {
398 wfDeprecated( __METHOD__, '1.18' );
399 return $this->mFile;
400 }
401
402 // @todo FIXME: Decide which syntax to use for this, and stick to it
403 /**
404 * Whether this special page is listed in Special:SpecialPages
405 * @since r3583 (v1.3)
406 * @return Bool
407 */
408 function isListed() {
409 return $this->mListed;
410 }
411 /**
412 * Set whether this page is listed in Special:Specialpages, at run-time
413 * @since r3583 (v1.3)
414 * @param $listed Bool
415 * @return Bool
416 */
417 function setListed( $listed ) {
418 return wfSetVar( $this->mListed, $listed );
419 }
420 /**
421 * Get or set whether this special page is listed in Special:SpecialPages
422 * @since r11308 (v1.6)
423 * @param $x Bool
424 * @return Bool
425 */
426 function listed( $x = null ) {
427 return wfSetVar( $this->mListed, $x );
428 }
429
430 /**
431 * Whether it's allowed to transclude the special page via {{Special:Foo/params}}
432 * @return Bool
433 */
434 public function isIncludable() {
435 return $this->mIncludable;
436 }
437
438 /**
439 * These mutators are very evil, as the relevant variables should not mutate. So
440 * don't use them.
441 * @param $x Mixed
442 * @return Mixed
443 * @deprecated since 1.18
444 */
445 function name( $x = null ) { wfDeprecated( __METHOD__, '1.18' ); return wfSetVar( $this->mName, $x ); }
446
447 /**
448 * These mutators are very evil, as the relevant variables should not mutate. So
449 * don't use them.
450 * @param $x Mixed
451 * @return Mixed
452 * @deprecated since 1.18
453 */
454 function restriction( $x = null ) { wfDeprecated( __METHOD__, '1.18' ); return wfSetVar( $this->mRestriction, $x ); }
455
456 /**
457 * These mutators are very evil, as the relevant variables should not mutate. So
458 * don't use them.
459 * @param $x Mixed
460 * @return Mixed
461 * @deprecated since 1.18
462 */
463 function func( $x = null ) { wfDeprecated( __METHOD__, '1.18' ); return wfSetVar( $this->mFunction, $x ); }
464
465 /**
466 * These mutators are very evil, as the relevant variables should not mutate. So
467 * don't use them.
468 * @param $x Mixed
469 * @return Mixed
470 * @deprecated since 1.18
471 */
472 function file( $x = null ) { wfDeprecated( __METHOD__, '1.18' ); return wfSetVar( $this->mFile, $x ); }
473
474 /**
475 * These mutators are very evil, as the relevant variables should not mutate. So
476 * don't use them.
477 * @param $x Mixed
478 * @return Mixed
479 * @deprecated since 1.18
480 */
481 function includable( $x = null ) { wfDeprecated( __METHOD__, '1.18' ); return wfSetVar( $this->mIncludable, $x ); }
482
483 /**
484 * Whether the special page is being evaluated via transclusion
485 * @param $x Bool
486 * @return Bool
487 */
488 function including( $x = null ) {
489 return wfSetVar( $this->mIncluding, $x );
490 }
491
492 /**
493 * Get the localised name of the special page
494 */
495 function getLocalName() {
496 if ( !isset( $this->mLocalName ) ) {
497 $this->mLocalName = SpecialPageFactory::getLocalNameFor( $this->mName );
498 }
499 return $this->mLocalName;
500 }
501
502 /**
503 * Is this page expensive (for some definition of expensive)?
504 * Expensive pages are disabled or cached in miser mode. Originally used
505 * (and still overridden) by QueryPage and subclasses, moved here so that
506 * Special:SpecialPages can safely call it for all special pages.
507 *
508 * @return Boolean
509 */
510 public function isExpensive() {
511 return false;
512 }
513
514 /**
515 * Can be overridden by subclasses with more complicated permissions
516 * schemes.
517 *
518 * @return Boolean: should the page be displayed with the restricted-access
519 * pages?
520 */
521 public function isRestricted() {
522 global $wgGroupPermissions;
523 // DWIM: If all anons can do something, then it is not restricted
524 return $this->mRestriction != '' && empty( $wgGroupPermissions['*'][$this->mRestriction] );
525 }
526
527 /**
528 * Checks if the given user (identified by an object) can execute this
529 * special page (as defined by $mRestriction). Can be overridden by sub-
530 * classes with more complicated permissions schemes.
531 *
532 * @param $user User: the user to check
533 * @return Boolean: does the user have permission to view the page?
534 */
535 public function userCanExecute( User $user ) {
536 return $user->isAllowed( $this->mRestriction );
537 }
538
539 /**
540 * Output an error message telling the user what access level they have to have
541 */
542 function displayRestrictionError() {
543 throw new PermissionsError( $this->mRestriction );
544 }
545
546 /**
547 * Checks if userCanExecute, and if not throws a PermissionsError
548 *
549 * @since 1.19
550 */
551 public function checkPermissions() {
552 if ( !$this->userCanExecute( $this->getUser() ) ) {
553 $this->displayRestrictionError();
554 }
555 }
556
557 /**
558 * If the wiki is currently in readonly mode, throws a ReadOnlyError
559 *
560 * @since 1.19
561 * @throws ReadOnlyError
562 */
563 public function checkReadOnly() {
564 if ( wfReadOnly() ) {
565 throw new ReadOnlyError;
566 }
567 }
568
569 /**
570 * Sets headers - this should be called from the execute() method of all derived classes!
571 */
572 function setHeaders() {
573 $out = $this->getOutput();
574 $out->setArticleRelated( false );
575 $out->setRobotPolicy( "noindex,nofollow" );
576 $out->setPageTitle( $this->getDescription() );
577 }
578
579 /**
580 * Entry point.
581 *
582 * @since 1.20
583 *
584 * @param $subPage string|null
585 */
586 public final function run( $subPage ) {
587 /**
588 * Gets called before @see SpecialPage::execute.
589 *
590 * @since 1.20
591 *
592 * @param $special SpecialPage
593 * @param $subPage string|null
594 */
595 wfRunHooks( 'SpecialPageBeforeExecute', array( $this, $subPage ) );
596
597 $this->beforeExecute( $subPage );
598 $this->execute( $subPage );
599 $this->afterExecute( $subPage );
600
601 /**
602 * Gets called after @see SpecialPage::execute.
603 *
604 * @since 1.20
605 *
606 * @param $special SpecialPage
607 * @param $subPage string|null
608 */
609 wfRunHooks( 'SpecialPageAfterExecute', array( $this, $subPage ) );
610 }
611
612 /**
613 * Gets called before @see SpecialPage::execute.
614 *
615 * @since 1.20
616 *
617 * @param $subPage string|null
618 */
619 protected function beforeExecute( $subPage ) {
620 // No-op
621 }
622
623 /**
624 * Gets called after @see SpecialPage::execute.
625 *
626 * @since 1.20
627 *
628 * @param $subPage string|null
629 */
630 protected function afterExecute( $subPage ) {
631 // No-op
632 }
633
634 /**
635 * Default execute method
636 * Checks user permissions, calls the function given in mFunction
637 *
638 * This must be overridden by subclasses; it will be made abstract in a future version
639 *
640 * @param $subPage string|null
641 */
642 public function execute( $subPage ) {
643 $this->setHeaders();
644 $this->checkPermissions();
645
646 $func = $this->mFunction;
647 // only load file if the function does not exist
648 if ( !is_callable( $func ) && $this->mFile ) {
649 require_once( $this->mFile );
650 }
651 $this->outputHeader();
652 call_user_func( $func, $subPage, $this );
653 }
654
655 /**
656 * Outputs a summary message on top of special pages
657 * Per default the message key is the canonical name of the special page
658 * May be overriden, i.e. by extensions to stick with the naming conventions
659 * for message keys: 'extensionname-xxx'
660 *
661 * @param $summaryMessageKey String: message key of the summary
662 */
663 function outputHeader( $summaryMessageKey = '' ) {
664 global $wgContLang;
665
666 if ( $summaryMessageKey == '' ) {
667 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
668 } else {
669 $msg = $summaryMessageKey;
670 }
671 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
672 $this->getOutput()->wrapWikiMsg(
673 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
674 }
675
676 }
677
678 /**
679 * Returns the name that goes in the \<h1\> in the special page itself, and
680 * also the name that will be listed in Special:Specialpages
681 *
682 * Derived classes can override this, but usually it is easier to keep the
683 * default behaviour. Messages can be added at run-time, see
684 * MessageCache.php.
685 *
686 * @return String
687 */
688 function getDescription() {
689 return $this->msg( strtolower( $this->mName ) )->text();
690 }
691
692 /**
693 * Get a self-referential title object
694 *
695 * @param $subpage String|Bool
696 * @return Title object
697 */
698 function getTitle( $subpage = false ) {
699 return self::getTitleFor( $this->mName, $subpage );
700 }
701
702 /**
703 * Sets the context this SpecialPage is executed in
704 *
705 * @param $context IContextSource
706 * @since 1.18
707 */
708 public function setContext( $context ) {
709 $this->mContext = $context;
710 }
711
712 /**
713 * Gets the context this SpecialPage is executed in
714 *
715 * @return IContextSource|RequestContext
716 * @since 1.18
717 */
718 public function getContext() {
719 if ( $this->mContext instanceof IContextSource ) {
720 return $this->mContext;
721 } else {
722 wfDebug( __METHOD__ . " called and \$mContext is null. Return RequestContext::getMain(); for sanity\n" );
723 return RequestContext::getMain();
724 }
725 }
726
727 /**
728 * Get the WebRequest being used for this instance
729 *
730 * @return WebRequest
731 * @since 1.18
732 */
733 public function getRequest() {
734 return $this->getContext()->getRequest();
735 }
736
737 /**
738 * Get the OutputPage being used for this instance
739 *
740 * @return OutputPage
741 * @since 1.18
742 */
743 public function getOutput() {
744 return $this->getContext()->getOutput();
745 }
746
747 /**
748 * Shortcut to get the User executing this instance
749 *
750 * @return User
751 * @since 1.18
752 */
753 public function getUser() {
754 return $this->getContext()->getUser();
755 }
756
757 /**
758 * Shortcut to get the skin being used for this instance
759 *
760 * @return Skin
761 * @since 1.18
762 */
763 public function getSkin() {
764 return $this->getContext()->getSkin();
765 }
766
767 /**
768 * Shortcut to get user's language
769 *
770 * @deprecated 1.19 Use getLanguage instead
771 * @return Language
772 * @since 1.18
773 */
774 public function getLang() {
775 wfDeprecated( __METHOD__, '1.19' );
776 return $this->getLanguage();
777 }
778
779 /**
780 * Shortcut to get user's language
781 *
782 * @return Language
783 * @since 1.19
784 */
785 public function getLanguage() {
786 return $this->getContext()->getLanguage();
787 }
788
789 /**
790 * Return the full title, including $par
791 *
792 * @return Title
793 * @since 1.18
794 */
795 public function getFullTitle() {
796 return $this->getContext()->getTitle();
797 }
798
799 /**
800 * Wrapper around wfMessage that sets the current context.
801 *
802 * @return Message
803 * @see wfMessage
804 */
805 public function msg( /* $args */ ) {
806 // Note: can't use func_get_args() directly as second or later item in
807 // a parameter list until PHP 5.3 or you get a fatal error.
808 // Works fine as the first parameter, which appears elsewhere in the
809 // code base. Sighhhh.
810 $args = func_get_args();
811 $message = call_user_func_array( array( $this->getContext(), 'msg' ), $args );
812 // RequestContext passes context to wfMessage, and the language is set from
813 // the context, but setting the language for Message class removes the
814 // interface message status, which breaks for example usernameless gender
815 // invokations. Restore the flag when not including special page in content.
816 if ( $this->including() ) {
817 $message->setInterfaceMessageFlag( false );
818 }
819 return $message;
820 }
821
822 /**
823 * Adds RSS/atom links
824 *
825 * @param $params array
826 */
827 protected function addFeedLinks( $params ) {
828 global $wgFeedClasses;
829
830 $feedTemplate = wfScript( 'api' ) . '?';
831
832 foreach ( $wgFeedClasses as $format => $class ) {
833 $theseParams = $params + array( 'feedformat' => $format );
834 $url = $feedTemplate . wfArrayToCGI( $theseParams );
835 $this->getOutput()->addFeedLink( $format, $url );
836 }
837 }
838 }
839
840 /**
841 * Special page which uses an HTMLForm to handle processing. This is mostly a
842 * clone of FormAction. More special pages should be built this way; maybe this could be
843 * a new structure for SpecialPages
844 */
845 abstract class FormSpecialPage extends SpecialPage {
846
847 /**
848 * Get an HTMLForm descriptor array
849 * @return Array
850 */
851 protected abstract function getFormFields();
852
853 /**
854 * Add pre- or post-text to the form
855 * @return String HTML which will be sent to $form->addPreText()
856 */
857 protected function preText() { return ''; }
858 protected function postText() { return ''; }
859
860 /**
861 * Play with the HTMLForm if you need to more substantially
862 * @param $form HTMLForm
863 */
864 protected function alterForm( HTMLForm $form ) {}
865
866 /**
867 * Get the HTMLForm to control behaviour
868 * @return HTMLForm|null
869 */
870 protected function getForm() {
871 $this->fields = $this->getFormFields();
872
873 $form = new HTMLForm( $this->fields, $this->getContext() );
874 $form->setSubmitCallback( array( $this, 'onSubmit' ) );
875 $form->setWrapperLegend( $this->msg( strtolower( $this->getName() ) . '-legend' ) );
876 $form->addHeaderText(
877 $this->msg( strtolower( $this->getName() ) . '-text' )->parseAsBlock() );
878
879 // Retain query parameters (uselang etc)
880 $params = array_diff_key(
881 $this->getRequest()->getQueryValues(), array( 'title' => null ) );
882 $form->addHiddenField( 'redirectparams', wfArrayToCGI( $params ) );
883
884 $form->addPreText( $this->preText() );
885 $form->addPostText( $this->postText() );
886 $this->alterForm( $form );
887
888 // Give hooks a chance to alter the form, adding extra fields or text etc
889 wfRunHooks( "Special{$this->getName()}BeforeFormDisplay", array( &$form ) );
890
891 return $form;
892 }
893
894 /**
895 * Process the form on POST submission.
896 * @param $data Array
897 * @return Bool|Array true for success, false for didn't-try, array of errors on failure
898 */
899 public abstract function onSubmit( array $data );
900
901 /**
902 * Do something exciting on successful processing of the form, most likely to show a
903 * confirmation message
904 */
905 public abstract function onSuccess();
906
907 /**
908 * Basic SpecialPage workflow: get a form, send it to the user; get some data back,
909 *
910 * @param $par String Subpage string if one was specified
911 */
912 public function execute( $par ) {
913 $this->setParameter( $par );
914 $this->setHeaders();
915
916 // This will throw exceptions if there's a problem
917 $this->checkExecutePermissions( $this->getUser() );
918
919 $form = $this->getForm();
920 if ( $form->show() ) {
921 $this->onSuccess();
922 }
923 }
924
925 /**
926 * Maybe do something interesting with the subpage parameter
927 * @param $par String
928 */
929 protected function setParameter( $par ) {}
930
931 /**
932 * Called from execute() to check if the given user can perform this action.
933 * Failures here must throw subclasses of ErrorPageError.
934 * @param $user User
935 * @return Bool true
936 * @throws ErrorPageError
937 */
938 protected function checkExecutePermissions( User $user ) {
939 $this->checkPermissions();
940
941 if ( $this->requiresUnblock() && $user->isBlocked() ) {
942 $block = $user->getBlock();
943 throw new UserBlockedError( $block );
944 }
945
946 if ( $this->requiresWrite() ) {
947 $this->checkReadOnly();
948 }
949
950 return true;
951 }
952
953 /**
954 * Whether this action requires the wiki not to be locked
955 * @return Bool
956 */
957 public function requiresWrite() {
958 return true;
959 }
960
961 /**
962 * Whether this action cannot be executed by a blocked user
963 * @return Bool
964 */
965 public function requiresUnblock() {
966 return true;
967 }
968 }
969
970 /**
971 * Shortcut to construct a special page which is unlisted by default
972 * @ingroup SpecialPage
973 */
974 class UnlistedSpecialPage extends SpecialPage {
975 function __construct( $name, $restriction = '', $function = false, $file = 'default' ) {
976 parent::__construct( $name, $restriction, false, $function, $file );
977 }
978
979 public function isListed() {
980 return false;
981 }
982 }
983
984 /**
985 * Shortcut to construct an includable special page
986 * @ingroup SpecialPage
987 */
988 class IncludableSpecialPage extends SpecialPage {
989 function __construct(
990 $name, $restriction = '', $listed = true, $function = false, $file = 'default'
991 ) {
992 parent::__construct( $name, $restriction, $listed, $function, $file, true );
993 }
994
995 public function isIncludable() {
996 return true;
997 }
998 }
999
1000 /**
1001 * Shortcut to construct a special page alias.
1002 * @ingroup SpecialPage
1003 */
1004 abstract class RedirectSpecialPage extends UnlistedSpecialPage {
1005
1006 // Query parameters that can be passed through redirects
1007 protected $mAllowedRedirectParams = array();
1008
1009 // Query parameteres added by redirects
1010 protected $mAddedRedirectParams = array();
1011
1012 public function execute( $par ) {
1013 $redirect = $this->getRedirect( $par );
1014 $query = $this->getRedirectQuery();
1015 // Redirect to a page title with possible query parameters
1016 if ( $redirect instanceof Title ) {
1017 $url = $redirect->getFullUrl( $query );
1018 $this->getOutput()->redirect( $url );
1019 wfProfileOut( __METHOD__ );
1020 return $redirect;
1021 // Redirect to index.php with query parameters
1022 } elseif ( $redirect === true ) {
1023 global $wgScript;
1024 $url = $wgScript . '?' . wfArrayToCGI( $query );
1025 $this->getOutput()->redirect( $url );
1026 wfProfileOut( __METHOD__ );
1027 return $redirect;
1028 } else {
1029 $class = __CLASS__;
1030 throw new MWException( "RedirectSpecialPage $class doesn't redirect!" );
1031 }
1032 }
1033
1034 /**
1035 * If the special page is a redirect, then get the Title object it redirects to.
1036 * False otherwise.
1037 *
1038 * @param $par String Subpage string
1039 * @return Title|bool
1040 */
1041 abstract public function getRedirect( $par );
1042
1043 /**
1044 * Return part of the request string for a special redirect page
1045 * This allows passing, e.g. action=history to Special:Mypage, etc.
1046 *
1047 * @return String
1048 */
1049 public function getRedirectQuery() {
1050 $params = array();
1051
1052 foreach ( $this->mAllowedRedirectParams as $arg ) {
1053 if ( $this->getRequest()->getVal( $arg, null ) !== null ) {
1054 $params[$arg] = $this->getRequest()->getVal( $arg );
1055 }
1056 }
1057
1058 foreach ( $this->mAddedRedirectParams as $arg => $val ) {
1059 $params[$arg] = $val;
1060 }
1061
1062 return count( $params )
1063 ? $params
1064 : false;
1065 }
1066 }
1067
1068 abstract class SpecialRedirectToSpecial extends RedirectSpecialPage {
1069 var $redirName, $redirSubpage;
1070
1071 function __construct(
1072 $name, $redirName, $redirSubpage = false,
1073 $allowedRedirectParams = array(), $addedRedirectParams = array()
1074 ) {
1075 parent::__construct( $name );
1076 $this->redirName = $redirName;
1077 $this->redirSubpage = $redirSubpage;
1078 $this->mAllowedRedirectParams = $allowedRedirectParams;
1079 $this->mAddedRedirectParams = $addedRedirectParams;
1080 }
1081
1082 public function getRedirect( $subpage ) {
1083 if ( $this->redirSubpage === false ) {
1084 return SpecialPage::getTitleFor( $this->redirName, $subpage );
1085 } else {
1086 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
1087 }
1088 }
1089 }
1090
1091 /**
1092 * ListAdmins --> ListUsers/sysop
1093 */
1094 class SpecialListAdmins extends SpecialRedirectToSpecial {
1095 function __construct() {
1096 parent::__construct( 'Listadmins', 'Listusers', 'sysop' );
1097 }
1098 }
1099
1100 /**
1101 * ListBots --> ListUsers/bot
1102 */
1103 class SpecialListBots extends SpecialRedirectToSpecial {
1104 function __construct() {
1105 parent::__construct( 'Listbots', 'Listusers', 'bot' );
1106 }
1107 }
1108
1109 /**
1110 * CreateAccount --> UserLogin/signup
1111 * @todo FIXME: This (and the rest of the login frontend) needs to die a horrible painful death
1112 */
1113 class SpecialCreateAccount extends SpecialRedirectToSpecial {
1114 function __construct() {
1115 parent::__construct( 'CreateAccount', 'Userlogin', 'signup', array( 'uselang' ) );
1116 }
1117 }
1118 /**
1119 * SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
1120 * are used to get user independant links pointing to the user page, talk
1121 * page and list of contributions.
1122 * This can let us cache a single copy of any generated content for all
1123 * users.
1124 */
1125
1126 /**
1127 * Superclass for any RedirectSpecialPage which redirects the user
1128 * to a particular article (as opposed to user contributions, logs, etc.).
1129 *
1130 * For security reasons these special pages are restricted to pass on
1131 * the following subset of GET parameters to the target page while
1132 * removing all others:
1133 *
1134 * - useskin, uselang, printable: to alter the appearance of the resulting page
1135 *
1136 * - redirect: allows viewing one's user page or talk page even if it is a
1137 * redirect.
1138 *
1139 * - rdfrom: allows redirecting to one's user page or talk page from an
1140 * external wiki with the "Redirect from..." notice.
1141 *
1142 * - limit, offset: Useful for linking to history of one's own user page or
1143 * user talk page. For example, this would be a link to "the last edit to your
1144 * user talk page in the year 2010":
1145 * http://en.wikipedia.org/w/index.php?title=Special:MyPage&offset=20110000000000&limit=1&action=history
1146 *
1147 * - feed: would allow linking to the current user's RSS feed for their user
1148 * talk page:
1149 * http://en.wikipedia.org/w/index.php?title=Special:MyTalk&action=history&feed=rss
1150 *
1151 * - preloadtitle: Can be used to provide a default section title for a
1152 * preloaded new comment on one's own talk page.
1153 *
1154 * - summary : Can be used to provide a default edit summary for a preloaded
1155 * edit to one's own user page or talk page.
1156 *
1157 * - preview: Allows showing/hiding preview on first edit regardless of user
1158 * preference, useful for preloaded edits where you know preview wouldn't be
1159 * useful.
1160 *
1161 * - internaledit, externaledit, mode: Allows forcing the use of the
1162 * internal/external editor, e.g. to force the internal editor for
1163 * short/simple preloaded edits.
1164 *
1165 * - redlink: Affects the message the user sees if their talk page/user talk
1166 * page does not currently exist. Avoids confusion for newbies with no user
1167 * pages over why they got a "permission error" following this link:
1168 * http://en.wikipedia.org/w/index.php?title=Special:MyPage&redlink=1
1169 *
1170 * - debug: determines whether the debug parameter is passed to load.php,
1171 * which disables reformatting and allows scripts to be debugged. Useful
1172 * when debugging scripts that manipulate one's own user page or talk page.
1173 *
1174 * @par Hook extension:
1175 * Extensions can add to the redirect parameters list by using the hook
1176 * RedirectSpecialArticleRedirectParams
1177 *
1178 * This hook allows extensions which add GET parameters like FlaggedRevs to
1179 * retain those parameters when redirecting using special pages.
1180 *
1181 * @par Hook extension example:
1182 * @code
1183 * $wgHooks['RedirectSpecialArticleRedirectParams'][] =
1184 * 'MyExtensionHooks::onRedirectSpecialArticleRedirectParams';
1185 * public static function onRedirectSpecialArticleRedirectParams( &$redirectParams ) {
1186 * $redirectParams[] = 'stable';
1187 * return true;
1188 * }
1189 * @endcode
1190 * @ingroup SpecialPage
1191 */
1192 abstract class RedirectSpecialArticle extends RedirectSpecialPage {
1193 function __construct( $name ) {
1194 parent::__construct( $name );
1195 $redirectParams = array(
1196 'action',
1197 'redirect', 'rdfrom',
1198 # Options for preloaded edits
1199 'preload', 'editintro', 'preloadtitle', 'summary',
1200 # Options for overriding user settings
1201 'preview', 'internaledit', 'externaledit', 'mode',
1202 # Options for history/diffs
1203 'section', 'oldid', 'diff', 'dir',
1204 'limit', 'offset', 'feed',
1205 # Misc options
1206 'redlink', 'debug',
1207 # Options for action=raw; missing ctype can break JS or CSS in some browsers
1208 'ctype', 'maxage', 'smaxage',
1209 );
1210
1211 wfRunHooks( "RedirectSpecialArticleRedirectParams", array(&$redirectParams) );
1212 $this->mAllowedRedirectParams = $redirectParams;
1213 }
1214 }
1215
1216 /**
1217 * Shortcut to construct a special page pointing to current user user's page.
1218 * @ingroup SpecialPage
1219 */
1220 class SpecialMypage extends RedirectSpecialArticle {
1221 function __construct() {
1222 parent::__construct( 'Mypage' );
1223 }
1224
1225 function getRedirect( $subpage ) {
1226 if ( strval( $subpage ) !== '' ) {
1227 return Title::makeTitle( NS_USER, $this->getUser()->getName() . '/' . $subpage );
1228 } else {
1229 return Title::makeTitle( NS_USER, $this->getUser()->getName() );
1230 }
1231 }
1232 }
1233
1234 /**
1235 * Shortcut to construct a special page pointing to current user talk page.
1236 * @ingroup SpecialPage
1237 */
1238 class SpecialMytalk extends RedirectSpecialArticle {
1239 function __construct() {
1240 parent::__construct( 'Mytalk' );
1241 }
1242
1243 function getRedirect( $subpage ) {
1244 if ( strval( $subpage ) !== '' ) {
1245 return Title::makeTitle( NS_USER_TALK, $this->getUser()->getName() . '/' . $subpage );
1246 } else {
1247 return Title::makeTitle( NS_USER_TALK, $this->getUser()->getName() );
1248 }
1249 }
1250 }
1251
1252 /**
1253 * Shortcut to construct a special page pointing to current user contributions.
1254 * @ingroup SpecialPage
1255 */
1256 class SpecialMycontributions extends RedirectSpecialPage {
1257 function __construct() {
1258 parent::__construct( 'Mycontributions' );
1259 $this->mAllowedRedirectParams = array( 'limit', 'namespace', 'tagfilter',
1260 'offset', 'dir', 'year', 'month', 'feed' );
1261 }
1262
1263 function getRedirect( $subpage ) {
1264 return SpecialPage::getTitleFor( 'Contributions', $this->getUser()->getName() );
1265 }
1266 }
1267
1268 /**
1269 * Redirect to Special:Listfiles?user=$wgUser
1270 */
1271 class SpecialMyuploads extends RedirectSpecialPage {
1272 function __construct() {
1273 parent::__construct( 'Myuploads' );
1274 $this->mAllowedRedirectParams = array( 'limit' );
1275 }
1276
1277 function getRedirect( $subpage ) {
1278 return SpecialPage::getTitleFor( 'Listfiles', $this->getUser()->getName() );
1279 }
1280 }
1281
1282 /**
1283 * Redirect from Special:PermanentLink/### to index.php?oldid=###
1284 */
1285 class SpecialPermanentLink extends RedirectSpecialPage {
1286 function __construct() {
1287 parent::__construct( 'PermanentLink' );
1288 $this->mAllowedRedirectParams = array();
1289 }
1290
1291 function getRedirect( $subpage ) {
1292 $subpage = intval( $subpage );
1293 if ( $subpage === 0 ) {
1294 # throw an error page when no subpage was given
1295 throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
1296 }
1297 $this->mAddedRedirectParams['oldid'] = $subpage;
1298 return true;
1299 }
1300 }