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 use MediaWiki\Revision\RevisionRecord
;
27 * API interface to RevDel. The API equivalent of Special:RevisionDelete.
28 * Requires API write mode to be enabled.
32 class ApiRevisionDelete
extends ApiBase
{
34 public function execute() {
35 $this->useTransactionalTimeLimit();
37 $params = $this->extractRequestParams();
38 $user = $this->getUser();
39 $this->checkUserRightsAny( RevisionDeleter
::getRestriction( $params['type'] ) );
41 // @TODO Use PermissionManager::isBlockedFrom() instead.
42 $block = $user->getBlock();
44 $this->dieBlocked( $block );
47 if ( !$params['ids'] ) {
48 $this->dieWithError( [ 'apierror-paramempty', 'ids' ], 'paramempty_ids' );
51 // Check if user can add tags
52 if ( $params['tags'] ) {
53 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $user );
54 if ( !$ableToTag->isOK() ) {
55 $this->dieStatus( $ableToTag );
59 $hide = $params['hide'] ?
: [];
60 $show = $params['show'] ?
: [];
61 if ( array_intersect( $hide, $show ) ) {
62 $this->dieWithError( 'apierror-revdel-mutuallyexclusive', 'badparams' );
63 } elseif ( !$hide && !$show ) {
64 $this->dieWithError( 'apierror-revdel-paramneeded', 'badparams' );
67 'content' => RevisionDeleter
::getRevdelConstant( $params['type'] ),
68 'comment' => RevisionRecord
::DELETED_COMMENT
,
69 'user' => RevisionRecord
::DELETED_USER
,
72 foreach ( $bits as $key => $bit ) {
73 if ( in_array( $key, $hide ) ) {
75 } elseif ( in_array( $key, $show ) ) {
82 if ( $params['suppress'] === 'yes' ) {
83 $this->checkUserRightsAny( 'suppressrevision' );
84 $bitfield[RevisionRecord
::DELETED_RESTRICTED
] = 1;
85 } elseif ( $params['suppress'] === 'no' ) {
86 $bitfield[RevisionRecord
::DELETED_RESTRICTED
] = 0;
88 $bitfield[RevisionRecord
::DELETED_RESTRICTED
] = -1;
92 if ( $params['target'] ) {
93 $targetObj = Title
::newFromText( $params['target'] );
95 $targetObj = RevisionDeleter
::suggestTarget( $params['type'], $targetObj, $params['ids'] );
96 if ( $targetObj === null ) {
97 $this->dieWithError( [ 'apierror-revdel-needtarget' ], 'needtarget' );
100 $list = RevisionDeleter
::createList(
101 $params['type'], $this->getContext(), $targetObj, $params['ids']
103 $status = $list->setVisibility( [
104 'value' => $bitfield,
105 'comment' => $params['reason'],
106 'perItemStatus' => true,
107 'tags' => $params['tags']
110 $result = $this->getResult();
111 $data = $this->extractStatusInfo( $status );
112 $data['target'] = $targetObj->getFullText();
115 foreach ( $status->itemStatuses
as $id => $s ) {
116 $data['items'][$id] = $this->extractStatusInfo( $s );
117 $data['items'][$id]['id'] = $id;
120 $list->reloadFromMaster();
121 for ( $item = $list->reset(); $list->current(); $item = $list->next() ) {
122 $data['items'][$item->getId()] +
= $item->getApiData( $this->getResult() );
125 $data['items'] = array_values( $data['items'] );
126 ApiResult
::setIndexedTagName( $data['items'], 'i' );
127 $result->addValue( null, $this->getModuleName(), $data );
130 private function extractStatusInfo( $status ) {
132 'status' => $status->isOK() ?
'Success' : 'Fail',
135 $errors = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
137 $ret['errors'] = $errors;
139 $warnings = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
141 $ret['warnings'] = $warnings;
147 public function mustBePosted() {
151 public function isWriteMode() {
155 public function getAllowedParams() {
158 ApiBase
::PARAM_TYPE
=> RevisionDeleter
::getTypes(),
159 ApiBase
::PARAM_REQUIRED
=> true
163 ApiBase
::PARAM_ISMULTI
=> true,
164 ApiBase
::PARAM_REQUIRED
=> true
167 ApiBase
::PARAM_TYPE
=> [ 'content', 'comment', 'user' ],
168 ApiBase
::PARAM_ISMULTI
=> true,
171 ApiBase
::PARAM_TYPE
=> [ 'content', 'comment', 'user' ],
172 ApiBase
::PARAM_ISMULTI
=> true,
175 ApiBase
::PARAM_TYPE
=> [ 'yes', 'no', 'nochange' ],
176 ApiBase
::PARAM_DFLT
=> 'nochange',
180 ApiBase
::PARAM_TYPE
=> 'tags',
181 ApiBase
::PARAM_ISMULTI
=> true,
186 public function needsToken() {
190 protected function getExamplesMessages() {
192 'action=revisiondelete&target=Main%20Page&type=revision&ids=12345&' .
193 'hide=content&token=123ABC'
194 => 'apihelp-revisiondelete-example-revision',
195 'action=revisiondelete&type=logging&ids=67890&hide=content|comment|user&' .
196 'reason=BLP%20violation&token=123ABC'
197 => 'apihelp-revisiondelete-example-log',
201 public function getHelpUrls() {
202 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Revisiondelete';