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