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 $user = $this->getUser();
44 $params = $this->extractRequestParams();
46 // WikiPage::doRollback needs a Web UI token, so get one of those if we
47 // validated based on an API rollback token.
48 $token = $params['token'];
49 if ( $user->matchEditToken( $token, 'rollback', $this->getRequest() ) ) {
50 $token = $this->getUser()->getEditToken(
51 $this->getWebUITokenSalt( $params ),
56 $titleObj = $this->getRbTitle( $params );
57 $pageObj = WikiPage
::factory( $titleObj );
58 $summary = $params['summary'];
60 $retval = $pageObj->doRollback(
61 $this->getRbUser( $params ),
70 // We don't care about multiple errors, just report one of them
71 $this->dieUsageMsg( reset( $retval ) );
74 $watch = 'preferences';
75 if ( isset( $params['watchlist'] ) ) {
76 $watch = $params['watchlist'];
80 $this->setWatch( $watch, $titleObj, 'watchrollback' );
83 'title' => $titleObj->getPrefixedText(),
84 'pageid' => intval( $details['current']->getPage() ),
85 'summary' => $details['summary'],
86 'revid' => intval( $details['newid'] ),
87 'old_revid' => intval( $details['current']->getID() ),
88 'last_revid' => intval( $details['target']->getID() )
91 $this->getResult()->addValue( null, $this->getModuleName(), $info );
94 public function mustBePosted() {
98 public function isWriteMode() {
102 public function getAllowedParams() {
106 ApiBase
::PARAM_TYPE
=> 'integer'
109 ApiBase
::PARAM_TYPE
=> 'string',
110 ApiBase
::PARAM_REQUIRED
=> true
114 'watchlist' => array(
115 ApiBase
::PARAM_DFLT
=> 'preferences',
116 ApiBase
::PARAM_TYPE
=> array(
126 public function getParamDescription() {
127 $p = $this->getModulePrefix();
130 'title' => "Title of the page you want to roll back. Cannot be used together with {$p}pageid",
131 'pageid' => "Page ID of the page you want to roll back. Cannot be used together with {$p}title",
132 'user' => 'Name of the user whose edits are to be rolled back.',
134 /* Standard description automatically prepended */
135 'For compatibility, the token used in the web UI is also accepted.'
137 'summary' => 'Custom edit summary. If empty, default summary will be used',
138 'markbot' => 'Mark the reverted edits and the revert as bot edits',
139 'watchlist' => 'Unconditionally add or remove the page from your watchlist, ' .
140 'use preferences or do not change watch',
144 public function getDescription() {
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 needsToken() {
155 protected function getWebUITokenSalt( array $params ) {
157 $this->getRbTitle( $params )->getPrefixedText(),
158 $this->getRbUser( $params )
163 * @param array $params
167 private function getRbUser( array $params ) {
168 if ( $this->mUser
!== null ) {
172 // We need to be able to revert IPs, but getCanonicalName rejects them
173 $this->mUser
= User
::isIP( $params['user'] )
175 : User
::getCanonicalName( $params['user'] );
176 if ( !$this->mUser
) {
177 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
184 * @param array $params
188 private function getRbTitle( array $params ) {
189 if ( $this->mTitleObj
!== null ) {
190 return $this->mTitleObj
;
193 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
195 if ( isset( $params['title'] ) ) {
196 $this->mTitleObj
= Title
::newFromText( $params['title'] );
197 if ( !$this->mTitleObj ||
$this->mTitleObj
->isExternal() ) {
198 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
200 } elseif ( isset( $params['pageid'] ) ) {
201 $this->mTitleObj
= Title
::newFromID( $params['pageid'] );
202 if ( !$this->mTitleObj
) {
203 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
207 if ( !$this->mTitleObj
->exists() ) {
208 $this->dieUsageMsg( 'notanarticle' );
211 return $this->mTitleObj
;
214 public function getExamples() {
216 'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
217 'api.php?action=rollback&pageid=122&user=Catrope&token=123ABC',
218 'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&' .
219 'token=123ABC&summary=Reverting%20vandalism&markbot=1'
223 public function getHelpUrls() {
224 return 'https://www.mediawiki.org/wiki/API:Rollback';