MediaWiki REL1_37
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'], $this->getAuthority() );
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 // TODO: replace use of PermissionManager
95 if ( $this->getPermissionManager()->isBlockedFrom( $user, $targetObj ) ) {
96 $this->dieBlocked( $user->getBlock() );
97 }
98
100 $params['type'], $this->getContext(), $targetObj, $params['ids']
101 );
102 $status = $list->setVisibility( [
103 'value' => $bitfield,
104 'comment' => $params['reason'],
105 'perItemStatus' => true,
106 'tags' => $params['tags']
107 ] );
108
109 $result = $this->getResult();
110 $data = $this->extractStatusInfo( $status );
111 $data['target'] = $targetObj->getFullText();
112 $data['items'] = [];
113
114 foreach ( $status->itemStatuses as $id => $s ) {
115 $data['items'][$id] = $this->extractStatusInfo( $s );
116 $data['items'][$id]['id'] = $id;
117 }
118
119 $list->reloadFromPrimary();
120 for ( $item = $list->reset(); $list->current(); $item = $list->next() ) {
121 $data['items'][$item->getId()] += $item->getApiData( $this->getResult() );
122 }
123
124 $data['items'] = array_values( $data['items'] );
125 ApiResult::setIndexedTagName( $data['items'], 'i' );
126 $result->addValue( null, $this->getModuleName(), $data );
127 }
128
129 private function extractStatusInfo( Status $status ) {
130 $ret = [
131 'status' => $status->isOK() ? 'Success' : 'Fail',
132 ];
133
134 $errors = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
135 if ( $errors ) {
136 $ret['errors'] = $errors;
137 }
138 $warnings = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
139 if ( $warnings ) {
140 $ret['warnings'] = $warnings;
141 }
142
143 return $ret;
144 }
145
146 public function mustBePosted() {
147 return true;
148 }
149
150 public function isWriteMode() {
151 return true;
152 }
153
154 public function getAllowedParams() {
155 return [
156 'type' => [
159 ],
160 'target' => null,
161 'ids' => [
164 ],
165 'hide' => [
166 ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
168 ],
169 'show' => [
170 ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
172 ],
173 'suppress' => [
174 ApiBase::PARAM_TYPE => [ 'yes', 'no', 'nochange' ],
175 ApiBase::PARAM_DFLT => 'nochange',
176 ],
177 'reason' => null,
178 'tags' => [
179 ApiBase::PARAM_TYPE => 'tags',
181 ],
182 ];
183 }
184
185 public function needsToken() {
186 return 'csrf';
187 }
188
189 protected function getExamplesMessages() {
190 return [
191 'action=revisiondelete&target=Main%20Page&type=revision&ids=12345&' .
192 'hide=content&token=123ABC'
193 => 'apihelp-revisiondelete-example-revision',
194 'action=revisiondelete&type=logging&ids=67890&hide=content|comment|user&' .
195 'reason=BLP%20violation&token=123ABC'
196 => 'apihelp-revisiondelete-example-log',
197 ];
198 }
199
200 public function getHelpUrls() {
201 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Revisiondelete';
202 }
203}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:55
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1436
const PARAM_REQUIRED
Definition ApiBase.php:105
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition ApiBase.php:1539
const PARAM_TYPE
Definition ApiBase.php:81
getErrorFormatter()
Definition ApiBase.php:639
const PARAM_DFLT
Definition ApiBase.php:73
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition ApiBase.php:685
getResult()
Get the result object.
Definition ApiBase.php:628
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:764
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:497
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1495
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1293
dieBlocked(Block $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition ApiBase.php:1463
const PARAM_ISMULTI
Definition ApiBase.php:77
API interface to RevDel.
isWriteMode()
Indicates whether this module requires write mode.
mustBePosted()
Indicates whether this module must be called with a POST request.
extractStatusInfo(Status $status)
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)
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 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:44
foreach( $mmfl['setupFiles'] as $fileName) if($queue) if(empty( $mmfl['quiet'])) $s
return true
Definition router.php:92