MediaWiki  1.23.2
Action.php
Go to the documentation of this file.
1 <?php
37 abstract class Action {
38 
43  protected $page;
44 
49  protected $context;
50 
55  protected $fields;
56 
64  final private static function getClass( $action, array $overrides ) {
65  global $wgActions;
66  $action = strtolower( $action );
67 
68  if ( !isset( $wgActions[$action] ) ) {
69  return null;
70  }
71 
72  if ( $wgActions[$action] === false ) {
73  return false;
74  } elseif ( $wgActions[$action] === true && isset( $overrides[$action] ) ) {
75  return $overrides[$action];
76  } elseif ( $wgActions[$action] === true ) {
77  return ucfirst( $action ) . 'Action';
78  } else {
79  return $wgActions[$action];
80  }
81  }
82 
91  final public static function factory( $action, Page $page, IContextSource $context = null ) {
92  $classOrCallable = self::getClass( $action, $page->getActionOverrides() );
93 
94  if ( is_string( $classOrCallable ) ) {
95  $obj = new $classOrCallable( $page, $context );
96  return $obj;
97  }
98 
99  if ( is_callable( $classOrCallable ) ) {
100  return call_user_func_array( $classOrCallable, array( $page, $context ) );
101  }
102 
103  return $classOrCallable;
104  }
105 
115  final public static function getActionName( IContextSource $context ) {
116  global $wgActions;
117 
118  $request = $context->getRequest();
119  $actionName = $request->getVal( 'action', 'view' );
120 
121  // Check for disabled actions
122  if ( isset( $wgActions[$actionName] ) && $wgActions[$actionName] === false ) {
123  $actionName = 'nosuchaction';
124  }
125 
126  // Workaround for bug #20966: inability of IE to provide an action dependent
127  // on which submit button is clicked.
128  if ( $actionName === 'historysubmit' ) {
129  if ( $request->getBool( 'revisiondelete' ) ) {
130  $actionName = 'revisiondelete';
131  } else {
132  $actionName = 'view';
133  }
134  } elseif ( $actionName == 'editredlink' ) {
135  $actionName = 'edit';
136  }
137 
138  // Trying to get a WikiPage for NS_SPECIAL etc. will result
139  // in WikiPage::factory throwing "Invalid or virtual namespace -1 given."
140  // For SpecialPages et al, default to action=view.
141  if ( !$context->canUseWikiPage() ) {
142  return 'view';
143  }
144 
145  $action = Action::factory( $actionName, $context->getWikiPage(), $context );
146  if ( $action instanceof Action ) {
147  return $action->getName();
148  }
149 
150  return 'nosuchaction';
151  }
152 
159  final public static function exists( $name ) {
160  return self::getClass( $name, array() ) !== null;
161  }
162 
167  final public function getContext() {
168  if ( $this->context instanceof IContextSource ) {
169  return $this->context;
170  } elseif ( $this->page instanceof Article ) {
171  // NOTE: $this->page can be a WikiPage, which does not have a context.
172  wfDebug( __METHOD__ . ": no context known, falling back to Article's context.\n" );
173  return $this->page->getContext();
174  }
175 
176  wfWarn( __METHOD__ . ': no context known, falling back to RequestContext::getMain().' );
177  return RequestContext::getMain();
178  }
179 
185  final public function getRequest() {
186  return $this->getContext()->getRequest();
187  }
188 
194  final public function getOutput() {
195  return $this->getContext()->getOutput();
196  }
197 
203  final public function getUser() {
204  return $this->getContext()->getUser();
205  }
206 
212  final public function getSkin() {
213  return $this->getContext()->getSkin();
214  }
215 
221  final public function getLanguage() {
222  return $this->getContext()->getLanguage();
223  }
224 
231  final public function getLang() {
232  wfDeprecated( __METHOD__, '1.19' );
233  return $this->getLanguage();
234  }
235 
240  final public function getTitle() {
241  return $this->page->getTitle();
242  }
243 
250  final public function msg() {
251  $params = func_get_args();
252  return call_user_func_array( array( $this->getContext(), 'msg' ), $params );
253  }
254 
263  public function __construct( Page $page, IContextSource $context = null ) {
264  if ( $context === null ) {
265  wfWarn( __METHOD__ . ' called without providing a Context object.' );
266  // NOTE: We could try to initialize $context using $page->getContext(),
267  // if $page is an Article. That however seems to not work seamlessly.
268  }
269 
270  $this->page = $page;
271  $this->context = $context;
272  }
273 
278  abstract public function getName();
279 
285  public function getRestriction() {
286  return null;
287  }
288 
298  protected function checkCanExecute( User $user ) {
299  $right = $this->getRestriction();
300  if ( $right !== null ) {
301  $errors = $this->getTitle()->getUserPermissionsErrors( $right, $user );
302  if ( count( $errors ) ) {
303  throw new PermissionsError( $right, $errors );
304  }
305  }
306 
307  if ( $this->requiresUnblock() && $user->isBlocked() ) {
308  $block = $user->getBlock();
309  throw new UserBlockedError( $block );
310  }
311 
312  // This should be checked at the end so that the user won't think the
313  // error is only temporary when he also don't have the rights to execute
314  // this action
315  if ( $this->requiresWrite() && wfReadOnly() ) {
316  throw new ReadOnlyError();
317  }
318  return true;
319  }
320 
325  public function requiresWrite() {
326  return true;
327  }
328 
333  public function requiresUnblock() {
334  return true;
335  }
336 
341  protected function setHeaders() {
342  $out = $this->getOutput();
343  $out->setRobotPolicy( "noindex,nofollow" );
344  $out->setPageTitle( $this->getPageTitle() );
345  $this->getOutput()->setSubtitle( $this->getDescription() );
346  $out->setArticleRelated( true );
347  }
348 
354  protected function getPageTitle() {
355  return $this->getTitle()->getPrefixedText();
356  }
357 
363  protected function getDescription() {
364  return $this->msg( strtolower( $this->getName() ) )->escaped();
365  }
366 
373  abstract public function show();
374 
379  abstract public function execute();
380 }
ReadOnlyError
Show an error when the wiki is locked/read-only and the user tries to do something that requires writ...
Definition: ReadOnlyError.php:28
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:112
Page
Abstract class for type hinting (accepts WikiPage, Article, ImagePage, CategoryPage)
Definition: WikiPage.php:26
Action\__construct
__construct(Page $page, IContextSource $context=null)
Constructor.
Definition: Action.php:260
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
UserBlockedError
Show an error when the user tries to do something whilst blocked.
Definition: UserBlockedError.php:27
Action\getDescription
getDescription()
Returns the description that goes below the <h1> tag.
Definition: Action.php:360
Action\setHeaders
setHeaders()
Set output headers for noindexing etc.
Definition: Action.php:338
Action\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: Action.php:182
CategoryPage
Special handling for category description pages, showing pages, subcategories and file that belong to...
Definition: CategoryPage.php:28
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:37
$right
return false if a UserGetRights hook might remove the named right $right
Definition: hooks.txt:2798
Action\getName
getName()
Return the name of the action this object responds to.
$params
$params
Definition: styleTest.css.php:40
wfReadOnly
wfReadOnly()
Check whether the wiki is in read-only mode.
Definition: GlobalFunctions.php:1313
ImagePage
Class for viewing MediaWiki file description pages.
Definition: ImagePage.php:28
PermissionsError
Show an error when a user tries to do something they do not have the necessary permissions for.
Definition: PermissionsError.php:28
WikiPage\getActionOverrides
getActionOverrides()
Returns overrides for action handlers.
Definition: WikiPage.php:199
Action
Actions are things which can be done to pages (edit, delete, rollback, etc).
Definition: Action.php:37
Action\getContext
getContext()
Get the IContextSource in use here.
Definition: Action.php:164
$out
$out
Definition: UtfNormalGenerate.php:167
IContextSource\canUseWikiPage
canUseWikiPage()
Check whether a WikiPage object can be get with getWikiPage().
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1127
IContextSource\getWikiPage
getWikiPage()
Get the WikiPage object.
Action\checkCanExecute
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
Definition: Action.php:295
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
wfDebug
wfDebug( $text, $dest='all')
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:933
Action\getUser
getUser()
Shortcut to get the User being used for this instance.
Definition: Action.php:200
Action\$context
IContextSource $context
IContextSource if specified; otherwise we'll use the Context from the Page $context.
Definition: Action.php:47
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
Action\getTitle
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:237
Action\requiresUnblock
requiresUnblock()
Whether this action can still be executed by a blocked user.
Definition: Action.php:330
Action\show
show()
The main action entry point.
RequestContext\getMain
static getMain()
Static methods.
Definition: RequestContext.php:420
$user
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 account $user
Definition: hooks.txt:237
IContextSource
Interface for objects which can provide a context on request.
Definition: IContextSource.php:29
Action\getLang
getLang()
Shortcut to get the user Language being used for this instance.
Definition: Action.php:228
Action\msg
msg()
Get a Message object with context set Parameters are the same as wfMessage()
Definition: Action.php:247
Action\exists
static exists( $name)
Check if a given action is recognised, even if it's disabled.
Definition: Action.php:156
Action\getSkin
getSkin()
Shortcut to get the Skin being used for this instance.
Definition: Action.php:209
Action\$page
WikiPage Article ImagePage CategoryPage Page $page
Page on which we're performing the action $page.
Definition: Action.php:42
Action\getClass
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:61
Action\getLanguage
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition: Action.php:218
Action\requiresWrite
requiresWrite()
Whether this action requires the wiki not to be locked.
Definition: Action.php:322
Action\$fields
array $fields
The fields used to create the HTMLForm $fields.
Definition: Action.php:52
Action\getPageTitle
getPageTitle()
Returns the name that goes in the <h1> page title.
Definition: Action.php:351
Action\getRestriction
getRestriction()
Get the permission required to perform this action.
Definition: Action.php:282
Action\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:191
IContextSource\getRequest
getRequest()
Get the WebRequest object.
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:1141
Action\execute
execute()
Execute the action in a silent fashion: do not display anything or release any errors.
Article
Class for viewing MediaWiki article and history.
Definition: Article.php:36
User\isBlocked
isBlocked( $bFromSlave=true)
Check if user is blocked.
Definition: User.php:1721
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:59
Action\factory
static factory( $action, Page $page, IContextSource $context=null)
Get an appropriate Action subclass for the given action.
Definition: Action.php:88
page
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values my talk page
Definition: hooks.txt:1956