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