Merge "Make sure images that don't have an explicit alignment get aligned right"
[mediawiki.git] / includes / api / ApiRollback.php
blobd0ce6d7dbf03d5f1a80a7158dc8a9c0906e26825
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( $params );
47 $pageObj = WikiPage::factory( $titleObj );
48 $summary = $params['summary'];
49 $details = array();
50 $retval = $pageObj->doRollback(
51 $this->getRbUser( $params ),
52 $summary,
53 $params['token'],
54 $params['markbot'],
55 $details,
56 $this->getUser()
59 if ( $retval ) {
60 // We don't care about multiple errors, just report one of them
61 $this->dieUsageMsg( reset( $retval ) );
64 $this->setWatch( $params['watchlist'], $titleObj );
66 $info = array(
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() {
79 return true;
82 public function isWriteMode() {
83 return true;
86 public function getAllowedParams() {
87 return array(
88 'title' => null,
89 'pageid' => array(
90 ApiBase::PARAM_TYPE => 'integer'
92 'user' => array(
93 ApiBase::PARAM_TYPE => 'string',
94 ApiBase::PARAM_REQUIRED => true
96 'token' => array(
97 ApiBase::PARAM_TYPE => 'string',
98 ApiBase::PARAM_REQUIRED => true
100 'summary' => '',
101 'markbot' => false,
102 'watchlist' => array(
103 ApiBase::PARAM_DFLT => 'preferences',
104 ApiBase::PARAM_TYPE => array(
105 'watch',
106 'unwatch',
107 'preferences',
108 'nochange'
114 public function getParamDescription() {
115 $p = $this->getModulePrefix();
117 return array(
118 'title' => "Title of the page you want to delete. Cannot be used together with {$p}pageid",
119 'pageid' => "Page ID of the page you want to delete. Cannot be used together with {$p}title",
120 'user' => 'Name of the user whose edits are to be rolled back. If ' .
121 'set incorrectly, you\'ll get a badtoken error.',
122 'token' => 'A rollback token previously retrieved through ' .
123 "{$this->getModulePrefix()}prop=revisions",
124 'summary' => 'Custom edit summary. If empty, default summary will be used',
125 'markbot' => 'Mark the reverted edits and the revert as bot edits',
126 'watchlist' => 'Unconditionally add or remove the page from your watchlist, ' .
127 'use preferences or do not change watch',
131 public function getResultProperties() {
132 return array(
133 '' => array(
134 'title' => 'string',
135 'pageid' => 'integer',
136 'summary' => 'string',
137 'revid' => 'integer',
138 'old_revid' => 'integer',
139 'last_revid' => 'integer'
144 public function getDescription() {
145 return array(
146 'Undo the last edit to the page. If the last user who edited the page made',
147 'multiple edits in a row, they will all be rolled back.'
151 public function getPossibleErrors() {
152 return array_merge(
153 parent::getPossibleErrors(),
154 $this->getRequireOnlyOneParameterErrorMessages( array( 'title', 'pageid' ) ),
155 array(
156 array( 'invalidtitle', 'title' ),
157 array( 'notanarticle' ),
158 array( 'nosuchpageid', 'pageid' ),
159 array( 'invaliduser', 'user' ),
164 public function needsToken() {
165 return true;
168 public function getTokenSalt() {
169 $params = $this->extractRequestParams();
171 return array(
172 $this->getRbTitle( $params )->getPrefixedText(),
173 $this->getRbUser( $params )
178 * @param array $params
180 * @return string
182 private function getRbUser( array $params ) {
183 if ( $this->mUser !== null ) {
184 return $this->mUser;
187 // We need to be able to revert IPs, but getCanonicalName rejects them
188 $this->mUser = User::isIP( $params['user'] )
189 ? $params['user']
190 : User::getCanonicalName( $params['user'] );
191 if ( !$this->mUser ) {
192 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
195 return $this->mUser;
199 * @param array $params
201 * @return Title
203 private function getRbTitle( array $params ) {
204 if ( $this->mTitleObj !== null ) {
205 return $this->mTitleObj;
208 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
210 if ( isset( $params['title'] ) ) {
211 $this->mTitleObj = Title::newFromText( $params['title'] );
212 if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
213 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
215 } elseif ( isset( $params['pageid'] ) ) {
216 $this->mTitleObj = Title::newFromID( $params['pageid'] );
217 if ( !$this->mTitleObj ) {
218 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
222 if ( !$this->mTitleObj->exists() ) {
223 $this->dieUsageMsg( 'notanarticle' );
226 return $this->mTitleObj;
229 public function getExamples() {
230 return array(
231 'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
232 'api.php?action=rollback&pageid=122&user=Catrope&token=123ABC',
233 'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&' .
234 'token=123ABC&summary=Reverting%20vandalism&markbot=1'
238 public function getHelpUrls() {
239 return 'https://www.mediawiki.org/wiki/API:Rollback';