MediaWiki master
ApiRevisionDelete.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Api;
25
26use ChangeTags;
32
40
41 public function execute() {
43
45 $user = $this->getUser();
47
48 if ( !$params['ids'] ) {
49 $this->dieWithError( [ 'apierror-paramempty', 'ids' ], 'paramempty_ids' );
50 }
51
52 // Check if user can add tags
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 // TODO: replace use of PermissionManager
102 if ( $this->getPermissionManager()->isBlockedFrom( $user, $targetObj ) ) {
103 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Block is checked and not null
104 $this->dieBlocked( $user->getBlock() );
105 }
106
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
219class_alias( ApiRevisionDelete::class, 'ApiRevisionDelete' );
array $params
The job parameters.
Recent changes tagging.
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...
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:76
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1577
checkUserRightsAny( $rights)
Helper function for permission-denied errors.
Definition ApiBase.php:1692
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:571
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1425
getResult()
Get the result object.
Definition ApiBase.php:710
dieBlocked(Block $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition ApiBase.php:1605
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1632
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition ApiBase.php:770
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.
getExamplesMessages()
Returns usage examples for this module.
getHelpUrls()
Return links to more detailed help pages about the module.
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.
isWriteMode()
Indicates whether this module requires write access to the wiki.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Page revision base class.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Represents a title within MediaWiki.
Definition Title.php:78
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 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.
Service for formatting and validating API parameters.