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
25 * API interface to RevDel. The API equivalent of Special:RevisionDelete.
26 * Requires API write mode to be enabled.
30 class ApiRevisionDelete
extends ApiBase
{
32 public function execute() {
33 $this->useTransactionalTimeLimit();
35 $params = $this->extractRequestParams();
36 $user = $this->getUser();
37 $this->checkUserRightsAny( RevisionDeleter
::getRestriction( $params['type'] ) );
39 if ( $user->isBlocked() ) {
40 $this->dieBlocked( $user->getBlock() );
43 if ( !$params['ids'] ) {
44 $this->dieWithError( [ 'apierror-paramempty', 'ids' ], 'paramempty_ids' );
47 // Check if user can add tags
48 if ( $params['tags'] ) {
49 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $user );
50 if ( !$ableToTag->isOK() ) {
51 $this->dieStatus( $ableToTag );
55 $hide = $params['hide'] ?
: [];
56 $show = $params['show'] ?
: [];
57 if ( array_intersect( $hide, $show ) ) {
58 $this->dieWithError( 'apierror-revdel-mutuallyexclusive', 'badparams' );
59 } elseif ( !$hide && !$show ) {
60 $this->dieWithError( 'apierror-revdel-paramneeded', 'badparams' );
63 'content' => RevisionDeleter
::getRevdelConstant( $params['type'] ),
64 'comment' => Revision
::DELETED_COMMENT
,
65 'user' => Revision
::DELETED_USER
,
68 foreach ( $bits as $key => $bit ) {
69 if ( in_array( $key, $hide ) ) {
71 } elseif ( in_array( $key, $show ) ) {
78 if ( $params['suppress'] === 'yes' ) {
79 $this->checkUserRightsAny( 'suppressrevision' );
80 $bitfield[Revision
::DELETED_RESTRICTED
] = 1;
81 } elseif ( $params['suppress'] === 'no' ) {
82 $bitfield[Revision
::DELETED_RESTRICTED
] = 0;
84 $bitfield[Revision
::DELETED_RESTRICTED
] = -1;
88 if ( $params['target'] ) {
89 $targetObj = Title
::newFromText( $params['target'] );
91 $targetObj = RevisionDeleter
::suggestTarget( $params['type'], $targetObj, $params['ids'] );
92 if ( $targetObj === null ) {
93 $this->dieWithError( [ 'apierror-revdel-needtarget' ], 'needtarget' );
96 $list = RevisionDeleter
::createList(
97 $params['type'], $this->getContext(), $targetObj, $params['ids']
99 $status = $list->setVisibility( [
100 'value' => $bitfield,
101 'comment' => $params['reason'],
102 'perItemStatus' => true,
103 'tags' => $params['tags']
106 $result = $this->getResult();
107 $data = $this->extractStatusInfo( $status );
108 $data['target'] = $targetObj->getFullText();
111 foreach ( $status->itemStatuses
as $id => $s ) {
112 $data['items'][$id] = $this->extractStatusInfo( $s );
113 $data['items'][$id]['id'] = $id;
116 $list->reloadFromMaster();
117 // phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall
118 for ( $item = $list->reset(); $list->current(); $item = $list->next() ) {
119 $data['items'][$item->getId()] +
= $item->getApiData( $this->getResult() );
122 $data['items'] = array_values( $data['items'] );
123 ApiResult
::setIndexedTagName( $data['items'], 'i' );
124 $result->addValue( null, $this->getModuleName(), $data );
127 private function extractStatusInfo( $status ) {
129 'status' => $status->isOK() ?
'Success' : 'Fail',
132 $errors = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
134 $ret['errors'] = $errors;
136 $warnings = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
138 $ret['warnings'] = $warnings;
144 public function mustBePosted() {
148 public function isWriteMode() {
152 public function getAllowedParams() {
155 ApiBase
::PARAM_TYPE
=> RevisionDeleter
::getTypes(),
156 ApiBase
::PARAM_REQUIRED
=> true
160 ApiBase
::PARAM_ISMULTI
=> true,
161 ApiBase
::PARAM_REQUIRED
=> true
164 ApiBase
::PARAM_TYPE
=> [ 'content', 'comment', 'user' ],
165 ApiBase
::PARAM_ISMULTI
=> true,
168 ApiBase
::PARAM_TYPE
=> [ 'content', 'comment', 'user' ],
169 ApiBase
::PARAM_ISMULTI
=> true,
172 ApiBase
::PARAM_TYPE
=> [ 'yes', 'no', 'nochange' ],
173 ApiBase
::PARAM_DFLT
=> 'nochange',
177 ApiBase
::PARAM_TYPE
=> 'tags',
178 ApiBase
::PARAM_ISMULTI
=> true,
183 public function needsToken() {
187 protected function getExamplesMessages() {
189 'action=revisiondelete&target=Main%20Page&type=revision&ids=12345&' .
190 'hide=content&token=123ABC'
191 => 'apihelp-revisiondelete-example-revision',
192 'action=revisiondelete&type=logging&ids=67890&hide=content|comment|user&' .
193 'reason=BLP%20violation&token=123ABC'
194 => 'apihelp-revisiondelete-example-log',
198 public function getHelpUrls() {
199 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Revisiondelete';