MediaWiki  master
Action.php
Go to the documentation of this file.
1 <?php
23 
39 abstract class Action implements MessageLocalizer {
40 
46  protected $page;
47 
53  protected $context;
54 
60  protected $fields;
61 
69  final private static function getClass( $action, array $overrides ) {
70  global $wgActions;
71  $action = strtolower( $action );
72 
73  if ( !isset( $wgActions[$action] ) ) {
74  return null;
75  }
76 
77  if ( $wgActions[$action] === false ) {
78  return false;
79  } elseif ( $wgActions[$action] === true && isset( $overrides[$action] ) ) {
80  return $overrides[$action];
81  } elseif ( $wgActions[$action] === true ) {
82  return ucfirst( $action ) . 'Action';
83  } else {
84  return $wgActions[$action];
85  }
86  }
87 
97  final public static function factory( $action, Page $page, IContextSource $context = null ) {
98  $classOrCallable = self::getClass( $action, $page->getActionOverrides() );
99 
100  if ( is_string( $classOrCallable ) ) {
101  if ( !class_exists( $classOrCallable ) ) {
102  return false;
103  }
104  $obj = new $classOrCallable( $page, $context );
105  return $obj;
106  }
107 
108  if ( is_callable( $classOrCallable ) ) {
109  return $classOrCallable( $page, $context );
110  }
111 
112  return $classOrCallable;
113  }
114 
124  final public static function getActionName( IContextSource $context ) {
125  global $wgActions;
126 
127  $request = $context->getRequest();
128  $actionName = $request->getVal( 'action', 'view' );
129 
130  // Check for disabled actions
131  if ( isset( $wgActions[$actionName] ) && $wgActions[$actionName] === false ) {
132  $actionName = 'nosuchaction';
133  }
134 
135  // Workaround for T22966: inability of IE to provide an action dependent
136  // on which submit button is clicked.
137  if ( $actionName === 'historysubmit' ) {
138  if ( $request->getBool( 'revisiondelete' ) ) {
139  $actionName = 'revisiondelete';
140  } elseif ( $request->getBool( 'editchangetags' ) ) {
141  $actionName = 'editchangetags';
142  } else {
143  $actionName = 'view';
144  }
145  } elseif ( $actionName == 'editredlink' ) {
146  $actionName = 'edit';
147  }
148 
149  // Trying to get a WikiPage for NS_SPECIAL etc. will result
150  // in WikiPage::factory throwing "Invalid or virtual namespace -1 given."
151  // For SpecialPages et al, default to action=view.
152  if ( !$context->canUseWikiPage() ) {
153  return 'view';
154  }
155 
156  $action = self::factory( $actionName, $context->getWikiPage(), $context );
157  if ( $action instanceof Action ) {
158  return $action->getName();
159  }
160 
161  return 'nosuchaction';
162  }
163 
171  final public static function exists( $name ) {
172  return self::getClass( $name, [] ) !== null;
173  }
174 
180  final public function getContext() {
181  if ( $this->context instanceof IContextSource ) {
182  return $this->context;
183  } elseif ( $this->page instanceof Article ) {
184  // NOTE: $this->page can be a WikiPage, which does not have a context.
185  wfDebug( __METHOD__ . ": no context known, falling back to Article's context.\n" );
186  return $this->page->getContext();
187  }
188 
189  wfWarn( __METHOD__ . ': no context known, falling back to RequestContext::getMain().' );
190  return RequestContext::getMain();
191  }
192 
199  final public function getRequest() {
200  return $this->getContext()->getRequest();
201  }
202 
209  final public function getOutput() {
210  return $this->getContext()->getOutput();
211  }
212 
219  final public function getUser() {
220  return $this->getContext()->getUser();
221  }
222 
229  final public function getSkin() {
230  return $this->getContext()->getSkin();
231  }
232 
238  final public function getLanguage() {
239  return $this->getContext()->getLanguage();
240  }
241 
248  final public function getTitle() {
249  return $this->page->getTitle();
250  }
251 
258  final public function msg( $key ) {
259  $params = func_get_args();
260  return $this->getContext()->msg( ...$params );
261  }
262 
269  public function __construct( Page $page, IContextSource $context = null ) {
270  if ( $context === null ) {
271  wfWarn( __METHOD__ . ' called without providing a Context object.' );
272  // NOTE: We could try to initialize $context using $page->getContext(),
273  // if $page is an Article. That however seems to not work seamlessly.
274  }
275 
276  $this->page = $page;
277  $this->context = $context;
278  }
279 
286  abstract public function getName();
287 
295  public function getRestriction() {
296  return null;
297  }
298 
308  protected function checkCanExecute( User $user ) {
309  $right = $this->getRestriction();
310  if ( $right !== null ) {
311  $errors = $this->getTitle()->getUserPermissionsErrors( $right, $user );
312  if ( count( $errors ) ) {
313  throw new PermissionsError( $right, $errors );
314  }
315  }
316 
317  // If the action requires an unblock, explicitly check the user's block.
318  if ( $this->requiresUnblock() && $user->isBlockedFrom( $this->getTitle() ) ) {
319  $block = $user->getBlock();
320  if ( $block ) {
321  throw new UserBlockedError( $block );
322  }
323 
324  throw new PermissionsError( $this->getName(), [ 'badaccess-group0' ] );
325  }
326 
327  // This should be checked at the end so that the user won't think the
328  // error is only temporary when he also don't have the rights to execute
329  // this action
330  if ( $this->requiresWrite() && wfReadOnly() ) {
331  throw new ReadOnlyError();
332  }
333  }
334 
341  public function requiresWrite() {
342  return true;
343  }
344 
351  public function requiresUnblock() {
352  return true;
353  }
354 
360  protected function setHeaders() {
361  $out = $this->getOutput();
362  $out->setRobotPolicy( "noindex,nofollow" );
363  $out->setPageTitle( $this->getPageTitle() );
364  $out->setSubtitle( $this->getDescription() );
365  $out->setArticleRelated( true );
366  }
367 
373  protected function getPageTitle() {
374  return $this->getTitle()->getPrefixedText();
375  }
376 
383  protected function getDescription() {
384  return $this->msg( strtolower( $this->getName() ) )->escaped();
385  }
386 
395  public function addHelpLink( $to, $overrideBaseUrl = false ) {
396  $msg = wfMessage( MediaWikiServices::getInstance()->getContentLanguage()->lc(
397  self::getActionName( $this->getContext() )
398  ) . '-helppage' );
399 
400  if ( !$msg->isDisabled() ) {
401  $helpUrl = Skin::makeUrl( $msg->plain() );
402  $this->getOutput()->addHelpLink( $helpUrl, true );
403  } else {
404  $this->getOutput()->addHelpLink( $to, $overrideBaseUrl );
405  }
406  }
407 
416  abstract public function show();
417 
422  protected function useTransactionalTimeLimit() {
423  if ( $this->getRequest()->wasPosted() ) {
425  }
426  }
427 
433  public function doesWrites() {
434  return false;
435  }
436 }
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:785
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:209
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: Action.php:395
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:248
Show an error when the wiki is locked/read-only and the user tries to do something that requires writ...
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
canUseWikiPage()
Check whether a WikiPage object can be get with getWikiPage().
static factory( $action, Page $page, IContextSource $context=null)
Get an appropriate Action subclass for the given action.
Definition: Action.php:97
getUser()
Shortcut to get the User being used for this instance.
Definition: Action.php:219
Class for viewing MediaWiki article and history.
Definition: Article.php:38
show()
The main action entry point.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition: Action.php:238
requiresUnblock()
Whether this action can still be executed by a blocked user.
Definition: Action.php:351
target page
static exists( $name)
Check if a given action is recognised, even if it&#39;s disabled.
Definition: Action.php:171
getSkin()
Shortcut to get the Skin being used for this instance.
Definition: Action.php:229
doesWrites()
Indicates whether this action may perform database writes.
Definition: Action.php:433
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
Definition: User.php:47
Actions are things which can be done to pages (edit, delete, rollback, etc).
Definition: Action.php:39
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: Action.php:422
$context
IContextSource if specified; otherwise we&#39;ll use the Context from the Page.
Definition: Action.php:53
wfReadOnly()
Check whether the wiki is in read-only mode.
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
static getMain()
Get the RequestContext object associated with the main request.
getRestriction()
Get the permission required to perform this action.
Definition: Action.php:295
getName()
Return the name of the action this object responds to.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
$params
setHeaders()
Set output headers for noindexing etc.
Definition: Action.php:360
isBlockedFrom( $title, $fromReplica=false)
Check if user is blocked from editing a particular article.
Definition: User.php:2294
getContext()
Get the IContextSource in use here.
Definition: Action.php:180
static getActionName(IContextSource $context)
Get the action that will be executed, not necessarily the one passed passed through the "action" requ...
Definition: Action.php:124
static getClass( $action, array $overrides)
Get the Action subclass which should be used to handle this action, false if the action is disabled...
Definition: Action.php:69
Show an error when the user tries to do something whilst blocked.
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
Definition: Action.php:308
$page
Page on which we&#39;re performing the action.
Definition: Action.php:46
$fields
The fields used to create the HTMLForm.
Definition: Action.php:60
static makeUrl( $name, $urlaction='')
Definition: Skin.php:1185
getDescription()
Returns the description that goes below the <h1> tag.
Definition: Action.php:383
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
getPageTitle()
Returns the name that goes in the <h1> page title.
Definition: Action.php:373
getBlock( $bFromReplica=true)
Get the block affecting the user, or null if the user is not blocked.
Definition: User.php:2282
$wgActions
Array of allowed values for the "title=foo&action=<action>" parameter.
Show an error when a user tries to do something they do not have the necessary permissions for...
wfTransactionalTimeLimit()
Set PHP&#39;s time limit to the larger of php.ini or $wgTransactionalTimeLimit.
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:276
getWikiPage()
Get the WikiPage object.
__construct(Page $page, IContextSource $context=null)
Only public since 1.21.
Definition: Action.php:269
msg( $key)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: Action.php:258
getRequest()
Get the WebRequest being used for this instance.
Definition: Action.php:199
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2626
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1487
requiresWrite()
Whether this action requires the wiki not to be locked.
Definition: Action.php:341