0251bdbddd13adf0c2c93b91693e2feb90425bad
[lhc/web/wiklou.git] / includes / api / ApiRevisionDelete.php
1 <?php
2 /**
3 * Created on Jun 25, 2013
4 *
5 * Copyright © 2013 Brad Jorsch <bjorsch@wikimedia.org>
6 *
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.
11 *
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.
16 *
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
21 *
22 * @file
23 * @since 1.23
24 */
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 ( $user->isBlocked() ) {
42 $this->dieBlocked( $user->getBlock() );
43 }
44
45 if ( !$params['ids'] ) {
46 $this->dieWithError( [ 'apierror-paramempty', 'ids' ], 'paramempty_ids' );
47 }
48
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' );
55 }
56 $bits = [
57 'content' => RevisionDeleter::getRevdelConstant( $params['type'] ),
58 'comment' => Revision::DELETED_COMMENT,
59 'user' => Revision::DELETED_USER,
60 ];
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;
69 }
70 }
71
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;
79 }
80
81 $targetObj = null;
82 if ( $params['target'] ) {
83 $targetObj = Title::newFromText( $params['target'] );
84 }
85 $targetObj = RevisionDeleter::suggestTarget( $params['type'], $targetObj, $params['ids'] );
86 if ( $targetObj === null ) {
87 $this->dieWithError( [ 'apierror-revdel-needtarget' ], 'needtarget' );
88 }
89
90 $list = RevisionDeleter::createList(
91 $params['type'], $this->getContext(), $targetObj, $params['ids']
92 );
93 $status = $list->setVisibility(
94 [ 'value' => $bitfield, 'comment' => $params['reason'], 'perItemStatus' => true ]
95 );
96
97 $result = $this->getResult();
98 $data = $this->extractStatusInfo( $status );
99 $data['target'] = $targetObj->getFullText();
100 $data['items'] = [];
101
102 foreach ( $status->itemStatuses as $id => $s ) {
103 $data['items'][$id] = $this->extractStatusInfo( $s );
104 $data['items'][$id]['id'] = $id;
105 }
106
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() );
111 }
112 // @codingStandardsIgnoreEnd
113
114 $data['items'] = array_values( $data['items'] );
115 ApiResult::setIndexedTagName( $data['items'], 'i' );
116 $result->addValue( null, $this->getModuleName(), $data );
117 }
118
119 private function extractStatusInfo( $status ) {
120 $ret = [
121 'status' => $status->isOK() ? 'Success' : 'Fail',
122 ];
123 $errors = $this->formatStatusMessages( $status->getErrorsByType( 'error' ) );
124 if ( $errors ) {
125 ApiResult::setIndexedTagName( $errors, 'e' );
126 $ret['errors'] = $errors;
127 }
128 $warnings = $this->formatStatusMessages( $status->getErrorsByType( 'warning' ) );
129 if ( $warnings ) {
130 ApiResult::setIndexedTagName( $warnings, 'w' );
131 $ret['warnings'] = $warnings;
132 }
133
134 return $ret;
135 }
136
137 private function formatStatusMessages( $messages ) {
138 if ( !$messages ) {
139 return [];
140 }
141 $ret = [];
142 foreach ( $messages as $m ) {
143 if ( $m['message'] instanceof Message ) {
144 $msg = $m['message'];
145 $message = [ 'message' => $msg->getKey() ];
146 if ( $msg->getParams() ) {
147 $message['params'] = $msg->getParams();
148 ApiResult::setIndexedTagName( $message['params'], 'p' );
149 }
150 } else {
151 $message = [ 'message' => $m['message'] ];
152 $msg = wfMessage( $m['message'] );
153 if ( isset( $m['params'] ) ) {
154 $message['params'] = $m['params'];
155 ApiResult::setIndexedTagName( $message['params'], 'p' );
156 $msg->params( $m['params'] );
157 }
158 }
159 $message['rendered'] = $msg->useDatabase( false )->inLanguage( 'en' )->plain();
160 $ret[] = $message;
161 }
162
163 return $ret;
164 }
165
166 public function mustBePosted() {
167 return true;
168 }
169
170 public function isWriteMode() {
171 return true;
172 }
173
174 public function getAllowedParams() {
175 return [
176 'type' => [
177 ApiBase::PARAM_TYPE => RevisionDeleter::getTypes(),
178 ApiBase::PARAM_REQUIRED => true
179 ],
180 'target' => null,
181 'ids' => [
182 ApiBase::PARAM_ISMULTI => true,
183 ApiBase::PARAM_REQUIRED => true
184 ],
185 'hide' => [
186 ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
187 ApiBase::PARAM_ISMULTI => true,
188 ],
189 'show' => [
190 ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
191 ApiBase::PARAM_ISMULTI => true,
192 ],
193 'suppress' => [
194 ApiBase::PARAM_TYPE => [ 'yes', 'no', 'nochange' ],
195 ApiBase::PARAM_DFLT => 'nochange',
196 ],
197 'reason' => null,
198 ];
199 }
200
201 public function needsToken() {
202 return 'csrf';
203 }
204
205 protected function getExamplesMessages() {
206 return [
207 'action=revisiondelete&target=Main%20Page&type=revision&ids=12345&' .
208 'hide=content&token=123ABC'
209 => 'apihelp-revisiondelete-example-revision',
210 'action=revisiondelete&type=logging&ids=67890&hide=content|comment|user&' .
211 'reason=BLP%20violation&token=123ABC'
212 => 'apihelp-revisiondelete-example-log',
213 ];
214 }
215
216 public function getHelpUrls() {
217 return 'https://www.mediawiki.org/wiki/API:Revisiondelete';
218 }
219 }