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 $params = $this->extractRequestParams();
45 // User and title already validated in call to getTokenSalt from Main
46 $titleObj = $this->getRbTitle();
47 $pageObj = WikiPage
::factory( $titleObj );
48 $summary = $params['summary'];
50 $retval = $pageObj->doRollback(
60 // We don't care about multiple errors, just report one of them
61 $this->dieUsageMsg( reset( $retval ) );
64 $this->setWatch( $params['watchlist'], $titleObj );
67 'title' => $titleObj->getPrefixedText(),
68 'pageid' => intval( $details['current']->getPage() ),
69 'summary' => $details['summary'],
70 'revid' => intval( $details['newid'] ),
71 'old_revid' => intval( $details['current']->getID() ),
72 'last_revid' => intval( $details['target']->getID() )
75 $this->getResult()->addValue( null, $this->getModuleName(), $info );
78 public function mustBePosted() {
82 public function isWriteMode() {
86 public function getAllowedParams() {
89 ApiBase
::PARAM_TYPE
=> 'string',
90 ApiBase
::PARAM_REQUIRED
=> true
93 ApiBase
::PARAM_TYPE
=> 'string',
94 ApiBase
::PARAM_REQUIRED
=> true
97 ApiBase
::PARAM_TYPE
=> 'string',
98 ApiBase
::PARAM_REQUIRED
=> true
102 'watchlist' => array(
103 ApiBase
::PARAM_DFLT
=> 'preferences',
104 ApiBase
::PARAM_TYPE
=> array(
114 public function getParamDescription() {
116 'title' => 'Title of the page you want to rollback.',
117 'user' => 'Name of the user whose edits are to be rolled back. If ' .
118 'set incorrectly, you\'ll get a badtoken error.',
119 'token' => 'A rollback token previously retrieved through ' .
120 "{$this->getModulePrefix()}prop=revisions",
121 'summary' => 'Custom edit summary. If empty, default summary will be used',
122 'markbot' => 'Mark the reverted edits and the revert as bot edits',
123 'watchlist' => 'Unconditionally add or remove the page from your watchlist, ' .
124 'use preferences or do not change watch',
128 public function getResultProperties() {
132 'pageid' => 'integer',
133 'summary' => 'string',
134 'revid' => 'integer',
135 'old_revid' => 'integer',
136 'last_revid' => 'integer'
141 public function getDescription() {
143 'Undo the last edit to the page. If the last user who edited the page made',
144 'multiple edits in a row, they will all be rolled back.'
148 public function getPossibleErrors() {
149 return array_merge( parent
::getPossibleErrors(), array(
150 array( 'invalidtitle', 'title' ),
151 array( 'notanarticle' ),
152 array( 'invaliduser', 'user' ),
156 public function needsToken() {
160 public function getTokenSalt() {
161 return array( $this->getRbTitle()->getPrefixedText(), $this->getRbUser() );
164 private function getRbUser() {
165 if ( $this->mUser
!== null ) {
169 $params = $this->extractRequestParams();
171 // We need to be able to revert IPs, but getCanonicalName rejects them
172 $this->mUser
= User
::isIP( $params['user'] )
174 : User
::getCanonicalName( $params['user'] );
175 if ( !$this->mUser
) {
176 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
185 private function getRbTitle() {
186 if ( $this->mTitleObj
!== null ) {
187 return $this->mTitleObj
;
190 $params = $this->extractRequestParams();
192 $this->mTitleObj
= Title
::newFromText( $params['title'] );
194 if ( !$this->mTitleObj ||
$this->mTitleObj
->isExternal() ) {
195 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
197 if ( !$this->mTitleObj
->exists() ) {
198 $this->dieUsageMsg( 'notanarticle' );
201 return $this->mTitleObj
;
204 public function getExamples() {
206 'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
207 'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&' .
208 'token=123ABC&summary=Reverting%20vandalism&markbot=1'
212 public function getHelpUrls() {
213 return 'https://www.mediawiki.org/wiki/API:Rollback';