Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / api / ApiRollback.php
blob02e62a035110e0680ee57107596c80c3b28de4e2
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 $user = $this->getUser();
44 $params = $this->extractRequestParams();
46 // WikiPage::doRollback needs a Web UI token, so get one of those if we
47 // validated based on an API rollback token.
48 $token = $params['token'];
49 if ( $user->matchEditToken( $token, 'rollback', $this->getRequest() ) ) {
50 $token = $this->getUser()->getEditToken(
51 $this->getWebUITokenSalt( $params ),
52 $this->getRequest()
56 $titleObj = $this->getRbTitle( $params );
57 $pageObj = WikiPage::factory( $titleObj );
58 $summary = $params['summary'];
59 $details = array();
60 $retval = $pageObj->doRollback(
61 $this->getRbUser( $params ),
62 $summary,
63 $token,
64 $params['markbot'],
65 $details,
66 $user
69 if ( $retval ) {
70 // We don't care about multiple errors, just report one of them
71 $this->dieUsageMsg( reset( $retval ) );
74 $watch = 'preferences';
75 if ( isset( $params['watchlist'] ) ) {
76 $watch = $params['watchlist'];
79 // Watch pages
80 $this->setWatch( $watch, $titleObj, 'watchrollback' );
82 $info = array(
83 'title' => $titleObj->getPrefixedText(),
84 'pageid' => intval( $details['current']->getPage() ),
85 'summary' => $details['summary'],
86 'revid' => intval( $details['newid'] ),
87 'old_revid' => intval( $details['current']->getID() ),
88 'last_revid' => intval( $details['target']->getID() )
91 $this->getResult()->addValue( null, $this->getModuleName(), $info );
94 public function mustBePosted() {
95 return true;
98 public function isWriteMode() {
99 return true;
102 public function getAllowedParams() {
103 return array(
104 'title' => null,
105 'pageid' => array(
106 ApiBase::PARAM_TYPE => 'integer'
108 'user' => array(
109 ApiBase::PARAM_TYPE => 'string',
110 ApiBase::PARAM_REQUIRED => true
112 'summary' => '',
113 'markbot' => false,
114 'watchlist' => array(
115 ApiBase::PARAM_DFLT => 'preferences',
116 ApiBase::PARAM_TYPE => array(
117 'watch',
118 'unwatch',
119 'preferences',
120 'nochange'
123 'token' => array(
124 // Standard definition automatically inserted
125 ApiBase::PARAM_HELP_MSG_APPEND => array( 'api-help-param-token-webui' ),
130 public function needsToken() {
131 return 'rollback';
134 protected function getWebUITokenSalt( array $params ) {
135 return array(
136 $this->getRbTitle( $params )->getPrefixedText(),
137 $this->getRbUser( $params )
142 * @param array $params
144 * @return string
146 private function getRbUser( array $params ) {
147 if ( $this->mUser !== null ) {
148 return $this->mUser;
151 // We need to be able to revert IPs, but getCanonicalName rejects them
152 $this->mUser = User::isIP( $params['user'] )
153 ? $params['user']
154 : User::getCanonicalName( $params['user'] );
155 if ( !$this->mUser ) {
156 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
159 return $this->mUser;
163 * @param array $params
165 * @return Title
167 private function getRbTitle( array $params ) {
168 if ( $this->mTitleObj !== null ) {
169 return $this->mTitleObj;
172 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
174 if ( isset( $params['title'] ) ) {
175 $this->mTitleObj = Title::newFromText( $params['title'] );
176 if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
177 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
179 } elseif ( isset( $params['pageid'] ) ) {
180 $this->mTitleObj = Title::newFromID( $params['pageid'] );
181 if ( !$this->mTitleObj ) {
182 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
186 if ( !$this->mTitleObj->exists() ) {
187 $this->dieUsageMsg( 'notanarticle' );
190 return $this->mTitleObj;
193 protected function getExamplesMessages() {
194 return array(
195 'action=rollback&title=Main%20Page&user=Example&token=123ABC' =>
196 'apihelp-rollback-example-simple',
197 'action=rollback&title=Main%20Page&user=192.0.2.5&' .
198 'token=123ABC&summary=Reverting%20vandalism&markbot=1' =>
199 'apihelp-rollback-example-summary',
203 public function getHelpUrls() {
204 return 'https://www.mediawiki.org/wiki/API:Rollback';