3 * Copyright © 2013 Wikimedia Foundation and contributors
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.
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.
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
24 namespace MediaWiki\Api
;
27 use MediaWiki\Revision\RevisionRecord
;
28 use MediaWiki\Status\Status
;
29 use MediaWiki\Title\Title
;
31 use Wikimedia\ParamValidator\ParamValidator
;
34 * API interface to RevDel. The API equivalent of Special:RevisionDelete.
35 * Requires API write mode to be enabled.
39 class ApiRevisionDelete
extends ApiBase
{
41 public function execute() {
42 $this->useTransactionalTimeLimit();
44 $params = $this->extractRequestParams();
45 $user = $this->getUser();
46 $this->checkUserRightsAny( RevisionDeleter
::getRestriction( $params['type'] ) );
48 if ( !$params['ids'] ) {
49 $this->dieWithError( [ 'apierror-paramempty', 'ids' ], 'paramempty_ids' );
52 // Check if user can add tags
53 if ( $params['tags'] ) {
54 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
55 if ( !$ableToTag->isOK() ) {
56 $this->dieStatus( $ableToTag );
60 $hide = $params['hide'] ?
: [];
61 $show = $params['show'] ?
: [];
62 if ( array_intersect( $hide, $show ) ) {
63 $this->dieWithError( 'apierror-revdel-mutuallyexclusive', 'badparams' );
64 } elseif ( !$hide && !$show ) {
65 $this->dieWithError( 'apierror-revdel-paramneeded', 'badparams' );
68 'content' => RevisionDeleter
::getRevdelConstant( $params['type'] ),
69 'comment' => RevisionRecord
::DELETED_COMMENT
,
70 'user' => RevisionRecord
::DELETED_USER
,
73 foreach ( $bits as $key => $bit ) {
74 if ( in_array( $key, $hide ) ) {
76 } elseif ( in_array( $key, $show ) ) {
83 if ( $params['suppress'] === 'yes' ) {
84 $this->checkUserRightsAny( 'suppressrevision' );
85 $bitfield[RevisionRecord
::DELETED_RESTRICTED
] = 1;
86 } elseif ( $params['suppress'] === 'no' ) {
87 $bitfield[RevisionRecord
::DELETED_RESTRICTED
] = 0;
89 $bitfield[RevisionRecord
::DELETED_RESTRICTED
] = -1;
93 if ( $params['target'] ) {
94 $targetObj = Title
::newFromText( $params['target'] );
96 $targetObj = RevisionDeleter
::suggestTarget( $params['type'], $targetObj, $params['ids'] );
97 if ( $targetObj === null ) {
98 $this->dieWithError( [ 'apierror-revdel-needtarget' ], 'needtarget' );
101 // TODO: replace use of PermissionManager
102 if ( $this->getPermissionManager()->isBlockedFrom( $user, $targetObj ) ) {
103 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Block is checked and not null
104 $this->dieBlocked( $user->getBlock() );
107 $list = RevisionDeleter
::createList(
108 $params['type'], $this->getContext(), $targetObj, $params['ids']
110 $status = $list->setVisibility( [
111 'value' => $bitfield,
112 'comment' => $params['reason'] ??
'',
113 'perItemStatus' => true,
114 'tags' => $params['tags']
117 $result = $this->getResult();
118 $data = $this->extractStatusInfo( $status );
119 $data['target'] = $targetObj->getFullText();
122 foreach ( $status->getValue()['itemStatuses'] as $id => $s ) {
123 $data['items'][$id] = $this->extractStatusInfo( $s );
124 $data['items'][$id]['id'] = $id;
127 $list->reloadFromPrimary();
128 for ( $item = $list->reset(); $list->current(); $item = $list->next() ) {
129 $data['items'][$item->getId()] +
= $item->getApiData( $this->getResult() );
132 $data['items'] = array_values( $data['items'] );
133 ApiResult
::setIndexedTagName( $data['items'], 'i' );
134 $result->addValue( null, $this->getModuleName(), $data );
137 private function extractStatusInfo( Status
$status ) {
139 'status' => $status->isOK() ?
'Success' : 'Fail',
142 $errors = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
144 $ret['errors'] = $errors;
146 $warnings = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
148 $ret['warnings'] = $warnings;
154 public function mustBePosted() {
158 public function isWriteMode() {
162 public function getAllowedParams() {
165 ParamValidator
::PARAM_TYPE
=> RevisionDeleter
::getTypes(),
166 ParamValidator
::PARAM_REQUIRED
=> true
170 ParamValidator
::PARAM_ISMULTI
=> true,
171 ParamValidator
::PARAM_REQUIRED
=> true
174 ParamValidator
::PARAM_TYPE
=> [ 'content', 'comment', 'user' ],
175 ParamValidator
::PARAM_ISMULTI
=> true,
178 ParamValidator
::PARAM_TYPE
=> [ 'content', 'comment', 'user' ],
179 ParamValidator
::PARAM_ISMULTI
=> true,
182 ParamValidator
::PARAM_TYPE
=> [ 'yes', 'no', 'nochange' ],
183 ParamValidator
::PARAM_DEFAULT
=> 'nochange',
186 ParamValidator
::PARAM_TYPE
=> 'string'
189 ParamValidator
::PARAM_TYPE
=> 'tags',
190 ParamValidator
::PARAM_ISMULTI
=> true,
195 public function needsToken() {
199 protected function getExamplesMessages() {
200 $title = Title
::newMainPage()->getPrefixedText();
201 $mp = rawurlencode( $title );
204 "action=revisiondelete&target={$mp}&type=revision&ids=12345&" .
205 'hide=content&token=123ABC'
206 => 'apihelp-revisiondelete-example-revision',
207 'action=revisiondelete&type=logging&ids=67890&hide=content|comment|user&' .
208 'reason=BLP%20violation&token=123ABC'
209 => 'apihelp-revisiondelete-example-log',
213 public function getHelpUrls() {
214 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Revisiondelete';
218 /** @deprecated class alias since 1.43 */
219 class_alias( ApiRevisionDelete
::class, 'ApiRevisionDelete' );