Add language name for aeb
[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 $user = $this->getUser();
44 $params = $this->extractRequestParams();
45
46 // WikiPage::doRollback needs a Web UI token, so get one of those if we
47 // validated based on an API rollback token.
48 $token = $params['token'];
49 if ( $user->matchEditToken( $token, 'rollback', $this->getRequest() ) ) {
50 $token = $this->getUser()->getEditToken(
51 $this->getWebUITokenSalt( $params ),
52 $this->getRequest()
53 );
54 }
55
56 $titleObj = $this->getRbTitle( $params );
57 $pageObj = WikiPage::factory( $titleObj );
58 $summary = $params['summary'];
59 $details = array();
60 $retval = $pageObj->doRollback(
61 $this->getRbUser( $params ),
62 $summary,
63 $token,
64 $params['markbot'],
65 $details,
66 $user
67 );
68
69 if ( $retval ) {
70 // We don't care about multiple errors, just report one of them
71 $this->dieUsageMsg( reset( $retval ) );
72 }
73
74 $watch = 'preferences';
75 if ( isset( $params['watchlist'] ) ) {
76 $watch = $params['watchlist'];
77 }
78
79 // Watch pages
80 $this->setWatch( $watch, $titleObj, 'watchrollback' );
81
82 $info = array(
83 'title' => $titleObj->getPrefixedText(),
84 'pageid' => intval( $details['current']->getPage() ),
85 'summary' => $details['summary'],
86 'revid' => intval( $details['newid'] ),
87 'old_revid' => intval( $details['current']->getID() ),
88 'last_revid' => intval( $details['target']->getID() )
89 );
90
91 $this->getResult()->addValue( null, $this->getModuleName(), $info );
92 }
93
94 public function mustBePosted() {
95 return true;
96 }
97
98 public function isWriteMode() {
99 return true;
100 }
101
102 public function getAllowedParams() {
103 return array(
104 'title' => null,
105 'pageid' => array(
106 ApiBase::PARAM_TYPE => 'integer'
107 ),
108 'user' => array(
109 ApiBase::PARAM_TYPE => 'string',
110 ApiBase::PARAM_REQUIRED => true
111 ),
112 'summary' => '',
113 'markbot' => false,
114 'watchlist' => array(
115 ApiBase::PARAM_DFLT => 'preferences',
116 ApiBase::PARAM_TYPE => array(
117 'watch',
118 'unwatch',
119 'preferences',
120 'nochange'
121 ),
122 ),
123 );
124 }
125
126 public function getParamDescription() {
127 $p = $this->getModulePrefix();
128
129 return array(
130 'title' => "Title of the page you want to roll back. Cannot be used together with {$p}pageid",
131 'pageid' => "Page ID of the page you want to roll back. Cannot be used together with {$p}title",
132 'user' => 'Name of the user whose edits are to be rolled back.',
133 'token' => array(
134 /* Standard description automatically prepended */
135 'For compatibility, the token used in the web UI is also accepted.'
136 ),
137 'summary' => 'Custom edit summary. If empty, default summary will be used',
138 'markbot' => 'Mark the reverted edits and the revert as bot edits',
139 'watchlist' => 'Unconditionally add or remove the page from your watchlist, ' .
140 'use preferences or do not change watch',
141 );
142 }
143
144 public function getDescription() {
145 return array(
146 'Undo the last edit to the page. If the last user who edited the page made',
147 'multiple edits in a row, they will all be rolled back.'
148 );
149 }
150
151 public function needsToken() {
152 return 'rollback';
153 }
154
155 protected function getWebUITokenSalt( array $params ) {
156 return array(
157 $this->getRbTitle( $params )->getPrefixedText(),
158 $this->getRbUser( $params )
159 );
160 }
161
162 /**
163 * @param array $params
164 *
165 * @return string
166 */
167 private function getRbUser( array $params ) {
168 if ( $this->mUser !== null ) {
169 return $this->mUser;
170 }
171
172 // We need to be able to revert IPs, but getCanonicalName rejects them
173 $this->mUser = User::isIP( $params['user'] )
174 ? $params['user']
175 : User::getCanonicalName( $params['user'] );
176 if ( !$this->mUser ) {
177 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
178 }
179
180 return $this->mUser;
181 }
182
183 /**
184 * @param array $params
185 *
186 * @return Title
187 */
188 private function getRbTitle( array $params ) {
189 if ( $this->mTitleObj !== null ) {
190 return $this->mTitleObj;
191 }
192
193 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
194
195 if ( isset( $params['title'] ) ) {
196 $this->mTitleObj = Title::newFromText( $params['title'] );
197 if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
198 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
199 }
200 } elseif ( isset( $params['pageid'] ) ) {
201 $this->mTitleObj = Title::newFromID( $params['pageid'] );
202 if ( !$this->mTitleObj ) {
203 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
204 }
205 }
206
207 if ( !$this->mTitleObj->exists() ) {
208 $this->dieUsageMsg( 'notanarticle' );
209 }
210
211 return $this->mTitleObj;
212 }
213
214 public function getExamples() {
215 return array(
216 'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
217 'api.php?action=rollback&pageid=122&user=Catrope&token=123ABC',
218 'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&' .
219 'token=123ABC&summary=Reverting%20vandalism&markbot=1'
220 );
221 }
222
223 public function getHelpUrls() {
224 return 'https://www.mediawiki.org/wiki/API:Rollback';
225 }
226 }