39d908e52258b269b49bc15768835a1554305a39
[lhc/web/wiklou.git] / includes / api / ApiRollback.php
1 <?php
2
3 /*
4 * Created on Jun 20, 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 * @addtogroup API
32 */
33 class ApiRollback extends ApiBase {
34
35 public function __construct($main, $action) {
36 parent :: __construct($main, $action);
37 }
38
39 public function execute() {
40 global $wgUser;
41 $this->getMain()->requestWriteMode();
42 $params = $this->extractRequestParams();
43
44 $titleObj = NULL;
45 if(!isset($params['title']))
46 $this->dieUsage('The title parameter must be set', 'notitle');
47 if(!isset($params['user']))
48 $this->dieUsage('The user parameter must be set', 'nouser');
49 if(!isset($params['token']))
50 $this->dieUsage('The token parameter must be set', 'notoken');
51
52 // doRollback() also checks for these, but we wanna save some work
53 if($wgUser->isBlocked())
54 $this->dieUsage('You have been blocked from editing', 'blocked');
55 if(wfReadOnly())
56 $this->dieUsage('The wiki is in read-only mode', 'readonly');
57
58 $titleObj = Title::newFromText($params['title']);
59 if(!$titleObj)
60 $this->dieUsage("Bad title ``{$params['title']}''", 'invalidtitle');
61 if(!$titleObj->userCan('rollback'))
62 $this->dieUsage('You don\'t have permission to rollback', 'permissiondenied');
63
64 $username = User::getCanonicalName($params['user']);
65 if(!$username)
66 $this->dieUsage("Invalid username ``{$params['user']}''", 'invaliduser');
67
68 $articleObj = new Article($titleObj);
69 $summary = (isset($params['summary']) ? $params['summary'] : "");
70 $details = NULL;
71 $dbw = wfGetDb(DB_MASTER);
72 $dbw->begin();
73 $retval = $articleObj->doRollback($username, $summary, $params['token'], $params['markbot'], &$details);
74
75 switch($retval)
76 {
77 case Article::SUCCESS:
78 break; // We'll deal with that later
79 case Article::PERM_DENIED:
80 $this->dieUsage("You don't have permission to rollback", 'permissiondenied');
81 case Article::BLOCKED: // If we get BLOCKED or PERM_DENIED that's very weird, but it's possible
82 $this->dieUsage('You have been blocked from editing', 'blocked');
83 case Article::READONLY:
84 $this->dieUsage('The wiki is in read-only mode', 'readonly');
85 case Article::BAD_TOKEN:
86 $this->dieUsage('Invalid token', 'badtoken');
87 case Article::BAD_TITLE:
88 $this->dieUsage("``{$params['title']}'' doesn't exist", 'missingtitle');
89 case Article::ALREADYROLLED:
90 $current = $details['current'];
91 $currentID = $current->getId();
92 $this->dieUsage("The edit(s) you tried to rollback is/are already rolled back." .
93 "The current revision ID is ``$currentID''", 'alreadyrolled');
94 case Article::ONLY_AUTHOR:
95 $this->dieUsage("User ``$username'' is the only author of the page", 'onlyauthor');
96 case Article::RATE_LIMITED:
97 $this->dieUsage("You can't rollback too many articles in too short a time. Please wait a little while and try again", 'ratelimited');
98 default:
99 // rollback() has apparently invented a new error, which is extremely weird
100 $this->dieDebug(__METHOD__, "rollback() returned an unknown error ($retval)");
101 }
102 // $retval has to be Article::SUCCESS if we get here
103 $dbw->commit();
104 $current = $target = $summary = NULL;
105 extract($details);
106
107 $info = array(
108 'title' => $titleObj->getPrefixedText(),
109 'pageid' => $current->getPage(),
110 'summary' => $summary,
111 'revid' => $titleObj->getLatestRevID(),
112 'old_revid' => $current->getID(),
113 'last_revid' => $target->getID()
114 );
115
116 $this->getResult()->addValue(null, $this->getModuleName(), $info);
117 }
118
119 public function isEditMode() {
120 return true;
121 }
122
123 protected function getAllowedParams() {
124 return array (
125 'title' => null,
126 'user' => null,
127 'token' => null,
128 'summary' => null,
129 'markbot' => false
130 );
131 }
132
133 protected function getParamDescription() {
134 return array (
135 'title' => 'Title of the page you want to rollback.',
136 'user' => 'Name of the user whose edits are to be rolled back. If set incorrectly, you\'ll get a badtoken error.',
137 'token' => 'A rollback token previously retrieved through prop=info',
138 'summary' => 'Custom edit summary. If not set, default summary will be used.',
139 'markbot' => 'Mark the reverted edits and the revert as bot edits'
140 );
141 }
142
143 protected function getDescription() {
144 return array(
145 'Undoes the last edit to the page. If the last user who edited the page made multiple edits in a row,',
146 'they will all be rolled back. You need to be logged in as a sysop to use this function, see also action=login.'
147 );
148 }
149
150 protected function getExamples() {
151 return array (
152 'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
153 'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&token=123ABC&summary=Reverting%20vandalism&markbot=1'
154 );
155 }
156
157 public function getVersion() {
158 return __CLASS__ . ': $Id: ApiRollback.php 22289 2007-05-20 23:31:44Z yurik $';
159 }
160 }