Committing patch for bug 10931, which also fixes bug 13651. For a detailed explanatio...
[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 /**
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 $articleObj = new Article($titleObj);
68 $reason = (isset($params['reason']) ? $params['reason'] : NULL);
69 $retval = self::delete($articleObj, $params['token'], $reason);
70
71 if(!empty($retval))
72 // We don't care about multiple errors, just report one of them
73 $this->dieUsageMsg(current($retval));
74
75 if($params['watch'] || $wgUser->getOption('watchdeletion'))
76 $articleObj->doWatch();
77 else if($params['unwatch'])
78 $articleObj->doUnwatch();
79 $this->getMain()->scheduleCommit();
80 $r = array('title' => $titleObj->getPrefixedText(), 'reason' => $reason);
81 $this->getResult()->addValue(null, $this->getModuleName(), $r);
82 }
83
84 /**
85 * We have our own delete() function, since Article.php's implementation is split in two phases
86 *
87 * @param Article $article - Article object to work on
88 * @param string $token - Delete token (same as edit token)
89 * @param string $reason - Reason for the deletion. Autogenerated if NULL
90 * @return Title::getUserPermissionsErrors()-like array
91 */
92 public static function delete(&$article, $token, &$reason = NULL)
93 {
94 global $wgUser;
95
96 // Check permissions
97 $errors = $article->mTitle->getUserPermissionsErrors('delete', $wgUser);
98 if(!empty($errors))
99 return $errors;
100 if(wfReadOnly())
101 return array(array('readonlytext'));
102 if($wgUser->isBlocked())
103 return array(array('blocked'));
104
105 // Check token
106 if(!$wgUser->matchEditToken($token))
107 return array(array('sessionfailure'));
108
109 // Auto-generate a summary, if necessary
110 if(is_null($reason))
111 {
112 $reason = $article->generateReason($hasHistory);
113 if($reason === false)
114 return array(array('cannotdelete'));
115 }
116
117 // Luckily, Article.php provides a reusable delete function that does the hard work for us
118 if($article->doDeleteArticle($reason))
119 return array();
120 return array(array('cannotdelete', $article->mTitle->getPrefixedText()));
121 }
122
123 public function mustBePosted() { return true; }
124
125 public function getAllowedParams() {
126 return array (
127 'title' => null,
128 'token' => null,
129 'reason' => null,
130 'watch' => false,
131 'unwatch' => false
132 );
133 }
134
135 public function getParamDescription() {
136 return array (
137 'title' => 'Title of the page you want to delete.',
138 'token' => 'A delete token previously retrieved through prop=info',
139 'reason' => 'Reason for the deletion. If not set, an automatically generated reason will be used.',
140 'watch' => 'Add the page to your watchlist',
141 'unwatch' => 'Remove the page from your watchlist'
142 );
143 }
144
145 public function getDescription() {
146 return array(
147 'Deletes a page. You need to be logged in as a sysop to use this function, see also action=login.'
148 );
149 }
150
151 protected function getExamples() {
152 return array (
153 'api.php?action=delete&title=Main%20Page&token=123ABC',
154 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
155 );
156 }
157
158 public function getVersion() {
159 return __CLASS__ . ': $Id$';
160 }
161 }