| 1 |
<?php |
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
use ChangeTags; |
| 26 |
use MediaWiki\Deferred\DeferredUpdates; |
| 27 |
use MediaWiki\MainConfigNames; |
| 28 |
use MediaWiki\Page\RollbackPageFactory; |
| 29 |
use MediaWiki\ParamValidator\TypeDef\UserDef; |
| 30 |
use MediaWiki\Title\Title; |
| 31 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 32 |
use MediaWiki\User\UserIdentity; |
| 33 |
use MediaWiki\Watchlist\WatchlistManager; |
| 34 |
use Profiler; |
| 35 |
use Wikimedia\ParamValidator\ParamValidator; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class ApiRollback extends ApiBase { |
| 41 |
|
| 42 |
use ApiWatchlistTrait; |
| 43 |
|
| 44 |
private RollbackPageFactory $rollbackPageFactory; |
| 45 |
|
| 46 |
public function __construct( |
| 47 |
ApiMain $mainModule, |
| 48 |
string $moduleName, |
| 49 |
RollbackPageFactory $rollbackPageFactory, |
| 50 |
WatchlistManager $watchlistManager, |
| 51 |
UserOptionsLookup $userOptionsLookup |
| 52 |
) { |
| 53 |
parent::__construct( $mainModule, $moduleName ); |
| 54 |
$this->rollbackPageFactory = $rollbackPageFactory; |
| 55 |
|
| 56 |
|
| 57 |
$this->watchlistExpiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry ); |
| 58 |
$this->watchlistMaxDuration = |
| 59 |
$this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration ); |
| 60 |
$this->watchlistManager = $watchlistManager; |
| 61 |
$this->userOptionsLookup = $userOptionsLookup; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
private $mTitleObj = null; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private $mUser = null; |
| 73 |
|
| 74 |
public function execute() { |
| 75 |
$this->useTransactionalTimeLimit(); |
| 76 |
|
| 77 |
$user = $this->getUser(); |
| 78 |
$params = $this->extractRequestParams(); |
| 79 |
|
| 80 |
$titleObj = $this->getRbTitle( $params ); |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
if ( $params['tags'] ) { |
| 85 |
$tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() ); |
| 86 |
if ( !$tagStatus->isOK() ) { |
| 87 |
$this->dieStatus( $tagStatus ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
$fname = __METHOD__; |
| 93 |
$trxLimits = $this->getConfig()->get( MainConfigNames::TrxProfilerLimits ); |
| 94 |
$trxProfiler = Profiler::instance()->getTransactionProfiler(); |
| 95 |
$trxProfiler->redefineExpectations( $trxLimits['POST'], $fname ); |
| 96 |
DeferredUpdates::addCallableUpdate( static function () use ( $trxProfiler, $trxLimits, $fname ) { |
| 97 |
$trxProfiler->redefineExpectations( $trxLimits['PostSend-POST'], $fname ); |
| 98 |
} ); |
| 99 |
|
| 100 |
$rollbackResult = $this->rollbackPageFactory |
| 101 |
->newRollbackPage( $titleObj, $this->getAuthority(), $this->getRbUser( $params ) ) |
| 102 |
->setSummary( $params['summary'] ) |
| 103 |
->markAsBot( $params['markbot'] ) |
| 104 |
->setChangeTags( $params['tags'] ) |
| 105 |
->rollbackIfAllowed(); |
| 106 |
|
| 107 |
if ( !$rollbackResult->isGood() ) { |
| 108 |
$this->dieStatus( $rollbackResult ); |
| 109 |
} |
| 110 |
|
| 111 |
$watch = $params['watchlist'] ?? 'preferences'; |
| 112 |
$watchlistExpiry = $this->getExpiryFromParams( $params ); |
| 113 |
|
| 114 |
|
| 115 |
$this->setWatch( $watch, $titleObj, $user, 'watchrollback', $watchlistExpiry ); |
| 116 |
|
| 117 |
$details = $rollbackResult->getValue(); |
| 118 |
$currentRevisionRecord = $details['current-revision-record']; |
| 119 |
$targetRevisionRecord = $details['target-revision-record']; |
| 120 |
|
| 121 |
$info = [ |
| 122 |
'title' => $titleObj->getPrefixedText(), |
| 123 |
'pageid' => $currentRevisionRecord->getPageId(), |
| 124 |
'summary' => $details['summary'], |
| 125 |
'revid' => (int)$details['newid'], |
| 126 |
|
| 127 |
'old_revid' => $currentRevisionRecord->getID(), |
| 128 |
|
| 129 |
'last_revid' => $targetRevisionRecord->getID() |
| 130 |
]; |
| 131 |
|
| 132 |
$this->getResult()->addValue( null, $this->getModuleName(), $info ); |
| 133 |
} |
| 134 |
|
| 135 |
public function mustBePosted() { |
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
public function isWriteMode() { |
| 140 |
return true; |
| 141 |
} |
| 142 |
|
| 143 |
public function getAllowedParams() { |
| 144 |
$params = [ |
| 145 |
'title' => null, |
| 146 |
'pageid' => [ |
| 147 |
ParamValidator::PARAM_TYPE => 'integer' |
| 148 |
], |
| 149 |
'tags' => [ |
| 150 |
ParamValidator::PARAM_TYPE => 'tags', |
| 151 |
ParamValidator::PARAM_ISMULTI => true, |
| 152 |
], |
| 153 |
'user' => [ |
| 154 |
ParamValidator::PARAM_TYPE => 'user', |
| 155 |
UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'temp', 'id', 'interwiki' ], |
| 156 |
UserDef::PARAM_RETURN_OBJECT => true, |
| 157 |
ParamValidator::PARAM_REQUIRED => true |
| 158 |
], |
| 159 |
'summary' => '', |
| 160 |
'markbot' => false, |
| 161 |
]; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
$params += $this->getWatchlistParams(); |
| 166 |
|
| 167 |
return $params + [ |
| 168 |
'token' => [ |
| 169 |
|
| 170 |
ApiBase::PARAM_HELP_MSG_APPEND => [ 'api-help-param-token-webui' ], |
| 171 |
], |
| 172 |
]; |
| 173 |
} |
| 174 |
|
| 175 |
public function needsToken() { |
| 176 |
return 'rollback'; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
private function getRbUser( array $params ): UserIdentity { |
| 185 |
if ( $this->mUser !== null ) { |
| 186 |
return $this->mUser; |
| 187 |
} |
| 188 |
|
| 189 |
$this->mUser = $params['user']; |
| 190 |
|
| 191 |
return $this->mUser; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
private function getRbTitle( array $params ) { |
| 200 |
if ( $this->mTitleObj !== null ) { |
| 201 |
return $this->mTitleObj; |
| 202 |
} |
| 203 |
|
| 204 |
$this->requireOnlyOneParameter( $params, 'title', 'pageid' ); |
| 205 |
|
| 206 |
if ( isset( $params['title'] ) ) { |
| 207 |
$this->mTitleObj = Title::newFromText( $params['title'] ); |
| 208 |
if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) { |
| 209 |
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] ); |
| 210 |
} |
| 211 |
} elseif ( isset( $params['pageid'] ) ) { |
| 212 |
$this->mTitleObj = Title::newFromID( $params['pageid'] ); |
| 213 |
if ( !$this->mTitleObj ) { |
| 214 |
$this->dieWithError( [ 'apierror-nosuchpageid', $params['pageid'] ] ); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
if ( !$this->mTitleObj->exists() ) { |
| 219 |
$this->dieWithError( 'apierror-missingtitle' ); |
| 220 |
} |
| 221 |
|
| 222 |
return $this->mTitleObj; |
| 223 |
} |
| 224 |
|
| 225 |
protected function getExamplesMessages() { |
| 226 |
$title = Title::newMainPage()->getPrefixedText(); |
| 227 |
$mp = rawurlencode( $title ); |
| 228 |
|
| 229 |
return [ |
| 230 |
"action=rollback&title={$mp}&user=Example&token=123ABC" => |
| 231 |
'apihelp-rollback-example-simple', |
| 232 |
"action=rollback&title={$mp}&user=192.0.2.5&" . |
| 233 |
'token=123ABC&summary=Reverting%20vandalism&markbot=1' => |
| 234 |
'apihelp-rollback-example-summary', |
| 235 |
]; |
| 236 |
} |
| 237 |
|
| 238 |
public function getHelpUrls() { |
| 239 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Rollback'; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
class_alias( ApiRollback::class, 'ApiRollback' ); |