| 1 |
<?php |
| 2 |
|
| 3 |
s |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace MediaWiki\Api; |
| 25 |
|
| 26 |
use ChangeTags; |
| 27 |
use MediaWiki\Revision\RevisionRecord; |
| 28 |
use MediaWiki\Status\Status; |
| 29 |
use MediaWiki\Title\Title; |
| 30 |
use RevisionDeleter; |
| 31 |
use Wikimedia\ParamValidator\ParamValidator; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class ApiRevisionDelete extends ApiBase { |
| 40 |
|
| 41 |
public function execute() { |
| 42 |
$this->useTransactionalTimeLimit(); |
| 43 |
|
| 44 |
$params = $this->extractRequestParams(); |
| 45 |
$user = $this->getUser(); |
| 46 |
$this->checkUserRightsAny( RevisionDeleter::getRestriction( $params['type'] ) ); |
| 47 |
|
| 48 |
if ( !$params['ids'] ) { |
| 49 |
$this->dieWithError( [ 'apierror-paramempty', 'ids' ], 'paramempty_ids' ); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
if ( $params['tags'] ) { |
| 54 |
$ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() ); |
| 55 |
if ( !$ableToTag->isOK() ) { |
| 56 |
$this->dieStatus( $ableToTag ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
$hide = $params['hide'] ?: []; |
| 61 |
$show = $params['show'] ?: []; |
| 62 |
if ( array_intersect( $hide, $show ) ) { |
| 63 |
$this->dieWithError( 'apierror-revdel-mutuallyexclusive', 'badparams' ); |
| 64 |
} elseif ( !$hide && !$show ) { |
| 65 |
$this->dieWithError( 'apierror-revdel-paramneeded', 'badparams' ); |
| 66 |
} |
| 67 |
$bits = [ |
| 68 |
'content' => RevisionDeleter::getRevdelConstant( $params['type'] ), |
| 69 |
'comment' => RevisionRecord::DELETED_COMMENT, |
| 70 |
'user' => RevisionRecord::DELETED_USER, |
| 71 |
]; |
| 72 |
$bitfield = []; |
| 73 |
foreach ( $bits as $key => $bit ) { |
| 74 |
if ( in_array( $key, $hide ) ) { |
| 75 |
$bitfield[$bit] = 1; |
| 76 |
} elseif ( in_array( $key, $show ) ) { |
| 77 |
$bitfield[$bit] = 0; |
| 78 |
} else { |
| 79 |
$bitfield[$bit] = -1; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
if ( $params['suppress'] === 'yes' ) { |
| 84 |
$this->checkUserRightsAny( 'suppressrevision' ); |
| 85 |
$bitfield[RevisionRecord::DELETED_RESTRICTED] = 1; |
| 86 |
} elseif ( $params['suppress'] === 'no' ) { |
| 87 |
$bitfield[RevisionRecord::DELETED_RESTRICTED] = 0; |
| 88 |
} else { |
| 89 |
$bitfield[RevisionRecord::DELETED_RESTRICTED] = -1; |
| 90 |
} |
| 91 |
|
| 92 |
$targetObj = null; |
| 93 |
if ( $params['target'] ) { |
| 94 |
$targetObj = Title::newFromText( $params['target'] ); |
| 95 |
} |
| 96 |
$targetObj = RevisionDeleter::suggestTarget( $params['type'], $targetObj, $params['ids'] ); |
| 97 |
if ( $targetObj === null ) { |
| 98 |
$this->dieWithError( [ 'apierror-revdel-needtarget' ], 'needtarget' ); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
if ( $this->getPermissionManager()->isBlockedFrom( $user, $targetObj ) ) { |
| 103 |
|
| 104 |
$this->dieBlocked( $user->getBlock() ); |
| 105 |
} |
| 106 |
|
| 107 |
$list = RevisionDeleter::createList( |
| 108 |
$params['type'], $this->getContext(), $targetObj, $params['ids'] |
| 109 |
); |
| 110 |
$status = $list->setVisibility( [ |
| 111 |
'value' => $bitfield, |
| 112 |
'comment' => $params['reason'] ?? '', |
| 113 |
'perItemStatus' => true, |
| 114 |
'tags' => $params['tags'] |
| 115 |
] ); |
| 116 |
|
| 117 |
$result = $this->getResult(); |
| 118 |
$data = $this->extractStatusInfo( $status ); |
| 119 |
$data['target'] = $targetObj->getFullText(); |
| 120 |
$data['items'] = []; |
| 121 |
|
| 122 |
foreach ( $status->getValue()['itemStatuses'] as $id => $s ) { |
| 123 |
$data['items'][$id] = $this->extractStatusInfo( $s ); |
| 124 |
$data['items'][$id]['id'] = $id; |
| 125 |
} |
| 126 |
|
| 127 |
$list->reloadFromPrimary(); |
| 128 |
for ( $item = $list->reset(); $list->current(); $item = $list->next() ) { |
| 129 |
$data['items'][$item->getId()] += $item->getApiData( $this->getResult() ); |
| 130 |
} |
| 131 |
|
| 132 |
$data['items'] = array_values( $data['items'] ); |
| 133 |
ApiResult::setIndexedTagName( $data['items'], 'i' ); |
| 134 |
$result->addValue( null, $this->getModuleName(), $data ); |
| 135 |
} |
| 136 |
|
| 137 |
private function extractStatusInfo( Status $status ) { |
| 138 |
$ret = [ |
| 139 |
'status' => $status->isOK() ? 'Success' : 'Fail', |
| 140 |
]; |
| 141 |
|
| 142 |
$errors = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' ); |
| 143 |
if ( $errors ) { |
| 144 |
$ret['errors'] = $errors; |
| 145 |
} |
| 146 |
$warnings = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' ); |
| 147 |
if ( $warnings ) { |
| 148 |
$ret['warnings'] = $warnings; |
| 149 |
} |
| 150 |
|
| 151 |
return $ret; |
| 152 |
} |
| 153 |
|
| 154 |
public function mustBePosted() { |
| 155 |
return true; |
| 156 |
} |
| 157 |
|
| 158 |
public function isWriteMode() { |
| 159 |
return true; |
| 160 |
} |
| 161 |
|
| 162 |
public function getAllowedParams() { |
| 163 |
return [ |
| 164 |
'type' => [ |
| 165 |
ParamValidator::PARAM_TYPE => RevisionDeleter::getTypes(), |
| 166 |
ParamValidator::PARAM_REQUIRED => true |
| 167 |
], |
| 168 |
'target' => null, |
| 169 |
'ids' => [ |
| 170 |
ParamValidator::PARAM_ISMULTI => true, |
| 171 |
ParamValidator::PARAM_REQUIRED => true |
| 172 |
], |
| 173 |
'hide' => [ |
| 174 |
ParamValidator::PARAM_TYPE => [ 'content', 'comment', 'user' ], |
| 175 |
ParamValidator::PARAM_ISMULTI => true, |
| 176 |
], |
| 177 |
'show' => [ |
| 178 |
ParamValidator::PARAM_TYPE => [ 'content', 'comment', 'user' ], |
| 179 |
ParamValidator::PARAM_ISMULTI => true, |
| 180 |
], |
| 181 |
'suppress' => [ |
| 182 |
ParamValidator::PARAM_TYPE => [ 'yes', 'no', 'nochange' ], |
| 183 |
ParamValidator::PARAM_DEFAULT => 'nochange', |
| 184 |
], |
| 185 |
'reason' => [ |
| 186 |
ParamValidator::PARAM_TYPE => 'string' |
| 187 |
], |
| 188 |
'tags' => [ |
| 189 |
ParamValidator::PARAM_TYPE => 'tags', |
| 190 |
ParamValidator::PARAM_ISMULTI => true, |
| 191 |
], |
| 192 |
]; |
| 193 |
} |
| 194 |
|
| 195 |
public function needsToken() { |
| 196 |
return 'csrf'; |
| 197 |
} |
| 198 |
|
| 199 |
protected function getExamplesMessages() { |
| 200 |
$title = Title::newMainPage()->getPrefixedText(); |
| 201 |
$mp = rawurlencode( $title ); |
| 202 |
|
| 203 |
return [ |
| 204 |
"action=revisiondelete&target={$mp}&type=revision&ids=12345&" . |
| 205 |
'hide=content&token=123ABC' |
| 206 |
=> 'apihelp-revisiondelete-example-revision', |
| 207 |
'action=revisiondelete&type=logging&ids=67890&hide=content|comment|user&' . |
| 208 |
'reason=BLP%20violation&token=123ABC' |
| 209 |
=> 'apihelp-revisiondelete-example-log', |
| 210 |
]; |
| 211 |
} |
| 212 |
|
| 213 |
public function getHelpUrls() { |
| 214 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Revisiondelete'; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
class_alias( ApiRevisionDelete::class, 'ApiRevisionDelete' ); |