ApiRevisionDelete.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Copyright © 2013 Wikimedia Foundation and contributors
  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. * @since 1.23
  22. */
  23. use MediaWiki\Revision\RevisionRecord;
  24. /**
  25. * API interface to RevDel. The API equivalent of Special:RevisionDelete.
  26. * Requires API write mode to be enabled.
  27. *
  28. * @ingroup API
  29. */
  30. class ApiRevisionDelete extends ApiBase {
  31. public function execute() {
  32. $this->useTransactionalTimeLimit();
  33. $params = $this->extractRequestParams();
  34. $user = $this->getUser();
  35. $this->checkUserRightsAny( RevisionDeleter::getRestriction( $params['type'] ) );
  36. if ( !$params['ids'] ) {
  37. $this->dieWithError( [ 'apierror-paramempty', 'ids' ], 'paramempty_ids' );
  38. }
  39. // Check if user can add tags
  40. if ( $params['tags'] ) {
  41. $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
  42. if ( !$ableToTag->isOK() ) {
  43. $this->dieStatus( $ableToTag );
  44. }
  45. }
  46. $hide = $params['hide'] ?: [];
  47. $show = $params['show'] ?: [];
  48. if ( array_intersect( $hide, $show ) ) {
  49. $this->dieWithError( 'apierror-revdel-mutuallyexclusive', 'badparams' );
  50. } elseif ( !$hide && !$show ) {
  51. $this->dieWithError( 'apierror-revdel-paramneeded', 'badparams' );
  52. }
  53. $bits = [
  54. 'content' => RevisionDeleter::getRevdelConstant( $params['type'] ),
  55. 'comment' => RevisionRecord::DELETED_COMMENT,
  56. 'user' => RevisionRecord::DELETED_USER,
  57. ];
  58. $bitfield = [];
  59. foreach ( $bits as $key => $bit ) {
  60. if ( in_array( $key, $hide ) ) {
  61. $bitfield[$bit] = 1;
  62. } elseif ( in_array( $key, $show ) ) {
  63. $bitfield[$bit] = 0;
  64. } else {
  65. $bitfield[$bit] = -1;
  66. }
  67. }
  68. if ( $params['suppress'] === 'yes' ) {
  69. $this->checkUserRightsAny( 'suppressrevision' );
  70. $bitfield[RevisionRecord::DELETED_RESTRICTED] = 1;
  71. } elseif ( $params['suppress'] === 'no' ) {
  72. $bitfield[RevisionRecord::DELETED_RESTRICTED] = 0;
  73. } else {
  74. $bitfield[RevisionRecord::DELETED_RESTRICTED] = -1;
  75. }
  76. $targetObj = null;
  77. if ( $params['target'] ) {
  78. $targetObj = Title::newFromText( $params['target'] );
  79. }
  80. $targetObj = RevisionDeleter::suggestTarget( $params['type'], $targetObj, $params['ids'] );
  81. if ( $targetObj === null ) {
  82. $this->dieWithError( [ 'apierror-revdel-needtarget' ], 'needtarget' );
  83. }
  84. if ( $this->getPermissionManager()->isBlockedFrom( $user, $targetObj ) ) {
  85. $this->dieBlocked( $user->getBlock() );
  86. }
  87. $list = RevisionDeleter::createList(
  88. $params['type'], $this->getContext(), $targetObj, $params['ids']
  89. );
  90. $status = $list->setVisibility( [
  91. 'value' => $bitfield,
  92. 'comment' => $params['reason'],
  93. 'perItemStatus' => true,
  94. 'tags' => $params['tags']
  95. ] );
  96. $result = $this->getResult();
  97. $data = $this->extractStatusInfo( $status );
  98. $data['target'] = $targetObj->getFullText();
  99. $data['items'] = [];
  100. foreach ( $status->itemStatuses as $id => $s ) {
  101. $data['items'][$id] = $this->extractStatusInfo( $s );
  102. $data['items'][$id]['id'] = $id;
  103. }
  104. $list->reloadFromMaster();
  105. for ( $item = $list->reset(); $list->current(); $item = $list->next() ) {
  106. $data['items'][$item->getId()] += $item->getApiData( $this->getResult() );
  107. }
  108. $data['items'] = array_values( $data['items'] );
  109. ApiResult::setIndexedTagName( $data['items'], 'i' );
  110. $result->addValue( null, $this->getModuleName(), $data );
  111. }
  112. private function extractStatusInfo( $status ) {
  113. $ret = [
  114. 'status' => $status->isOK() ? 'Success' : 'Fail',
  115. ];
  116. $errors = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
  117. if ( $errors ) {
  118. $ret['errors'] = $errors;
  119. }
  120. $warnings = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
  121. if ( $warnings ) {
  122. $ret['warnings'] = $warnings;
  123. }
  124. return $ret;
  125. }
  126. public function mustBePosted() {
  127. return true;
  128. }
  129. public function isWriteMode() {
  130. return true;
  131. }
  132. public function getAllowedParams() {
  133. return [
  134. 'type' => [
  135. ApiBase::PARAM_TYPE => RevisionDeleter::getTypes(),
  136. ApiBase::PARAM_REQUIRED => true
  137. ],
  138. 'target' => null,
  139. 'ids' => [
  140. ApiBase::PARAM_ISMULTI => true,
  141. ApiBase::PARAM_REQUIRED => true
  142. ],
  143. 'hide' => [
  144. ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
  145. ApiBase::PARAM_ISMULTI => true,
  146. ],
  147. 'show' => [
  148. ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
  149. ApiBase::PARAM_ISMULTI => true,
  150. ],
  151. 'suppress' => [
  152. ApiBase::PARAM_TYPE => [ 'yes', 'no', 'nochange' ],
  153. ApiBase::PARAM_DFLT => 'nochange',
  154. ],
  155. 'reason' => null,
  156. 'tags' => [
  157. ApiBase::PARAM_TYPE => 'tags',
  158. ApiBase::PARAM_ISMULTI => true,
  159. ],
  160. ];
  161. }
  162. public function needsToken() {
  163. return 'csrf';
  164. }
  165. protected function getExamplesMessages() {
  166. return [
  167. 'action=revisiondelete&target=Main%20Page&type=revision&ids=12345&' .
  168. 'hide=content&token=123ABC'
  169. => 'apihelp-revisiondelete-example-revision',
  170. 'action=revisiondelete&type=logging&ids=67890&hide=content|comment|user&' .
  171. 'reason=BLP%20violation&token=123ABC'
  172. => 'apihelp-revisiondelete-example-log',
  173. ];
  174. }
  175. public function getHelpUrls() {
  176. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Revisiondelete';
  177. }
  178. }