Remove unused variable in ProfilerMwprof
[mediawiki.git] / includes / api / ApiDelete.php
blobabca82454559a84efa1efa0673eccf065d11e440
1 <?php
2 /**
5 * Created on Jun 30, 2007
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
24 * @file
27 /**
28 * API module that facilitates deleting pages. The API equivalent of action=delete.
29 * Requires API write mode to be enabled.
31 * @ingroup API
33 class ApiDelete extends ApiBase {
34 /**
35 * Extracts the title, token, and reason from the request parameters and invokes
36 * the local delete() function with these as arguments. It does not make use of
37 * the delete function specified by Article.php. If the deletion succeeds, the
38 * details of the article deleted and the reason for deletion are added to the
39 * result object.
41 public function execute() {
42 $params = $this->extractRequestParams();
44 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
45 if ( !$pageObj->exists() ) {
46 $this->dieUsageMsg( 'notanarticle' );
49 $titleObj = $pageObj->getTitle();
50 $reason = $params['reason'];
51 $user = $this->getUser();
53 if ( $titleObj->getNamespace() == NS_FILE ) {
54 $status = self::deleteFile(
55 $pageObj,
56 $user,
57 $params['token'],
58 $params['oldimage'],
59 $reason,
60 false
62 } else {
63 $status = self::delete( $pageObj, $user, $params['token'], $reason );
66 if ( is_array( $status ) ) {
67 $this->dieUsageMsg( $status[0] );
69 if ( !$status->isGood() ) {
70 $this->dieStatus( $status );
73 // Deprecated parameters
74 if ( $params['watch'] ) {
75 $this->logFeatureUsage( 'action=delete&watch' );
76 $watch = 'watch';
77 } elseif ( $params['unwatch'] ) {
78 $this->logFeatureUsage( 'action=delete&unwatch' );
79 $watch = 'unwatch';
80 } else {
81 $watch = $params['watchlist'];
83 $this->setWatch( $watch, $titleObj, 'watchdeletion' );
85 $r = array(
86 'title' => $titleObj->getPrefixedText(),
87 'reason' => $reason,
88 'logid' => $status->value
90 $this->getResult()->addValue( null, $this->getModuleName(), $r );
93 /**
94 * @param Title $title
95 * @param User $user User doing the action
96 * @param string $token
97 * @return array
99 private static function getPermissionsError( $title, $user, $token ) {
100 // Check permissions
101 return $title->getUserPermissionsErrors( 'delete', $user );
105 * We have our own delete() function, since Article.php's implementation is split in two phases
107 * @param Page|WikiPage $page Page or WikiPage object to work on
108 * @param User $user User doing the action
109 * @param string $token Delete token (same as edit token)
110 * @param string|null $reason Reason for the deletion. Autogenerated if null
111 * @return Status|array
113 public static function delete( Page $page, User $user, $token, &$reason = null ) {
114 $title = $page->getTitle();
115 $errors = self::getPermissionsError( $title, $user, $token );
116 if ( count( $errors ) ) {
117 return $errors;
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 array( array( 'cannotdelete', $title->getPrefixedText() ) );
131 $error = '';
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 );
138 * @param Page $page Object to work on
139 * @param User $user User doing the action
140 * @param string $token Delete token (same as edit token)
141 * @param string $oldimage Archive name
142 * @param string $reason Reason for the deletion. Autogenerated if null.
143 * @param bool $suppress Whether to mark all deleted versions as restricted
144 * @return Status|array
146 public static function deleteFile( Page $page, User $user, $token, $oldimage,
147 &$reason = null, $suppress = false
149 $title = $page->getTitle();
150 $errors = self::getPermissionsError( $title, $user, $token );
151 if ( count( $errors ) ) {
152 return $errors;
155 $file = $page->getFile();
156 if ( !$file->exists() || !$file->isLocal() || $file->getRedirected() ) {
157 return self::delete( $page, $user, $token, $reason );
160 if ( $oldimage ) {
161 if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
162 return array( array( 'invalidoldimage' ) );
164 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
165 if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
166 return array( array( 'nodeleteablefile' ) );
170 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
171 $reason = '';
174 return FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress, $user );
177 public function mustBePosted() {
178 return true;
181 public function isWriteMode() {
182 return true;
185 public function getAllowedParams() {
186 return array(
187 'title' => null,
188 'pageid' => array(
189 ApiBase::PARAM_TYPE => 'integer'
191 'reason' => null,
192 'watch' => array(
193 ApiBase::PARAM_DFLT => false,
194 ApiBase::PARAM_DEPRECATED => true,
196 'watchlist' => array(
197 ApiBase::PARAM_DFLT => 'preferences',
198 ApiBase::PARAM_TYPE => array(
199 'watch',
200 'unwatch',
201 'preferences',
202 'nochange'
205 'unwatch' => array(
206 ApiBase::PARAM_DFLT => false,
207 ApiBase::PARAM_DEPRECATED => true,
209 'oldimage' => null,
213 public function getParamDescription() {
214 $p = $this->getModulePrefix();
216 return array(
217 'title' => "Title of the page you want to delete. Cannot be used together with {$p}pageid",
218 'pageid' => "Page ID of the page you want to delete. Cannot be used together with {$p}title",
219 'reason'
220 => 'Reason for the deletion. If not set, an automatically generated reason will be used',
221 'watch' => 'Add the page to your watchlist',
222 'watchlist' => 'Unconditionally add or remove the page from your ' .
223 'watchlist, use preferences or do not change watch',
224 'unwatch' => 'Remove the page from your watchlist',
225 'oldimage' => 'The name of the old image to delete as provided by iiprop=archivename'
229 public function getDescription() {
230 return 'Delete a page.';
233 public function needsToken() {
234 return 'csrf';
237 public function getExamples() {
238 return array(
239 'api.php?action=delete&title=Main%20Page&token=123ABC' => 'Delete the Main Page',
240 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
241 => 'Delete the Main Page with the reason "Preparing for move"',
245 public function getHelpUrls() {
246 return 'https://www.mediawiki.org/wiki/API:Delete';