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
30 class ApiRollback
extends ApiBase
{
35 private $mTitleObj = null;
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 ),
56 $titleObj = $this->getRbTitle( $params );
57 $pageObj = WikiPage
::factory( $titleObj );
58 $summary = $params['summary'];
60 $retval = $pageObj->doRollback(
61 $this->getRbUser( $params ),
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'];
80 $this->setWatch( $watch, $titleObj, 'watchrollback' );
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() {
98 public function isWriteMode() {
102 public function getAllowedParams() {
106 ApiBase
::PARAM_TYPE
=> 'integer'
109 ApiBase
::PARAM_TYPE
=> 'string',
110 ApiBase
::PARAM_REQUIRED
=> true
114 'watchlist' => array(
115 ApiBase
::PARAM_DFLT
=> 'preferences',
116 ApiBase
::PARAM_TYPE
=> array(
124 // Standard definition automatically inserted
125 ApiBase
::PARAM_HELP_MSG_APPEND
=> array( 'api-help-param-token-webui' ),
130 public function needsToken() {
134 protected function getWebUITokenSalt( array $params ) {
136 $this->getRbTitle( $params )->getPrefixedText(),
137 $this->getRbUser( $params )
142 * @param array $params
146 private function getRbUser( array $params ) {
147 if ( $this->mUser
!== null ) {
151 // We need to be able to revert IPs, but getCanonicalName rejects them
152 $this->mUser
= User
::isIP( $params['user'] )
154 : User
::getCanonicalName( $params['user'] );
155 if ( !$this->mUser
) {
156 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
163 * @param array $params
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() {
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';