Oops, requestWriteMode() is in ApiMain, not ApiBase
[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 * @addtogroup API
33 */
34 class ApiDelete extends ApiBase {
35
36 public function __construct($main, $action) {
37 parent :: __construct($main, $action);
38 }
39
40 /**
41 * We have our own delete() function, since Article.php's implementation is split in two phases
42 * @param Article $article - Article object to work on
43 * @param string $token - Delete token (same as edit token)
44 * @param string $reason - Reason for the deletion. Autogenerated if NULL
45 * @return DELETE_SUCCESS on success, DELETE_* on failure
46 */
47
48 const DELETE_SUCCESS = 0;
49 const DELETE_PERM = 1;
50 const DELETE_BLOCKED = 2;
51 const DELETE_READONLY = 3;
52 const DELETE_BADTOKEN = 4;
53 const DELETE_BADARTICLE = 5;
54
55 public static function delete(&$article, $token, &$reason = NULL)
56 {
57 global $wgUser;
58
59 // Check permissions first
60 if(!$article->mTitle->userCan('delete'))
61 return self::DELETE_PERM;
62 if($wgUser->isBlocked())
63 return self::DELETE_BLOCKED;
64 if(wfReadOnly())
65 return self::DELETE_READONLY;
66
67 // Check token
68 if(!$wgUser->matchEditToken($token))
69 return self::DELETE_BADTOKEN;
70
71 // Auto-generate a summary, if necessary
72 if(is_null($reason))
73 {
74 $reason = $article->generateReason($hasHistory);
75 if($reason === false)
76 return self::DELETE_BADARTICLE;
77 }
78
79 // Luckily, Article.php provides a reusable delete function that does the hard work for us
80 if($article->doDeleteArticle($reason))
81 return self::DELETE_SUCCESS;
82 return self::DELETE_BADARTICLE;
83 }
84
85 public function execute() {
86 global $wgUser;
87 $this->requestWriteMode();
88 $params = $this->getMain()->extractRequestParams();
89
90 $titleObj = NULL;
91 if(!isset($params['title']))
92 $this->dieUsage('The title parameter must be set', 'notitle');
93 if(!isset($params['token']))
94 $this->dieUsage('The token parameter must be set', 'notoken');
95
96 // delete() also checks for these, but we wanna save some work
97 if(!$wgUser->isAllowed('delete'))
98 $this->dieUsage('You don\'t have permission to delete pages', 'permissiondenied');
99 if($wgUser->isBlocked())
100 $this->dieUsage('You have been blocked from editing', 'blocked');
101 if(wfReadOnly())
102 $this->dieUsage('The wiki is in read-only mode', 'readonly');
103
104 $titleObj = Title::newFromText($params['title']);
105 if(!$titleObj)
106 $this->dieUsage("Bad title ``{$params['title']}''", 'invalidtitle');
107 if(!$titleObj->exists())
108 $this->dieUsage("``{$params['title']}'' doesn't exist", 'missingtitle');
109
110 $articleObj = new Article($titleObj);
111 $reason = (isset($params['reason']) ? $params['reason'] : NULL);
112 $dbw = wfGetDb(DB_MASTER);
113 $dbw->begin();
114 $retval = self::delete(&$articleObj, $params['token'], &$reason);
115
116 switch($retval)
117 {
118 case self::DELETE_SUCCESS:
119 break; // We'll deal with that later
120 case self::DELETE_PERM: // If we get PERM, BLOCKED or READONLY that's weird, but it's possible
121 $this->dieUsage('You don\'t have permission to delete', 'permissiondenied');
122 case self::DELETE_BLOCKED:
123 $this->dieUsage('You have been blocked from editing', 'blocked');
124 case self::DELETE_READONLY:
125 $this->dieUsage('The wiki is in read-only mode', 'readonly');
126 case self::DELETE_BADTOKEN:
127 $this->dieUsage('Invalid token', 'badtoken');
128 case self::DELETE_BADARTICLE:
129 $this->dieUsage("The article ``{$params['title']}'' doesn't exist or has already been deleted", 'missingtitle');
130 default:
131 // delete() has apparently invented a new error, which is extremely weird
132 $this->dieDebug(__METHOD__, "delete() returned an unknown error ($retval)");
133 }
134 // $retval has to be self::DELETE_SUCCESS if we get here
135 $dbw->commit();
136 $r = array('title' => $titleObj->getPrefixedText(), 'reason' => $reason);
137 $this->getResult()->addValue(null, $this->getModuleName(), $r);
138 }
139
140 protected function getAllowedParams() {
141 return array (
142 'title' => null,
143 'token' => null,
144 'reason' => null,
145 );
146 }
147
148 protected function getParamDescription() {
149 return array (
150 'title' => 'Title of the page you want to delete.',
151 'token' => 'A delete token previously retrieved through prop=info',
152 'reason' => 'Reason for the deletion. If not set, an automatically generated reason will be used.'
153 );
154 }
155
156 protected function getDescription() {
157 return array(
158 'Deletes a page. You need to be logged in as a sysop to use this function, see also action=login.'
159 );
160 }
161
162 protected function getExamples() {
163 return array (
164 'api.php?action=delete&title=Main%20Page&token=123ABC',
165 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
166 );
167 }
168
169 public function getVersion() {
170 return __CLASS__ . ': $Id: ApiDelete.php 22289 2007-05-20 23:31:44Z yurik $';
171 }
172 }