Add parser tests for bug 52468 and bug 52363.
[mediawiki.git] / includes / SpecialPage.php
blobd87f9107ac0d1b040fca449f654f88fa2ddc4e29
1 <?php
2 /**
3 * Parent class for all special pages.
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Parent special page class, also static functions for handling the special
26 * page list.
27 * @ingroup SpecialPage
29 class SpecialPage {
30 // The canonical name of this special page
31 // Also used for the default <h1> heading, @see getDescription()
32 protected $mName;
34 // The local name of this special page
35 private $mLocalName;
37 // Minimum user level required to access this page, or "" for anyone.
38 // Also used to categorise the pages in Special:Specialpages
39 private $mRestriction;
41 // Listed in Special:Specialpages?
42 private $mListed;
44 // Function name called by the default execute()
45 private $mFunction;
47 // File which needs to be included before the function above can be called
48 private $mFile;
50 // Whether or not this special page is being included from an article
51 protected $mIncluding;
53 // Whether the special page can be included in an article
54 protected $mIncludable;
56 /**
57 * Current request context
58 * @var IContextSource
60 protected $mContext;
62 /**
63 * Initialise the special page list
64 * This must be called before accessing SpecialPage::$mList
65 * @deprecated since 1.18
67 static function initList() {
68 wfDeprecated( __METHOD__, '1.18' );
69 // Noop
72 /**
73 * @deprecated since 1.18
75 static function initAliasList() {
76 wfDeprecated( __METHOD__, '1.18' );
77 // Noop
80 /**
81 * Given a special page alias, return the special page name.
82 * Returns false if there is no such alias.
84 * @param $alias String
85 * @return String or false
86 * @deprecated since 1.18 call SpecialPageFactory method directly
88 static function resolveAlias( $alias ) {
89 wfDeprecated( __METHOD__, '1.18' );
90 list( $name, /*...*/ ) = SpecialPageFactory::resolveAlias( $alias );
91 return $name;
94 /**
95 * Given a special page name with a possible subpage, return an array
96 * where the first element is the special page name and the second is the
97 * subpage.
99 * @param $alias String
100 * @return Array
101 * @deprecated since 1.18 call SpecialPageFactory method directly
103 static function resolveAliasWithSubpage( $alias ) {
104 return SpecialPageFactory::resolveAlias( $alias );
108 * Add a page to a certain display group for Special:SpecialPages
110 * @param $page Mixed: SpecialPage or string
111 * @param $group String
112 * @deprecated since 1.18 call SpecialPageFactory method directly
114 static function setGroup( $page, $group ) {
115 wfDeprecated( __METHOD__, '1.18' );
116 SpecialPageFactory::setGroup( $page, $group );
120 * Get the group that the special page belongs in on Special:SpecialPage
122 * @param $page SpecialPage
123 * @return string
124 * @deprecated since 1.18 call SpecialPageFactory method directly
126 static function getGroup( &$page ) {
127 wfDeprecated( __METHOD__, '1.18' );
128 return SpecialPageFactory::getGroup( $page );
132 * Remove a special page from the list
133 * Formerly used to disable expensive or dangerous special pages. The
134 * preferred method is now to add a SpecialPage_initList hook.
135 * @deprecated since 1.18
137 * @param string $name the page to remove
139 static function removePage( $name ) {
140 wfDeprecated( __METHOD__, '1.18' );
141 unset( SpecialPageFactory::getList()->$name );
145 * Check if a given name exist as a special page or as a special page alias
147 * @param string $name name of a special page
148 * @return Boolean: true if a special page exists with this name
149 * @deprecated since 1.18 call SpecialPageFactory method directly
151 static function exists( $name ) {
152 wfDeprecated( __METHOD__, '1.18' );
153 return SpecialPageFactory::exists( $name );
157 * Find the object with a given name and return it (or NULL)
159 * @param $name String
160 * @return SpecialPage object or null if the page doesn't exist
161 * @deprecated since 1.18 call SpecialPageFactory method directly
163 static function getPage( $name ) {
164 wfDeprecated( __METHOD__, '1.18' );
165 return SpecialPageFactory::getPage( $name );
169 * Get a special page with a given localised name, or NULL if there
170 * is no such special page.
172 * @param $alias String
173 * @return SpecialPage object or null if the page doesn't exist
174 * @deprecated since 1.18 call SpecialPageFactory method directly
176 static function getPageByAlias( $alias ) {
177 wfDeprecated( __METHOD__, '1.18' );
178 return SpecialPageFactory::getPage( $alias );
182 * Return categorised listable special pages which are available
183 * for the current user, and everyone.
185 * @param $user User object to check permissions, $wgUser will be used
186 * if not provided
187 * @return array Associative array mapping page's name to its SpecialPage object
188 * @deprecated since 1.18 call SpecialPageFactory method directly
190 static function getUsablePages( User $user = null ) {
191 wfDeprecated( __METHOD__, '1.18' );
192 return SpecialPageFactory::getUsablePages( $user );
196 * Return categorised listable special pages for all users
198 * @return array Associative array mapping page's name to its SpecialPage object
199 * @deprecated since 1.18 call SpecialPageFactory method directly
201 static function getRegularPages() {
202 wfDeprecated( __METHOD__, '1.18' );
203 return SpecialPageFactory::getRegularPages();
207 * Return categorised listable special pages which are available
208 * for the current user, but not for everyone
210 * @return array Associative array mapping page's name to its SpecialPage object
211 * @deprecated since 1.18 call SpecialPageFactory method directly
213 static function getRestrictedPages() {
214 wfDeprecated( __METHOD__, '1.18' );
215 return SpecialPageFactory::getRestrictedPages();
219 * Execute a special page path.
220 * The path may contain parameters, e.g. Special:Name/Params
221 * Extracts the special page name and call the execute method, passing the parameters
223 * Returns a title object if the page is redirected, false if there was no such special
224 * page, and true if it was successful.
226 * @param $title Title object
227 * @param $context IContextSource
228 * @param $including Bool output is being captured for use in {{special:whatever}}
229 * @return Bool
230 * @deprecated since 1.18 call SpecialPageFactory method directly
232 public static function executePath( &$title, IContextSource &$context, $including = false ) {
233 wfDeprecated( __METHOD__, '1.18' );
234 return SpecialPageFactory::executePath( $title, $context, $including );
238 * Get the local name for a specified canonical name
240 * @param $name String
241 * @param $subpage Mixed: boolean false, or string
243 * @return String
244 * @deprecated since 1.18 call SpecialPageFactory method directly
246 static function getLocalNameFor( $name, $subpage = false ) {
247 wfDeprecated( __METHOD__, '1.18' );
248 return SpecialPageFactory::getLocalNameFor( $name, $subpage );
252 * Get a localised Title object for a specified special page name
254 * @param $name String
255 * @param string|Bool $subpage subpage string, or false to not use a subpage
256 * @param string $fragment the link fragment (after the "#")
257 * @throws MWException
258 * @return Title object
260 public static function getTitleFor( $name, $subpage = false, $fragment = '' ) {
261 $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
262 if ( $name ) {
263 return Title::makeTitle( NS_SPECIAL, $name, $fragment );
264 } else {
265 throw new MWException( "Invalid special page name \"$name\"" );
270 * Get a localised Title object for a page name with a possibly unvalidated subpage
272 * @param $name String
273 * @param string|Bool $subpage subpage string, or false to not use a subpage
274 * @return Title object or null if the page doesn't exist
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;
286 * Get a title for a given alias
288 * @param $alias String
289 * @return Title or null if there is no such alias
290 * @deprecated since 1.18 call SpecialPageFactory method directly
292 static function getTitleForAlias( $alias ) {
293 wfDeprecated( __METHOD__, '1.18' );
294 return SpecialPageFactory::getTitleForAlias( $alias );
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.
304 * If you override execute(), you can recover the default behavior with userCanExecute()
305 * and displayRestrictionError()
307 * @param string $name Name of the special page, as seen in links and URLs
308 * @param string $restriction User right required, e.g. "block" or "delete"
309 * @param bool $listed Whether the page is listed in Special:Specialpages
310 * @param Callback|Bool $function Function called by execute(). By default
311 * it is constructed from $name
312 * @param string $file File which is included by execute(). It is also
313 * constructed from $name by default
314 * @param bool $includable Whether the page can be included in normal pages
316 public function __construct(
317 $name = '', $restriction = '', $listed = true,
318 $function = false, $file = 'default', $includable = false
320 $this->init( $name, $restriction, $listed, $function, $file, $includable );
324 * Do the real work for the constructor, mainly so __call() can intercept
325 * calls to SpecialPage()
326 * @param string $name Name of the special page, as seen in links and URLs
327 * @param string $restriction User right required, e.g. "block" or "delete"
328 * @param bool $listed Whether the page is listed in Special:Specialpages
329 * @param Callback|Bool $function Function called by execute(). By default
330 * it is constructed from $name
331 * @param string $file File which is included by execute(). It is also
332 * constructed from $name by default
333 * @param bool $includable Whether the page can be included in normal pages
335 private function init( $name, $restriction, $listed, $function, $file, $includable ) {
336 $this->mName = $name;
337 $this->mRestriction = $restriction;
338 $this->mListed = $listed;
339 $this->mIncludable = $includable;
340 if ( !$function ) {
341 $this->mFunction = 'wfSpecial' . $name;
342 } else {
343 $this->mFunction = $function;
345 if ( $file === 'default' ) {
346 $this->mFile = __DIR__ . "/specials/Special$name.php";
347 } else {
348 $this->mFile = $file;
353 * Use PHP's magic __call handler to get calls to the old PHP4 constructor
354 * because PHP E_STRICT yells at you for having __construct() and SpecialPage()
356 * @param string $fName Name of called method
357 * @param array $a Arguments to the method
358 * @throws MWException
359 * @deprecated since 1.17, call parent::__construct()
361 public function __call( $fName, $a ) {
362 // Deprecated messages now, remove in 1.19 or 1.20?
363 wfDeprecated( __METHOD__, '1.17' );
365 // Sometimes $fName is SpecialPage, sometimes it's specialpage. <3 PHP
366 if ( strtolower( $fName ) == 'specialpage' ) {
367 $name = isset( $a[0] ) ? $a[0] : '';
368 $restriction = isset( $a[1] ) ? $a[1] : '';
369 $listed = isset( $a[2] ) ? $a[2] : true;
370 $function = isset( $a[3] ) ? $a[3] : false;
371 $file = isset( $a[4] ) ? $a[4] : 'default';
372 $includable = isset( $a[5] ) ? $a[5] : false;
373 $this->init( $name, $restriction, $listed, $function, $file, $includable );
374 } else {
375 $className = get_class( $this );
376 throw new MWException( "Call to undefined method $className::$fName" );
381 * Get the name of this Special Page.
382 * @return String
384 function getName() {
385 return $this->mName;
389 * Get the permission that a user must have to execute this page
390 * @return String
392 function getRestriction() {
393 return $this->mRestriction;
397 * Get the file which will be included by SpecialPage::execute() if your extension is
398 * still stuck in the past and hasn't overridden the execute() method. No modern code
399 * should want or need to know this.
400 * @return String
401 * @deprecated since 1.18
403 function getFile() {
404 wfDeprecated( __METHOD__, '1.18' );
405 return $this->mFile;
408 // @todo FIXME: Decide which syntax to use for this, and stick to it
410 * Whether this special page is listed in Special:SpecialPages
411 * @since r3583 (v1.3)
412 * @return Bool
414 function isListed() {
415 return $this->mListed;
418 * Set whether this page is listed in Special:Specialpages, at run-time
419 * @since r3583 (v1.3)
420 * @param $listed Bool
421 * @return Bool
423 function setListed( $listed ) {
424 return wfSetVar( $this->mListed, $listed );
427 * Get or set whether this special page is listed in Special:SpecialPages
428 * @since r11308 (v1.6)
429 * @param $x Bool
430 * @return Bool
432 function listed( $x = null ) {
433 return wfSetVar( $this->mListed, $x );
437 * Whether it's allowed to transclude the special page via {{Special:Foo/params}}
438 * @return Bool
440 public function isIncludable() {
441 return $this->mIncludable;
445 * These mutators are very evil, as the relevant variables should not mutate. So
446 * don't use them.
447 * @param $x Mixed
448 * @return Mixed
449 * @deprecated since 1.18
451 function name( $x = null ) {
452 wfDeprecated( __METHOD__, '1.18' );
453 return wfSetVar( $this->mName, $x );
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
463 function restriction( $x = null ) {
464 wfDeprecated( __METHOD__, '1.18' );
465 return wfSetVar( $this->mRestriction, $x );
469 * These mutators are very evil, as the relevant variables should not mutate. So
470 * don't use them.
471 * @param $x Mixed
472 * @return Mixed
473 * @deprecated since 1.18
475 function func( $x = null ) {
476 wfDeprecated( __METHOD__, '1.18' );
477 return wfSetVar( $this->mFunction, $x );
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
487 function file( $x = null ) {
488 wfDeprecated( __METHOD__, '1.18' );
489 return wfSetVar( $this->mFile, $x );
493 * These mutators are very evil, as the relevant variables should not mutate. So
494 * don't use them.
495 * @param $x Mixed
496 * @return Mixed
497 * @deprecated since 1.18
499 function includable( $x = null ) {
500 wfDeprecated( __METHOD__, '1.18' );
501 return wfSetVar( $this->mIncludable, $x );
505 * Whether the special page is being evaluated via transclusion
506 * @param $x Bool
507 * @return Bool
509 function including( $x = null ) {
510 return wfSetVar( $this->mIncluding, $x );
514 * Get the localised name of the special page
516 function getLocalName() {
517 if ( !isset( $this->mLocalName ) ) {
518 $this->mLocalName = SpecialPageFactory::getLocalNameFor( $this->mName );
520 return $this->mLocalName;
524 * Is this page expensive (for some definition of expensive)?
525 * Expensive pages are disabled or cached in miser mode. Originally used
526 * (and still overridden) by QueryPage and subclasses, moved here so that
527 * Special:SpecialPages can safely call it for all special pages.
529 * @return Boolean
531 public function isExpensive() {
532 return false;
536 * Is this page cached?
537 * Expensive pages are cached or disabled in miser mode.
538 * Used by QueryPage and subclasses, moved here so that
539 * Special:SpecialPages can safely call it for all special pages.
541 * @return Boolean
542 * @since 1.21
544 public function isCached() {
545 return false;
549 * Can be overridden by subclasses with more complicated permissions
550 * schemes.
552 * @return Boolean: should the page be displayed with the restricted-access
553 * pages?
555 public function isRestricted() {
556 // DWIM: If anons can do something, then it is not restricted
557 return $this->mRestriction != '' && !User::groupHasPermission( '*', $this->mRestriction );
561 * Checks if the given user (identified by an object) can execute this
562 * special page (as defined by $mRestriction). Can be overridden by sub-
563 * classes with more complicated permissions schemes.
565 * @param $user User: the user to check
566 * @return Boolean: does the user have permission to view the page?
568 public function userCanExecute( User $user ) {
569 return $user->isAllowed( $this->mRestriction );
573 * Output an error message telling the user what access level they have to have
575 function displayRestrictionError() {
576 throw new PermissionsError( $this->mRestriction );
580 * Checks if userCanExecute, and if not throws a PermissionsError
582 * @since 1.19
584 public function checkPermissions() {
585 if ( !$this->userCanExecute( $this->getUser() ) ) {
586 $this->displayRestrictionError();
591 * If the wiki is currently in readonly mode, throws a ReadOnlyError
593 * @since 1.19
594 * @throws ReadOnlyError
596 public function checkReadOnly() {
597 if ( wfReadOnly() ) {
598 throw new ReadOnlyError;
603 * Sets headers - this should be called from the execute() method of all derived classes!
605 function setHeaders() {
606 $out = $this->getOutput();
607 $out->setArticleRelated( false );
608 $out->setRobotPolicy( "noindex,nofollow" );
609 $out->setPageTitle( $this->getDescription() );
613 * Entry point.
615 * @since 1.20
617 * @param $subPage string|null
619 final public function run( $subPage ) {
621 * Gets called before @see SpecialPage::execute.
623 * @since 1.20
625 * @param $special SpecialPage
626 * @param $subPage string|null
628 wfRunHooks( 'SpecialPageBeforeExecute', array( $this, $subPage ) );
630 $this->beforeExecute( $subPage );
631 $this->execute( $subPage );
632 $this->afterExecute( $subPage );
635 * Gets called after @see SpecialPage::execute.
637 * @since 1.20
639 * @param $special SpecialPage
640 * @param $subPage string|null
642 wfRunHooks( 'SpecialPageAfterExecute', array( $this, $subPage ) );
646 * Gets called before @see SpecialPage::execute.
648 * @since 1.20
650 * @param $subPage string|null
652 protected function beforeExecute( $subPage ) {
653 // No-op
657 * Gets called after @see SpecialPage::execute.
659 * @since 1.20
661 * @param $subPage string|null
663 protected function afterExecute( $subPage ) {
664 // No-op
668 * Default execute method
669 * Checks user permissions, calls the function given in mFunction
671 * This must be overridden by subclasses; it will be made abstract in a future version
673 * @param $subPage string|null
675 public function execute( $subPage ) {
676 $this->setHeaders();
677 $this->checkPermissions();
679 $func = $this->mFunction;
680 // only load file if the function does not exist
681 if ( !is_callable( $func ) && $this->mFile ) {
682 require_once $this->mFile;
684 $this->outputHeader();
685 call_user_func( $func, $subPage, $this );
689 * Outputs a summary message on top of special pages
690 * Per default the message key is the canonical name of the special page
691 * May be overridden, i.e. by extensions to stick with the naming conventions
692 * for message keys: 'extensionname-xxx'
694 * @param string $summaryMessageKey message key of the summary
696 function outputHeader( $summaryMessageKey = '' ) {
697 global $wgContLang;
699 if ( $summaryMessageKey == '' ) {
700 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
701 } else {
702 $msg = $summaryMessageKey;
704 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
705 $this->getOutput()->wrapWikiMsg(
706 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
712 * Returns the name that goes in the \<h1\> in the special page itself, and
713 * also the name that will be listed in Special:Specialpages
715 * Derived classes can override this, but usually it is easier to keep the
716 * default behavior. Messages can be added at run-time, see
717 * MessageCache.php.
719 * @return String
721 function getDescription() {
722 return $this->msg( strtolower( $this->mName ) )->text();
726 * Get a self-referential title object
728 * @param $subpage String|Bool
729 * @return Title object
731 function getTitle( $subpage = false ) {
732 return self::getTitleFor( $this->mName, $subpage );
736 * Sets the context this SpecialPage is executed in
738 * @param $context IContextSource
739 * @since 1.18
741 public function setContext( $context ) {
742 $this->mContext = $context;
746 * Gets the context this SpecialPage is executed in
748 * @return IContextSource|RequestContext
749 * @since 1.18
751 public function getContext() {
752 if ( $this->mContext instanceof IContextSource ) {
753 return $this->mContext;
754 } else {
755 wfDebug( __METHOD__ . " called and \$mContext is null. " .
756 "Return RequestContext::getMain(); for sanity\n" );
757 return RequestContext::getMain();
762 * Get the WebRequest being used for this instance
764 * @return WebRequest
765 * @since 1.18
767 public function getRequest() {
768 return $this->getContext()->getRequest();
772 * Get the OutputPage being used for this instance
774 * @return OutputPage
775 * @since 1.18
777 public function getOutput() {
778 return $this->getContext()->getOutput();
782 * Shortcut to get the User executing this instance
784 * @return User
785 * @since 1.18
787 public function getUser() {
788 return $this->getContext()->getUser();
792 * Shortcut to get the skin being used for this instance
794 * @return Skin
795 * @since 1.18
797 public function getSkin() {
798 return $this->getContext()->getSkin();
802 * Shortcut to get user's language
804 * @deprecated since 1.19 Use getLanguage instead
805 * @return Language
806 * @since 1.18
808 public function getLang() {
809 wfDeprecated( __METHOD__, '1.19' );
810 return $this->getLanguage();
814 * Shortcut to get user's language
816 * @return Language
817 * @since 1.19
819 public function getLanguage() {
820 return $this->getContext()->getLanguage();
824 * Return the full title, including $par
826 * @return Title
827 * @since 1.18
829 public function getFullTitle() {
830 return $this->getContext()->getTitle();
834 * Wrapper around wfMessage that sets the current context.
836 * @return Message
837 * @see wfMessage
839 public function msg( /* $args */ ) {
840 // Note: can't use func_get_args() directly as second or later item in
841 // a parameter list until PHP 5.3 or you get a fatal error.
842 // Works fine as the first parameter, which appears elsewhere in the
843 // code base. Sighhhh.
844 $args = func_get_args();
845 $message = call_user_func_array( array( $this->getContext(), 'msg' ), $args );
846 // RequestContext passes context to wfMessage, and the language is set from
847 // the context, but setting the language for Message class removes the
848 // interface message status, which breaks for example usernameless gender
849 // invocations. Restore the flag when not including special page in content.
850 if ( $this->including() ) {
851 $message->setInterfaceMessageFlag( false );
853 return $message;
857 * Adds RSS/atom links
859 * @param $params array
861 protected function addFeedLinks( $params ) {
862 global $wgFeedClasses;
864 $feedTemplate = wfScript( 'api' );
866 foreach ( $wgFeedClasses as $format => $class ) {
867 $theseParams = $params + array( 'feedformat' => $format );
868 $url = wfAppendQuery( $feedTemplate, $theseParams );
869 $this->getOutput()->addFeedLink( $format, $url );
874 * Get the group that the special page belongs in on Special:SpecialPage
875 * Use this method, instead of getGroupName to allow customization
876 * of the group name from the wiki side
878 * @return string Group of this special page
879 * @since 1.21
881 public function getFinalGroupName() {
882 global $wgSpecialPageGroups;
883 $name = $this->getName();
884 $group = '-';
886 // Allow overbidding the group from the wiki side
887 $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
888 if ( !$msg->isBlank() ) {
889 $group = $msg->text();
890 } else {
891 // Than use the group from this object
892 $group = $this->getGroupName();
894 // Group '-' is used as default to have the chance to determine,
895 // if the special pages overrides this method,
896 // if not overridden, $wgSpecialPageGroups is checked for b/c
897 if ( $group === '-' && isset( $wgSpecialPageGroups[$name] ) ) {
898 $group = $wgSpecialPageGroups[$name];
902 // never give '-' back, change to 'other'
903 if ( $group === '-' ) {
904 $group = 'other';
907 return $group;
911 * Under which header this special page is listed in Special:SpecialPages
912 * See messages 'specialpages-group-*' for valid names
913 * This method defaults to group 'other'
915 * @return string
916 * @since 1.21
918 protected function getGroupName() {
919 // '-' used here to determine, if this group is overridden or has a hardcoded 'other'
920 // Needed for b/c in getFinalGroupName
921 return '-';
926 * Special page which uses an HTMLForm to handle processing. This is mostly a
927 * clone of FormAction. More special pages should be built this way; maybe this could be
928 * a new structure for SpecialPages
930 abstract class FormSpecialPage extends SpecialPage {
932 * The sub-page of the special page.
933 * @var string
935 protected $par = null;
938 * Get an HTMLForm descriptor array
939 * @return Array
941 abstract protected function getFormFields();
944 * Add pre-text to the form
945 * @return String HTML which will be sent to $form->addPreText()
947 protected function preText() {
948 return '';
952 * Add post-text to the form
953 * @return String HTML which will be sent to $form->addPostText()
955 protected function postText() {
956 return '';
960 * Play with the HTMLForm if you need to more substantially
961 * @param $form HTMLForm
963 protected function alterForm( HTMLForm $form ) {
967 * Get message prefix for HTMLForm
969 * @since 1.21
970 * @return string
972 protected function getMessagePrefix() {
973 return strtolower( $this->getName() );
977 * Get the HTMLForm to control behavior
978 * @return HTMLForm|null
980 protected function getForm() {
981 $this->fields = $this->getFormFields();
983 $form = new HTMLForm( $this->fields, $this->getContext(), $this->getMessagePrefix() );
984 $form->setSubmitCallback( array( $this, 'onSubmit' ) );
985 $form->setWrapperLegendMsg( $this->getMessagePrefix() . '-legend' );
987 $headerMsg = $this->msg( $this->getMessagePrefix() . '-text' );
988 if ( !$headerMsg->isDisabled() ) {
989 $form->addHeaderText( $headerMsg->parseAsBlock() );
992 // Retain query parameters (uselang etc)
993 $params = array_diff_key(
994 $this->getRequest()->getQueryValues(), array( 'title' => null ) );
995 $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
997 $form->addPreText( $this->preText() );
998 $form->addPostText( $this->postText() );
999 $this->alterForm( $form );
1001 // Give hooks a chance to alter the form, adding extra fields or text etc
1002 wfRunHooks( "Special{$this->getName()}BeforeFormDisplay", array( &$form ) );
1004 return $form;
1008 * Process the form on POST submission.
1009 * @param $data Array
1010 * @return Bool|Array true for success, false for didn't-try, array of errors on failure
1012 abstract public function onSubmit( array $data );
1015 * Do something exciting on successful processing of the form, most likely to show a
1016 * confirmation message
1017 * @since 1.22 Default is to do nothing
1019 public function onSuccess() {
1023 * Basic SpecialPage workflow: get a form, send it to the user; get some data back,
1025 * @param string $par Subpage string if one was specified
1027 public function execute( $par ) {
1028 $this->setParameter( $par );
1029 $this->setHeaders();
1031 // This will throw exceptions if there's a problem
1032 $this->checkExecutePermissions( $this->getUser() );
1034 $form = $this->getForm();
1035 if ( $form->show() ) {
1036 $this->onSuccess();
1041 * Maybe do something interesting with the subpage parameter
1042 * @param string $par
1044 protected function setParameter( $par ) {
1045 $this->par = $par;
1049 * Called from execute() to check if the given user can perform this action.
1050 * Failures here must throw subclasses of ErrorPageError.
1051 * @param $user User
1052 * @throws UserBlockedError
1053 * @return Bool true
1055 protected function checkExecutePermissions( User $user ) {
1056 $this->checkPermissions();
1058 if ( $this->requiresUnblock() && $user->isBlocked() ) {
1059 $block = $user->getBlock();
1060 throw new UserBlockedError( $block );
1063 if ( $this->requiresWrite() ) {
1064 $this->checkReadOnly();
1067 return true;
1071 * Whether this action requires the wiki not to be locked
1072 * @return Bool
1074 public function requiresWrite() {
1075 return true;
1079 * Whether this action cannot be executed by a blocked user
1080 * @return Bool
1082 public function requiresUnblock() {
1083 return true;
1088 * Shortcut to construct a special page which is unlisted by default
1089 * @ingroup SpecialPage
1091 class UnlistedSpecialPage extends SpecialPage {
1092 function __construct( $name, $restriction = '', $function = false, $file = 'default' ) {
1093 parent::__construct( $name, $restriction, false, $function, $file );
1096 public function isListed() {
1097 return false;
1102 * Shortcut to construct an includable special page
1103 * @ingroup SpecialPage
1105 class IncludableSpecialPage extends SpecialPage {
1106 function __construct(
1107 $name, $restriction = '', $listed = true, $function = false, $file = 'default'
1109 parent::__construct( $name, $restriction, $listed, $function, $file, true );
1112 public function isIncludable() {
1113 return true;
1118 * Shortcut to construct a special page alias.
1119 * @ingroup SpecialPage
1121 abstract class RedirectSpecialPage extends UnlistedSpecialPage {
1123 // Query parameters that can be passed through redirects
1124 protected $mAllowedRedirectParams = array();
1126 // Query parameters added by redirects
1127 protected $mAddedRedirectParams = array();
1129 public function execute( $par ) {
1130 $redirect = $this->getRedirect( $par );
1131 $query = $this->getRedirectQuery();
1132 // Redirect to a page title with possible query parameters
1133 if ( $redirect instanceof Title ) {
1134 $url = $redirect->getFullURL( $query );
1135 $this->getOutput()->redirect( $url );
1136 return $redirect;
1137 } elseif ( $redirect === true ) {
1138 // Redirect to index.php with query parameters
1139 $url = wfAppendQuery( wfScript( 'index' ), $query );
1140 $this->getOutput()->redirect( $url );
1141 return $redirect;
1142 } else {
1143 $class = get_class( $this );
1144 throw new MWException( "RedirectSpecialPage $class doesn't redirect!" );
1149 * If the special page is a redirect, then get the Title object it redirects to.
1150 * False otherwise.
1152 * @param string $par Subpage string
1153 * @return Title|bool
1155 abstract public function getRedirect( $par );
1158 * Return part of the request string for a special redirect page
1159 * This allows passing, e.g. action=history to Special:Mypage, etc.
1161 * @return String
1163 public function getRedirectQuery() {
1164 $params = array();
1166 foreach ( $this->mAllowedRedirectParams as $arg ) {
1167 if ( $this->getRequest()->getVal( $arg, null ) !== null ) {
1168 $params[$arg] = $this->getRequest()->getVal( $arg );
1172 foreach ( $this->mAddedRedirectParams as $arg => $val ) {
1173 $params[$arg] = $val;
1176 return count( $params )
1177 ? $params
1178 : false;
1182 abstract class SpecialRedirectToSpecial extends RedirectSpecialPage {
1183 // @todo FIXME: Visibility must be declared
1184 var $redirName, $redirSubpage;
1186 function __construct(
1187 $name, $redirName, $redirSubpage = false,
1188 $allowedRedirectParams = array(), $addedRedirectParams = array()
1190 parent::__construct( $name );
1191 $this->redirName = $redirName;
1192 $this->redirSubpage = $redirSubpage;
1193 $this->mAllowedRedirectParams = $allowedRedirectParams;
1194 $this->mAddedRedirectParams = $addedRedirectParams;
1197 public function getRedirect( $subpage ) {
1198 if ( $this->redirSubpage === false ) {
1199 return SpecialPage::getTitleFor( $this->redirName, $subpage );
1200 } else {
1201 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
1207 * ListAdmins --> ListUsers/sysop
1209 class SpecialListAdmins extends SpecialRedirectToSpecial {
1210 function __construct() {
1211 parent::__construct( 'Listadmins', 'Listusers', 'sysop' );
1216 * ListBots --> ListUsers/bot
1218 class SpecialListBots extends SpecialRedirectToSpecial {
1219 function __construct() {
1220 parent::__construct( 'Listbots', 'Listusers', 'bot' );
1225 * CreateAccount --> UserLogin/signup
1226 * @todo FIXME: This (and the rest of the login frontend) needs to die a horrible painful death
1228 class SpecialCreateAccount extends SpecialRedirectToSpecial {
1229 function __construct() {
1230 parent::__construct( 'CreateAccount', 'Userlogin', 'signup', array( 'uselang' ) );
1234 * SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
1235 * are used to get user independent links pointing to the user page, talk
1236 * page and list of contributions.
1237 * This can let us cache a single copy of any generated content for all
1238 * users.
1242 * Superclass for any RedirectSpecialPage which redirects the user
1243 * to a particular article (as opposed to user contributions, logs, etc.).
1245 * For security reasons these special pages are restricted to pass on
1246 * the following subset of GET parameters to the target page while
1247 * removing all others:
1249 * - useskin, uselang, printable: to alter the appearance of the resulting page
1251 * - redirect: allows viewing one's user page or talk page even if it is a
1252 * redirect.
1254 * - rdfrom: allows redirecting to one's user page or talk page from an
1255 * external wiki with the "Redirect from..." notice.
1257 * - limit, offset: Useful for linking to history of one's own user page or
1258 * user talk page. For example, this would be a link to "the last edit to your
1259 * user talk page in the year 2010":
1260 * http://en.wikipedia.org/wiki/Special:MyPage?offset=20110000000000&limit=1&action=history
1262 * - feed: would allow linking to the current user's RSS feed for their user
1263 * talk page:
1264 * http://en.wikipedia.org/w/index.php?title=Special:MyTalk&action=history&feed=rss
1266 * - preloadtitle: Can be used to provide a default section title for a
1267 * preloaded new comment on one's own talk page.
1269 * - summary : Can be used to provide a default edit summary for a preloaded
1270 * edit to one's own user page or talk page.
1272 * - preview: Allows showing/hiding preview on first edit regardless of user
1273 * preference, useful for preloaded edits where you know preview wouldn't be
1274 * useful.
1276 * - internaledit, externaledit, mode: Allows forcing the use of the
1277 * internal/external editor, e.g. to force the internal editor for
1278 * short/simple preloaded edits.
1280 * - redlink: Affects the message the user sees if their talk page/user talk
1281 * page does not currently exist. Avoids confusion for newbies with no user
1282 * pages over why they got a "permission error" following this link:
1283 * http://en.wikipedia.org/w/index.php?title=Special:MyPage&redlink=1
1285 * - debug: determines whether the debug parameter is passed to load.php,
1286 * which disables reformatting and allows scripts to be debugged. Useful
1287 * when debugging scripts that manipulate one's own user page or talk page.
1289 * @par Hook extension:
1290 * Extensions can add to the redirect parameters list by using the hook
1291 * RedirectSpecialArticleRedirectParams
1293 * This hook allows extensions which add GET parameters like FlaggedRevs to
1294 * retain those parameters when redirecting using special pages.
1296 * @par Hook extension example:
1297 * @code
1298 * $wgHooks['RedirectSpecialArticleRedirectParams'][] =
1299 * 'MyExtensionHooks::onRedirectSpecialArticleRedirectParams';
1300 * public static function onRedirectSpecialArticleRedirectParams( &$redirectParams ) {
1301 * $redirectParams[] = 'stable';
1302 * return true;
1304 * @endcode
1305 * @ingroup SpecialPage
1307 abstract class RedirectSpecialArticle extends RedirectSpecialPage {
1308 function __construct( $name ) {
1309 parent::__construct( $name );
1310 $redirectParams = array(
1311 'action',
1312 'redirect', 'rdfrom',
1313 # Options for preloaded edits
1314 'preload', 'editintro', 'preloadtitle', 'summary', 'nosummary',
1315 # Options for overriding user settings
1316 'preview', 'internaledit', 'externaledit', 'mode', 'minor', 'watchthis',
1317 # Options for history/diffs
1318 'section', 'oldid', 'diff', 'dir',
1319 'limit', 'offset', 'feed',
1320 # Misc options
1321 'redlink', 'debug',
1322 # Options for action=raw; missing ctype can break JS or CSS in some browsers
1323 'ctype', 'maxage', 'smaxage',
1326 wfRunHooks( "RedirectSpecialArticleRedirectParams", array( &$redirectParams ) );
1327 $this->mAllowedRedirectParams = $redirectParams;
1332 * Shortcut to construct a special page pointing to current user user's page.
1333 * @ingroup SpecialPage
1335 class SpecialMypage extends RedirectSpecialArticle {
1336 function __construct() {
1337 parent::__construct( 'Mypage' );
1340 function getRedirect( $subpage ) {
1341 if ( strval( $subpage ) !== '' ) {
1342 return Title::makeTitle( NS_USER, $this->getUser()->getName() . '/' . $subpage );
1343 } else {
1344 return Title::makeTitle( NS_USER, $this->getUser()->getName() );
1350 * Shortcut to construct a special page pointing to current user talk page.
1351 * @ingroup SpecialPage
1353 class SpecialMytalk extends RedirectSpecialArticle {
1354 function __construct() {
1355 parent::__construct( 'Mytalk' );
1358 function getRedirect( $subpage ) {
1359 if ( strval( $subpage ) !== '' ) {
1360 return Title::makeTitle( NS_USER_TALK, $this->getUser()->getName() . '/' . $subpage );
1361 } else {
1362 return Title::makeTitle( NS_USER_TALK, $this->getUser()->getName() );
1368 * Shortcut to construct a special page pointing to current user contributions.
1369 * @ingroup SpecialPage
1371 class SpecialMycontributions extends RedirectSpecialPage {
1372 function __construct() {
1373 parent::__construct( 'Mycontributions' );
1374 $this->mAllowedRedirectParams = array( 'limit', 'namespace', 'tagfilter',
1375 'offset', 'dir', 'year', 'month', 'feed' );
1378 function getRedirect( $subpage ) {
1379 return SpecialPage::getTitleFor( 'Contributions', $this->getUser()->getName() );
1384 * Redirect to Special:Listfiles?user=$wgUser
1386 class SpecialMyuploads extends RedirectSpecialPage {
1387 function __construct() {
1388 parent::__construct( 'Myuploads' );
1389 $this->mAllowedRedirectParams = array( 'limit' );
1392 function getRedirect( $subpage ) {
1393 return SpecialPage::getTitleFor( 'Listfiles', $this->getUser()->getName() );
1398 * Redirect from Special:PermanentLink/### to index.php?oldid=###
1400 class SpecialPermanentLink extends RedirectSpecialPage {
1401 function __construct() {
1402 parent::__construct( 'PermanentLink' );
1403 $this->mAllowedRedirectParams = array();
1406 function getRedirect( $subpage ) {
1407 $subpage = intval( $subpage );
1408 if ( $subpage === 0 ) {
1409 # throw an error page when no subpage was given
1410 throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
1412 $this->mAddedRedirectParams['oldid'] = $subpage;
1413 return true;