Action.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <?php
  2. /**
  3. * Base classes for actions done on 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
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  18. *
  19. * @file
  20. */
  21. use MediaWiki\MediaWikiServices;
  22. /**
  23. * @defgroup Actions Actions
  24. */
  25. /**
  26. * Actions are things which can be done to pages (edit, delete, rollback, etc). They
  27. * are distinct from Special Pages because an action must apply to exactly one page.
  28. *
  29. * To add an action in an extension, create a subclass of Action, and add the key to
  30. * $wgActions.
  31. *
  32. * Actions generally fall into two groups: the show-a-form-then-do-something-with-the-input
  33. * format (protect, delete, move, etc), and the just-do-something format (watch, rollback,
  34. * patrol, etc). The FormAction and FormlessAction classes represent these two groups.
  35. */
  36. abstract class Action implements MessageLocalizer {
  37. /**
  38. * Page on which we're performing the action
  39. * @since 1.17
  40. * @var WikiPage|Article|ImagePage|CategoryPage|Page $page
  41. */
  42. protected $page;
  43. /**
  44. * IContextSource if specified; otherwise we'll use the Context from the Page
  45. * @since 1.17
  46. * @var IContextSource $context
  47. */
  48. protected $context;
  49. /**
  50. * The fields used to create the HTMLForm
  51. * @since 1.17
  52. * @var array $fields
  53. */
  54. protected $fields;
  55. /**
  56. * Get the Action subclass which should be used to handle this action, false if
  57. * the action is disabled, or null if it's not recognised
  58. * @param string $action
  59. * @param array $overrides
  60. * @return bool|null|string|callable|Action
  61. */
  62. final private static function getClass( $action, array $overrides ) {
  63. global $wgActions;
  64. $action = strtolower( $action );
  65. if ( !isset( $wgActions[$action] ) ) {
  66. return null;
  67. }
  68. if ( $wgActions[$action] === false ) {
  69. return false;
  70. } elseif ( $wgActions[$action] === true && isset( $overrides[$action] ) ) {
  71. return $overrides[$action];
  72. } elseif ( $wgActions[$action] === true ) {
  73. return ucfirst( $action ) . 'Action';
  74. } else {
  75. return $wgActions[$action];
  76. }
  77. }
  78. /**
  79. * Get an appropriate Action subclass for the given action
  80. * @since 1.17
  81. * @param string $action
  82. * @param Page $page
  83. * @param IContextSource|null $context
  84. * @return Action|bool|null False if the action is disabled, null
  85. * if it is not recognised
  86. */
  87. final public static function factory( $action, Page $page, IContextSource $context = null ) {
  88. $classOrCallable = self::getClass( $action, $page->getActionOverrides() );
  89. if ( is_string( $classOrCallable ) ) {
  90. if ( !class_exists( $classOrCallable ) ) {
  91. return false;
  92. }
  93. return new $classOrCallable( $page, $context );
  94. }
  95. if ( is_callable( $classOrCallable ) ) {
  96. return $classOrCallable( $page, $context );
  97. }
  98. return $classOrCallable;
  99. }
  100. /**
  101. * Get the action that will be executed, not necessarily the one passed
  102. * passed through the "action" request parameter. Actions disabled in
  103. * $wgActions will be replaced by "nosuchaction".
  104. *
  105. * @since 1.19
  106. * @param IContextSource $context
  107. * @return string Action name
  108. */
  109. final public static function getActionName( IContextSource $context ) {
  110. global $wgActions;
  111. $request = $context->getRequest();
  112. $actionName = $request->getVal( 'action', 'view' );
  113. // Check for disabled actions
  114. if ( isset( $wgActions[$actionName] ) && $wgActions[$actionName] === false ) {
  115. $actionName = 'nosuchaction';
  116. }
  117. // Workaround for T22966: inability of IE to provide an action dependent
  118. // on which submit button is clicked.
  119. if ( $actionName === 'historysubmit' ) {
  120. if ( $request->getBool( 'revisiondelete' ) ) {
  121. $actionName = 'revisiondelete';
  122. } elseif ( $request->getBool( 'editchangetags' ) ) {
  123. $actionName = 'editchangetags';
  124. } else {
  125. $actionName = 'view';
  126. }
  127. } elseif ( $actionName === 'editredlink' ) {
  128. $actionName = 'edit';
  129. }
  130. // Trying to get a WikiPage for NS_SPECIAL etc. will result
  131. // in WikiPage::factory throwing "Invalid or virtual namespace -1 given."
  132. // For SpecialPages et al, default to action=view.
  133. if ( !$context->canUseWikiPage() ) {
  134. return 'view';
  135. }
  136. $action = self::factory( $actionName, $context->getWikiPage(), $context );
  137. if ( $action instanceof Action ) {
  138. return $action->getName();
  139. }
  140. return 'nosuchaction';
  141. }
  142. /**
  143. * Check if a given action is recognised, even if it's disabled
  144. * @since 1.17
  145. *
  146. * @param string $name Name of an action
  147. * @return bool
  148. */
  149. final public static function exists( $name ) {
  150. return self::getClass( $name, [] ) !== null;
  151. }
  152. /**
  153. * Get the IContextSource in use here
  154. * @since 1.17
  155. * @return IContextSource
  156. */
  157. final public function getContext() {
  158. if ( $this->context instanceof IContextSource ) {
  159. return $this->context;
  160. } elseif ( $this->page instanceof Article ) {
  161. // NOTE: $this->page can be a WikiPage, which does not have a context.
  162. wfDebug( __METHOD__ . ": no context known, falling back to Article's context.\n" );
  163. return $this->page->getContext();
  164. }
  165. wfWarn( __METHOD__ . ': no context known, falling back to RequestContext::getMain().' );
  166. return RequestContext::getMain();
  167. }
  168. /**
  169. * Get the WebRequest being used for this instance
  170. * @since 1.17
  171. *
  172. * @return WebRequest
  173. */
  174. final public function getRequest() {
  175. return $this->getContext()->getRequest();
  176. }
  177. /**
  178. * Get the OutputPage being used for this instance
  179. * @since 1.17
  180. *
  181. * @return OutputPage
  182. */
  183. final public function getOutput() {
  184. return $this->getContext()->getOutput();
  185. }
  186. /**
  187. * Shortcut to get the User being used for this instance
  188. * @since 1.17
  189. *
  190. * @return User
  191. */
  192. final public function getUser() {
  193. return $this->getContext()->getUser();
  194. }
  195. /**
  196. * Shortcut to get the Skin being used for this instance
  197. * @since 1.17
  198. *
  199. * @return Skin
  200. */
  201. final public function getSkin() {
  202. return $this->getContext()->getSkin();
  203. }
  204. /**
  205. * Shortcut to get the user Language being used for this instance
  206. *
  207. * @return Language
  208. */
  209. final public function getLanguage() {
  210. return $this->getContext()->getLanguage();
  211. }
  212. /**
  213. * Shortcut to get the Title object from the page
  214. * @since 1.17
  215. *
  216. * @return Title
  217. */
  218. final public function getTitle() {
  219. return $this->page->getTitle();
  220. }
  221. /**
  222. * Get a Message object with context set
  223. * Parameters are the same as wfMessage()
  224. *
  225. * @param string|string[]|MessageSpecifier $key
  226. * @param mixed ...$params
  227. * @return Message
  228. */
  229. final public function msg( $key, ...$params ) {
  230. return $this->getContext()->msg( $key, ...$params );
  231. }
  232. /**
  233. * Only public since 1.21
  234. *
  235. * @param Page $page
  236. * @param IContextSource|null $context
  237. */
  238. public function __construct( Page $page, IContextSource $context = null ) {
  239. if ( $context === null ) {
  240. wfWarn( __METHOD__ . ' called without providing a Context object.' );
  241. // NOTE: We could try to initialize $context using $page->getContext(),
  242. // if $page is an Article. That however seems to not work seamlessly.
  243. }
  244. $this->page = $page;
  245. $this->context = $context;
  246. }
  247. /**
  248. * Return the name of the action this object responds to
  249. * @since 1.17
  250. *
  251. * @return string Lowercase name
  252. */
  253. abstract public function getName();
  254. /**
  255. * Get the permission required to perform this action. Often, but not always,
  256. * the same as the action name
  257. * @since 1.17
  258. *
  259. * @return string|null
  260. */
  261. public function getRestriction() {
  262. return null;
  263. }
  264. /**
  265. * Checks if the given user (identified by an object) can perform this action. Can be
  266. * overridden by sub-classes with more complicated permissions schemes. Failures here
  267. * must throw subclasses of ErrorPageError
  268. * @since 1.17
  269. *
  270. * @param User $user The user to check, or null to use the context user
  271. * @throws UserBlockedError|ReadOnlyError|PermissionsError
  272. */
  273. protected function checkCanExecute( User $user ) {
  274. $right = $this->getRestriction();
  275. if ( $right !== null ) {
  276. $errors = $this->getTitle()->getUserPermissionsErrors( $right, $user );
  277. if ( count( $errors ) ) {
  278. throw new PermissionsError( $right, $errors );
  279. }
  280. }
  281. // If the action requires an unblock, explicitly check the user's block.
  282. if ( $this->requiresUnblock() && $user->isBlockedFrom( $this->getTitle() ) ) {
  283. $block = $user->getBlock();
  284. if ( $block ) {
  285. throw new UserBlockedError( $block );
  286. }
  287. throw new PermissionsError( $this->getName(), [ 'badaccess-group0' ] );
  288. }
  289. // This should be checked at the end so that the user won't think the
  290. // error is only temporary when he also don't have the rights to execute
  291. // this action
  292. if ( $this->requiresWrite() && wfReadOnly() ) {
  293. throw new ReadOnlyError();
  294. }
  295. }
  296. /**
  297. * Whether this action requires the wiki not to be locked
  298. * @since 1.17
  299. *
  300. * @return bool
  301. */
  302. public function requiresWrite() {
  303. return true;
  304. }
  305. /**
  306. * Whether this action can still be executed by a blocked user
  307. * @since 1.17
  308. *
  309. * @return bool
  310. */
  311. public function requiresUnblock() {
  312. return true;
  313. }
  314. /**
  315. * Set output headers for noindexing etc. This function will not be called through
  316. * the execute() entry point, so only put UI-related stuff in here.
  317. * @since 1.17
  318. */
  319. protected function setHeaders() {
  320. $out = $this->getOutput();
  321. $out->setRobotPolicy( 'noindex,nofollow' );
  322. $out->setPageTitle( $this->getPageTitle() );
  323. $out->setSubtitle( $this->getDescription() );
  324. $out->setArticleRelated( true );
  325. }
  326. /**
  327. * Returns the name that goes in the \<h1\> page title
  328. *
  329. * @return string
  330. */
  331. protected function getPageTitle() {
  332. return $this->getTitle()->getPrefixedText();
  333. }
  334. /**
  335. * Returns the description that goes below the \<h1\> tag
  336. * @since 1.17
  337. *
  338. * @return string HTML
  339. */
  340. protected function getDescription() {
  341. return $this->msg( strtolower( $this->getName() ) )->escaped();
  342. }
  343. /**
  344. * Adds help link with an icon via page indicators.
  345. * Link target can be overridden by a local message containing a wikilink:
  346. * the message key is: lowercase action name + '-helppage'.
  347. * @param string $to Target MediaWiki.org page title or encoded URL.
  348. * @param bool $overrideBaseUrl Whether $url is a full URL, to avoid MW.o.
  349. * @since 1.25
  350. */
  351. public function addHelpLink( $to, $overrideBaseUrl = false ) {
  352. $msg = wfMessage( MediaWikiServices::getInstance()->getContentLanguage()->lc(
  353. self::getActionName( $this->getContext() )
  354. ) . '-helppage' );
  355. if ( !$msg->isDisabled() ) {
  356. $helpUrl = Skin::makeUrl( $msg->plain() );
  357. $this->getOutput()->addHelpLink( $helpUrl, true );
  358. } else {
  359. $this->getOutput()->addHelpLink( $to, $overrideBaseUrl );
  360. }
  361. }
  362. /**
  363. * The main action entry point. Do all output for display and send it to the context
  364. * output. Do not use globals $wgOut, $wgRequest, etc, in implementations; use
  365. * $this->getOutput(), etc.
  366. * @since 1.17
  367. *
  368. * @throws ErrorPageError
  369. */
  370. abstract public function show();
  371. /**
  372. * Call wfTransactionalTimeLimit() if this request was POSTed
  373. * @since 1.26
  374. */
  375. protected function useTransactionalTimeLimit() {
  376. if ( $this->getRequest()->wasPosted() ) {
  377. wfTransactionalTimeLimit();
  378. }
  379. }
  380. /**
  381. * Indicates whether this action may perform database writes
  382. * @return bool
  383. * @since 1.27
  384. */
  385. public function doesWrites() {
  386. return false;
  387. }
  388. }