MediaWiki  master
Action.php
Go to the documentation of this file.
1 <?php
37 abstract class Action implements MessageLocalizer {
38 
44  protected $page;
45 
51  protected $context;
52 
58  protected $fields;
59 
67  final private static function getClass( $action, array $overrides ) {
68  global $wgActions;
69  $action = strtolower( $action );
70 
71  if ( !isset( $wgActions[$action] ) ) {
72  return null;
73  }
74 
75  if ( $wgActions[$action] === false ) {
76  return false;
77  } elseif ( $wgActions[$action] === true && isset( $overrides[$action] ) ) {
78  return $overrides[$action];
79  } elseif ( $wgActions[$action] === true ) {
80  return ucfirst( $action ) . 'Action';
81  } else {
82  return $wgActions[$action];
83  }
84  }
85 
95  final public static function factory( $action, Page $page, IContextSource $context = null ) {
96  $classOrCallable = self::getClass( $action, $page->getActionOverrides() );
97 
98  if ( is_string( $classOrCallable ) ) {
99  if ( !class_exists( $classOrCallable ) ) {
100  return false;
101  }
102  $obj = new $classOrCallable( $page, $context );
103  return $obj;
104  }
105 
106  if ( is_callable( $classOrCallable ) ) {
107  return call_user_func_array( $classOrCallable, [ $page, $context ] );
108  }
109 
110  return $classOrCallable;
111  }
112 
122  final public static function getActionName( IContextSource $context ) {
123  global $wgActions;
124 
125  $request = $context->getRequest();
126  $actionName = $request->getVal( 'action', 'view' );
127 
128  // Check for disabled actions
129  if ( isset( $wgActions[$actionName] ) && $wgActions[$actionName] === false ) {
130  $actionName = 'nosuchaction';
131  }
132 
133  // Workaround for bug #20966: inability of IE to provide an action dependent
134  // on which submit button is clicked.
135  if ( $actionName === 'historysubmit' ) {
136  if ( $request->getBool( 'revisiondelete' ) ) {
137  $actionName = 'revisiondelete';
138  } elseif ( $request->getBool( 'editchangetags' ) ) {
139  $actionName = 'editchangetags';
140  } else {
141  $actionName = 'view';
142  }
143  } elseif ( $actionName == 'editredlink' ) {
144  $actionName = 'edit';
145  }
146 
147  // Trying to get a WikiPage for NS_SPECIAL etc. will result
148  // in WikiPage::factory throwing "Invalid or virtual namespace -1 given."
149  // For SpecialPages et al, default to action=view.
150  if ( !$context->canUseWikiPage() ) {
151  return 'view';
152  }
153 
154  $action = self::factory( $actionName, $context->getWikiPage(), $context );
155  if ( $action instanceof Action ) {
156  return $action->getName();
157  }
158 
159  return 'nosuchaction';
160  }
161 
169  final public static function exists( $name ) {
170  return self::getClass( $name, [] ) !== null;
171  }
172 
178  final public function getContext() {
179  if ( $this->context instanceof IContextSource ) {
180  return $this->context;
181  } elseif ( $this->page instanceof Article ) {
182  // NOTE: $this->page can be a WikiPage, which does not have a context.
183  wfDebug( __METHOD__ . ": no context known, falling back to Article's context.\n" );
184  return $this->page->getContext();
185  }
186 
187  wfWarn( __METHOD__ . ': no context known, falling back to RequestContext::getMain().' );
188  return RequestContext::getMain();
189  }
190 
197  final public function getRequest() {
198  return $this->getContext()->getRequest();
199  }
200 
207  final public function getOutput() {
208  return $this->getContext()->getOutput();
209  }
210 
217  final public function getUser() {
218  return $this->getContext()->getUser();
219  }
220 
227  final public function getSkin() {
228  return $this->getContext()->getSkin();
229  }
230 
236  final public function getLanguage() {
237  return $this->getContext()->getLanguage();
238  }
239 
246  final public function getTitle() {
247  return $this->page->getTitle();
248  }
249 
256  final public function msg( $key ) {
257  $params = func_get_args();
258  return call_user_func_array( [ $this->getContext(), 'msg' ], $params );
259  }
260 
267  public function __construct( Page $page, IContextSource $context = null ) {
268  if ( $context === null ) {
269  wfWarn( __METHOD__ . ' called without providing a Context object.' );
270  // NOTE: We could try to initialize $context using $page->getContext(),
271  // if $page is an Article. That however seems to not work seamlessly.
272  }
273 
274  $this->page = $page;
275  $this->context = $context;
276  }
277 
284  abstract public function getName();
285 
293  public function getRestriction() {
294  return null;
295  }
296 
306  protected function checkCanExecute( User $user ) {
307  $right = $this->getRestriction();
308  if ( $right !== null ) {
309  $errors = $this->getTitle()->getUserPermissionsErrors( $right, $user );
310  if ( count( $errors ) ) {
311  throw new PermissionsError( $right, $errors );
312  }
313  }
314 
315  if ( $this->requiresUnblock() && $user->isBlocked() ) {
316  $block = $user->getBlock();
317  throw new UserBlockedError( $block );
318  }
319 
320  // This should be checked at the end so that the user won't think the
321  // error is only temporary when he also don't have the rights to execute
322  // this action
323  if ( $this->requiresWrite() && wfReadOnly() ) {
324  throw new ReadOnlyError();
325  }
326  }
327 
334  public function requiresWrite() {
335  return true;
336  }
337 
344  public function requiresUnblock() {
345  return true;
346  }
347 
353  protected function setHeaders() {
354  $out = $this->getOutput();
355  $out->setRobotPolicy( "noindex,nofollow" );
356  $out->setPageTitle( $this->getPageTitle() );
357  $out->setSubtitle( $this->getDescription() );
358  $out->setArticleRelated( true );
359  }
360 
366  protected function getPageTitle() {
367  return $this->getTitle()->getPrefixedText();
368  }
369 
376  protected function getDescription() {
377  return $this->msg( strtolower( $this->getName() ) )->escaped();
378  }
379 
388  public function addHelpLink( $to, $overrideBaseUrl = false ) {
390  $msg = wfMessage( $wgContLang->lc(
391  self::getActionName( $this->getContext() )
392  ) . '-helppage' );
393 
394  if ( !$msg->isDisabled() ) {
395  $helpUrl = Skin::makeUrl( $msg->plain() );
396  $this->getOutput()->addHelpLink( $helpUrl, true );
397  } else {
398  $this->getOutput()->addHelpLink( $to, $overrideBaseUrl );
399  }
400  }
401 
410  abstract public function show();
411 
416  protected function useTransactionalTimeLimit() {
417  if ( $this->getRequest()->wasPosted() ) {
419  }
420  }
421 
427  public function doesWrites() {
428  return false;
429  }
430 }
addHelpLink($to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: Action.php:388
Interface for objects which can provide a MediaWiki context on request.
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:782
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:207
the array() calling protocol came about after MediaWiki 1.4rc1.
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2556
static makeUrl($name, $urlaction= '')
Definition: Skin.php:1162
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:246
Show an error when the wiki is locked/read-only and the user tries to do something that requires writ...
canUseWikiPage()
Check whether a WikiPage object can be get with getWikiPage().
getUser()
Shortcut to get the User being used for this instance.
Definition: Action.php:217
Class for viewing MediaWiki article and history.
Definition: Article.php:35
show()
The main action entry point.
static exists($name)
Check if a given action is recognised, even if it's disabled.
Definition: Action.php:169
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition: Action.php:236
requiresUnblock()
Whether this action can still be executed by a blocked user.
Definition: Action.php:344
target page
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
getSkin()
Shortcut to get the Skin being used for this instance.
Definition: Action.php:227
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
doesWrites()
Indicates whether this action may perform database writes.
Definition: Action.php:427
static factory($action, Page $page, IContextSource $context=null)
Get an appropriate Action subclass for the given action.
Definition: Action.php:95
Actions are things which can be done to pages (edit, delete, rollback, etc).
Definition: Action.php:37
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: Action.php:416
getBlock($bFromSlave=true)
Get the block affecting the user, or null if the user is not blocked.
Definition: User.php:2079
$context
IContextSource if specified; otherwise we'll use the Context from the Page.
Definition: Action.php:51
Interface for type hinting (accepts WikiPage, Article, ImagePage, CategoryPage)
Definition: Page.php:24
wfReadOnly()
Check whether the wiki is in read-only mode.
static getMain()
Static methods.
getRestriction()
Get the permission required to perform this action.
Definition: Action.php:293
getName()
Return the name of the action this object responds to.
wfWarn($msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
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 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 unsetoffset-wrap String Wrap the message in html(usually something like"&lt
$params
setHeaders()
Set output headers for noindexing etc.
Definition: Action.php:353
getContext()
Get the IContextSource in use here.
Definition: Action.php:178
static getActionName(IContextSource $context)
Get the action that will be executed, not necessarily the one passed passed through the "action" requ...
Definition: Action.php:122
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:306
$page
Page on which we're performing the action.
Definition: Action.php:44
$fields
The fields used to create the HTMLForm.
Definition: Action.php:58
isBlocked($bFromSlave=true)
Check if user is blocked.
Definition: User.php:2069
getDescription()
Returns the description that goes below the \ tag.
Definition: Action.php:376
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:67
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 \ page title.
Definition: Action.php:366
Show an error when a user tries to do something they do not have the necessary permissions for...
wfTransactionalTimeLimit()
Set PHP's time limit to the larger of php.ini or $wgTransactionalTimeLimit.
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition: design.txt:56
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
getWikiPage()
Get the WikiPage object.
Interface for localizing messages in MediaWiki.
msg($key)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: Action.php:256
__construct(Page $page, IContextSource $context=null)
Only public since 1.21.
Definition: Action.php:267
getRequest()
Get the WebRequest being used for this instance.
Definition: Action.php:197
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:2582
getRequest()
Get the WebRequest object.
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition: hooks.txt:244
requiresWrite()
Whether this action requires the wiki not to be locked.
Definition: Action.php:334