Merge "RC/Watchlist: Filter out parameters that cannot be displayed"
[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 $titleObj = $this->getRbTitle( $params );
49 $pageObj = WikiPage::factory( $titleObj );
50 $summary = $params['summary'];
51 $details = [];
52
53 // If change tagging was requested, check that the user is allowed to tag,
54 // and the tags are valid
55 if ( count( $params['tags'] ) ) {
56 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
57 if ( !$tagStatus->isOK() ) {
58 $this->dieStatus( $tagStatus );
59 }
60 }
61
62 $retval = $pageObj->doRollback(
63 $this->getRbUser( $params ),
64 $summary,
65 $params['token'],
66 $params['markbot'],
67 $details,
68 $user,
69 $params['tags']
70 );
71
72 if ( $retval ) {
73 $this->dieStatus( $this->errorArrayToStatus( $retval, $user ) );
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 = [
85 'title' => $titleObj->getPrefixedText(),
86 'pageid' => intval( $details['current']->getPage() ),
87 'summary' => $details['summary'],
88 'revid' => intval( $details['newid'] ),
89 // The revision being reverted (previously the current revision of the page)
90 'old_revid' => intval( $details['current']->getID() ),
91 // The revision being restored (the last revision before revision(s) by the reverted user)
92 'last_revid' => intval( $details['target']->getID() )
93 ];
94
95 $oldUser = $details['current']->getUserText( Revision::FOR_THIS_USER );
96 $lastUser = $details['target']->getUserText( Revision::FOR_THIS_USER );
97 $diffUrl = $titleObj->getFullURL( [
98 'diff' => $info['revid'],
99 'oldid' => $info['old_revid'],
100 'diffonly' => '1'
101 ] );
102 $info['messageHtml'] = $this->msg( 'rollback-success-notify' )
103 ->params( $oldUser, $lastUser, $diffUrl )
104 ->parseAsBlock();
105
106 $this->getResult()->addValue( null, $this->getModuleName(), $info );
107 }
108
109 public function mustBePosted() {
110 return true;
111 }
112
113 public function isWriteMode() {
114 return true;
115 }
116
117 public function getAllowedParams() {
118 return [
119 'title' => null,
120 'pageid' => [
121 ApiBase::PARAM_TYPE => 'integer'
122 ],
123 'tags' => [
124 ApiBase::PARAM_TYPE => 'tags',
125 ApiBase::PARAM_ISMULTI => true,
126 ],
127 'user' => [
128 ApiBase::PARAM_TYPE => 'user',
129 ApiBase::PARAM_REQUIRED => true
130 ],
131 'summary' => '',
132 'markbot' => false,
133 'watchlist' => [
134 ApiBase::PARAM_DFLT => 'preferences',
135 ApiBase::PARAM_TYPE => [
136 'watch',
137 'unwatch',
138 'preferences',
139 'nochange'
140 ],
141 ],
142 'token' => [
143 // Standard definition automatically inserted
144 ApiBase::PARAM_HELP_MSG_APPEND => [ 'api-help-param-token-webui' ],
145 ],
146 ];
147 }
148
149 public function needsToken() {
150 return 'rollback';
151 }
152
153 /**
154 * @param array $params
155 *
156 * @return string
157 */
158 private function getRbUser( array $params ) {
159 if ( $this->mUser !== null ) {
160 return $this->mUser;
161 }
162
163 // We need to be able to revert IPs, but getCanonicalName rejects them
164 $this->mUser = User::isIP( $params['user'] )
165 ? $params['user']
166 : User::getCanonicalName( $params['user'] );
167 if ( !$this->mUser ) {
168 $this->dieWithError( [ 'apierror-invaliduser', wfEscapeWikiText( $params['user'] ) ] );
169 }
170
171 return $this->mUser;
172 }
173
174 /**
175 * @param array $params
176 *
177 * @return Title
178 */
179 private function getRbTitle( array $params ) {
180 if ( $this->mTitleObj !== null ) {
181 return $this->mTitleObj;
182 }
183
184 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
185
186 if ( isset( $params['title'] ) ) {
187 $this->mTitleObj = Title::newFromText( $params['title'] );
188 if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
189 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
190 }
191 } elseif ( isset( $params['pageid'] ) ) {
192 $this->mTitleObj = Title::newFromID( $params['pageid'] );
193 if ( !$this->mTitleObj ) {
194 $this->dieWithError( [ 'apierror-nosuchpageid', $params['pageid'] ] );
195 }
196 }
197
198 if ( !$this->mTitleObj->exists() ) {
199 $this->dieWithError( 'apierror-missingtitle' );
200 }
201
202 return $this->mTitleObj;
203 }
204
205 protected function getExamplesMessages() {
206 return [
207 'action=rollback&title=Main%20Page&user=Example&token=123ABC' =>
208 'apihelp-rollback-example-simple',
209 'action=rollback&title=Main%20Page&user=192.0.2.5&' .
210 'token=123ABC&summary=Reverting%20vandalism&markbot=1' =>
211 'apihelp-rollback-example-summary',
212 ];
213 }
214
215 public function getHelpUrls() {
216 return 'https://www.mediawiki.org/wiki/API:Rollback';
217 }
218 }