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 * @since 1.21 $fragment parameter added
66 * @param string|bool $subpage Subpage string, or false to not use a subpage
67 * @param string $fragment The link fragment (after the "#")
71 public static function getTitleFor( $name, $subpage = false, $fragment = '' ) {
72 $name = SpecialPageFactory
::getLocalNameFor( $name, $subpage );
74 return Title
::makeTitle( NS_SPECIAL
, $name, $fragment );
78 * Get a localised Title object for a page name with a possibly unvalidated subpage
81 * @param string|bool $subpage Subpage string, or false to not use a subpage
82 * @return Title|null Title object or null if the page doesn't exist
84 public static function getSafeTitleFor( $name, $subpage = false ) {
85 $name = SpecialPageFactory
::getLocalNameFor( $name, $subpage );
87 return Title
::makeTitleSafe( NS_SPECIAL
, $name );
94 * Default constructor for special pages
95 * Derivative classes should call this from their constructor
96 * Note that if the user does not have the required level, an error message will
97 * be displayed by the default execute() method, without the global function ever
100 * If you override execute(), you can recover the default behavior with userCanExecute()
101 * and displayRestrictionError()
103 * @param string $name Name of the special page, as seen in links and URLs
104 * @param string $restriction User right required, e.g. "block" or "delete"
105 * @param bool $listed Whether the page is listed in Special:Specialpages
106 * @param callable|bool $function Unused
107 * @param string $file Unused
108 * @param bool $includable Whether the page can be included in normal pages
110 public function __construct(
111 $name = '', $restriction = '', $listed = true,
112 $function = false, $file = '', $includable = false
114 $this->mName
= $name;
115 $this->mRestriction
= $restriction;
116 $this->mListed
= $listed;
117 $this->mIncludable
= $includable;
121 * Get the name of this Special Page.
129 * Get the permission that a user must have to execute this page
132 function getRestriction() {
133 return $this->mRestriction
;
136 // @todo FIXME: Decide which syntax to use for this, and stick to it
138 * Whether this special page is listed in Special:SpecialPages
142 function isListed() {
143 return $this->mListed
;
147 * Set whether this page is listed in Special:Specialpages, at run-time
149 * @param bool $listed
152 function setListed( $listed ) {
153 return wfSetVar( $this->mListed
, $listed );
157 * Get or set whether this special page is listed in Special:SpecialPages
162 function listed( $x = null ) {
163 return wfSetVar( $this->mListed
, $x );
167 * Whether it's allowed to transclude the special page via {{Special:Foo/params}}
170 public function isIncludable() {
171 return $this->mIncludable
;
175 * Whether the special page is being evaluated via transclusion
179 function including( $x = null ) {
180 return wfSetVar( $this->mIncluding
, $x );
184 * Get the localised name of the special page
187 function getLocalName() {
188 if ( !isset( $this->mLocalName
) ) {
189 $this->mLocalName
= SpecialPageFactory
::getLocalNameFor( $this->mName
);
192 return $this->mLocalName
;
196 * Is this page expensive (for some definition of expensive)?
197 * Expensive pages are disabled or cached in miser mode. Originally used
198 * (and still overridden) by QueryPage and subclasses, moved here so that
199 * Special:SpecialPages can safely call it for all special pages.
203 public function isExpensive() {
208 * Is this page cached?
209 * Expensive pages are cached or disabled in miser mode.
210 * Used by QueryPage and subclasses, moved here so that
211 * Special:SpecialPages can safely call it for all special pages.
216 public function isCached() {
221 * Can be overridden by subclasses with more complicated permissions
224 * @return bool Should the page be displayed with the restricted-access
227 public function isRestricted() {
228 // DWIM: If anons can do something, then it is not restricted
229 return $this->mRestriction
!= '' && !User
::groupHasPermission( '*', $this->mRestriction
);
233 * Checks if the given user (identified by an object) can execute this
234 * special page (as defined by $mRestriction). Can be overridden by sub-
235 * classes with more complicated permissions schemes.
237 * @param User $user The user to check
238 * @return bool Does the user have permission to view the page?
240 public function userCanExecute( User
$user ) {
241 return $user->isAllowed( $this->mRestriction
);
245 * Output an error message telling the user what access level they have to have
246 * @throws PermissionsError
248 function displayRestrictionError() {
249 throw new PermissionsError( $this->mRestriction
);
253 * Checks if userCanExecute, and if not throws a PermissionsError
257 * @throws PermissionsError
259 public function checkPermissions() {
260 if ( !$this->userCanExecute( $this->getUser() ) ) {
261 $this->displayRestrictionError();
266 * If the wiki is currently in readonly mode, throws a ReadOnlyError
270 * @throws ReadOnlyError
272 public function checkReadOnly() {
273 if ( wfReadOnly() ) {
274 throw new ReadOnlyError
;
279 * If the user is not logged in, throws UserNotLoggedIn error
281 * The user will be redirected to Special:Userlogin with the given message as an error on
285 * @param string $reasonMsg [optional] Message key to be displayed on login page
286 * @param string $titleMsg [optional] Passed on to UserNotLoggedIn constructor
287 * @throws UserNotLoggedIn
289 public function requireLogin(
290 $reasonMsg = 'exception-nologin-text', $titleMsg = 'exception-nologin'
292 if ( $this->getUser()->isAnon() ) {
293 throw new UserNotLoggedIn( $reasonMsg, $titleMsg );
298 * Return an array of subpages beginning with $search that this special page will accept.
300 * For example, if a page supports subpages "foo", "bar" and "baz" (as in Special:PageName/foo,
303 * - `prefixSearchSubpages( "ba" )` should return `array( "bar", "baz" )`
304 * - `prefixSearchSubpages( "f" )` should return `array( "foo" )`
305 * - `prefixSearchSubpages( "z" )` should return `array()`
306 * - `prefixSearchSubpages( "" )` should return `array( foo", "bar", "baz" )`
308 * @param string $search Prefix to search for
309 * @param int $limit Maximum number of results to return (usually 10)
310 * @param int $offset Number of results to skip (usually 0)
311 * @return string[] Matching subpages
313 public function prefixSearchSubpages( $search, $limit, $offset ) {
314 $subpages = $this->getSubpagesForPrefixSearch();
319 return self
::prefixSearchArray( $search, $limit, $subpages, $offset );
323 * Return an array of subpages that this special page will accept for prefix
324 * searches. If this method requires a query you might instead want to implement
325 * prefixSearchSubpages() directly so you can support $limit and $offset. This
326 * method is better for static-ish lists of things.
328 * @return string[] subpages to search from
330 protected function getSubpagesForPrefixSearch() {
335 * Perform a regular substring search for prefixSearchSubpages
336 * @param string $search Prefix to search for
337 * @param int $limit Maximum number of results to return (usually 10)
338 * @param int $offset Number of results to skip (usually 0)
339 * @return string[] Matching subpages
341 protected function prefixSearchString( $search, $limit, $offset ) {
342 $title = Title
::newFromText( $search );
343 if ( !$title ||
!$title->canExist() ) {
344 // No prefix suggestion in special and media namespace
348 $searchEngine = SearchEngine
::create();
349 $searchEngine->setLimitOffset( $limit, $offset );
350 $searchEngine->setNamespaces( [] );
351 $result = $searchEngine->defaultPrefixSearch( $search );
352 return array_map( function( Title
$t ) {
353 return $t->getPrefixedText();
358 * Helper function for implementations of prefixSearchSubpages() that
359 * filter the values in memory (as opposed to making a query).
362 * @param string $search
364 * @param array $subpages
368 protected static function prefixSearchArray( $search, $limit, array $subpages, $offset ) {
369 $escaped = preg_quote( $search, '/' );
370 return array_slice( preg_grep( "/^$escaped/i",
371 array_slice( $subpages, $offset ) ), 0, $limit );
375 * Sets headers - this should be called from the execute() method of all derived classes!
377 function setHeaders() {
378 $out = $this->getOutput();
379 $out->setArticleRelated( false );
380 $out->setRobotPolicy( $this->getRobotPolicy() );
381 $out->setPageTitle( $this->getDescription() );
382 if ( $this->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
383 $out->addModuleStyles( [
384 'mediawiki.ui.input',
385 'mediawiki.ui.radio',
386 'mediawiki.ui.checkbox',
396 * @param string|null $subPage
398 final public function run( $subPage ) {
400 * Gets called before @see SpecialPage::execute.
401 * Return false to prevent calling execute() (since 1.27+).
405 * @param SpecialPage $this
406 * @param string|null $subPage
408 if ( !Hooks
::run( 'SpecialPageBeforeExecute', [ $this, $subPage ] ) ) {
412 if ( $this->beforeExecute( $subPage ) === false ) {
415 $this->execute( $subPage );
416 $this->afterExecute( $subPage );
419 * Gets called after @see SpecialPage::execute.
423 * @param SpecialPage $this
424 * @param string|null $subPage
426 Hooks
::run( 'SpecialPageAfterExecute', [ $this, $subPage ] );
430 * Gets called before @see SpecialPage::execute.
431 * Return false to prevent calling execute() (since 1.27+).
435 * @param string|null $subPage
438 protected function beforeExecute( $subPage ) {
443 * Gets called after @see SpecialPage::execute.
447 * @param string|null $subPage
449 protected function afterExecute( $subPage ) {
454 * Default execute method
455 * Checks user permissions
457 * This must be overridden by subclasses; it will be made abstract in a future version
459 * @param string|null $subPage
461 public function execute( $subPage ) {
463 $this->checkPermissions();
464 $this->outputHeader();
468 * Outputs a summary message on top of special pages
469 * Per default the message key is the canonical name of the special page
470 * May be overridden, i.e. by extensions to stick with the naming conventions
471 * for message keys: 'extensionname-xxx'
473 * @param string $summaryMessageKey Message key of the summary
475 function outputHeader( $summaryMessageKey = '' ) {
478 if ( $summaryMessageKey == '' ) {
479 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
481 $msg = $summaryMessageKey;
483 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
484 $this->getOutput()->wrapWikiMsg(
485 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
490 * Returns the name that goes in the \<h1\> in the special page itself, and
491 * also the name that will be listed in Special:Specialpages
493 * Derived classes can override this, but usually it is easier to keep the
498 function getDescription() {
499 return $this->msg( strtolower( $this->mName
) )->text();
503 * Get a self-referential title object
505 * @param string|bool $subpage
507 * @deprecated since 1.23, use SpecialPage::getPageTitle
509 function getTitle( $subpage = false ) {
510 return $this->getPageTitle( $subpage );
514 * Get a self-referential title object
516 * @param string|bool $subpage
520 function getPageTitle( $subpage = false ) {
521 return self
::getTitleFor( $this->mName
, $subpage );
525 * Sets the context this SpecialPage is executed in
527 * @param IContextSource $context
530 public function setContext( $context ) {
531 $this->mContext
= $context;
535 * Gets the context this SpecialPage is executed in
537 * @return IContextSource|RequestContext
540 public function getContext() {
541 if ( $this->mContext
instanceof IContextSource
) {
542 return $this->mContext
;
544 wfDebug( __METHOD__
. " called and \$mContext is null. " .
545 "Return RequestContext::getMain(); for sanity\n" );
547 return RequestContext
::getMain();
552 * Get the WebRequest being used for this instance
557 public function getRequest() {
558 return $this->getContext()->getRequest();
562 * Get the OutputPage being used for this instance
567 public function getOutput() {
568 return $this->getContext()->getOutput();
572 * Shortcut to get the User executing this instance
577 public function getUser() {
578 return $this->getContext()->getUser();
582 * Shortcut to get the skin being used for this instance
587 public function getSkin() {
588 return $this->getContext()->getSkin();
592 * Shortcut to get user's language
597 public function getLanguage() {
598 return $this->getContext()->getLanguage();
602 * Shortcut to get main config object
606 public function getConfig() {
607 return $this->getContext()->getConfig();
611 * Return the full title, including $par
616 public function getFullTitle() {
617 return $this->getContext()->getTitle();
621 * Return the robot policy. Derived classes that override this can change
622 * the robot policy set by setHeaders() from the default 'noindex,nofollow'.
627 protected function getRobotPolicy() {
628 return 'noindex,nofollow';
632 * Wrapper around wfMessage that sets the current context.
637 public function msg( /* $args */ ) {
638 $message = call_user_func_array(
639 [ $this->getContext(), 'msg' ],
642 // RequestContext passes context to wfMessage, and the language is set from
643 // the context, but setting the language for Message class removes the
644 // interface message status, which breaks for example usernameless gender
645 // invocations. Restore the flag when not including special page in content.
646 if ( $this->including() ) {
647 $message->setInterfaceMessageFlag( false );
654 * Adds RSS/atom links
656 * @param array $params
658 protected function addFeedLinks( $params ) {
659 $feedTemplate = wfScript( 'api' );
661 foreach ( $this->getConfig()->get( 'FeedClasses' ) as $format => $class ) {
662 $theseParams = $params +
[ 'feedformat' => $format ];
663 $url = wfAppendQuery( $feedTemplate, $theseParams );
664 $this->getOutput()->addFeedLink( $format, $url );
669 * Adds help link with an icon via page indicators.
670 * Link target can be overridden by a local message containing a wikilink:
671 * the message key is: lowercase special page name + '-helppage'.
672 * @param string $to Target MediaWiki.org page title or encoded URL.
673 * @param bool $overrideBaseUrl Whether $url is a full URL, to avoid MW.o.
676 public function addHelpLink( $to, $overrideBaseUrl = false ) {
678 $msg = $this->msg( $wgContLang->lc( $this->getName() ) . '-helppage' );
680 if ( !$msg->isDisabled() ) {
681 $helpUrl = Skin
::makeUrl( $msg->plain() );
682 $this->getOutput()->addHelpLink( $helpUrl, true );
684 $this->getOutput()->addHelpLink( $to, $overrideBaseUrl );
689 * Get the group that the special page belongs in on Special:SpecialPage
690 * Use this method, instead of getGroupName to allow customization
691 * of the group name from the wiki side
693 * @return string Group of this special page
696 public function getFinalGroupName() {
697 $name = $this->getName();
699 // Allow overbidding the group from the wiki side
700 $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
701 if ( !$msg->isBlank() ) {
702 $group = $msg->text();
704 // Than use the group from this object
705 $group = $this->getGroupName();
712 * Indicates whether this special page may perform database writes
717 public function doesWrites() {
722 * Under which header this special page is listed in Special:SpecialPages
723 * See messages 'specialpages-group-*' for valid names
724 * This method defaults to group 'other'
729 protected function getGroupName() {
734 * Call wfTransactionalTimeLimit() if this request was POSTed
737 protected function useTransactionalTimeLimit() {
738 if ( $this->getRequest()->wasPosted() ) {
739 wfTransactionalTimeLimit();