MediaWiki  1.34.0
ApiRevisionDelete.php
Go to the documentation of this file.
1 <?php
25 
32 class ApiRevisionDelete extends ApiBase {
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' => [
161  ApiBase::PARAM_ISMULTI => true,
163  ],
164  'hide' => [
165  ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
166  ApiBase::PARAM_ISMULTI => true,
167  ],
168  'show' => [
169  ApiBase::PARAM_TYPE => [ 'content', 'comment', 'user' ],
170  ApiBase::PARAM_ISMULTI => true,
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',
179  ApiBase::PARAM_ISMULTI => true,
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 }
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:316
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:40
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:118
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2014
true
return true
Definition: router.php:92
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:94
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:640
ApiBase\checkUserRightsAny
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition: ApiBase.php:2130
ApiRevisionDelete\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiRevisionDelete.php:153
$s
$s
Definition: mergeMessageFileList.php:185
ApiBase\dieBlocked
dieBlocked(AbstractBlock $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition: ApiBase.php:2055
ContextSource\getUser
getUser()
Definition: ContextSource.php:120
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:42
ApiRevisionDelete
API interface to RevDel.
Definition: ApiRevisionDelete.php:32
RevisionDeleter\getTypes
static getTypes()
Lists the valid possible types for revision deletion.
Definition: RevisionDeleter.php:56
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:761
RevisionDeleter\createList
static createList( $typeName, IContextSource $context, Title $title, array $ids)
Instantiate the appropriate list class for a given list of IDs.
Definition: RevisionDeleter.php:85
ApiRevisionDelete\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiRevisionDelete.php:199
ApiRevisionDelete\extractStatusInfo
extractStatusInfo( $status)
Definition: ApiRevisionDelete.php:128
RevisionDeleter\getRevdelConstant
static getRevdelConstant( $typeName)
Get the revision deletion constant for the RevDel type.
Definition: RevisionDeleter.php:184
ApiResult\setIndexedTagName
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Definition: ApiResult.php:616
ApiBase\getPermissionManager
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition: ApiBase.php:710
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1871
ChangeTags\canAddTagsAccompanyingChange
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...
Definition: ChangeTags.php:521
$status
return $status
Definition: SyntaxHighlight.php:347
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:55
ApiRevisionDelete\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiRevisionDelete.php:149
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:2086
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:520
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:58
ApiRevisionDelete\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiRevisionDelete.php:145
ApiRevisionDelete\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiRevisionDelete.php:188
RevisionDeleter\suggestTarget
static suggestTarget( $typeName, $target, array $ids)
Suggest a target for the revision deletion.
Definition: RevisionDeleter.php:200
RevisionDeleter\getRestriction
static getRestriction( $typeName)
Get the user right required for the RevDel type.
Definition: RevisionDeleter.php:170
ApiBase\getErrorFormatter
getErrorFormatter()
Get the error formatter.
Definition: ApiBase.php:654
ApiRevisionDelete\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiRevisionDelete.php:184
ApiRevisionDelete\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiRevisionDelete.php:34