4 * Created on Jun 30, 2007
5 * API for MediaWiki 1.8+
7 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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.
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.
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
25 if (!defined('MEDIAWIKI')) {
26 // Eclipse helper - will be ignored in production
27 require_once ("ApiBase.php");
34 class ApiDelete
extends ApiBase
{
36 public function __construct($main, $action) {
37 parent
:: __construct($main, $action);
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
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;
55 public static function delete(&$article, $token, &$reason = NULL)
59 // Check permissions first
60 if(!$article->mTitle
->userCan('delete'))
61 return self
::DELETE_PERM
;
62 if($wgUser->isBlocked())
63 return self
::DELETE_BLOCKED
;
65 return self
::DELETE_READONLY
;
68 if(!$wgUser->matchEditToken($token))
69 return self
::DELETE_BADTOKEN
;
71 // Auto-generate a summary, if necessary
74 $reason = $article->generateReason($hasHistory);
76 return self
::DELETE_BADARTICLE
;
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
;
85 public function execute() {
87 $this->getMain()->requestWriteMode();
88 $params = $this->extractRequestParams();
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');
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');
102 $this->dieUsage('The wiki is in read-only mode', 'readonly');
104 $titleObj = Title
::newFromText($params['title']);
106 $this->dieUsage("Bad title ``{$params['title']}''", 'invalidtitle');
107 if(!$titleObj->exists())
108 $this->dieUsage("``{$params['title']}'' doesn't exist", 'missingtitle');
110 $articleObj = new Article($titleObj);
111 $reason = (isset($params['reason']) ?
$params['reason'] : NULL);
112 $dbw = wfGetDb(DB_MASTER
);
114 $retval = self
::delete(&$articleObj, $params['token'], &$reason);
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');
131 // delete() has apparently invented a new error, which is extremely weird
132 $this->dieDebug(__METHOD__
, "delete() returned an unknown error ($retval)");
134 // $retval has to be self::DELETE_SUCCESS if we get here
136 $r = array('title' => $titleObj->getPrefixedText(), 'reason' => $reason);
137 $this->getResult()->addValue(null, $this->getModuleName(), $r);
140 protected function getAllowedParams() {
148 protected function getParamDescription() {
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.'
156 protected function getDescription() {
158 'Deletes a page. You need to be logged in as a sysop to use this function, see also action=login.'
162 protected function getExamples() {
164 'api.php?action=delete&title=Main%20Page&token=123ABC',
165 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
169 public function getVersion() {
170 return __CLASS__
. ': $Id$';