Merge "rdbms: add DBConnRef sanity check to LoadBalancer::closeConnection"
[lhc/web/wiklou.git] / includes / api / ApiRollback.php
1 <?php
2 /**
3 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * @ingroup API
25 */
26 class ApiRollback extends ApiBase {
27
28 /**
29 * @var Title
30 */
31 private $mTitleObj = null;
32
33 /**
34 * @var User
35 */
36 private $mUser = null;
37
38 public function execute() {
39 $this->useTransactionalTimeLimit();
40
41 $user = $this->getUser();
42 $params = $this->extractRequestParams();
43
44 $titleObj = $this->getRbTitle( $params );
45 $pageObj = WikiPage::factory( $titleObj );
46 $summary = $params['summary'];
47 $details = [];
48
49 // If change tagging was requested, check that the user is allowed to tag,
50 // and the tags are valid
51 if ( $params['tags'] ) {
52 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
53 if ( !$tagStatus->isOK() ) {
54 $this->dieStatus( $tagStatus );
55 }
56 }
57
58 // @TODO: remove this hack once rollback uses POST (T88044)
59 $trxLimits = $this->getConfig()->get( 'TrxProfilerLimits' );
60 $trxProfiler = Profiler::instance()->getTransactionProfiler();
61 $trxProfiler->setExpectations( $trxLimits['POST'], __METHOD__ );
62 DeferredUpdates::addCallableUpdate( function () use ( $trxProfiler, $trxLimits ) {
63 $trxProfiler->setExpectations( $trxLimits['PostSend-POST'], __METHOD__ );
64 } );
65
66 $retval = $pageObj->doRollback(
67 $this->getRbUser( $params ),
68 $summary,
69 $params['token'],
70 $params['markbot'],
71 $details,
72 $user,
73 $params['tags']
74 );
75
76 if ( $retval ) {
77 $this->dieStatus( $this->errorArrayToStatus( $retval, $user ) );
78 }
79
80 $watch = $params['watchlist'] ?? 'preferences';
81
82 // Watch pages
83 $this->setWatch( $watch, $titleObj, 'watchrollback' );
84
85 $info = [
86 'title' => $titleObj->getPrefixedText(),
87 'pageid' => intval( $details['current']->getPage() ),
88 'summary' => $details['summary'],
89 'revid' => intval( $details['newid'] ),
90 // The revision being reverted (previously the current revision of the page)
91 'old_revid' => intval( $details['current']->getID() ),
92 // The revision being restored (the last revision before revision(s) by the reverted user)
93 'last_revid' => intval( $details['target']->getID() )
94 ];
95
96 $this->getResult()->addValue( null, $this->getModuleName(), $info );
97 }
98
99 public function mustBePosted() {
100 return true;
101 }
102
103 public function isWriteMode() {
104 return true;
105 }
106
107 public function getAllowedParams() {
108 return [
109 'title' => null,
110 'pageid' => [
111 ApiBase::PARAM_TYPE => 'integer'
112 ],
113 'tags' => [
114 ApiBase::PARAM_TYPE => 'tags',
115 ApiBase::PARAM_ISMULTI => true,
116 ],
117 'user' => [
118 ApiBase::PARAM_TYPE => 'user',
119 ApiBase::PARAM_REQUIRED => true
120 ],
121 'summary' => '',
122 'markbot' => false,
123 'watchlist' => [
124 ApiBase::PARAM_DFLT => 'preferences',
125 ApiBase::PARAM_TYPE => [
126 'watch',
127 'unwatch',
128 'preferences',
129 'nochange'
130 ],
131 ],
132 'token' => [
133 // Standard definition automatically inserted
134 ApiBase::PARAM_HELP_MSG_APPEND => [ 'api-help-param-token-webui' ],
135 ],
136 ];
137 }
138
139 public function needsToken() {
140 return 'rollback';
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->dieWithError( [ 'apierror-invaliduser', wfEscapeWikiText( $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->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
180 }
181 } elseif ( isset( $params['pageid'] ) ) {
182 $this->mTitleObj = Title::newFromID( $params['pageid'] );
183 if ( !$this->mTitleObj ) {
184 $this->dieWithError( [ 'apierror-nosuchpageid', $params['pageid'] ] );
185 }
186 }
187
188 if ( !$this->mTitleObj->exists() ) {
189 $this->dieWithError( 'apierror-missingtitle' );
190 }
191
192 return $this->mTitleObj;
193 }
194
195 protected function getExamplesMessages() {
196 return [
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/Special:MyLanguage/API:Rollback';
207 }
208 }