Correct the address of the FSF in some of the GPL headers
[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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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, $mUser = null;
40
41 public function execute() {
42 $params = $this->extractRequestParams();
43
44 // User and title already validated in call to getTokenSalt from Main
45 $titleObj = $this->getTitle();
46 $articleObj = new Article( $titleObj );
47 $summary = ( isset( $params['summary'] ) ? $params['summary'] : '' );
48 $details = null;
49 $retval = $articleObj->doRollback( $this->getUser(), $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 $this->setWatch( $params['watchlist'], $titleObj );
57
58 $info = array(
59 'title' => $titleObj->getPrefixedText(),
60 'pageid' => intval( $details['current']->getPage() ),
61 'summary' => $details['summary'],
62 'revid' => intval( $details['newid'] ),
63 'old_revid' => intval( $details['current']->getID() ),
64 'last_revid' => intval( $details['target']->getID() )
65 );
66
67 $this->getResult()->addValue( null, $this->getModuleName(), $info );
68 }
69
70 public function mustBePosted() {
71 return true;
72 }
73
74 public function isWriteMode() {
75 return true;
76 }
77
78 public function getAllowedParams() {
79 return array(
80 'title' => null,
81 'user' => null,
82 'token' => null,
83 'summary' => null,
84 'markbot' => false,
85 'watchlist' => array(
86 ApiBase::PARAM_DFLT => 'preferences',
87 ApiBase::PARAM_TYPE => array(
88 'watch',
89 'unwatch',
90 'preferences',
91 'nochange'
92 ),
93 ),
94 );
95 }
96
97 public function getParamDescription() {
98 return array(
99 'title' => 'Title of the page you want to rollback.',
100 'user' => 'Name of the user whose edits are to be rolled back. If set incorrectly, you\'ll get a badtoken error.',
101 'token' => "A rollback token previously retrieved through {$this->getModulePrefix()}prop=revisions",
102 'summary' => 'Custom edit summary. If not set, default summary will be used',
103 'markbot' => 'Mark the reverted edits and the revert as bot edits',
104 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
105 );
106 }
107
108 public function getDescription() {
109 return array(
110 'Undo the last edit to the page. If the last user who edited the page made multiple edits in a row,',
111 'they will all be rolled back'
112 );
113 }
114
115 public function getPossibleErrors() {
116 return array_merge( parent::getPossibleErrors(), array(
117 array( 'missingparam', 'title' ),
118 array( 'missingparam', 'user' ),
119 array( 'invalidtitle', 'title' ),
120 array( 'notanarticle' ),
121 array( 'invaliduser', 'user' ),
122 ) );
123 }
124
125 public function getTokenSalt() {
126 return array( $this->getTitle()->getPrefixedText(), $this->getUser() );
127 }
128
129 private function getUser() {
130 if ( $this->mUser !== null ) {
131 return $this->mUser;
132 }
133
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->mUser = User::isIP( $params['user'] )
142 ? $params['user']
143 : User::getCanonicalName( $params['user'] );
144 if ( !$this->mUser ) {
145 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
146 }
147
148 return $this->mUser;
149 }
150
151 private function getTitle() {
152 if ( $this->mTitleObj !== null ) {
153 return $this->mTitleObj;
154 }
155
156 $params = $this->extractRequestParams();
157 if ( !isset( $params['title'] ) ) {
158 $this->dieUsageMsg( array( 'missingparam', 'title' ) );
159 }
160
161 $this->mTitleObj = Title::newFromText( $params['title'] );
162
163 if ( !$this->mTitleObj ) {
164 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
165 }
166 if ( !$this->mTitleObj->exists() ) {
167 $this->dieUsageMsg( array( 'notanarticle' ) );
168 }
169
170 return $this->mTitleObj;
171 }
172
173 protected function getExamples() {
174 return array(
175 'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
176 'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&token=123ABC&summary=Reverting%20vandalism&markbot=1'
177 );
178 }
179
180 public function getVersion() {
181 return __CLASS__ . ': $Id$';
182 }
183 }