MediaWiki  1.28.0
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->dieUsageMsg( 'notanarticle' );
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  $errors = $titleObj->getUserPermissionsErrors( 'delete', $user );
57  if ( count( $errors ) ) {
58  $this->dieUsageMsg( $errors[0] );
59  }
60 
61  // If change tagging was requested, check that the user is allowed to tag,
62  // and the tags are valid
63  if ( count( $params['tags'] ) ) {
65  if ( !$tagStatus->isOK() ) {
66  $this->dieStatus( $tagStatus );
67  }
68  }
69 
70  if ( $titleObj->getNamespace() == NS_FILE ) {
71  $status = self::deleteFile(
72  $pageObj,
73  $user,
74  $params['oldimage'],
75  $reason,
76  false,
77  $params['tags']
78  );
79  } else {
80  $status = self::delete( $pageObj, $user, $reason, $params['tags'] );
81  }
82 
83  if ( is_array( $status ) ) {
84  $this->dieUsageMsg( $status[0] );
85  }
86  if ( !$status->isGood() ) {
87  $this->dieStatus( $status );
88  }
89 
90  // Deprecated parameters
91  if ( $params['watch'] ) {
92  $watch = 'watch';
93  } elseif ( $params['unwatch'] ) {
94  $watch = 'unwatch';
95  } else {
96  $watch = $params['watchlist'];
97  }
98  $this->setWatch( $watch, $titleObj, 'watchdeletion' );
99 
100  $r = [
101  'title' => $titleObj->getPrefixedText(),
102  'reason' => $reason,
103  '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  return [ [ 'cannotdelete', $title->getPrefixedText() ] ];
128  }
129  }
130 
131  $error = '';
132 
133  // Luckily, Article.php provides a reusable delete function that does the hard work for us
134  return $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user, $tags );
135  }
136 
146  protected static function deleteFile( Page $page, User $user, $oldimage,
147  &$reason = null, $suppress = false, $tags = []
148  ) {
149  $title = $page->getTitle();
150 
151  $file = $page->getFile();
152  if ( !$file->exists() || !$file->isLocal() || $file->getRedirected() ) {
153  return self::delete( $page, $user, $reason, $tags );
154  }
155 
156  if ( $oldimage ) {
157  if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
158  return [ [ 'invalidoldimage' ] ];
159  }
160  $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
161  if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
162  return [ [ 'nodeleteablefile' ] ];
163  }
164  }
165 
166  if ( is_null( $reason ) ) { // Log and RC don't like null reasons
167  $reason = '';
168  }
169 
170  return FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress, $user, $tags );
171  }
172 
173  public function mustBePosted() {
174  return true;
175  }
176 
177  public function isWriteMode() {
178  return true;
179  }
180 
181  public function getAllowedParams() {
182  return [
183  'title' => null,
184  'pageid' => [
185  ApiBase::PARAM_TYPE => 'integer'
186  ],
187  'reason' => null,
188  'tags' => [
189  ApiBase::PARAM_TYPE => 'tags',
190  ApiBase::PARAM_ISMULTI => true,
191  ],
192  'watch' => [
193  ApiBase::PARAM_DFLT => false,
195  ],
196  'watchlist' => [
197  ApiBase::PARAM_DFLT => 'preferences',
199  'watch',
200  'unwatch',
201  'preferences',
202  'nochange'
203  ],
204  ],
205  'unwatch' => [
206  ApiBase::PARAM_DFLT => false,
208  ],
209  'oldimage' => null,
210  ];
211  }
212 
213  public function needsToken() {
214  return 'csrf';
215  }
216 
217  protected function getExamplesMessages() {
218  return [
219  'action=delete&title=Main%20Page&token=123ABC'
220  => 'apihelp-delete-example-simple',
221  'action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
222  => 'apihelp-delete-example-reason',
223  ];
224  }
225 
226  public function getHelpUrls() {
227  return 'https://www.mediawiki.org/wiki/API:Delete';
228  }
229 }
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below...
Definition: ApiBase.php:88
getResult()
Get the result object.
Definition: ApiBase.php:577
getExamplesMessages()
Definition: ApiDelete.php:217
getAllowedParams()
Definition: ApiDelete.php:181
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:2752
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:50
setWatch($watch, $titleObj, $userOption=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
Definition: ApiBase.php:1423
extractRequestParams($parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user...
Definition: ApiBase.php:678
getTitleOrPageId($params, $load=false)
Get a WikiPage object from a title or pageid param, if possible.
Definition: ApiBase.php:833
Interface for type hinting (accepts WikiPage, Article, ImagePage, CategoryPage)
Definition: Page.php:24
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:59
$params
static doDelete(&$title, &$file, &$oldimage, $reason, $suppress, User $user=null, $tags=[])
Really delete the file.
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:953
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:457
API module that facilitates deleting pages.
Definition: ApiDelete.php:33
const NS_FILE
Definition: Defines.php:62
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 local account $user
Definition: hooks.txt:242
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
static deleteFile(Page $page, User $user, $oldimage, &$reason=null, $suppress=false, $tags=[])
Definition: ApiDelete.php:146
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:53
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist 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:1046
This abstract class implements many basic API functions, and is the base of all API classes...
Definition: ApiBase.php:39
const PARAM_DEPRECATED
(boolean) Is the parameter deprecated (will show a warning)?
Definition: ApiBase.php:106
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:392
static isValidOldSpec($oldimage)
Is the provided oldimage value valid?
dieStatus($status)
Throw a UsageException based on the errors in the Status object.
Definition: ApiBase.php:1663
getUser()
Get the User object.
mustBePosted()
Definition: ApiDelete.php:173
dieUsageMsg($error)
Output the error message related to a certain array.
Definition: ApiBase.php:2192
execute()
Extracts the title and reason from the request parameters and invokes the local delete() function wit...
Definition: ApiDelete.php:41
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:2491