Merge "Correct deprecation warning for $.quoteString"
[mediawiki.git] / includes / specialpage / SpecialPage.php
blobf9682762289f3d2bf3d5da519814e3338c056f8b
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 // Whether or not this special page is being included from an article
49 protected $mIncluding;
51 // Whether the special page can be included in an article
52 protected $mIncludable;
54 /**
55 * Current request context
56 * @var IContextSource
58 protected $mContext;
60 /**
61 * Get a localised Title object for a specified special page name
63 * @param string $name
64 * @param string|bool $subpage Subpage string, or false to not use a subpage
65 * @param string $fragment The link fragment (after the "#")
66 * @return Title
67 * @throws MWException
69 public static function getTitleFor( $name, $subpage = false, $fragment = '' ) {
70 $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
72 return Title::makeTitle( NS_SPECIAL, $name, $fragment );
75 /**
76 * Get a localised Title object for a page name with a possibly unvalidated subpage
78 * @param string $name
79 * @param string|bool $subpage Subpage string, or false to not use a subpage
80 * @return Title|null Title object or null if the page doesn't exist
82 public static function getSafeTitleFor( $name, $subpage = false ) {
83 $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
84 if ( $name ) {
85 return Title::makeTitleSafe( NS_SPECIAL, $name );
86 } else {
87 return null;
91 /**
92 * Default constructor for special pages
93 * Derivative classes should call this from their constructor
94 * Note that if the user does not have the required level, an error message will
95 * be displayed by the default execute() method, without the global function ever
96 * being called.
98 * If you override execute(), you can recover the default behavior with userCanExecute()
99 * and displayRestrictionError()
101 * @param string $name Name of the special page, as seen in links and URLs
102 * @param string $restriction User right required, e.g. "block" or "delete"
103 * @param bool $listed Whether the page is listed in Special:Specialpages
104 * @param callable|bool $function unused
105 * @param string $file unused
106 * @param bool $includable Whether the page can be included in normal pages
108 public function __construct(
109 $name = '', $restriction = '', $listed = true,
110 $function = false, $file = '', $includable = false
112 $this->mName = $name;
113 $this->mRestriction = $restriction;
114 $this->mListed = $listed;
115 $this->mIncludable = $includable;
119 * Get the name of this Special Page.
120 * @return string
122 function getName() {
123 return $this->mName;
127 * Get the permission that a user must have to execute this page
128 * @return string
130 function getRestriction() {
131 return $this->mRestriction;
134 // @todo FIXME: Decide which syntax to use for this, and stick to it
136 * Whether this special page is listed in Special:SpecialPages
137 * @since r3583 (v1.3)
138 * @return bool
140 function isListed() {
141 return $this->mListed;
145 * Set whether this page is listed in Special:Specialpages, at run-time
146 * @since 1.3
147 * @param bool $listed
148 * @return bool
150 function setListed( $listed ) {
151 return wfSetVar( $this->mListed, $listed );
155 * Get or set whether this special page is listed in Special:SpecialPages
156 * @since 1.6
157 * @param bool $x
158 * @return bool
160 function listed( $x = null ) {
161 return wfSetVar( $this->mListed, $x );
165 * Whether it's allowed to transclude the special page via {{Special:Foo/params}}
166 * @return bool
168 public function isIncludable() {
169 return $this->mIncludable;
173 * Whether the special page is being evaluated via transclusion
174 * @param bool $x
175 * @return bool
177 function including( $x = null ) {
178 return wfSetVar( $this->mIncluding, $x );
182 * Get the localised name of the special page
183 * @return string
185 function getLocalName() {
186 if ( !isset( $this->mLocalName ) ) {
187 $this->mLocalName = SpecialPageFactory::getLocalNameFor( $this->mName );
190 return $this->mLocalName;
194 * Is this page expensive (for some definition of expensive)?
195 * Expensive pages are disabled or cached in miser mode. Originally used
196 * (and still overridden) by QueryPage and subclasses, moved here so that
197 * Special:SpecialPages can safely call it for all special pages.
199 * @return bool
201 public function isExpensive() {
202 return false;
206 * Is this page cached?
207 * Expensive pages are cached or disabled in miser mode.
208 * Used by QueryPage and subclasses, moved here so that
209 * Special:SpecialPages can safely call it for all special pages.
211 * @return bool
212 * @since 1.21
214 public function isCached() {
215 return false;
219 * Can be overridden by subclasses with more complicated permissions
220 * schemes.
222 * @return bool Should the page be displayed with the restricted-access
223 * pages?
225 public function isRestricted() {
226 // DWIM: If anons can do something, then it is not restricted
227 return $this->mRestriction != '' && !User::groupHasPermission( '*', $this->mRestriction );
231 * Checks if the given user (identified by an object) can execute this
232 * special page (as defined by $mRestriction). Can be overridden by sub-
233 * classes with more complicated permissions schemes.
235 * @param User $user The user to check
236 * @return bool Does the user have permission to view the page?
238 public function userCanExecute( User $user ) {
239 return $user->isAllowed( $this->mRestriction );
243 * Output an error message telling the user what access level they have to have
244 * @throws PermissionsError
246 function displayRestrictionError() {
247 throw new PermissionsError( $this->mRestriction );
251 * Checks if userCanExecute, and if not throws a PermissionsError
253 * @since 1.19
254 * @return void
255 * @throws PermissionsError
257 public function checkPermissions() {
258 if ( !$this->userCanExecute( $this->getUser() ) ) {
259 $this->displayRestrictionError();
264 * If the wiki is currently in readonly mode, throws a ReadOnlyError
266 * @since 1.19
267 * @return void
268 * @throws ReadOnlyError
270 public function checkReadOnly() {
271 if ( wfReadOnly() ) {
272 throw new ReadOnlyError;
277 * If the user is not logged in, throws UserNotLoggedIn error.
279 * Default error message includes a link to Special:Userlogin with properly set 'returnto' query
280 * parameter.
282 * @since 1.23
283 * @param string|Message $reasonMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
284 * will be used as message keys. If a string is given, the message will also receive a
285 * formatted login link (generated using the 'loginreqlink' message) as first parameter. If a
286 * Message is given, it will be passed on verbatim.
287 * @param string|Message $titleMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
288 * will be used as message keys.
289 * @throws UserNotLoggedIn
291 public function requireLogin( $reasonMsg = null, $titleMsg = null ) {
292 if ( $this->getUser()->isAnon() ) {
293 // Use default messages if not given or explicit null passed
294 if ( !$reasonMsg ) {
295 $reasonMsg = 'exception-nologin-text-manual';
297 if ( !$titleMsg ) {
298 $titleMsg = 'exception-nologin';
301 // Convert to Messages with current context
302 if ( is_string( $reasonMsg ) ) {
303 $loginreqlink = Linker::linkKnown(
304 SpecialPage::getTitleFor( 'Userlogin' ),
305 $this->msg( 'loginreqlink' )->escaped(),
306 array(),
307 array( 'returnto' => $this->getPageTitle()->getPrefixedText() )
309 $reasonMsg = $this->msg( $reasonMsg )->rawParams( $loginreqlink );
311 if ( is_string( $titleMsg ) ) {
312 $titleMsg = $this->msg( $titleMsg );
315 throw new UserNotLoggedIn( $reasonMsg, $titleMsg );
320 * Return an array of subpages beginning with $search that this special page will accept.
322 * For example, if a page supports subpages "foo", "bar" and "baz" (as in Special:PageName/foo,
323 * etc.):
325 * - `prefixSearchSubpages( "ba" )` should return `array( "bar", "baz" )`
326 * - `prefixSearchSubpages( "f" )` should return `array( "foo" )`
327 * - `prefixSearchSubpages( "z" )` should return `array()`
328 * - `prefixSearchSubpages( "" )` should return `array( foo", "bar", "baz" )`
330 * @param string $search Prefix to search for
331 * @param integer $limit Maximum number of results to return
332 * @return string[] Matching subpages
334 public function prefixSearchSubpages( $search, $limit = 10 ) {
335 return array();
339 * Helper function for implementations of prefixSearchSubpages() that
340 * filter the values in memory (as oppposed to making a query).
342 * @since 1.24
343 * @param string $search
344 * @param int $limit
345 * @param array $subpages
346 * @return string[]
348 protected static function prefixSearchArray( $search, $limit, array $subpages ) {
349 $escaped = preg_quote( $search, '/' );
350 return array_slice( preg_grep( "/^$escaped/i", $subpages ), 0, $limit );
354 * Sets headers - this should be called from the execute() method of all derived classes!
356 function setHeaders() {
357 $out = $this->getOutput();
358 $out->setArticleRelated( false );
359 $out->setRobotPolicy( $this->getRobotPolicy() );
360 $out->setPageTitle( $this->getDescription() );
364 * Entry point.
366 * @since 1.20
368 * @param string|null $subPage
370 final public function run( $subPage ) {
372 * Gets called before @see SpecialPage::execute.
374 * @since 1.20
376 * @param SpecialPage $this
377 * @param string|null $subPage
379 wfRunHooks( 'SpecialPageBeforeExecute', array( $this, $subPage ) );
381 $this->beforeExecute( $subPage );
382 $this->execute( $subPage );
383 $this->afterExecute( $subPage );
386 * Gets called after @see SpecialPage::execute.
388 * @since 1.20
390 * @param SpecialPage $this
391 * @param string|null $subPage
393 wfRunHooks( 'SpecialPageAfterExecute', array( $this, $subPage ) );
397 * Gets called before @see SpecialPage::execute.
399 * @since 1.20
401 * @param string|null $subPage
403 protected function beforeExecute( $subPage ) {
404 // No-op
408 * Gets called after @see SpecialPage::execute.
410 * @since 1.20
412 * @param string|null $subPage
414 protected function afterExecute( $subPage ) {
415 // No-op
419 * Default execute method
420 * Checks user permissions
422 * This must be overridden by subclasses; it will be made abstract in a future version
424 * @param string|null $subPage
426 public function execute( $subPage ) {
427 $this->setHeaders();
428 $this->checkPermissions();
429 $this->outputHeader();
433 * Outputs a summary message on top of special pages
434 * Per default the message key is the canonical name of the special page
435 * May be overridden, i.e. by extensions to stick with the naming conventions
436 * for message keys: 'extensionname-xxx'
438 * @param string $summaryMessageKey Message key of the summary
440 function outputHeader( $summaryMessageKey = '' ) {
441 global $wgContLang;
443 if ( $summaryMessageKey == '' ) {
444 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
445 } else {
446 $msg = $summaryMessageKey;
448 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
449 $this->getOutput()->wrapWikiMsg(
450 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
455 * Returns the name that goes in the \<h1\> in the special page itself, and
456 * also the name that will be listed in Special:Specialpages
458 * Derived classes can override this, but usually it is easier to keep the
459 * default behavior.
461 * @return string
463 function getDescription() {
464 return $this->msg( strtolower( $this->mName ) )->text();
468 * Get a self-referential title object
470 * @param string|bool $subpage
471 * @return Title
472 * @deprecated since 1.23, use SpecialPage::getPageTitle
474 function getTitle( $subpage = false ) {
475 return $this->getPageTitle( $subpage );
479 * Get a self-referential title object
481 * @param string|bool $subpage
482 * @return Title
483 * @since 1.23
485 function getPageTitle( $subpage = false ) {
486 return self::getTitleFor( $this->mName, $subpage );
490 * Sets the context this SpecialPage is executed in
492 * @param IContextSource $context
493 * @since 1.18
495 public function setContext( $context ) {
496 $this->mContext = $context;
500 * Gets the context this SpecialPage is executed in
502 * @return IContextSource|RequestContext
503 * @since 1.18
505 public function getContext() {
506 if ( $this->mContext instanceof IContextSource ) {
507 return $this->mContext;
508 } else {
509 wfDebug( __METHOD__ . " called and \$mContext is null. " .
510 "Return RequestContext::getMain(); for sanity\n" );
512 return RequestContext::getMain();
517 * Get the WebRequest being used for this instance
519 * @return WebRequest
520 * @since 1.18
522 public function getRequest() {
523 return $this->getContext()->getRequest();
527 * Get the OutputPage being used for this instance
529 * @return OutputPage
530 * @since 1.18
532 public function getOutput() {
533 return $this->getContext()->getOutput();
537 * Shortcut to get the User executing this instance
539 * @return User
540 * @since 1.18
542 public function getUser() {
543 return $this->getContext()->getUser();
547 * Shortcut to get the skin being used for this instance
549 * @return Skin
550 * @since 1.18
552 public function getSkin() {
553 return $this->getContext()->getSkin();
557 * Shortcut to get user's language
559 * @return Language
560 * @since 1.19
562 public function getLanguage() {
563 return $this->getContext()->getLanguage();
567 * Shortcut to get main config object
568 * @return Config
569 * @since 1.24
571 public function getConfig() {
572 return $this->getContext()->getConfig();
576 * Return the full title, including $par
578 * @return Title
579 * @since 1.18
581 public function getFullTitle() {
582 return $this->getContext()->getTitle();
586 * Return the robot policy. Derived classes that override this can change
587 * the robot policy set by setHeaders() from the default 'noindex,nofollow'.
589 * @return string
590 * @since 1.23
592 protected function getRobotPolicy() {
593 return 'noindex,nofollow';
597 * Wrapper around wfMessage that sets the current context.
599 * @return Message
600 * @see wfMessage
602 public function msg( /* $args */ ) {
603 $message = call_user_func_array(
604 array( $this->getContext(), 'msg' ),
605 func_get_args()
607 // RequestContext passes context to wfMessage, and the language is set from
608 // the context, but setting the language for Message class removes the
609 // interface message status, which breaks for example usernameless gender
610 // invocations. Restore the flag when not including special page in content.
611 if ( $this->including() ) {
612 $message->setInterfaceMessageFlag( false );
615 return $message;
619 * Adds RSS/atom links
621 * @param array $params
623 protected function addFeedLinks( $params ) {
624 global $wgFeedClasses;
626 $feedTemplate = wfScript( 'api' );
628 foreach ( $wgFeedClasses as $format => $class ) {
629 $theseParams = $params + array( 'feedformat' => $format );
630 $url = wfAppendQuery( $feedTemplate, $theseParams );
631 $this->getOutput()->addFeedLink( $format, $url );
636 * Get the group that the special page belongs in on Special:SpecialPage
637 * Use this method, instead of getGroupName to allow customization
638 * of the group name from the wiki side
640 * @return string Group of this special page
641 * @since 1.21
643 public function getFinalGroupName() {
644 global $wgSpecialPageGroups;
645 $name = $this->getName();
647 // Allow overbidding the group from the wiki side
648 $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
649 if ( !$msg->isBlank() ) {
650 $group = $msg->text();
651 } else {
652 // Than use the group from this object
653 $group = $this->getGroupName();
655 // Group '-' is used as default to have the chance to determine,
656 // if the special pages overrides this method,
657 // if not overridden, $wgSpecialPageGroups is checked for b/c
658 if ( $group === '-' && isset( $wgSpecialPageGroups[$name] ) ) {
659 $group = $wgSpecialPageGroups[$name];
663 // never give '-' back, change to 'other'
664 if ( $group === '-' ) {
665 $group = 'other';
668 return $group;
672 * Under which header this special page is listed in Special:SpecialPages
673 * See messages 'specialpages-group-*' for valid names
674 * This method defaults to group 'other'
676 * @return string
677 * @since 1.21
679 protected function getGroupName() {
680 // '-' used here to determine, if this group is overridden or has a hardcoded 'other'
681 // Needed for b/c in getFinalGroupName
682 return '-';