MediaWiki  1.29.2
ApiRollback.php
Go to the documentation of this file.
1 <?php
30 class ApiRollback extends ApiBase {
31 
35  private $mTitleObj = null;
36 
40  private $mUser = null;
41 
42  public function execute() {
44 
45  $user = $this->getUser();
46  $params = $this->extractRequestParams();
47 
48  $titleObj = $this->getRbTitle( $params );
49  $pageObj = WikiPage::factory( $titleObj );
50  $summary = $params['summary'];
51  $details = [];
52 
53  // If change tagging was requested, check that the user is allowed to tag,
54  // and the tags are valid
55  if ( count( $params['tags'] ) ) {
57  if ( !$tagStatus->isOK() ) {
58  $this->dieStatus( $tagStatus );
59  }
60  }
61 
62  $retval = $pageObj->doRollback(
63  $this->getRbUser( $params ),
64  $summary,
65  $params['token'],
66  $params['markbot'],
67  $details,
68  $user,
69  $params['tags']
70  );
71 
72  if ( $retval ) {
73  $this->dieStatus( $this->errorArrayToStatus( $retval, $user ) );
74  }
75 
76  $watch = 'preferences';
77  if ( isset( $params['watchlist'] ) ) {
78  $watch = $params['watchlist'];
79  }
80 
81  // Watch pages
82  $this->setWatch( $watch, $titleObj, 'watchrollback' );
83 
84  $info = [
85  'title' => $titleObj->getPrefixedText(),
86  'pageid' => intval( $details['current']->getPage() ),
87  'summary' => $details['summary'],
88  'revid' => intval( $details['newid'] ),
89  // The revision being reverted (previously the current revision of the page)
90  'old_revid' => intval( $details['current']->getID() ),
91  // The revision being restored (the last revision before revision(s) by the reverted user)
92  'last_revid' => intval( $details['target']->getID() )
93  ];
94 
95  $this->getResult()->addValue( null, $this->getModuleName(), $info );
96  }
97 
98  public function mustBePosted() {
99  return true;
100  }
101 
102  public function isWriteMode() {
103  return true;
104  }
105 
106  public function getAllowedParams() {
107  return [
108  'title' => null,
109  'pageid' => [
110  ApiBase::PARAM_TYPE => 'integer'
111  ],
112  'tags' => [
113  ApiBase::PARAM_TYPE => 'tags',
114  ApiBase::PARAM_ISMULTI => true,
115  ],
116  'user' => [
117  ApiBase::PARAM_TYPE => 'user',
119  ],
120  'summary' => '',
121  'markbot' => false,
122  'watchlist' => [
123  ApiBase::PARAM_DFLT => 'preferences',
125  'watch',
126  'unwatch',
127  'preferences',
128  'nochange'
129  ],
130  ],
131  'token' => [
132  // Standard definition automatically inserted
133  ApiBase::PARAM_HELP_MSG_APPEND => [ 'api-help-param-token-webui' ],
134  ],
135  ];
136  }
137 
138  public function needsToken() {
139  return 'rollback';
140  }
141 
147  private function getRbUser( array $params ) {
148  if ( $this->mUser !== null ) {
149  return $this->mUser;
150  }
151 
152  // We need to be able to revert IPs, but getCanonicalName rejects them
153  $this->mUser = User::isIP( $params['user'] )
154  ? $params['user']
155  : User::getCanonicalName( $params['user'] );
156  if ( !$this->mUser ) {
157  $this->dieWithError( [ 'apierror-invaliduser', wfEscapeWikiText( $params['user'] ) ] );
158  }
159 
160  return $this->mUser;
161  }
162 
168  private function getRbTitle( array $params ) {
169  if ( $this->mTitleObj !== null ) {
170  return $this->mTitleObj;
171  }
172 
173  $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
174 
175  if ( isset( $params['title'] ) ) {
176  $this->mTitleObj = Title::newFromText( $params['title'] );
177  if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
178  $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
179  }
180  } elseif ( isset( $params['pageid'] ) ) {
181  $this->mTitleObj = Title::newFromID( $params['pageid'] );
182  if ( !$this->mTitleObj ) {
183  $this->dieWithError( [ 'apierror-nosuchpageid', $params['pageid'] ] );
184  }
185  }
186 
187  if ( !$this->mTitleObj->exists() ) {
188  $this->dieWithError( 'apierror-missingtitle' );
189  }
190 
191  return $this->mTitleObj;
192  }
193 
194  protected function getExamplesMessages() {
195  return [
196  'action=rollback&title=Main%20Page&user=Example&token=123ABC' =>
197  'apihelp-rollback-example-simple',
198  'action=rollback&title=Main%20Page&user=192.0.2.5&' .
199  'token=123ABC&summary=Reverting%20vandalism&markbot=1' =>
200  'apihelp-rollback-example-summary',
201  ];
202  }
203 
204  public function getHelpUrls() {
205  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Rollback';
206  }
207 }
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:265
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:115
ApiRollback\$mUser
User $mUser
Definition: ApiRollback.php:40
ApiRollback
Definition: ApiRollback.php:30
captcha-old.count
count
Definition: captcha-old.py:225
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1796
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:91
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:610
ApiRollback\$mTitleObj
Title $mTitleObj
Definition: ApiRollback.php:35
$user
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 account $user
Definition: hooks.txt:246
$params
$params
Definition: styleTest.css.php:40
ContextSource\getUser
getUser()
Get the User object.
Definition: ContextSource.php:133
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:135
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:41
ApiRollback\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiRollback.php:42
ApiRollback\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiRollback.php:98
ApiRollback\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiRollback.php:194
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:120
ApiRollback\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiRollback.php:102
ApiRollback\getRbTitle
getRbTitle(array $params)
Definition: ApiRollback.php:168
User\isIP
static isIP( $name)
Does the string match an anonymous IP address?
Definition: User.php:819
ApiRollback\getRbUser
getRbUser(array $params)
Definition: ApiRollback.php:147
ApiBase\setWatch
setWatch( $watch, $titleObj, $userOption=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
Definition: ApiBase.php:1557
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:718
$retval
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 account incomplete not yet checked for validity & $retval
Definition: hooks.txt:246
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:2440
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1657
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:754
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:395
ApiRollback\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiRollback.php:204
Title
Represents a title within MediaWiki.
Definition: Title.php:39
ApiRollback\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiRollback.php:138
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:1076
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:52
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1861
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:490
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:55
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:1956
ApiRollback\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiRollback.php:106
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:50
Title\newFromID
static newFromID( $id, $flags=0)
Create a new Title from an article ID.
Definition: Title.php:405
ApiBase\errorArrayToStatus
errorArrayToStatus(array $errors, User $user=null)
Turn an array of message keys or key+param arrays into a Status.
Definition: ApiBase.php:1667
array
the array() calling protocol came about after MediaWiki 1.4rc1.