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