fix a bug where an undefined var was used, api actually works now?
[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 * @ingroup API
32 */
33 class ApiRollback extends ApiBase {
34
35 public function __construct( $main, $action ) {
36 parent::__construct( $main, $action );
37 }
38
39 private $mTitleObj = null;
40
41 public function execute() {
42 $params = $this->extractRequestParams();
43
44 // User and title already validated in call to getTokenSalt from Main
45
46 $articleObj = new Article( $this->mTitleObj );
47 $summary = ( isset( $params['summary'] ) ? $params['summary'] : '' );
48 $details = null;
49 $retval = $articleObj->doRollback( $params['user'], $summary, $params['token'], $params['markbot'], $details );
50
51 if ( $retval ) {
52 // We don't care about multiple errors, just report one of them
53 $this->dieUsageMsg( reset( $retval ) );
54 }
55
56 $watch = $this->getWatchlistValue( $params['watchlist'], $this->mTitleObj );
57
58 if ( $watch !== null) {
59 if ( $watch ) {
60 $articleObj->doWatch();
61 } else {
62 $articleObj->doUnwatch();
63 }
64 }
65
66 $info = array(
67 'title' => $this->mTitleObj->getPrefixedText(),
68 'pageid' => intval( $details['current']->getPage() ),
69 'summary' => $details['summary'],
70 'revid' => intval( $details['newid'] ),
71 'old_revid' => intval( $details['current']->getID() ),
72 'last_revid' => intval( $details['target']->getID() )
73 );
74
75 $this->getResult()->addValue( null, $this->getModuleName(), $info );
76 }
77
78 public function mustBePosted() {
79 return true;
80 }
81
82 public function isWriteMode() {
83 return true;
84 }
85
86 public function getAllowedParams() {
87 return array(
88 'title' => null,
89 'user' => null,
90 'token' => null,
91 'summary' => null,
92 'markbot' => false,
93 'watchlist' => array(
94 ApiBase::PARAM_DFLT => 'preferences',
95 ApiBase::PARAM_TYPE => array(
96 'watch',
97 'unwatch',
98 'preferences',
99 'nochange'
100 ),
101 ),
102 );
103 }
104
105 public function getParamDescription() {
106 return array(
107 'title' => 'Title of the page you want to rollback.',
108 'user' => 'Name of the user whose edits are to be rolled back. If set incorrectly, you\'ll get a badtoken error.',
109 'token' => 'A rollback token previously retrieved through prop=revisions',
110 'summary' => 'Custom edit summary. If not set, default summary will be used.',
111 'markbot' => 'Mark the reverted edits and the revert as bot edits',
112 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
113 );
114 }
115
116 public function getDescription() {
117 return array(
118 'Undo the last edit to the page. If the last user who edited the page made multiple edits in a row,',
119 'they will all be rolled back.'
120 );
121 }
122
123 public function getPossibleErrors() {
124 return array_merge( parent::getPossibleErrors(), array(
125 array( 'missingparam', 'title' ),
126 array( 'missingparam', 'user' ),
127 array( 'invalidtitle', 'title' ),
128 array( 'notanarticle' ),
129 array( 'invaliduser', 'user' ),
130 ) );
131 }
132
133 public function getTokenSalt() {
134 $params = $this->extractRequestParams();
135
136 if ( !isset( $params['user'] ) ) {
137 $this->dieUsageMsg( array( 'missingparam', 'user' ) );
138 }
139
140 // We need to be able to revert IPs, but getCanonicalName rejects them
141 $this->username = User::isIP( $params['user'] )
142 ? $params['user']
143 : User::getCanonicalName( $params['user'] );
144 if ( !$this->username ) {
145 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
146 }
147
148 if ( !isset( $params['title'] ) ) {
149 $this->dieUsageMsg( array( 'missingparam', 'title' ) );
150 }
151
152 $this->mTitleObj = Title::newFromText( $params['title'] );
153 if ( !$this->mTitleObj ) {
154 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
155 }
156 if ( !$this->mTitleObj->exists() ) {
157 $this->dieUsageMsg( array( 'notanarticle' ) );
158 }
159
160 return array( $this->mTitleObj->getPrefixedText(), $this->username );
161 }
162
163 protected function getExamples() {
164 return array(
165 'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
166 'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&token=123ABC&summary=Reverting%20vandalism&markbot=1'
167 );
168 }
169
170 public function getVersion() {
171 return __CLASS__ . ': $Id$';
172 }
173 }