Add parameter to API modules to apply change tags to log entries
[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
124 $errors = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
125 if ( $errors ) {
126 $ret['errors'] = $errors;
127 }
128 $warnings = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
129 if ( $warnings ) {
130 $ret['warnings'] = $warnings;
131 }
132
133 return $ret;
134 }
135
136 public function mustBePosted() {
137 return true;
138 }
139
140 public function isWriteMode() {
141 return true;
142 }
143
144 public function getAllowedParams() {
145 return [
146 'type' => [
147 ApiBase::PARAM_TYPE => RevisionDeleter::getTypes(),
148 ApiBase::PARAM_REQUIRED => true
149 ],
150 'target' => null,
151 'ids' => [
152 ApiBase::PARAM_ISMULTI => true,
153 ApiBase::PARAM_REQUIRED => true
154 ],
155 'hide' => [
156 ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
157 ApiBase::PARAM_ISMULTI => true,
158 ],
159 'show' => [
160 ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
161 ApiBase::PARAM_ISMULTI => true,
162 ],
163 'suppress' => [
164 ApiBase::PARAM_TYPE => [ 'yes', 'no', 'nochange' ],
165 ApiBase::PARAM_DFLT => 'nochange',
166 ],
167 'reason' => null,
168 ];
169 }
170
171 public function needsToken() {
172 return 'csrf';
173 }
174
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',
183 ];
184 }
185
186 public function getHelpUrls() {
187 return 'https://www.mediawiki.org/wiki/API:Revisiondelete';
188 }
189 }