MediaWiki  1.33.1
ApiRollback.php
Go to the documentation of this file.
1 <?php
26 class ApiRollback extends ApiBase {
27 
31  private $mTitleObj = null;
32 
36  private $mUser = null;
37 
38  public function execute() {
40 
41  $user = $this->getUser();
42  $params = $this->extractRequestParams();
43 
44  $titleObj = $this->getRbTitle( $params );
45  $pageObj = WikiPage::factory( $titleObj );
46  $summary = $params['summary'];
47  $details = [];
48 
49  // If change tagging was requested, check that the user is allowed to tag,
50  // and the tags are valid
51  if ( $params['tags'] ) {
53  if ( !$tagStatus->isOK() ) {
54  $this->dieStatus( $tagStatus );
55  }
56  }
57 
58  // @TODO: remove this hack once rollback uses POST (T88044)
59  $fname = __METHOD__;
60  $trxLimits = $this->getConfig()->get( 'TrxProfilerLimits' );
61  $trxProfiler = Profiler::instance()->getTransactionProfiler();
62  $trxProfiler->redefineExpectations( $trxLimits['POST'], $fname );
63  DeferredUpdates::addCallableUpdate( function () use ( $trxProfiler, $trxLimits, $fname ) {
64  $trxProfiler->redefineExpectations( $trxLimits['PostSend-POST'], $fname );
65  } );
66 
67  $retval = $pageObj->doRollback(
68  $this->getRbUser( $params ),
69  $summary,
70  $params['token'],
71  $params['markbot'],
72  $details,
73  $user,
74  $params['tags']
75  );
76 
77  if ( $retval ) {
78  $this->dieStatus( $this->errorArrayToStatus( $retval, $user ) );
79  }
80 
81  $watch = $params['watchlist'] ?? 'preferences';
82 
83  // Watch pages
84  $this->setWatch( $watch, $titleObj, 'watchrollback' );
85 
86  $info = [
87  'title' => $titleObj->getPrefixedText(),
88  'pageid' => (int)$details['current']->getPage(),
89  'summary' => $details['summary'],
90  'revid' => (int)$details['newid'],
91  // The revision being reverted (previously the current revision of the page)
92  'old_revid' => (int)$details['current']->getID(),
93  // The revision being restored (the last revision before revision(s) by the reverted user)
94  'last_revid' => (int)$details['target']->getID()
95  ];
96 
97  $this->getResult()->addValue( null, $this->getModuleName(), $info );
98  }
99 
100  public function mustBePosted() {
101  return true;
102  }
103 
104  public function isWriteMode() {
105  return true;
106  }
107 
108  public function getAllowedParams() {
109  return [
110  'title' => null,
111  'pageid' => [
112  ApiBase::PARAM_TYPE => 'integer'
113  ],
114  'tags' => [
115  ApiBase::PARAM_TYPE => 'tags',
116  ApiBase::PARAM_ISMULTI => true,
117  ],
118  'user' => [
119  ApiBase::PARAM_TYPE => 'user',
121  ],
122  'summary' => '',
123  'markbot' => false,
124  'watchlist' => [
125  ApiBase::PARAM_DFLT => 'preferences',
127  'watch',
128  'unwatch',
129  'preferences',
130  'nochange'
131  ],
132  ],
133  'token' => [
134  // Standard definition automatically inserted
135  ApiBase::PARAM_HELP_MSG_APPEND => [ 'api-help-param-token-webui' ],
136  ],
137  ];
138  }
139 
140  public function needsToken() {
141  return 'rollback';
142  }
143 
149  private function getRbUser( array $params ) {
150  if ( $this->mUser !== null ) {
151  return $this->mUser;
152  }
153 
154  // We need to be able to revert IPs, but getCanonicalName rejects them
155  $this->mUser = User::isIP( $params['user'] )
156  ? $params['user']
157  : User::getCanonicalName( $params['user'] );
158  if ( !$this->mUser ) {
159  $this->dieWithError( [ 'apierror-invaliduser', wfEscapeWikiText( $params['user'] ) ] );
160  }
161 
162  return $this->mUser;
163  }
164 
170  private function getRbTitle( array $params ) {
171  if ( $this->mTitleObj !== null ) {
172  return $this->mTitleObj;
173  }
174 
175  $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
176 
177  if ( isset( $params['title'] ) ) {
178  $this->mTitleObj = Title::newFromText( $params['title'] );
179  if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
180  $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
181  }
182  } elseif ( isset( $params['pageid'] ) ) {
183  $this->mTitleObj = Title::newFromID( $params['pageid'] );
184  if ( !$this->mTitleObj ) {
185  $this->dieWithError( [ 'apierror-nosuchpageid', $params['pageid'] ] );
186  }
187  }
188 
189  if ( !$this->mTitleObj->exists() ) {
190  $this->dieWithError( 'apierror-missingtitle' );
191  }
192 
193  return $this->mTitleObj;
194  }
195 
196  protected function getExamplesMessages() {
197  return [
198  'action=rollback&title=Main%20Page&user=Example&token=123ABC' =>
199  'apihelp-rollback-example-simple',
200  'action=rollback&title=Main%20Page&user=192.0.2.5&' .
201  'token=123ABC&summary=Reverting%20vandalism&markbot=1' =>
202  'apihelp-rollback-example-summary',
203  ];
204  }
205 
206  public function getHelpUrls() {
207  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Rollback';
208  }
209 }
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:63
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
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:306
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:111
Profiler\instance
static instance()
Singleton.
Definition: Profiler.php:62
ApiRollback\$mUser
User $mUser
Definition: ApiRollback.php:36
ApiRollback
Definition: ApiRollback.php:26
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1990
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:87
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:632
ApiRollback\$mTitleObj
Title $mTitleObj
Definition: ApiRollback.php:31
$params
$params
Definition: styleTest.css.php:44
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:131
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:37
ApiRollback\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiRollback.php:38
ApiRollback\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiRollback.php:100
ApiRollback\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiRollback.php:196
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:138
ApiRollback\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiRollback.php:104
ApiRollback\getRbTitle
getRbTitle(array $params)
Definition: ApiRollback.php:170
User\isIP
static isIP( $name)
Does the string match an anonymous IP address?
Definition: User.php:967
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:743
ApiRollback\getRbUser
getRbUser(array $params)
Definition: ApiRollback.php:149
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$fname
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition: Setup.php:123
ApiBase\setWatch
setWatch( $watch, $titleObj, $userOption=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
Definition: ApiBase.php:1726
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1847
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1577
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:875
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:481
ApiRollback\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiRollback.php:206
Title
Represents a title within MediaWiki.
Definition: Title.php:40
ApiRollback\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiRollback.php:140
User\getCanonicalName
static getCanonicalName( $name, $validate='valid')
Given unvalidated user input, return a canonical username, or false if the username is invalid.
Definition: User.php:1244
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:48
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:2061
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:512
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:51
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1993
ApiRollback\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiRollback.php:108
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:48
DeferredUpdates\addCallableUpdate
static addCallableUpdate( $callable, $stage=self::POSTSEND, $dbw=null)
Add a callable update.
Definition: DeferredUpdates.php:118
Title\newFromID
static newFromID( $id, $flags=0)
Create a new Title from an article ID.
Definition: Title.php:457
ApiBase\errorArrayToStatus
errorArrayToStatus(array $errors, User $user=null)
Turn an array of message keys or key+param arrays into a Status.
Definition: ApiBase.php:1801