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
{
32 public function __construct( $main, $action ) {
33 parent
::__construct( $main, $action );
39 private $mTitleObj = null;
44 private $mUser = null;
46 public function execute() {
47 $params = $this->extractRequestParams();
49 // User and title already validated in call to getTokenSalt from Main
50 $titleObj = $this->getRbTitle();
51 $pageObj = WikiPage
::factory( $titleObj );
52 $summary = ( isset( $params['summary'] ) ?
$params['summary'] : '' );
54 $retval = $pageObj->doRollback( $this->getRbUser(), $summary, $params['token'], $params['markbot'], $details, $this->getUser() );
57 // We don't care about multiple errors, just report one of them
58 $this->dieUsageMsg( reset( $retval ) );
61 $this->setWatch( $params['watchlist'], $titleObj );
64 'title' => $titleObj->getPrefixedText(),
65 'pageid' => intval( $details['current']->getPage() ),
66 'summary' => $details['summary'],
67 'revid' => intval( $details['newid'] ),
68 'old_revid' => intval( $details['current']->getID() ),
69 'last_revid' => intval( $details['target']->getID() )
72 $this->getResult()->addValue( null, $this->getModuleName(), $info );
75 public function mustBePosted() {
79 public function isWriteMode() {
83 public function getAllowedParams() {
86 ApiBase
::PARAM_TYPE
=> 'string',
87 ApiBase
::PARAM_REQUIRED
=> true
90 ApiBase
::PARAM_TYPE
=> 'string',
91 ApiBase
::PARAM_REQUIRED
=> true
97 ApiBase
::PARAM_DFLT
=> 'preferences',
98 ApiBase
::PARAM_TYPE
=> array(
108 public function getParamDescription() {
110 'title' => 'Title of the page you want to rollback.',
111 'user' => 'Name of the user whose edits are to be rolled back. If set incorrectly, you\'ll get a badtoken error.',
112 'token' => "A rollback token previously retrieved through {$this->getModulePrefix()}prop=revisions",
113 'summary' => 'Custom edit summary. If not set, default summary will be used',
114 'markbot' => 'Mark the reverted edits and the revert as bot edits',
115 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
119 public function getDescription() {
121 'Undo the last edit to the page. If the last user who edited the page made multiple edits in a row,',
122 'they will all be rolled back'
126 public function getPossibleErrors() {
127 return array_merge( parent
::getPossibleErrors(), array(
128 array( 'invalidtitle', 'title' ),
129 array( 'notanarticle' ),
130 array( 'invaliduser', 'user' ),
134 public function needsToken() {
138 public function getTokenSalt() {
139 return array( $this->getRbTitle()->getPrefixedText(), $this->getRbUser() );
142 private function getRbUser() {
143 if ( $this->mUser
!== null ) {
147 $params = $this->extractRequestParams();
149 // We need to be able to revert IPs, but getCanonicalName rejects them
150 $this->mUser
= User
::isIP( $params['user'] )
152 : User
::getCanonicalName( $params['user'] );
153 if ( !$this->mUser
) {
154 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
163 private function getRbTitle() {
164 if ( $this->mTitleObj
!== null ) {
165 return $this->mTitleObj
;
168 $params = $this->extractRequestParams();
170 $this->mTitleObj
= Title
::newFromText( $params['title'] );
172 if ( !$this->mTitleObj
) {
173 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
175 if ( !$this->mTitleObj
->exists() ) {
176 $this->dieUsageMsg( 'notanarticle' );
179 return $this->mTitleObj
;
182 public function getExamples() {
184 'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
185 'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&token=123ABC&summary=Reverting%20vandalism&markbot=1'
189 public function getHelpUrls() {
190 return 'https://www.mediawiki.org/wiki/API:Rollback';
193 public function getVersion() {
194 return __CLASS__
. ': $Id$';