2 use MediaWiki\MediaWikiServices
;
5 * Parent class for all special pages.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
23 * @ingroup SpecialPage
27 * Parent class for all special pages.
29 * Includes some static functions for handling the special page list deprecated
30 * in favor of SpecialPageFactory.
32 * @ingroup SpecialPage
35 // The canonical name of this special page
36 // Also used for the default <h1> heading, @see getDescription()
39 // The local name of this special page
42 // Minimum user level required to access this page, or "" for anyone.
43 // Also used to categorise the pages in Special:Specialpages
44 protected $mRestriction;
46 // Listed in Special:Specialpages?
49 // Whether or not this special page is being included from an article
50 protected $mIncluding;
52 // Whether the special page can be included in an article
53 protected $mIncludable;
56 * Current request context
62 * Get a localised Title object for a specified special page name
65 * @since 1.21 $fragment parameter added
68 * @param string|bool $subpage Subpage string, or false to not use a subpage
69 * @param string $fragment The link fragment (after the "#")
73 public static function getTitleFor( $name, $subpage = false, $fragment = '' ) {
74 $name = SpecialPageFactory
::getLocalNameFor( $name, $subpage );
76 return Title
::makeTitle( NS_SPECIAL
, $name, $fragment );
80 * Get a localised Title object for a page name with a possibly unvalidated subpage
83 * @param string|bool $subpage Subpage string, or false to not use a subpage
84 * @return Title|null Title object or null if the page doesn't exist
86 public static function getSafeTitleFor( $name, $subpage = false ) {
87 $name = SpecialPageFactory
::getLocalNameFor( $name, $subpage );
89 return Title
::makeTitleSafe( NS_SPECIAL
, $name );
96 * Default constructor for special pages
97 * Derivative classes should call this from their constructor
98 * Note that if the user does not have the required level, an error message will
99 * be displayed by the default execute() method, without the global function ever
102 * If you override execute(), you can recover the default behavior with userCanExecute()
103 * and displayRestrictionError()
105 * @param string $name Name of the special page, as seen in links and URLs
106 * @param string $restriction User right required, e.g. "block" or "delete"
107 * @param bool $listed Whether the page is listed in Special:Specialpages
108 * @param callable|bool $function Unused
109 * @param string $file Unused
110 * @param bool $includable Whether the page can be included in normal pages
112 public function __construct(
113 $name = '', $restriction = '', $listed = true,
114 $function = false, $file = '', $includable = false
116 $this->mName
= $name;
117 $this->mRestriction
= $restriction;
118 $this->mListed
= $listed;
119 $this->mIncludable
= $includable;
123 * Get the name of this Special Page.
131 * Get the permission that a user must have to execute this page
134 function getRestriction() {
135 return $this->mRestriction
;
138 // @todo FIXME: Decide which syntax to use for this, and stick to it
140 * Whether this special page is listed in Special:SpecialPages
144 function isListed() {
145 return $this->mListed
;
149 * Set whether this page is listed in Special:Specialpages, at run-time
151 * @param bool $listed
154 function setListed( $listed ) {
155 return wfSetVar( $this->mListed
, $listed );
159 * Get or set whether this special page is listed in Special:SpecialPages
164 function listed( $x = null ) {
165 return wfSetVar( $this->mListed
, $x );
169 * Whether it's allowed to transclude the special page via {{Special:Foo/params}}
172 public function isIncludable() {
173 return $this->mIncludable
;
177 * Whether the special page is being evaluated via transclusion
181 function including( $x = null ) {
182 return wfSetVar( $this->mIncluding
, $x );
186 * Get the localised name of the special page
189 function getLocalName() {
190 if ( !isset( $this->mLocalName
) ) {
191 $this->mLocalName
= SpecialPageFactory
::getLocalNameFor( $this->mName
);
194 return $this->mLocalName
;
198 * Is this page expensive (for some definition of expensive)?
199 * Expensive pages are disabled or cached in miser mode. Originally used
200 * (and still overridden) by QueryPage and subclasses, moved here so that
201 * Special:SpecialPages can safely call it for all special pages.
205 public function isExpensive() {
210 * Is this page cached?
211 * Expensive pages are cached or disabled in miser mode.
212 * Used by QueryPage and subclasses, moved here so that
213 * Special:SpecialPages can safely call it for all special pages.
218 public function isCached() {
223 * Can be overridden by subclasses with more complicated permissions
226 * @return bool Should the page be displayed with the restricted-access
229 public function isRestricted() {
230 // DWIM: If anons can do something, then it is not restricted
231 return $this->mRestriction
!= '' && !User
::groupHasPermission( '*', $this->mRestriction
);
235 * Checks if the given user (identified by an object) can execute this
236 * special page (as defined by $mRestriction). Can be overridden by sub-
237 * classes with more complicated permissions schemes.
239 * @param User $user The user to check
240 * @return bool Does the user have permission to view the page?
242 public function userCanExecute( User
$user ) {
243 return $user->isAllowed( $this->mRestriction
);
247 * Output an error message telling the user what access level they have to have
248 * @throws PermissionsError
250 function displayRestrictionError() {
251 throw new PermissionsError( $this->mRestriction
);
255 * Checks if userCanExecute, and if not throws a PermissionsError
259 * @throws PermissionsError
261 public function checkPermissions() {
262 if ( !$this->userCanExecute( $this->getUser() ) ) {
263 $this->displayRestrictionError();
268 * If the wiki is currently in readonly mode, throws a ReadOnlyError
272 * @throws ReadOnlyError
274 public function checkReadOnly() {
275 if ( wfReadOnly() ) {
276 throw new ReadOnlyError
;
281 * If the user is not logged in, throws UserNotLoggedIn error
283 * The user will be redirected to Special:Userlogin with the given message as an error on
287 * @param string $reasonMsg [optional] Message key to be displayed on login page
288 * @param string $titleMsg [optional] Passed on to UserNotLoggedIn constructor
289 * @throws UserNotLoggedIn
291 public function requireLogin(
292 $reasonMsg = 'exception-nologin-text', $titleMsg = 'exception-nologin'
294 if ( $this->getUser()->isAnon() ) {
295 throw new UserNotLoggedIn( $reasonMsg, $titleMsg );
300 * Return an array of subpages beginning with $search that this special page will accept.
302 * For example, if a page supports subpages "foo", "bar" and "baz" (as in Special:PageName/foo,
305 * - `prefixSearchSubpages( "ba" )` should return `array( "bar", "baz" )`
306 * - `prefixSearchSubpages( "f" )` should return `array( "foo" )`
307 * - `prefixSearchSubpages( "z" )` should return `array()`
308 * - `prefixSearchSubpages( "" )` should return `array( foo", "bar", "baz" )`
310 * @param string $search Prefix to search for
311 * @param int $limit Maximum number of results to return (usually 10)
312 * @param int $offset Number of results to skip (usually 0)
313 * @return string[] Matching subpages
315 public function prefixSearchSubpages( $search, $limit, $offset ) {
316 $subpages = $this->getSubpagesForPrefixSearch();
321 return self
::prefixSearchArray( $search, $limit, $subpages, $offset );
325 * Return an array of subpages that this special page will accept for prefix
326 * searches. If this method requires a query you might instead want to implement
327 * prefixSearchSubpages() directly so you can support $limit and $offset. This
328 * method is better for static-ish lists of things.
330 * @return string[] subpages to search from
332 protected function getSubpagesForPrefixSearch() {
337 * Perform a regular substring search for prefixSearchSubpages
338 * @param string $search Prefix to search for
339 * @param int $limit Maximum number of results to return (usually 10)
340 * @param int $offset Number of results to skip (usually 0)
341 * @return string[] Matching subpages
343 protected function prefixSearchString( $search, $limit, $offset ) {
344 $title = Title
::newFromText( $search );
345 if ( !$title ||
!$title->canExist() ) {
346 // No prefix suggestion in special and media namespace
350 $searchEngine = MediaWikiServices
::getInstance()->newSearchEngine();
351 $searchEngine->setLimitOffset( $limit, $offset );
352 $searchEngine->setNamespaces( [] );
353 $result = $searchEngine->defaultPrefixSearch( $search );
354 return array_map( function( Title
$t ) {
355 return $t->getPrefixedText();
360 * Helper function for implementations of prefixSearchSubpages() that
361 * filter the values in memory (as opposed to making a query).
364 * @param string $search
366 * @param array $subpages
370 protected static function prefixSearchArray( $search, $limit, array $subpages, $offset ) {
371 $escaped = preg_quote( $search, '/' );
372 return array_slice( preg_grep( "/^$escaped/i",
373 array_slice( $subpages, $offset ) ), 0, $limit );
377 * Sets headers - this should be called from the execute() method of all derived classes!
379 function setHeaders() {
380 $out = $this->getOutput();
381 $out->setArticleRelated( false );
382 $out->setRobotPolicy( $this->getRobotPolicy() );
383 $out->setPageTitle( $this->getDescription() );
384 if ( $this->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
385 $out->addModuleStyles( [
386 'mediawiki.ui.input',
387 'mediawiki.ui.radio',
388 'mediawiki.ui.checkbox',
398 * @param string|null $subPage
400 final public function run( $subPage ) {
402 * Gets called before @see SpecialPage::execute.
403 * Return false to prevent calling execute() (since 1.27+).
407 * @param SpecialPage $this
408 * @param string|null $subPage
410 if ( !Hooks
::run( 'SpecialPageBeforeExecute', [ $this, $subPage ] ) ) {
414 if ( $this->beforeExecute( $subPage ) === false ) {
417 $this->execute( $subPage );
418 $this->afterExecute( $subPage );
421 * Gets called after @see SpecialPage::execute.
425 * @param SpecialPage $this
426 * @param string|null $subPage
428 Hooks
::run( 'SpecialPageAfterExecute', [ $this, $subPage ] );
432 * Gets called before @see SpecialPage::execute.
433 * Return false to prevent calling execute() (since 1.27+).
437 * @param string|null $subPage
440 protected function beforeExecute( $subPage ) {
445 * Gets called after @see SpecialPage::execute.
449 * @param string|null $subPage
451 protected function afterExecute( $subPage ) {
456 * Default execute method
457 * Checks user permissions
459 * This must be overridden by subclasses; it will be made abstract in a future version
461 * @param string|null $subPage
463 public function execute( $subPage ) {
465 $this->checkPermissions();
466 $this->outputHeader();
470 * Outputs a summary message on top of special pages
471 * Per default the message key is the canonical name of the special page
472 * May be overridden, i.e. by extensions to stick with the naming conventions
473 * for message keys: 'extensionname-xxx'
475 * @param string $summaryMessageKey Message key of the summary
477 function outputHeader( $summaryMessageKey = '' ) {
480 if ( $summaryMessageKey == '' ) {
481 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
483 $msg = $summaryMessageKey;
485 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
486 $this->getOutput()->wrapWikiMsg(
487 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
492 * Returns the name that goes in the \<h1\> in the special page itself, and
493 * also the name that will be listed in Special:Specialpages
495 * Derived classes can override this, but usually it is easier to keep the
500 function getDescription() {
501 return $this->msg( strtolower( $this->mName
) )->text();
505 * Get a self-referential title object
507 * @param string|bool $subpage
509 * @deprecated since 1.23, use SpecialPage::getPageTitle
511 function getTitle( $subpage = false ) {
512 return $this->getPageTitle( $subpage );
516 * Get a self-referential title object
518 * @param string|bool $subpage
522 function getPageTitle( $subpage = false ) {
523 return self
::getTitleFor( $this->mName
, $subpage );
527 * Sets the context this SpecialPage is executed in
529 * @param IContextSource $context
532 public function setContext( $context ) {
533 $this->mContext
= $context;
537 * Gets the context this SpecialPage is executed in
539 * @return IContextSource|RequestContext
542 public function getContext() {
543 if ( $this->mContext
instanceof IContextSource
) {
544 return $this->mContext
;
546 wfDebug( __METHOD__
. " called and \$mContext is null. " .
547 "Return RequestContext::getMain(); for sanity\n" );
549 return RequestContext
::getMain();
554 * Get the WebRequest being used for this instance
559 public function getRequest() {
560 return $this->getContext()->getRequest();
564 * Get the OutputPage being used for this instance
569 public function getOutput() {
570 return $this->getContext()->getOutput();
574 * Shortcut to get the User executing this instance
579 public function getUser() {
580 return $this->getContext()->getUser();
584 * Shortcut to get the skin being used for this instance
589 public function getSkin() {
590 return $this->getContext()->getSkin();
594 * Shortcut to get user's language
599 public function getLanguage() {
600 return $this->getContext()->getLanguage();
604 * Shortcut to get main config object
608 public function getConfig() {
609 return $this->getContext()->getConfig();
613 * Return the full title, including $par
618 public function getFullTitle() {
619 return $this->getContext()->getTitle();
623 * Return the robot policy. Derived classes that override this can change
624 * the robot policy set by setHeaders() from the default 'noindex,nofollow'.
629 protected function getRobotPolicy() {
630 return 'noindex,nofollow';
634 * Wrapper around wfMessage that sets the current context.
640 public function msg( /* $args */ ) {
641 $message = call_user_func_array(
642 [ $this->getContext(), 'msg' ],
645 // RequestContext passes context to wfMessage, and the language is set from
646 // the context, but setting the language for Message class removes the
647 // interface message status, which breaks for example usernameless gender
648 // invocations. Restore the flag when not including special page in content.
649 if ( $this->including() ) {
650 $message->setInterfaceMessageFlag( false );
657 * Adds RSS/atom links
659 * @param array $params
661 protected function addFeedLinks( $params ) {
662 $feedTemplate = wfScript( 'api' );
664 foreach ( $this->getConfig()->get( 'FeedClasses' ) as $format => $class ) {
665 $theseParams = $params +
[ 'feedformat' => $format ];
666 $url = wfAppendQuery( $feedTemplate, $theseParams );
667 $this->getOutput()->addFeedLink( $format, $url );
672 * Adds help link with an icon via page indicators.
673 * Link target can be overridden by a local message containing a wikilink:
674 * the message key is: lowercase special page name + '-helppage'.
675 * @param string $to Target MediaWiki.org page title or encoded URL.
676 * @param bool $overrideBaseUrl Whether $url is a full URL, to avoid MW.o.
679 public function addHelpLink( $to, $overrideBaseUrl = false ) {
681 $msg = $this->msg( $wgContLang->lc( $this->getName() ) . '-helppage' );
683 if ( !$msg->isDisabled() ) {
684 $helpUrl = Skin
::makeUrl( $msg->plain() );
685 $this->getOutput()->addHelpLink( $helpUrl, true );
687 $this->getOutput()->addHelpLink( $to, $overrideBaseUrl );
692 * Get the group that the special page belongs in on Special:SpecialPage
693 * Use this method, instead of getGroupName to allow customization
694 * of the group name from the wiki side
696 * @return string Group of this special page
699 public function getFinalGroupName() {
700 $name = $this->getName();
702 // Allow overbidding the group from the wiki side
703 $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
704 if ( !$msg->isBlank() ) {
705 $group = $msg->text();
707 // Than use the group from this object
708 $group = $this->getGroupName();
715 * Indicates whether this special page may perform database writes
720 public function doesWrites() {
725 * Under which header this special page is listed in Special:SpecialPages
726 * See messages 'specialpages-group-*' for valid names
727 * This method defaults to group 'other'
732 protected function getGroupName() {
737 * Call wfTransactionalTimeLimit() if this request was POSTed
740 protected function useTransactionalTimeLimit() {
741 if ( $this->getRequest()->wasPosted() ) {
742 wfTransactionalTimeLimit();