Merge "mediawiki.api: Use then() in getToken instead of manual Deferred wrapping"
[mediawiki.git] / includes / specialpage / SpecialPage.php
blob82d617743f1fcc42f63c55f334292d8bb77e176a
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 class for all special pages.
27 * Includes some static functions for handling the special page list deprecated
28 * in favor of SpecialPageFactory.
30 * @todo Turn this into a real ContextSource
31 * @ingroup SpecialPage
33 class SpecialPage {
34 // The canonical name of this special page
35 // Also used for the default <h1> heading, @see getDescription()
36 protected $mName;
38 // The local name of this special page
39 private $mLocalName;
41 // Minimum user level required to access this page, or "" for anyone.
42 // Also used to categorise the pages in Special:Specialpages
43 private $mRestriction;
45 // Listed in Special:Specialpages?
46 private $mListed;
48 // Function name called by the default execute()
49 private $mFunction;
51 // File which needs to be included before the function above can be called
52 private $mFile;
54 // Whether or not this special page is being included from an article
55 protected $mIncluding;
57 // Whether the special page can be included in an article
58 protected $mIncludable;
60 /**
61 * Current request context
62 * @var IContextSource
64 protected $mContext;
66 /**
67 * Get a localised Title object for a specified special page name
69 * @param string $name
70 * @param string|bool $subpage Subpage string, or false to not use a subpage
71 * @param string $fragment The link fragment (after the "#")
72 * @return Title
73 * @throws MWException
75 public static function getTitleFor( $name, $subpage = false, $fragment = '' ) {
76 $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
78 return Title::makeTitle( NS_SPECIAL, $name, $fragment );
81 /**
82 * Get a localised Title object for a page name with a possibly unvalidated subpage
84 * @param string $name
85 * @param string|bool $subpage Subpage string, or false to not use a subpage
86 * @return Title|null Title object or null if the page doesn't exist
88 public static function getSafeTitleFor( $name, $subpage = false ) {
89 $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
90 if ( $name ) {
91 return Title::makeTitleSafe( NS_SPECIAL, $name );
92 } else {
93 return null;
97 /**
98 * Default constructor for special pages
99 * Derivative classes should call this from their constructor
100 * Note that if the user does not have the required level, an error message will
101 * be displayed by the default execute() method, without the global function ever
102 * being called.
104 * If you override execute(), you can recover the default behavior with userCanExecute()
105 * and displayRestrictionError()
107 * @param string $name Name of the special page, as seen in links and URLs
108 * @param string $restriction User right required, e.g. "block" or "delete"
109 * @param bool $listed Whether the page is listed in Special:Specialpages
110 * @param callable|bool $function Function called by execute(). By default
111 * it is constructed from $name
112 * @param string $file File which is included by execute(). It is also
113 * constructed from $name by default
114 * @param bool $includable Whether the page can be included in normal pages
116 public function __construct(
117 $name = '', $restriction = '', $listed = true,
118 $function = false, $file = 'default', $includable = false
120 $this->mName = $name;
121 $this->mRestriction = $restriction;
122 $this->mListed = $listed;
123 $this->mIncludable = $includable;
124 if ( !$function ) {
125 $this->mFunction = 'wfSpecial' . $name;
126 } else {
127 $this->mFunction = $function;
129 if ( $file === 'default' ) {
130 $this->mFile = __DIR__ . "/specials/Special$name.php";
131 } else {
132 $this->mFile = $file;
137 * Get the name of this Special Page.
138 * @return string
140 function getName() {
141 return $this->mName;
145 * Get the permission that a user must have to execute this page
146 * @return string
148 function getRestriction() {
149 return $this->mRestriction;
153 * Get the file which will be included by SpecialPage::execute() if your extension is
154 * still stuck in the past and hasn't overridden the execute() method. No modern code
155 * should want or need to know this.
156 * @return string
157 * @deprecated since 1.18
159 function getFile() {
160 wfDeprecated( __METHOD__, '1.18' );
162 return $this->mFile;
165 // @todo FIXME: Decide which syntax to use for this, and stick to it
167 * Whether this special page is listed in Special:SpecialPages
168 * @since r3583 (v1.3)
169 * @return bool
171 function isListed() {
172 return $this->mListed;
176 * Set whether this page is listed in Special:Specialpages, at run-time
177 * @since 1.3
178 * @param bool $listed
179 * @return bool
181 function setListed( $listed ) {
182 return wfSetVar( $this->mListed, $listed );
186 * Get or set whether this special page is listed in Special:SpecialPages
187 * @since 1.6
188 * @param bool $x
189 * @return bool
191 function listed( $x = null ) {
192 return wfSetVar( $this->mListed, $x );
196 * Whether it's allowed to transclude the special page via {{Special:Foo/params}}
197 * @return bool
199 public function isIncludable() {
200 return $this->mIncludable;
204 * Whether the special page is being evaluated via transclusion
205 * @param bool $x
206 * @return bool
208 function including( $x = null ) {
209 return wfSetVar( $this->mIncluding, $x );
213 * Get the localised name of the special page
214 * @return string
216 function getLocalName() {
217 if ( !isset( $this->mLocalName ) ) {
218 $this->mLocalName = SpecialPageFactory::getLocalNameFor( $this->mName );
221 return $this->mLocalName;
225 * Is this page expensive (for some definition of expensive)?
226 * Expensive pages are disabled or cached in miser mode. Originally used
227 * (and still overridden) by QueryPage and subclasses, moved here so that
228 * Special:SpecialPages can safely call it for all special pages.
230 * @return bool
232 public function isExpensive() {
233 return false;
237 * Is this page cached?
238 * Expensive pages are cached or disabled in miser mode.
239 * Used by QueryPage and subclasses, moved here so that
240 * Special:SpecialPages can safely call it for all special pages.
242 * @return bool
243 * @since 1.21
245 public function isCached() {
246 return false;
250 * Can be overridden by subclasses with more complicated permissions
251 * schemes.
253 * @return bool Should the page be displayed with the restricted-access
254 * pages?
256 public function isRestricted() {
257 // DWIM: If anons can do something, then it is not restricted
258 return $this->mRestriction != '' && !User::groupHasPermission( '*', $this->mRestriction );
262 * Checks if the given user (identified by an object) can execute this
263 * special page (as defined by $mRestriction). Can be overridden by sub-
264 * classes with more complicated permissions schemes.
266 * @param User $user The user to check
267 * @return bool Does the user have permission to view the page?
269 public function userCanExecute( User $user ) {
270 return $user->isAllowed( $this->mRestriction );
274 * Output an error message telling the user what access level they have to have
275 * @throws PermissionsError
277 function displayRestrictionError() {
278 throw new PermissionsError( $this->mRestriction );
282 * Checks if userCanExecute, and if not throws a PermissionsError
284 * @since 1.19
285 * @return void
286 * @throws PermissionsError
288 public function checkPermissions() {
289 if ( !$this->userCanExecute( $this->getUser() ) ) {
290 $this->displayRestrictionError();
295 * If the wiki is currently in readonly mode, throws a ReadOnlyError
297 * @since 1.19
298 * @return void
299 * @throws ReadOnlyError
301 public function checkReadOnly() {
302 if ( wfReadOnly() ) {
303 throw new ReadOnlyError;
308 * If the user is not logged in, throws UserNotLoggedIn error.
310 * Default error message includes a link to Special:Userlogin with properly set 'returnto' query
311 * parameter.
313 * @since 1.23
314 * @param string|Message $reasonMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
315 * will be used as message keys. If a string is given, the message will also receive a
316 * formatted login link (generated using the 'loginreqlink' message) as first parameter. If a
317 * Message is given, it will be passed on verbatim.
318 * @param string|Message $titleMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
319 * will be used as message keys.
320 * @throws UserNotLoggedIn
322 public function requireLogin( $reasonMsg = null, $titleMsg = null ) {
323 if ( $this->getUser()->isAnon() ) {
324 // Use default messages if not given or explicit null passed
325 if ( !$reasonMsg ) {
326 $reasonMsg = 'exception-nologin-text-manual';
328 if ( !$titleMsg ) {
329 $titleMsg = 'exception-nologin';
332 // Convert to Messages with current context
333 if ( is_string( $reasonMsg ) ) {
334 $loginreqlink = Linker::linkKnown(
335 SpecialPage::getTitleFor( 'Userlogin' ),
336 $this->msg( 'loginreqlink' )->escaped(),
337 array(),
338 array( 'returnto' => $this->getPageTitle()->getPrefixedText() )
340 $reasonMsg = $this->msg( $reasonMsg )->rawParams( $loginreqlink );
342 if ( is_string( $titleMsg ) ) {
343 $titleMsg = $this->msg( $titleMsg );
346 throw new UserNotLoggedIn( $reasonMsg, $titleMsg );
351 * Sets headers - this should be called from the execute() method of all derived classes!
353 function setHeaders() {
354 $out = $this->getOutput();
355 $out->setArticleRelated( false );
356 $out->setRobotPolicy( $this->getRobotPolicy() );
357 $out->setPageTitle( $this->getDescription() );
361 * Entry point.
363 * @since 1.20
365 * @param string|null $subPage
367 final public function run( $subPage ) {
369 * Gets called before @see SpecialPage::execute.
371 * @since 1.20
373 * @param SpecialPage $this
374 * @param string|null $subPage
376 wfRunHooks( 'SpecialPageBeforeExecute', array( $this, $subPage ) );
378 $this->beforeExecute( $subPage );
379 $this->execute( $subPage );
380 $this->afterExecute( $subPage );
383 * Gets called after @see SpecialPage::execute.
385 * @since 1.20
387 * @param SpecialPage $this
388 * @param string|null $subPage
390 wfRunHooks( 'SpecialPageAfterExecute', array( $this, $subPage ) );
394 * Gets called before @see SpecialPage::execute.
396 * @since 1.20
398 * @param string|null $subPage
400 protected function beforeExecute( $subPage ) {
401 // No-op
405 * Gets called after @see SpecialPage::execute.
407 * @since 1.20
409 * @param string|null $subPage
411 protected function afterExecute( $subPage ) {
412 // No-op
416 * Default execute method
417 * Checks user permissions, calls the function given in mFunction
419 * This must be overridden by subclasses; it will be made abstract in a future version
421 * @param string|null $subPage
423 public function execute( $subPage ) {
424 $this->setHeaders();
425 $this->checkPermissions();
427 $func = $this->mFunction;
428 // only load file if the function does not exist
429 if ( !is_callable( $func ) && $this->mFile ) {
430 require_once $this->mFile;
432 $this->outputHeader();
433 call_user_func( $func, $subPage, $this );
437 * Outputs a summary message on top of special pages
438 * Per default the message key is the canonical name of the special page
439 * May be overridden, i.e. by extensions to stick with the naming conventions
440 * for message keys: 'extensionname-xxx'
442 * @param string $summaryMessageKey Message key of the summary
444 function outputHeader( $summaryMessageKey = '' ) {
445 global $wgContLang;
447 if ( $summaryMessageKey == '' ) {
448 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
449 } else {
450 $msg = $summaryMessageKey;
452 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
453 $this->getOutput()->wrapWikiMsg(
454 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
459 * Returns the name that goes in the \<h1\> in the special page itself, and
460 * also the name that will be listed in Special:Specialpages
462 * Derived classes can override this, but usually it is easier to keep the
463 * default behavior.
465 * @return string
467 function getDescription() {
468 return $this->msg( strtolower( $this->mName ) )->text();
472 * Get a self-referential title object
474 * @param string|bool $subpage
475 * @return Title
476 * @deprecated since 1.23, use SpecialPage::getPageTitle
478 function getTitle( $subpage = false ) {
479 return $this->getPageTitle( $subpage );
483 * Get a self-referential title object
485 * @param string|bool $subpage
486 * @return Title
487 * @since 1.23
489 function getPageTitle( $subpage = false ) {
490 return self::getTitleFor( $this->mName, $subpage );
494 * Sets the context this SpecialPage is executed in
496 * @param IContextSource $context
497 * @since 1.18
499 public function setContext( $context ) {
500 $this->mContext = $context;
504 * Gets the context this SpecialPage is executed in
506 * @return IContextSource|RequestContext
507 * @since 1.18
509 public function getContext() {
510 if ( $this->mContext instanceof IContextSource ) {
511 return $this->mContext;
512 } else {
513 wfDebug( __METHOD__ . " called and \$mContext is null. " .
514 "Return RequestContext::getMain(); for sanity\n" );
516 return RequestContext::getMain();
521 * Get the WebRequest being used for this instance
523 * @return WebRequest
524 * @since 1.18
526 public function getRequest() {
527 return $this->getContext()->getRequest();
531 * Get the OutputPage being used for this instance
533 * @return OutputPage
534 * @since 1.18
536 public function getOutput() {
537 return $this->getContext()->getOutput();
541 * Shortcut to get the User executing this instance
543 * @return User
544 * @since 1.18
546 public function getUser() {
547 return $this->getContext()->getUser();
551 * Shortcut to get the skin being used for this instance
553 * @return Skin
554 * @since 1.18
556 public function getSkin() {
557 return $this->getContext()->getSkin();
561 * Shortcut to get user's language
563 * @deprecated since 1.19 Use getLanguage instead
564 * @return Language
565 * @since 1.18
567 public function getLang() {
568 wfDeprecated( __METHOD__, '1.19' );
570 return $this->getLanguage();
574 * Shortcut to get user's language
576 * @return Language
577 * @since 1.19
579 public function getLanguage() {
580 return $this->getContext()->getLanguage();
584 * Return the full title, including $par
586 * @return Title
587 * @since 1.18
589 public function getFullTitle() {
590 return $this->getContext()->getTitle();
594 * Return the robot policy. Derived classes that override this can change
595 * the robot policy set by setHeaders() from the default 'noindex,nofollow'.
597 * @return string
598 * @since 1.23
600 protected function getRobotPolicy() {
601 return 'noindex,nofollow';
605 * Wrapper around wfMessage that sets the current context.
607 * @return Message
608 * @see wfMessage
610 public function msg( /* $args */ ) {
611 $message = call_user_func_array(
612 array( $this->getContext(), 'msg' ),
613 func_get_args()
615 // RequestContext passes context to wfMessage, and the language is set from
616 // the context, but setting the language for Message class removes the
617 // interface message status, which breaks for example usernameless gender
618 // invocations. Restore the flag when not including special page in content.
619 if ( $this->including() ) {
620 $message->setInterfaceMessageFlag( false );
623 return $message;
627 * Adds RSS/atom links
629 * @param array $params
631 protected function addFeedLinks( $params ) {
632 global $wgFeedClasses;
634 $feedTemplate = wfScript( 'api' );
636 foreach ( $wgFeedClasses as $format => $class ) {
637 $theseParams = $params + array( 'feedformat' => $format );
638 $url = wfAppendQuery( $feedTemplate, $theseParams );
639 $this->getOutput()->addFeedLink( $format, $url );
644 * Get the group that the special page belongs in on Special:SpecialPage
645 * Use this method, instead of getGroupName to allow customization
646 * of the group name from the wiki side
648 * @return string Group of this special page
649 * @since 1.21
651 public function getFinalGroupName() {
652 global $wgSpecialPageGroups;
653 $name = $this->getName();
655 // Allow overbidding the group from the wiki side
656 $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
657 if ( !$msg->isBlank() ) {
658 $group = $msg->text();
659 } else {
660 // Than use the group from this object
661 $group = $this->getGroupName();
663 // Group '-' is used as default to have the chance to determine,
664 // if the special pages overrides this method,
665 // if not overridden, $wgSpecialPageGroups is checked for b/c
666 if ( $group === '-' && isset( $wgSpecialPageGroups[$name] ) ) {
667 $group = $wgSpecialPageGroups[$name];
671 // never give '-' back, change to 'other'
672 if ( $group === '-' ) {
673 $group = 'other';
676 return $group;
680 * Under which header this special page is listed in Special:SpecialPages
681 * See messages 'specialpages-group-*' for valid names
682 * This method defaults to group 'other'
684 * @return string
685 * @since 1.21
687 protected function getGroupName() {
688 // '-' used here to determine, if this group is overridden or has a hardcoded 'other'
689 // Needed for b/c in getFinalGroupName
690 return '-';