MediaWiki  1.34.0
ApiDelete.php
Go to the documentation of this file.
1 <?php
29 class ApiDelete extends ApiBase {
37  public function execute() {
39 
40  $params = $this->extractRequestParams();
41 
42  $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
43  $titleObj = $pageObj->getTitle();
44  if ( !$pageObj->exists() &&
45  // @phan-suppress-next-line PhanUndeclaredMethod
46  !( $titleObj->getNamespace() == NS_FILE && self::canDeleteFile( $pageObj->getFile() ) )
47  ) {
48  $this->dieWithError( 'apierror-missingtitle' );
49  }
50 
51  $reason = $params['reason'];
52  $user = $this->getUser();
53 
54  // Check that the user is allowed to carry out the deletion
55  $this->checkTitleUserPermissions( $titleObj, 'delete' );
56 
57  // If change tagging was requested, check that the user is allowed to tag,
58  // and the tags are valid
59  if ( $params['tags'] ) {
60  $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
61  if ( !$tagStatus->isOK() ) {
62  $this->dieStatus( $tagStatus );
63  }
64  }
65 
66  if ( $titleObj->getNamespace() == NS_FILE ) {
68  $pageObj,
69  $user,
70  $params['oldimage'],
71  $reason,
72  false,
73  $params['tags']
74  );
75  } else {
76  $status = self::delete( $pageObj, $user, $reason, $params['tags'] );
77  }
78 
79  if ( !$status->isOK() ) {
80  $this->dieStatus( $status );
81  }
82  $this->addMessagesFromStatus( $status, [ 'warning' ], [ 'delete-scheduled' ] );
83 
84  // Deprecated parameters
85  if ( $params['watch'] ) {
86  $watch = 'watch';
87  } elseif ( $params['unwatch'] ) {
88  $watch = 'unwatch';
89  } else {
90  $watch = $params['watchlist'];
91  }
92  $this->setWatch( $watch, $titleObj, 'watchdeletion' );
93 
94  $r = [
95  'title' => $titleObj->getPrefixedText(),
96  'reason' => $reason,
97  ];
98  if ( $status->hasMessage( 'delete-scheduled' ) ) {
99  $r['scheduled'] = true;
100  }
101  if ( $status->value !== null ) {
102  // Scheduled deletions don't currently have a log entry available at this point
103  $r['logid'] = $status->value;
104  }
105  $this->getResult()->addValue( null, $this->getModuleName(), $r );
106  }
107 
117  protected static function delete( Page $page, User $user, &$reason = null, $tags = [] ) {
118  $title = $page->getTitle();
119 
120  // Auto-generate a summary, if necessary
121  if ( is_null( $reason ) ) {
122  // Need to pass a throwaway variable because generateReason expects
123  // a reference
124  $hasHistory = false;
125  $reason = $page->getAutoDeleteReason( $hasHistory );
126  if ( $reason === false ) {
127  // Should be reachable only if the page has no revisions
128  return Status::newFatal( 'cannotdelete', $title->getPrefixedText() ); // @codeCoverageIgnore
129  }
130  }
131 
132  $error = '';
133 
134  // Luckily, Article.php provides a reusable delete function that does the hard work for us
135  return $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user, $tags );
136  }
137 
142  protected static function canDeleteFile( File $file ) {
143  return $file->exists() && $file->isLocal() && !$file->getRedirected();
144  }
145 
155  protected static function deleteFile( Page $page, User $user, $oldimage,
156  &$reason = null, $suppress = false, $tags = []
157  ) {
158  $title = $page->getTitle();
159 
160  // @phan-suppress-next-line PhanUndeclaredMethod There's no right typehint for it
161  $file = $page->getFile();
162  if ( !self::canDeleteFile( $file ) ) {
163  return self::delete( $page, $user, $reason, $tags );
164  }
165 
166  if ( $oldimage ) {
167  if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
168  return Status::newFatal( 'invalidoldimage' );
169  }
170  $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
171  if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
172  return Status::newFatal( 'nodeleteablefile' );
173  }
174  }
175 
176  if ( is_null( $reason ) ) { // Log and RC don't like null reasons
177  $reason = '';
178  }
179 
180  return FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress, $user, $tags );
181  }
182 
183  public function mustBePosted() {
184  return true;
185  }
186 
187  public function isWriteMode() {
188  return true;
189  }
190 
191  public function getAllowedParams() {
192  return [
193  'title' => null,
194  'pageid' => [
195  ApiBase::PARAM_TYPE => 'integer'
196  ],
197  'reason' => null,
198  'tags' => [
199  ApiBase::PARAM_TYPE => 'tags',
200  ApiBase::PARAM_ISMULTI => true,
201  ],
202  'watch' => [
203  ApiBase::PARAM_DFLT => false,
205  ],
206  'watchlist' => [
207  ApiBase::PARAM_DFLT => 'preferences',
209  'watch',
210  'unwatch',
211  'preferences',
212  'nochange'
213  ],
214  ],
215  'unwatch' => [
216  ApiBase::PARAM_DFLT => false,
218  ],
219  'oldimage' => null,
220  ];
221  }
222 
223  public function needsToken() {
224  return 'csrf';
225  }
226 
227  protected function getExamplesMessages() {
228  return [
229  'action=delete&title=Main%20Page&token=123ABC'
230  => 'apihelp-delete-example-simple',
231  'action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
232  => 'apihelp-delete-example-reason',
233  ];
234  }
235 
236  public function getHelpUrls() {
237  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Delete';
238  }
239 }
Page
Interface for type hinting (accepts WikiPage, Article, ImagePage, CategoryPage)
Definition: Page.php:29
RepoGroup\singleton
static singleton()
Definition: RepoGroup.php:60
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
ApiDelete\deleteFile
static deleteFile(Page $page, User $user, $oldimage, &$reason=null, $suppress=false, $tags=[])
Definition: ApiDelete.php:155
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2014
ApiBase\getTitleOrPageId
getTitleOrPageId( $params, $load=false)
Get a WikiPage object from a title or pageid param, if possible.
Definition: ApiBase.php:1034
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
NS_FILE
const NS_FILE
Definition: Defines.php:66
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
ApiBase\addMessagesFromStatus
addMessagesFromStatus(StatusValue $status, $types=[ 'warning', 'error'], array $filter=[])
Add warnings and/or errors from a Status.
Definition: ApiBase.php:1993
ApiDelete\canDeleteFile
static canDeleteFile(File $file)
Definition: ApiDelete.php:142
ApiBase\checkTitleUserPermissions
checkTitleUserPermissions(LinkTarget $linkTarget, $actions, $options=[])
Helper function for permission-denied errors.
Definition: ApiBase.php:2156
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
ApiBase\PARAM_DEPRECATED
const PARAM_DEPRECATED
(boolean) Is the parameter deprecated (will show a warning)?
Definition: ApiBase.php:112
ApiDelete\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiDelete.php:223
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:61
ApiDelete\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiDelete.php:191
ApiDelete
API module that facilitates deleting pages.
Definition: ApiDelete.php:29
ApiDelete\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiDelete.php:183
ApiDelete\execute
execute()
Extracts the title and reason from the request parameters and invokes the local delete() function wit...
Definition: ApiDelete.php:37
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:761
$title
$title
Definition: testCompression.php:34
ApiBase\setWatch
setWatch( $watch, $titleObj, $userOption=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
Definition: ApiBase.php:1750
ApiDelete\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiDelete.php:227
ApiDelete\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiDelete.php:236
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1871
ApiDelete\delete
static delete(Page $page, User $user, &$reason=null, $tags=[])
We have our own delete() function, since Article.php's implementation is split in two phases.
Definition: ApiDelete.php:117
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
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
ApiDelete\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiDelete.php:187
FileDeleteForm\doDelete
static doDelete(&$title, &$file, &$oldimage, $reason, $suppress, User $user=null, $tags=[])
Really delete the file.
Definition: FileDeleteForm.php:159
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
FileDeleteForm\isValidOldSpec
static isValidOldSpec( $oldimage)
Is the provided oldimage value valid?
Definition: FileDeleteForm.php:443