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