Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / api / ApiRollback.php
blob9584f09be44515a04b911134d805142b70e14696
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 $titleObj = $this->getRbTitle( $params );
49 $pageObj = WikiPage::factory( $titleObj );
50 $summary = $params['summary'];
51 $details = [];
53 // If change tagging was requested, check that the user is allowed to tag,
54 // and the tags are valid
55 if ( count( $params['tags'] ) ) {
56 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
57 if ( !$tagStatus->isOK() ) {
58 $this->dieStatus( $tagStatus );
62 $retval = $pageObj->doRollback(
63 $this->getRbUser( $params ),
64 $summary,
65 $params['token'],
66 $params['markbot'],
67 $details,
68 $user,
69 $params['tags']
72 if ( $retval ) {
73 $this->dieStatus( $this->errorArrayToStatus( $retval, $user ) );
76 $watch = 'preferences';
77 if ( isset( $params['watchlist'] ) ) {
78 $watch = $params['watchlist'];
81 // Watch pages
82 $this->setWatch( $watch, $titleObj, 'watchrollback' );
84 $info = [
85 'title' => $titleObj->getPrefixedText(),
86 'pageid' => intval( $details['current']->getPage() ),
87 'summary' => $details['summary'],
88 'revid' => intval( $details['newid'] ),
89 // The revision being reverted (previously the current revision of the page)
90 'old_revid' => intval( $details['current']->getID() ),
91 // The revision being restored (the last revision before revision(s) by the reverted user)
92 'last_revid' => intval( $details['target']->getID() )
95 $this->getResult()->addValue( null, $this->getModuleName(), $info );
98 public function mustBePosted() {
99 return true;
102 public function isWriteMode() {
103 return true;
106 public function getAllowedParams() {
107 return [
108 'title' => null,
109 'pageid' => [
110 ApiBase::PARAM_TYPE => 'integer'
112 'tags' => [
113 ApiBase::PARAM_TYPE => 'tags',
114 ApiBase::PARAM_ISMULTI => true,
116 'user' => [
117 ApiBase::PARAM_TYPE => 'user',
118 ApiBase::PARAM_REQUIRED => true
120 'summary' => '',
121 'markbot' => false,
122 'watchlist' => [
123 ApiBase::PARAM_DFLT => 'preferences',
124 ApiBase::PARAM_TYPE => [
125 'watch',
126 'unwatch',
127 'preferences',
128 'nochange'
131 'token' => [
132 // Standard definition automatically inserted
133 ApiBase::PARAM_HELP_MSG_APPEND => [ 'api-help-param-token-webui' ],
138 public function needsToken() {
139 return 'rollback';
143 * @param array $params
145 * @return string
147 private function getRbUser( array $params ) {
148 if ( $this->mUser !== null ) {
149 return $this->mUser;
152 // We need to be able to revert IPs, but getCanonicalName rejects them
153 $this->mUser = User::isIP( $params['user'] )
154 ? $params['user']
155 : User::getCanonicalName( $params['user'] );
156 if ( !$this->mUser ) {
157 $this->dieWithError( [ 'apierror-invaliduser', wfEscapeWikiText( $params['user'] ) ] );
160 return $this->mUser;
164 * @param array $params
166 * @return Title
168 private function getRbTitle( array $params ) {
169 if ( $this->mTitleObj !== null ) {
170 return $this->mTitleObj;
173 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
175 if ( isset( $params['title'] ) ) {
176 $this->mTitleObj = Title::newFromText( $params['title'] );
177 if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
178 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
180 } elseif ( isset( $params['pageid'] ) ) {
181 $this->mTitleObj = Title::newFromID( $params['pageid'] );
182 if ( !$this->mTitleObj ) {
183 $this->dieWithError( [ 'apierror-nosuchpageid', $params['pageid'] ] );
187 if ( !$this->mTitleObj->exists() ) {
188 $this->dieWithError( 'apierror-missingtitle' );
191 return $this->mTitleObj;
194 protected function getExamplesMessages() {
195 return [
196 'action=rollback&title=Main%20Page&user=Example&token=123ABC' =>
197 'apihelp-rollback-example-simple',
198 'action=rollback&title=Main%20Page&user=192.0.2.5&' .
199 'token=123ABC&summary=Reverting%20vandalism&markbot=1' =>
200 'apihelp-rollback-example-summary',
204 public function getHelpUrls() {
205 return 'https://www.mediawiki.org/wiki/API:Rollback';