Merge "Added a --profiler option to all maintenance scripts."
[mediawiki.git] / includes / api / ApiRollback.php
blobb9873f49ce09c3daeb7fd5ce32e6a58cfa70158d
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 /**
28 * @ingroup API
30 class ApiRollback extends ApiBase {
32 /**
33 * @var Title
35 private $mTitleObj = null;
37 /**
38 * @var User
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'];
49 $details = array();
50 $retval = $pageObj->doRollback( $this->getRbUser(), $summary, $params['token'], $params['markbot'], $details, $this->getUser() );
52 if ( $retval ) {
53 // We don't care about multiple errors, just report one of them
54 $this->dieUsageMsg( reset( $retval ) );
57 $this->setWatch( $params['watchlist'], $titleObj );
59 $info = array(
60 'title' => $titleObj->getPrefixedText(),
61 'pageid' => intval( $details['current']->getPage() ),
62 'summary' => $details['summary'],
63 'revid' => intval( $details['newid'] ),
64 'old_revid' => intval( $details['current']->getID() ),
65 'last_revid' => intval( $details['target']->getID() )
68 $this->getResult()->addValue( null, $this->getModuleName(), $info );
71 public function mustBePosted() {
72 return true;
75 public function isWriteMode() {
76 return true;
79 public function getAllowedParams() {
80 return array(
81 'title' => array(
82 ApiBase::PARAM_TYPE => 'string',
83 ApiBase::PARAM_REQUIRED => true
85 'user' => array(
86 ApiBase::PARAM_TYPE => 'string',
87 ApiBase::PARAM_REQUIRED => true
89 'token' => array(
90 ApiBase::PARAM_TYPE => 'string',
91 ApiBase::PARAM_REQUIRED => true
93 'summary' => '',
94 'markbot' => false,
95 'watchlist' => array(
96 ApiBase::PARAM_DFLT => 'preferences',
97 ApiBase::PARAM_TYPE => array(
98 'watch',
99 'unwatch',
100 'preferences',
101 'nochange'
107 public function getParamDescription() {
108 return array(
109 'title' => 'Title of the page you want to rollback.',
110 'user' => 'Name of the user whose edits are to be rolled back. If set incorrectly, you\'ll get a badtoken error.',
111 'token' => "A rollback token previously retrieved through {$this->getModulePrefix()}prop=revisions",
112 'summary' => 'Custom edit summary. If empty, default summary will be used',
113 'markbot' => 'Mark the reverted edits and the revert as bot edits',
114 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
118 public function getResultProperties() {
119 return array(
120 '' => array(
121 'title' => 'string',
122 'pageid' => 'integer',
123 'summary' => 'string',
124 'revid' => 'integer',
125 'old_revid' => 'integer',
126 'last_revid' => 'integer'
131 public function getDescription() {
132 return array(
133 'Undo the last edit to the page. If the last user who edited the page made multiple edits in a row,',
134 'they will all be rolled back'
138 public function getPossibleErrors() {
139 return array_merge( parent::getPossibleErrors(), array(
140 array( 'invalidtitle', 'title' ),
141 array( 'notanarticle' ),
142 array( 'invaliduser', 'user' ),
143 ) );
146 public function needsToken() {
147 return true;
150 public function getTokenSalt() {
151 return array( $this->getRbTitle()->getPrefixedText(), $this->getRbUser() );
154 private function getRbUser() {
155 if ( $this->mUser !== null ) {
156 return $this->mUser;
159 $params = $this->extractRequestParams();
161 // We need to be able to revert IPs, but getCanonicalName rejects them
162 $this->mUser = User::isIP( $params['user'] )
163 ? $params['user']
164 : User::getCanonicalName( $params['user'] );
165 if ( !$this->mUser ) {
166 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
169 return $this->mUser;
173 * @return Title
175 private function getRbTitle() {
176 if ( $this->mTitleObj !== null ) {
177 return $this->mTitleObj;
180 $params = $this->extractRequestParams();
182 $this->mTitleObj = Title::newFromText( $params['title'] );
184 if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
185 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
187 if ( !$this->mTitleObj->exists() ) {
188 $this->dieUsageMsg( 'notanarticle' );
191 return $this->mTitleObj;
194 public function getExamples() {
195 return array(
196 'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
197 'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&token=123ABC&summary=Reverting%20vandalism&markbot=1'
201 public function getHelpUrls() {
202 return 'https://www.mediawiki.org/wiki/API:Rollback';