MediaWiki  1.23.8
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() {
43  $params = $this->extractRequestParams();
44 
45  // User and title already validated in call to getTokenSalt from Main
46  $titleObj = $this->getRbTitle();
47  $pageObj = WikiPage::factory( $titleObj );
48  $summary = $params['summary'];
49  $details = array();
50  $retval = $pageObj->doRollback(
51  $this->getRbUser(),
52  $summary,
53  $params['token'],
54  $params['markbot'],
55  $details,
56  $this->getUser()
57  );
58 
59  if ( $retval ) {
60  // We don't care about multiple errors, just report one of them
61  $this->dieUsageMsg( reset( $retval ) );
62  }
63 
64  $this->setWatch( $params['watchlist'], $titleObj );
65 
66  $info = array(
67  'title' => $titleObj->getPrefixedText(),
68  'pageid' => intval( $details['current']->getPage() ),
69  'summary' => $details['summary'],
70  'revid' => intval( $details['newid'] ),
71  'old_revid' => intval( $details['current']->getID() ),
72  'last_revid' => intval( $details['target']->getID() )
73  );
74 
75  $this->getResult()->addValue( null, $this->getModuleName(), $info );
76  }
77 
78  public function mustBePosted() {
79  return true;
80  }
81 
82  public function isWriteMode() {
83  return true;
84  }
85 
86  public function getAllowedParams() {
87  return array(
88  'title' => array(
89  ApiBase::PARAM_TYPE => 'string',
91  ),
92  'user' => array(
93  ApiBase::PARAM_TYPE => 'string',
95  ),
96  'token' => array(
97  ApiBase::PARAM_TYPE => 'string',
99  ),
100  'summary' => '',
101  'markbot' => false,
102  'watchlist' => array(
103  ApiBase::PARAM_DFLT => 'preferences',
105  'watch',
106  'unwatch',
107  'preferences',
108  'nochange'
109  ),
110  ),
111  );
112  }
113 
114  public function getParamDescription() {
115  return array(
116  'title' => 'Title of the page you want to rollback.',
117  'user' => 'Name of the user whose edits are to be rolled back. If ' .
118  'set incorrectly, you\'ll get a badtoken error.',
119  'token' => 'A rollback token previously retrieved through ' .
120  "{$this->getModulePrefix()}prop=revisions",
121  'summary' => 'Custom edit summary. If empty, default summary will be used',
122  'markbot' => 'Mark the reverted edits and the revert as bot edits',
123  'watchlist' => 'Unconditionally add or remove the page from your watchlist, ' .
124  'use preferences or do not change watch',
125  );
126  }
127 
128  public function getResultProperties() {
129  return array(
130  '' => array(
131  'title' => 'string',
132  'pageid' => 'integer',
133  'summary' => 'string',
134  'revid' => 'integer',
135  'old_revid' => 'integer',
136  'last_revid' => 'integer'
137  )
138  );
139  }
140 
141  public function getDescription() {
142  return array(
143  'Undo the last edit to the page. If the last user who edited the page made',
144  'multiple edits in a row, they will all be rolled back.'
145  );
146  }
147 
148  public function getPossibleErrors() {
149  return array_merge( parent::getPossibleErrors(), array(
150  array( 'invalidtitle', 'title' ),
151  array( 'notanarticle' ),
152  array( 'invaliduser', 'user' ),
153  ) );
154  }
155 
156  public function needsToken() {
157  return true;
158  }
159 
160  public function getTokenSalt() {
161  return array( $this->getRbTitle()->getPrefixedText(), $this->getRbUser() );
162  }
163 
164  private function getRbUser() {
165  if ( $this->mUser !== null ) {
166  return $this->mUser;
167  }
168 
169  $params = $this->extractRequestParams();
170 
171  // We need to be able to revert IPs, but getCanonicalName rejects them
172  $this->mUser = User::isIP( $params['user'] )
173  ? $params['user']
174  : User::getCanonicalName( $params['user'] );
175  if ( !$this->mUser ) {
176  $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
177  }
178 
179  return $this->mUser;
180  }
181 
185  private function getRbTitle() {
186  if ( $this->mTitleObj !== null ) {
187  return $this->mTitleObj;
188  }
189 
190  $params = $this->extractRequestParams();
191 
192  $this->mTitleObj = Title::newFromText( $params['title'] );
193 
194  if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
195  $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
196  }
197  if ( !$this->mTitleObj->exists() ) {
198  $this->dieUsageMsg( 'notanarticle' );
199  }
200 
201  return $this->mTitleObj;
202  }
203 
204  public function getExamples() {
205  return array(
206  'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
207  'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&' .
208  'token=123ABC&summary=Reverting%20vandalism&markbot=1'
209  );
210  }
211 
212  public function getHelpUrls() {
213  return 'https://www.mediawiki.org/wiki/API:Rollback';
214  }
215 }
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:189
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
Definition: ApiBase.php:62
ApiRollback\getResultProperties
getResultProperties()
Returns possible properties in the result, grouped by the value of the prop parameter that shows them...
Definition: ApiRollback.php:126
ApiRollback\$mUser
User $mUser
Definition: ApiRollback.php:38
ApiRollback
Definition: ApiRollback.php:30
ApiRollback\getRbUser
getRbUser()
Definition: ApiRollback.php:162
ApiBase\dieUsageMsg
dieUsageMsg( $error)
Output the error message related to a certain array.
Definition: ApiBase.php:1933
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:50
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:205
ApiRollback\getParamDescription
getParamDescription()
Returns an array of parameter descriptions.
Definition: ApiRollback.php:112
ApiRollback\$mTitleObj
Title $mTitleObj
Definition: ApiRollback.php:34
$params
$params
Definition: styleTest.css.php:40
ContextSource\getUser
getUser()
Get the User object.
Definition: ContextSource.php:132
ApiRollback\getRbTitle
getRbTitle()
Definition: ApiRollback.php:183
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:42
ApiRollback\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiRollback.php:40
ApiRollback\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiRollback.php:76
ApiRollback\getExamples
getExamples()
Returns usage examples for this module.
Definition: ApiRollback.php:202
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:103
ApiRollback\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiRollback.php:80
User\isIP
static isIP( $name)
Does the string match an anonymous IPv4 address?
Definition: User.php:555
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
ApiBase\setWatch
setWatch( $watch, $titleObj, $userOption=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
Definition: ApiBase.php:952
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:687
ApiRollback\getDescription
getDescription()
Returns the description string for this module.
Definition: ApiRollback.php:139
ApiRollback\getPossibleErrors
getPossibleErrors()
Returns a list of all possible errors returned by the module.
Definition: ApiRollback.php:146
$summary
$summary
Definition: importImages.php:120
ApiRollback\getHelpUrls
getHelpUrls()
Definition: ApiRollback.php:210
Title
Represents a title within MediaWiki.
Definition: Title.php:35
ApiRollback\needsToken
needsToken()
Returns whether this module requires a token to execute It is used to show possible errors in action=...
Definition: ApiRollback.php:154
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:884
ApiBase\PARAM_DFLT
const PARAM_DFLT
Definition: ApiBase.php:46
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:148
ApiRollback\getTokenSalt
getTokenSalt()
Returns the token salt if there is one, '' if the module doesn't require a salt, else false if the mo...
Definition: ApiRollback.php:158
ApiRollback\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiRollback.php:84
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:59
$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:237