Followup r92396, add help urls for most of the core (non query) modules
[lhc/web/wiklou.git] / includes / api / ApiRollback.php
1 <?php
2 /**
3 *
4 *
5 * Created on Jun 20, 2007
6 *
7 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@gmail.com
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 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
30 }
31
32 /**
33 * @ingroup API
34 */
35 class ApiRollback extends ApiBase {
36
37 public function __construct( $main, $action ) {
38 parent::__construct( $main, $action );
39 }
40
41 /**
42 * @var Title
43 */
44 private $mTitleObj = null;
45
46 /**
47 * @var User
48 */
49 private $mUser = null;
50
51 public function execute() {
52 $params = $this->extractRequestParams();
53
54 // User and title already validated in call to getTokenSalt from Main
55 $titleObj = $this->getTitle();
56 $articleObj = new Article( $titleObj );
57 $summary = ( isset( $params['summary'] ) ? $params['summary'] : '' );
58 $details = array();
59 $retval = $articleObj->doRollback( $this->getUser(), $summary, $params['token'], $params['markbot'], $details );
60
61 if ( $retval ) {
62 // We don't care about multiple errors, just report one of them
63 $this->dieUsageMsg( reset( $retval ) );
64 }
65
66 $this->setWatch( $params['watchlist'], $titleObj );
67
68 $info = array(
69 'title' => $titleObj->getPrefixedText(),
70 'pageid' => intval( $details['current']->getPage() ),
71 'summary' => $details['summary'],
72 'revid' => intval( $details['newid'] ),
73 'old_revid' => intval( $details['current']->getID() ),
74 'last_revid' => intval( $details['target']->getID() )
75 );
76
77 $this->getResult()->addValue( null, $this->getModuleName(), $info );
78 }
79
80 public function mustBePosted() {
81 return true;
82 }
83
84 public function isWriteMode() {
85 return true;
86 }
87
88 public function getAllowedParams() {
89 return array(
90 'title' => array(
91 ApiBase::PARAM_TYPE => 'string',
92 ApiBase::PARAM_REQUIRED => true
93 ),
94 'user' => array(
95 ApiBase::PARAM_TYPE => 'string',
96 ApiBase::PARAM_REQUIRED => true
97 ),
98 'token' => null,
99 'summary' => null,
100 'markbot' => false,
101 'watchlist' => array(
102 ApiBase::PARAM_DFLT => 'preferences',
103 ApiBase::PARAM_TYPE => array(
104 'watch',
105 'unwatch',
106 'preferences',
107 'nochange'
108 ),
109 ),
110 );
111 }
112
113 public function getParamDescription() {
114 return array(
115 'title' => 'Title of the page you want to rollback.',
116 'user' => 'Name of the user whose edits are to be rolled back. If set incorrectly, you\'ll get a badtoken error.',
117 'token' => "A rollback token previously retrieved through {$this->getModulePrefix()}prop=revisions",
118 'summary' => 'Custom edit summary. If not set, default summary will be used',
119 'markbot' => 'Mark the reverted edits and the revert as bot edits',
120 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
121 );
122 }
123
124 public function getDescription() {
125 return array(
126 'Undo the last edit to the page. If the last user who edited the page made multiple edits in a row,',
127 'they will all be rolled back'
128 );
129 }
130
131 public function getPossibleErrors() {
132 return array_merge( parent::getPossibleErrors(), array(
133 array( 'invalidtitle', 'title' ),
134 array( 'notanarticle' ),
135 array( 'invaliduser', 'user' ),
136 ) );
137 }
138
139 public function needsToken() {
140 return true;
141 }
142
143 public function getTokenSalt() {
144 return array( $this->getTitle()->getPrefixedText(), $this->getUser() );
145 }
146
147 private function getUser() {
148 if ( $this->mUser !== null ) {
149 return $this->mUser;
150 }
151
152 $params = $this->extractRequestParams();
153
154 // We need to be able to revert IPs, but getCanonicalName rejects them
155 $this->mUser = User::isIP( $params['user'] )
156 ? $params['user']
157 : User::getCanonicalName( $params['user'] );
158 if ( !$this->mUser ) {
159 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
160 }
161
162 return $this->mUser;
163 }
164
165 /**
166 * @return Title
167 */
168 private function getTitle() {
169 if ( $this->mTitleObj !== null ) {
170 return $this->mTitleObj;
171 }
172
173 $params = $this->extractRequestParams();
174
175 $this->mTitleObj = Title::newFromText( $params['title'] );
176
177 if ( !$this->mTitleObj ) {
178 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
179 }
180 if ( !$this->mTitleObj->exists() ) {
181 $this->dieUsageMsg( 'notanarticle' );
182 }
183
184 return $this->mTitleObj;
185 }
186
187 protected function getExamples() {
188 return array(
189 'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
190 'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&token=123ABC&summary=Reverting%20vandalism&markbot=1'
191 );
192 }
193
194 public function getHelpUrls() {
195 return 'http://www.mediawiki.org/wiki/API:Rollback';
196 }
197
198 public function getVersion() {
199 return __CLASS__ . ': $Id$';
200 }
201 }