Merge "Import: Handle uploads with sha1 starting with 0 properly"
[mediawiki.git] / includes / api / ApiRevisionDelete.php
blob4db3ca1d89b4827bb8b07223b76248618c3e2c01
1 <?php
2 /**
3 * Created on Jun 25, 2013
5 * Copyright © 2013 Brad Jorsch <bjorsch@wikimedia.org>
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.
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.
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
22 * @file
23 * @since 1.23
26 /**
27 * API interface to RevDel. The API equivalent of Special:RevisionDelete.
28 * Requires API write mode to be enabled.
30 * @ingroup API
32 class ApiRevisionDelete extends ApiBase {
34 public function execute() {
35 $this->useTransactionalTimeLimit();
37 $params = $this->extractRequestParams();
38 $user = $this->getUser();
39 if ( !$user->isAllowed( RevisionDeleter::getRestriction( $params['type'] ) ) ) {
40 $this->dieUsageMsg( 'badaccess-group0' );
43 if ( $user->isBlocked() ) {
44 $this->dieBlocked( $user->getBlock() );
47 if ( !$params['ids'] ) {
48 $this->dieUsage( "At least one value is required for 'ids'", 'badparams' );
51 $hide = $params['hide'] ?: array();
52 $show = $params['show'] ?: array();
53 if ( array_intersect( $hide, $show ) ) {
54 $this->dieUsage( "Mutually exclusive values for 'hide' and 'show'", 'badparams' );
55 } elseif ( !$hide && !$show ) {
56 $this->dieUsage( "At least one value is required for 'hide' or 'show'", 'badparams' );
58 $bits = array(
59 'content' => RevisionDeleter::getRevdelConstant( $params['type'] ),
60 'comment' => Revision::DELETED_COMMENT,
61 'user' => Revision::DELETED_USER,
63 $bitfield = array();
64 foreach ( $bits as $key => $bit ) {
65 if ( in_array( $key, $hide ) ) {
66 $bitfield[$bit] = 1;
67 } elseif ( in_array( $key, $show ) ) {
68 $bitfield[$bit] = 0;
69 } else {
70 $bitfield[$bit] = -1;
74 if ( $params['suppress'] === 'yes' ) {
75 if ( !$user->isAllowed( 'suppressrevision' ) ) {
76 $this->dieUsageMsg( 'badaccess-group0' );
78 $bitfield[Revision::DELETED_RESTRICTED] = 1;
79 } elseif ( $params['suppress'] === 'no' ) {
80 $bitfield[Revision::DELETED_RESTRICTED] = 0;
81 } else {
82 $bitfield[Revision::DELETED_RESTRICTED] = -1;
85 $targetObj = null;
86 if ( $params['target'] ) {
87 $targetObj = Title::newFromText( $params['target'] );
89 $targetObj = RevisionDeleter::suggestTarget( $params['type'], $targetObj, $params['ids'] );
90 if ( $targetObj === null ) {
91 $this->dieUsage( 'A target title is required for this RevDel type', 'needtarget' );
94 $list = RevisionDeleter::createList(
95 $params['type'], $this->getContext(), $targetObj, $params['ids']
97 $status = $list->setVisibility(
98 array( 'value' => $bitfield, 'comment' => $params['reason'], 'perItemStatus' => true )
101 $result = $this->getResult();
102 $data = $this->extractStatusInfo( $status );
103 $data['target'] = $targetObj->getFullText();
104 $data['items'] = array();
106 foreach ( $status->itemStatuses as $id => $s ) {
107 $data['items'][$id] = $this->extractStatusInfo( $s );
108 $data['items'][$id]['id'] = $id;
111 $list->reloadFromMaster();
112 // @codingStandardsIgnoreStart Avoid function calls in a FOR loop test part
113 for ( $item = $list->reset(); $list->current(); $item = $list->next() ) {
114 $data['items'][$item->getId()] += $item->getApiData( $this->getResult() );
116 // @codingStandardsIgnoreEnd
118 $data['items'] = array_values( $data['items'] );
119 ApiResult::setIndexedTagName( $data['items'], 'i' );
120 $result->addValue( null, $this->getModuleName(), $data );
123 private function extractStatusInfo( $status ) {
124 $ret = array(
125 'status' => $status->isOK() ? 'Success' : 'Fail',
127 $errors = $this->formatStatusMessages( $status->getErrorsByType( 'error' ) );
128 if ( $errors ) {
129 ApiResult::setIndexedTagName( $errors, 'e' );
130 $ret['errors'] = $errors;
132 $warnings = $this->formatStatusMessages( $status->getErrorsByType( 'warning' ) );
133 if ( $warnings ) {
134 ApiResult::setIndexedTagName( $warnings, 'w' );
135 $ret['warnings'] = $warnings;
138 return $ret;
141 private function formatStatusMessages( $messages ) {
142 if ( !$messages ) {
143 return array();
145 $ret = array();
146 foreach ( $messages as $m ) {
147 if ( $m['message'] instanceof Message ) {
148 $msg = $m['message'];
149 $message = array( 'message' => $msg->getKey() );
150 if ( $msg->getParams() ) {
151 $message['params'] = $msg->getParams();
152 ApiResult::setIndexedTagName( $message['params'], 'p' );
154 } else {
155 $message = array( 'message' => $m['message'] );
156 $msg = wfMessage( $m['message'] );
157 if ( isset( $m['params'] ) ) {
158 $message['params'] = $m['params'];
159 ApiResult::setIndexedTagName( $message['params'], 'p' );
160 $msg->params( $m['params'] );
163 $message['rendered'] = $msg->useDatabase( false )->inLanguage( 'en' )->plain();
164 $ret[] = $message;
167 return $ret;
170 public function mustBePosted() {
171 return true;
174 public function isWriteMode() {
175 return true;
178 public function getAllowedParams() {
179 return array(
180 'type' => array(
181 ApiBase::PARAM_TYPE => RevisionDeleter::getTypes(),
182 ApiBase::PARAM_REQUIRED => true
184 'target' => null,
185 'ids' => array(
186 ApiBase::PARAM_ISMULTI => true,
187 ApiBase::PARAM_REQUIRED => true
189 'hide' => array(
190 ApiBase::PARAM_TYPE => array( 'content', 'comment', 'user' ),
191 ApiBase::PARAM_ISMULTI => true,
193 'show' => array(
194 ApiBase::PARAM_TYPE => array( 'content', 'comment', 'user' ),
195 ApiBase::PARAM_ISMULTI => true,
197 'suppress' => array(
198 ApiBase::PARAM_TYPE => array( 'yes', 'no', 'nochange' ),
199 ApiBase::PARAM_DFLT => 'nochange',
201 'reason' => null,
205 public function needsToken() {
206 return 'csrf';
209 protected function getExamplesMessages() {
210 return array(
211 'action=revisiondelete&target=Main%20Page&type=revision&ids=12345&' .
212 'hide=content&token=123ABC'
213 => 'apihelp-revisiondelete-example-revision',
214 'action=revisiondelete&type=logging&ids=67890&hide=content|comment|user&' .
215 'reason=BLP%20violation&token=123ABC'
216 => 'apihelp-revisiondelete-example-log',
220 public function getHelpUrls() {
221 return 'https://www.mediawiki.org/wiki/API:Revisiondelete';