Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / api / ApiRevisionDelete.php
blob4896e7e527a880a616b6b2ec6017450c62fe5edc
1 <?php
2 /**
3 * Created on Jun 25, 2013
5 * Copyright © 2013 Brad Jorsch <bjorsch@wikimedia.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
22 * @file
23 * @since 1.23
26 /**
27 * API interface to RevDel. The API equivalent of Special:RevisionDelete.
28 * Requires API write mode to be enabled.
30 * @ingroup API
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 ( count( $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' );
64 $bits = [
65 'content' => RevisionDeleter::getRevdelConstant( $params['type'] ),
66 'comment' => Revision::DELETED_COMMENT,
67 'user' => Revision::DELETED_USER,
69 $bitfield = [];
70 foreach ( $bits as $key => $bit ) {
71 if ( in_array( $key, $hide ) ) {
72 $bitfield[$bit] = 1;
73 } elseif ( in_array( $key, $show ) ) {
74 $bitfield[$bit] = 0;
75 } else {
76 $bitfield[$bit] = -1;
80 if ( $params['suppress'] === 'yes' ) {
81 $this->checkUserRightsAny( 'suppressrevision' );
82 $bitfield[Revision::DELETED_RESTRICTED] = 1;
83 } elseif ( $params['suppress'] === 'no' ) {
84 $bitfield[Revision::DELETED_RESTRICTED] = 0;
85 } else {
86 $bitfield[Revision::DELETED_RESTRICTED] = -1;
89 $targetObj = null;
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']
106 ] );
108 $result = $this->getResult();
109 $data = $this->extractStatusInfo( $status );
110 $data['target'] = $targetObj->getFullText();
111 $data['items'] = [];
113 foreach ( $status->itemStatuses as $id => $s ) {
114 $data['items'][$id] = $this->extractStatusInfo( $s );
115 $data['items'][$id]['id'] = $id;
118 $list->reloadFromMaster();
119 // @codingStandardsIgnoreStart Avoid function calls in a FOR loop test part
120 for ( $item = $list->reset(); $list->current(); $item = $list->next() ) {
121 $data['items'][$item->getId()] += $item->getApiData( $this->getResult() );
123 // @codingStandardsIgnoreEnd
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 ) {
131 $ret = [
132 'status' => $status->isOK() ? 'Success' : 'Fail',
135 $errors = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
136 if ( $errors ) {
137 $ret['errors'] = $errors;
139 $warnings = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
140 if ( $warnings ) {
141 $ret['warnings'] = $warnings;
144 return $ret;
147 public function mustBePosted() {
148 return true;
151 public function isWriteMode() {
152 return true;
155 public function getAllowedParams() {
156 return [
157 'type' => [
158 ApiBase::PARAM_TYPE => RevisionDeleter::getTypes(),
159 ApiBase::PARAM_REQUIRED => true
161 'target' => null,
162 'ids' => [
163 ApiBase::PARAM_ISMULTI => true,
164 ApiBase::PARAM_REQUIRED => true
166 'hide' => [
167 ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
168 ApiBase::PARAM_ISMULTI => true,
170 'show' => [
171 ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
172 ApiBase::PARAM_ISMULTI => true,
174 'suppress' => [
175 ApiBase::PARAM_TYPE => [ 'yes', 'no', 'nochange' ],
176 ApiBase::PARAM_DFLT => 'nochange',
178 'reason' => null,
179 'tags' => [
180 ApiBase::PARAM_TYPE => 'tags',
181 ApiBase::PARAM_ISMULTI => true,
186 public function needsToken() {
187 return 'csrf';
190 protected function getExamplesMessages() {
191 return [
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/API:Revisiondelete';