MediaWiki  master
Action.php
Go to the documentation of this file.
1 <?php
25 
43 abstract class Action implements MessageLocalizer {
44 
53  protected $page;
54 
59  private $article;
60 
66  protected $context;
67 
73  protected $fields;
74 
85  final public static function factory(
86  string $action,
89  ) {
90  if ( $context === null ) {
92  }
93 
94  return MediaWikiServices::getInstance()
95  ->getActionFactory()
96  ->getAction( $action, $article, $context );
97  }
98 
108  final public static function getActionName( IContextSource $context ) {
109  return MediaWikiServices::getInstance()
110  ->getActionFactory()
111  ->getActionName( $context );
112  }
113 
121  final public static function exists( string $name ): bool {
122  return MediaWikiServices::getInstance()
123  ->getActionFactory()
124  ->actionExists( $name );
125  }
126 
132  final public function getContext() {
133  if ( $this->context instanceof IContextSource ) {
134  return $this->context;
135  }
136  wfDebug( __METHOD__ . ": no context known, falling back to Article's context." );
137  return $this->getArticle()->getContext();
138  }
139 
146  final public function getRequest() {
147  return $this->getContext()->getRequest();
148  }
149 
156  final public function getOutput() {
157  return $this->getContext()->getOutput();
158  }
159 
166  final public function getUser() {
167  return $this->getContext()->getUser();
168  }
169 
176  final public function getSkin() {
177  return $this->getContext()->getSkin();
178  }
179 
185  final public function getLanguage() {
186  return $this->getContext()->getLanguage();
187  }
188 
195  final public function getWikiPage(): WikiPage {
196  return $this->getArticle()->getPage();
197  }
198 
206  public function getArticle() {
207  return $this->article;
208  }
209 
216  final public function getTitle() {
217  return $this->getWikiPage()->getTitle();
218  }
219 
228  final public function msg( $key, ...$params ) {
229  return $this->getContext()->msg( $key, ...$params );
230  }
231 
237  protected function getHookContainer() {
238  return MediaWikiServices::getInstance()->getHookContainer();
239  }
240 
247  protected function getHookRunner() {
248  return new HookRunner( $this->getHookContainer() );
249  }
250 
260  public function __construct(
261  Page $page,
262  IContextSource $context = null
263  ) {
264  if ( $context === null ) {
265  wfWarn( __METHOD__ . ' called without providing a Context object.' );
266  }
267 
268  $this->page = $page;// @todo remove b/c
269  $this->article = self::convertPageToArticle( $page, $context, __METHOD__ );
270  $this->context = $context;
271  }
272 
273  private static function convertPageToArticle(
274  Page $page,
276  string $method
277  ): Article {
278  if ( $page instanceof Article ) {
279  return $page;
280  }
281 
282  if ( !$page instanceof WikiPage ) {
283  throw new LogicException(
284  $method . ' called with unknown Page: ' . get_class( $page )
285  );
286  }
287 
288  wfDeprecated(
289  $method . ' with: ' . get_class( $page ),
290  '1.35'
291  );
292 
294  $page,
296  );
297  }
298 
305  abstract public function getName();
306 
315  public function getRestriction() {
316  return null;
317  }
318 
329  protected function checkCanExecute( User $user ) {
330  $right = $this->getRestriction();
331  $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
332  if ( $right !== null ) {
333  $errors = $permissionManager->getPermissionErrors( $right, $user, $this->getTitle() );
334  if ( count( $errors ) ) {
335  throw new PermissionsError( $right, $errors );
336  }
337  }
338 
339  // If the action requires an unblock, explicitly check the user's block.
340  $checkReplica = !$this->getRequest()->wasPosted();
341  if (
342  $this->requiresUnblock() &&
343  $permissionManager->isBlockedFrom( $user, $this->getTitle(), $checkReplica )
344  ) {
345  $block = $user->getBlock();
346  if ( $block ) {
347  throw new UserBlockedError(
348  $block,
349  $user,
350  $this->getLanguage(),
351  $this->getRequest()->getIP()
352  );
353  }
354 
355  throw new PermissionsError( $this->getName(), [ 'badaccess-group0' ] );
356  }
357 
358  // This should be checked at the end so that the user won't think the
359  // error is only temporary when he also don't have the rights to execute
360  // this action
361  $readOnlyMode = MediaWikiServices::getInstance()->getReadOnlyMode();
362  if ( $this->requiresWrite() && $readOnlyMode->isReadOnly() ) {
363  throw new ReadOnlyError();
364  }
365  }
366 
374  public function requiresWrite() {
375  return true;
376  }
377 
385  public function requiresUnblock() {
386  return true;
387  }
388 
395  protected function setHeaders() {
396  $out = $this->getOutput();
397  $out->setRobotPolicy( 'noindex,nofollow' );
398  $out->setPageTitle( $this->getPageTitle() );
399  $out->setSubtitle( $this->getDescription() );
400  $out->setArticleRelated( true );
401  }
402 
409  protected function getPageTitle() {
410  return $this->getTitle()->getPrefixedText();
411  }
412 
420  protected function getDescription() {
421  return $this->msg( strtolower( $this->getName() ) )->escaped();
422  }
423 
432  public function addHelpLink( $to, $overrideBaseUrl = false ) {
433  $lang = MediaWikiServices::getInstance()->getContentLanguage();
434  $target = $lang->lc( $this->getName() . '-helppage' );
435  $msg = $this->msg( $target );
436 
437  if ( !$msg->isDisabled() ) {
438  $title = Title::newFromText( $msg->plain() );
439  if ( $title instanceof Title ) {
440  $this->getOutput()->addHelpLink( $title->getLocalURL(), true );
441  }
442  } else {
443  $this->getOutput()->addHelpLink( $to, $overrideBaseUrl );
444  }
445  }
446 
455  abstract public function show();
456 
461  protected function useTransactionalTimeLimit() {
462  if ( $this->getRequest()->wasPosted() ) {
464  }
465  }
466 
473  public function doesWrites() {
474  return false;
475  }
476 }
ReadOnlyError
Show an error when the wiki is locked/read-only and the user tries to do something that requires writ...
Definition: ReadOnlyError.php:29
Action\getActionName
static getActionName(IContextSource $context)
Get the action that will be executed, not necessarily the one passed passed through the "action" requ...
Definition: Action.php:108
Page
Interface for type hinting (accepts WikiPage, Article, ImagePage, CategoryPage)
Definition: Page.php:29
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:395
Action\__construct
__construct(Page $page, IContextSource $context=null)
Only public since 1.21.
Definition: Action.php:260
UserBlockedError
Show an error when the user tries to do something whilst blocked.
Definition: UserBlockedError.php:32
Action\getDescription
getDescription()
Returns the description that goes below the <h1> tag.
Definition: Action.php:420
Action\factory
static factory(string $action, Article $article, IContextSource $context=null)
Get an appropriate Action subclass for the given action.
Definition: Action.php:85
Action\setHeaders
setHeaders()
Set output headers for noindexing etc.
Definition: Action.php:395
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:200
Action\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: Action.php:146
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
Action\doesWrites
doesWrites()
Indicates whether this action may perform database writes.
Definition: Action.php:473
CategoryPage
Special handling for category description pages, showing pages, subcategories and file that belong to...
Definition: CategoryPage.php:30
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:60
Action\exists
static exists(string $name)
Check if a given action is recognised, even if it's disabled.
Definition: Action.php:121
Action\getName
getName()
Return the name of the action this object responds to.
ImagePage
Class for viewing MediaWiki file description pages.
Definition: ImagePage.php:34
PermissionsError
Show an error when a user tries to do something they do not have the necessary permissions for.
Definition: PermissionsError.php:32
User\getBlock
getBlock( $freshness=self::READ_NORMAL, $disableIpBlockExemptChecking=false)
Get the block affecting the user, or null if the user is not blocked.
Definition: User.php:1731
MessageLocalizer
Interface for localizing messages in MediaWiki.
Definition: MessageLocalizer.php:29
Action
Actions are things which can be done to pages (edit, delete, rollback, etc).
Definition: Action.php:43
Action\getContext
getContext()
Get the IContextSource in use here.
Definition: Action.php:132
Article\newFromWikiPage
static newFromWikiPage(WikiPage $page, IContextSource $context)
Create an Article object of the appropriate class for the given page.
Definition: Article.php:195
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Definition: GlobalFunctions.php:997
Action\getArticle
getArticle()
Get a Article object.
Definition: Action.php:206
wfTransactionalTimeLimit
wfTransactionalTimeLimit()
Raise the request time limit to $wgTransactionalTimeLimit.
Definition: GlobalFunctions.php:2390
Action\checkCanExecute
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
Definition: Action.php:329
Article\getContext
getContext()
Gets the context this Article is executed in.
Definition: Article.php:1972
Action\convertPageToArticle
static convertPageToArticle(Page $page, ?IContextSource $context, string $method)
Definition: Action.php:273
$title
$title
Definition: testCompression.php:38
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:894
Action\getWikiPage
getWikiPage()
Get a WikiPage object.
Definition: Action.php:195
Action\$article
Article $article
Definition: Action.php:59
Action\getUser
getUser()
Shortcut to get the User being used for this instance.
Definition: Action.php:166
Action\$context
IContextSource $context
IContextSource if specified; otherwise we'll use the Context from the Page.
Definition: Action.php:66
Action\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: Action.php:432
Action\getTitle
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:216
Action\requiresUnblock
requiresUnblock()
Whether this action can still be executed by a blocked user.
Definition: Action.php:385
Action\show
show()
The main action entry point.
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:484
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:58
Action\getHookRunner
getHookRunner()
Definition: Action.php:247
Title
Represents a title within MediaWiki.
Definition: Title.php:47
Action\getSkin
getSkin()
Shortcut to get the Skin being used for this instance.
Definition: Action.php:176
Action\$page
WikiPage Article ImagePage CategoryPage Page $page
Page on which we're performing the action.
Definition: Action.php:53
Action\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: Action.php:228
Action\getLanguage
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition: Action.php:185
Action\requiresWrite
requiresWrite()
Whether this action requires the wiki not to be locked.
Definition: Action.php:374
Action\$fields
array $fields
The fields used to create the HTMLForm.
Definition: Action.php:73
Action\getPageTitle
getPageTitle()
Returns the name that goes in the <h1> page title.
Definition: Action.php:409
Action\getRestriction
getRestriction()
Get the permission required to perform this action.
Definition: Action.php:315
Action\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:156
Action\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: Action.php:461
MediaWiki\HookContainer\HookContainer
HookContainer class.
Definition: HookContainer.php:45
wfWarn
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
Definition: GlobalFunctions.php:1043
MediaWiki\HookContainer\HookRunner
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:554
Article
Class for viewing MediaWiki article and history.
Definition: Article.php:49
Action\getHookContainer
getHookContainer()
Definition: Action.php:237
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:67