Move remaining LoadBalancer classes to Rdbms
[mediawiki.git] / includes / api / ApiDelete.php
blob50c24aeca8b5954af8a04ce34ef0f13cb98414cf
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 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 $this->useTransactionalTimeLimit();
44 $params = $this->extractRequestParams();
46 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
47 if ( !$pageObj->exists() ) {
48 $this->dieWithError( 'apierror-missingtitle' );
51 $titleObj = $pageObj->getTitle();
52 $reason = $params['reason'];
53 $user = $this->getUser();
55 // Check that the user is allowed to carry out the deletion
56 $this->checkTitleUserPermissions( $titleObj, 'delete' );
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'] ) ) {
61 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
62 if ( !$tagStatus->isOK() ) {
63 $this->dieStatus( $tagStatus );
67 if ( $titleObj->getNamespace() == NS_FILE ) {
68 $status = self::deleteFile(
69 $pageObj,
70 $user,
71 $params['oldimage'],
72 $reason,
73 false,
74 $params['tags']
76 } else {
77 $status = self::delete( $pageObj, $user, $reason, $params['tags'] );
80 if ( !$status->isGood() ) {
81 $this->dieStatus( $status );
84 // Deprecated parameters
85 if ( $params['watch'] ) {
86 $watch = 'watch';
87 } elseif ( $params['unwatch'] ) {
88 $watch = 'unwatch';
89 } else {
90 $watch = $params['watchlist'];
92 $this->setWatch( $watch, $titleObj, 'watchdeletion' );
94 $r = [
95 'title' => $titleObj->getPrefixedText(),
96 'reason' => $reason,
97 'logid' => $status->value
99 $this->getResult()->addValue( null, $this->getModuleName(), $r );
103 * We have our own delete() function, since Article.php's implementation is split in two phases
105 * @param Page|WikiPage $page Page or WikiPage object to work on
106 * @param User $user User doing the action
107 * @param string|null $reason Reason for the deletion. Autogenerated if null
108 * @param array $tags Tags to tag the deletion with
109 * @return Status
111 protected static function delete( Page $page, User $user, &$reason = null, $tags = [] ) {
112 $title = $page->getTitle();
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() );
125 $error = '';
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 );
132 * @param Page $page Object to work on
133 * @param User $user User doing the action
134 * @param string $oldimage Archive name
135 * @param string $reason Reason for the deletion. Autogenerated if null.
136 * @param bool $suppress Whether to mark all deleted versions as restricted
137 * @param array $tags Tags to tag the deletion with
138 * @return Status
140 protected static function deleteFile( Page $page, User $user, $oldimage,
141 &$reason = null, $suppress = false, $tags = []
143 $title = $page->getTitle();
145 $file = $page->getFile();
146 if ( !$file->exists() || !$file->isLocal() || $file->getRedirected() ) {
147 return self::delete( $page, $user, $reason, $tags );
150 if ( $oldimage ) {
151 if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
152 return Status::newFatal( 'invalidoldimage' );
154 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
155 if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
156 return Status::newFatal( 'nodeleteablefile' );
160 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
161 $reason = '';
164 return FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress, $user, $tags );
167 public function mustBePosted() {
168 return true;
171 public function isWriteMode() {
172 return true;
175 public function getAllowedParams() {
176 return [
177 'title' => null,
178 'pageid' => [
179 ApiBase::PARAM_TYPE => 'integer'
181 'reason' => null,
182 'tags' => [
183 ApiBase::PARAM_TYPE => 'tags',
184 ApiBase::PARAM_ISMULTI => true,
186 'watch' => [
187 ApiBase::PARAM_DFLT => false,
188 ApiBase::PARAM_DEPRECATED => true,
190 'watchlist' => [
191 ApiBase::PARAM_DFLT => 'preferences',
192 ApiBase::PARAM_TYPE => [
193 'watch',
194 'unwatch',
195 'preferences',
196 'nochange'
199 'unwatch' => [
200 ApiBase::PARAM_DFLT => false,
201 ApiBase::PARAM_DEPRECATED => true,
203 'oldimage' => null,
207 public function needsToken() {
208 return 'csrf';
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',
220 public function getHelpUrls() {
221 return 'https://www.mediawiki.org/wiki/API:Delete';