ApiRollback.php 5.2 KB

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