2007-12-06 16:06:22 +00:00
|
|
|
<?php
|
2010-08-07 19:59:42 +00:00
|
|
|
/**
|
2023-03-16 17:27:37 +00:00
|
|
|
* Copyright © 2007 Roan Kattouw <roan.kattouw@gmail.com>
|
2007-12-06 16:06:22 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2010-06-21 13:13:32 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2007-12-06 16:06:22 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-07 19:59:42 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
2007-12-06 16:06:22 +00:00
|
|
|
*/
|
|
|
|
|
|
2022-04-13 15:28:26 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2021-03-26 22:56:39 +00:00
|
|
|
use MediaWiki\Page\RollbackPageFactory;
|
2019-08-21 19:53:53 +00:00
|
|
|
use MediaWiki\ParamValidator\TypeDef\UserDef;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2020-02-13 16:37:51 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2021-07-04 12:47:21 +00:00
|
|
|
use MediaWiki\User\UserOptionsLookup;
|
|
|
|
|
use MediaWiki\Watchlist\WatchlistManager;
|
2022-06-05 23:18:50 +00:00
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
2019-08-21 19:53:53 +00:00
|
|
|
|
2007-12-06 16:06:22 +00:00
|
|
|
/**
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup API
|
2007-12-06 16:06:22 +00:00
|
|
|
*/
|
|
|
|
|
class ApiRollback extends ApiBase {
|
|
|
|
|
|
2020-06-04 20:16:23 +00:00
|
|
|
use ApiWatchlistTrait;
|
|
|
|
|
|
2023-08-28 15:32:58 +00:00
|
|
|
private RollbackPageFactory $rollbackPageFactory;
|
2021-03-26 22:56:39 +00:00
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
ApiMain $mainModule,
|
|
|
|
|
$moduleName,
|
2021-07-04 12:47:21 +00:00
|
|
|
RollbackPageFactory $rollbackPageFactory,
|
|
|
|
|
WatchlistManager $watchlistManager,
|
|
|
|
|
UserOptionsLookup $userOptionsLookup
|
2021-03-26 22:56:39 +00:00
|
|
|
) {
|
|
|
|
|
parent::__construct( $mainModule, $moduleName );
|
2021-07-04 12:47:21 +00:00
|
|
|
$this->rollbackPageFactory = $rollbackPageFactory;
|
2020-06-04 20:16:23 +00:00
|
|
|
|
2021-07-04 12:47:21 +00:00
|
|
|
// Variables needed in ApiWatchlistTrait trait
|
2022-04-13 15:28:26 +00:00
|
|
|
$this->watchlistExpiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry );
|
|
|
|
|
$this->watchlistMaxDuration =
|
|
|
|
|
$this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration );
|
2021-07-04 12:47:21 +00:00
|
|
|
$this->watchlistManager = $watchlistManager;
|
|
|
|
|
$this->userOptionsLookup = $userOptionsLookup;
|
2020-06-04 20:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
2011-02-27 21:10:11 +00:00
|
|
|
/**
|
|
|
|
|
* @var Title
|
|
|
|
|
*/
|
|
|
|
|
private $mTitleObj = null;
|
|
|
|
|
|
|
|
|
|
/**
|
2020-02-13 16:37:51 +00:00
|
|
|
* @var UserIdentity
|
2011-02-27 21:10:11 +00:00
|
|
|
*/
|
|
|
|
|
private $mUser = null;
|
2007-12-06 16:06:22 +00:00
|
|
|
|
|
|
|
|
public function execute() {
|
2015-08-08 00:08:33 +00:00
|
|
|
$this->useTransactionalTimeLimit();
|
|
|
|
|
|
2014-08-08 16:56:07 +00:00
|
|
|
$user = $this->getUser();
|
2007-12-06 16:06:22 +00:00
|
|
|
$params = $this->extractRequestParams();
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2014-05-13 14:29:22 +00:00
|
|
|
$titleObj = $this->getRbTitle( $params );
|
2015-12-15 23:45:37 +00:00
|
|
|
|
|
|
|
|
// If change tagging was requested, check that the user is allowed to tag,
|
2021-03-26 22:56:39 +00:00
|
|
|
// and the tags are valid. TODO: move inside rollback command?
|
2017-12-04 18:36:48 +00:00
|
|
|
if ( $params['tags'] ) {
|
2021-02-23 20:45:00 +00:00
|
|
|
$tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
|
2015-12-15 23:45:37 +00:00
|
|
|
if ( !$tagStatus->isOK() ) {
|
|
|
|
|
$this->dieStatus( $tagStatus );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 09:48:14 +00:00
|
|
|
// @TODO: remove this hack once rollback uses POST (T88044)
|
2018-11-16 23:30:28 +00:00
|
|
|
$fname = __METHOD__;
|
2022-04-13 15:28:26 +00:00
|
|
|
$trxLimits = $this->getConfig()->get( MainConfigNames::TrxProfilerLimits );
|
2018-10-24 09:48:14 +00:00
|
|
|
$trxProfiler = Profiler::instance()->getTransactionProfiler();
|
2018-12-04 21:03:59 +00:00
|
|
|
$trxProfiler->redefineExpectations( $trxLimits['POST'], $fname );
|
2021-02-10 22:31:02 +00:00
|
|
|
DeferredUpdates::addCallableUpdate( static function () use ( $trxProfiler, $trxLimits, $fname ) {
|
2018-12-04 21:03:59 +00:00
|
|
|
$trxProfiler->redefineExpectations( $trxLimits['PostSend-POST'], $fname );
|
2018-11-19 19:56:49 +00:00
|
|
|
} );
|
2018-10-24 09:48:14 +00:00
|
|
|
|
2021-03-26 22:56:39 +00:00
|
|
|
$rollbackResult = $this->rollbackPageFactory
|
|
|
|
|
->newRollbackPage( $titleObj, $this->getAuthority(), $this->getRbUser( $params ) )
|
|
|
|
|
->setSummary( $params['summary'] )
|
|
|
|
|
->markAsBot( $params['markbot'] )
|
|
|
|
|
->setChangeTags( $params['tags'] )
|
|
|
|
|
->rollbackIfAllowed();
|
|
|
|
|
|
|
|
|
|
if ( !$rollbackResult->isGood() ) {
|
|
|
|
|
$this->dieStatus( $rollbackResult );
|
2010-02-26 13:18:56 +00:00
|
|
|
}
|
2010-04-10 06:11:02 +00:00
|
|
|
|
2018-10-20 21:55:44 +00:00
|
|
|
$watch = $params['watchlist'] ?? 'preferences';
|
2020-06-04 20:16:23 +00:00
|
|
|
$watchlistExpiry = $this->getExpiryFromParams( $params );
|
2014-07-15 12:32:16 +00:00
|
|
|
|
|
|
|
|
// Watch pages
|
2020-09-04 12:31:40 +00:00
|
|
|
$this->setWatch( $watch, $titleObj, $user, 'watchrollback', $watchlistExpiry );
|
2008-01-15 20:21:16 +00:00
|
|
|
|
2021-03-26 22:56:39 +00:00
|
|
|
$details = $rollbackResult->getValue();
|
2020-06-20 09:34:03 +00:00
|
|
|
$currentRevisionRecord = $details['current-revision-record'];
|
|
|
|
|
$targetRevisionRecord = $details['target-revision-record'];
|
2020-06-10 00:23:03 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$info = [
|
2010-04-10 13:33:24 +00:00
|
|
|
'title' => $titleObj->getPrefixedText(),
|
2020-06-10 00:23:03 +00:00
|
|
|
'pageid' => $currentRevisionRecord->getPageId(),
|
2008-12-17 16:34:01 +00:00
|
|
|
'summary' => $details['summary'],
|
2019-02-25 00:38:33 +00:00
|
|
|
'revid' => (int)$details['newid'],
|
2015-09-29 02:53:20 +00:00
|
|
|
// The revision being reverted (previously the current revision of the page)
|
2020-06-10 00:23:03 +00:00
|
|
|
'old_revid' => $currentRevisionRecord->getID(),
|
2015-09-29 02:53:20 +00:00
|
|
|
// The revision being restored (the last revision before revision(s) by the reverted user)
|
2020-06-10 00:23:03 +00:00
|
|
|
'last_revid' => $targetRevisionRecord->getID()
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2007-12-06 16:06:22 +00:00
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), $info );
|
2007-12-06 16:06:22 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-02-26 13:18:56 +00:00
|
|
|
public function mustBePosted() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2009-03-06 13:49:44 +00:00
|
|
|
public function isWriteMode() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getAllowedParams() {
|
2020-06-04 20:16:23 +00:00
|
|
|
$params = [
|
2014-05-13 11:42:10 +00:00
|
|
|
'title' => null,
|
2016-02-17 09:09:32 +00:00
|
|
|
'pageid' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'integer'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'tags' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'tags',
|
|
|
|
|
ParamValidator::PARAM_ISMULTI => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'user' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'user',
|
2019-08-21 19:53:53 +00:00
|
|
|
UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'id', 'interwiki' ],
|
|
|
|
|
UserDef::PARAM_RETURN_OBJECT => true,
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_REQUIRED => true
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2012-07-07 07:12:04 +00:00
|
|
|
'summary' => '',
|
2010-03-25 22:15:08 +00:00
|
|
|
'markbot' => false,
|
2020-06-04 20:16:23 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Params appear in the docs in the order they are defined,
|
|
|
|
|
// which is why this is here (we want it above the token param).
|
|
|
|
|
$params += $this->getWatchlistParams();
|
|
|
|
|
|
|
|
|
|
return $params + [
|
2016-02-17 09:09:32 +00:00
|
|
|
'token' => [
|
2014-09-18 17:38:23 +00:00
|
|
|
// Standard definition automatically inserted
|
2016-02-17 09:09:32 +00:00
|
|
|
ApiBase::PARAM_HELP_MSG_APPEND => [ 'api-help-param-token-webui' ],
|
|
|
|
|
],
|
|
|
|
|
];
|
2007-12-06 16:06:22 +00:00
|
|
|
}
|
2010-02-26 13:18:56 +00:00
|
|
|
|
2010-10-01 20:12:50 +00:00
|
|
|
public function needsToken() {
|
2014-08-08 16:56:07 +00:00
|
|
|
return 'rollback';
|
2010-10-01 20:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
2014-05-13 14:29:22 +00:00
|
|
|
/**
|
|
|
|
|
* @param array $params
|
|
|
|
|
*
|
2020-02-13 16:37:51 +00:00
|
|
|
* @return UserIdentity
|
2014-05-13 14:29:22 +00:00
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
private function getRbUser( array $params ): UserIdentity {
|
2010-04-10 13:33:24 +00:00
|
|
|
if ( $this->mUser !== null ) {
|
|
|
|
|
return $this->mUser;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 19:53:53 +00:00
|
|
|
$this->mUser = $params['user'];
|
2010-04-10 13:33:24 +00:00
|
|
|
|
|
|
|
|
return $this->mUser;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-28 00:04:48 +00:00
|
|
|
/**
|
2014-05-13 14:29:22 +00:00
|
|
|
* @param array $params
|
|
|
|
|
*
|
2010-10-28 00:04:48 +00:00
|
|
|
* @return Title
|
|
|
|
|
*/
|
2014-05-13 14:29:22 +00:00
|
|
|
private function getRbTitle( array $params ) {
|
2010-04-10 13:33:24 +00:00
|
|
|
if ( $this->mTitleObj !== null ) {
|
|
|
|
|
return $this->mTitleObj;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-13 14:29:22 +00:00
|
|
|
$this->requireOnlyOneParameter( $params, 'title', 'pageid' );
|
2010-04-10 13:33:24 +00:00
|
|
|
|
2014-05-13 11:42:10 +00:00
|
|
|
if ( isset( $params['title'] ) ) {
|
|
|
|
|
$this->mTitleObj = Title::newFromText( $params['title'] );
|
|
|
|
|
if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
|
2014-05-13 11:42:10 +00:00
|
|
|
}
|
|
|
|
|
} elseif ( isset( $params['pageid'] ) ) {
|
|
|
|
|
$this->mTitleObj = Title::newFromID( $params['pageid'] );
|
|
|
|
|
if ( !$this->mTitleObj ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( [ 'apierror-nosuchpageid', $params['pageid'] ] );
|
2014-05-13 11:42:10 +00:00
|
|
|
}
|
2010-04-09 20:05:33 +00:00
|
|
|
}
|
2014-05-13 11:42:10 +00:00
|
|
|
|
2010-04-09 20:05:33 +00:00
|
|
|
if ( !$this->mTitleObj->exists() ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( 'apierror-missingtitle' );
|
2010-04-09 20:05:33 +00:00
|
|
|
}
|
2010-07-23 07:33:40 +00:00
|
|
|
|
2010-04-10 13:33:24 +00:00
|
|
|
return $this->mTitleObj;
|
2010-02-14 22:20:27 +00:00
|
|
|
}
|
2007-12-06 16:06:22 +00:00
|
|
|
|
2014-10-28 17:17:02 +00:00
|
|
|
protected function getExamplesMessages() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2014-09-18 17:38:23 +00:00
|
|
|
'action=rollback&title=Main%20Page&user=Example&token=123ABC' =>
|
|
|
|
|
'apihelp-rollback-example-simple',
|
|
|
|
|
'action=rollback&title=Main%20Page&user=192.0.2.5&' .
|
|
|
|
|
'token=123ABC&summary=Reverting%20vandalism&markbot=1' =>
|
|
|
|
|
'apihelp-rollback-example-summary',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2007-12-06 16:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
2011-07-17 16:38:24 +00:00
|
|
|
public function getHelpUrls() {
|
2017-04-04 22:52:57 +00:00
|
|
|
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Rollback';
|
2011-07-17 16:38:24 +00:00
|
|
|
}
|
2007-12-06 16:06:22 +00:00
|
|
|
}
|