Merge "Fix bad reference in method doc"
[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 /**
28 * @ingroup API
29 */
30 class ApiRollback extends ApiBase {
31
32 /**
33 * @var Title
34 */
35 private $mTitleObj = null;
36
37 /**
38 * @var User
39 */
40 private $mUser = null;
41
42 public function execute() {
43 $params = $this->extractRequestParams();
44
45 // User and title already validated in call to getTokenSalt from Main
46 $titleObj = $this->getRbTitle( $params );
47 $pageObj = WikiPage::factory( $titleObj );
48 $summary = $params['summary'];
49 $details = array();
50 $retval = $pageObj->doRollback(
51 $this->getRbUser( $params ),
52 $summary,
53 $params['token'],
54 $params['markbot'],
55 $details,
56 $this->getUser()
57 );
58
59 if ( $retval ) {
60 // We don't care about multiple errors, just report one of them
61 $this->dieUsageMsg( reset( $retval ) );
62 }
63
64 $watch = 'preferences';
65 if ( isset( $params['watchlist'] ) ) {
66 $watch = $params['watchlist'];
67 }
68
69 // Watch pages
70 $this->setWatch( $watch, $titleObj, 'watchrollback' );
71
72 $info = array(
73 'title' => $titleObj->getPrefixedText(),
74 'pageid' => intval( $details['current']->getPage() ),
75 'summary' => $details['summary'],
76 'revid' => intval( $details['newid'] ),
77 'old_revid' => intval( $details['current']->getID() ),
78 'last_revid' => intval( $details['target']->getID() )
79 );
80
81 $this->getResult()->addValue( null, $this->getModuleName(), $info );
82 }
83
84 public function mustBePosted() {
85 return true;
86 }
87
88 public function isWriteMode() {
89 return true;
90 }
91
92 public function getAllowedParams() {
93 return array(
94 'title' => null,
95 'pageid' => array(
96 ApiBase::PARAM_TYPE => 'integer'
97 ),
98 'user' => array(
99 ApiBase::PARAM_TYPE => 'string',
100 ApiBase::PARAM_REQUIRED => true
101 ),
102 'token' => array(
103 ApiBase::PARAM_TYPE => 'string',
104 ApiBase::PARAM_REQUIRED => true
105 ),
106 'summary' => '',
107 'markbot' => false,
108 'watchlist' => array(
109 ApiBase::PARAM_DFLT => 'preferences',
110 ApiBase::PARAM_TYPE => array(
111 'watch',
112 'unwatch',
113 'preferences',
114 'nochange'
115 ),
116 ),
117 );
118 }
119
120 public function getParamDescription() {
121 $p = $this->getModulePrefix();
122
123 return array(
124 'title' => "Title of the page you want to roll back. Cannot be used together with {$p}pageid",
125 'pageid' => "Page ID of the page you want to roll back. Cannot be used together with {$p}title",
126 'user' => 'Name of the user whose edits are to be rolled back. If ' .
127 'set incorrectly, you\'ll get a badtoken error.',
128 'token' => 'A rollback token previously retrieved through ' .
129 "{$this->getModulePrefix()}prop=revisions",
130 'summary' => 'Custom edit summary. If empty, default summary will be used',
131 'markbot' => 'Mark the reverted edits and the revert as bot edits',
132 'watchlist' => 'Unconditionally add or remove the page from your watchlist, ' .
133 'use preferences or do not change watch',
134 );
135 }
136
137 public function getResultProperties() {
138 return array(
139 '' => array(
140 'title' => 'string',
141 'pageid' => 'integer',
142 'summary' => 'string',
143 'revid' => 'integer',
144 'old_revid' => 'integer',
145 'last_revid' => 'integer'
146 )
147 );
148 }
149
150 public function getDescription() {
151 return array(
152 'Undo the last edit to the page. If the last user who edited the page made',
153 'multiple edits in a row, they will all be rolled back.'
154 );
155 }
156
157 public function getPossibleErrors() {
158 return array_merge(
159 parent::getPossibleErrors(),
160 $this->getRequireOnlyOneParameterErrorMessages( array( 'title', 'pageid' ) ),
161 array(
162 array( 'invalidtitle', 'title' ),
163 array( 'notanarticle' ),
164 array( 'nosuchpageid', 'pageid' ),
165 array( 'invaliduser', 'user' ),
166 )
167 );
168 }
169
170 public function needsToken() {
171 return true;
172 }
173
174 public function getTokenSalt() {
175 $params = $this->extractRequestParams();
176
177 return array(
178 $this->getRbTitle( $params )->getPrefixedText(),
179 $this->getRbUser( $params )
180 );
181 }
182
183 /**
184 * @param array $params
185 *
186 * @return string
187 */
188 private function getRbUser( array $params ) {
189 if ( $this->mUser !== null ) {
190 return $this->mUser;
191 }
192
193 // We need to be able to revert IPs, but getCanonicalName rejects them
194 $this->mUser = User::isIP( $params['user'] )
195 ? $params['user']
196 : User::getCanonicalName( $params['user'] );
197 if ( !$this->mUser ) {
198 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
199 }
200
201 return $this->mUser;
202 }
203
204 /**
205 * @param array $params
206 *
207 * @return Title
208 */
209 private function getRbTitle( array $params ) {
210 if ( $this->mTitleObj !== null ) {
211 return $this->mTitleObj;
212 }
213
214 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
215
216 if ( isset( $params['title'] ) ) {
217 $this->mTitleObj = Title::newFromText( $params['title'] );
218 if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
219 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
220 }
221 } elseif ( isset( $params['pageid'] ) ) {
222 $this->mTitleObj = Title::newFromID( $params['pageid'] );
223 if ( !$this->mTitleObj ) {
224 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
225 }
226 }
227
228 if ( !$this->mTitleObj->exists() ) {
229 $this->dieUsageMsg( 'notanarticle' );
230 }
231
232 return $this->mTitleObj;
233 }
234
235 public function getExamples() {
236 return array(
237 'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
238 'api.php?action=rollback&pageid=122&user=Catrope&token=123ABC',
239 'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&' .
240 'token=123ABC&summary=Reverting%20vandalism&markbot=1'
241 );
242 }
243
244 public function getHelpUrls() {
245 return 'https://www.mediawiki.org/wiki/API:Rollback';
246 }
247 }