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