Reduced some master queries by adding flags to Revision functions.
[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 getDescription() {
120 return array(
121 'Undo the last edit to the page. If the last user who edited the page made multiple edits in a row,',
122 'they will all be rolled back'
123 );
124 }
125
126 public function getPossibleErrors() {
127 return array_merge( parent::getPossibleErrors(), array(
128 array( 'invalidtitle', 'title' ),
129 array( 'notanarticle' ),
130 array( 'invaliduser', 'user' ),
131 ) );
132 }
133
134 public function needsToken() {
135 return true;
136 }
137
138 public function getTokenSalt() {
139 return array( $this->getRbTitle()->getPrefixedText(), $this->getRbUser() );
140 }
141
142 private function getRbUser() {
143 if ( $this->mUser !== null ) {
144 return $this->mUser;
145 }
146
147 $params = $this->extractRequestParams();
148
149 // We need to be able to revert IPs, but getCanonicalName rejects them
150 $this->mUser = User::isIP( $params['user'] )
151 ? $params['user']
152 : User::getCanonicalName( $params['user'] );
153 if ( !$this->mUser ) {
154 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
155 }
156
157 return $this->mUser;
158 }
159
160 /**
161 * @return Title
162 */
163 private function getRbTitle() {
164 if ( $this->mTitleObj !== null ) {
165 return $this->mTitleObj;
166 }
167
168 $params = $this->extractRequestParams();
169
170 $this->mTitleObj = Title::newFromText( $params['title'] );
171
172 if ( !$this->mTitleObj ) {
173 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
174 }
175 if ( !$this->mTitleObj->exists() ) {
176 $this->dieUsageMsg( 'notanarticle' );
177 }
178
179 return $this->mTitleObj;
180 }
181
182 public function getExamples() {
183 return array(
184 'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
185 'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&token=123ABC&summary=Reverting%20vandalism&markbot=1'
186 );
187 }
188
189 public function getHelpUrls() {
190 return 'https://www.mediawiki.org/wiki/API:Rollback';
191 }
192
193 public function getVersion() {
194 return __CLASS__ . ': $Id$';
195 }
196 }