MediaWiki  master
ApiRevisionDelete.php
Go to the documentation of this file.
1 <?php
26 
33 class ApiRevisionDelete extends ApiBase {
34 
35  public function execute() {
37 
38  $params = $this->extractRequestParams();
39  $user = $this->getUser();
40  $this->checkUserRightsAny( RevisionDeleter::getRestriction( $params['type'] ) );
41 
42  if ( !$params['ids'] ) {
43  $this->dieWithError( [ 'apierror-paramempty', 'ids' ], 'paramempty_ids' );
44  }
45 
46  // Check if user can add tags
47  if ( $params['tags'] ) {
48  $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
49  if ( !$ableToTag->isOK() ) {
50  $this->dieStatus( $ableToTag );
51  }
52  }
53 
54  $hide = $params['hide'] ?: [];
55  $show = $params['show'] ?: [];
56  if ( array_intersect( $hide, $show ) ) {
57  $this->dieWithError( 'apierror-revdel-mutuallyexclusive', 'badparams' );
58  } elseif ( !$hide && !$show ) {
59  $this->dieWithError( 'apierror-revdel-paramneeded', 'badparams' );
60  }
61  $bits = [
62  'content' => RevisionDeleter::getRevdelConstant( $params['type'] ),
63  'comment' => RevisionRecord::DELETED_COMMENT,
64  'user' => RevisionRecord::DELETED_USER,
65  ];
66  $bitfield = [];
67  foreach ( $bits as $key => $bit ) {
68  if ( in_array( $key, $hide ) ) {
69  $bitfield[$bit] = 1;
70  } elseif ( in_array( $key, $show ) ) {
71  $bitfield[$bit] = 0;
72  } else {
73  $bitfield[$bit] = -1;
74  }
75  }
76 
77  if ( $params['suppress'] === 'yes' ) {
78  $this->checkUserRightsAny( 'suppressrevision' );
79  $bitfield[RevisionRecord::DELETED_RESTRICTED] = 1;
80  } elseif ( $params['suppress'] === 'no' ) {
81  $bitfield[RevisionRecord::DELETED_RESTRICTED] = 0;
82  } else {
83  $bitfield[RevisionRecord::DELETED_RESTRICTED] = -1;
84  }
85 
86  $targetObj = null;
87  if ( $params['target'] ) {
88  $targetObj = Title::newFromText( $params['target'] );
89  }
90  $targetObj = RevisionDeleter::suggestTarget( $params['type'], $targetObj, $params['ids'] );
91  if ( $targetObj === null ) {
92  $this->dieWithError( [ 'apierror-revdel-needtarget' ], 'needtarget' );
93  }
94 
95  // TODO: replace use of PermissionManager
96  if ( $this->getPermissionManager()->isBlockedFrom( $user, $targetObj ) ) {
97  // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Block is checked and not null
98  $this->dieBlocked( $user->getBlock() );
99  }
100 
102  $params['type'], $this->getContext(), $targetObj, $params['ids']
103  );
104  $status = $list->setVisibility( [
105  'value' => $bitfield,
106  'comment' => $params['reason'] ?? '',
107  'perItemStatus' => true,
108  'tags' => $params['tags']
109  ] );
110 
111  $result = $this->getResult();
112  $data = $this->extractStatusInfo( $status );
113  $data['target'] = $targetObj->getFullText();
114  $data['items'] = [];
115 
116  foreach ( $status->itemStatuses as $id => $s ) {
117  $data['items'][$id] = $this->extractStatusInfo( $s );
118  $data['items'][$id]['id'] = $id;
119  }
120 
121  $list->reloadFromPrimary();
122  for ( $item = $list->reset(); $list->current(); $item = $list->next() ) {
123  $data['items'][$item->getId()] += $item->getApiData( $this->getResult() );
124  }
125 
126  $data['items'] = array_values( $data['items'] );
127  ApiResult::setIndexedTagName( $data['items'], 'i' );
128  $result->addValue( null, $this->getModuleName(), $data );
129  }
130 
131  private function extractStatusInfo( Status $status ) {
132  $ret = [
133  'status' => $status->isOK() ? 'Success' : 'Fail',
134  ];
135 
136  $errors = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
137  if ( $errors ) {
138  $ret['errors'] = $errors;
139  }
140  $warnings = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
141  if ( $warnings ) {
142  $ret['warnings'] = $warnings;
143  }
144 
145  return $ret;
146  }
147 
148  public function mustBePosted() {
149  return true;
150  }
151 
152  public function isWriteMode() {
153  return true;
154  }
155 
156  public function getAllowedParams() {
157  return [
158  'type' => [
159  ParamValidator::PARAM_TYPE => RevisionDeleter::getTypes(),
160  ParamValidator::PARAM_REQUIRED => true
161  ],
162  'target' => null,
163  'ids' => [
164  ParamValidator::PARAM_ISMULTI => true,
165  ParamValidator::PARAM_REQUIRED => true
166  ],
167  'hide' => [
168  ParamValidator::PARAM_TYPE => [ 'content', 'comment', 'user' ],
169  ParamValidator::PARAM_ISMULTI => true,
170  ],
171  'show' => [
172  ParamValidator::PARAM_TYPE => [ 'content', 'comment', 'user' ],
173  ParamValidator::PARAM_ISMULTI => true,
174  ],
175  'suppress' => [
176  ParamValidator::PARAM_TYPE => [ 'yes', 'no', 'nochange' ],
177  ParamValidator::PARAM_DEFAULT => 'nochange',
178  ],
179  'reason' => [
180  ParamValidator::PARAM_TYPE => 'string'
181  ],
182  'tags' => [
183  ParamValidator::PARAM_TYPE => 'tags',
184  ParamValidator::PARAM_ISMULTI => true,
185  ],
186  ];
187  }
188 
189  public function needsToken() {
190  return 'csrf';
191  }
192 
193  protected function getExamplesMessages() {
194  return [
195  'action=revisiondelete&target=Main%20Page&type=revision&ids=12345&' .
196  'hide=content&token=123ABC'
197  => 'apihelp-revisiondelete-example-revision',
198  'action=revisiondelete&type=logging&ids=67890&hide=content|comment|user&' .
199  'reason=BLP%20violation&token=123ABC'
200  => 'apihelp-revisiondelete-example-log',
201  ];
202  }
203 
204  public function getHelpUrls() {
205  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Revisiondelete';
206  }
207 }
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:57
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition: ApiBase.php:1455
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition: ApiBase.php:1561
getErrorFormatter()
Definition: ApiBase.php:641
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition: ApiBase.php:687
getResult()
Get the result object.
Definition: ApiBase.php:630
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:766
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:499
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1516
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1300
dieBlocked(Block $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition: ApiBase.php:1484
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Definition: ApiResult.php:604
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...
Definition: ChangeTags.php:635
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:45
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:373
Service for formatting and validating API parameters.
foreach( $mmfl['setupFiles'] as $fileName) if( $queue) if(empty( $mmfl['quiet'])) $s
return true
Definition: router.php:90