Make this private since nothing outside here calls it
[mediawiki.git] / includes / api / ApiRollback.php
blobfee9622baf8b7e5830a06952a2d225a389a107dd
1 <?php
2 /**
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
24 * @file
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
32 /**
33 * @ingroup API
35 class ApiRollback extends ApiBase {
37 public function __construct( $main, $action ) {
38 parent::__construct( $main, $action );
41 /**
42 * @var Title
44 private $mTitleObj = null;
46 /**
47 * @var User
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'] : '' );
58 $details = array();
59 $retval = $articleObj->doRollback( $this->getUser(), $summary, $params['token'], $params['markbot'], $details );
61 if ( $retval ) {
62 // We don't care about multiple errors, just report one of them
63 $this->dieUsageMsg( reset( $retval ) );
66 $this->setWatch( $params['watchlist'], $titleObj );
68 $info = array(
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() {
81 return true;
84 public function isWriteMode() {
85 return true;
88 public function getAllowedParams() {
89 return array(
90 'title' => array(
91 ApiBase::PARAM_TYPE => 'string',
92 ApiBase::PARAM_REQUIRED => true
94 'user' => array(
95 ApiBase::PARAM_TYPE => 'string',
96 ApiBase::PARAM_REQUIRED => true
98 'token' => null,
99 'summary' => null,
100 'markbot' => false,
101 'watchlist' => array(
102 ApiBase::PARAM_DFLT => 'preferences',
103 ApiBase::PARAM_TYPE => array(
104 'watch',
105 'unwatch',
106 'preferences',
107 'nochange'
113 public function getParamDescription() {
114 return array(
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() {
125 return array(
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' ),
136 ) );
139 public function needsToken() {
140 return true;
143 public function getTokenSalt() {
144 return array( $this->getTitle()->getPrefixedText(), $this->getUser() );
147 private function getUser() {
148 if ( $this->mUser !== null ) {
149 return $this->mUser;
152 $params = $this->extractRequestParams();
154 // We need to be able to revert IPs, but getCanonicalName rejects them
155 $this->mUser = User::isIP( $params['user'] )
156 ? $params['user']
157 : User::getCanonicalName( $params['user'] );
158 if ( !$this->mUser ) {
159 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
162 return $this->mUser;
166 * @return Title
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() {
188 return array(
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$';