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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
35 class ApiRollback
extends ApiBase
{
37 public function __construct( $main, $action ) {
38 parent
::__construct( $main, $action );
44 private $mTitleObj = null;
49 private $mUser = null;
51 public function execute() {
52 $params = $this->extractRequestParams();
54 // User and title already validated in call to getTokenSalt from Main
55 $titleObj = $this->getTitle();
56 $articleObj = new Article( $titleObj );
57 $summary = ( isset( $params['summary'] ) ?
$params['summary'] : '' );
59 $retval = $articleObj->doRollback( $this->getUser(), $summary, $params['token'], $params['markbot'], $details );
62 // We don't care about multiple errors, just report one of them
63 $this->dieUsageMsg( reset( $retval ) );
66 $this->setWatch( $params['watchlist'], $titleObj );
69 'title' => $titleObj->getPrefixedText(),
70 'pageid' => intval( $details['current']->getPage() ),
71 'summary' => $details['summary'],
72 'revid' => intval( $details['newid'] ),
73 'old_revid' => intval( $details['current']->getID() ),
74 'last_revid' => intval( $details['target']->getID() )
77 $this->getResult()->addValue( null, $this->getModuleName(), $info );
80 public function mustBePosted() {
84 public function isWriteMode() {
88 public function getAllowedParams() {
91 ApiBase
::PARAM_TYPE
=> 'string',
92 ApiBase
::PARAM_REQUIRED
=> true
95 ApiBase
::PARAM_TYPE
=> 'string',
96 ApiBase
::PARAM_REQUIRED
=> true
101 'watchlist' => array(
102 ApiBase
::PARAM_DFLT
=> 'preferences',
103 ApiBase
::PARAM_TYPE
=> array(
113 public function getParamDescription() {
115 'title' => 'Title of the page you want to rollback.',
116 'user' => 'Name of the user whose edits are to be rolled back. If set incorrectly, you\'ll get a badtoken error.',
117 'token' => "A rollback token previously retrieved through {$this->getModulePrefix()}prop=revisions",
118 'summary' => 'Custom edit summary. If not set, default summary will be used',
119 'markbot' => 'Mark the reverted edits and the revert as bot edits',
120 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
124 public function getDescription() {
126 'Undo the last edit to the page. If the last user who edited the page made multiple edits in a row,',
127 'they will all be rolled back'
131 public function getPossibleErrors() {
132 return array_merge( parent
::getPossibleErrors(), array(
133 array( 'invalidtitle', 'title' ),
134 array( 'notanarticle' ),
135 array( 'invaliduser', 'user' ),
139 public function needsToken() {
143 public function getTokenSalt() {
144 return array( $this->getTitle()->getPrefixedText(), $this->getUser() );
147 private function getUser() {
148 if ( $this->mUser
!== null ) {
152 $params = $this->extractRequestParams();
154 // We need to be able to revert IPs, but getCanonicalName rejects them
155 $this->mUser
= User
::isIP( $params['user'] )
157 : User
::getCanonicalName( $params['user'] );
158 if ( !$this->mUser
) {
159 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
168 private function getTitle() {
169 if ( $this->mTitleObj
!== null ) {
170 return $this->mTitleObj
;
173 $params = $this->extractRequestParams();
175 $this->mTitleObj
= Title
::newFromText( $params['title'] );
177 if ( !$this->mTitleObj
) {
178 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
180 if ( !$this->mTitleObj
->exists() ) {
181 $this->dieUsageMsg( array( 'notanarticle' ) );
184 return $this->mTitleObj
;
187 protected function getExamples() {
189 'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
190 'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&token=123ABC&summary=Reverting%20vandalism&markbot=1'
194 public function getVersion() {
195 return __CLASS__
. ': $Id$';