2013-06-26 20:59:40 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Created on Jun 25, 2013
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2013 Brad Jorsch <bjorsch@wikimedia.org>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @since 1.23
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API interface to RevDel. The API equivalent of Special:RevisionDelete.
|
|
|
|
|
* Requires API write mode to be enabled.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup API
|
|
|
|
|
*/
|
|
|
|
|
class ApiRevisionDelete extends ApiBase {
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
2015-08-08 00:08:33 +00:00
|
|
|
$this->useTransactionalTimeLimit();
|
|
|
|
|
|
2013-06-26 20:59:40 +00:00
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
$user = $this->getUser();
|
|
|
|
|
if ( !$user->isAllowed( RevisionDeleter::getRestriction( $params['type'] ) ) ) {
|
|
|
|
|
$this->dieUsageMsg( 'badaccess-group0' );
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-20 23:49:10 +00:00
|
|
|
if ( $user->isBlocked() ) {
|
2015-12-21 22:59:16 +00:00
|
|
|
$this->dieBlocked( $user->getBlock() );
|
2015-12-20 23:49:10 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-26 20:59:40 +00:00
|
|
|
if ( !$params['ids'] ) {
|
|
|
|
|
$this->dieUsage( "At least one value is required for 'ids'", 'badparams' );
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$hide = $params['hide'] ?: [];
|
|
|
|
|
$show = $params['show'] ?: [];
|
2013-06-26 20:59:40 +00:00
|
|
|
if ( array_intersect( $hide, $show ) ) {
|
|
|
|
|
$this->dieUsage( "Mutually exclusive values for 'hide' and 'show'", 'badparams' );
|
|
|
|
|
} elseif ( !$hide && !$show ) {
|
|
|
|
|
$this->dieUsage( "At least one value is required for 'hide' or 'show'", 'badparams' );
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$bits = [
|
2013-06-26 20:59:40 +00:00
|
|
|
'content' => RevisionDeleter::getRevdelConstant( $params['type'] ),
|
|
|
|
|
'comment' => Revision::DELETED_COMMENT,
|
|
|
|
|
'user' => Revision::DELETED_USER,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
|
|
|
|
$bitfield = [];
|
2013-06-26 20:59:40 +00:00
|
|
|
foreach ( $bits as $key => $bit ) {
|
|
|
|
|
if ( in_array( $key, $hide ) ) {
|
|
|
|
|
$bitfield[$bit] = 1;
|
|
|
|
|
} elseif ( in_array( $key, $show ) ) {
|
|
|
|
|
$bitfield[$bit] = 0;
|
|
|
|
|
} else {
|
|
|
|
|
$bitfield[$bit] = -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $params['suppress'] === 'yes' ) {
|
|
|
|
|
if ( !$user->isAllowed( 'suppressrevision' ) ) {
|
|
|
|
|
$this->dieUsageMsg( 'badaccess-group0' );
|
|
|
|
|
}
|
|
|
|
|
$bitfield[Revision::DELETED_RESTRICTED] = 1;
|
|
|
|
|
} elseif ( $params['suppress'] === 'no' ) {
|
|
|
|
|
$bitfield[Revision::DELETED_RESTRICTED] = 0;
|
|
|
|
|
} else {
|
|
|
|
|
$bitfield[Revision::DELETED_RESTRICTED] = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$targetObj = null;
|
|
|
|
|
if ( $params['target'] ) {
|
|
|
|
|
$targetObj = Title::newFromText( $params['target'] );
|
|
|
|
|
}
|
|
|
|
|
$targetObj = RevisionDeleter::suggestTarget( $params['type'], $targetObj, $params['ids'] );
|
|
|
|
|
if ( $targetObj === null ) {
|
|
|
|
|
$this->dieUsage( 'A target title is required for this RevDel type', 'needtarget' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$list = RevisionDeleter::createList(
|
|
|
|
|
$params['type'], $this->getContext(), $targetObj, $params['ids']
|
|
|
|
|
);
|
|
|
|
|
$status = $list->setVisibility(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'value' => $bitfield, 'comment' => $params['reason'], 'perItemStatus' => true ]
|
2013-06-26 20:59:40 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result = $this->getResult();
|
|
|
|
|
$data = $this->extractStatusInfo( $status );
|
|
|
|
|
$data['target'] = $targetObj->getFullText();
|
2016-02-17 09:09:32 +00:00
|
|
|
$data['items'] = [];
|
2014-02-05 10:20:17 +00:00
|
|
|
|
2013-06-26 20:59:40 +00:00
|
|
|
foreach ( $status->itemStatuses as $id => $s ) {
|
|
|
|
|
$data['items'][$id] = $this->extractStatusInfo( $s );
|
|
|
|
|
$data['items'][$id]['id'] = $id;
|
|
|
|
|
}
|
2014-02-05 10:20:17 +00:00
|
|
|
|
2013-06-26 20:59:40 +00:00
|
|
|
$list->reloadFromMaster();
|
2014-02-05 10:20:17 +00:00
|
|
|
// @codingStandardsIgnoreStart Avoid function calls in a FOR loop test part
|
2013-06-26 20:59:40 +00:00
|
|
|
for ( $item = $list->reset(); $list->current(); $item = $list->next() ) {
|
|
|
|
|
$data['items'][$item->getId()] += $item->getApiData( $this->getResult() );
|
|
|
|
|
}
|
2014-02-05 10:20:17 +00:00
|
|
|
// @codingStandardsIgnoreEnd
|
|
|
|
|
|
2013-06-26 20:59:40 +00:00
|
|
|
$data['items'] = array_values( $data['items'] );
|
API: Overhaul ApiResult, make format=xml not throw, and add json formatversion
ApiResult was a mess: some methods could only be used with an array
reference instead of manipulating the stored data, methods that had both
array-ref and internal-data versions had names that didn't at all
correspond, some methods that worked on an array reference were
annoyingly non-static, and then the whole mess with setIndexedTagName.
ApiFormatXml is also entirely annoying to deal with, as it liked to
throw exceptions if certain metadata wasn't provided that no other
formatter required. Its legacy also means we have this silly convention
of using empty-string rather than boolean true, annoying restrictions on
keys (leading to things that should be hashes being arrays of key-value
object instead), '*' used as a key all over the place, and so on.
So, changes here:
* ApiResult is no longer an ApiBase or a ContextSource.
* Wherever sensible, ApiResult provides a static method working on an
arrayref and a non-static method working on internal data.
* Metadata is now always added to ApiResult's internal data structure.
Formatters are responsible for stripping it if necessary. "raw mode"
is deprecated.
* New metadata to replace the '*' key, solve the array() => '[]' vs '{}'
question, and so on.
* New class for formatting warnings and errors using i18n messages, and
support for multiple errors and a more machine-readable format for
warnings. For the moment, though, the actual output will not be changing
yet (see T47843 for future plans).
* New formatversion parameter for format=json and format=php, to select
between BC mode and the modern output.
* In BC mode, booleans will be converted to empty-string presence style;
modules currently returning booleans will need to use
ApiResult::META_BC_BOOLS to preserve their current output.
Actual changes to the API modules' output (e.g. actually returning
booleans for the new formatversion) beyond the use of
ApiResult::setContentValue() are left for a future change.
Bug: T76728
Bug: T57371
Bug: T33629
Change-Id: I7b37295e8862b188d1f3b0cd07f66ac34629678f
2014-12-03 22:14:22 +00:00
|
|
|
ApiResult::setIndexedTagName( $data['items'], 'i' );
|
2013-06-26 20:59:40 +00:00
|
|
|
$result->addValue( null, $this->getModuleName(), $data );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function extractStatusInfo( $status ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$ret = [
|
2013-06-26 20:59:40 +00:00
|
|
|
'status' => $status->isOK() ? 'Success' : 'Fail',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-06-26 20:59:40 +00:00
|
|
|
$errors = $this->formatStatusMessages( $status->getErrorsByType( 'error' ) );
|
|
|
|
|
if ( $errors ) {
|
API: Overhaul ApiResult, make format=xml not throw, and add json formatversion
ApiResult was a mess: some methods could only be used with an array
reference instead of manipulating the stored data, methods that had both
array-ref and internal-data versions had names that didn't at all
correspond, some methods that worked on an array reference were
annoyingly non-static, and then the whole mess with setIndexedTagName.
ApiFormatXml is also entirely annoying to deal with, as it liked to
throw exceptions if certain metadata wasn't provided that no other
formatter required. Its legacy also means we have this silly convention
of using empty-string rather than boolean true, annoying restrictions on
keys (leading to things that should be hashes being arrays of key-value
object instead), '*' used as a key all over the place, and so on.
So, changes here:
* ApiResult is no longer an ApiBase or a ContextSource.
* Wherever sensible, ApiResult provides a static method working on an
arrayref and a non-static method working on internal data.
* Metadata is now always added to ApiResult's internal data structure.
Formatters are responsible for stripping it if necessary. "raw mode"
is deprecated.
* New metadata to replace the '*' key, solve the array() => '[]' vs '{}'
question, and so on.
* New class for formatting warnings and errors using i18n messages, and
support for multiple errors and a more machine-readable format for
warnings. For the moment, though, the actual output will not be changing
yet (see T47843 for future plans).
* New formatversion parameter for format=json and format=php, to select
between BC mode and the modern output.
* In BC mode, booleans will be converted to empty-string presence style;
modules currently returning booleans will need to use
ApiResult::META_BC_BOOLS to preserve their current output.
Actual changes to the API modules' output (e.g. actually returning
booleans for the new formatversion) beyond the use of
ApiResult::setContentValue() are left for a future change.
Bug: T76728
Bug: T57371
Bug: T33629
Change-Id: I7b37295e8862b188d1f3b0cd07f66ac34629678f
2014-12-03 22:14:22 +00:00
|
|
|
ApiResult::setIndexedTagName( $errors, 'e' );
|
2013-06-26 20:59:40 +00:00
|
|
|
$ret['errors'] = $errors;
|
|
|
|
|
}
|
|
|
|
|
$warnings = $this->formatStatusMessages( $status->getErrorsByType( 'warning' ) );
|
|
|
|
|
if ( $warnings ) {
|
API: Overhaul ApiResult, make format=xml not throw, and add json formatversion
ApiResult was a mess: some methods could only be used with an array
reference instead of manipulating the stored data, methods that had both
array-ref and internal-data versions had names that didn't at all
correspond, some methods that worked on an array reference were
annoyingly non-static, and then the whole mess with setIndexedTagName.
ApiFormatXml is also entirely annoying to deal with, as it liked to
throw exceptions if certain metadata wasn't provided that no other
formatter required. Its legacy also means we have this silly convention
of using empty-string rather than boolean true, annoying restrictions on
keys (leading to things that should be hashes being arrays of key-value
object instead), '*' used as a key all over the place, and so on.
So, changes here:
* ApiResult is no longer an ApiBase or a ContextSource.
* Wherever sensible, ApiResult provides a static method working on an
arrayref and a non-static method working on internal data.
* Metadata is now always added to ApiResult's internal data structure.
Formatters are responsible for stripping it if necessary. "raw mode"
is deprecated.
* New metadata to replace the '*' key, solve the array() => '[]' vs '{}'
question, and so on.
* New class for formatting warnings and errors using i18n messages, and
support for multiple errors and a more machine-readable format for
warnings. For the moment, though, the actual output will not be changing
yet (see T47843 for future plans).
* New formatversion parameter for format=json and format=php, to select
between BC mode and the modern output.
* In BC mode, booleans will be converted to empty-string presence style;
modules currently returning booleans will need to use
ApiResult::META_BC_BOOLS to preserve their current output.
Actual changes to the API modules' output (e.g. actually returning
booleans for the new formatversion) beyond the use of
ApiResult::setContentValue() are left for a future change.
Bug: T76728
Bug: T57371
Bug: T33629
Change-Id: I7b37295e8862b188d1f3b0cd07f66ac34629678f
2014-12-03 22:14:22 +00:00
|
|
|
ApiResult::setIndexedTagName( $warnings, 'w' );
|
2013-06-26 20:59:40 +00:00
|
|
|
$ret['warnings'] = $warnings;
|
|
|
|
|
}
|
2014-02-05 11:02:29 +00:00
|
|
|
|
2013-06-26 20:59:40 +00:00
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function formatStatusMessages( $messages ) {
|
|
|
|
|
if ( !$messages ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [];
|
2013-06-26 20:59:40 +00:00
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$ret = [];
|
2013-06-26 20:59:40 +00:00
|
|
|
foreach ( $messages as $m ) {
|
|
|
|
|
if ( $m['message'] instanceof Message ) {
|
|
|
|
|
$msg = $m['message'];
|
2016-02-17 09:09:32 +00:00
|
|
|
$message = [ 'message' => $msg->getKey() ];
|
2013-06-26 20:59:40 +00:00
|
|
|
if ( $msg->getParams() ) {
|
|
|
|
|
$message['params'] = $msg->getParams();
|
API: Overhaul ApiResult, make format=xml not throw, and add json formatversion
ApiResult was a mess: some methods could only be used with an array
reference instead of manipulating the stored data, methods that had both
array-ref and internal-data versions had names that didn't at all
correspond, some methods that worked on an array reference were
annoyingly non-static, and then the whole mess with setIndexedTagName.
ApiFormatXml is also entirely annoying to deal with, as it liked to
throw exceptions if certain metadata wasn't provided that no other
formatter required. Its legacy also means we have this silly convention
of using empty-string rather than boolean true, annoying restrictions on
keys (leading to things that should be hashes being arrays of key-value
object instead), '*' used as a key all over the place, and so on.
So, changes here:
* ApiResult is no longer an ApiBase or a ContextSource.
* Wherever sensible, ApiResult provides a static method working on an
arrayref and a non-static method working on internal data.
* Metadata is now always added to ApiResult's internal data structure.
Formatters are responsible for stripping it if necessary. "raw mode"
is deprecated.
* New metadata to replace the '*' key, solve the array() => '[]' vs '{}'
question, and so on.
* New class for formatting warnings and errors using i18n messages, and
support for multiple errors and a more machine-readable format for
warnings. For the moment, though, the actual output will not be changing
yet (see T47843 for future plans).
* New formatversion parameter for format=json and format=php, to select
between BC mode and the modern output.
* In BC mode, booleans will be converted to empty-string presence style;
modules currently returning booleans will need to use
ApiResult::META_BC_BOOLS to preserve their current output.
Actual changes to the API modules' output (e.g. actually returning
booleans for the new formatversion) beyond the use of
ApiResult::setContentValue() are left for a future change.
Bug: T76728
Bug: T57371
Bug: T33629
Change-Id: I7b37295e8862b188d1f3b0cd07f66ac34629678f
2014-12-03 22:14:22 +00:00
|
|
|
ApiResult::setIndexedTagName( $message['params'], 'p' );
|
2013-06-26 20:59:40 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2016-02-17 09:09:32 +00:00
|
|
|
$message = [ 'message' => $m['message'] ];
|
2013-06-26 20:59:40 +00:00
|
|
|
$msg = wfMessage( $m['message'] );
|
|
|
|
|
if ( isset( $m['params'] ) ) {
|
|
|
|
|
$message['params'] = $m['params'];
|
API: Overhaul ApiResult, make format=xml not throw, and add json formatversion
ApiResult was a mess: some methods could only be used with an array
reference instead of manipulating the stored data, methods that had both
array-ref and internal-data versions had names that didn't at all
correspond, some methods that worked on an array reference were
annoyingly non-static, and then the whole mess with setIndexedTagName.
ApiFormatXml is also entirely annoying to deal with, as it liked to
throw exceptions if certain metadata wasn't provided that no other
formatter required. Its legacy also means we have this silly convention
of using empty-string rather than boolean true, annoying restrictions on
keys (leading to things that should be hashes being arrays of key-value
object instead), '*' used as a key all over the place, and so on.
So, changes here:
* ApiResult is no longer an ApiBase or a ContextSource.
* Wherever sensible, ApiResult provides a static method working on an
arrayref and a non-static method working on internal data.
* Metadata is now always added to ApiResult's internal data structure.
Formatters are responsible for stripping it if necessary. "raw mode"
is deprecated.
* New metadata to replace the '*' key, solve the array() => '[]' vs '{}'
question, and so on.
* New class for formatting warnings and errors using i18n messages, and
support for multiple errors and a more machine-readable format for
warnings. For the moment, though, the actual output will not be changing
yet (see T47843 for future plans).
* New formatversion parameter for format=json and format=php, to select
between BC mode and the modern output.
* In BC mode, booleans will be converted to empty-string presence style;
modules currently returning booleans will need to use
ApiResult::META_BC_BOOLS to preserve their current output.
Actual changes to the API modules' output (e.g. actually returning
booleans for the new formatversion) beyond the use of
ApiResult::setContentValue() are left for a future change.
Bug: T76728
Bug: T57371
Bug: T33629
Change-Id: I7b37295e8862b188d1f3b0cd07f66ac34629678f
2014-12-03 22:14:22 +00:00
|
|
|
ApiResult::setIndexedTagName( $message['params'], 'p' );
|
2013-06-26 20:59:40 +00:00
|
|
|
$msg->params( $m['params'] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$message['rendered'] = $msg->useDatabase( false )->inLanguage( 'en' )->plain();
|
|
|
|
|
$ret[] = $message;
|
|
|
|
|
}
|
2014-02-05 11:02:29 +00:00
|
|
|
|
2013-06-26 20:59:40 +00:00
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function mustBePosted() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isWriteMode() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getAllowedParams() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
'type' => [
|
2013-06-26 20:59:40 +00:00
|
|
|
ApiBase::PARAM_TYPE => RevisionDeleter::getTypes(),
|
|
|
|
|
ApiBase::PARAM_REQUIRED => true
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2013-06-26 20:59:40 +00:00
|
|
|
'target' => null,
|
2016-02-17 09:09:32 +00:00
|
|
|
'ids' => [
|
2013-06-26 20:59:40 +00:00
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
|
ApiBase::PARAM_REQUIRED => true
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'hide' => [
|
|
|
|
|
ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
|
2013-06-26 20:59:40 +00:00
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'show' => [
|
|
|
|
|
ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
|
2013-06-26 20:59:40 +00:00
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'suppress' => [
|
|
|
|
|
ApiBase::PARAM_TYPE => [ 'yes', 'no', 'nochange' ],
|
2013-06-26 20:59:40 +00:00
|
|
|
ApiBase::PARAM_DFLT => 'nochange',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2013-06-26 20:59:40 +00:00
|
|
|
'reason' => null,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-06-26 20:59:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function needsToken() {
|
2014-08-08 16:56:07 +00:00
|
|
|
return 'csrf';
|
2013-06-26 20:59:40 +00:00
|
|
|
}
|
|
|
|
|
|
2014-10-28 17:17:02 +00:00
|
|
|
protected function getExamplesMessages() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2014-09-18 17:38:23 +00:00
|
|
|
'action=revisiondelete&target=Main%20Page&type=revision&ids=12345&' .
|
2014-02-05 10:20:17 +00:00
|
|
|
'hide=content&token=123ABC'
|
2014-09-18 17:38:23 +00:00
|
|
|
=> 'apihelp-revisiondelete-example-revision',
|
|
|
|
|
'action=revisiondelete&type=logging&ids=67890&hide=content|comment|user&' .
|
2014-02-05 10:20:17 +00:00
|
|
|
'reason=BLP%20violation&token=123ABC'
|
2014-09-18 17:38:23 +00:00
|
|
|
=> 'apihelp-revisiondelete-example-log',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-06-26 20:59:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getHelpUrls() {
|
|
|
|
|
return 'https://www.mediawiki.org/wiki/API:Revisiondelete';
|
|
|
|
|
}
|
|
|
|
|
}
|