Merge "Add passing ''italic'''s case to 'Unclosed and unmatched quotes' test"
[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 = ( isset( $params['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' => null,
94 'summary' => null,
95 'markbot' => false,
96 'watchlist' => array(
97 ApiBase::PARAM_DFLT => 'preferences',
98 ApiBase::PARAM_TYPE => array(
99 'watch',
100 'unwatch',
101 'preferences',
102 'nochange'
103 ),
104 ),
105 );
106 }
107
108 public function getParamDescription() {
109 return array(
110 'title' => 'Title of the page you want to rollback.',
111 'user' => 'Name of the user whose edits are to be rolled back. If set incorrectly, you\'ll get a badtoken error.',
112 'token' => "A rollback token previously retrieved through {$this->getModulePrefix()}prop=revisions",
113 'summary' => 'Custom edit summary. If not set, default summary will be used',
114 'markbot' => 'Mark the reverted edits and the revert as bot edits',
115 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
116 );
117 }
118
119 public function getResultProperties() {
120 return array(
121 '' => array(
122 'title' => 'string',
123 'pageid' => 'integer',
124 'summary' => 'string',
125 'revid' => 'integer',
126 'old_revid' => 'integer',
127 'last_revid' => 'integer'
128 )
129 );
130 }
131
132 public function getDescription() {
133 return array(
134 'Undo the last edit to the page. If the last user who edited the page made multiple edits in a row,',
135 'they will all be rolled back'
136 );
137 }
138
139 public function getPossibleErrors() {
140 return array_merge( parent::getPossibleErrors(), array(
141 array( 'invalidtitle', 'title' ),
142 array( 'notanarticle' ),
143 array( 'invaliduser', 'user' ),
144 ) );
145 }
146
147 public function needsToken() {
148 return true;
149 }
150
151 public function getTokenSalt() {
152 return array( $this->getRbTitle()->getPrefixedText(), $this->getRbUser() );
153 }
154
155 private function getRbUser() {
156 if ( $this->mUser !== null ) {
157 return $this->mUser;
158 }
159
160 $params = $this->extractRequestParams();
161
162 // We need to be able to revert IPs, but getCanonicalName rejects them
163 $this->mUser = User::isIP( $params['user'] )
164 ? $params['user']
165 : User::getCanonicalName( $params['user'] );
166 if ( !$this->mUser ) {
167 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
168 }
169
170 return $this->mUser;
171 }
172
173 /**
174 * @return Title
175 */
176 private function getRbTitle() {
177 if ( $this->mTitleObj !== null ) {
178 return $this->mTitleObj;
179 }
180
181 $params = $this->extractRequestParams();
182
183 $this->mTitleObj = Title::newFromText( $params['title'] );
184
185 if ( !$this->mTitleObj ) {
186 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
187 }
188 if ( !$this->mTitleObj->exists() ) {
189 $this->dieUsageMsg( 'notanarticle' );
190 }
191
192 return $this->mTitleObj;
193 }
194
195 public function getExamples() {
196 return array(
197 'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
198 'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&token=123ABC&summary=Reverting%20vandalism&markbot=1'
199 );
200 }
201
202 public function getHelpUrls() {
203 return 'https://www.mediawiki.org/wiki/API:Rollback';
204 }
205
206 public function getVersion() {
207 return __CLASS__ . ': $Id$';
208 }
209 }