SpecialPage.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. <?php
  2. /**
  3. * Parent class for all special pages.
  4. *
  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.
  9. *
  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.
  14. *
  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
  19. *
  20. * @file
  21. * @ingroup SpecialPage
  22. */
  23. use MediaWiki\Auth\AuthManager;
  24. use MediaWiki\Linker\LinkRenderer;
  25. use MediaWiki\MediaWikiServices;
  26. /**
  27. * Parent class for all special pages.
  28. *
  29. * Includes some static functions for handling the special page list deprecated
  30. * in favor of SpecialPageFactory.
  31. *
  32. * @ingroup SpecialPage
  33. */
  34. class SpecialPage implements MessageLocalizer {
  35. // The canonical name of this special page
  36. // Also used for the default <h1> heading, @see getDescription()
  37. protected $mName;
  38. // The local name of this special page
  39. private $mLocalName;
  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;
  43. // Listed in Special:Specialpages?
  44. private $mListed;
  45. // Whether or not this special page is being included from an article
  46. protected $mIncluding;
  47. // Whether the special page can be included in an article
  48. protected $mIncludable;
  49. /**
  50. * Current request context
  51. * @var IContextSource
  52. */
  53. protected $mContext;
  54. /**
  55. * @var \MediaWiki\Linker\LinkRenderer|null
  56. */
  57. private $linkRenderer;
  58. /**
  59. * Get a localised Title object for a specified special page name
  60. * If you don't need a full Title object, consider using TitleValue through
  61. * getTitleValueFor() below.
  62. *
  63. * @since 1.9
  64. * @since 1.21 $fragment parameter added
  65. *
  66. * @param string $name
  67. * @param string|bool $subpage Subpage string, or false to not use a subpage
  68. * @param string $fragment The link fragment (after the "#")
  69. * @return Title
  70. * @throws MWException
  71. */
  72. public static function getTitleFor( $name, $subpage = false, $fragment = '' ) {
  73. return Title::newFromTitleValue(
  74. self::getTitleValueFor( $name, $subpage, $fragment )
  75. );
  76. }
  77. /**
  78. * Get a localised TitleValue object for a specified special page name
  79. *
  80. * @since 1.28
  81. * @param string $name
  82. * @param string|bool $subpage Subpage string, or false to not use a subpage
  83. * @param string $fragment The link fragment (after the "#")
  84. * @return TitleValue
  85. */
  86. public static function getTitleValueFor( $name, $subpage = false, $fragment = '' ) {
  87. $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
  88. return new TitleValue( NS_SPECIAL, $name, $fragment );
  89. }
  90. /**
  91. * Get a localised Title object for a page name with a possibly unvalidated subpage
  92. *
  93. * @param string $name
  94. * @param string|bool $subpage Subpage string, or false to not use a subpage
  95. * @return Title|null Title object or null if the page doesn't exist
  96. */
  97. public static function getSafeTitleFor( $name, $subpage = false ) {
  98. $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
  99. if ( $name ) {
  100. return Title::makeTitleSafe( NS_SPECIAL, $name );
  101. } else {
  102. return null;
  103. }
  104. }
  105. /**
  106. * Default constructor for special pages
  107. * Derivative classes should call this from their constructor
  108. * Note that if the user does not have the required level, an error message will
  109. * be displayed by the default execute() method, without the global function ever
  110. * being called.
  111. *
  112. * If you override execute(), you can recover the default behavior with userCanExecute()
  113. * and displayRestrictionError()
  114. *
  115. * @param string $name Name of the special page, as seen in links and URLs
  116. * @param string $restriction User right required, e.g. "block" or "delete"
  117. * @param bool $listed Whether the page is listed in Special:Specialpages
  118. * @param callable|bool $function Unused
  119. * @param string $file Unused
  120. * @param bool $includable Whether the page can be included in normal pages
  121. */
  122. public function __construct(
  123. $name = '', $restriction = '', $listed = true,
  124. $function = false, $file = '', $includable = false
  125. ) {
  126. $this->mName = $name;
  127. $this->mRestriction = $restriction;
  128. $this->mListed = $listed;
  129. $this->mIncludable = $includable;
  130. }
  131. /**
  132. * Get the name of this Special Page.
  133. * @return string
  134. */
  135. function getName() {
  136. return $this->mName;
  137. }
  138. /**
  139. * Get the permission that a user must have to execute this page
  140. * @return string
  141. */
  142. function getRestriction() {
  143. return $this->mRestriction;
  144. }
  145. // @todo FIXME: Decide which syntax to use for this, and stick to it
  146. /**
  147. * Whether this special page is listed in Special:SpecialPages
  148. * @since 1.3 (r3583)
  149. * @return bool
  150. */
  151. function isListed() {
  152. return $this->mListed;
  153. }
  154. /**
  155. * Set whether this page is listed in Special:Specialpages, at run-time
  156. * @since 1.3
  157. * @param bool $listed
  158. * @return bool
  159. */
  160. function setListed( $listed ) {
  161. return wfSetVar( $this->mListed, $listed );
  162. }
  163. /**
  164. * Get or set whether this special page is listed in Special:SpecialPages
  165. * @since 1.6
  166. * @param bool $x
  167. * @return bool
  168. */
  169. function listed( $x = null ) {
  170. return wfSetVar( $this->mListed, $x );
  171. }
  172. /**
  173. * Whether it's allowed to transclude the special page via {{Special:Foo/params}}
  174. * @return bool
  175. */
  176. public function isIncludable() {
  177. return $this->mIncludable;
  178. }
  179. /**
  180. * How long to cache page when it is being included.
  181. *
  182. * @note If cache time is not 0, then the current user becomes an anon
  183. * if you want to do any per-user customizations, than this method
  184. * must be overriden to return 0.
  185. * @since 1.26
  186. * @return int Time in seconds, 0 to disable caching altogether,
  187. * false to use the parent page's cache settings
  188. */
  189. public function maxIncludeCacheTime() {
  190. return $this->getConfig()->get( 'MiserMode' ) ? $this->getCacheTTL() : 0;
  191. }
  192. /**
  193. * @return int Seconds that this page can be cached
  194. */
  195. protected function getCacheTTL() {
  196. return 60 * 60;
  197. }
  198. /**
  199. * Whether the special page is being evaluated via transclusion
  200. * @param bool $x
  201. * @return bool
  202. */
  203. function including( $x = null ) {
  204. return wfSetVar( $this->mIncluding, $x );
  205. }
  206. /**
  207. * Get the localised name of the special page
  208. * @return string
  209. */
  210. function getLocalName() {
  211. if ( !isset( $this->mLocalName ) ) {
  212. $this->mLocalName = SpecialPageFactory::getLocalNameFor( $this->mName );
  213. }
  214. return $this->mLocalName;
  215. }
  216. /**
  217. * Is this page expensive (for some definition of expensive)?
  218. * Expensive pages are disabled or cached in miser mode. Originally used
  219. * (and still overridden) by QueryPage and subclasses, moved here so that
  220. * Special:SpecialPages can safely call it for all special pages.
  221. *
  222. * @return bool
  223. */
  224. public function isExpensive() {
  225. return false;
  226. }
  227. /**
  228. * Is this page cached?
  229. * Expensive pages are cached or disabled in miser mode.
  230. * Used by QueryPage and subclasses, moved here so that
  231. * Special:SpecialPages can safely call it for all special pages.
  232. *
  233. * @return bool
  234. * @since 1.21
  235. */
  236. public function isCached() {
  237. return false;
  238. }
  239. /**
  240. * Can be overridden by subclasses with more complicated permissions
  241. * schemes.
  242. *
  243. * @return bool Should the page be displayed with the restricted-access
  244. * pages?
  245. */
  246. public function isRestricted() {
  247. // DWIM: If anons can do something, then it is not restricted
  248. return $this->mRestriction != '' && !User::groupHasPermission( '*', $this->mRestriction );
  249. }
  250. /**
  251. * Checks if the given user (identified by an object) can execute this
  252. * special page (as defined by $mRestriction). Can be overridden by sub-
  253. * classes with more complicated permissions schemes.
  254. *
  255. * @param User $user The user to check
  256. * @return bool Does the user have permission to view the page?
  257. */
  258. public function userCanExecute( User $user ) {
  259. return $user->isAllowed( $this->mRestriction );
  260. }
  261. /**
  262. * Output an error message telling the user what access level they have to have
  263. * @throws PermissionsError
  264. */
  265. function displayRestrictionError() {
  266. throw new PermissionsError( $this->mRestriction );
  267. }
  268. /**
  269. * Checks if userCanExecute, and if not throws a PermissionsError
  270. *
  271. * @since 1.19
  272. * @return void
  273. * @throws PermissionsError
  274. */
  275. public function checkPermissions() {
  276. if ( !$this->userCanExecute( $this->getUser() ) ) {
  277. $this->displayRestrictionError();
  278. }
  279. }
  280. /**
  281. * If the wiki is currently in readonly mode, throws a ReadOnlyError
  282. *
  283. * @since 1.19
  284. * @return void
  285. * @throws ReadOnlyError
  286. */
  287. public function checkReadOnly() {
  288. if ( wfReadOnly() ) {
  289. throw new ReadOnlyError;
  290. }
  291. }
  292. /**
  293. * If the user is not logged in, throws UserNotLoggedIn error
  294. *
  295. * The user will be redirected to Special:Userlogin with the given message as an error on
  296. * the form.
  297. *
  298. * @since 1.23
  299. * @param string $reasonMsg [optional] Message key to be displayed on login page
  300. * @param string $titleMsg [optional] Passed on to UserNotLoggedIn constructor
  301. * @throws UserNotLoggedIn
  302. */
  303. public function requireLogin(
  304. $reasonMsg = 'exception-nologin-text', $titleMsg = 'exception-nologin'
  305. ) {
  306. if ( $this->getUser()->isAnon() ) {
  307. throw new UserNotLoggedIn( $reasonMsg, $titleMsg );
  308. }
  309. }
  310. /**
  311. * Tells if the special page does something security-sensitive and needs extra defense against
  312. * a stolen account (e.g. a reauthentication). What exactly that will mean is decided by the
  313. * authentication framework.
  314. * @return bool|string False or the argument for AuthManager::securitySensitiveOperationStatus().
  315. * Typically a special page needing elevated security would return its name here.
  316. */
  317. protected function getLoginSecurityLevel() {
  318. return false;
  319. }
  320. /**
  321. * Verifies that the user meets the security level, possibly reauthenticating them in the process.
  322. *
  323. * This should be used when the page does something security-sensitive and needs extra defense
  324. * against a stolen account (e.g. a reauthentication). The authentication framework will make
  325. * an extra effort to make sure the user account is not compromised. What that exactly means
  326. * will depend on the system and user settings; e.g. the user might be required to log in again
  327. * unless their last login happened recently, or they might be given a second-factor challenge.
  328. *
  329. * Calling this method will result in one if these actions:
  330. * - return true: all good.
  331. * - return false and set a redirect: caller should abort; the redirect will take the user
  332. * to the login page for reauthentication, and back.
  333. * - throw an exception if there is no way for the user to meet the requirements without using
  334. * a different access method (e.g. this functionality is only available from a specific IP).
  335. *
  336. * Note that this does not in any way check that the user is authorized to use this special page
  337. * (use checkPermissions() for that).
  338. *
  339. * @param string $level A security level. Can be an arbitrary string, defaults to the page name.
  340. * @return bool False means a redirect to the reauthentication page has been set and processing
  341. * of the special page should be aborted.
  342. * @throws ErrorPageError If the security level cannot be met, even with reauthentication.
  343. */
  344. protected function checkLoginSecurityLevel( $level = null ) {
  345. $level = $level ?: $this->getName();
  346. $securityStatus = AuthManager::singleton()->securitySensitiveOperationStatus( $level );
  347. if ( $securityStatus === AuthManager::SEC_OK ) {
  348. return true;
  349. } elseif ( $securityStatus === AuthManager::SEC_REAUTH ) {
  350. $request = $this->getRequest();
  351. $title = self::getTitleFor( 'Userlogin' );
  352. $query = [
  353. 'returnto' => $this->getFullTitle()->getPrefixedDBkey(),
  354. 'returntoquery' => wfArrayToCgi( array_diff_key( $request->getQueryValues(),
  355. [ 'title' => true ] ) ),
  356. 'force' => $level,
  357. ];
  358. $url = $title->getFullURL( $query, false, PROTO_HTTPS );
  359. $this->getOutput()->redirect( $url );
  360. return false;
  361. }
  362. $titleMessage = wfMessage( 'specialpage-securitylevel-not-allowed-title' );
  363. $errorMessage = wfMessage( 'specialpage-securitylevel-not-allowed' );
  364. throw new ErrorPageError( $titleMessage, $errorMessage );
  365. }
  366. /**
  367. * Return an array of subpages beginning with $search that this special page will accept.
  368. *
  369. * For example, if a page supports subpages "foo", "bar" and "baz" (as in Special:PageName/foo,
  370. * etc.):
  371. *
  372. * - `prefixSearchSubpages( "ba" )` should return `array( "bar", "baz" )`
  373. * - `prefixSearchSubpages( "f" )` should return `array( "foo" )`
  374. * - `prefixSearchSubpages( "z" )` should return `array()`
  375. * - `prefixSearchSubpages( "" )` should return `array( foo", "bar", "baz" )`
  376. *
  377. * @param string $search Prefix to search for
  378. * @param int $limit Maximum number of results to return (usually 10)
  379. * @param int $offset Number of results to skip (usually 0)
  380. * @return string[] Matching subpages
  381. */
  382. public function prefixSearchSubpages( $search, $limit, $offset ) {
  383. $subpages = $this->getSubpagesForPrefixSearch();
  384. if ( !$subpages ) {
  385. return [];
  386. }
  387. return self::prefixSearchArray( $search, $limit, $subpages, $offset );
  388. }
  389. /**
  390. * Return an array of subpages that this special page will accept for prefix
  391. * searches. If this method requires a query you might instead want to implement
  392. * prefixSearchSubpages() directly so you can support $limit and $offset. This
  393. * method is better for static-ish lists of things.
  394. *
  395. * @return string[] subpages to search from
  396. */
  397. protected function getSubpagesForPrefixSearch() {
  398. return [];
  399. }
  400. /**
  401. * Perform a regular substring search for prefixSearchSubpages
  402. * @param string $search Prefix to search for
  403. * @param int $limit Maximum number of results to return (usually 10)
  404. * @param int $offset Number of results to skip (usually 0)
  405. * @return string[] Matching subpages
  406. */
  407. protected function prefixSearchString( $search, $limit, $offset ) {
  408. $title = Title::newFromText( $search );
  409. if ( !$title || !$title->canExist() ) {
  410. // No prefix suggestion in special and media namespace
  411. return [];
  412. }
  413. $searchEngine = MediaWikiServices::getInstance()->newSearchEngine();
  414. $searchEngine->setLimitOffset( $limit, $offset );
  415. $searchEngine->setNamespaces( [] );
  416. $result = $searchEngine->defaultPrefixSearch( $search );
  417. return array_map( function ( Title $t ) {
  418. return $t->getPrefixedText();
  419. }, $result );
  420. }
  421. /**
  422. * Helper function for implementations of prefixSearchSubpages() that
  423. * filter the values in memory (as opposed to making a query).
  424. *
  425. * @since 1.24
  426. * @param string $search
  427. * @param int $limit
  428. * @param array $subpages
  429. * @param int $offset
  430. * @return string[]
  431. */
  432. protected static function prefixSearchArray( $search, $limit, array $subpages, $offset ) {
  433. $escaped = preg_quote( $search, '/' );
  434. return array_slice( preg_grep( "/^$escaped/i",
  435. array_slice( $subpages, $offset ) ), 0, $limit );
  436. }
  437. /**
  438. * Sets headers - this should be called from the execute() method of all derived classes!
  439. */
  440. function setHeaders() {
  441. $out = $this->getOutput();
  442. $out->setArticleRelated( false );
  443. $out->setRobotPolicy( $this->getRobotPolicy() );
  444. $out->setPageTitle( $this->getDescription() );
  445. if ( $this->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
  446. $out->addModuleStyles( [
  447. 'mediawiki.ui.input',
  448. 'mediawiki.ui.radio',
  449. 'mediawiki.ui.checkbox',
  450. ] );
  451. }
  452. }
  453. /**
  454. * Entry point.
  455. *
  456. * @since 1.20
  457. *
  458. * @param string|null $subPage
  459. */
  460. final public function run( $subPage ) {
  461. /**
  462. * Gets called before @see SpecialPage::execute.
  463. * Return false to prevent calling execute() (since 1.27+).
  464. *
  465. * @since 1.20
  466. *
  467. * @param SpecialPage $this
  468. * @param string|null $subPage
  469. */
  470. if ( !Hooks::run( 'SpecialPageBeforeExecute', [ $this, $subPage ] ) ) {
  471. return;
  472. }
  473. if ( $this->beforeExecute( $subPage ) === false ) {
  474. return;
  475. }
  476. $this->execute( $subPage );
  477. $this->afterExecute( $subPage );
  478. /**
  479. * Gets called after @see SpecialPage::execute.
  480. *
  481. * @since 1.20
  482. *
  483. * @param SpecialPage $this
  484. * @param string|null $subPage
  485. */
  486. Hooks::run( 'SpecialPageAfterExecute', [ $this, $subPage ] );
  487. }
  488. /**
  489. * Gets called before @see SpecialPage::execute.
  490. * Return false to prevent calling execute() (since 1.27+).
  491. *
  492. * @since 1.20
  493. *
  494. * @param string|null $subPage
  495. * @return bool|void
  496. */
  497. protected function beforeExecute( $subPage ) {
  498. // No-op
  499. }
  500. /**
  501. * Gets called after @see SpecialPage::execute.
  502. *
  503. * @since 1.20
  504. *
  505. * @param string|null $subPage
  506. */
  507. protected function afterExecute( $subPage ) {
  508. // No-op
  509. }
  510. /**
  511. * Default execute method
  512. * Checks user permissions
  513. *
  514. * This must be overridden by subclasses; it will be made abstract in a future version
  515. *
  516. * @param string|null $subPage
  517. */
  518. public function execute( $subPage ) {
  519. $this->setHeaders();
  520. $this->checkPermissions();
  521. $this->checkLoginSecurityLevel( $this->getLoginSecurityLevel() );
  522. $this->outputHeader();
  523. }
  524. /**
  525. * Outputs a summary message on top of special pages
  526. * Per default the message key is the canonical name of the special page
  527. * May be overridden, i.e. by extensions to stick with the naming conventions
  528. * for message keys: 'extensionname-xxx'
  529. *
  530. * @param string $summaryMessageKey Message key of the summary
  531. */
  532. function outputHeader( $summaryMessageKey = '' ) {
  533. global $wgContLang;
  534. if ( $summaryMessageKey == '' ) {
  535. $msg = $wgContLang->lc( $this->getName() ) . '-summary';
  536. } else {
  537. $msg = $summaryMessageKey;
  538. }
  539. if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
  540. $this->getOutput()->wrapWikiMsg(
  541. "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
  542. }
  543. }
  544. /**
  545. * Returns the name that goes in the \<h1\> in the special page itself, and
  546. * also the name that will be listed in Special:Specialpages
  547. *
  548. * Derived classes can override this, but usually it is easier to keep the
  549. * default behavior.
  550. *
  551. * @return string
  552. */
  553. function getDescription() {
  554. return $this->msg( strtolower( $this->mName ) )->text();
  555. }
  556. /**
  557. * Get a self-referential title object
  558. *
  559. * @param string|bool $subpage
  560. * @return Title
  561. * @deprecated since 1.23, use SpecialPage::getPageTitle
  562. */
  563. function getTitle( $subpage = false ) {
  564. wfDeprecated( __METHOD__, '1.23' );
  565. return $this->getPageTitle( $subpage );
  566. }
  567. /**
  568. * Get a self-referential title object
  569. *
  570. * @param string|bool $subpage
  571. * @return Title
  572. * @since 1.23
  573. */
  574. function getPageTitle( $subpage = false ) {
  575. return self::getTitleFor( $this->mName, $subpage );
  576. }
  577. /**
  578. * Sets the context this SpecialPage is executed in
  579. *
  580. * @param IContextSource $context
  581. * @since 1.18
  582. */
  583. public function setContext( $context ) {
  584. $this->mContext = $context;
  585. }
  586. /**
  587. * Gets the context this SpecialPage is executed in
  588. *
  589. * @return IContextSource|RequestContext
  590. * @since 1.18
  591. */
  592. public function getContext() {
  593. if ( $this->mContext instanceof IContextSource ) {
  594. return $this->mContext;
  595. } else {
  596. wfDebug( __METHOD__ . " called and \$mContext is null. " .
  597. "Return RequestContext::getMain(); for sanity\n" );
  598. return RequestContext::getMain();
  599. }
  600. }
  601. /**
  602. * Get the WebRequest being used for this instance
  603. *
  604. * @return WebRequest
  605. * @since 1.18
  606. */
  607. public function getRequest() {
  608. return $this->getContext()->getRequest();
  609. }
  610. /**
  611. * Get the OutputPage being used for this instance
  612. *
  613. * @return OutputPage
  614. * @since 1.18
  615. */
  616. public function getOutput() {
  617. return $this->getContext()->getOutput();
  618. }
  619. /**
  620. * Shortcut to get the User executing this instance
  621. *
  622. * @return User
  623. * @since 1.18
  624. */
  625. public function getUser() {
  626. return $this->getContext()->getUser();
  627. }
  628. /**
  629. * Shortcut to get the skin being used for this instance
  630. *
  631. * @return Skin
  632. * @since 1.18
  633. */
  634. public function getSkin() {
  635. return $this->getContext()->getSkin();
  636. }
  637. /**
  638. * Shortcut to get user's language
  639. *
  640. * @return Language
  641. * @since 1.19
  642. */
  643. public function getLanguage() {
  644. return $this->getContext()->getLanguage();
  645. }
  646. /**
  647. * Shortcut to get main config object
  648. * @return Config
  649. * @since 1.24
  650. */
  651. public function getConfig() {
  652. return $this->getContext()->getConfig();
  653. }
  654. /**
  655. * Return the full title, including $par
  656. *
  657. * @return Title
  658. * @since 1.18
  659. */
  660. public function getFullTitle() {
  661. return $this->getContext()->getTitle();
  662. }
  663. /**
  664. * Return the robot policy. Derived classes that override this can change
  665. * the robot policy set by setHeaders() from the default 'noindex,nofollow'.
  666. *
  667. * @return string
  668. * @since 1.23
  669. */
  670. protected function getRobotPolicy() {
  671. return 'noindex,nofollow';
  672. }
  673. /**
  674. * Wrapper around wfMessage that sets the current context.
  675. *
  676. * @since 1.16
  677. * @return Message
  678. * @see wfMessage
  679. */
  680. public function msg( $key /* $args */ ) {
  681. $message = call_user_func_array(
  682. [ $this->getContext(), 'msg' ],
  683. func_get_args()
  684. );
  685. // RequestContext passes context to wfMessage, and the language is set from
  686. // the context, but setting the language for Message class removes the
  687. // interface message status, which breaks for example usernameless gender
  688. // invocations. Restore the flag when not including special page in content.
  689. if ( $this->including() ) {
  690. $message->setInterfaceMessageFlag( false );
  691. }
  692. return $message;
  693. }
  694. /**
  695. * Adds RSS/atom links
  696. *
  697. * @param array $params
  698. */
  699. protected function addFeedLinks( $params ) {
  700. $feedTemplate = wfScript( 'api' );
  701. foreach ( $this->getConfig()->get( 'FeedClasses' ) as $format => $class ) {
  702. $theseParams = $params + [ 'feedformat' => $format ];
  703. $url = wfAppendQuery( $feedTemplate, $theseParams );
  704. $this->getOutput()->addFeedLink( $format, $url );
  705. }
  706. }
  707. /**
  708. * Adds help link with an icon via page indicators.
  709. * Link target can be overridden by a local message containing a wikilink:
  710. * the message key is: lowercase special page name + '-helppage'.
  711. * @param string $to Target MediaWiki.org page title or encoded URL.
  712. * @param bool $overrideBaseUrl Whether $url is a full URL, to avoid MW.o.
  713. * @since 1.25
  714. */
  715. public function addHelpLink( $to, $overrideBaseUrl = false ) {
  716. if ( $this->including() ) {
  717. return;
  718. }
  719. global $wgContLang;
  720. $msg = $this->msg( $wgContLang->lc( $this->getName() ) . '-helppage' );
  721. if ( !$msg->isDisabled() ) {
  722. $helpUrl = Skin::makeUrl( $msg->plain() );
  723. $this->getOutput()->addHelpLink( $helpUrl, true );
  724. } else {
  725. $this->getOutput()->addHelpLink( $to, $overrideBaseUrl );
  726. }
  727. }
  728. /**
  729. * Get the group that the special page belongs in on Special:SpecialPage
  730. * Use this method, instead of getGroupName to allow customization
  731. * of the group name from the wiki side
  732. *
  733. * @return string Group of this special page
  734. * @since 1.21
  735. */
  736. public function getFinalGroupName() {
  737. $name = $this->getName();
  738. // Allow overriding the group from the wiki side
  739. $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
  740. if ( !$msg->isBlank() ) {
  741. $group = $msg->text();
  742. } else {
  743. // Than use the group from this object
  744. $group = $this->getGroupName();
  745. }
  746. return $group;
  747. }
  748. /**
  749. * Indicates whether this special page may perform database writes
  750. *
  751. * @return bool
  752. * @since 1.27
  753. */
  754. public function doesWrites() {
  755. return false;
  756. }
  757. /**
  758. * Under which header this special page is listed in Special:SpecialPages
  759. * See messages 'specialpages-group-*' for valid names
  760. * This method defaults to group 'other'
  761. *
  762. * @return string
  763. * @since 1.21
  764. */
  765. protected function getGroupName() {
  766. return 'other';
  767. }
  768. /**
  769. * Call wfTransactionalTimeLimit() if this request was POSTed
  770. * @since 1.26
  771. */
  772. protected function useTransactionalTimeLimit() {
  773. if ( $this->getRequest()->wasPosted() ) {
  774. wfTransactionalTimeLimit();
  775. }
  776. }
  777. /**
  778. * @since 1.28
  779. * @return \MediaWiki\Linker\LinkRenderer
  780. */
  781. public function getLinkRenderer() {
  782. if ( $this->linkRenderer ) {
  783. return $this->linkRenderer;
  784. } else {
  785. return MediaWikiServices::getInstance()->getLinkRenderer();
  786. }
  787. }
  788. /**
  789. * @since 1.28
  790. * @param \MediaWiki\Linker\LinkRenderer $linkRenderer
  791. */
  792. public function setLinkRenderer( LinkRenderer $linkRenderer ) {
  793. $this->linkRenderer = $linkRenderer;
  794. }
  795. }