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 $params = $this->extractRequestParams();
36 $user = $this->getUser();
38 if ( !$user->isAllowed( RevisionDeleter
::getRestriction( $params['type'] ) ) ) {
39 $this->dieUsageMsg( 'badaccess-group0' );
42 if ( !$params['ids'] ) {
43 $this->dieUsage( "At least one value is required for 'ids'", 'badparams' );
46 $hide = $params['hide'] ?
: array();
47 $show = $params['show'] ?
: array();
48 if ( array_intersect( $hide, $show ) ) {
49 $this->dieUsage( "Mutually exclusive values for 'hide' and 'show'", 'badparams' );
50 } elseif ( !$hide && !$show ) {
51 $this->dieUsage( "At least one value is required for 'hide' or 'show'", 'badparams' );
54 'content' => RevisionDeleter
::getRevdelConstant( $params['type'] ),
55 'comment' => Revision
::DELETED_COMMENT
,
56 'user' => Revision
::DELETED_USER
,
59 foreach ( $bits as $key => $bit ) {
60 if ( in_array( $key, $hide ) ) {
62 } elseif ( in_array( $key, $show ) ) {
69 if ( $params['suppress'] === 'yes' ) {
70 if ( !$user->isAllowed( 'suppressrevision' ) ) {
71 $this->dieUsageMsg( 'badaccess-group0' );
73 $bitfield[Revision
::DELETED_RESTRICTED
] = 1;
74 } elseif ( $params['suppress'] === 'no' ) {
75 $bitfield[Revision
::DELETED_RESTRICTED
] = 0;
77 $bitfield[Revision
::DELETED_RESTRICTED
] = -1;
81 if ( $params['target'] ) {
82 $targetObj = Title
::newFromText( $params['target'] );
84 $targetObj = RevisionDeleter
::suggestTarget( $params['type'], $targetObj, $params['ids'] );
85 if ( $targetObj === null ) {
86 $this->dieUsage( 'A target title is required for this RevDel type', 'needtarget' );
89 $list = RevisionDeleter
::createList(
90 $params['type'], $this->getContext(), $targetObj, $params['ids']
92 $status = $list->setVisibility(
93 array( 'value' => $bitfield, 'comment' => $params['reason'], 'perItemStatus' => true )
96 $result = $this->getResult();
97 $data = $this->extractStatusInfo( $status );
98 $data['target'] = $targetObj->getFullText();
99 $data['items'] = array();
101 foreach ( $status->itemStatuses
as $id => $s ) {
102 $data['items'][$id] = $this->extractStatusInfo( $s );
103 $data['items'][$id]['id'] = $id;
106 $list->reloadFromMaster();
107 // @codingStandardsIgnoreStart Avoid function calls in a FOR loop test part
108 for ( $item = $list->reset(); $list->current(); $item = $list->next() ) {
109 $data['items'][$item->getId()] +
= $item->getApiData( $this->getResult() );
111 // @codingStandardsIgnoreEnd
113 $data['items'] = array_values( $data['items'] );
114 $result->setIndexedTagName( $data['items'], 'i' );
115 $result->addValue( null, $this->getModuleName(), $data );
118 private function extractStatusInfo( $status ) {
120 'status' => $status->isOK() ?
'Success' : 'Fail',
122 $errors = $this->formatStatusMessages( $status->getErrorsByType( 'error' ) );
124 $this->getResult()->setIndexedTagName( $errors, 'e' );
125 $ret['errors'] = $errors;
127 $warnings = $this->formatStatusMessages( $status->getErrorsByType( 'warning' ) );
129 $this->getResult()->setIndexedTagName( $warnings, 'w' );
130 $ret['warnings'] = $warnings;
136 private function formatStatusMessages( $messages ) {
140 $result = $this->getResult();
142 foreach ( $messages as $m ) {
144 if ( $m['message'] instanceof Message
) {
145 $msg = $m['message'];
146 $message = array( 'message' => $msg->getKey() );
147 if ( $msg->getParams() ) {
148 $message['params'] = $msg->getParams();
149 $result->setIndexedTagName( $message['params'], 'p' );
152 $message = array( 'message' => $m['message'] );
153 $msg = wfMessage( $m['message'] );
154 if ( isset( $m['params'] ) ) {
155 $message['params'] = $m['params'];
156 $result->setIndexedTagName( $message['params'], 'p' );
157 $msg->params( $m['params'] );
160 $message['rendered'] = $msg->useDatabase( false )->inLanguage( 'en' )->plain();
167 public function mustBePosted() {
171 public function isWriteMode() {
175 public function getAllowedParams() {
178 ApiBase
::PARAM_TYPE
=> RevisionDeleter
::getTypes(),
179 ApiBase
::PARAM_REQUIRED
=> true
183 ApiBase
::PARAM_ISMULTI
=> true,
184 ApiBase
::PARAM_REQUIRED
=> true
187 ApiBase
::PARAM_TYPE
=> array( 'content', 'comment', 'user' ),
188 ApiBase
::PARAM_ISMULTI
=> true,
191 ApiBase
::PARAM_TYPE
=> array( 'content', 'comment', 'user' ),
192 ApiBase
::PARAM_ISMULTI
=> true,
195 ApiBase
::PARAM_TYPE
=> array( 'yes', 'no', 'nochange' ),
196 ApiBase
::PARAM_DFLT
=> 'nochange',
202 public function needsToken() {
206 protected function getExamplesMessages() {
208 'action=revisiondelete&target=Main%20Page&type=revision&ids=12345&' .
209 'hide=content&token=123ABC'
210 => 'apihelp-revisiondelete-example-revision',
211 'action=revisiondelete&type=logging&ids=67890&hide=content|comment|user&' .
212 'reason=BLP%20violation&token=123ABC'
213 => 'apihelp-revisiondelete-example-log',
217 public function getHelpUrls() {
218 return 'https://www.mediawiki.org/wiki/API:Revisiondelete';