Add logic for "tags" in ApiBase
[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 $this->useTransactionalTimeLimit();
44
45 $user = $this->getUser();
46 $params = $this->extractRequestParams();
47
48 // WikiPage::doRollback needs a Web UI token, so get one of those if we
49 // validated based on an API rollback token.
50 $token = $params['token'];
51 if ( $user->matchEditToken( $token, 'rollback', $this->getRequest() ) ) {
52 $token = $this->getUser()->getEditToken(
53 $this->getWebUITokenSalt( $params ),
54 $this->getRequest()
55 );
56 }
57
58 $titleObj = $this->getRbTitle( $params );
59 $pageObj = WikiPage::factory( $titleObj );
60 $summary = $params['summary'];
61 $details = array();
62
63 // If change tagging was requested, check that the user is allowed to tag,
64 // and the tags are valid
65 if ( count( $params['tags'] ) ) {
66 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
67 if ( !$tagStatus->isOK() ) {
68 $this->dieStatus( $tagStatus );
69 }
70 }
71
72 $retval = $pageObj->doRollback(
73 $this->getRbUser( $params ),
74 $summary,
75 $token,
76 $params['markbot'],
77 $details,
78 $user
79 );
80
81 if ( $retval ) {
82 // We don't care about multiple errors, just report one of them
83 $this->dieUsageMsg( reset( $retval ) );
84 }
85
86 $watch = 'preferences';
87 if ( isset( $params['watchlist'] ) ) {
88 $watch = $params['watchlist'];
89 }
90
91 // Watch pages
92 $this->setWatch( $watch, $titleObj, 'watchrollback' );
93
94 if ( count( $params['tags'] ) ) {
95 ChangeTags::addTags( $params['tags'], null, intval( $details['newid'] ), null, null );
96 }
97
98 $info = array(
99 'title' => $titleObj->getPrefixedText(),
100 'pageid' => intval( $details['current']->getPage() ),
101 'summary' => $details['summary'],
102 'revid' => intval( $details['newid'] ),
103 'old_revid' => intval( $details['current']->getID() ),
104 'last_revid' => intval( $details['target']->getID() )
105 );
106
107 $this->getResult()->addValue( null, $this->getModuleName(), $info );
108 }
109
110 public function mustBePosted() {
111 return true;
112 }
113
114 public function isWriteMode() {
115 return true;
116 }
117
118 public function getAllowedParams() {
119 return array(
120 'title' => null,
121 'pageid' => array(
122 ApiBase::PARAM_TYPE => 'integer'
123 ),
124 'tags' => array(
125 ApiBase::PARAM_TYPE => 'tags',
126 ApiBase::PARAM_ISMULTI => true,
127 ),
128 'user' => array(
129 ApiBase::PARAM_TYPE => 'user',
130 ApiBase::PARAM_REQUIRED => true
131 ),
132 'summary' => '',
133 'markbot' => false,
134 'watchlist' => array(
135 ApiBase::PARAM_DFLT => 'preferences',
136 ApiBase::PARAM_TYPE => array(
137 'watch',
138 'unwatch',
139 'preferences',
140 'nochange'
141 ),
142 ),
143 'token' => array(
144 // Standard definition automatically inserted
145 ApiBase::PARAM_HELP_MSG_APPEND => array( 'api-help-param-token-webui' ),
146 ),
147 );
148 }
149
150 public function needsToken() {
151 return 'rollback';
152 }
153
154 protected function getWebUITokenSalt( array $params ) {
155 return array(
156 $this->getRbTitle( $params )->getPrefixedText(),
157 $this->getRbUser( $params )
158 );
159 }
160
161 /**
162 * @param array $params
163 *
164 * @return string
165 */
166 private function getRbUser( array $params ) {
167 if ( $this->mUser !== null ) {
168 return $this->mUser;
169 }
170
171 // We need to be able to revert IPs, but getCanonicalName rejects them
172 $this->mUser = User::isIP( $params['user'] )
173 ? $params['user']
174 : User::getCanonicalName( $params['user'] );
175 if ( !$this->mUser ) {
176 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
177 }
178
179 return $this->mUser;
180 }
181
182 /**
183 * @param array $params
184 *
185 * @return Title
186 */
187 private function getRbTitle( array $params ) {
188 if ( $this->mTitleObj !== null ) {
189 return $this->mTitleObj;
190 }
191
192 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
193
194 if ( isset( $params['title'] ) ) {
195 $this->mTitleObj = Title::newFromText( $params['title'] );
196 if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
197 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
198 }
199 } elseif ( isset( $params['pageid'] ) ) {
200 $this->mTitleObj = Title::newFromID( $params['pageid'] );
201 if ( !$this->mTitleObj ) {
202 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
203 }
204 }
205
206 if ( !$this->mTitleObj->exists() ) {
207 $this->dieUsageMsg( 'notanarticle' );
208 }
209
210 return $this->mTitleObj;
211 }
212
213 protected function getExamplesMessages() {
214 return array(
215 'action=rollback&title=Main%20Page&user=Example&token=123ABC' =>
216 'apihelp-rollback-example-simple',
217 'action=rollback&title=Main%20Page&user=192.0.2.5&' .
218 'token=123ABC&summary=Reverting%20vandalism&markbot=1' =>
219 'apihelp-rollback-example-summary',
220 );
221 }
222
223 public function getHelpUrls() {
224 return 'https://www.mediawiki.org/wiki/API:Rollback';
225 }
226 }