MediaWiki  1.29.2
ApiDelete.php
Go to the documentation of this file.
1 <?php
33 class ApiDelete extends ApiBase {
41  public function execute() {
43 
44  $params = $this->extractRequestParams();
45 
46  $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
47  if ( !$pageObj->exists() ) {
48  $this->dieWithError( 'apierror-missingtitle' );
49  }
50 
51  $titleObj = $pageObj->getTitle();
52  $reason = $params['reason'];
53  $user = $this->getUser();
54 
55  // Check that the user is allowed to carry out the deletion
56  $this->checkTitleUserPermissions( $titleObj, 'delete' );
57 
58  // If change tagging was requested, check that the user is allowed to tag,
59  // and the tags are valid
60  if ( count( $params['tags'] ) ) {
62  if ( !$tagStatus->isOK() ) {
63  $this->dieStatus( $tagStatus );
64  }
65  }
66 
67  if ( $titleObj->getNamespace() == NS_FILE ) {
69  $pageObj,
70  $user,
71  $params['oldimage'],
72  $reason,
73  false,
74  $params['tags']
75  );
76  } else {
77  $status = self::delete( $pageObj, $user, $reason, $params['tags'] );
78  }
79 
80  if ( !$status->isGood() ) {
81  $this->dieStatus( $status );
82  }
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  'logid' => $status->value
98  ];
99  $this->getResult()->addValue( null, $this->getModuleName(), $r );
100  }
101 
111  protected static function delete( Page $page, User $user, &$reason = null, $tags = [] ) {
112  $title = $page->getTitle();
113 
114  // Auto-generate a summary, if necessary
115  if ( is_null( $reason ) ) {
116  // Need to pass a throwaway variable because generateReason expects
117  // a reference
118  $hasHistory = false;
119  $reason = $page->getAutoDeleteReason( $hasHistory );
120  if ( $reason === false ) {
121  return Status::newFatal( 'cannotdelete', $title->getPrefixedText() );
122  }
123  }
124 
125  $error = '';
126 
127  // Luckily, Article.php provides a reusable delete function that does the hard work for us
128  return $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user, $tags );
129  }
130 
140  protected static function deleteFile( Page $page, User $user, $oldimage,
141  &$reason = null, $suppress = false, $tags = []
142  ) {
143  $title = $page->getTitle();
144 
145  $file = $page->getFile();
146  if ( !$file->exists() || !$file->isLocal() || $file->getRedirected() ) {
147  return self::delete( $page, $user, $reason, $tags );
148  }
149 
150  if ( $oldimage ) {
151  if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
152  return Status::newFatal( 'invalidoldimage' );
153  }
154  $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
155  if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
156  return Status::newFatal( 'nodeleteablefile' );
157  }
158  }
159 
160  if ( is_null( $reason ) ) { // Log and RC don't like null reasons
161  $reason = '';
162  }
163 
164  return FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress, $user, $tags );
165  }
166 
167  public function mustBePosted() {
168  return true;
169  }
170 
171  public function isWriteMode() {
172  return true;
173  }
174 
175  public function getAllowedParams() {
176  return [
177  'title' => null,
178  'pageid' => [
179  ApiBase::PARAM_TYPE => 'integer'
180  ],
181  'reason' => null,
182  'tags' => [
183  ApiBase::PARAM_TYPE => 'tags',
184  ApiBase::PARAM_ISMULTI => true,
185  ],
186  'watch' => [
187  ApiBase::PARAM_DFLT => false,
189  ],
190  'watchlist' => [
191  ApiBase::PARAM_DFLT => 'preferences',
193  'watch',
194  'unwatch',
195  'preferences',
196  'nochange'
197  ],
198  ],
199  'unwatch' => [
200  ApiBase::PARAM_DFLT => false,
202  ],
203  'oldimage' => null,
204  ];
205  }
206 
207  public function needsToken() {
208  return 'csrf';
209  }
210 
211  protected function getExamplesMessages() {
212  return [
213  'action=delete&title=Main%20Page&token=123ABC'
214  => 'apihelp-delete-example-simple',
215  'action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
216  => 'apihelp-delete-example-reason',
217  ];
218  }
219 
220  public function getHelpUrls() {
221  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Delete';
222  }
223 }
Page
Interface for type hinting (accepts WikiPage, Article, ImagePage, CategoryPage)
Definition: Page.php:24
RepoGroup\singleton
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:59
ApiDelete\deleteFile
static deleteFile(Page $page, User $user, $oldimage, &$reason=null, $suppress=false, $tags=[])
Definition: ApiDelete.php:140
captcha-old.count
count
Definition: captcha-old.py:225
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1796
ApiBase\getTitleOrPageId
getTitleOrPageId( $params, $load=false)
Get a WikiPage object from a title or pageid param, if possible.
Definition: ApiBase.php:895
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:91
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:610
$status
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1049
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:246
StatusValue\newFatal
static newFatal( $message)
Factory function for fatal errors.
Definition: StatusValue.php:63
NS_FILE
const NS_FILE
Definition: Defines.php:68
$params
$params
Definition: styleTest.css.php:40
ContextSource\getUser
getUser()
Get the User object.
Definition: ContextSource.php:133
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:41
ApiBase\PARAM_DEPRECATED
const PARAM_DEPRECATED
(boolean) Is the parameter deprecated (will show a warning)?
Definition: ApiBase.php:109
ApiDelete\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiDelete.php:207
ApiDelete\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiDelete.php:175
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
ApiDelete
API module that facilitates deleting pages.
Definition: ApiDelete.php:33
ApiDelete\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiDelete.php:167
$page
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition: hooks.txt:2536
ApiDelete\execute
execute()
Extracts the title and reason from the request parameters and invokes the local delete() function wit...
Definition: ApiDelete.php:41
ApiBase\setWatch
setWatch( $watch, $titleObj, $userOption=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
Definition: ApiBase.php:1557
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:718
ApiDelete\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiDelete.php:211
ApiDelete\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiDelete.php:220
ApiBase\checkTitleUserPermissions
checkTitleUserPermissions(Title $title, $actions, $user=null)
Helper function for permission-denied errors.
Definition: ApiBase.php:1908
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:2440
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:111
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:395
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:52
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1861
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:490
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:55
ApiDelete\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiDelete.php:171
FileDeleteForm\doDelete
static doDelete(&$title, &$file, &$oldimage, $reason, $suppress, User $user=null, $tags=[])
Really delete the file.
Definition: FileDeleteForm.php:158
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:50
FileDeleteForm\isValidOldSpec
static isValidOldSpec( $oldimage)
Is the provided oldimage value valid?
Definition: FileDeleteForm.php:372