3 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
26 class ApiRollback
extends ApiBase
{
31 private $mTitleObj = null;
36 private $mUser = null;
38 public function execute() {
39 $this->useTransactionalTimeLimit();
41 $user = $this->getUser();
42 $params = $this->extractRequestParams();
44 $titleObj = $this->getRbTitle( $params );
45 $pageObj = WikiPage
::factory( $titleObj );
46 $summary = $params['summary'];
49 // If change tagging was requested, check that the user is allowed to tag,
50 // and the tags are valid
51 if ( $params['tags'] ) {
52 $tagStatus = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $user );
53 if ( !$tagStatus->isOK() ) {
54 $this->dieStatus( $tagStatus );
58 // @TODO: remove this hack once rollback uses POST (T88044)
59 $trxLimits = $this->getConfig()->get( 'TrxProfilerLimits' );
60 $trxProfiler = Profiler
::instance()->getTransactionProfiler();
61 $trxProfiler->setExpectations( $trxLimits['POST'], __METHOD__
);
63 $retval = $pageObj->doRollback(
64 $this->getRbUser( $params ),
74 $this->dieStatus( $this->errorArrayToStatus( $retval, $user ) );
77 $watch = $params['watchlist'] ??
'preferences';
80 $this->setWatch( $watch, $titleObj, 'watchrollback' );
83 'title' => $titleObj->getPrefixedText(),
84 'pageid' => intval( $details['current']->getPage() ),
85 'summary' => $details['summary'],
86 'revid' => intval( $details['newid'] ),
87 // The revision being reverted (previously the current revision of the page)
88 'old_revid' => intval( $details['current']->getID() ),
89 // The revision being restored (the last revision before revision(s) by the reverted user)
90 'last_revid' => intval( $details['target']->getID() )
93 $this->getResult()->addValue( null, $this->getModuleName(), $info );
96 public function mustBePosted() {
100 public function isWriteMode() {
104 public function getAllowedParams() {
108 ApiBase
::PARAM_TYPE
=> 'integer'
111 ApiBase
::PARAM_TYPE
=> 'tags',
112 ApiBase
::PARAM_ISMULTI
=> true,
115 ApiBase
::PARAM_TYPE
=> 'user',
116 ApiBase
::PARAM_REQUIRED
=> true
121 ApiBase
::PARAM_DFLT
=> 'preferences',
122 ApiBase
::PARAM_TYPE
=> [
130 // Standard definition automatically inserted
131 ApiBase
::PARAM_HELP_MSG_APPEND
=> [ 'api-help-param-token-webui' ],
136 public function needsToken() {
141 * @param array $params
145 private function getRbUser( array $params ) {
146 if ( $this->mUser
!== null ) {
150 // We need to be able to revert IPs, but getCanonicalName rejects them
151 $this->mUser
= User
::isIP( $params['user'] )
153 : User
::getCanonicalName( $params['user'] );
154 if ( !$this->mUser
) {
155 $this->dieWithError( [ 'apierror-invaliduser', wfEscapeWikiText( $params['user'] ) ] );
162 * @param array $params
166 private function getRbTitle( array $params ) {
167 if ( $this->mTitleObj
!== null ) {
168 return $this->mTitleObj
;
171 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
173 if ( isset( $params['title'] ) ) {
174 $this->mTitleObj
= Title
::newFromText( $params['title'] );
175 if ( !$this->mTitleObj ||
$this->mTitleObj
->isExternal() ) {
176 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
178 } elseif ( isset( $params['pageid'] ) ) {
179 $this->mTitleObj
= Title
::newFromID( $params['pageid'] );
180 if ( !$this->mTitleObj
) {
181 $this->dieWithError( [ 'apierror-nosuchpageid', $params['pageid'] ] );
185 if ( !$this->mTitleObj
->exists() ) {
186 $this->dieWithError( 'apierror-missingtitle' );
189 return $this->mTitleObj
;
192 protected function getExamplesMessages() {
194 'action=rollback&title=Main%20Page&user=Example&token=123ABC' =>
195 'apihelp-rollback-example-simple',
196 'action=rollback&title=Main%20Page&user=192.0.2.5&' .
197 'token=123ABC&summary=Reverting%20vandalism&markbot=1' =>
198 'apihelp-rollback-example-summary',
202 public function getHelpUrls() {
203 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Rollback';