MediaWiki  master
ApiRollback.php
Go to the documentation of this file.
1 <?php
25 
29 class ApiRollback extends ApiBase {
30 
34  private $mTitleObj = null;
35 
39  private $mUser = null;
40 
41  public function execute() {
43 
44  $user = $this->getUser();
45  $params = $this->extractRequestParams();
46 
47  $titleObj = $this->getRbTitle( $params );
48  $pageObj = WikiPage::factory( $titleObj );
49  $summary = $params['summary'];
50  $details = [];
51 
52  // If change tagging was requested, check that the user is allowed to tag,
53  // and the tags are valid
54  if ( $params['tags'] ) {
55  $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
56  if ( !$tagStatus->isOK() ) {
57  $this->dieStatus( $tagStatus );
58  }
59  }
60 
61  // @TODO: remove this hack once rollback uses POST (T88044)
62  $fname = __METHOD__;
63  $trxLimits = $this->getConfig()->get( 'TrxProfilerLimits' );
64  $trxProfiler = Profiler::instance()->getTransactionProfiler();
65  $trxProfiler->redefineExpectations( $trxLimits['POST'], $fname );
66  DeferredUpdates::addCallableUpdate( function () use ( $trxProfiler, $trxLimits, $fname ) {
67  $trxProfiler->redefineExpectations( $trxLimits['PostSend-POST'], $fname );
68  } );
69 
70  $retval = $pageObj->doRollback(
71  $this->getRbUser( $params )->getName(),
72  $summary,
73  $params['token'],
74  $params['markbot'],
75  $details,
76  $user,
77  $params['tags']
78  );
79 
80  if ( $retval ) {
81  $this->dieStatus( $this->errorArrayToStatus( $retval, $user ) );
82  }
83 
84  $watch = $params['watchlist'] ?? 'preferences';
85 
86  // Watch pages
87  $this->setWatch( $watch, $titleObj, 'watchrollback' );
88 
89  $info = [
90  'title' => $titleObj->getPrefixedText(),
91  'pageid' => (int)$details['current']->getPage(),
92  'summary' => $details['summary'],
93  'revid' => (int)$details['newid'],
94  // The revision being reverted (previously the current revision of the page)
95  'old_revid' => (int)$details['current']->getID(),
96  // The revision being restored (the last revision before revision(s) by the reverted user)
97  'last_revid' => (int)$details['target']->getID()
98  ];
99 
100  $this->getResult()->addValue( null, $this->getModuleName(), $info );
101  }
102 
103  public function mustBePosted() {
104  return true;
105  }
106 
107  public function isWriteMode() {
108  return true;
109  }
110 
111  public function getAllowedParams() {
112  return [
113  'title' => null,
114  'pageid' => [
115  ApiBase::PARAM_TYPE => 'integer'
116  ],
117  'tags' => [
118  ApiBase::PARAM_TYPE => 'tags',
119  ApiBase::PARAM_ISMULTI => true,
120  ],
121  'user' => [
122  ApiBase::PARAM_TYPE => 'user',
123  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'id', 'interwiki' ],
124  UserDef::PARAM_RETURN_OBJECT => true,
126  ],
127  'summary' => '',
128  'markbot' => false,
129  'watchlist' => [
130  ApiBase::PARAM_DFLT => 'preferences',
132  'watch',
133  'unwatch',
134  'preferences',
135  'nochange'
136  ],
137  ],
138  'token' => [
139  // Standard definition automatically inserted
140  ApiBase::PARAM_HELP_MSG_APPEND => [ 'api-help-param-token-webui' ],
141  ],
142  ];
143  }
144 
145  public function needsToken() {
146  return 'rollback';
147  }
148 
154  private function getRbUser( array $params ) : UserIdentity {
155  if ( $this->mUser !== null ) {
156  return $this->mUser;
157  }
158 
159  $this->mUser = $params['user'];
160 
161  return $this->mUser;
162  }
163 
169  private function getRbTitle( array $params ) {
170  if ( $this->mTitleObj !== null ) {
171  return $this->mTitleObj;
172  }
173 
174  $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
175 
176  if ( isset( $params['title'] ) ) {
177  $this->mTitleObj = Title::newFromText( $params['title'] );
178  if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
179  $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
180  }
181  } elseif ( isset( $params['pageid'] ) ) {
182  $this->mTitleObj = Title::newFromID( $params['pageid'] );
183  if ( !$this->mTitleObj ) {
184  $this->dieWithError( [ 'apierror-nosuchpageid', $params['pageid'] ] );
185  }
186  }
187 
188  if ( !$this->mTitleObj->exists() ) {
189  $this->dieWithError( 'apierror-missingtitle' );
190  }
191 
192  return $this->mTitleObj;
193  }
194 
195  protected function getExamplesMessages() {
196  return [
197  'action=rollback&title=Main%20Page&user=Example&token=123ABC' =>
198  'apihelp-rollback-example-simple',
199  'action=rollback&title=Main%20Page&user=192.0.2.5&' .
200  'token=123ABC&summary=Reverting%20vandalism&markbot=1' =>
201  'apihelp-rollback-example-summary',
202  ];
203  }
204 
205  public function getHelpUrls() {
206  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Rollback';
207  }
208 }
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:63
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:332
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:74
Profiler\instance
static instance()
Singleton.
Definition: Profiler.php:63
ApiRollback
Definition: ApiRollback.php:29
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1415
true
return true
Definition: router.php:90
ApiBase\PARAM_TYPE
const PARAM_TYPE
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:68
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:546
ApiRollback\$mTitleObj
Title $mTitleObj
Definition: ApiRollback.php:34
ContextSource\getUser
getUser()
Definition: ContextSource.php:120
ApiBase\PARAM_HELP_MSG_APPEND
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
Definition: ApiBase.php:111
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:50
ApiRollback\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiRollback.php:41
Wikimedia\ParamValidator\ParamValidator::TypeDef\UserDef
Type definition for user types.
Definition: UserDef.php:23
ApiRollback\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiRollback.php:103
ApiRollback\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiRollback.php:195
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:146
ApiRollback\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiRollback.php:107
ApiRollback\getRbTitle
getRbTitle(array $params)
Definition: ApiRollback.php:169
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:695
ApiRollback\getRbUser
getRbUser(array $params)
Definition: ApiRollback.php:154
ApiRollback\$mUser
UserIdentity $mUser
Definition: ApiRollback.php:39
ApiBase\setWatch
setWatch( $watch, $titleObj, $userOption=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
Definition: ApiBase.php:1151
ApiBase\requireOnlyOneParameter
requireOnlyOneParameter( $params,... $required)
Die if none or more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:832
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1272
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1485
ChangeTags\canAddTagsAccompanyingChange
static canAddTagsAccompanyingChange(array $tags, User $user=null)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
Definition: ChangeTags.php:531
ApiRollback\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiRollback.php:205
Title
Represents a title within MediaWiki.
Definition: Title.php:42
ApiRollback\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiRollback.php:145
ApiBase\PARAM_DFLT
const PARAM_DFLT
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:66
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1473
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:426
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:67
ApiRollback\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiRollback.php:111
DeferredUpdates\addCallableUpdate
static addCallableUpdate( $callable, $stage=self::POSTSEND, $dbw=null)
Add a callable update.
Definition: DeferredUpdates.php:145
Title\newFromID
static newFromID( $id, $flags=0)
Create a new Title from an article ID.
Definition: Title.php:476
ApiBase\errorArrayToStatus
errorArrayToStatus(array $errors, User $user=null)
Turn an array of message keys or key+param arrays into a Status.
Definition: ApiBase.php:1226