MediaWiki REL1_34
ApiRevisionDelete.php
Go to the documentation of this file.
1<?php
25
33
34 public function execute() {
36
37 $params = $this->extractRequestParams();
38 $user = $this->getUser();
39 $this->checkUserRightsAny( RevisionDeleter::getRestriction( $params['type'] ) );
40
41 if ( !$params['ids'] ) {
42 $this->dieWithError( [ 'apierror-paramempty', 'ids' ], 'paramempty_ids' );
43 }
44
45 // Check if user can add tags
46 if ( $params['tags'] ) {
47 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
48 if ( !$ableToTag->isOK() ) {
49 $this->dieStatus( $ableToTag );
50 }
51 }
52
53 $hide = $params['hide'] ?: [];
54 $show = $params['show'] ?: [];
55 if ( array_intersect( $hide, $show ) ) {
56 $this->dieWithError( 'apierror-revdel-mutuallyexclusive', 'badparams' );
57 } elseif ( !$hide && !$show ) {
58 $this->dieWithError( 'apierror-revdel-paramneeded', 'badparams' );
59 }
60 $bits = [
61 'content' => RevisionDeleter::getRevdelConstant( $params['type'] ),
62 'comment' => RevisionRecord::DELETED_COMMENT,
63 'user' => RevisionRecord::DELETED_USER,
64 ];
65 $bitfield = [];
66 foreach ( $bits as $key => $bit ) {
67 if ( in_array( $key, $hide ) ) {
68 $bitfield[$bit] = 1;
69 } elseif ( in_array( $key, $show ) ) {
70 $bitfield[$bit] = 0;
71 } else {
72 $bitfield[$bit] = -1;
73 }
74 }
75
76 if ( $params['suppress'] === 'yes' ) {
77 $this->checkUserRightsAny( 'suppressrevision' );
78 $bitfield[RevisionRecord::DELETED_RESTRICTED] = 1;
79 } elseif ( $params['suppress'] === 'no' ) {
80 $bitfield[RevisionRecord::DELETED_RESTRICTED] = 0;
81 } else {
82 $bitfield[RevisionRecord::DELETED_RESTRICTED] = -1;
83 }
84
85 $targetObj = null;
86 if ( $params['target'] ) {
87 $targetObj = Title::newFromText( $params['target'] );
88 }
89 $targetObj = RevisionDeleter::suggestTarget( $params['type'], $targetObj, $params['ids'] );
90 if ( $targetObj === null ) {
91 $this->dieWithError( [ 'apierror-revdel-needtarget' ], 'needtarget' );
92 }
93
94 if ( $this->getPermissionManager()->isBlockedFrom( $user, $targetObj ) ) {
95 $this->dieBlocked( $user->getBlock() );
96 }
97
99 $params['type'], $this->getContext(), $targetObj, $params['ids']
100 );
101 $status = $list->setVisibility( [
102 'value' => $bitfield,
103 'comment' => $params['reason'],
104 'perItemStatus' => true,
105 'tags' => $params['tags']
106 ] );
107
108 $result = $this->getResult();
109 $data = $this->extractStatusInfo( $status );
110 $data['target'] = $targetObj->getFullText();
111 $data['items'] = [];
112
113 foreach ( $status->itemStatuses as $id => $s ) {
114 $data['items'][$id] = $this->extractStatusInfo( $s );
115 $data['items'][$id]['id'] = $id;
116 }
117
118 $list->reloadFromMaster();
119 for ( $item = $list->reset(); $list->current(); $item = $list->next() ) {
120 $data['items'][$item->getId()] += $item->getApiData( $this->getResult() );
121 }
122
123 $data['items'] = array_values( $data['items'] );
124 ApiResult::setIndexedTagName( $data['items'], 'i' );
125 $result->addValue( null, $this->getModuleName(), $data );
126 }
127
128 private function extractStatusInfo( $status ) {
129 $ret = [
130 'status' => $status->isOK() ? 'Success' : 'Fail',
131 ];
132
133 $errors = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
134 if ( $errors ) {
135 $ret['errors'] = $errors;
136 }
137 $warnings = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
138 if ( $warnings ) {
139 $ret['warnings'] = $warnings;
140 }
141
142 return $ret;
143 }
144
145 public function mustBePosted() {
146 return true;
147 }
148
149 public function isWriteMode() {
150 return true;
151 }
152
153 public function getAllowedParams() {
154 return [
155 'type' => [
158 ],
159 'target' => null,
160 'ids' => [
163 ],
164 'hide' => [
165 ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
167 ],
168 'show' => [
169 ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
171 ],
172 'suppress' => [
173 ApiBase::PARAM_TYPE => [ 'yes', 'no', 'nochange' ],
174 ApiBase::PARAM_DFLT => 'nochange',
175 ],
176 'reason' => null,
177 'tags' => [
178 ApiBase::PARAM_TYPE => 'tags',
180 ],
181 ];
182 }
183
184 public function needsToken() {
185 return 'csrf';
186 }
187
188 protected function getExamplesMessages() {
189 return [
190 'action=revisiondelete&target=Main%20Page&type=revision&ids=12345&' .
191 'hide=content&token=123ABC'
192 => 'apihelp-revisiondelete-example-revision',
193 'action=revisiondelete&type=logging&ids=67890&hide=content|comment|user&' .
194 'reason=BLP%20violation&token=123ABC'
195 => 'apihelp-revisiondelete-example-log',
196 ];
197 }
198
199 public function getHelpUrls() {
200 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Revisiondelete';
201 }
202}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:42
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition ApiBase.php:118
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition ApiBase.php:2130
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition ApiBase.php:2014
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:94
getErrorFormatter()
Get the error formatter.
Definition ApiBase.php:654
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition ApiBase.php:55
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition ApiBase.php:710
dieBlocked(AbstractBlock $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition ApiBase.php:2055
getResult()
Get the result object.
Definition ApiBase.php:640
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:761
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:520
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:2086
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1871
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition ApiBase.php:58
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
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, User $user=null)
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.
static createList( $typeName, IContextSource $context, Title $title, array $ids)
Instantiate the appropriate list class for a given list of IDs.
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.
return true
Definition router.php:94