*/
abstract class Action {
- // Page on which we're performing the action
- // @var Article
+ /**
+ * Page on which we're performing the action
+ * @var Page
+ */
protected $page;
- // RequestContext if specified; otherwise we'll use the Context from the Page
- // @var RequestContext
+ /**
+ * IContextSource if specified; otherwise we'll use the Context from the Page
+ * @var IContextSource
+ */
protected $context;
- // The fields used to create the HTMLForm
- // @var Array
+ /**
+ * The fields used to create the HTMLForm
+ * @var Array
+ */
protected $fields;
/**
* Get the Action subclass which should be used to handle this action, false if
* the action is disabled, or null if it's not recognised
* @param $action String
+ * @param $overrides Array
* @return bool|null|string
*/
- private final static function getClass( $action ){
+ private final static function getClass( $action, array $overrides ) {
global $wgActions;
$action = strtolower( $action );
- if( !isset( $wgActions[$action] ) ){
+ if ( !isset( $wgActions[$action] ) ) {
return null;
}
- if( $wgActions[$action] === false ){
+ if ( $wgActions[$action] === false ) {
return false;
- }
-
- elseif( $wgActions[$action] === true ){
+ } elseif ( $wgActions[$action] === true && isset( $overrides[$action] ) ) {
+ return $overrides[$action];
+ } elseif ( $wgActions[$action] === true ) {
return ucfirst( $action ) . 'Action';
- }
-
- else {
+ } else {
return $wgActions[$action];
}
}
/**
* Get an appropriate Action subclass for the given action
* @param $action String
- * @param $page Article
+ * @param $page Page
+ * @param $context IContextSource
* @return Action|false|null false if the action is disabled, null
* if it is not recognised
*/
- public final static function factory( $action, Article $page ){
- $class = self::getClass( $action );
- if( $class ){
- $obj = new $class( $page );
+ public final static function factory( $action, Page $page, IContextSource $context = null ) {
+ $class = self::getClass( $action, $page->getActionOverrides() );
+ if ( $class ) {
+ $obj = new $class( $page, $context );
return $obj;
}
return $class;
* @return Bool
*/
public final static function exists( $name ) {
- return self::getClass( $name ) !== null;
+ return self::getClass( $name, array() ) !== null;
}
/**
- * Get the RequestContext in use here
- * @return RequestContext
+ * Get the IContextSource in use here
+ * @return IContextSource
*/
- protected final function getContext(){
- if( $this->context instanceof RequestContext ){
+ public final function getContext() {
+ if ( $this->context instanceof IContextSource ) {
return $this->context;
}
return $this->page->getContext();
*
* @return WebRequest
*/
- protected final function getRequest() {
- return $this->getContext()->request;
+ public final function getRequest() {
+ return $this->getContext()->getRequest();
}
/**
*
* @return OutputPage
*/
- protected final function getOutput() {
- return $this->getContext()->output;
+ public final function getOutput() {
+ return $this->getContext()->getOutput();
}
/**
*
* @return User
*/
- protected final function getUser() {
- return $this->getContext()->user;
+ public final function getUser() {
+ return $this->getContext()->getUser();
}
/**
*
* @return Skin
*/
- protected final function getSkin() {
- return $this->getContext()->skin;
+ public final function getSkin() {
+ return $this->getContext()->getSkin();
+ }
+
+ /**
+ * Shortcut to get the user Language being used for this instance
+ *
+ * @return Skin
+ */
+ public final function getLanguage() {
+ return $this->getContext()->getLanguage();
+ }
+
+ /**
+ * Shortcut to get the user Language being used for this instance
+ *
+ * @deprecated 1.19 Use getLanguage instead
+ * @return Skin
+ */
+ public final function getLang() {
+ wfDeprecated( __METHOD__, '1.19' );
+ return $this->getLanguage();
}
/**
* Shortcut to get the Title object from the page
* @return Title
*/
- protected final function getTitle(){
+ public final function getTitle() {
return $this->page->getTitle();
}
+ /**
+ * Get a Message object with context set
+ * Parameters are the same as wfMessage()
+ *
+ * @return Message object
+ */
+ public final function msg() {
+ $params = func_get_args();
+ return call_user_func_array( array( $this->getContext(), 'msg' ), $params );
+ }
+
/**
* Protected constructor: use Action::factory( $action, $page ) to actually build
* these things in the real world
- * @param Article $page
+ * @param $page Page
+ * @param $context IContextSource
*/
- protected function __construct( Article $page ){
+ protected function __construct( Page $page, IContextSource $context = null ) {
$this->page = $page;
+ $this->context = $context;
}
/**
/**
* Get the permission required to perform this action. Often, but not always,
* the same as the action name
+ * @return String|null
*/
- public abstract function getRestriction();
+ public function getRestriction() {
+ return null;
+ }
/**
* Checks if the given user (identified by an object) can perform this action. Can be
* @throws ErrorPageError
*/
protected function checkCanExecute( User $user ) {
- if( $this->requiresWrite() && wfReadOnly() ){
- throw new ReadOnlyError();
- }
-
- if( $this->getRestriction() !== null && !$user->isAllowed( $this->getRestriction() ) ){
- throw new PermissionsError( $this->getRestriction() );
+ $right = $this->getRestriction();
+ if ( $right !== null ) {
+ $errors = $this->getTitle()->getUserPermissionsErrors( $right, $user );
+ if ( count( $errors ) ) {
+ throw new PermissionsError( $right, $errors );
+ }
}
- if( $this->requiresUnblock() && $user->isBlocked() ){
+ if ( $this->requiresUnblock() && $user->isBlocked() ) {
$block = $user->mBlock;
throw new UserBlockedError( $block );
}
+
+ // This should be checked at the end so that the user won't think the
+ // error is only temporary when he also don't have the rights to execute
+ // this action
+ if ( $this->requiresWrite() && wfReadOnly() ) {
+ throw new ReadOnlyError();
+ }
}
/**
* Whether this action requires the wiki not to be locked
* @return Bool
*/
- public function requiresWrite(){
+ public function requiresWrite() {
return true;
}
* Whether this action can still be executed by a blocked user
* @return Bool
*/
- public function requiresUnblock(){
+ public function requiresUnblock() {
return true;
}
protected function setHeaders() {
$out = $this->getOutput();
$out->setRobotPolicy( "noindex,nofollow" );
- $out->setPageTitle( $this->getTitle()->getPrefixedText() );
+ $out->setPageTitle( $this->getPageTitle() );
$this->getOutput()->setSubtitle( $this->getDescription() );
$out->setArticleRelated( true );
}
/**
* Returns the name that goes in the \<h1\> page title
*
- * Derived classes can override this, but usually it is easier to keep the
- * default behaviour. Messages can be added at run-time, see
- * MessageCache.php.
+ * @return String
+ */
+ protected function getPageTitle() {
+ return $this->getTitle()->getPrefixedText();
+ }
+
+ /**
+ * Returns the description that goes below the \<h1\> tag
*
* @return String
*/
* Add pre- or post-text to the form
* @return String HTML which will be sent to $form->addPreText()
*/
- protected function preText(){ return ''; }
- protected function postText(){ return ''; }
+ protected function preText() { return ''; }
+
+ /**
+ * @return string
+ */
+ protected function postText() { return ''; }
/**
* Play with the HTMLForm if you need to more substantially
- * @param &$form HTMLForm
+ * @param $form HTMLForm
*/
- protected function alterForm( HTMLForm &$form ){}
+ protected function alterForm( HTMLForm $form ) {}
/**
* Get the HTMLForm to control behaviour
* @return HTMLForm|null
*/
- protected function getForm(){
+ protected function getForm() {
$this->fields = $this->getFormFields();
// Give hooks a chance to alter the form, adding extra fields or text etc
$form = new HTMLForm( $this->fields, $this->getContext() );
$form->setSubmitCallback( array( $this, 'onSubmit' ) );
- $form->addHiddenField( 'action', $this->getName() );
+
+ // Retain query parameters (uselang etc)
+ $form->addHiddenField( 'action', $this->getName() ); // Might not be the same as the query string
+ $params = array_diff_key(
+ $this->getRequest()->getQueryValues(),
+ array( 'action' => null, 'title' => null )
+ );
+ $form->addHiddenField( 'redirectparams', wfArrayToCGI( $params ) );
$form->addPreText( $this->preText() );
$form->addPostText( $this->postText() );
* display something new or redirect to somewhere. Some actions have more exotic
* behaviour, but that's what subclassing is for :D
*/
- public function show(){
+ public function show() {
$this->setHeaders();
// This will throw exceptions if there's a problem
$this->checkCanExecute( $this->getUser() );
$form = $this->getForm();
- if( $form->show() ){
+ if ( $form->show() ) {
$this->onSuccess();
}
}
* @param bool $captureErrors
* @return bool
*/
- public function execute( array $data = null, $captureErrors = true ){
+ public function execute( array $data = null, $captureErrors = true ) {
try {
// Set a new context so output doesn't leak.
$this->context = clone $this->page->getContext();
$this->checkCanExecute( $this->getUser() );
$fields = array();
- foreach( $this->fields as $key => $params ){
- if( isset( $data[$key] ) ){
+ foreach ( $this->fields as $key => $params ) {
+ if ( isset( $data[$key] ) ) {
$fields[$key] = $data[$key];
- } elseif( isset( $params['default'] ) ) {
+ } elseif ( isset( $params['default'] ) ) {
$fields[$key] = $params['default'];
} else {
$fields[$key] = null;
}
}
$status = $this->onSubmit( $fields );
- if( $status === true ){
+ if ( $status === true ) {
// This might do permanent stuff
$this->onSuccess();
return true;
return false;
}
}
- catch ( ErrorPageError $e ){
- if( $captureErrors ){
+ catch ( ErrorPageError $e ) {
+ if ( $captureErrors ) {
return false;
} else {
throw $e;
/**
* We don't want an HTMLForm
*/
- protected function getFormFields(){
+ protected function getFormFields() {
return false;
}
- public function onSubmit( $data ){
+ public function onSubmit( $data ) {
return false;
}
- public function onSuccess(){
+ public function onSuccess() {
return false;
}
- public function show(){
+ public function show() {
$this->setHeaders();
// This will throw exceptions if there's a problem
* @param $captureErrors Bool whether to catch exceptions and just return false
* @return Bool whether execution was successful
*/
- public function execute( array $data = null, $captureErrors = true){
+ public function execute( array $data = null, $captureErrors = true ) {
try {
// Set a new context so output doesn't leak.
$this->context = clone $this->page->getContext();
- if( is_array( $data ) ){
+ if ( is_array( $data ) ) {
$this->context->setRequest( new FauxRequest( $data, false ) );
}
$this->onView();
return true;
}
- catch ( ErrorPageError $e ){
- if( $captureErrors ){
+ catch ( ErrorPageError $e ) {
+ if ( $captureErrors ) {
return false;
} else {
throw $e;
}
}
}
-}
\ No newline at end of file
+}