ApiQueryWatchlist: Handle empty wltypes
[mediawiki.git] / includes / api / ApiRevisionDelete.php
blob763aef5f03a11ff5a9500f8a15c45baad6d9111b
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 $hide = $params['hide'] ?: [];
50 $show = $params['show'] ?: [];
51 if ( array_intersect( $hide, $show ) ) {
52 $this->dieWithError( 'apierror-revdel-mutuallyexclusive', 'badparams' );
53 } elseif ( !$hide && !$show ) {
54 $this->dieWithError( 'apierror-revdel-paramneeded', 'badparams' );
56 $bits = [
57 'content' => RevisionDeleter::getRevdelConstant( $params['type'] ),
58 'comment' => Revision::DELETED_COMMENT,
59 'user' => Revision::DELETED_USER,
61 $bitfield = [];
62 foreach ( $bits as $key => $bit ) {
63 if ( in_array( $key, $hide ) ) {
64 $bitfield[$bit] = 1;
65 } elseif ( in_array( $key, $show ) ) {
66 $bitfield[$bit] = 0;
67 } else {
68 $bitfield[$bit] = -1;
72 if ( $params['suppress'] === 'yes' ) {
73 $this->checkUserRightsAny( 'suppressrevision' );
74 $bitfield[Revision::DELETED_RESTRICTED] = 1;
75 } elseif ( $params['suppress'] === 'no' ) {
76 $bitfield[Revision::DELETED_RESTRICTED] = 0;
77 } else {
78 $bitfield[Revision::DELETED_RESTRICTED] = -1;
81 $targetObj = null;
82 if ( $params['target'] ) {
83 $targetObj = Title::newFromText( $params['target'] );
85 $targetObj = RevisionDeleter::suggestTarget( $params['type'], $targetObj, $params['ids'] );
86 if ( $targetObj === null ) {
87 $this->dieWithError( [ 'apierror-revdel-needtarget' ], 'needtarget' );
90 $list = RevisionDeleter::createList(
91 $params['type'], $this->getContext(), $targetObj, $params['ids']
93 $status = $list->setVisibility(
94 [ 'value' => $bitfield, 'comment' => $params['reason'], 'perItemStatus' => true ]
97 $result = $this->getResult();
98 $data = $this->extractStatusInfo( $status );
99 $data['target'] = $targetObj->getFullText();
100 $data['items'] = [];
102 foreach ( $status->itemStatuses as $id => $s ) {
103 $data['items'][$id] = $this->extractStatusInfo( $s );
104 $data['items'][$id]['id'] = $id;
107 $list->reloadFromMaster();
108 // @codingStandardsIgnoreStart Avoid function calls in a FOR loop test part
109 for ( $item = $list->reset(); $list->current(); $item = $list->next() ) {
110 $data['items'][$item->getId()] += $item->getApiData( $this->getResult() );
112 // @codingStandardsIgnoreEnd
114 $data['items'] = array_values( $data['items'] );
115 ApiResult::setIndexedTagName( $data['items'], 'i' );
116 $result->addValue( null, $this->getModuleName(), $data );
119 private function extractStatusInfo( $status ) {
120 $ret = [
121 'status' => $status->isOK() ? 'Success' : 'Fail',
124 $errors = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
125 if ( $errors ) {
126 $ret['errors'] = $errors;
128 $warnings = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
129 if ( $warnings ) {
130 $ret['warnings'] = $warnings;
133 return $ret;
136 public function mustBePosted() {
137 return true;
140 public function isWriteMode() {
141 return true;
144 public function getAllowedParams() {
145 return [
146 'type' => [
147 ApiBase::PARAM_TYPE => RevisionDeleter::getTypes(),
148 ApiBase::PARAM_REQUIRED => true
150 'target' => null,
151 'ids' => [
152 ApiBase::PARAM_ISMULTI => true,
153 ApiBase::PARAM_REQUIRED => true
155 'hide' => [
156 ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
157 ApiBase::PARAM_ISMULTI => true,
159 'show' => [
160 ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
161 ApiBase::PARAM_ISMULTI => true,
163 'suppress' => [
164 ApiBase::PARAM_TYPE => [ 'yes', 'no', 'nochange' ],
165 ApiBase::PARAM_DFLT => 'nochange',
167 'reason' => null,
171 public function needsToken() {
172 return 'csrf';
175 protected function getExamplesMessages() {
176 return [
177 'action=revisiondelete&target=Main%20Page&type=revision&ids=12345&' .
178 'hide=content&token=123ABC'
179 => 'apihelp-revisiondelete-example-revision',
180 'action=revisiondelete&type=logging&ids=67890&hide=content|comment|user&' .
181 'reason=BLP%20violation&token=123ABC'
182 => 'apihelp-revisiondelete-example-log',
186 public function getHelpUrls() {
187 return 'https://www.mediawiki.org/wiki/API:Revisiondelete';