Merge "Fix CodeSniffer errors and warnings"
[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 $params = $this->extractRequestParams();
36 $user = $this->getUser();
37
38 if ( !$user->isAllowed( RevisionDeleter::getRestriction( $params['type'] ) ) ) {
39 $this->dieUsageMsg( 'badaccess-group0' );
40 }
41
42 if ( !$params['ids'] ) {
43 $this->dieUsage( "At least one value is required for 'ids'", 'badparams' );
44 }
45
46 $hide = $params['hide'] ?: array();
47 $show = $params['show'] ?: array();
48 if ( array_intersect( $hide, $show ) ) {
49 $this->dieUsage( "Mutually exclusive values for 'hide' and 'show'", 'badparams' );
50 } elseif ( !$hide && !$show ) {
51 $this->dieUsage( "At least one value is required for 'hide' or 'show'", 'badparams' );
52 }
53 $bits = array(
54 'content' => RevisionDeleter::getRevdelConstant( $params['type'] ),
55 'comment' => Revision::DELETED_COMMENT,
56 'user' => Revision::DELETED_USER,
57 );
58 $bitfield = array();
59 foreach ( $bits as $key => $bit ) {
60 if ( in_array( $key, $hide ) ) {
61 $bitfield[$bit] = 1;
62 } elseif ( in_array( $key, $show ) ) {
63 $bitfield[$bit] = 0;
64 } else {
65 $bitfield[$bit] = -1;
66 }
67 }
68
69 if ( $params['suppress'] === 'yes' ) {
70 if ( !$user->isAllowed( 'suppressrevision' ) ) {
71 $this->dieUsageMsg( 'badaccess-group0' );
72 }
73 $bitfield[Revision::DELETED_RESTRICTED] = 1;
74 } elseif ( $params['suppress'] === 'no' ) {
75 $bitfield[Revision::DELETED_RESTRICTED] = 0;
76 } else {
77 $bitfield[Revision::DELETED_RESTRICTED] = -1;
78 }
79
80 $targetObj = null;
81 if ( $params['target'] ) {
82 $targetObj = Title::newFromText( $params['target'] );
83 }
84 $targetObj = RevisionDeleter::suggestTarget( $params['type'], $targetObj, $params['ids'] );
85 if ( $targetObj === null ) {
86 $this->dieUsage( 'A target title is required for this RevDel type', 'needtarget' );
87 }
88
89 $list = RevisionDeleter::createList(
90 $params['type'], $this->getContext(), $targetObj, $params['ids']
91 );
92 $status = $list->setVisibility(
93 array( 'value' => $bitfield, 'comment' => $params['reason'], 'perItemStatus' => true )
94 );
95
96 $result = $this->getResult();
97 $data = $this->extractStatusInfo( $status );
98 $data['target'] = $targetObj->getFullText();
99 $data['items'] = array();
100
101 foreach ( $status->itemStatuses as $id => $s ) {
102 $data['items'][$id] = $this->extractStatusInfo( $s );
103 $data['items'][$id]['id'] = $id;
104 }
105
106 $list->reloadFromMaster();
107 // @codingStandardsIgnoreStart Avoid function calls in a FOR loop test part
108 for ( $item = $list->reset(); $list->current(); $item = $list->next() ) {
109 $data['items'][$item->getId()] += $item->getApiData( $this->getResult() );
110 }
111 // @codingStandardsIgnoreEnd
112
113 $data['items'] = array_values( $data['items'] );
114 $result->setIndexedTagName( $data['items'], 'i' );
115 $result->addValue( null, $this->getModuleName(), $data );
116 }
117
118 private function extractStatusInfo( $status ) {
119 $ret = array(
120 'status' => $status->isOK() ? 'Success' : 'Fail',
121 );
122 $errors = $this->formatStatusMessages( $status->getErrorsByType( 'error' ) );
123 if ( $errors ) {
124 $this->getResult()->setIndexedTagName( $errors, 'e' );
125 $ret['errors'] = $errors;
126 }
127 $warnings = $this->formatStatusMessages( $status->getErrorsByType( 'warning' ) );
128 if ( $warnings ) {
129 $this->getResult()->setIndexedTagName( $warnings, 'w' );
130 $ret['warnings'] = $warnings;
131 }
132 return $ret;
133 }
134
135 private function formatStatusMessages( $messages ) {
136 if ( !$messages ) {
137 return array();
138 }
139 $result = $this->getResult();
140 $ret = array();
141 foreach ( $messages as $m ) {
142 $message = array();
143 if ( $m['message'] instanceof Message ) {
144 $msg = $m['message'];
145 $message = array( 'message' => $msg->getKey() );
146 if ( $msg->getParams() ) {
147 $message['params'] = $msg->getParams();
148 $result->setIndexedTagName( $message['params'], 'p' );
149 }
150 } else {
151 $message = array( 'message' => $m['message'] );
152 $msg = wfMessage( $m['message'] );
153 if ( isset( $m['params'] ) ) {
154 $message['params'] = $m['params'];
155 $result->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 return $ret;
163 }
164
165 public function mustBePosted() {
166 return true;
167 }
168
169 public function isWriteMode() {
170 return true;
171 }
172
173 public function getAllowedParams() {
174 return array(
175 'type' => array(
176 ApiBase::PARAM_TYPE => RevisionDeleter::getTypes(),
177 ApiBase::PARAM_REQUIRED => true
178 ),
179 'target' => null,
180 'ids' => array(
181 ApiBase::PARAM_ISMULTI => true,
182 ApiBase::PARAM_REQUIRED => true
183 ),
184 'hide' => array(
185 ApiBase::PARAM_TYPE => array( 'content', 'comment', 'user' ),
186 ApiBase::PARAM_ISMULTI => true,
187 ),
188 'show' => array(
189 ApiBase::PARAM_TYPE => array( 'content', 'comment', 'user' ),
190 ApiBase::PARAM_ISMULTI => true,
191 ),
192 'suppress' => array(
193 ApiBase::PARAM_TYPE => array( 'yes', 'no', 'nochange' ),
194 ApiBase::PARAM_DFLT => 'nochange',
195 ),
196 'token' => array(
197 ApiBase::PARAM_TYPE => 'string',
198 ApiBase::PARAM_REQUIRED => true
199 ),
200 'reason' => null,
201 );
202 }
203
204 public function getParamDescription() {
205 return array(
206 'type' => 'Type of revision deletion being performed',
207 'target' => 'Page title for the revision deletion, if required for the type',
208 'ids' => 'Identifiers for the revisions to be deleted',
209 'hide' => 'What to hide for each revision',
210 'show' => 'What to unhide for each revision',
211 'suppress' => 'Whether to suppress data from administrators as well as others',
212 'token' => 'A delete token previously retrieved through action=tokens',
213 'reason' => 'Reason for the deletion/undeletion',
214 );
215 }
216
217 public function getDescription() {
218 return 'Delete/undelete revisions';
219 }
220
221 public function getPossibleErrors() {
222 return array_merge( parent::getPossibleErrors(),
223 array(
224 'needtarget' => 'A target title is required for this RevDel type',
225 'badparams' => 'Bad value for some parameter',
226 )
227 );
228 }
229
230 public function needsToken() {
231 return true;
232 }
233
234 public function getTokenSalt() {
235 return '';
236 }
237
238 public function getExamples() {
239 return array(
240 'api.php?action=revisiondelete&target=Main%20Page&type=revision&ids=12345&' .
241 'hide=content&token=123ABC'
242 => 'Hide content for revision 12345 on the Main Page',
243 'api.php?action=revisiondelete&type=logging&ids=67890&hide=content|comment|user&' .
244 'reason=BLP%20violation&token=123ABC'
245 => 'Hide all data on log entry 67890 with the reason "BLP violation"',
246 );
247 }
248
249 public function getHelpUrls() {
250 return 'https://www.mediawiki.org/wiki/API:Revisiondelete';
251 }
252 }