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