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 if ( $user->isBlocked() ) {
42 $this->dieBlocked( $user->getBlock() );
45 if ( !$params['ids'] ) {
46 $this->dieWithError( [ 'apierror-paramempty', 'ids' ], 'paramempty_ids' );
49 // Check if user can add tags
50 if ( $params['tags'] ) {
51 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $user );
52 if ( !$ableToTag->isOK() ) {
53 $this->dieStatus( $ableToTag );
57 $hide = $params['hide'] ?
: [];
58 $show = $params['show'] ?
: [];
59 if ( array_intersect( $hide, $show ) ) {
60 $this->dieWithError( 'apierror-revdel-mutuallyexclusive', 'badparams' );
61 } elseif ( !$hide && !$show ) {
62 $this->dieWithError( 'apierror-revdel-paramneeded', 'badparams' );
65 'content' => RevisionDeleter
::getRevdelConstant( $params['type'] ),
66 'comment' => RevisionRecord
::DELETED_COMMENT
,
67 'user' => RevisionRecord
::DELETED_USER
,
70 foreach ( $bits as $key => $bit ) {
71 if ( in_array( $key, $hide ) ) {
73 } elseif ( in_array( $key, $show ) ) {
80 if ( $params['suppress'] === 'yes' ) {
81 $this->checkUserRightsAny( 'suppressrevision' );
82 $bitfield[RevisionRecord
::DELETED_RESTRICTED
] = 1;
83 } elseif ( $params['suppress'] === 'no' ) {
84 $bitfield[RevisionRecord
::DELETED_RESTRICTED
] = 0;
86 $bitfield[RevisionRecord
::DELETED_RESTRICTED
] = -1;
90 if ( $params['target'] ) {
91 $targetObj = Title
::newFromText( $params['target'] );
93 $targetObj = RevisionDeleter
::suggestTarget( $params['type'], $targetObj, $params['ids'] );
94 if ( $targetObj === null ) {
95 $this->dieWithError( [ 'apierror-revdel-needtarget' ], 'needtarget' );
98 $list = RevisionDeleter
::createList(
99 $params['type'], $this->getContext(), $targetObj, $params['ids']
101 $status = $list->setVisibility( [
102 'value' => $bitfield,
103 'comment' => $params['reason'],
104 'perItemStatus' => true,
105 'tags' => $params['tags']
108 $result = $this->getResult();
109 $data = $this->extractStatusInfo( $status );
110 $data['target'] = $targetObj->getFullText();
113 foreach ( $status->itemStatuses
as $id => $s ) {
114 $data['items'][$id] = $this->extractStatusInfo( $s );
115 $data['items'][$id]['id'] = $id;
118 $list->reloadFromMaster();
119 for ( $item = $list->reset(); $list->current(); $item = $list->next() ) {
120 $data['items'][$item->getId()] +
= $item->getApiData( $this->getResult() );
123 $data['items'] = array_values( $data['items'] );
124 ApiResult
::setIndexedTagName( $data['items'], 'i' );
125 $result->addValue( null, $this->getModuleName(), $data );
128 private function extractStatusInfo( $status ) {
130 'status' => $status->isOK() ?
'Success' : 'Fail',
133 $errors = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
135 $ret['errors'] = $errors;
137 $warnings = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
139 $ret['warnings'] = $warnings;
145 public function mustBePosted() {
149 public function isWriteMode() {
153 public function getAllowedParams() {
156 ApiBase
::PARAM_TYPE
=> RevisionDeleter
::getTypes(),
157 ApiBase
::PARAM_REQUIRED
=> true
161 ApiBase
::PARAM_ISMULTI
=> true,
162 ApiBase
::PARAM_REQUIRED
=> true
165 ApiBase
::PARAM_TYPE
=> [ 'content', 'comment', 'user' ],
166 ApiBase
::PARAM_ISMULTI
=> true,
169 ApiBase
::PARAM_TYPE
=> [ 'content', 'comment', 'user' ],
170 ApiBase
::PARAM_ISMULTI
=> true,
173 ApiBase
::PARAM_TYPE
=> [ 'yes', 'no', 'nochange' ],
174 ApiBase
::PARAM_DFLT
=> 'nochange',
178 ApiBase
::PARAM_TYPE
=> 'tags',
179 ApiBase
::PARAM_ISMULTI
=> true,
184 public function needsToken() {
188 protected function getExamplesMessages() {
190 'action=revisiondelete&target=Main%20Page&type=revision&ids=12345&' .
191 'hide=content&token=123ABC'
192 => 'apihelp-revisiondelete-example-revision',
193 'action=revisiondelete&type=logging&ids=67890&hide=content|comment|user&' .
194 'reason=BLP%20violation&token=123ABC'
195 => 'apihelp-revisiondelete-example-log',
199 public function getHelpUrls() {
200 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Revisiondelete';