MediaWiki  1.27.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->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  );
78  } else {
79  $status = self::delete( $pageObj, $user, $reason );
80  }
81 
82  if ( is_array( $status ) ) {
83  $this->dieUsageMsg( $status[0] );
84  }
85  if ( !$status->isGood() ) {
86  $this->dieStatus( $status );
87  }
88 
89  // Deprecated parameters
90  if ( $params['watch'] ) {
91  $watch = 'watch';
92  } elseif ( $params['unwatch'] ) {
93  $watch = 'unwatch';
94  } else {
95  $watch = $params['watchlist'];
96  }
97  $this->setWatch( $watch, $titleObj, 'watchdeletion' );
98 
99  // Apply change tags to the log entry, if requested
100  if ( count( $params['tags'] ) ) {
101  ChangeTags::addTags( $params['tags'], null, null, $status->value, null );
102  }
103 
104  $r = [
105  'title' => $titleObj->getPrefixedText(),
106  'reason' => $reason,
107  'logid' => $status->value
108  ];
109  $this->getResult()->addValue( null, $this->getModuleName(), $r );
110  }
111 
120  protected static function delete( Page $page, User $user, &$reason = null ) {
121  $title = $page->getTitle();
122 
123  // Auto-generate a summary, if necessary
124  if ( is_null( $reason ) ) {
125  // Need to pass a throwaway variable because generateReason expects
126  // a reference
127  $hasHistory = false;
128  $reason = $page->getAutoDeleteReason( $hasHistory );
129  if ( $reason === false ) {
130  return [ [ 'cannotdelete', $title->getPrefixedText() ] ];
131  }
132  }
133 
134  $error = '';
135 
136  // Luckily, Article.php provides a reusable delete function that does the hard work for us
137  return $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user );
138  }
139 
148  protected static function deleteFile( Page $page, User $user, $oldimage,
149  &$reason = null, $suppress = false
150  ) {
151  $title = $page->getTitle();
152 
153  $file = $page->getFile();
154  if ( !$file->exists() || !$file->isLocal() || $file->getRedirected() ) {
155  return self::delete( $page, $user, $reason );
156  }
157 
158  if ( $oldimage ) {
159  if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
160  return [ [ 'invalidoldimage' ] ];
161  }
162  $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
163  if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
164  return [ [ 'nodeleteablefile' ] ];
165  }
166  }
167 
168  if ( is_null( $reason ) ) { // Log and RC don't like null reasons
169  $reason = '';
170  }
171 
172  return FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress, $user );
173  }
174 
175  public function mustBePosted() {
176  return true;
177  }
178 
179  public function isWriteMode() {
180  return true;
181  }
182 
183  public function getAllowedParams() {
184  return [
185  'title' => null,
186  'pageid' => [
187  ApiBase::PARAM_TYPE => 'integer'
188  ],
189  'reason' => null,
190  'tags' => [
191  ApiBase::PARAM_TYPE => 'tags',
192  ApiBase::PARAM_ISMULTI => true,
193  ],
194  'watch' => [
195  ApiBase::PARAM_DFLT => false,
197  ],
198  'watchlist' => [
199  ApiBase::PARAM_DFLT => 'preferences',
201  'watch',
202  'unwatch',
203  'preferences',
204  'nochange'
205  ],
206  ],
207  'unwatch' => [
208  ApiBase::PARAM_DFLT => false,
210  ],
211  'oldimage' => null,
212  ];
213  }
214 
215  public function needsToken() {
216  return 'csrf';
217  }
218 
219  protected function getExamplesMessages() {
220  return [
221  'action=delete&title=Main%20Page&token=123ABC'
222  => 'apihelp-delete-example-simple',
223  'action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
224  => 'apihelp-delete-example-reason',
225  ];
226  }
227 
228  public function getHelpUrls() {
229  return 'https://www.mediawiki.org/wiki/API:Delete';
230  }
231 }
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:584
getExamplesMessages()
Definition: ApiDelete.php:219
getAllowedParams()
Definition: ApiDelete.php:183
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:2976
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:1375
extractRequestParams($parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user...
Definition: ApiBase.php:685
getTitleOrPageId($params, $load=false)
Get a WikiPage object from a title or pageid param, if possible.
Definition: ApiBase.php:840
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 deleteFile(Page $page, User $user, $oldimage, &$reason=null, $suppress=false)
Definition: ApiDelete.php:148
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:912
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:464
API module that facilitates deleting pages.
Definition: ApiDelete.php:33
const NS_FILE
Definition: Defines.php:75
static doDelete(&$title, &$file, &$oldimage, $reason, $suppress, User $user=null)
Really delete the file.
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
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:1004
This abstract class implements many basic API functions, and is the base of all API classes...
Definition: ApiBase.php:39
static addTags($tags, $rc_id=null, $rev_id=null, $log_id=null, $params=null)
Add tags to a change given its rc_id, rev_id and/or log_id.
Definition: ChangeTags.php:126
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:378
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:1615
getUser()
Get the User object.
mustBePosted()
Definition: ApiDelete.php:175
dieUsageMsg($error)
Output the error message related to a certain array.
Definition: ApiBase.php:2144
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:2338