ApiRollback.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
  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 along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * @ingroup API
  24. */
  25. class ApiRollback extends ApiBase {
  26. /**
  27. * @var Title
  28. */
  29. private $mTitleObj = null;
  30. /**
  31. * @var User
  32. */
  33. private $mUser = null;
  34. public function execute() {
  35. $this->useTransactionalTimeLimit();
  36. $user = $this->getUser();
  37. $params = $this->extractRequestParams();
  38. $titleObj = $this->getRbTitle( $params );
  39. $pageObj = WikiPage::factory( $titleObj );
  40. $summary = $params['summary'];
  41. $details = [];
  42. // If change tagging was requested, check that the user is allowed to tag,
  43. // and the tags are valid
  44. if ( $params['tags'] ) {
  45. $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
  46. if ( !$tagStatus->isOK() ) {
  47. $this->dieStatus( $tagStatus );
  48. }
  49. }
  50. // @TODO: remove this hack once rollback uses POST (T88044)
  51. $fname = __METHOD__;
  52. $trxLimits = $this->getConfig()->get( 'TrxProfilerLimits' );
  53. $trxProfiler = Profiler::instance()->getTransactionProfiler();
  54. $trxProfiler->redefineExpectations( $trxLimits['POST'], $fname );
  55. DeferredUpdates::addCallableUpdate( function () use ( $trxProfiler, $trxLimits, $fname ) {
  56. $trxProfiler->redefineExpectations( $trxLimits['PostSend-POST'], $fname );
  57. } );
  58. $retval = $pageObj->doRollback(
  59. $this->getRbUser( $params ),
  60. $summary,
  61. $params['token'],
  62. $params['markbot'],
  63. $details,
  64. $user,
  65. $params['tags']
  66. );
  67. if ( $retval ) {
  68. $this->dieStatus( $this->errorArrayToStatus( $retval, $user ) );
  69. }
  70. $watch = $params['watchlist'] ?? 'preferences';
  71. // Watch pages
  72. $this->setWatch( $watch, $titleObj, 'watchrollback' );
  73. $info = [
  74. 'title' => $titleObj->getPrefixedText(),
  75. 'pageid' => (int)$details['current']->getPage(),
  76. 'summary' => $details['summary'],
  77. 'revid' => (int)$details['newid'],
  78. // The revision being reverted (previously the current revision of the page)
  79. 'old_revid' => (int)$details['current']->getID(),
  80. // The revision being restored (the last revision before revision(s) by the reverted user)
  81. 'last_revid' => (int)$details['target']->getID()
  82. ];
  83. $this->getResult()->addValue( null, $this->getModuleName(), $info );
  84. }
  85. public function mustBePosted() {
  86. return true;
  87. }
  88. public function isWriteMode() {
  89. return true;
  90. }
  91. public function getAllowedParams() {
  92. return [
  93. 'title' => null,
  94. 'pageid' => [
  95. ApiBase::PARAM_TYPE => 'integer'
  96. ],
  97. 'tags' => [
  98. ApiBase::PARAM_TYPE => 'tags',
  99. ApiBase::PARAM_ISMULTI => true,
  100. ],
  101. 'user' => [
  102. ApiBase::PARAM_TYPE => 'user',
  103. ApiBase::PARAM_REQUIRED => true
  104. ],
  105. 'summary' => '',
  106. 'markbot' => false,
  107. 'watchlist' => [
  108. ApiBase::PARAM_DFLT => 'preferences',
  109. ApiBase::PARAM_TYPE => [
  110. 'watch',
  111. 'unwatch',
  112. 'preferences',
  113. 'nochange'
  114. ],
  115. ],
  116. 'token' => [
  117. // Standard definition automatically inserted
  118. ApiBase::PARAM_HELP_MSG_APPEND => [ 'api-help-param-token-webui' ],
  119. ],
  120. ];
  121. }
  122. public function needsToken() {
  123. return 'rollback';
  124. }
  125. /**
  126. * @param array $params
  127. *
  128. * @return string
  129. */
  130. private function getRbUser( array $params ) {
  131. if ( $this->mUser !== null ) {
  132. return $this->mUser;
  133. }
  134. // We need to be able to revert IPs, but getCanonicalName rejects them
  135. $this->mUser = User::isIP( $params['user'] )
  136. ? $params['user']
  137. : User::getCanonicalName( $params['user'] );
  138. if ( !$this->mUser ) {
  139. $this->dieWithError( [ 'apierror-invaliduser', wfEscapeWikiText( $params['user'] ) ] );
  140. }
  141. return $this->mUser;
  142. }
  143. /**
  144. * @param array $params
  145. *
  146. * @return Title
  147. */
  148. private function getRbTitle( array $params ) {
  149. if ( $this->mTitleObj !== null ) {
  150. return $this->mTitleObj;
  151. }
  152. $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
  153. if ( isset( $params['title'] ) ) {
  154. $this->mTitleObj = Title::newFromText( $params['title'] );
  155. if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
  156. $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
  157. }
  158. } elseif ( isset( $params['pageid'] ) ) {
  159. $this->mTitleObj = Title::newFromID( $params['pageid'] );
  160. if ( !$this->mTitleObj ) {
  161. $this->dieWithError( [ 'apierror-nosuchpageid', $params['pageid'] ] );
  162. }
  163. }
  164. if ( !$this->mTitleObj->exists() ) {
  165. $this->dieWithError( 'apierror-missingtitle' );
  166. }
  167. return $this->mTitleObj;
  168. }
  169. protected function getExamplesMessages() {
  170. return [
  171. 'action=rollback&title=Main%20Page&user=Example&token=123ABC' =>
  172. 'apihelp-rollback-example-simple',
  173. 'action=rollback&title=Main%20Page&user=192.0.2.5&' .
  174. 'token=123ABC&summary=Reverting%20vandalism&markbot=1' =>
  175. 'apihelp-rollback-example-summary',
  176. ];
  177. }
  178. public function getHelpUrls() {
  179. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Rollback';
  180. }
  181. }