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
97 if ( $list->areAnySuppressed() ) {
98 // To change anything at all about an already-suppressed revision, the user must have
99 // the suppressrevision right (T435026)
100 $this->checkUserRightsAny( 'suppressrevision' );
101 }
102
103 $status = $list->setVisibility( [
104 'value' => $bitfield,
105 'comment' => $params['reason'] ?? '',
106 'perItemStatus' => true,
107 'tags' => $params['tags']
108 ] );
109
110 $result = $this->getResult();
111 $data = $this->extractStatusInfo( $status );
112 $data['target'] = $targetObj->getFullText();
113 $data['items'] = [];
114
115 foreach ( $status->getValue()['itemStatuses'] as $id => $s ) {
116 $data['items'][$id] = $this->extractStatusInfo( $s );
117 $data['items'][$id]['id'] = $id;
118 }
119
120 $list->reloadFromPrimary();
121 foreach ( $list as $item ) {
122 $data['items'][$item->getId()] += $item->getApiData( $this->getResult() );
123 }
124
125 $data['items'] = array_values( $data['items'] );
126 ApiResult::setIndexedTagName( $data['items'], 'i' );
127 $result->addValue( null, $this->getModuleName(), $data );
128 }
129
130 private function extractStatusInfo( Status $status ): array {
131 $ret = [
132 'status' => $status->isOK() ? 'Success' : 'Fail',
133 ];
134
135 $errors = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
136 if ( $errors ) {
137 $ret['errors'] = $errors;
138 }
139 $warnings = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
140 if ( $warnings ) {
141 $ret['warnings'] = $warnings;
142 }
143
144 return $ret;
145 }
146
148 public function mustBePosted() {
149 return true;
150 }
151
153 public function isWriteMode() {
154 return true;
155 }
156
158 public function getAllowedParams() {
159 return [
160 'type' => [
161 ParamValidator::PARAM_TYPE => RevisionDeleter::getTypes(),
162 ParamValidator::PARAM_REQUIRED => true
163 ],
164 'target' => null,
165 'ids' => [
166 ParamValidator::PARAM_ISMULTI => true,
167 ParamValidator::PARAM_REQUIRED => true
168 ],
169 'hide' => [
170 ParamValidator::PARAM_TYPE => [ 'content', 'comment', 'user' ],
171 ParamValidator::PARAM_ISMULTI => true,
172 ],
173 'show' => [
174 ParamValidator::PARAM_TYPE => [ 'content', 'comment', 'user' ],
175 ParamValidator::PARAM_ISMULTI => true,
176 ],
177 'suppress' => [
178 ParamValidator::PARAM_TYPE => [ 'yes', 'no', 'nochange' ],
179 ParamValidator::PARAM_DEFAULT => 'nochange',
180 ],
181 'reason' => [
182 ParamValidator::PARAM_TYPE => 'string'
183 ],
184 'tags' => [
185 ParamValidator::PARAM_TYPE => 'tags',
186 ParamValidator::PARAM_ISMULTI => true,
187 ],
188 ];
189 }
190
192 public function needsToken() {
193 return 'csrf';
194 }
195
197 protected function getExamplesMessages() {
198 $title = Title::newMainPage()->getPrefixedText();
199 $mp = rawurlencode( $title );
200
201 return [
202 "action=revisiondelete&target={$mp}&type=revision&ids=12345&" .
203 'hide=content&token=123ABC'
204 => 'apihelp-revisiondelete-example-revision',
205 'action=revisiondelete&type=logging&ids=67890&hide=content|comment|user&' .
206 'reason=BLP%20violation&token=123ABC'
207 => 'apihelp-revisiondelete-example-log',
208 ];
209 }
210
212 public function getHelpUrls() {
213 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Revisiondelete';
214 }
215}
216
218class_alias( ApiRevisionDelete::class, 'ApiRevisionDelete' );
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:59
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1488
checkUserRightsAny( $rights)
Helper function for permission-denied errors.
Definition ApiBase.php:1597
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:556
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1335
getResult()
Get the result object.
Definition ApiBase.php:695
dieBlocked(Block $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition ApiBase.php:1516
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1539
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:836
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition ApiBase.php:755
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.