MediaWiki master
ApiRevisionDelete.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Api;
11
18
26
27 public function execute() {
29
30 $params = $this->extractRequestParams();
31 $user = $this->getUser();
32 $this->checkUserRightsAny( RevisionDeleter::getRestriction( $params['type'] ) );
33
34 if ( !$params['ids'] ) {
35 $this->dieWithError( [ 'apierror-paramempty', 'ids' ], 'paramempty_ids' );
36 }
37
38 // Check if user can add tags
39 if ( $params['tags'] ) {
40 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
41 if ( !$ableToTag->isOK() ) {
42 $this->dieStatus( $ableToTag );
43 }
44 }
45
46 $hide = $params['hide'] ?: [];
47 $show = $params['show'] ?: [];
48 if ( array_intersect( $hide, $show ) ) {
49 $this->dieWithError( 'apierror-revdel-mutuallyexclusive', 'badparams' );
50 } elseif ( !$hide && !$show ) {
51 $this->dieWithError( 'apierror-revdel-paramneeded', 'badparams' );
52 }
53 $bits = [
54 'content' => RevisionDeleter::getRevdelConstant( $params['type'] ),
55 'comment' => RevisionRecord::DELETED_COMMENT,
56 'user' => RevisionRecord::DELETED_USER,
57 ];
58 $bitfield = [];
59 foreach ( $bits as $key => $bit ) {
60 if ( in_array( $key, $hide ) ) {
61 $bitfield[$bit] = 1;
62 } elseif ( in_array( $key, $show ) ) {
63 $bitfield[$bit] = 0;
64 } else {
65 $bitfield[$bit] = -1;
66 }
67 }
68
69 if ( $params['suppress'] === 'yes' ) {
70 $this->checkUserRightsAny( 'suppressrevision' );
71 $bitfield[RevisionRecord::DELETED_RESTRICTED] = 1;
72 } elseif ( $params['suppress'] === 'no' ) {
73 $bitfield[RevisionRecord::DELETED_RESTRICTED] = 0;
74 } else {
75 $bitfield[RevisionRecord::DELETED_RESTRICTED] = -1;
76 }
77
78 $targetObj = null;
79 if ( $params['target'] ) {
80 $targetObj = Title::newFromText( $params['target'] );
81 }
82 $targetObj = RevisionDeleter::suggestTarget( $params['type'], $targetObj, $params['ids'] );
83 if ( $targetObj === null ) {
84 $this->dieWithError( [ 'apierror-revdel-needtarget' ], 'needtarget' );
85 }
86
87 // TODO: replace use of PermissionManager
88 if ( $this->getPermissionManager()->isBlockedFrom( $user, $targetObj ) ) {
89 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Block is checked and not null
90 $this->dieBlocked( $user->getBlock() );
91 }
92
94 $params['type'], $this->getContext(), $targetObj, $params['ids']
95 );
96 $status = $list->setVisibility( [
97 'value' => $bitfield,
98 'comment' => $params['reason'] ?? '',
99 'perItemStatus' => true,
100 'tags' => $params['tags']
101 ] );
102
103 $result = $this->getResult();
104 $data = $this->extractStatusInfo( $status );
105 $data['target'] = $targetObj->getFullText();
106 $data['items'] = [];
107
108 foreach ( $status->getValue()['itemStatuses'] as $id => $s ) {
109 $data['items'][$id] = $this->extractStatusInfo( $s );
110 $data['items'][$id]['id'] = $id;
111 }
112
113 $list->reloadFromPrimary();
114 foreach ( $list as $item ) {
115 $data['items'][$item->getId()] += $item->getApiData( $this->getResult() );
116 }
117
118 $data['items'] = array_values( $data['items'] );
119 ApiResult::setIndexedTagName( $data['items'], 'i' );
120 $result->addValue( null, $this->getModuleName(), $data );
121 }
122
123 private function extractStatusInfo( Status $status ): array {
124 $ret = [
125 'status' => $status->isOK() ? 'Success' : 'Fail',
126 ];
127
128 $errors = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
129 if ( $errors ) {
130 $ret['errors'] = $errors;
131 }
132 $warnings = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
133 if ( $warnings ) {
134 $ret['warnings'] = $warnings;
135 }
136
137 return $ret;
138 }
139
141 public function mustBePosted() {
142 return true;
143 }
144
146 public function isWriteMode() {
147 return true;
148 }
149
151 public function getAllowedParams() {
152 return [
153 'type' => [
154 ParamValidator::PARAM_TYPE => RevisionDeleter::getTypes(),
155 ParamValidator::PARAM_REQUIRED => true
156 ],
157 'target' => null,
158 'ids' => [
159 ParamValidator::PARAM_ISMULTI => true,
160 ParamValidator::PARAM_REQUIRED => true
161 ],
162 'hide' => [
163 ParamValidator::PARAM_TYPE => [ 'content', 'comment', 'user' ],
164 ParamValidator::PARAM_ISMULTI => true,
165 ],
166 'show' => [
167 ParamValidator::PARAM_TYPE => [ 'content', 'comment', 'user' ],
168 ParamValidator::PARAM_ISMULTI => true,
169 ],
170 'suppress' => [
171 ParamValidator::PARAM_TYPE => [ 'yes', 'no', 'nochange' ],
172 ParamValidator::PARAM_DEFAULT => 'nochange',
173 ],
174 'reason' => [
175 ParamValidator::PARAM_TYPE => 'string'
176 ],
177 'tags' => [
178 ParamValidator::PARAM_TYPE => 'tags',
179 ParamValidator::PARAM_ISMULTI => true,
180 ],
181 ];
182 }
183
185 public function needsToken() {
186 return 'csrf';
187 }
188
190 protected function getExamplesMessages() {
191 $title = Title::newMainPage()->getPrefixedText();
192 $mp = rawurlencode( $title );
193
194 return [
195 "action=revisiondelete&target={$mp}&type=revision&ids=12345&" .
196 'hide=content&token=123ABC'
197 => 'apihelp-revisiondelete-example-revision',
198 'action=revisiondelete&type=logging&ids=67890&hide=content|comment|user&' .
199 'reason=BLP%20violation&token=123ABC'
200 => 'apihelp-revisiondelete-example-log',
201 ];
202 }
203
205 public function getHelpUrls() {
206 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Revisiondelete';
207 }
208}
209
211class_alias( ApiRevisionDelete::class, 'ApiRevisionDelete' );
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:61
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1511
checkUserRightsAny( $rights)
Helper function for permission-denied errors.
Definition ApiBase.php:1620
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:543
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1359
getResult()
Get the result object.
Definition ApiBase.php:682
dieBlocked(Block $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition ApiBase.php:1539
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1562
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:823
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition ApiBase.php:742
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
needsToken()
Returns the token type this module requires in order to execute.Modules are strongly encouraged to us...
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
mustBePosted()
Indicates whether this module must be called with a POST request.Implementations of this method must ...
isWriteMode()
Indicates whether this module requires write access to the wiki.API modules must override this method...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Recent changes tagging.
General controller for RevDel, used by both SpecialRevisiondelete and ApiRevisionDelete.
static getTypes()
Lists the valid possible types for revision deletion.
static suggestTarget( $typeName, $target, array $ids)
Suggest a target for the revision deletion.
static getRestriction( $typeName)
Get the user right required for the RevDel type.
static getRevdelConstant( $typeName)
Get the revision deletion constant for the RevDel type.
static createList( $typeName, IContextSource $context, PageIdentity $page, array $ids)
Instantiate the appropriate list class for a given list of IDs.
Page revision base class.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
Represents a title within MediaWiki.
Definition Title.php:69
Service for formatting and validating API parameters.