Stop mutually exclusive values in ApiProtect
[lhc/web/wiklou.git] / includes / api / ApiRollback.php
1 <?php
2
3 /*
4 * Created on Jun 20, 2007
5 * API for MediaWiki 1.8+
6 *
7 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
8 *
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.
13 *
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.
18 *
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 */
24
25 if ( !defined( 'MEDIAWIKI' ) ) {
26 // Eclipse helper - will be ignored in production
27 require_once( "ApiBase.php" );
28 }
29
30 /**
31 * @ingroup API
32 */
33 class ApiRollback extends ApiBase {
34
35 public function __construct( $main, $action ) {
36 parent::__construct( $main, $action );
37 }
38
39 public function execute() {
40 $params = $this->extractRequestParams();
41
42 if ( isset( $params['watch'] ) && isset( $params['unwatch'] ) ) {
43 $this->dieUsageMsg( array( 'show' ) );
44 }
45
46 $titleObj = null;
47 if ( !isset( $params['title'] ) ) {
48 $this->dieUsageMsg( array( 'missingparam', 'title' ) );
49 }
50 if ( !isset( $params['user'] ) ) {
51 $this->dieUsageMsg( array( 'missingparam', 'user' ) );
52 }
53
54 $titleObj = Title::newFromText( $params['title'] );
55 if ( !$titleObj ) {
56 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
57 }
58 if ( !$titleObj->exists() ) {
59 $this->dieUsageMsg( array( 'notanarticle' ) );
60 }
61
62 // We need to be able to revert IPs, but getCanonicalName rejects them
63 $username = User::isIP( $params['user'] )
64 ? $params['user']
65 : User::getCanonicalName( $params['user'] );
66 if ( !$username ) {
67 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
68 }
69
70 $articleObj = new Article( $titleObj );
71 $summary = ( isset( $params['summary'] ) ? $params['summary'] : '' );
72 $details = null;
73 $retval = $articleObj->doRollback( $username, $summary, $params['token'], $params['markbot'], $details );
74
75 if ( $retval ) {
76 // We don't care about multiple errors, just report one of them
77 $this->dieUsageMsg( reset( $retval ) );
78 }
79
80 if ( $params['watch'] || $wgUser->getOption( 'watchdeletion' ) ) {
81 $articleObj->doWatch();
82 } elseif ( $params['unwatch'] ) {
83 $articleObj->doUnwatch();
84 }
85
86 $info = array(
87 'title' => $titleObj->getPrefixedText(),
88 'pageid' => intval( $details['current']->getPage() ),
89 'summary' => $details['summary'],
90 'revid' => intval( $details['newid'] ),
91 'old_revid' => intval( $details['current']->getID() ),
92 'last_revid' => intval( $details['target']->getID() )
93 );
94
95 $this->getResult()->addValue( null, $this->getModuleName(), $info );
96 }
97
98 public function mustBePosted() {
99 return true;
100 }
101
102 public function isWriteMode() {
103 return true;
104 }
105
106 public function getAllowedParams() {
107 return array(
108 'title' => null,
109 'user' => null,
110 'token' => null,
111 'summary' => null,
112 'markbot' => false,
113 'watch' => false,
114 'unwatch' => false,
115 );
116 }
117
118 public function getParamDescription() {
119 return array(
120 'title' => 'Title of the page you want to rollback.',
121 'user' => 'Name of the user whose edits are to be rolled back. If set incorrectly, you\'ll get a badtoken error.',
122 'token' => 'A rollback token previously retrieved through prop=revisions',
123 'summary' => 'Custom edit summary. If not set, default summary will be used.',
124 'markbot' => 'Mark the reverted edits and the revert as bot edits',
125 'watch' => 'Add the page to your watchlist',
126 'unwatch' => 'Remove the page from your watchlist',
127 );
128 }
129
130 public function getDescription() {
131 return array(
132 'Undo the last edit to the page. If the last user who edited the page made multiple edits in a row,',
133 'they will all be rolled back.'
134 );
135 }
136
137 public function getPossibleErrors() {
138 return array_merge( parent::getPossibleErrors(), array(
139 array( 'missingparam', 'title' ),
140 array( 'missingparam', 'user' ),
141 array( 'invalidtitle', 'title' ),
142 array( 'notanarticle' ),
143 array( 'invaliduser', 'user' ),
144 ) );
145 }
146
147 public function getTokenSalt() {
148 return '';
149 }
150
151 protected function getExamples() {
152 return array(
153 'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
154 'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&token=123ABC&summary=Reverting%20vandalism&markbot=1'
155 );
156 }
157
158 public function getVersion() {
159 return __CLASS__ . ': $Id$';
160 }
161 }