MediaWiki REL1_27
ApiRollback.php
Go to the documentation of this file.
1<?php
30class ApiRollback extends ApiBase {
31
35 private $mTitleObj = null;
36
40 private $mUser = null;
41
42 public function execute() {
44
45 $user = $this->getUser();
47
48 // WikiPage::doRollback needs a Web UI token, so get one of those if we
49 // validated based on an API rollback token.
50 $token = $params['token'];
51 if ( $user->matchEditToken( $token, 'rollback', $this->getRequest() ) ) {
52 $token = $this->getUser()->getEditToken(
53 $this->getWebUITokenSalt( $params ),
54 $this->getRequest()
55 );
56 }
57
58 $titleObj = $this->getRbTitle( $params );
59 $pageObj = WikiPage::factory( $titleObj );
60 $summary = $params['summary'];
61 $details = [];
62
63 // If change tagging was requested, check that the user is allowed to tag,
64 // and the tags are valid
65 if ( count( $params['tags'] ) ) {
67 if ( !$tagStatus->isOK() ) {
68 $this->dieStatus( $tagStatus );
69 }
70 }
71
72 $retval = $pageObj->doRollback(
73 $this->getRbUser( $params ),
75 $token,
76 $params['markbot'],
77 $details,
78 $user,
79 $params['tags']
80 );
81
82 if ( $retval ) {
83 // We don't care about multiple errors, just report one of them
84 $this->dieUsageMsg( reset( $retval ) );
85 }
86
87 $watch = 'preferences';
88 if ( isset( $params['watchlist'] ) ) {
89 $watch = $params['watchlist'];
90 }
91
92 // Watch pages
93 $this->setWatch( $watch, $titleObj, 'watchrollback' );
94
95 $info = [
96 'title' => $titleObj->getPrefixedText(),
97 'pageid' => intval( $details['current']->getPage() ),
98 'summary' => $details['summary'],
99 'revid' => intval( $details['newid'] ),
100 'old_revid' => intval( $details['current']->getID() ),
101 'last_revid' => intval( $details['target']->getID() )
102 ];
103
104 $this->getResult()->addValue( null, $this->getModuleName(), $info );
105 }
106
107 public function mustBePosted() {
108 return true;
109 }
110
111 public function isWriteMode() {
112 return true;
113 }
114
115 public function getAllowedParams() {
116 return [
117 'title' => null,
118 'pageid' => [
119 ApiBase::PARAM_TYPE => 'integer'
120 ],
121 'tags' => [
122 ApiBase::PARAM_TYPE => 'tags',
124 ],
125 'user' => [
126 ApiBase::PARAM_TYPE => 'user',
128 ],
129 'summary' => '',
130 'markbot' => false,
131 'watchlist' => [
132 ApiBase::PARAM_DFLT => 'preferences',
134 'watch',
135 'unwatch',
136 'preferences',
137 'nochange'
138 ],
139 ],
140 'token' => [
141 // Standard definition automatically inserted
142 ApiBase::PARAM_HELP_MSG_APPEND => [ 'api-help-param-token-webui' ],
143 ],
144 ];
145 }
146
147 public function needsToken() {
148 return 'rollback';
149 }
150
151 protected function getWebUITokenSalt( array $params ) {
152 return [
153 $this->getRbTitle( $params )->getPrefixedText(),
154 $this->getRbUser( $params )
155 ];
156 }
157
163 private function getRbUser( array $params ) {
164 if ( $this->mUser !== null ) {
165 return $this->mUser;
166 }
167
168 // We need to be able to revert IPs, but getCanonicalName rejects them
169 $this->mUser = User::isIP( $params['user'] )
170 ? $params['user']
171 : User::getCanonicalName( $params['user'] );
172 if ( !$this->mUser ) {
173 $this->dieUsageMsg( [ 'invaliduser', $params['user'] ] );
174 }
175
176 return $this->mUser;
177 }
178
184 private function getRbTitle( array $params ) {
185 if ( $this->mTitleObj !== null ) {
186 return $this->mTitleObj;
187 }
188
189 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
190
191 if ( isset( $params['title'] ) ) {
192 $this->mTitleObj = Title::newFromText( $params['title'] );
193 if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
194 $this->dieUsageMsg( [ 'invalidtitle', $params['title'] ] );
195 }
196 } elseif ( isset( $params['pageid'] ) ) {
197 $this->mTitleObj = Title::newFromID( $params['pageid'] );
198 if ( !$this->mTitleObj ) {
199 $this->dieUsageMsg( [ 'nosuchpageid', $params['pageid'] ] );
200 }
201 }
202
203 if ( !$this->mTitleObj->exists() ) {
204 $this->dieUsageMsg( 'notanarticle' );
205 }
206
207 return $this->mTitleObj;
208 }
209
210 protected function getExamplesMessages() {
211 return [
212 'action=rollback&title=Main%20Page&user=Example&token=123ABC' =>
213 'apihelp-rollback-example-simple',
214 'action=rollback&title=Main%20Page&user=192.0.2.5&' .
215 'token=123ABC&summary=Reverting%20vandalism&markbot=1' =>
216 'apihelp-rollback-example-summary',
217 ];
218 }
219
220 public function getHelpUrls() {
221 return 'https://www.mediawiki.org/wiki/API:Rollback';
222 }
223}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:39
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition ApiBase.php:112
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:88
setWatch( $watch, $titleObj, $userOption=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
Definition ApiBase.php:1375
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition ApiBase.php:50
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
Definition ApiBase.php:132
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:685
dieUsageMsg( $error)
Output the error message related to a certain array.
Definition ApiBase.php:2144
getResult()
Get the result object.
Definition ApiBase.php:584
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:464
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:2984
dieStatus( $status)
Throw a UsageException based on the errors in the Status object.
Definition ApiBase.php:1615
requireOnlyOneParameter( $params, $required)
Die if none or more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:721
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition ApiBase.php:53
getRbTitle(array $params)
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getRbUser(array $params)
Title $mTitleObj
getHelpUrls()
Return links to more detailed help pages about the module.
isWriteMode()
Indicates whether this module requires write mode.
getExamplesMessages()
Returns usage examples for this module.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getWebUITokenSalt(array $params)
Fetch the salt used in the Web UI corresponding to this module.
mustBePosted()
Indicates whether this module must be called with a POST request.
needsToken()
Returns the token type this module requires in order to execute.
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...
getUser()
Get the User object.
getRequest()
Get the WebRequest object.
Represents a title within MediaWiki.
Definition Title.php:34
static newFromID( $id, $flags=0)
Create a new Title from an article ID.
Definition Title.php:417
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition Title.php:277
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:47
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition WikiPage.php:99
the array() calling protocol came about after MediaWiki 1.4rc1.
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition hooks.txt:249
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account incomplete not yet checked for validity & $retval
Definition hooks.txt:268
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:1811
$summary
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:37
$params