Kill code duplication & other style tweaks
[lhc/web/wiklou.git] / includes / api / ApiDelete.php
1 <?php
2
3 /*
4 * Created on Jun 30, 2007
5 * API for MediaWiki 1.8+
6 *
7 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 */
24
25 if (!defined('MEDIAWIKI')) {
26 // Eclipse helper - will be ignored in production
27 require_once ("ApiBase.php");
28 }
29
30
31 /**
32 * API module that facilitates deleting pages. The API eqivalent of action=delete.
33 * Requires API write mode to be enabled.
34 *
35 * @ingroup API
36 */
37 class ApiDelete extends ApiBase {
38
39 public function __construct($main, $action) {
40 parent :: __construct($main, $action);
41 }
42
43 /**
44 * Extracts the title, token, and reason from the request parameters and invokes
45 * the local delete() function with these as arguments. It does not make use of
46 * the delete function specified by Article.php. If the deletion succeeds, the
47 * details of the article deleted and the reason for deletion are added to the
48 * result object.
49 */
50 public function execute() {
51 global $wgUser;
52 $this->getMain()->requestWriteMode();
53 $params = $this->extractRequestParams();
54
55 $this->requireOnlyOneParameter($params, 'title', 'pageid');
56 if(!isset($params['token']))
57 $this->dieUsageMsg(array('missingparam', 'token'));
58
59 if(isset($params['title']))
60 {
61 $titleObj = Title::newFromText($params['title']);
62 if(!$titleObj)
63 $this->dieUsageMsg(array('invalidtitle', $params['title']));
64 }
65 else if(isset($params['pageid']))
66 {
67 $titleObj = Title::newFromID($params['pageid']);
68 if(!$titleObj)
69 $this->dieUsageMsg(array('nosuchpageid', $params['pageid']));
70 }
71 if(!$titleObj->exists())
72 $this->dieUsageMsg(array('notanarticle'));
73
74 $reason = (isset($params['reason']) ? $params['reason'] : NULL);
75 if ($titleObj->getNamespace() == NS_FILE) {
76 $retval = self::deleteFile($params['token'], $titleObj, $params['oldimage'], $reason, false);
77 if(count($retval))
78 // We don't care about multiple errors, just report one of them
79 $this->dieUsageMsg(current($retval));
80 } else {
81 $articleObj = new Article($titleObj);
82 if($articleObj->isBigDeletion() && !$wgUser->isAllowed('bigdelete')) {
83 global $wgDeleteRevisionsLimit;
84 $this->dieUsageMsg(array('delete-toobig', $wgDeleteRevisionsLimit));
85 }
86 $retval = self::delete($articleObj, $params['token'], $reason);
87
88 if(count($retval))
89 // We don't care about multiple errors, just report one of them
90 $this->dieUsageMsg(current($retval));
91
92 if($params['watch'] || $wgUser->getOption('watchdeletion'))
93 $articleObj->doWatch();
94 else if($params['unwatch'])
95 $articleObj->doUnwatch();
96 }
97
98 $r = array('title' => $titleObj->getPrefixedText(), 'reason' => $reason);
99 $this->getResult()->addValue(null, $this->getModuleName(), $r);
100 }
101
102 private static function getPermissionsError(&$title, $token) {
103 global $wgUser;
104
105 // Check permissions
106 $errors = $title->getUserPermissionsErrors('delete', $wgUser);
107 if (count($errors) > 0) return $errors;
108
109 // Check token
110 if(!$wgUser->matchEditToken($token))
111 return array(array('sessionfailure'));
112 return array();
113 }
114
115 /**
116 * We have our own delete() function, since Article.php's implementation is split in two phases
117 *
118 * @param Article $article - Article object to work on
119 * @param string $token - Delete token (same as edit token)
120 * @param string $reason - Reason for the deletion. Autogenerated if NULL
121 * @return Title::getUserPermissionsErrors()-like array
122 */
123 public static function delete(&$article, $token, &$reason = NULL)
124 {
125 global $wgUser;
126 $title = $article->getTitle();
127 $errors = self::getPermissionsError($title, $token);
128 if (count($errors)) return $errors;
129
130 // Auto-generate a summary, if necessary
131 if(is_null($reason))
132 {
133 # Need to pass a throwaway variable because generateReason expects
134 # a reference
135 $hasHistory = false;
136 $reason = $article->generateReason($hasHistory);
137 if($reason === false)
138 return array(array('cannotdelete'));
139 }
140
141 $error = '';
142 if (!wfRunHooks('ArticleDelete', array(&$article, &$wgUser, &$reason, $error)))
143 $this->dieUsageMsg(array('hookaborted', $error));
144
145 // Luckily, Article.php provides a reusable delete function that does the hard work for us
146 if($article->doDeleteArticle($reason)) {
147 wfRunHooks('ArticleDeleteComplete', array(&$article, &$wgUser, $reason, $article->getId()));
148 return array();
149 }
150 return array(array('cannotdelete', $article->mTitle->getPrefixedText()));
151 }
152
153 public static function deleteFile($token, &$title, $oldimage, &$reason = NULL, $suppress = false)
154 {
155 $errors = self::getPermissionsError($title, $token);
156 if (count($errors)) return $errors;
157
158 if( $oldimage && !FileDeleteForm::isValidOldSpec($oldimage) )
159 return array(array('invalidoldimage'));
160
161 $file = wfFindFile($title, false, FileRepo::FIND_IGNORE_REDIRECT);
162 $oldfile = false;
163
164 if( $oldimage )
165 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
166
167 if( !FileDeleteForm::haveDeletableFile($file, $oldfile, $oldimage) )
168 return array(array('nofile'));
169 if (is_null($reason)) # Log and RC don't like null reasons
170 $reason = '';
171 $status = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress );
172
173 if( !$status->isGood() )
174 return array(array('cannotdelete', $title->getPrefixedText()));
175
176 return array();
177 }
178
179 public function mustBePosted() { return true; }
180
181 public function getAllowedParams() {
182 return array (
183 'title' => null,
184 'pageid' => array(
185 ApiBase::PARAM_TYPE => 'integer'
186 ),
187 'token' => null,
188 'reason' => null,
189 'watch' => false,
190 'unwatch' => false,
191 'oldimage' => null
192 );
193 }
194
195 public function getParamDescription() {
196 return array (
197 'title' => 'Title of the page you want to delete. Cannot be used together with pageid',
198 'pageid' => 'Page ID of the page you want to delete. Cannot be used together with title',
199 'token' => 'A delete token previously retrieved through prop=info',
200 'reason' => 'Reason for the deletion. If not set, an automatically generated reason will be used.',
201 'watch' => 'Add the page to your watchlist',
202 'unwatch' => 'Remove the page from your watchlist',
203 'oldimage' => 'The name of the old image to delete as provided by iiprop=archivename'
204 );
205 }
206
207 public function getDescription() {
208 return array(
209 'Delete a page.'
210 );
211 }
212
213 protected function getExamples() {
214 return array (
215 'api.php?action=delete&title=Main%20Page&token=123ABC',
216 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
217 );
218 }
219
220 public function getVersion() {
221 return __CLASS__ . ': $Id$';
222 }
223 }