Merge "Special:Newpages feed now shows first revision instead of latest revision"
[lhc/web/wiklou.git] / includes / api / ApiRevisionDelete.php
1 <?php
2 /**
3 * Created on Jun 25, 2013
4 *
5 * Copyright © 2013 Wikimedia Foundation and contributors
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 // Check if user can add tags
50 if ( count( $params['tags'] ) ) {
51 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
52 if ( !$ableToTag->isOK() ) {
53 $this->dieStatus( $ableToTag );
54 }
55 }
56
57 $hide = $params['hide'] ?: [];
58 $show = $params['show'] ?: [];
59 if ( array_intersect( $hide, $show ) ) {
60 $this->dieWithError( 'apierror-revdel-mutuallyexclusive', 'badparams' );
61 } elseif ( !$hide && !$show ) {
62 $this->dieWithError( 'apierror-revdel-paramneeded', 'badparams' );
63 }
64 $bits = [
65 'content' => RevisionDeleter::getRevdelConstant( $params['type'] ),
66 'comment' => Revision::DELETED_COMMENT,
67 'user' => Revision::DELETED_USER,
68 ];
69 $bitfield = [];
70 foreach ( $bits as $key => $bit ) {
71 if ( in_array( $key, $hide ) ) {
72 $bitfield[$bit] = 1;
73 } elseif ( in_array( $key, $show ) ) {
74 $bitfield[$bit] = 0;
75 } else {
76 $bitfield[$bit] = -1;
77 }
78 }
79
80 if ( $params['suppress'] === 'yes' ) {
81 $this->checkUserRightsAny( 'suppressrevision' );
82 $bitfield[Revision::DELETED_RESTRICTED] = 1;
83 } elseif ( $params['suppress'] === 'no' ) {
84 $bitfield[Revision::DELETED_RESTRICTED] = 0;
85 } else {
86 $bitfield[Revision::DELETED_RESTRICTED] = -1;
87 }
88
89 $targetObj = null;
90 if ( $params['target'] ) {
91 $targetObj = Title::newFromText( $params['target'] );
92 }
93 $targetObj = RevisionDeleter::suggestTarget( $params['type'], $targetObj, $params['ids'] );
94 if ( $targetObj === null ) {
95 $this->dieWithError( [ 'apierror-revdel-needtarget' ], 'needtarget' );
96 }
97
98 $list = RevisionDeleter::createList(
99 $params['type'], $this->getContext(), $targetObj, $params['ids']
100 );
101 $status = $list->setVisibility( [
102 'value' => $bitfield,
103 'comment' => $params['reason'],
104 'perItemStatus' => true,
105 'tags' => $params['tags']
106 ] );
107
108 $result = $this->getResult();
109 $data = $this->extractStatusInfo( $status );
110 $data['target'] = $targetObj->getFullText();
111 $data['items'] = [];
112
113 foreach ( $status->itemStatuses as $id => $s ) {
114 $data['items'][$id] = $this->extractStatusInfo( $s );
115 $data['items'][$id]['id'] = $id;
116 }
117
118 $list->reloadFromMaster();
119 // @codingStandardsIgnoreStart Avoid function calls in a FOR loop test part
120 for ( $item = $list->reset(); $list->current(); $item = $list->next() ) {
121 $data['items'][$item->getId()] += $item->getApiData( $this->getResult() );
122 }
123 // @codingStandardsIgnoreEnd
124
125 $data['items'] = array_values( $data['items'] );
126 ApiResult::setIndexedTagName( $data['items'], 'i' );
127 $result->addValue( null, $this->getModuleName(), $data );
128 }
129
130 private function extractStatusInfo( $status ) {
131 $ret = [
132 'status' => $status->isOK() ? 'Success' : 'Fail',
133 ];
134
135 $errors = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
136 if ( $errors ) {
137 $ret['errors'] = $errors;
138 }
139 $warnings = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
140 if ( $warnings ) {
141 $ret['warnings'] = $warnings;
142 }
143
144 return $ret;
145 }
146
147 public function mustBePosted() {
148 return true;
149 }
150
151 public function isWriteMode() {
152 return true;
153 }
154
155 public function getAllowedParams() {
156 return [
157 'type' => [
158 ApiBase::PARAM_TYPE => RevisionDeleter::getTypes(),
159 ApiBase::PARAM_REQUIRED => true
160 ],
161 'target' => null,
162 'ids' => [
163 ApiBase::PARAM_ISMULTI => true,
164 ApiBase::PARAM_REQUIRED => true
165 ],
166 'hide' => [
167 ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
168 ApiBase::PARAM_ISMULTI => true,
169 ],
170 'show' => [
171 ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
172 ApiBase::PARAM_ISMULTI => true,
173 ],
174 'suppress' => [
175 ApiBase::PARAM_TYPE => [ 'yes', 'no', 'nochange' ],
176 ApiBase::PARAM_DFLT => 'nochange',
177 ],
178 'reason' => null,
179 'tags' => [
180 ApiBase::PARAM_TYPE => 'tags',
181 ApiBase::PARAM_ISMULTI => true,
182 ],
183 ];
184 }
185
186 public function needsToken() {
187 return 'csrf';
188 }
189
190 protected function getExamplesMessages() {
191 return [
192 'action=revisiondelete&target=Main%20Page&type=revision&ids=12345&' .
193 'hide=content&token=123ABC'
194 => 'apihelp-revisiondelete-example-revision',
195 'action=revisiondelete&type=logging&ids=67890&hide=content|comment|user&' .
196 'reason=BLP%20violation&token=123ABC'
197 => 'apihelp-revisiondelete-example-log',
198 ];
199 }
200
201 public function getHelpUrls() {
202 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Revisiondelete';
203 }
204 }