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();
100 foreach ( $status->itemStatuses
as $id => $s ) {
101 $data['items'][$id] = $this->extractStatusInfo( $s );
102 $data['items'][$id]['id'] = $id;
104 $list->reloadFromMaster();
105 for ( $item = $list->reset(); $list->current(); $item = $list->next() ) {
106 $data['items'][$item->getId()] +
= $item->getApiData( $this->getResult() );
108 $data['items'] = array_values( $data['items'] );
109 $result->setIndexedTagName( $data['items'], 'i' );
110 $result->addValue( null, $this->getModuleName(), $data );
113 private function extractStatusInfo( $status ) {
115 'status' => $status->isOK() ?
'Success' : 'Fail',
117 $errors = $this->formatStatusMessages( $status->getErrorsByType( 'error' ) );
119 $this->getResult()->setIndexedTagName( $errors, 'e' );
120 $ret['errors'] = $errors;
122 $warnings = $this->formatStatusMessages( $status->getErrorsByType( 'warning' ) );
124 $this->getResult()->setIndexedTagName( $warnings, 'w' );
125 $ret['warnings'] = $warnings;
130 private function formatStatusMessages( $messages ) {
134 $result = $this->getResult();
136 foreach ( $messages as $m ) {
138 if ( $m['message'] instanceof Message
) {
139 $msg = $m['message'];
140 $message = array( 'message' => $msg->getKey() );
141 if ( $msg->getParams() ) {
142 $message['params'] = $msg->getParams();
143 $result->setIndexedTagName( $message['params'], 'p' );
146 $message = array( 'message' => $m['message'] );
147 $msg = wfMessage( $m['message'] );
148 if ( isset( $m['params'] ) ) {
149 $message['params'] = $m['params'];
150 $result->setIndexedTagName( $message['params'], 'p' );
151 $msg->params( $m['params'] );
154 $message['rendered'] = $msg->useDatabase( false )->inLanguage( 'en' )->plain();
160 public function mustBePosted() {
164 public function isWriteMode() {
168 public function getAllowedParams() {
171 ApiBase
::PARAM_TYPE
=> RevisionDeleter
::getTypes(),
172 ApiBase
::PARAM_REQUIRED
=> true
176 ApiBase
::PARAM_ISMULTI
=> true,
177 ApiBase
::PARAM_REQUIRED
=> true
180 ApiBase
::PARAM_TYPE
=> array( 'content', 'comment', 'user' ),
181 ApiBase
::PARAM_ISMULTI
=> true,
184 ApiBase
::PARAM_TYPE
=> array( 'content', 'comment', 'user' ),
185 ApiBase
::PARAM_ISMULTI
=> true,
188 ApiBase
::PARAM_TYPE
=> array( 'yes', 'no', 'nochange' ),
189 ApiBase
::PARAM_DFLT
=> 'nochange',
192 ApiBase
::PARAM_TYPE
=> 'string',
193 ApiBase
::PARAM_REQUIRED
=> true
199 public function getParamDescription() {
201 'type' => 'Type of revision deletion being performed',
202 'target' => 'Page title for the revision deletion, if required for the type',
203 'ids' => 'Identifiers for the revisions to be deleted',
204 'hide' => 'What to hide for each revision',
205 'show' => 'What to unhide for each revision',
206 'suppress' => 'Whether to suppress data from administrators as well as others',
207 'token' => 'A delete token previously retrieved through action=tokens',
208 'reason' => 'Reason for the deletion/undeletion',
212 public function getDescription() {
213 return 'Delete/undelete revisions';
216 public function getPossibleErrors() {
217 return array_merge( parent
::getPossibleErrors(),
219 'needtarget' => 'A target title is required for this RevDel type',
220 'badparams' => 'Bad value for some parameter',
225 public function needsToken() {
229 public function getTokenSalt() {
233 public function getExamples() {
235 'api.php?action=revisiondelete&target=Main%20Page&type=revision&ids=12345&hide=content&token=123ABC'
236 => 'Hide content for revision 12345 on the Main Page',
237 'api.php?action=revisiondelete&type=logging&ids=67890&hide=content|comment|user&reason=BLP%20violation&token=123ABC'
238 => 'Hide all data on log entry 67890 with the reason "BLP violation"',
242 public function getHelpUrls() {
243 return 'https://www.mediawiki.org/wiki/API:Revisiondelete';