Merge "TitlesMultiselectWidget: pass through additional configs"
[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
63 $retval = $pageObj->doRollback(
64 $this->getRbUser( $params ),
65 $summary,
66 $params['token'],
67 $params['markbot'],
68 $details,
69 $user,
70 $params['tags']
71 );
72
73 if ( $retval ) {
74 $this->dieStatus( $this->errorArrayToStatus( $retval, $user ) );
75 }
76
77 $watch = $params['watchlist'] ?? 'preferences';
78
79 // Watch pages
80 $this->setWatch( $watch, $titleObj, 'watchrollback' );
81
82 $info = [
83 'title' => $titleObj->getPrefixedText(),
84 'pageid' => intval( $details['current']->getPage() ),
85 'summary' => $details['summary'],
86 'revid' => intval( $details['newid'] ),
87 // The revision being reverted (previously the current revision of the page)
88 'old_revid' => intval( $details['current']->getID() ),
89 // The revision being restored (the last revision before revision(s) by the reverted user)
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 [
106 'title' => null,
107 'pageid' => [
108 ApiBase::PARAM_TYPE => 'integer'
109 ],
110 'tags' => [
111 ApiBase::PARAM_TYPE => 'tags',
112 ApiBase::PARAM_ISMULTI => true,
113 ],
114 'user' => [
115 ApiBase::PARAM_TYPE => 'user',
116 ApiBase::PARAM_REQUIRED => true
117 ],
118 'summary' => '',
119 'markbot' => false,
120 'watchlist' => [
121 ApiBase::PARAM_DFLT => 'preferences',
122 ApiBase::PARAM_TYPE => [
123 'watch',
124 'unwatch',
125 'preferences',
126 'nochange'
127 ],
128 ],
129 'token' => [
130 // Standard definition automatically inserted
131 ApiBase::PARAM_HELP_MSG_APPEND => [ 'api-help-param-token-webui' ],
132 ],
133 ];
134 }
135
136 public function needsToken() {
137 return 'rollback';
138 }
139
140 /**
141 * @param array $params
142 *
143 * @return string
144 */
145 private function getRbUser( array $params ) {
146 if ( $this->mUser !== null ) {
147 return $this->mUser;
148 }
149
150 // We need to be able to revert IPs, but getCanonicalName rejects them
151 $this->mUser = User::isIP( $params['user'] )
152 ? $params['user']
153 : User::getCanonicalName( $params['user'] );
154 if ( !$this->mUser ) {
155 $this->dieWithError( [ 'apierror-invaliduser', wfEscapeWikiText( $params['user'] ) ] );
156 }
157
158 return $this->mUser;
159 }
160
161 /**
162 * @param array $params
163 *
164 * @return Title
165 */
166 private function getRbTitle( array $params ) {
167 if ( $this->mTitleObj !== null ) {
168 return $this->mTitleObj;
169 }
170
171 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
172
173 if ( isset( $params['title'] ) ) {
174 $this->mTitleObj = Title::newFromText( $params['title'] );
175 if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
176 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
177 }
178 } elseif ( isset( $params['pageid'] ) ) {
179 $this->mTitleObj = Title::newFromID( $params['pageid'] );
180 if ( !$this->mTitleObj ) {
181 $this->dieWithError( [ 'apierror-nosuchpageid', $params['pageid'] ] );
182 }
183 }
184
185 if ( !$this->mTitleObj->exists() ) {
186 $this->dieWithError( 'apierror-missingtitle' );
187 }
188
189 return $this->mTitleObj;
190 }
191
192 protected function getExamplesMessages() {
193 return [
194 'action=rollback&title=Main%20Page&user=Example&token=123ABC' =>
195 'apihelp-rollback-example-simple',
196 'action=rollback&title=Main%20Page&user=192.0.2.5&' .
197 'token=123ABC&summary=Reverting%20vandalism&markbot=1' =>
198 'apihelp-rollback-example-summary',
199 ];
200 }
201
202 public function getHelpUrls() {
203 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Rollback';
204 }
205 }