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