Merge "output: Narrow Title type hint to LinkTarget"
[lhc/web/wiklou.git] / includes / api / ApiRevisionDelete.php
1 <?php
2 /**
3 * Copyright © 2013 Wikimedia Foundation and contributors
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @since 1.23
22 */
23
24 use MediaWiki\Revision\RevisionRecord;
25
26 /**
27 * API interface to RevDel. The API equivalent of Special:RevisionDelete.
28 * Requires API write mode to be enabled.
29 *
30 * @ingroup API
31 */
32 class ApiRevisionDelete extends ApiBase {
33
34 public function execute() {
35 $this->useTransactionalTimeLimit();
36
37 $params = $this->extractRequestParams();
38 $user = $this->getUser();
39 $this->checkUserRightsAny( RevisionDeleter::getRestriction( $params['type'] ) );
40
41 if ( !$params['ids'] ) {
42 $this->dieWithError( [ 'apierror-paramempty', 'ids' ], 'paramempty_ids' );
43 }
44
45 // Check if user can add tags
46 if ( $params['tags'] ) {
47 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
48 if ( !$ableToTag->isOK() ) {
49 $this->dieStatus( $ableToTag );
50 }
51 }
52
53 $hide = $params['hide'] ?: [];
54 $show = $params['show'] ?: [];
55 if ( array_intersect( $hide, $show ) ) {
56 $this->dieWithError( 'apierror-revdel-mutuallyexclusive', 'badparams' );
57 } elseif ( !$hide && !$show ) {
58 $this->dieWithError( 'apierror-revdel-paramneeded', 'badparams' );
59 }
60 $bits = [
61 'content' => RevisionDeleter::getRevdelConstant( $params['type'] ),
62 'comment' => RevisionRecord::DELETED_COMMENT,
63 'user' => RevisionRecord::DELETED_USER,
64 ];
65 $bitfield = [];
66 foreach ( $bits as $key => $bit ) {
67 if ( in_array( $key, $hide ) ) {
68 $bitfield[$bit] = 1;
69 } elseif ( in_array( $key, $show ) ) {
70 $bitfield[$bit] = 0;
71 } else {
72 $bitfield[$bit] = -1;
73 }
74 }
75
76 if ( $params['suppress'] === 'yes' ) {
77 $this->checkUserRightsAny( 'suppressrevision' );
78 $bitfield[RevisionRecord::DELETED_RESTRICTED] = 1;
79 } elseif ( $params['suppress'] === 'no' ) {
80 $bitfield[RevisionRecord::DELETED_RESTRICTED] = 0;
81 } else {
82 $bitfield[RevisionRecord::DELETED_RESTRICTED] = -1;
83 }
84
85 $targetObj = null;
86 if ( $params['target'] ) {
87 $targetObj = Title::newFromText( $params['target'] );
88 }
89 $targetObj = RevisionDeleter::suggestTarget( $params['type'], $targetObj, $params['ids'] );
90 if ( $targetObj === null ) {
91 $this->dieWithError( [ 'apierror-revdel-needtarget' ], 'needtarget' );
92 }
93
94 if ( $this->getPermissionManager()->isBlockedFrom( $user, $targetObj ) ) {
95 $this->dieBlocked( $user->getBlock() );
96 }
97
98 $list = RevisionDeleter::createList(
99 $params['type'], $this->getContext(), $targetObj, $params['ids']
100 );
101 $status = $list->setVisibility( [
102 'value' => $bitfield,
103 'comment' => $params['reason'],
104 'perItemStatus' => true,
105 'tags' => $params['tags']
106 ] );
107
108 $result = $this->getResult();
109 $data = $this->extractStatusInfo( $status );
110 $data['target'] = $targetObj->getFullText();
111 $data['items'] = [];
112
113 foreach ( $status->itemStatuses as $id => $s ) {
114 $data['items'][$id] = $this->extractStatusInfo( $s );
115 $data['items'][$id]['id'] = $id;
116 }
117
118 $list->reloadFromMaster();
119 for ( $item = $list->reset(); $list->current(); $item = $list->next() ) {
120 $data['items'][$item->getId()] += $item->getApiData( $this->getResult() );
121 }
122
123 $data['items'] = array_values( $data['items'] );
124 ApiResult::setIndexedTagName( $data['items'], 'i' );
125 $result->addValue( null, $this->getModuleName(), $data );
126 }
127
128 private function extractStatusInfo( $status ) {
129 $ret = [
130 'status' => $status->isOK() ? 'Success' : 'Fail',
131 ];
132
133 $errors = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
134 if ( $errors ) {
135 $ret['errors'] = $errors;
136 }
137 $warnings = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
138 if ( $warnings ) {
139 $ret['warnings'] = $warnings;
140 }
141
142 return $ret;
143 }
144
145 public function mustBePosted() {
146 return true;
147 }
148
149 public function isWriteMode() {
150 return true;
151 }
152
153 public function getAllowedParams() {
154 return [
155 'type' => [
156 ApiBase::PARAM_TYPE => RevisionDeleter::getTypes(),
157 ApiBase::PARAM_REQUIRED => true
158 ],
159 'target' => null,
160 'ids' => [
161 ApiBase::PARAM_ISMULTI => true,
162 ApiBase::PARAM_REQUIRED => true
163 ],
164 'hide' => [
165 ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
166 ApiBase::PARAM_ISMULTI => true,
167 ],
168 'show' => [
169 ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
170 ApiBase::PARAM_ISMULTI => true,
171 ],
172 'suppress' => [
173 ApiBase::PARAM_TYPE => [ 'yes', 'no', 'nochange' ],
174 ApiBase::PARAM_DFLT => 'nochange',
175 ],
176 'reason' => null,
177 'tags' => [
178 ApiBase::PARAM_TYPE => 'tags',
179 ApiBase::PARAM_ISMULTI => true,
180 ],
181 ];
182 }
183
184 public function needsToken() {
185 return 'csrf';
186 }
187
188 protected function getExamplesMessages() {
189 return [
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',
196 ];
197 }
198
199 public function getHelpUrls() {
200 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Revisiondelete';
201 }
202 }