bb5c145140fe66ce2837c5831859821ef6977720
[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
40 if ( !$user->isAllowed( RevisionDeleter::getRestriction( $params['type'] ) ) ) {
41 $this->dieUsageMsg( 'badaccess-group0' );
42 }
43
44 if ( !$params['ids'] ) {
45 $this->dieUsage( "At least one value is required for 'ids'", 'badparams' );
46 }
47
48 $hide = $params['hide'] ?: array();
49 $show = $params['show'] ?: array();
50 if ( array_intersect( $hide, $show ) ) {
51 $this->dieUsage( "Mutually exclusive values for 'hide' and 'show'", 'badparams' );
52 } elseif ( !$hide && !$show ) {
53 $this->dieUsage( "At least one value is required for 'hide' or 'show'", 'badparams' );
54 }
55 $bits = array(
56 'content' => RevisionDeleter::getRevdelConstant( $params['type'] ),
57 'comment' => Revision::DELETED_COMMENT,
58 'user' => Revision::DELETED_USER,
59 );
60 $bitfield = array();
61 foreach ( $bits as $key => $bit ) {
62 if ( in_array( $key, $hide ) ) {
63 $bitfield[$bit] = 1;
64 } elseif ( in_array( $key, $show ) ) {
65 $bitfield[$bit] = 0;
66 } else {
67 $bitfield[$bit] = -1;
68 }
69 }
70
71 if ( $params['suppress'] === 'yes' ) {
72 if ( !$user->isAllowed( 'suppressrevision' ) ) {
73 $this->dieUsageMsg( 'badaccess-group0' );
74 }
75 $bitfield[Revision::DELETED_RESTRICTED] = 1;
76 } elseif ( $params['suppress'] === 'no' ) {
77 $bitfield[Revision::DELETED_RESTRICTED] = 0;
78 } else {
79 $bitfield[Revision::DELETED_RESTRICTED] = -1;
80 }
81
82 $targetObj = null;
83 if ( $params['target'] ) {
84 $targetObj = Title::newFromText( $params['target'] );
85 }
86 $targetObj = RevisionDeleter::suggestTarget( $params['type'], $targetObj, $params['ids'] );
87 if ( $targetObj === null ) {
88 $this->dieUsage( 'A target title is required for this RevDel type', 'needtarget' );
89 }
90
91 $list = RevisionDeleter::createList(
92 $params['type'], $this->getContext(), $targetObj, $params['ids']
93 );
94 $status = $list->setVisibility(
95 array( 'value' => $bitfield, 'comment' => $params['reason'], 'perItemStatus' => true )
96 );
97
98 $result = $this->getResult();
99 $data = $this->extractStatusInfo( $status );
100 $data['target'] = $targetObj->getFullText();
101 $data['items'] = array();
102
103 foreach ( $status->itemStatuses as $id => $s ) {
104 $data['items'][$id] = $this->extractStatusInfo( $s );
105 $data['items'][$id]['id'] = $id;
106 }
107
108 $list->reloadFromMaster();
109 // @codingStandardsIgnoreStart Avoid function calls in a FOR loop test part
110 for ( $item = $list->reset(); $list->current(); $item = $list->next() ) {
111 $data['items'][$item->getId()] += $item->getApiData( $this->getResult() );
112 }
113 // @codingStandardsIgnoreEnd
114
115 $data['items'] = array_values( $data['items'] );
116 ApiResult::setIndexedTagName( $data['items'], 'i' );
117 $result->addValue( null, $this->getModuleName(), $data );
118 }
119
120 private function extractStatusInfo( $status ) {
121 $ret = array(
122 'status' => $status->isOK() ? 'Success' : 'Fail',
123 );
124 $errors = $this->formatStatusMessages( $status->getErrorsByType( 'error' ) );
125 if ( $errors ) {
126 ApiResult::setIndexedTagName( $errors, 'e' );
127 $ret['errors'] = $errors;
128 }
129 $warnings = $this->formatStatusMessages( $status->getErrorsByType( 'warning' ) );
130 if ( $warnings ) {
131 ApiResult::setIndexedTagName( $warnings, 'w' );
132 $ret['warnings'] = $warnings;
133 }
134
135 return $ret;
136 }
137
138 private function formatStatusMessages( $messages ) {
139 if ( !$messages ) {
140 return array();
141 }
142 $result = $this->getResult();
143 $ret = array();
144 foreach ( $messages as $m ) {
145 $message = array();
146 if ( $m['message'] instanceof Message ) {
147 $msg = $m['message'];
148 $message = array( 'message' => $msg->getKey() );
149 if ( $msg->getParams() ) {
150 $message['params'] = $msg->getParams();
151 ApiResult::setIndexedTagName( $message['params'], 'p' );
152 }
153 } else {
154 $message = array( 'message' => $m['message'] );
155 $msg = wfMessage( $m['message'] );
156 if ( isset( $m['params'] ) ) {
157 $message['params'] = $m['params'];
158 ApiResult::setIndexedTagName( $message['params'], 'p' );
159 $msg->params( $m['params'] );
160 }
161 }
162 $message['rendered'] = $msg->useDatabase( false )->inLanguage( 'en' )->plain();
163 $ret[] = $message;
164 }
165
166 return $ret;
167 }
168
169 public function mustBePosted() {
170 return true;
171 }
172
173 public function isWriteMode() {
174 return true;
175 }
176
177 public function getAllowedParams() {
178 return array(
179 'type' => array(
180 ApiBase::PARAM_TYPE => RevisionDeleter::getTypes(),
181 ApiBase::PARAM_REQUIRED => true
182 ),
183 'target' => null,
184 'ids' => array(
185 ApiBase::PARAM_ISMULTI => true,
186 ApiBase::PARAM_REQUIRED => true
187 ),
188 'hide' => array(
189 ApiBase::PARAM_TYPE => array( 'content', 'comment', 'user' ),
190 ApiBase::PARAM_ISMULTI => true,
191 ),
192 'show' => array(
193 ApiBase::PARAM_TYPE => array( 'content', 'comment', 'user' ),
194 ApiBase::PARAM_ISMULTI => true,
195 ),
196 'suppress' => array(
197 ApiBase::PARAM_TYPE => array( 'yes', 'no', 'nochange' ),
198 ApiBase::PARAM_DFLT => 'nochange',
199 ),
200 'reason' => null,
201 );
202 }
203
204 public function needsToken() {
205 return 'csrf';
206 }
207
208 protected function getExamplesMessages() {
209 return array(
210 'action=revisiondelete&target=Main%20Page&type=revision&ids=12345&' .
211 'hide=content&token=123ABC'
212 => 'apihelp-revisiondelete-example-revision',
213 'action=revisiondelete&type=logging&ids=67890&hide=content|comment|user&' .
214 'reason=BLP%20violation&token=123ABC'
215 => 'apihelp-revisiondelete-example-log',
216 );
217 }
218
219 public function getHelpUrls() {
220 return 'https://www.mediawiki.org/wiki/API:Revisiondelete';
221 }
222 }