SpecialPage.php 26 KB

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