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
21 * @ingroup SpecialPage
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 * @ingroup SpecialPage
33 // The canonical name of this special page
34 // Also used for the default <h1> heading, @see getDescription()
37 // The local name of this special page
40 // Minimum user level required to access this page, or "" for anyone.
41 // Also used to categorise the pages in Special:Specialpages
42 protected $mRestriction;
44 // Listed in Special:Specialpages?
47 // Whether or not this special page is being included from an article
48 protected $mIncluding;
50 // Whether the special page can be included in an article
51 protected $mIncludable;
54 * Current request context
60 * Get a localised Title object for a specified special page name
63 * @param string|bool $subpage Subpage string, or false to not use a subpage
64 * @param string $fragment The link fragment (after the "#")
68 public static function getTitleFor( $name, $subpage = false, $fragment = '' ) {
69 $name = SpecialPageFactory
::getLocalNameFor( $name, $subpage );
71 return Title
::makeTitle( NS_SPECIAL
, $name, $fragment );
75 * Get a localised Title object for a page name with a possibly unvalidated subpage
78 * @param string|bool $subpage Subpage string, or false to not use a subpage
79 * @return Title|null Title object or null if the page doesn't exist
81 public static function getSafeTitleFor( $name, $subpage = false ) {
82 $name = SpecialPageFactory
::getLocalNameFor( $name, $subpage );
84 return Title
::makeTitleSafe( NS_SPECIAL
, $name );
91 * Default constructor for special pages
92 * Derivative classes should call this from their constructor
93 * Note that if the user does not have the required level, an error message will
94 * be displayed by the default execute() method, without the global function ever
97 * If you override execute(), you can recover the default behavior with userCanExecute()
98 * and displayRestrictionError()
100 * @param string $name Name of the special page, as seen in links and URLs
101 * @param string $restriction User right required, e.g. "block" or "delete"
102 * @param bool $listed Whether the page is listed in Special:Specialpages
103 * @param callable|bool $function Unused
104 * @param string $file Unused
105 * @param bool $includable Whether the page can be included in normal pages
107 public function __construct(
108 $name = '', $restriction = '', $listed = true,
109 $function = false, $file = '', $includable = false
111 $this->mName
= $name;
112 $this->mRestriction
= $restriction;
113 $this->mListed
= $listed;
114 $this->mIncludable
= $includable;
118 * Get the name of this Special Page.
126 * Get the permission that a user must have to execute this page
129 function getRestriction() {
130 return $this->mRestriction
;
133 // @todo FIXME: Decide which syntax to use for this, and stick to it
135 * Whether this special page is listed in Special:SpecialPages
139 function isListed() {
140 return $this->mListed
;
144 * Set whether this page is listed in Special:Specialpages, at run-time
146 * @param bool $listed
149 function setListed( $listed ) {
150 return wfSetVar( $this->mListed
, $listed );
154 * Get or set whether this special page is listed in Special:SpecialPages
159 function listed( $x = null ) {
160 return wfSetVar( $this->mListed
, $x );
164 * Whether it's allowed to transclude the special page via {{Special:Foo/params}}
167 public function isIncludable() {
168 return $this->mIncludable
;
172 * Whether the special page is being evaluated via transclusion
176 function including( $x = null ) {
177 return wfSetVar( $this->mIncluding
, $x );
181 * Get the localised name of the special page
184 function getLocalName() {
185 if ( !isset( $this->mLocalName
) ) {
186 $this->mLocalName
= SpecialPageFactory
::getLocalNameFor( $this->mName
);
189 return $this->mLocalName
;
193 * Is this page expensive (for some definition of expensive)?
194 * Expensive pages are disabled or cached in miser mode. Originally used
195 * (and still overridden) by QueryPage and subclasses, moved here so that
196 * Special:SpecialPages can safely call it for all special pages.
200 public function isExpensive() {
205 * Is this page cached?
206 * Expensive pages are cached or disabled in miser mode.
207 * Used by QueryPage and subclasses, moved here so that
208 * Special:SpecialPages can safely call it for all special pages.
213 public function isCached() {
218 * Can be overridden by subclasses with more complicated permissions
221 * @return bool Should the page be displayed with the restricted-access
224 public function isRestricted() {
225 // DWIM: If anons can do something, then it is not restricted
226 return $this->mRestriction
!= '' && !User
::groupHasPermission( '*', $this->mRestriction
);
230 * Checks if the given user (identified by an object) can execute this
231 * special page (as defined by $mRestriction). Can be overridden by sub-
232 * classes with more complicated permissions schemes.
234 * @param User $user The user to check
235 * @return bool Does the user have permission to view the page?
237 public function userCanExecute( User
$user ) {
238 return $user->isAllowed( $this->mRestriction
);
242 * Output an error message telling the user what access level they have to have
243 * @throws PermissionsError
245 function displayRestrictionError() {
246 throw new PermissionsError( $this->mRestriction
);
250 * Checks if userCanExecute, and if not throws a PermissionsError
254 * @throws PermissionsError
256 public function checkPermissions() {
257 if ( !$this->userCanExecute( $this->getUser() ) ) {
258 $this->displayRestrictionError();
263 * If the wiki is currently in readonly mode, throws a ReadOnlyError
267 * @throws ReadOnlyError
269 public function checkReadOnly() {
270 if ( wfReadOnly() ) {
271 throw new ReadOnlyError
;
276 * If the user is not logged in, throws UserNotLoggedIn error
278 * The user will be redirected to Special:Userlogin with the given message as an error on
282 * @param string $reasonMsg [optional] Message key to be displayed on login page
283 * @param string $titleMsg [optional] Passed on to UserNotLoggedIn constructor
284 * @throws UserNotLoggedIn
286 public function requireLogin(
287 $reasonMsg = 'exception-nologin-text', $titleMsg = 'exception-nologin'
289 if ( $this->getUser()->isAnon() ) {
290 throw new UserNotLoggedIn( $reasonMsg, $titleMsg );
295 * Return an array of subpages beginning with $search that this special page will accept.
297 * For example, if a page supports subpages "foo", "bar" and "baz" (as in Special:PageName/foo,
300 * - `prefixSearchSubpages( "ba" )` should return `array( "bar", "baz" )`
301 * - `prefixSearchSubpages( "f" )` should return `array( "foo" )`
302 * - `prefixSearchSubpages( "z" )` should return `array()`
303 * - `prefixSearchSubpages( "" )` should return `array( foo", "bar", "baz" )`
305 * @param string $search Prefix to search for
306 * @param int $limit Maximum number of results to return (usually 10)
307 * @param int $offset Number of results to skip (usually 0)
308 * @return string[] Matching subpages
310 public function prefixSearchSubpages( $search, $limit, $offset ) {
311 $subpages = $this->getSubpagesForPrefixSearch();
316 return self
::prefixSearchArray( $search, $limit, $subpages, $offset );
320 * Return an array of subpages that this special page will accept for prefix
321 * searches. If this method requires a query you might instead want to implement
322 * prefixSearchSubpages() directly so you can support $limit and $offset. This
323 * method is better for static-ish lists of things.
325 * @return string[] subpages to search from
327 protected function getSubpagesForPrefixSearch() {
332 * Helper function for implementations of prefixSearchSubpages() that
333 * filter the values in memory (as opposed to making a query).
336 * @param string $search
338 * @param array $subpages
342 protected static function prefixSearchArray( $search, $limit, array $subpages, $offset ) {
343 $escaped = preg_quote( $search, '/' );
344 return array_slice( preg_grep( "/^$escaped/i",
345 array_slice( $subpages, $offset ) ), 0, $limit );
349 * Sets headers - this should be called from the execute() method of all derived classes!
351 function setHeaders() {
352 $out = $this->getOutput();
353 $out->setArticleRelated( false );
354 $out->setRobotPolicy( $this->getRobotPolicy() );
355 $out->setPageTitle( $this->getDescription() );
356 if ( $this->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
357 $out->addModuleStyles( array(
358 'mediawiki.ui.input',
359 'mediawiki.ui.checkbox',
369 * @param string|null $subPage
371 final public function run( $subPage ) {
373 * Gets called before @see SpecialPage::execute.
377 * @param SpecialPage $this
378 * @param string|null $subPage
380 Hooks
::run( 'SpecialPageBeforeExecute', array( $this, $subPage ) );
382 $this->beforeExecute( $subPage );
383 $this->execute( $subPage );
384 $this->afterExecute( $subPage );
387 * Gets called after @see SpecialPage::execute.
391 * @param SpecialPage $this
392 * @param string|null $subPage
394 Hooks
::run( 'SpecialPageAfterExecute', array( $this, $subPage ) );
398 * Gets called before @see SpecialPage::execute.
402 * @param string|null $subPage
404 protected function beforeExecute( $subPage ) {
409 * Gets called after @see SpecialPage::execute.
413 * @param string|null $subPage
415 protected function afterExecute( $subPage ) {
420 * Default execute method
421 * Checks user permissions
423 * This must be overridden by subclasses; it will be made abstract in a future version
425 * @param string|null $subPage
427 public function execute( $subPage ) {
429 $this->checkPermissions();
430 $this->outputHeader();
434 * Outputs a summary message on top of special pages
435 * Per default the message key is the canonical name of the special page
436 * May be overridden, i.e. by extensions to stick with the naming conventions
437 * for message keys: 'extensionname-xxx'
439 * @param string $summaryMessageKey Message key of the summary
441 function outputHeader( $summaryMessageKey = '' ) {
444 if ( $summaryMessageKey == '' ) {
445 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
447 $msg = $summaryMessageKey;
449 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
450 $this->getOutput()->wrapWikiMsg(
451 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
456 * Returns the name that goes in the \<h1\> in the special page itself, and
457 * also the name that will be listed in Special:Specialpages
459 * Derived classes can override this, but usually it is easier to keep the
464 function getDescription() {
465 return $this->msg( strtolower( $this->mName
) )->text();
469 * Get a self-referential title object
471 * @param string|bool $subpage
473 * @deprecated since 1.23, use SpecialPage::getPageTitle
475 function getTitle( $subpage = false ) {
476 return $this->getPageTitle( $subpage );
480 * Get a self-referential title object
482 * @param string|bool $subpage
486 function getPageTitle( $subpage = false ) {
487 return self
::getTitleFor( $this->mName
, $subpage );
491 * Sets the context this SpecialPage is executed in
493 * @param IContextSource $context
496 public function setContext( $context ) {
497 $this->mContext
= $context;
501 * Gets the context this SpecialPage is executed in
503 * @return IContextSource|RequestContext
506 public function getContext() {
507 if ( $this->mContext
instanceof IContextSource
) {
508 return $this->mContext
;
510 wfDebug( __METHOD__
. " called and \$mContext is null. " .
511 "Return RequestContext::getMain(); for sanity\n" );
513 return RequestContext
::getMain();
518 * Get the WebRequest being used for this instance
523 public function getRequest() {
524 return $this->getContext()->getRequest();
528 * Get the OutputPage being used for this instance
533 public function getOutput() {
534 return $this->getContext()->getOutput();
538 * Shortcut to get the User executing this instance
543 public function getUser() {
544 return $this->getContext()->getUser();
548 * Shortcut to get the skin being used for this instance
553 public function getSkin() {
554 return $this->getContext()->getSkin();
558 * Shortcut to get user's language
563 public function getLanguage() {
564 return $this->getContext()->getLanguage();
568 * Shortcut to get main config object
572 public function getConfig() {
573 return $this->getContext()->getConfig();
577 * Return the full title, including $par
582 public function getFullTitle() {
583 return $this->getContext()->getTitle();
587 * Return the robot policy. Derived classes that override this can change
588 * the robot policy set by setHeaders() from the default 'noindex,nofollow'.
593 protected function getRobotPolicy() {
594 return 'noindex,nofollow';
598 * Wrapper around wfMessage that sets the current context.
603 public function msg( /* $args */ ) {
604 $message = call_user_func_array(
605 array( $this->getContext(), 'msg' ),
608 // RequestContext passes context to wfMessage, and the language is set from
609 // the context, but setting the language for Message class removes the
610 // interface message status, which breaks for example usernameless gender
611 // invocations. Restore the flag when not including special page in content.
612 if ( $this->including() ) {
613 $message->setInterfaceMessageFlag( false );
620 * Adds RSS/atom links
622 * @param array $params
624 protected function addFeedLinks( $params ) {
625 $feedTemplate = wfScript( 'api' );
627 foreach ( $this->getConfig()->get( 'FeedClasses' ) as $format => $class ) {
628 $theseParams = $params +
array( 'feedformat' => $format );
629 $url = wfAppendQuery( $feedTemplate, $theseParams );
630 $this->getOutput()->addFeedLink( $format, $url );
635 * Get the group that the special page belongs in on Special:SpecialPage
636 * Use this method, instead of getGroupName to allow customization
637 * of the group name from the wiki side
639 * @return string Group of this special page
642 public function getFinalGroupName() {
643 $name = $this->getName();
644 $specialPageGroups = $this->getConfig()->get( 'SpecialPageGroups' );
646 // Allow overbidding the group from the wiki side
647 $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
648 if ( !$msg->isBlank() ) {
649 $group = $msg->text();
651 // Than use the group from this object
652 $group = $this->getGroupName();
654 // Group '-' is used as default to have the chance to determine,
655 // if the special pages overrides this method,
656 // if not overridden, $wgSpecialPageGroups is checked for b/c
657 if ( $group === '-' && isset( $specialPageGroups[$name] ) ) {
658 $group = $specialPageGroups[$name];
662 // never give '-' back, change to 'other'
663 if ( $group === '-' ) {
671 * Under which header this special page is listed in Special:SpecialPages
672 * See messages 'specialpages-group-*' for valid names
673 * This method defaults to group 'other'
678 protected function getGroupName() {
679 // '-' used here to determine, if this group is overridden or has a hardcoded 'other'
680 // Needed for b/c in getFinalGroupName