MediaWiki  1.23.13
ApiDelete.php
Go to the documentation of this file.
1 <?php
33 class ApiDelete extends ApiBase {
41  public function execute() {
42  $params = $this->extractRequestParams();
43 
44  $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
45  if ( !$pageObj->exists() ) {
46  $this->dieUsageMsg( 'notanarticle' );
47  }
48 
49  $titleObj = $pageObj->getTitle();
50  $reason = $params['reason'];
51  $user = $this->getUser();
52 
53  if ( $titleObj->getNamespace() == NS_FILE ) {
54  $status = self::deleteFile(
55  $pageObj,
56  $user,
57  $params['token'],
58  $params['oldimage'],
59  $reason,
60  false
61  );
62  } else {
63  $status = self::delete( $pageObj, $user, $params['token'], $reason );
64  }
65 
66  if ( is_array( $status ) ) {
67  $this->dieUsageMsg( $status[0] );
68  }
69  if ( !$status->isGood() ) {
70  $this->dieStatus( $status );
71  }
72 
73  // Deprecated parameters
74  if ( $params['watch'] ) {
75  $watch = 'watch';
76  } elseif ( $params['unwatch'] ) {
77  $watch = 'unwatch';
78  } else {
79  $watch = $params['watchlist'];
80  }
81  $this->setWatch( $watch, $titleObj, 'watchdeletion' );
82 
83  $r = array(
84  'title' => $titleObj->getPrefixedText(),
85  'reason' => $reason,
86  'logid' => $status->value
87  );
88  $this->getResult()->addValue( null, $this->getModuleName(), $r );
89  }
90 
97  private static function getPermissionsError( $title, $user, $token ) {
98  // Check permissions
99  return $title->getUserPermissionsErrors( 'delete', $user );
100  }
101 
111  public static function delete( Page $page, User $user, $token, &$reason = null ) {
112  $title = $page->getTitle();
113  $errors = self::getPermissionsError( $title, $user, $token );
114  if ( count( $errors ) ) {
115  return $errors;
116  }
117 
118  // Auto-generate a summary, if necessary
119  if ( is_null( $reason ) ) {
120  // Need to pass a throwaway variable because generateReason expects
121  // a reference
122  $hasHistory = false;
123  $reason = $page->getAutoDeleteReason( $hasHistory );
124  if ( $reason === false ) {
125  return array( array( 'cannotdelete', $title->getPrefixedText() ) );
126  }
127  }
128 
129  $error = '';
130 
131  // Luckily, Article.php provides a reusable delete function that does the hard work for us
132  return $page->doDeleteArticleReal( $reason, false, 0, true, $error );
133  }
134 
144  public static function deleteFile( Page $page, User $user, $token, $oldimage,
145  &$reason = null, $suppress = false
146  ) {
147  $title = $page->getTitle();
148  $errors = self::getPermissionsError( $title, $user, $token );
149  if ( count( $errors ) ) {
150  return $errors;
151  }
152 
153  $file = $page->getFile();
154  if ( !$file->exists() || !$file->isLocal() || $file->getRedirected() ) {
155  return self::delete( $page, $user, $token, $reason );
156  }
157 
158  if ( $oldimage ) {
159  if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
160  return array( array( 'invalidoldimage' ) );
161  }
162  $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
163  if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
164  return array( array( '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 array(
185  'title' => null,
186  'pageid' => array(
187  ApiBase::PARAM_TYPE => 'integer'
188  ),
189  'token' => array(
190  ApiBase::PARAM_TYPE => 'string',
192  ),
193  'reason' => null,
194  'watch' => array(
195  ApiBase::PARAM_DFLT => false,
197  ),
198  'watchlist' => array(
199  ApiBase::PARAM_DFLT => 'preferences',
201  'watch',
202  'unwatch',
203  'preferences',
204  'nochange'
205  ),
206  ),
207  'unwatch' => array(
208  ApiBase::PARAM_DFLT => false,
210  ),
211  'oldimage' => null,
212  );
213  }
214 
215  public function getParamDescription() {
216  $p = $this->getModulePrefix();
217 
218  return array(
219  'title' => "Title of the page you want to delete. Cannot be used together with {$p}pageid",
220  'pageid' => "Page ID of the page you want to delete. Cannot be used together with {$p}title",
221  'token' => 'A delete token previously retrieved through prop=info',
222  'reason'
223  => 'Reason for the deletion. If not set, an automatically generated reason will be used',
224  'watch' => 'Add the page to your watchlist',
225  'watchlist' => 'Unconditionally add or remove the page from your ' .
226  'watchlist, use preferences or do not change watch',
227  'unwatch' => 'Remove the page from your watchlist',
228  'oldimage' => 'The name of the old image to delete as provided by iiprop=archivename'
229  );
230  }
231 
232  public function getResultProperties() {
233  return array(
234  '' => array(
235  'title' => 'string',
236  'reason' => 'string',
237  'logid' => 'integer'
238  )
239  );
240  }
241 
242  public function getDescription() {
243  return 'Delete a page.';
244  }
245 
246  public function getPossibleErrors() {
247  return array_merge( parent::getPossibleErrors(),
249  array(
250  array( 'notanarticle' ),
251  array( 'hookaborted', 'error' ),
252  array( 'delete-toobig', 'limit' ),
253  array( 'cannotdelete', 'title' ),
254  array( 'invalidoldimage' ),
255  array( 'nodeleteablefile' ),
256  )
257  );
258  }
259 
260  public function needsToken() {
261  return true;
262  }
263 
264  public function getTokenSalt() {
265  return '';
266  }
267 
268  public function getExamples() {
269  return array(
270  'api.php?action=delete&title=Main%20Page&token=123ABC' => 'Delete the Main Page',
271  'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
272  => 'Delete the Main Page with the reason "Preparing for move"',
273  );
274  }
275 
276  public function getHelpUrls() {
277  return 'https://www.mediawiki.org/wiki/API:Delete';
278  }
279 }
ApiBase\dieStatus
dieStatus( $status)
Throw a UsageException based on the errors in the Status object.
Definition: ApiBase.php:1419
Page
Abstract class for type hinting (accepts WikiPage, Article, ImagePage, CategoryPage)
Definition: WikiPage.php:26
RepoGroup\singleton
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:53
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ApiDelete\getParamDescription
getParamDescription()
Returns an array of parameter descriptions.
Definition: ApiDelete.php:215
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
Definition: ApiBase.php:62
ApiBase\dieUsageMsg
dieUsageMsg( $error)
Output the error message related to a certain array.
Definition: ApiBase.php:1933
ApiDelete\getTokenSalt
getTokenSalt()
Returns the token salt if there is one, '' if the module doesn't require a salt, else false if the mo...
Definition: ApiDelete.php:264
ApiBase\getTitleOrPageId
getTitleOrPageId( $params, $load=false)
Definition: ApiBase.php:853
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:50
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:205
NS_FILE
const NS_FILE
Definition: Defines.php:85
$params
$params
Definition: styleTest.css.php:40
ApiDelete\getDescription
getDescription()
Returns the description string for this module.
Definition: ApiDelete.php:242
ContextSource\getUser
getUser()
Get the User object.
Definition: ContextSource.php:132
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:42
ApiDelete\getExamples
getExamples()
Returns usage examples for this module.
Definition: ApiDelete.php:268
ApiBase\PARAM_DEPRECATED
const PARAM_DEPRECATED
Definition: ApiBase.php:60
ApiDelete\needsToken
needsToken()
Returns whether this module requires a token to execute It is used to show possible errors in action=...
Definition: ApiDelete.php:260
ApiDelete\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiDelete.php:183
ApiDelete\getResultProperties
getResultProperties()
Returns possible properties in the result, grouped by the value of the prop parameter that shows them...
Definition: ApiDelete.php:232
FileDeleteForm\doDelete
static doDelete(&$title, &$file, &$oldimage, $reason, $suppress, User $user=null)
Really delete the file.
Definition: FileDeleteForm.php:141
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:175
ApiDelete\execute
execute()
Extracts the title, token, and reason from the request parameters and invokes the local delete() func...
Definition: ApiDelete.php:41
ApiDelete\delete
static delete(Page $page, User $user, $token, &$reason=null)
We have our own delete() function, since Article.php's implementation is split in two phases.
Definition: ApiDelete.php:111
ApiDelete\getPossibleErrors
getPossibleErrors()
Returns a list of all possible errors returned by the module.
Definition: ApiDelete.php:246
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
ApiBase\getModulePrefix
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition: ApiBase.php:165
ApiBase\setWatch
setWatch( $watch, $titleObj, $userOption=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
Definition: ApiBase.php:952
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:687
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
ApiDelete\getHelpUrls
getHelpUrls()
Definition: ApiDelete.php:276
$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:237
$file
if(PHP_SAPI !='cli') $file
Definition: UtfNormalTest2.php:30
ApiDelete\deleteFile
static deleteFile(Page $page, User $user, $token, $oldimage, &$reason=null, $suppress=false)
Definition: ApiDelete.php:144
ApiBase\PARAM_DFLT
const PARAM_DFLT
Definition: ApiBase.php:46
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:148
ApiDelete\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiDelete.php:179
$error
usually copyright or history_copyright This message must be in HTML not wikitext $subpages will be ignored and the rest of subPageSubtitle() will run. 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink' whether MediaWiki currently thinks this is a CSS JS page Hooks may change this value to override the return value of Title::isCssOrJsPage(). 'TitleIsAlwaysKnown' whether MediaWiki currently thinks this page is known isMovable() always returns false. $title whether MediaWiki currently thinks this page is movable Hooks may change this value to override the return value of Title::isMovable(). 'TitleIsWikitextPage' whether MediaWiki currently thinks this is a wikitext page Hooks may change this value to override the return value of Title::isWikitextPage() 'TitleMove' use UploadVerification and UploadVerifyFile instead where the first element is the message key and the remaining elements are used as parameters to the message based on mime etc Preferred in most cases over UploadVerification object with all info about the upload string as detected by MediaWiki Handlers will typically only apply for specific mime types object & $error
Definition: hooks.txt:2578
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:59
ApiDelete\getPermissionsError
static getPermissionsError( $title, $user, $token)
Definition: ApiDelete.php:97
FileDeleteForm\isValidOldSpec
static isValidOldSpec( $oldimage)
Is the provided oldimage value valid?
Definition: FileDeleteForm.php:338
ApiBase\getTitleOrPageIdErrorMessage
getTitleOrPageIdErrorMessage()
Definition: ApiBase.php:885