Committing patch for bug 10931, which also fixes bug 13651. For a detailed explanatio...
[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->dieUsageMsg(array('missingparam', 'title'));
47 if(!isset($params['user']))
48 $this->dieUsageMsg(array('missingparam', 'user'));
49 if(!isset($params['token']))
50 $this->dieUsageMsg(array('missingparam', 'token'));
51
52 $titleObj = Title::newFromText($params['title']);
53 if(!$titleObj)
54 $this->dieUsageMsg(array('invalidtitle', $params['title']));
55 if(!$titleObj->exists())
56 $this->dieUsageMsg(array('notanarticle'));
57
58 $username = User::getCanonicalName($params['user']);
59 if(!$username)
60 $this->dieUsageMsg(array('invaliduser', $params['user']));
61
62 $articleObj = new Article($titleObj);
63 $summary = (isset($params['summary']) ? $params['summary'] : "");
64 $details = null;
65 $retval = $articleObj->doRollback($username, $summary, $params['token'], $params['markbot'], $details);
66
67 if(!empty($retval))
68 // We don't care about multiple errors, just report one of them
69 $this->dieUsageMsg(current($retval));
70
71 $this->getMain()->scheduleCommit();
72 $current = $target = $summary = NULL;
73 extract($details);
74
75 $info = array(
76 'title' => $titleObj->getPrefixedText(),
77 'pageid' => $current->getPage(),
78 'summary' => $summary,
79 'revid' => $titleObj->getLatestRevID(),
80 'old_revid' => $current->getID(),
81 'last_revid' => $target->getID()
82 );
83
84 $this->getResult()->addValue(null, $this->getModuleName(), $info);
85 }
86
87 public function mustBePosted() { return true; }
88
89 public function getAllowedParams() {
90 return array (
91 'title' => null,
92 'user' => null,
93 'token' => null,
94 'summary' => null,
95 'markbot' => false
96 );
97 }
98
99 public function getParamDescription() {
100 return array (
101 'title' => 'Title of the page you want to rollback.',
102 'user' => 'Name of the user whose edits are to be rolled back. If set incorrectly, you\'ll get a badtoken error.',
103 'token' => 'A rollback token previously retrieved through prop=info',
104 'summary' => 'Custom edit summary. If not set, default summary will be used.',
105 'markbot' => 'Mark the reverted edits and the revert as bot edits'
106 );
107 }
108
109 public function getDescription() {
110 return array(
111 'Undoes the last edit to the page. If the last user who edited the page made multiple edits in a row,',
112 'they will all be rolled back. You need to be logged in as a sysop to use this function, see also action=login.'
113 );
114 }
115
116 protected function getExamples() {
117 return array (
118 'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
119 'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&token=123ABC&summary=Reverting%20vandalism&markbot=1'
120 );
121 }
122
123 public function getVersion() {
124 return __CLASS__ . ': $Id$';
125 }
126 }