Code style tweak.
[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 if (!wfRunHooks('ArticleDelete', array(&$article, &$wgUser, &$reason)))
142 $this->dieUsageMsg(array('hookaborted'));
143
144 // Luckily, Article.php provides a reusable delete function that does the hard work for us
145 if($article->doDeleteArticle($reason)) {
146 wfRunHooks('ArticleDeleteComplete', array(&$article, &$wgUser, $reason, $article->getId()));
147 return array();
148 }
149 return array(array('cannotdelete', $article->mTitle->getPrefixedText()));
150 }
151
152 public static function deleteFile($token, &$title, $oldimage, &$reason = NULL, $suppress = false)
153 {
154 $errors = self::getPermissionsError($title, $token);
155 if (count($errors)) return $errors;
156
157 if( $oldimage && !FileDeleteForm::isValidOldSpec($oldimage) )
158 return array(array('invalidoldimage'));
159
160 $file = wfFindFile($title, false, FileRepo::FIND_IGNORE_REDIRECT);
161 $oldfile = false;
162
163 if( $oldimage )
164 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
165
166 if( !FileDeleteForm::haveDeletableFile($file, $oldfile, $oldimage) )
167 return array(array('nofile'));
168 if (is_null($reason)) # Log and RC don't like null reasons
169 $reason = '';
170 $status = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress );
171
172 if( !$status->isGood() )
173 return array(array('cannotdelete', $title->getPrefixedText()));
174
175 return array();
176 }
177
178 public function mustBePosted() { return true; }
179
180 public function getAllowedParams() {
181 return array (
182 'title' => null,
183 'pageid' => array(
184 ApiBase::PARAM_TYPE => 'integer'
185 ),
186 'token' => null,
187 'reason' => null,
188 'watch' => false,
189 'unwatch' => false,
190 'oldimage' => null
191 );
192 }
193
194 public function getParamDescription() {
195 return array (
196 'title' => 'Title of the page you want to delete. Cannot be used together with pageid',
197 'pageid' => 'Page ID of the page you want to delete. Cannot be used together with title',
198 'token' => 'A delete token previously retrieved through prop=info',
199 'reason' => 'Reason for the deletion. If not set, an automatically generated reason will be used.',
200 'watch' => 'Add the page to your watchlist',
201 'unwatch' => 'Remove the page from your watchlist',
202 'oldimage' => 'The name of the old image to delete as provided by iiprop=archivename'
203 );
204 }
205
206 public function getDescription() {
207 return array(
208 'Delete a page.'
209 );
210 }
211
212 protected function getExamples() {
213 return array (
214 'api.php?action=delete&title=Main%20Page&token=123ABC',
215 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
216 );
217 }
218
219 public function getVersion() {
220 return __CLASS__ . ': $Id$';
221 }
222 }