Merge "API: Use message-per-value for apihelp-query+search-param-prop"
[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 $this->useTransactionalTimeLimit();
44
45 $user = $this->getUser();
46 $params = $this->extractRequestParams();
47
48 // WikiPage::doRollback needs a Web UI token, so get one of those if we
49 // validated based on an API rollback token.
50 $token = $params['token'];
51 if ( $user->matchEditToken( $token, 'rollback', $this->getRequest() ) ) {
52 $token = $this->getUser()->getEditToken(
53 $this->getWebUITokenSalt( $params ),
54 $this->getRequest()
55 );
56 }
57
58 $titleObj = $this->getRbTitle( $params );
59 $pageObj = WikiPage::factory( $titleObj );
60 $summary = $params['summary'];
61 $details = array();
62 $retval = $pageObj->doRollback(
63 $this->getRbUser( $params ),
64 $summary,
65 $token,
66 $params['markbot'],
67 $details,
68 $user
69 );
70
71 if ( $retval ) {
72 // We don't care about multiple errors, just report one of them
73 $this->dieUsageMsg( reset( $retval ) );
74 }
75
76 $watch = 'preferences';
77 if ( isset( $params['watchlist'] ) ) {
78 $watch = $params['watchlist'];
79 }
80
81 // Watch pages
82 $this->setWatch( $watch, $titleObj, 'watchrollback' );
83
84 $info = array(
85 'title' => $titleObj->getPrefixedText(),
86 'pageid' => intval( $details['current']->getPage() ),
87 'summary' => $details['summary'],
88 'revid' => intval( $details['newid'] ),
89 'old_revid' => intval( $details['current']->getID() ),
90 'last_revid' => intval( $details['target']->getID() )
91 );
92
93 $this->getResult()->addValue( null, $this->getModuleName(), $info );
94 }
95
96 public function mustBePosted() {
97 return true;
98 }
99
100 public function isWriteMode() {
101 return true;
102 }
103
104 public function getAllowedParams() {
105 return array(
106 'title' => null,
107 'pageid' => array(
108 ApiBase::PARAM_TYPE => 'integer'
109 ),
110 'user' => array(
111 ApiBase::PARAM_TYPE => 'string',
112 ApiBase::PARAM_REQUIRED => true
113 ),
114 'summary' => '',
115 'markbot' => false,
116 'watchlist' => array(
117 ApiBase::PARAM_DFLT => 'preferences',
118 ApiBase::PARAM_TYPE => array(
119 'watch',
120 'unwatch',
121 'preferences',
122 'nochange'
123 ),
124 ),
125 'token' => array(
126 // Standard definition automatically inserted
127 ApiBase::PARAM_HELP_MSG_APPEND => array( 'api-help-param-token-webui' ),
128 ),
129 );
130 }
131
132 public function needsToken() {
133 return 'rollback';
134 }
135
136 protected function getWebUITokenSalt( array $params ) {
137 return array(
138 $this->getRbTitle( $params )->getPrefixedText(),
139 $this->getRbUser( $params )
140 );
141 }
142
143 /**
144 * @param array $params
145 *
146 * @return string
147 */
148 private function getRbUser( array $params ) {
149 if ( $this->mUser !== null ) {
150 return $this->mUser;
151 }
152
153 // We need to be able to revert IPs, but getCanonicalName rejects them
154 $this->mUser = User::isIP( $params['user'] )
155 ? $params['user']
156 : User::getCanonicalName( $params['user'] );
157 if ( !$this->mUser ) {
158 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
159 }
160
161 return $this->mUser;
162 }
163
164 /**
165 * @param array $params
166 *
167 * @return Title
168 */
169 private function getRbTitle( array $params ) {
170 if ( $this->mTitleObj !== null ) {
171 return $this->mTitleObj;
172 }
173
174 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
175
176 if ( isset( $params['title'] ) ) {
177 $this->mTitleObj = Title::newFromText( $params['title'] );
178 if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
179 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
180 }
181 } elseif ( isset( $params['pageid'] ) ) {
182 $this->mTitleObj = Title::newFromID( $params['pageid'] );
183 if ( !$this->mTitleObj ) {
184 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
185 }
186 }
187
188 if ( !$this->mTitleObj->exists() ) {
189 $this->dieUsageMsg( 'notanarticle' );
190 }
191
192 return $this->mTitleObj;
193 }
194
195 protected function getExamplesMessages() {
196 return array(
197 'action=rollback&title=Main%20Page&user=Example&token=123ABC' =>
198 'apihelp-rollback-example-simple',
199 'action=rollback&title=Main%20Page&user=192.0.2.5&' .
200 'token=123ABC&summary=Reverting%20vandalism&markbot=1' =>
201 'apihelp-rollback-example-summary',
202 );
203 }
204
205 public function getHelpUrls() {
206 return 'https://www.mediawiki.org/wiki/API:Rollback';
207 }
208 }