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