MediaWiki  1.28.1
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  // We don't care about multiple errors, just report one of them
73  if ( $retval ) {
74  if ( isset( $retval[0][0] ) &&
75  ( $retval[0][0] == 'alreadyrolled' || $retval[0][0] == 'cantrollback' )
76  ) {
77  $error = $retval[0];
78  $userMessage = $this->msg( $error[0], array_slice( $error, 1 ) );
79  // dieUsageMsg() doesn't support $extraData
80  $errorCode = $error[0];
81  $errorInfo = isset( ApiBase::$messageMap[$errorCode] ) ?
82  ApiBase::$messageMap[$errorCode]['info'] :
83  $errorCode;
84  $this->dieUsage( $errorInfo, $errorCode, 0, [
85  'messageHtml' => $userMessage->parseAsBlock()
86  ] );
87  }
88 
89  $this->dieUsageMsg( reset( $retval ) );
90  }
91 
92  $watch = 'preferences';
93  if ( isset( $params['watchlist'] ) ) {
94  $watch = $params['watchlist'];
95  }
96 
97  // Watch pages
98  $this->setWatch( $watch, $titleObj, 'watchrollback' );
99 
100  $info = [
101  'title' => $titleObj->getPrefixedText(),
102  'pageid' => intval( $details['current']->getPage() ),
103  'summary' => $details['summary'],
104  'revid' => intval( $details['newid'] ),
105  // The revision being reverted (previously the current revision of the page)
106  'old_revid' => intval( $details['current']->getID() ),
107  // The revision being restored (the last revision before revision(s) by the reverted user)
108  'last_revid' => intval( $details['target']->getID() )
109  ];
110 
111  $oldUser = $details['current']->getUserText( Revision::FOR_THIS_USER );
112  $lastUser = $details['target']->getUserText( Revision::FOR_THIS_USER );
113  $diffUrl = $titleObj->getFullURL( [
114  'diff' => $info['revid'],
115  'oldid' => $info['old_revid'],
116  'diffonly' => '1'
117  ] );
118  $info['messageHtml'] = $this->msg( 'rollback-success-notify' )
119  ->params( $oldUser, $lastUser, $diffUrl )
120  ->parseAsBlock();
121 
122  $this->getResult()->addValue( null, $this->getModuleName(), $info );
123  }
124 
125  public function mustBePosted() {
126  return true;
127  }
128 
129  public function isWriteMode() {
130  return true;
131  }
132 
133  public function getAllowedParams() {
134  return [
135  'title' => null,
136  'pageid' => [
137  ApiBase::PARAM_TYPE => 'integer'
138  ],
139  'tags' => [
140  ApiBase::PARAM_TYPE => 'tags',
141  ApiBase::PARAM_ISMULTI => true,
142  ],
143  'user' => [
144  ApiBase::PARAM_TYPE => 'user',
146  ],
147  'summary' => '',
148  'markbot' => false,
149  'watchlist' => [
150  ApiBase::PARAM_DFLT => 'preferences',
152  'watch',
153  'unwatch',
154  'preferences',
155  'nochange'
156  ],
157  ],
158  'token' => [
159  // Standard definition automatically inserted
160  ApiBase::PARAM_HELP_MSG_APPEND => [ 'api-help-param-token-webui' ],
161  ],
162  ];
163  }
164 
165  public function needsToken() {
166  return 'rollback';
167  }
168 
174  private function getRbUser( array $params ) {
175  if ( $this->mUser !== null ) {
176  return $this->mUser;
177  }
178 
179  // We need to be able to revert IPs, but getCanonicalName rejects them
180  $this->mUser = User::isIP( $params['user'] )
181  ? $params['user']
182  : User::getCanonicalName( $params['user'] );
183  if ( !$this->mUser ) {
184  $this->dieUsageMsg( [ 'invaliduser', $params['user'] ] );
185  }
186 
187  return $this->mUser;
188  }
189 
195  private function getRbTitle( array $params ) {
196  if ( $this->mTitleObj !== null ) {
197  return $this->mTitleObj;
198  }
199 
200  $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
201 
202  if ( isset( $params['title'] ) ) {
203  $this->mTitleObj = Title::newFromText( $params['title'] );
204  if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
205  $this->dieUsageMsg( [ 'invalidtitle', $params['title'] ] );
206  }
207  } elseif ( isset( $params['pageid'] ) ) {
208  $this->mTitleObj = Title::newFromID( $params['pageid'] );
209  if ( !$this->mTitleObj ) {
210  $this->dieUsageMsg( [ 'nosuchpageid', $params['pageid'] ] );
211  }
212  }
213 
214  if ( !$this->mTitleObj->exists() ) {
215  $this->dieUsageMsg( 'notanarticle' );
216  }
217 
218  return $this->mTitleObj;
219  }
220 
221  protected function getExamplesMessages() {
222  return [
223  'action=rollback&title=Main%20Page&user=Example&token=123ABC' =>
224  'apihelp-rollback-example-simple',
225  'action=rollback&title=Main%20Page&user=192.0.2.5&' .
226  'token=123ABC&summary=Reverting%20vandalism&markbot=1' =>
227  'apihelp-rollback-example-summary',
228  ];
229  }
230 
231  public function getHelpUrls() {
232  return 'https://www.mediawiki.org/wiki/API:Rollback';
233  }
234 }
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:115
const FOR_THIS_USER
Definition: Revision.php:93
static newFromID($id, $flags=0)
Create a new Title from an article ID.
Definition: Title.php:402
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below...
Definition: ApiBase.php:88
the array() calling protocol came about after MediaWiki 1.4rc1.
getResult()
Get the result object.
Definition: ApiBase.php:584
static $messageMap
Array that maps message keys to error messages.
Definition: ApiBase.php:1684
static getCanonicalName($name, $validate= 'valid')
Given unvalidated user input, return a canonical username, or false if the username is invalid...
Definition: User.php:1046
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:2764
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:50
setWatch($watch, $titleObj, $userOption=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
Definition: ApiBase.php:1434
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:112
extractRequestParams($parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user...
Definition: ApiBase.php:685
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
Title $mTitleObj
Definition: ApiRollback.php:35
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:262
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:1936
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
Definition: ApiBase.php:132
msg()
Get a Message object with context set Parameters are the same as wfMessage()
getRbTitle(array $params)
$summary
getRbUser(array $params)
$params
static isIP($name)
Does the string match an anonymous IP address?
Definition: User.php:788
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:464
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:242
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
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:53
dieUsage($description, $errorCode, $httpRespCode=0, $extradata=null)
Throw a UsageException, which will (if uncaught) call the main module's error handler and die with an...
Definition: ApiBase.php:1585
This abstract class implements many basic API functions, and is the base of all API classes...
Definition: ApiBase.php:39
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:392
dieStatus($status)
Throw a UsageException based on the errors in the Status object.
Definition: ApiBase.php:1674
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:242
getUser()
Get the User object.
dieUsageMsg($error)
Output the error message related to a certain array.
Definition: ApiBase.php:2203