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 = $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
94 ApiBase
::PARAM_TYPE
=> 'string',
95 ApiBase
::PARAM_REQUIRED
=> true
100 ApiBase
::PARAM_DFLT
=> 'preferences',
101 ApiBase
::PARAM_TYPE
=> array(
111 public function getParamDescription() {
113 'title' => 'Title of the page you want to rollback.',
114 'user' => 'Name of the user whose edits are to be rolled back. If set incorrectly, you\'ll get a badtoken error.',
115 'token' => "A rollback token previously retrieved through {$this->getModulePrefix()}prop=revisions",
116 'summary' => 'Custom edit summary. If empty, default summary will be used',
117 'markbot' => 'Mark the reverted edits and the revert as bot edits',
118 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
122 public function getResultProperties() {
126 'pageid' => 'integer',
127 'summary' => 'string',
128 'revid' => 'integer',
129 'old_revid' => 'integer',
130 'last_revid' => 'integer'
135 public function getDescription() {
137 'Undo the last edit to the page. If the last user who edited the page made multiple edits in a row,',
138 'they will all be rolled back'
142 public function getPossibleErrors() {
143 return array_merge( parent
::getPossibleErrors(), array(
144 array( 'invalidtitle', 'title' ),
145 array( 'notanarticle' ),
146 array( 'invaliduser', 'user' ),
150 public function needsToken() {
154 public function getTokenSalt() {
155 return array( $this->getRbTitle()->getPrefixedText(), $this->getRbUser() );
158 private function getRbUser() {
159 if ( $this->mUser
!== null ) {
163 $params = $this->extractRequestParams();
165 // We need to be able to revert IPs, but getCanonicalName rejects them
166 $this->mUser
= User
::isIP( $params['user'] )
168 : User
::getCanonicalName( $params['user'] );
169 if ( !$this->mUser
) {
170 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
179 private function getRbTitle() {
180 if ( $this->mTitleObj
!== null ) {
181 return $this->mTitleObj
;
184 $params = $this->extractRequestParams();
186 $this->mTitleObj
= Title
::newFromText( $params['title'] );
188 if ( !$this->mTitleObj
) {
189 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
191 if ( !$this->mTitleObj
->exists() ) {
192 $this->dieUsageMsg( 'notanarticle' );
195 return $this->mTitleObj
;
198 public function getExamples() {
200 'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
201 'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&token=123ABC&summary=Reverting%20vandalism&markbot=1'
205 public function getHelpUrls() {
206 return 'https://www.mediawiki.org/wiki/API:Rollback';
209 public function getVersion() {
210 return __CLASS__
. ': $Id$';