RollbackAction.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Edit rollback user interface
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  18. *
  19. * @file
  20. * @ingroup Actions
  21. */
  22. /**
  23. * User interface for the rollback action
  24. *
  25. * @ingroup Actions
  26. */
  27. class RollbackAction extends FormlessAction {
  28. public function getName() {
  29. return 'rollback';
  30. }
  31. public function getRestriction() {
  32. return 'rollback';
  33. }
  34. /**
  35. * Temporarily unused message keys due to T88044/T136375:
  36. * - confirm-rollback-top
  37. * - confirm-rollback-button
  38. * - rollbackfailed
  39. * - rollback-missingparam
  40. * - rollback-success-notify
  41. */
  42. /**
  43. * @throws ErrorPageError
  44. */
  45. public function onView() {
  46. // TODO: use $this->useTransactionalTimeLimit(); when POST only
  47. wfTransactionalTimeLimit();
  48. $request = $this->getRequest();
  49. $user = $this->getUser();
  50. $from = $request->getVal( 'from' );
  51. $rev = $this->page->getRevision();
  52. if ( $from === null ) {
  53. throw new ErrorPageError( 'rollbackfailed', 'rollback-missingparam' );
  54. }
  55. if ( !$rev ) {
  56. throw new ErrorPageError( 'rollbackfailed', 'rollback-missingrevision' );
  57. }
  58. if ( $from !== $rev->getUserText() ) {
  59. throw new ErrorPageError( 'rollbackfailed', 'alreadyrolled', [
  60. $this->getTitle()->getPrefixedText(),
  61. $from,
  62. $rev->getUserText()
  63. ] );
  64. }
  65. $data = null;
  66. $errors = $this->page->doRollback(
  67. $from,
  68. $request->getText( 'summary' ),
  69. $request->getVal( 'token' ),
  70. $request->getBool( 'bot' ),
  71. $data,
  72. $this->getUser()
  73. );
  74. if ( in_array( [ 'actionthrottledtext' ], $errors ) ) {
  75. throw new ThrottledError;
  76. }
  77. if ( isset( $errors[0][0] ) &&
  78. ( $errors[0][0] == 'alreadyrolled' || $errors[0][0] == 'cantrollback' )
  79. ) {
  80. $this->getOutput()->setPageTitle( $this->msg( 'rollbackfailed' ) );
  81. $errArray = $errors[0];
  82. $errMsg = array_shift( $errArray );
  83. $this->getOutput()->addWikiMsgArray( $errMsg, $errArray );
  84. if ( isset( $data['current'] ) ) {
  85. /** @var Revision $current */
  86. $current = $data['current'];
  87. if ( $current->getComment() != '' ) {
  88. $this->getOutput()->addHTML( $this->msg( 'editcomment' )->rawParams(
  89. Linker::formatComment( $current->getComment() ) )->parse() );
  90. }
  91. }
  92. return;
  93. }
  94. # NOTE: Permission errors already handled by Action::checkExecute.
  95. if ( $errors == [ [ 'readonlytext' ] ] ) {
  96. throw new ReadOnlyError;
  97. }
  98. # XXX: Would be nice if ErrorPageError could take multiple errors, and/or a status object.
  99. # Right now, we only show the first error
  100. foreach ( $errors as $error ) {
  101. throw new ErrorPageError( 'rollbackfailed', $error[0], array_slice( $error, 1 ) );
  102. }
  103. /** @var Revision $current */
  104. $current = $data['current'];
  105. $target = $data['target'];
  106. $newId = $data['newid'];
  107. $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
  108. $this->getOutput()->setRobotPolicy( 'noindex,nofollow' );
  109. $old = Linker::revUserTools( $current );
  110. $new = Linker::revUserTools( $target );
  111. $this->getOutput()->addHTML(
  112. $this->msg( 'rollback-success' )
  113. ->rawParams( $old, $new )
  114. ->params( $current->getUserText( Revision::FOR_THIS_USER, $user ) )
  115. ->params( $target->getUserText( Revision::FOR_THIS_USER, $user ) )
  116. ->parseAsBlock()
  117. );
  118. if ( $user->getBoolOption( 'watchrollback' ) ) {
  119. $user->addWatch( $this->page->getTitle(), User::IGNORE_USER_RIGHTS );
  120. }
  121. $this->getOutput()->returnToMain( false, $this->getTitle() );
  122. if ( !$request->getBool( 'hidediff', false ) &&
  123. !$this->getUser()->getBoolOption( 'norollbackdiff' )
  124. ) {
  125. $contentHandler = $current->getContentHandler();
  126. $de = $contentHandler->createDifferenceEngine(
  127. $this->getContext(),
  128. $current->getId(),
  129. $newId,
  130. false,
  131. true
  132. );
  133. $de->showDiff( '', '' );
  134. }
  135. }
  136. protected function getDescription() {
  137. return '';
  138. }
  139. public function doesWrites() {
  140. return true;
  141. }
  142. }