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