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 * @param string|bool $subpage Subpage string, or false to not use a subpage
66 * @param string $fragment The link fragment (after the "#")
70 public static function getTitleFor( $name, $subpage = false, $fragment = '' ) {
71 $name = SpecialPageFactory
::getLocalNameFor( $name, $subpage );
73 return Title
::makeTitle( NS_SPECIAL
, $name, $fragment );
77 * Get a localised Title object for a page name with a possibly unvalidated subpage
80 * @param string|bool $subpage Subpage string, or false to not use a subpage
81 * @return Title|null Title object or null if the page doesn't exist
83 public static function getSafeTitleFor( $name, $subpage = false ) {
84 $name = SpecialPageFactory
::getLocalNameFor( $name, $subpage );
86 return Title
::makeTitleSafe( NS_SPECIAL
, $name );
93 * Default constructor for special pages
94 * Derivative classes should call this from their constructor
95 * Note that if the user does not have the required level, an error message will
96 * be displayed by the default execute() method, without the global function ever
99 * If you override execute(), you can recover the default behavior with userCanExecute()
100 * and displayRestrictionError()
102 * @param string $name Name of the special page, as seen in links and URLs
103 * @param string $restriction User right required, e.g. "block" or "delete"
104 * @param bool $listed Whether the page is listed in Special:Specialpages
105 * @param callable|bool $function Unused
106 * @param string $file Unused
107 * @param bool $includable Whether the page can be included in normal pages
109 public function __construct(
110 $name = '', $restriction = '', $listed = true,
111 $function = false, $file = '', $includable = false
113 $this->mName
= $name;
114 $this->mRestriction
= $restriction;
115 $this->mListed
= $listed;
116 $this->mIncludable
= $includable;
120 * Get the name of this Special Page.
128 * Get the permission that a user must have to execute this page
131 function getRestriction() {
132 return $this->mRestriction
;
135 // @todo FIXME: Decide which syntax to use for this, and stick to it
137 * Whether this special page is listed in Special:SpecialPages
141 function isListed() {
142 return $this->mListed
;
146 * Set whether this page is listed in Special:Specialpages, at run-time
148 * @param bool $listed
151 function setListed( $listed ) {
152 return wfSetVar( $this->mListed
, $listed );
156 * Get or set whether this special page is listed in Special:SpecialPages
161 function listed( $x = null ) {
162 return wfSetVar( $this->mListed
, $x );
166 * Whether it's allowed to transclude the special page via {{Special:Foo/params}}
169 public function isIncludable() {
170 return $this->mIncludable
;
174 * Whether the special page is being evaluated via transclusion
178 function including( $x = null ) {
179 return wfSetVar( $this->mIncluding
, $x );
183 * Get the localised name of the special page
186 function getLocalName() {
187 if ( !isset( $this->mLocalName
) ) {
188 $this->mLocalName
= SpecialPageFactory
::getLocalNameFor( $this->mName
);
191 return $this->mLocalName
;
195 * Is this page expensive (for some definition of expensive)?
196 * Expensive pages are disabled or cached in miser mode. Originally used
197 * (and still overridden) by QueryPage and subclasses, moved here so that
198 * Special:SpecialPages can safely call it for all special pages.
202 public function isExpensive() {
207 * Is this page cached?
208 * Expensive pages are cached or disabled in miser mode.
209 * Used by QueryPage and subclasses, moved here so that
210 * Special:SpecialPages can safely call it for all special pages.
215 public function isCached() {
220 * Can be overridden by subclasses with more complicated permissions
223 * @return bool Should the page be displayed with the restricted-access
226 public function isRestricted() {
227 // DWIM: If anons can do something, then it is not restricted
228 return $this->mRestriction
!= '' && !User
::groupHasPermission( '*', $this->mRestriction
);
232 * Checks if the given user (identified by an object) can execute this
233 * special page (as defined by $mRestriction). Can be overridden by sub-
234 * classes with more complicated permissions schemes.
236 * @param User $user The user to check
237 * @return bool Does the user have permission to view the page?
239 public function userCanExecute( User
$user ) {
240 return $user->isAllowed( $this->mRestriction
);
244 * Output an error message telling the user what access level they have to have
245 * @throws PermissionsError
247 function displayRestrictionError() {
248 throw new PermissionsError( $this->mRestriction
);
252 * Checks if userCanExecute, and if not throws a PermissionsError
256 * @throws PermissionsError
258 public function checkPermissions() {
259 if ( !$this->userCanExecute( $this->getUser() ) ) {
260 $this->displayRestrictionError();
265 * If the wiki is currently in readonly mode, throws a ReadOnlyError
269 * @throws ReadOnlyError
271 public function checkReadOnly() {
272 if ( wfReadOnly() ) {
273 throw new ReadOnlyError
;
278 * If the user is not logged in, throws UserNotLoggedIn error
280 * The user will be redirected to Special:Userlogin with the given message as an error on
284 * @param string $reasonMsg [optional] Message key to be displayed on login page
285 * @param string $titleMsg [optional] Passed on to UserNotLoggedIn constructor
286 * @throws UserNotLoggedIn
288 public function requireLogin(
289 $reasonMsg = 'exception-nologin-text', $titleMsg = 'exception-nologin'
291 if ( $this->getUser()->isAnon() ) {
292 throw new UserNotLoggedIn( $reasonMsg, $titleMsg );
297 * Return an array of subpages beginning with $search that this special page will accept.
299 * For example, if a page supports subpages "foo", "bar" and "baz" (as in Special:PageName/foo,
302 * - `prefixSearchSubpages( "ba" )` should return `array( "bar", "baz" )`
303 * - `prefixSearchSubpages( "f" )` should return `array( "foo" )`
304 * - `prefixSearchSubpages( "z" )` should return `array()`
305 * - `prefixSearchSubpages( "" )` should return `array( foo", "bar", "baz" )`
307 * @param string $search Prefix to search for
308 * @param int $limit Maximum number of results to return (usually 10)
309 * @param int $offset Number of results to skip (usually 0)
310 * @return string[] Matching subpages
312 public function prefixSearchSubpages( $search, $limit, $offset ) {
313 $subpages = $this->getSubpagesForPrefixSearch();
318 return self
::prefixSearchArray( $search, $limit, $subpages, $offset );
322 * Return an array of subpages that this special page will accept for prefix
323 * searches. If this method requires a query you might instead want to implement
324 * prefixSearchSubpages() directly so you can support $limit and $offset. This
325 * method is better for static-ish lists of things.
327 * @return string[] subpages to search from
329 protected function getSubpagesForPrefixSearch() {
334 * Perform a regular substring search for prefixSearchSubpages
335 * @param string $search Prefix to search for
336 * @param int $limit Maximum number of results to return (usually 10)
337 * @param int $offset Number of results to skip (usually 0)
338 * @return string[] Matching subpages
340 protected function prefixSearchString( $search, $limit, $offset ) {
341 $title = Title
::newFromText( $search );
342 if ( !$title ||
!$title->canExist() ) {
343 // No prefix suggestion in special and media namespace
347 $searchEngine = MediaWikiServices
::getInstance()->newSearchEngine();
348 $searchEngine->setLimitOffset( $limit, $offset );
349 $searchEngine->setNamespaces( [] );
350 $result = $searchEngine->defaultPrefixSearch( $search );
351 return array_map( function( Title
$t ) {
352 return $t->getPrefixedText();
357 * Helper function for implementations of prefixSearchSubpages() that
358 * filter the values in memory (as opposed to making a query).
361 * @param string $search
363 * @param array $subpages
367 protected static function prefixSearchArray( $search, $limit, array $subpages, $offset ) {
368 $escaped = preg_quote( $search, '/' );
369 return array_slice( preg_grep( "/^$escaped/i",
370 array_slice( $subpages, $offset ) ), 0, $limit );
374 * Sets headers - this should be called from the execute() method of all derived classes!
376 function setHeaders() {
377 $out = $this->getOutput();
378 $out->setArticleRelated( false );
379 $out->setRobotPolicy( $this->getRobotPolicy() );
380 $out->setPageTitle( $this->getDescription() );
381 if ( $this->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
382 $out->addModuleStyles( [
383 'mediawiki.ui.input',
384 'mediawiki.ui.radio',
385 'mediawiki.ui.checkbox',
395 * @param string|null $subPage
397 final public function run( $subPage ) {
399 * Gets called before @see SpecialPage::execute.
400 * Return false to prevent calling execute() (since 1.27+).
404 * @param SpecialPage $this
405 * @param string|null $subPage
407 if ( !Hooks
::run( 'SpecialPageBeforeExecute', [ $this, $subPage ] ) ) {
411 if ( $this->beforeExecute( $subPage ) === false ) {
414 $this->execute( $subPage );
415 $this->afterExecute( $subPage );
418 * Gets called after @see SpecialPage::execute.
422 * @param SpecialPage $this
423 * @param string|null $subPage
425 Hooks
::run( 'SpecialPageAfterExecute', [ $this, $subPage ] );
429 * Gets called before @see SpecialPage::execute.
430 * Return false to prevent calling execute() (since 1.27+).
434 * @param string|null $subPage
437 protected function beforeExecute( $subPage ) {
442 * Gets called after @see SpecialPage::execute.
446 * @param string|null $subPage
448 protected function afterExecute( $subPage ) {
453 * Default execute method
454 * Checks user permissions
456 * This must be overridden by subclasses; it will be made abstract in a future version
458 * @param string|null $subPage
460 public function execute( $subPage ) {
462 $this->checkPermissions();
463 $this->outputHeader();
467 * Outputs a summary message on top of special pages
468 * Per default the message key is the canonical name of the special page
469 * May be overridden, i.e. by extensions to stick with the naming conventions
470 * for message keys: 'extensionname-xxx'
472 * @param string $summaryMessageKey Message key of the summary
474 function outputHeader( $summaryMessageKey = '' ) {
477 if ( $summaryMessageKey == '' ) {
478 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
480 $msg = $summaryMessageKey;
482 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
483 $this->getOutput()->wrapWikiMsg(
484 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
489 * Returns the name that goes in the \<h1\> in the special page itself, and
490 * also the name that will be listed in Special:Specialpages
492 * Derived classes can override this, but usually it is easier to keep the
497 function getDescription() {
498 return $this->msg( strtolower( $this->mName
) )->text();
502 * Get a self-referential title object
504 * @param string|bool $subpage
506 * @deprecated since 1.23, use SpecialPage::getPageTitle
508 function getTitle( $subpage = false ) {
509 return $this->getPageTitle( $subpage );
513 * Get a self-referential title object
515 * @param string|bool $subpage
519 function getPageTitle( $subpage = false ) {
520 return self
::getTitleFor( $this->mName
, $subpage );
524 * Sets the context this SpecialPage is executed in
526 * @param IContextSource $context
529 public function setContext( $context ) {
530 $this->mContext
= $context;
534 * Gets the context this SpecialPage is executed in
536 * @return IContextSource|RequestContext
539 public function getContext() {
540 if ( $this->mContext
instanceof IContextSource
) {
541 return $this->mContext
;
543 wfDebug( __METHOD__
. " called and \$mContext is null. " .
544 "Return RequestContext::getMain(); for sanity\n" );
546 return RequestContext
::getMain();
551 * Get the WebRequest being used for this instance
556 public function getRequest() {
557 return $this->getContext()->getRequest();
561 * Get the OutputPage being used for this instance
566 public function getOutput() {
567 return $this->getContext()->getOutput();
571 * Shortcut to get the User executing this instance
576 public function getUser() {
577 return $this->getContext()->getUser();
581 * Shortcut to get the skin being used for this instance
586 public function getSkin() {
587 return $this->getContext()->getSkin();
591 * Shortcut to get user's language
596 public function getLanguage() {
597 return $this->getContext()->getLanguage();
601 * Shortcut to get main config object
605 public function getConfig() {
606 return $this->getContext()->getConfig();
610 * Return the full title, including $par
615 public function getFullTitle() {
616 return $this->getContext()->getTitle();
620 * Return the robot policy. Derived classes that override this can change
621 * the robot policy set by setHeaders() from the default 'noindex,nofollow'.
626 protected function getRobotPolicy() {
627 return 'noindex,nofollow';
631 * Wrapper around wfMessage that sets the current context.
636 public function msg( /* $args */ ) {
637 $message = call_user_func_array(
638 [ $this->getContext(), 'msg' ],
641 // RequestContext passes context to wfMessage, and the language is set from
642 // the context, but setting the language for Message class removes the
643 // interface message status, which breaks for example usernameless gender
644 // invocations. Restore the flag when not including special page in content.
645 if ( $this->including() ) {
646 $message->setInterfaceMessageFlag( false );
653 * Adds RSS/atom links
655 * @param array $params
657 protected function addFeedLinks( $params ) {
658 $feedTemplate = wfScript( 'api' );
660 foreach ( $this->getConfig()->get( 'FeedClasses' ) as $format => $class ) {
661 $theseParams = $params +
[ 'feedformat' => $format ];
662 $url = wfAppendQuery( $feedTemplate, $theseParams );
663 $this->getOutput()->addFeedLink( $format, $url );
668 * Adds help link with an icon via page indicators.
669 * Link target can be overridden by a local message containing a wikilink:
670 * the message key is: lowercase special page name + '-helppage'.
671 * @param string $to Target MediaWiki.org page title or encoded URL.
672 * @param bool $overrideBaseUrl Whether $url is a full URL, to avoid MW.o.
675 public function addHelpLink( $to, $overrideBaseUrl = false ) {
677 $msg = $this->msg( $wgContLang->lc( $this->getName() ) . '-helppage' );
679 if ( !$msg->isDisabled() ) {
680 $helpUrl = Skin
::makeUrl( $msg->plain() );
681 $this->getOutput()->addHelpLink( $helpUrl, true );
683 $this->getOutput()->addHelpLink( $to, $overrideBaseUrl );
688 * Get the group that the special page belongs in on Special:SpecialPage
689 * Use this method, instead of getGroupName to allow customization
690 * of the group name from the wiki side
692 * @return string Group of this special page
695 public function getFinalGroupName() {
696 $name = $this->getName();
698 // Allow overbidding the group from the wiki side
699 $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
700 if ( !$msg->isBlank() ) {
701 $group = $msg->text();
703 // Than use the group from this object
704 $group = $this->getGroupName();
711 * Indicates whether this special page may perform database writes
716 public function doesWrites() {
721 * Under which header this special page is listed in Special:SpecialPages
722 * See messages 'specialpages-group-*' for valid names
723 * This method defaults to group 'other'
728 protected function getGroupName() {
733 * Call wfTransactionalTimeLimit() if this request was POSTed
736 protected function useTransactionalTimeLimit() {
737 if ( $this->getRequest()->wasPosted() ) {
738 wfTransactionalTimeLimit();