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