User::isSafeToLoad() should return false if MW_NO_SESSION
[mediawiki.git] / includes / api / ApiRollback.php
blob55f714371957bc2347e46da4c869e483108b5ec8
1 <?php
2 /**
5 * Created on Jun 20, 2007
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
24 * @file
27 /**
28 * @ingroup API
30 class ApiRollback extends ApiBase {
32 /**
33 * @var Title
35 private $mTitleObj = null;
37 /**
38 * @var User
40 private $mUser = null;
42 public function execute() {
43 $this->useTransactionalTimeLimit();
45 $user = $this->getUser();
46 $params = $this->extractRequestParams();
48 // WikiPage::doRollback needs a Web UI token, so get one of those if we
49 // validated based on an API rollback token.
50 $token = $params['token'];
51 if ( $user->matchEditToken( $token, 'rollback', $this->getRequest() ) ) {
52 $token = $this->getUser()->getEditToken(
53 $this->getWebUITokenSalt( $params ),
54 $this->getRequest()
58 $titleObj = $this->getRbTitle( $params );
59 $pageObj = WikiPage::factory( $titleObj );
60 $summary = $params['summary'];
61 $details = [];
63 // If change tagging was requested, check that the user is allowed to tag,
64 // and the tags are valid
65 if ( count( $params['tags'] ) ) {
66 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
67 if ( !$tagStatus->isOK() ) {
68 $this->dieStatus( $tagStatus );
72 $retval = $pageObj->doRollback(
73 $this->getRbUser( $params ),
74 $summary,
75 $token,
76 $params['markbot'],
77 $details,
78 $user,
79 $params['tags']
82 if ( $retval ) {
83 // We don't care about multiple errors, just report one of them
84 $this->dieUsageMsg( reset( $retval ) );
87 $watch = 'preferences';
88 if ( isset( $params['watchlist'] ) ) {
89 $watch = $params['watchlist'];
92 // Watch pages
93 $this->setWatch( $watch, $titleObj, 'watchrollback' );
95 $info = [
96 'title' => $titleObj->getPrefixedText(),
97 'pageid' => intval( $details['current']->getPage() ),
98 'summary' => $details['summary'],
99 'revid' => intval( $details['newid'] ),
100 'old_revid' => intval( $details['current']->getID() ),
101 'last_revid' => intval( $details['target']->getID() )
104 $this->getResult()->addValue( null, $this->getModuleName(), $info );
107 public function mustBePosted() {
108 return true;
111 public function isWriteMode() {
112 return true;
115 public function getAllowedParams() {
116 return [
117 'title' => null,
118 'pageid' => [
119 ApiBase::PARAM_TYPE => 'integer'
121 'tags' => [
122 ApiBase::PARAM_TYPE => 'tags',
123 ApiBase::PARAM_ISMULTI => true,
125 'user' => [
126 ApiBase::PARAM_TYPE => 'user',
127 ApiBase::PARAM_REQUIRED => true
129 'summary' => '',
130 'markbot' => false,
131 'watchlist' => [
132 ApiBase::PARAM_DFLT => 'preferences',
133 ApiBase::PARAM_TYPE => [
134 'watch',
135 'unwatch',
136 'preferences',
137 'nochange'
140 'token' => [
141 // Standard definition automatically inserted
142 ApiBase::PARAM_HELP_MSG_APPEND => [ 'api-help-param-token-webui' ],
147 public function needsToken() {
148 return 'rollback';
151 protected function getWebUITokenSalt( array $params ) {
152 return [
153 $this->getRbTitle( $params )->getPrefixedText(),
154 $this->getRbUser( $params )
159 * @param array $params
161 * @return string
163 private function getRbUser( array $params ) {
164 if ( $this->mUser !== null ) {
165 return $this->mUser;
168 // We need to be able to revert IPs, but getCanonicalName rejects them
169 $this->mUser = User::isIP( $params['user'] )
170 ? $params['user']
171 : User::getCanonicalName( $params['user'] );
172 if ( !$this->mUser ) {
173 $this->dieUsageMsg( [ 'invaliduser', $params['user'] ] );
176 return $this->mUser;
180 * @param array $params
182 * @return Title
184 private function getRbTitle( array $params ) {
185 if ( $this->mTitleObj !== null ) {
186 return $this->mTitleObj;
189 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
191 if ( isset( $params['title'] ) ) {
192 $this->mTitleObj = Title::newFromText( $params['title'] );
193 if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
194 $this->dieUsageMsg( [ 'invalidtitle', $params['title'] ] );
196 } elseif ( isset( $params['pageid'] ) ) {
197 $this->mTitleObj = Title::newFromID( $params['pageid'] );
198 if ( !$this->mTitleObj ) {
199 $this->dieUsageMsg( [ 'nosuchpageid', $params['pageid'] ] );
203 if ( !$this->mTitleObj->exists() ) {
204 $this->dieUsageMsg( 'notanarticle' );
207 return $this->mTitleObj;
210 protected function getExamplesMessages() {
211 return [
212 'action=rollback&title=Main%20Page&user=Example&token=123ABC' =>
213 'apihelp-rollback-example-simple',
214 'action=rollback&title=Main%20Page&user=192.0.2.5&' .
215 'token=123ABC&summary=Reverting%20vandalism&markbot=1' =>
216 'apihelp-rollback-example-summary',
220 public function getHelpUrls() {
221 return 'https://www.mediawiki.org/wiki/API:Rollback';