Fix bug 22944 in a much better fashion (using watchlist parameter)
[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 $titleObj = null;
43 if ( !isset( $params['title'] ) ) {
44 $this->dieUsageMsg( array( 'missingparam', 'title' ) );
45 }
46 if ( !isset( $params['user'] ) ) {
47 $this->dieUsageMsg( array( 'missingparam', 'user' ) );
48 }
49
50 $titleObj = Title::newFromText( $params['title'] );
51 if ( !$titleObj ) {
52 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
53 }
54 if ( !$titleObj->exists() ) {
55 $this->dieUsageMsg( array( 'notanarticle' ) );
56 }
57
58 // We need to be able to revert IPs, but getCanonicalName rejects them
59 $username = User::isIP( $params['user'] )
60 ? $params['user']
61 : User::getCanonicalName( $params['user'] );
62 if ( !$username ) {
63 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
64 }
65
66 $articleObj = new Article( $titleObj );
67 $summary = ( isset( $params['summary'] ) ? $params['summary'] : '' );
68 $details = null;
69 $retval = $articleObj->doRollback( $username, $summary, $params['token'], $params['markbot'], $details );
70
71 if ( $retval ) {
72 // We don't care about multiple errors, just report one of them
73 $this->dieUsageMsg( reset( $retval ) );
74 }
75
76 $watch = $this->getWatchlistValue( $params['watchlist'], $titleObj );
77
78 if ( $watch ) {
79 $articleObj->doWatch();
80 } else if ( !$watch ) {
81 $articleObj->doUnwatch();
82 }
83
84 $info = array(
85 'title' => $titleObj->getPrefixedText(),
86 'pageid' => intval( $details['current']->getPage() ),
87 'summary' => $details['summary'],
88 'revid' => intval( $details['newid'] ),
89 'old_revid' => intval( $details['current']->getID() ),
90 'last_revid' => intval( $details['target']->getID() )
91 );
92
93 $this->getResult()->addValue( null, $this->getModuleName(), $info );
94 }
95
96 public function mustBePosted() {
97 return true;
98 }
99
100 public function isWriteMode() {
101 return true;
102 }
103
104 public function getAllowedParams() {
105 return array(
106 'title' => null,
107 'user' => null,
108 'token' => null,
109 'summary' => null,
110 'markbot' => false,
111 'watchlist' => array(
112 ApiBase::PARAM_DFLT => 'preferences',
113 ApiBase::PARAM_TYPE => array(
114 'watch',
115 'unwatch',
116 'preferences',
117 'nochange'
118 ),
119 ),
120 );
121 }
122
123 public function getParamDescription() {
124 return array(
125 'title' => 'Title of the page you want to rollback.',
126 'user' => 'Name of the user whose edits are to be rolled back. If set incorrectly, you\'ll get a badtoken error.',
127 'token' => 'A rollback token previously retrieved through prop=revisions',
128 'summary' => 'Custom edit summary. If not set, default summary will be used.',
129 'markbot' => 'Mark the reverted edits and the revert as bot edits',
130 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
131 );
132 }
133
134 public function getDescription() {
135 return array(
136 'Undo the last edit to the page. If the last user who edited the page made multiple edits in a row,',
137 'they will all be rolled back.'
138 );
139 }
140
141 public function getPossibleErrors() {
142 return array_merge( parent::getPossibleErrors(), array(
143 array( 'missingparam', 'title' ),
144 array( 'missingparam', 'user' ),
145 array( 'invalidtitle', 'title' ),
146 array( 'notanarticle' ),
147 array( 'invaliduser', 'user' ),
148 ) );
149 }
150
151 public function getTokenSalt() {
152 return '';
153 }
154
155 protected function getExamples() {
156 return array(
157 'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
158 'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&token=123ABC&summary=Reverting%20vandalism&markbot=1'
159 );
160 }
161
162 public function getVersion() {
163 return __CLASS__ . ': $Id$';
164 }
165 }