Merge "Exclude redirects from Special:Fewestrevisions"
[lhc/web/wiklou.git] / includes / actions / RollbackAction.php
index 03a5bc8..519da61 100644 (file)
  * @ingroup Actions
  */
 
+use MediaWiki\Storage\RevisionRecord;
+
 /**
  * User interface for the rollback action
  *
  * @ingroup Actions
  */
-class RollbackAction extends FormlessAction {
+class RollbackAction extends FormAction {
 
        public function getName() {
                return 'rollback';
@@ -35,21 +37,59 @@ class RollbackAction extends FormlessAction {
                return 'rollback';
        }
 
-       /**
-        * Temporarily unused message keys due to T88044/T136375:
-        * - confirm-rollback-top
-        * - confirm-rollback-button
-        * - rollbackfailed
-        * - rollback-missingparam
-        * - rollback-success-notify
-        */
+       protected function usesOOUI() {
+               return true;
+       }
+
+       protected function getDescription() {
+               return '';
+       }
+
+       public function doesWrites() {
+               return true;
+       }
+
+       public function onSuccess() {
+               return false;
+       }
+
+       public function onSubmit( $data ) {
+               return false;
+       }
+
+       protected function alterForm( HTMLForm $form ) {
+               $form->setWrapperLegendMsg( 'confirm-rollback-top' );
+               $form->setSubmitTextMsg( 'confirm-rollback-button' );
+               $form->setTokenSalt( 'rollback' );
+
+               $from = $this->getRequest()->getVal( 'from' );
+               if ( $from === null ) {
+                       throw new BadRequestError( 'rollbackfailed', 'rollback-missingparam' );
+               }
+               foreach ( [ 'from', 'bot', 'hidediff', 'summary', 'token' ] as $param ) {
+                       $val = $this->getRequest()->getVal( $param );
+                       if ( $val !== null ) {
+                               $form->addHiddenField( $param, $val );
+                       }
+               }
+       }
 
        /**
         * @throws ErrorPageError
+        * @throws ReadOnlyError
+        * @throws ThrottledError
         */
-       public function onView() {
-               // TODO: use $this->useTransactionalTimeLimit(); when POST only
-               wfTransactionalTimeLimit();
+       public function show() {
+               if ( $this->getUser()->getOption( 'showrollbackconfirmation' ) == false ||
+                        $this->getRequest()->wasPosted() ) {
+                       $this->handleRollbackRequest();
+               } else {
+                       $this->showRollbackConfirmationForm();
+               }
+       }
+
+       public function handleRollbackRequest() {
+               $this->enableTransactionalTimelimit();
 
                $request = $this->getRequest();
                $user = $this->getUser();
@@ -69,15 +109,6 @@ class RollbackAction extends FormlessAction {
                        ] );
                }
 
-               // @TODO: remove this hack once rollback uses POST (T88044)
-               $fname = __METHOD__;
-               $trxLimits = $this->context->getConfig()->get( 'TrxProfilerLimits' );
-               $trxProfiler = Profiler::instance()->getTransactionProfiler();
-               $trxProfiler->redefineExpectations( $trxLimits['POST'], $fname );
-               DeferredUpdates::addCallableUpdate( function () use ( $trxProfiler, $trxLimits, $fname ) {
-                       $trxProfiler->redefineExpectations( $trxLimits['PostSend-POST'], $fname );
-               } );
-
                $data = null;
                $errors = $this->page->doRollback(
                        $from,
@@ -92,9 +123,7 @@ class RollbackAction extends FormlessAction {
                        throw new ThrottledError;
                }
 
-               if ( isset( $errors[0][0] ) &&
-                       ( $errors[0][0] == 'alreadyrolled' || $errors[0][0] == 'cantrollback' )
-               ) {
+               if ( $this->hasRollbackRelatedErrors( $errors ) ) {
                        $this->getOutput()->setPageTitle( $this->msg( 'rollbackfailed' ) );
                        $errArray = $errors[0];
                        $errMsg = array_shift( $errArray );
@@ -140,8 +169,8 @@ class RollbackAction extends FormlessAction {
                $this->getOutput()->addHTML(
                        $this->msg( 'rollback-success' )
                                ->rawParams( $old, $new )
-                               ->params( $current->getUserText( Revision::FOR_THIS_USER, $user ) )
-                               ->params( $target->getUserText( Revision::FOR_THIS_USER, $user ) )
+                               ->params( $current->getUserText( RevisionRecord::FOR_THIS_USER, $user ) )
+                               ->params( $target->getUserText( RevisionRecord::FOR_THIS_USER, $user ) )
                                ->parseAsBlock()
                );
 
@@ -166,11 +195,51 @@ class RollbackAction extends FormlessAction {
                }
        }
 
-       protected function getDescription() {
-               return '';
+       /**
+        * Enables transactional time limit for POST and GET requests to RollbackAction
+        * @throws ConfigException
+        */
+       private function enableTransactionalTimelimit() {
+               // If Rollbacks are made POST-only, use $this->useTransactionalTimeLimit()
+               wfTransactionalTimeLimit();
+               if ( !$this->getRequest()->wasPosted() ) {
+                       /**
+                        * We apply the higher POST limits on GET requests
+                        * to prevent logstash.wikimedia.org from being spammed
+                        */
+                       $fname = __METHOD__;
+                       $trxLimits = $this->context->getConfig()->get( 'TrxProfilerLimits' );
+                       $trxProfiler = Profiler::instance()->getTransactionProfiler();
+                       $trxProfiler->redefineExpectations( $trxLimits['POST'], $fname );
+                       DeferredUpdates::addCallableUpdate( function () use ( $trxProfiler, $trxLimits, $fname
+                       ) {
+                               $trxProfiler->redefineExpectations( $trxLimits['PostSend-POST'], $fname );
+                       } );
+               }
        }
 
-       public function doesWrites() {
-               return true;
+       private function showRollbackConfirmationForm() {
+               $form = $this->getForm();
+               if ( $form->show() ) {
+                       $this->onSuccess();
+               }
+       }
+
+       protected function getFormFields() {
+               return [
+                       'intro' => [
+                               'type' => 'info',
+                               'vertical-label' => true,
+                               'raw' => true,
+                               'default' => $this->msg( 'confirm-rollback-bottom' )->parse()
+                       ]
+               ];
+       }
+
+       private function hasRollbackRelatedErrors( array $errors ) {
+               return isset( $errors[0][0] ) &&
+                       ( $errors[0][0] == 'alreadyrolled' ||
+                               $errors[0][0] == 'cantrollback'
+                       );
        }
 }