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
27 * API interface to RevDel. The API equivalent of Special:RevisionDelete.
28 * Requires API write mode to be enabled.
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' );
59 'content' => RevisionDeleter
::getRevdelConstant( $params['type'] ),
60 'comment' => Revision
::DELETED_COMMENT
,
61 'user' => Revision
::DELETED_USER
,
64 foreach ( $bits as $key => $bit ) {
65 if ( in_array( $key, $hide ) ) {
67 } elseif ( in_array( $key, $show ) ) {
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;
82 $bitfield[Revision
::DELETED_RESTRICTED
] = -1;
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 ) {
125 'status' => $status->isOK() ?
'Success' : 'Fail',
127 $errors = $this->formatStatusMessages( $status->getErrorsByType( 'error' ) );
129 ApiResult
::setIndexedTagName( $errors, 'e' );
130 $ret['errors'] = $errors;
132 $warnings = $this->formatStatusMessages( $status->getErrorsByType( 'warning' ) );
134 ApiResult
::setIndexedTagName( $warnings, 'w' );
135 $ret['warnings'] = $warnings;
141 private function formatStatusMessages( $messages ) {
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' );
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();
170 public function mustBePosted() {
174 public function isWriteMode() {
178 public function getAllowedParams() {
181 ApiBase
::PARAM_TYPE
=> RevisionDeleter
::getTypes(),
182 ApiBase
::PARAM_REQUIRED
=> true
186 ApiBase
::PARAM_ISMULTI
=> true,
187 ApiBase
::PARAM_REQUIRED
=> true
190 ApiBase
::PARAM_TYPE
=> array( 'content', 'comment', 'user' ),
191 ApiBase
::PARAM_ISMULTI
=> true,
194 ApiBase
::PARAM_TYPE
=> array( 'content', 'comment', 'user' ),
195 ApiBase
::PARAM_ISMULTI
=> true,
198 ApiBase
::PARAM_TYPE
=> array( 'yes', 'no', 'nochange' ),
199 ApiBase
::PARAM_DFLT
=> 'nochange',
205 public function needsToken() {
209 protected function getExamplesMessages() {
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';