Don't check namespace in SpecialWantedtemplates
[lhc/web/wiklou.git] / includes / api / ApiRollback.php
1 <?php
2 /**
3 *
4 *
5 * Created on Jun 20, 2007
6 *
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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 /**
28 * @ingroup API
29 */
30 class ApiRollback extends ApiBase {
31
32 /**
33 * @var Title
34 */
35 private $mTitleObj = null;
36
37 /**
38 * @var User
39 */
40 private $mUser = null;
41
42 public function execute() {
43 $user = $this->getUser();
44 $params = $this->extractRequestParams();
45
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 ),
52 $this->getRequest()
53 );
54 }
55
56 $titleObj = $this->getRbTitle( $params );
57 $pageObj = WikiPage::factory( $titleObj );
58 $summary = $params['summary'];
59 $details = array();
60 $retval = $pageObj->doRollback(
61 $this->getRbUser( $params ),
62 $summary,
63 $token,
64 $params['markbot'],
65 $details,
66 $user
67 );
68
69 if ( $retval ) {
70 // We don't care about multiple errors, just report one of them
71 $this->dieUsageMsg( reset( $retval ) );
72 }
73
74 $watch = 'preferences';
75 if ( isset( $params['watchlist'] ) ) {
76 $watch = $params['watchlist'];
77 }
78
79 // Watch pages
80 $this->setWatch( $watch, $titleObj, 'watchrollback' );
81
82 $info = array(
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() )
89 );
90
91 $this->getResult()->addValue( null, $this->getModuleName(), $info );
92 }
93
94 public function mustBePosted() {
95 return true;
96 }
97
98 public function isWriteMode() {
99 return true;
100 }
101
102 public function getAllowedParams() {
103 return array(
104 'title' => null,
105 'pageid' => array(
106 ApiBase::PARAM_TYPE => 'integer'
107 ),
108 'user' => array(
109 ApiBase::PARAM_TYPE => 'string',
110 ApiBase::PARAM_REQUIRED => true
111 ),
112 'summary' => '',
113 'markbot' => false,
114 'watchlist' => array(
115 ApiBase::PARAM_DFLT => 'preferences',
116 ApiBase::PARAM_TYPE => array(
117 'watch',
118 'unwatch',
119 'preferences',
120 'nochange'
121 ),
122 ),
123 'token' => array(
124 // Standard definition automatically inserted
125 ApiBase::PARAM_HELP_MSG_APPEND => array( 'api-help-param-token-webui' ),
126 ),
127 );
128 }
129
130 public function needsToken() {
131 return 'rollback';
132 }
133
134 protected function getWebUITokenSalt( array $params ) {
135 return array(
136 $this->getRbTitle( $params )->getPrefixedText(),
137 $this->getRbUser( $params )
138 );
139 }
140
141 /**
142 * @param array $params
143 *
144 * @return string
145 */
146 private function getRbUser( array $params ) {
147 if ( $this->mUser !== null ) {
148 return $this->mUser;
149 }
150
151 // We need to be able to revert IPs, but getCanonicalName rejects them
152 $this->mUser = User::isIP( $params['user'] )
153 ? $params['user']
154 : User::getCanonicalName( $params['user'] );
155 if ( !$this->mUser ) {
156 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
157 }
158
159 return $this->mUser;
160 }
161
162 /**
163 * @param array $params
164 *
165 * @return Title
166 */
167 private function getRbTitle( array $params ) {
168 if ( $this->mTitleObj !== null ) {
169 return $this->mTitleObj;
170 }
171
172 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
173
174 if ( isset( $params['title'] ) ) {
175 $this->mTitleObj = Title::newFromText( $params['title'] );
176 if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
177 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
178 }
179 } elseif ( isset( $params['pageid'] ) ) {
180 $this->mTitleObj = Title::newFromID( $params['pageid'] );
181 if ( !$this->mTitleObj ) {
182 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
183 }
184 }
185
186 if ( !$this->mTitleObj->exists() ) {
187 $this->dieUsageMsg( 'notanarticle' );
188 }
189
190 return $this->mTitleObj;
191 }
192
193 protected function getExamplesMessages() {
194 return array(
195 'action=rollback&title=Main%20Page&user=Example&token=123ABC' =>
196 'apihelp-rollback-example-simple',
197 'action=rollback&title=Main%20Page&user=192.0.2.5&' .
198 'token=123ABC&summary=Reverting%20vandalism&markbot=1' =>
199 'apihelp-rollback-example-summary',
200 );
201 }
202
203 public function getHelpUrls() {
204 return 'https://www.mediawiki.org/wiki/API:Rollback';
205 }
206 }