Tweaks for r40686, r40687:
[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 $titleObj = NULL;
56 if(!isset($params['title']))
57 $this->dieUsageMsg(array('missingparam', 'title'));
58 if(!isset($params['token']))
59 $this->dieUsageMsg(array('missingparam', 'token'));
60
61 $titleObj = Title::newFromText($params['title']);
62 if(!$titleObj)
63 $this->dieUsageMsg(array('invalidtitle', $params['title']));
64 if(!$titleObj->exists())
65 $this->dieUsageMsg(array('notanarticle'));
66
67 $reason = (isset($params['reason']) ? $params['reason'] : NULL);
68 if ($titleObj->getNamespace() == NS_IMAGE) {
69 $retval = self::deletefile($params['token'], $titleObj, $params['oldimage'], $reason, false);
70 if(!empty($retval))
71 // We don't care about multiple errors, just report one of them
72 $this->dieUsageMsg(current($retval));
73 } else {
74 $articleObj = new Article($titleObj);
75 $retval = self::delete($articleObj, $params['token'], $reason);
76
77 if(!empty($retval))
78 // We don't care about multiple errors, just report one of them
79 $this->dieUsageMsg(current($retval));
80
81 if($params['watch'] || $wgUser->getOption('watchdeletion'))
82 $articleObj->doWatch();
83 else if($params['unwatch'])
84 $articleObj->doUnwatch();
85 }
86
87 $r = array('title' => $titleObj->getPrefixedText(), 'reason' => $reason);
88 $this->getResult()->addValue(null, $this->getModuleName(), $r);
89 }
90
91 private static function getPermissionsError(&$title, $token) {
92 global $wgUser;
93
94 // Check permissions
95 $errors = $title->getUserPermissionsErrors('delete', $wgUser);
96 if (count($errors) > 0) return $errors;
97
98 // Check token
99 if(!$wgUser->matchEditToken($token))
100 return array(array('sessionfailure'));
101 return array();
102 }
103
104 /**
105 * We have our own delete() function, since Article.php's implementation is split in two phases
106 *
107 * @param Article $article - Article object to work on
108 * @param string $token - Delete token (same as edit token)
109 * @param string $reason - Reason for the deletion. Autogenerated if NULL
110 * @return Title::getUserPermissionsErrors()-like array
111 */
112 public static function delete(&$article, $token, &$reason = NULL)
113 {
114 global $wgUser;
115
116 $errors = self::getPermissionsError($article->getTitle(), $token);
117 if (count($errors)) return $errors;
118
119 // Auto-generate a summary, if necessary
120 if(is_null($reason))
121 {
122 # Need to pass a throwaway variable because generateReason expects
123 # a reference
124 $hasHistory = false;
125 $reason = $article->generateReason($hasHistory);
126 if($reason === false)
127 return array(array('cannotdelete'));
128 }
129
130 if (!wfRunHooks('ArticleDelete', array(&$article, &$wgUser, &$reason)))
131 $this->dieUsageMsg(array('hookaborted'));
132
133 // Luckily, Article.php provides a reusable delete function that does the hard work for us
134 if($article->doDeleteArticle($reason)) {
135 wfRunHooks('ArticleDeleteComplete', array(&$article, &$wgUser, $reason, $article->getId()));
136 return array();
137 }
138 return array(array('cannotdelete', $article->mTitle->getPrefixedText()));
139 }
140
141 public static function deleteFile($token, &$title, $oldimage, &$reason = NULL, $suppress = false)
142 {
143 $errors = self::getPermissionsError($title, $token);
144 if (count($errors)) return $errors;
145
146 if( $oldimage && !FileDeleteForm::isValidOldSpec($oldimage) )
147 return array(array('invalidoldimage'));
148
149 $file = wfFindFile($title, false, FileRepo::FIND_IGNORE_REDIRECT);
150 $oldfile = false;
151
152 if( $oldimage )
153 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
154
155 if( !FileDeleteForm::haveDeletableFile($file, $oldfile, $oldimage) )
156 return array(array('nofile'));
157
158 $status = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress );
159
160 if( !$status->isGood() )
161 return array(array('cannotdelete', $title->getPrefixedText()));
162
163 return array();
164 }
165
166 public function mustBePosted() { return true; }
167
168 public function getAllowedParams() {
169 return array (
170 'title' => null,
171 'token' => null,
172 'reason' => null,
173 'watch' => false,
174 'unwatch' => false,
175 'oldimage' => null
176 );
177 }
178
179 public function getParamDescription() {
180 return array (
181 'title' => 'Title of the page you want to delete.',
182 'token' => 'A delete token previously retrieved through prop=info',
183 'reason' => 'Reason for the deletion. If not set, an automatically generated reason will be used.',
184 'watch' => 'Add the page to your watchlist',
185 'unwatch' => 'Remove the page from your watchlist',
186 'oldimage' => 'The name of the old image to delete as provided by iiprop=archivename'
187 );
188 }
189
190 public function getDescription() {
191 return array(
192 'Delete a page.'
193 );
194 }
195
196 protected function getExamples() {
197 return array (
198 'api.php?action=delete&title=Main%20Page&token=123ABC',
199 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
200 );
201 }
202
203 public function getVersion() {
204 return __CLASS__ . ': $Id$';
205 }
206 }