MediaWiki  master
ApiDelete.php
Go to the documentation of this file.
1 <?php
24 
31 class ApiDelete extends ApiBase {
39  public function execute() {
41 
42  $params = $this->extractRequestParams();
43 
44  $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
45  $titleObj = $pageObj->getTitle();
46  if ( !$pageObj->exists() &&
47  // @phan-suppress-next-line PhanUndeclaredMethod
48  !( $titleObj->getNamespace() == NS_FILE && self::canDeleteFile( $pageObj->getFile() ) )
49  ) {
50  $this->dieWithError( 'apierror-missingtitle' );
51  }
52 
53  $reason = $params['reason'];
54  $user = $this->getUser();
55 
56  // Check that the user is allowed to carry out the deletion
57  $this->checkTitleUserPermissions( $titleObj, 'delete' );
58 
59  // If change tagging was requested, check that the user is allowed to tag,
60  // and the tags are valid
61  if ( $params['tags'] ) {
62  $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
63  if ( !$tagStatus->isOK() ) {
64  $this->dieStatus( $tagStatus );
65  }
66  }
67 
68  if ( $titleObj->getNamespace() == NS_FILE ) {
69  $status = self::deleteFile(
70  $pageObj,
71  $user,
72  $params['oldimage'],
73  $reason,
74  false,
75  $params['tags']
76  );
77  } else {
78  $status = self::delete( $pageObj, $user, $reason, $params['tags'] );
79  }
80 
81  if ( !$status->isOK() ) {
82  $this->dieStatus( $status );
83  }
84  $this->addMessagesFromStatus( $status, [ 'warning' ], [ 'delete-scheduled' ] );
85 
86  // Deprecated parameters
87  if ( $params['watch'] ) {
88  $watch = 'watch';
89  } elseif ( $params['unwatch'] ) {
90  $watch = 'unwatch';
91  } else {
92  $watch = $params['watchlist'];
93  }
94  $this->setWatch( $watch, $titleObj, 'watchdeletion' );
95 
96  $r = [
97  'title' => $titleObj->getPrefixedText(),
98  'reason' => $reason,
99  ];
100  if ( $status->hasMessage( 'delete-scheduled' ) ) {
101  $r['scheduled'] = true;
102  }
103  if ( $status->value !== null ) {
104  // Scheduled deletions don't currently have a log entry available at this point
105  $r['logid'] = $status->value;
106  }
107  $this->getResult()->addValue( null, $this->getModuleName(), $r );
108  }
109 
119  private static function delete( WikiPage $page, User $user, &$reason = null, $tags = [] ) {
120  $title = $page->getTitle();
121 
122  // Auto-generate a summary, if necessary
123  if ( $reason === null ) {
124  // Need to pass a throwaway variable because generateReason expects
125  // a reference
126  $hasHistory = false;
127  $reason = $page->getAutoDeleteReason( $hasHistory );
128  if ( $reason === false ) {
129  // Should be reachable only if the page has no revisions
130  return Status::newFatal( 'cannotdelete', $title->getPrefixedText() ); // @codeCoverageIgnore
131  }
132  }
133 
134  $error = '';
135 
136  // Luckily, WikiPage provides a reusable delete function that does the hard work for us
137  return $page->doDeleteArticleReal(
138  $reason,
139  $user,
140  false, // don't suppress
141  null, // unused
142  $error,
143  null, // unused
144  $tags
145  );
146  }
147 
152  protected static function canDeleteFile( File $file ) {
153  return $file->exists() && $file->isLocal() && !$file->getRedirected();
154  }
155 
165  private static function deleteFile( WikiPage $page, User $user, $oldimage,
166  &$reason = null, $suppress = false, $tags = []
167  ) {
168  $title = $page->getTitle();
169 
170  // @phan-suppress-next-line PhanUndeclaredMethod There's no right typehint for it
171  $file = $page->getFile();
172  if ( !self::canDeleteFile( $file ) ) {
173  return self::delete( $page, $user, $reason, $tags );
174  }
175 
176  if ( $oldimage ) {
177  if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
178  return Status::newFatal( 'invalidoldimage' );
179  }
180  $oldfile = MediaWikiServices::getInstance()->getRepoGroup()
181  ->getLocalRepo()->newFromArchiveName( $title, $oldimage );
182  if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
183  return Status::newFatal( 'nodeleteablefile' );
184  }
185  }
186 
187  if ( $reason === null ) { // Log and RC don't like null reasons
188  $reason = '';
189  }
190 
191  return FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress, $user, $tags );
192  }
193 
194  public function mustBePosted() {
195  return true;
196  }
197 
198  public function isWriteMode() {
199  return true;
200  }
201 
202  public function getAllowedParams() {
203  return [
204  'title' => null,
205  'pageid' => [
206  ApiBase::PARAM_TYPE => 'integer'
207  ],
208  'reason' => null,
209  'tags' => [
210  ApiBase::PARAM_TYPE => 'tags',
211  ApiBase::PARAM_ISMULTI => true,
212  ],
213  'watch' => [
214  ApiBase::PARAM_DFLT => false,
216  ],
217  'watchlist' => [
218  ApiBase::PARAM_DFLT => 'preferences',
220  'watch',
221  'unwatch',
222  'preferences',
223  'nochange'
224  ],
225  ],
226  'unwatch' => [
227  ApiBase::PARAM_DFLT => false,
229  ],
230  'oldimage' => null,
231  ];
232  }
233 
234  public function needsToken() {
235  return 'csrf';
236  }
237 
238  protected function getExamplesMessages() {
239  return [
240  'action=delete&title=Main%20Page&token=123ABC'
241  => 'apihelp-delete-example-simple',
242  'action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
243  => 'apihelp-delete-example-reason',
244  ];
245  }
246 
247  public function getHelpUrls() {
248  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Delete';
249  }
250 }
WikiPage\getAutoDeleteReason
getAutoDeleteReason(&$hasHistory)
Auto-generates a deletion reason.
Definition: WikiPage.php:3702
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:146
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1415
ApiBase\getTitleOrPageId
getTitleOrPageId( $params, $load=false)
Get a WikiPage object from a title or pageid param, if possible.
Definition: ApiBase.php:964
ApiBase\PARAM_TYPE
const PARAM_TYPE
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:68
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:546
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:49
NS_FILE
const NS_FILE
Definition: Defines.php:75
$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:1394
ApiDelete\canDeleteFile
static canDeleteFile(File $file)
Definition: ApiDelete.php:152
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:50
ApiBase\PARAM_DEPRECATED
const PARAM_DEPRECATED
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:73
ApiDelete\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiDelete.php:234
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:62
ApiDelete\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiDelete.php:202
ApiDelete
API module that facilitates deleting pages.
Definition: ApiDelete.php:31
ApiDelete\delete
static delete(WikiPage $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:119
ApiDelete\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiDelete.php:194
ApiDelete\execute
execute()
Extracts the title and reason from the request parameters and invokes the local delete() function wit...
Definition: ApiDelete.php:39
WikiPage\getTitle
getTitle()
Get the title object of the article.
Definition: WikiPage.php:310
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:695
$title
$title
Definition: testCompression.php:38
ApiBase\setWatch
setWatch( $watch, $titleObj, $userOption=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
Definition: ApiBase.php:1151
ApiDelete\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiDelete.php:238
ApiDelete\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiDelete.php:247
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1272
ApiDelete\deleteFile
static deleteFile(WikiPage $page, User $user, $oldimage, &$reason=null, $suppress=false, $tags=[])
Definition: ApiDelete.php:165
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:531
ApiBase\PARAM_DFLT
const PARAM_DFLT
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:66
WikiPage\doDeleteArticleReal
doDeleteArticleReal( $reason, $user=false, $suppress=false, $u2=null, &$error='', User $deleter=null, $tags=[], $logsubtype='delete', $immediate=false)
Back-end article deletion Deletes the article with database consistency, writes logs,...
Definition: WikiPage.php:2715
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1473
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:426
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:67
ApiDelete\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiDelete.php:198
FileDeleteForm\doDelete
static doDelete(&$title, &$file, &$oldimage, $reason, $suppress, User $user=null, $tags=[])
Really delete the file.
Definition: FileDeleteForm.php:192
ApiBase\checkTitleUserPermissions
checkTitleUserPermissions(LinkTarget $linkTarget, $actions, array $options=[])
Helper function for permission-denied errors.
Definition: ApiBase.php:1542
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:55
FileDeleteForm\isValidOldSpec
static isValidOldSpec( $oldimage)
Is the provided oldimage value valid?
Definition: FileDeleteForm.php:485