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 * @todo Turn this into a real ContextSource
31 * @ingroup SpecialPage
34 // The canonical name of this special page
35 // Also used for the default <h1> heading, @see getDescription()
38 // The local name of this special page
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?
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;
55 * Current request context
61 * Get a localised Title object for a specified special page name
64 * @param string|bool $subpage Subpage string, or false to not use a subpage
65 * @param string $fragment The link fragment (after the "#")
69 public static function getTitleFor( $name, $subpage = false, $fragment = '' ) {
70 $name = SpecialPageFactory
::getLocalNameFor( $name, $subpage );
72 return Title
::makeTitle( NS_SPECIAL
, $name, $fragment );
76 * Get a localised Title object for a page name with a possibly unvalidated subpage
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 );
85 return Title
::makeTitleSafe( NS_SPECIAL
, $name );
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
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.
127 * Get the permission that a user must have to execute this page
130 function getRestriction() {
131 return $this->mRestriction
;
135 * Get the file which will be included by SpecialPage::execute() if your extension is
136 * still stuck in the past and hasn't overridden the execute() method. No modern code
137 * should want or need to know this.
139 * @deprecated since 1.18
142 wfDeprecated( __METHOD__
, '1.18' );
147 // @todo FIXME: Decide which syntax to use for this, and stick to it
149 * Whether this special page is listed in Special:SpecialPages
150 * @since r3583 (v1.3)
153 function isListed() {
154 return $this->mListed
;
158 * Set whether this page is listed in Special:Specialpages, at run-time
160 * @param bool $listed
163 function setListed( $listed ) {
164 return wfSetVar( $this->mListed
, $listed );
168 * Get or set whether this special page is listed in Special:SpecialPages
173 function listed( $x = null ) {
174 return wfSetVar( $this->mListed
, $x );
178 * Whether it's allowed to transclude the special page via {{Special:Foo/params}}
181 public function isIncludable() {
182 return $this->mIncludable
;
186 * Whether the special page is being evaluated via transclusion
190 function including( $x = null ) {
191 return wfSetVar( $this->mIncluding
, $x );
195 * Get the localised name of the special page
198 function getLocalName() {
199 if ( !isset( $this->mLocalName
) ) {
200 $this->mLocalName
= SpecialPageFactory
::getLocalNameFor( $this->mName
);
203 return $this->mLocalName
;
207 * Is this page expensive (for some definition of expensive)?
208 * Expensive pages are disabled or cached in miser mode. Originally used
209 * (and still overridden) by QueryPage and subclasses, moved here so that
210 * Special:SpecialPages can safely call it for all special pages.
214 public function isExpensive() {
219 * Is this page cached?
220 * Expensive pages are cached or disabled in miser mode.
221 * Used by QueryPage and subclasses, moved here so that
222 * Special:SpecialPages can safely call it for all special pages.
227 public function isCached() {
232 * Can be overridden by subclasses with more complicated permissions
235 * @return bool Should the page be displayed with the restricted-access
238 public function isRestricted() {
239 // DWIM: If anons can do something, then it is not restricted
240 return $this->mRestriction
!= '' && !User
::groupHasPermission( '*', $this->mRestriction
);
244 * Checks if the given user (identified by an object) can execute this
245 * special page (as defined by $mRestriction). Can be overridden by sub-
246 * classes with more complicated permissions schemes.
248 * @param User $user The user to check
249 * @return bool Does the user have permission to view the page?
251 public function userCanExecute( User
$user ) {
252 return $user->isAllowed( $this->mRestriction
);
256 * Output an error message telling the user what access level they have to have
257 * @throws PermissionsError
259 function displayRestrictionError() {
260 throw new PermissionsError( $this->mRestriction
);
264 * Checks if userCanExecute, and if not throws a PermissionsError
268 * @throws PermissionsError
270 public function checkPermissions() {
271 if ( !$this->userCanExecute( $this->getUser() ) ) {
272 $this->displayRestrictionError();
277 * If the wiki is currently in readonly mode, throws a ReadOnlyError
281 * @throws ReadOnlyError
283 public function checkReadOnly() {
284 if ( wfReadOnly() ) {
285 throw new ReadOnlyError
;
290 * If the user is not logged in, throws UserNotLoggedIn error.
292 * Default error message includes a link to Special:Userlogin with properly set 'returnto' query
296 * @param string|Message $reasonMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
297 * will be used as message keys. If a string is given, the message will also receive a
298 * formatted login link (generated using the 'loginreqlink' message) as first parameter. If a
299 * Message is given, it will be passed on verbatim.
300 * @param string|Message $titleMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
301 * will be used as message keys.
302 * @throws UserNotLoggedIn
304 public function requireLogin( $reasonMsg = null, $titleMsg = null ) {
305 if ( $this->getUser()->isAnon() ) {
306 // Use default messages if not given or explicit null passed
308 $reasonMsg = 'exception-nologin-text-manual';
311 $titleMsg = 'exception-nologin';
314 // Convert to Messages with current context
315 if ( is_string( $reasonMsg ) ) {
316 $loginreqlink = Linker
::linkKnown(
317 SpecialPage
::getTitleFor( 'Userlogin' ),
318 $this->msg( 'loginreqlink' )->escaped(),
320 array( 'returnto' => $this->getPageTitle()->getPrefixedText() )
322 $reasonMsg = $this->msg( $reasonMsg )->rawParams( $loginreqlink );
324 if ( is_string( $titleMsg ) ) {
325 $titleMsg = $this->msg( $titleMsg );
328 throw new UserNotLoggedIn( $reasonMsg, $titleMsg );
333 * Sets headers - this should be called from the execute() method of all derived classes!
335 function setHeaders() {
336 $out = $this->getOutput();
337 $out->setArticleRelated( false );
338 $out->setRobotPolicy( $this->getRobotPolicy() );
339 $out->setPageTitle( $this->getDescription() );
347 * @param string|null $subPage
349 final public function run( $subPage ) {
351 * Gets called before @see SpecialPage::execute.
355 * @param SpecialPage $this
356 * @param string|null $subPage
358 wfRunHooks( 'SpecialPageBeforeExecute', array( $this, $subPage ) );
360 $this->beforeExecute( $subPage );
361 $this->execute( $subPage );
362 $this->afterExecute( $subPage );
365 * Gets called after @see SpecialPage::execute.
369 * @param SpecialPage $this
370 * @param string|null $subPage
372 wfRunHooks( 'SpecialPageAfterExecute', array( $this, $subPage ) );
376 * Gets called before @see SpecialPage::execute.
380 * @param string|null $subPage
382 protected function beforeExecute( $subPage ) {
387 * Gets called after @see SpecialPage::execute.
391 * @param string|null $subPage
393 protected function afterExecute( $subPage ) {
398 * Default execute method
399 * Checks user permissions
401 * This must be overridden by subclasses; it will be made abstract in a future version
403 * @param string|null $subPage
405 public function execute( $subPage ) {
407 $this->checkPermissions();
408 $this->outputHeader();
412 * Outputs a summary message on top of special pages
413 * Per default the message key is the canonical name of the special page
414 * May be overridden, i.e. by extensions to stick with the naming conventions
415 * for message keys: 'extensionname-xxx'
417 * @param string $summaryMessageKey Message key of the summary
419 function outputHeader( $summaryMessageKey = '' ) {
422 if ( $summaryMessageKey == '' ) {
423 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
425 $msg = $summaryMessageKey;
427 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
428 $this->getOutput()->wrapWikiMsg(
429 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
434 * Returns the name that goes in the \<h1\> in the special page itself, and
435 * also the name that will be listed in Special:Specialpages
437 * Derived classes can override this, but usually it is easier to keep the
442 function getDescription() {
443 return $this->msg( strtolower( $this->mName
) )->text();
447 * Get a self-referential title object
449 * @param string|bool $subpage
451 * @deprecated since 1.23, use SpecialPage::getPageTitle
453 function getTitle( $subpage = false ) {
454 return $this->getPageTitle( $subpage );
458 * Get a self-referential title object
460 * @param string|bool $subpage
464 function getPageTitle( $subpage = false ) {
465 return self
::getTitleFor( $this->mName
, $subpage );
469 * Sets the context this SpecialPage is executed in
471 * @param IContextSource $context
474 public function setContext( $context ) {
475 $this->mContext
= $context;
479 * Gets the context this SpecialPage is executed in
481 * @return IContextSource|RequestContext
484 public function getContext() {
485 if ( $this->mContext
instanceof IContextSource
) {
486 return $this->mContext
;
488 wfDebug( __METHOD__
. " called and \$mContext is null. " .
489 "Return RequestContext::getMain(); for sanity\n" );
491 return RequestContext
::getMain();
496 * Get the WebRequest being used for this instance
501 public function getRequest() {
502 return $this->getContext()->getRequest();
506 * Get the OutputPage being used for this instance
511 public function getOutput() {
512 return $this->getContext()->getOutput();
516 * Shortcut to get the User executing this instance
521 public function getUser() {
522 return $this->getContext()->getUser();
526 * Shortcut to get the skin being used for this instance
531 public function getSkin() {
532 return $this->getContext()->getSkin();
536 * Shortcut to get user's language
538 * @deprecated since 1.19 Use getLanguage instead
542 public function getLang() {
543 wfDeprecated( __METHOD__
, '1.19' );
545 return $this->getLanguage();
549 * Shortcut to get user's language
554 public function getLanguage() {
555 return $this->getContext()->getLanguage();
559 * Shortcut to get main config object
563 public function getConfig() {
564 return $this->getContext()->getConfig();
568 * Return the full title, including $par
573 public function getFullTitle() {
574 return $this->getContext()->getTitle();
578 * Return the robot policy. Derived classes that override this can change
579 * the robot policy set by setHeaders() from the default 'noindex,nofollow'.
584 protected function getRobotPolicy() {
585 return 'noindex,nofollow';
589 * Wrapper around wfMessage that sets the current context.
594 public function msg( /* $args */ ) {
595 $message = call_user_func_array(
596 array( $this->getContext(), 'msg' ),
599 // RequestContext passes context to wfMessage, and the language is set from
600 // the context, but setting the language for Message class removes the
601 // interface message status, which breaks for example usernameless gender
602 // invocations. Restore the flag when not including special page in content.
603 if ( $this->including() ) {
604 $message->setInterfaceMessageFlag( false );
611 * Adds RSS/atom links
613 * @param array $params
615 protected function addFeedLinks( $params ) {
616 global $wgFeedClasses;
618 $feedTemplate = wfScript( 'api' );
620 foreach ( $wgFeedClasses as $format => $class ) {
621 $theseParams = $params +
array( 'feedformat' => $format );
622 $url = wfAppendQuery( $feedTemplate, $theseParams );
623 $this->getOutput()->addFeedLink( $format, $url );
628 * Get the group that the special page belongs in on Special:SpecialPage
629 * Use this method, instead of getGroupName to allow customization
630 * of the group name from the wiki side
632 * @return string Group of this special page
635 public function getFinalGroupName() {
636 global $wgSpecialPageGroups;
637 $name = $this->getName();
639 // Allow overbidding the group from the wiki side
640 $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
641 if ( !$msg->isBlank() ) {
642 $group = $msg->text();
644 // Than use the group from this object
645 $group = $this->getGroupName();
647 // Group '-' is used as default to have the chance to determine,
648 // if the special pages overrides this method,
649 // if not overridden, $wgSpecialPageGroups is checked for b/c
650 if ( $group === '-' && isset( $wgSpecialPageGroups[$name] ) ) {
651 $group = $wgSpecialPageGroups[$name];
655 // never give '-' back, change to 'other'
656 if ( $group === '-' ) {
664 * Under which header this special page is listed in Special:SpecialPages
665 * See messages 'specialpages-group-*' for valid names
666 * This method defaults to group 'other'
671 protected function getGroupName() {
672 // '-' used here to determine, if this group is overridden or has a hardcoded 'other'
673 // Needed for b/c in getFinalGroupName