bfd841f57294b0c3d2601026090b7598548c222d
[lhc/web/wiklou.git] / includes / api / ApiDelete.php
1 <?php
2 /**
3 *
4 *
5 * Created on Jun 30, 2007
6 *
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * API module that facilitates deleting pages. The API equivalent of action=delete.
29 * Requires API write mode to be enabled.
30 *
31 * @ingroup API
32 */
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.
40 */
41 public function execute() {
42 $this->useTransactionalTimeLimit();
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'] ) ) {
64 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
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 $this->logFeatureUsage( 'action=delete&watch' );
92 $watch = 'watch';
93 } elseif ( $params['unwatch'] ) {
94 $this->logFeatureUsage( 'action=delete&unwatch' );
95 $watch = 'unwatch';
96 } else {
97 $watch = $params['watchlist'];
98 }
99 $this->setWatch( $watch, $titleObj, 'watchdeletion' );
100
101 // Apply change tags to the log entry, if requested
102 if ( count( $params['tags'] ) ) {
103 ChangeTags::addTags( $params['tags'], null, null, $status->value, null );
104 }
105
106 $r = array(
107 'title' => $titleObj->getPrefixedText(),
108 'reason' => $reason,
109 'logid' => $status->value
110 );
111 $this->getResult()->addValue( null, $this->getModuleName(), $r );
112 }
113
114 /**
115 * We have our own delete() function, since Article.php's implementation is split in two phases
116 *
117 * @param Page|WikiPage $page Page or WikiPage object to work on
118 * @param User $user User doing the action
119 * @param string|null $reason Reason for the deletion. Autogenerated if null
120 * @return Status|array
121 */
122 protected static function delete( Page $page, User $user, &$reason = null ) {
123 $title = $page->getTitle();
124
125 // Auto-generate a summary, if necessary
126 if ( is_null( $reason ) ) {
127 // Need to pass a throwaway variable because generateReason expects
128 // a reference
129 $hasHistory = false;
130 $reason = $page->getAutoDeleteReason( $hasHistory );
131 if ( $reason === false ) {
132 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
133 }
134 }
135
136 $error = '';
137
138 // Luckily, Article.php provides a reusable delete function that does the hard work for us
139 return $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user );
140 }
141
142 /**
143 * @param Page $page Object to work on
144 * @param User $user User doing the action
145 * @param string $oldimage Archive name
146 * @param string $reason Reason for the deletion. Autogenerated if null.
147 * @param bool $suppress Whether to mark all deleted versions as restricted
148 * @return Status|array
149 */
150 protected static function deleteFile( Page $page, User $user, $oldimage,
151 &$reason = null, $suppress = false
152 ) {
153 $title = $page->getTitle();
154
155 $file = $page->getFile();
156 if ( !$file->exists() || !$file->isLocal() || $file->getRedirected() ) {
157 return self::delete( $page, $user, $reason );
158 }
159
160 if ( $oldimage ) {
161 if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
162 return array( array( 'invalidoldimage' ) );
163 }
164 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
165 if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
166 return array( array( 'nodeleteablefile' ) );
167 }
168 }
169
170 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
171 $reason = '';
172 }
173
174 return FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress, $user );
175 }
176
177 public function mustBePosted() {
178 return true;
179 }
180
181 public function isWriteMode() {
182 return true;
183 }
184
185 public function getAllowedParams() {
186 return array(
187 'title' => null,
188 'pageid' => array(
189 ApiBase::PARAM_TYPE => 'integer'
190 ),
191 'reason' => null,
192 'tags' => array(
193 ApiBase::PARAM_TYPE => ChangeTags::listExplicitlyDefinedTags(),
194 ApiBase::PARAM_ISMULTI => true,
195 ),
196 'watch' => array(
197 ApiBase::PARAM_DFLT => false,
198 ApiBase::PARAM_DEPRECATED => true,
199 ),
200 'watchlist' => array(
201 ApiBase::PARAM_DFLT => 'preferences',
202 ApiBase::PARAM_TYPE => array(
203 'watch',
204 'unwatch',
205 'preferences',
206 'nochange'
207 ),
208 ),
209 'unwatch' => array(
210 ApiBase::PARAM_DFLT => false,
211 ApiBase::PARAM_DEPRECATED => true,
212 ),
213 'oldimage' => null,
214 );
215 }
216
217 public function needsToken() {
218 return 'csrf';
219 }
220
221 protected function getExamplesMessages() {
222 return array(
223 'action=delete&title=Main%20Page&token=123ABC'
224 => 'apihelp-delete-example-simple',
225 'action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
226 => 'apihelp-delete-example-reason',
227 );
228 }
229
230 public function getHelpUrls() {
231 return 'https://www.mediawiki.org/wiki/API:Delete';
232 }
233 }