MediaWiki  REL1_20
ApiRollback.php
Go to the documentation of this file.
00001 <?php
00030 class ApiRollback extends ApiBase {
00031 
00032         public function __construct( $main, $action ) {
00033                 parent::__construct( $main, $action );
00034         }
00035 
00039         private $mTitleObj = null;
00040 
00044         private $mUser = null;
00045 
00046         public function execute() {
00047                 $params = $this->extractRequestParams();
00048 
00049                 // User and title already validated in call to getTokenSalt from Main
00050                 $titleObj = $this->getRbTitle();
00051                 $pageObj = WikiPage::factory( $titleObj );
00052                 $summary = $params['summary'];
00053                 $details = array();
00054                 $retval = $pageObj->doRollback( $this->getRbUser(), $summary, $params['token'], $params['markbot'], $details, $this->getUser() );
00055 
00056                 if ( $retval ) {
00057                         // We don't care about multiple errors, just report one of them
00058                         $this->dieUsageMsg( reset( $retval ) );
00059                 }
00060 
00061                 $this->setWatch( $params['watchlist'], $titleObj );
00062 
00063                 $info = array(
00064                         'title' => $titleObj->getPrefixedText(),
00065                         'pageid' => intval( $details['current']->getPage() ),
00066                         'summary' => $details['summary'],
00067                         'revid' => intval( $details['newid'] ),
00068                         'old_revid' => intval( $details['current']->getID() ),
00069                         'last_revid' => intval( $details['target']->getID() )
00070                 );
00071 
00072                 $this->getResult()->addValue( null, $this->getModuleName(), $info );
00073         }
00074 
00075         public function mustBePosted() {
00076                 return true;
00077         }
00078 
00079         public function isWriteMode() {
00080                 return true;
00081         }
00082 
00083         public function getAllowedParams() {
00084                 return array(
00085                         'title' => array(
00086                                 ApiBase::PARAM_TYPE => 'string',
00087                                 ApiBase::PARAM_REQUIRED => true
00088                         ),
00089                         'user' => array(
00090                                 ApiBase::PARAM_TYPE => 'string',
00091                                 ApiBase::PARAM_REQUIRED => true
00092                         ),
00093                         'token' => array(
00094                                 ApiBase::PARAM_TYPE => 'string',
00095                                 ApiBase::PARAM_REQUIRED => true
00096                         ),
00097                         'summary' => '',
00098                         'markbot' => false,
00099                         'watchlist' => array(
00100                                 ApiBase::PARAM_DFLT => 'preferences',
00101                                 ApiBase::PARAM_TYPE => array(
00102                                         'watch',
00103                                         'unwatch',
00104                                         'preferences',
00105                                         'nochange'
00106                                 ),
00107                         ),
00108                 );
00109         }
00110 
00111         public function getParamDescription() {
00112                 return array(
00113                         'title' => 'Title of the page you want to rollback.',
00114                         'user' => 'Name of the user whose edits are to be rolled back. If set incorrectly, you\'ll get a badtoken error.',
00115                         'token' => "A rollback token previously retrieved through {$this->getModulePrefix()}prop=revisions",
00116                         'summary' => 'Custom edit summary. If empty, default summary will be used',
00117                         'markbot' => 'Mark the reverted edits and the revert as bot edits',
00118                         'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
00119                 );
00120         }
00121 
00122         public function getResultProperties() {
00123                 return array(
00124                         '' => array(
00125                                 'title' => 'string',
00126                                 'pageid' => 'integer',
00127                                 'summary' => 'string',
00128                                 'revid' => 'integer',
00129                                 'old_revid' => 'integer',
00130                                 'last_revid' => 'integer'
00131                         )
00132                 );
00133         }
00134 
00135         public function getDescription() {
00136                 return array(
00137                         'Undo the last edit to the page. If the last user who edited the page made multiple edits in a row,',
00138                         'they will all be rolled back'
00139                 );
00140         }
00141 
00142         public function getPossibleErrors() {
00143                 return array_merge( parent::getPossibleErrors(), array(
00144                         array( 'invalidtitle', 'title' ),
00145                         array( 'notanarticle' ),
00146                         array( 'invaliduser', 'user' ),
00147                 ) );
00148         }
00149 
00150         public function needsToken() {
00151                 return true;
00152         }
00153 
00154         public function getTokenSalt() {
00155                 return array( $this->getRbTitle()->getPrefixedText(), $this->getRbUser() );
00156         }
00157 
00158         private function getRbUser() {
00159                 if ( $this->mUser !== null ) {
00160                         return $this->mUser;
00161                 }
00162 
00163                 $params = $this->extractRequestParams();
00164 
00165                 // We need to be able to revert IPs, but getCanonicalName rejects them
00166                 $this->mUser = User::isIP( $params['user'] )
00167                         ? $params['user']
00168                         : User::getCanonicalName( $params['user'] );
00169                 if ( !$this->mUser ) {
00170                         $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
00171                 }
00172 
00173                 return $this->mUser;
00174         }
00175 
00179         private function getRbTitle() {
00180                 if ( $this->mTitleObj !== null ) {
00181                         return $this->mTitleObj;
00182                 }
00183 
00184                 $params = $this->extractRequestParams();
00185 
00186                 $this->mTitleObj = Title::newFromText( $params['title'] );
00187 
00188                 if ( !$this->mTitleObj ) {
00189                         $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
00190                 }
00191                 if ( !$this->mTitleObj->exists() ) {
00192                         $this->dieUsageMsg( 'notanarticle' );
00193                 }
00194 
00195                 return $this->mTitleObj;
00196         }
00197 
00198         public function getExamples() {
00199                 return array(
00200                         'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
00201                         'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&token=123ABC&summary=Reverting%20vandalism&markbot=1'
00202                 );
00203         }
00204 
00205         public function getHelpUrls() {
00206                 return 'https://www.mediawiki.org/wiki/API:Rollback';
00207         }
00208 
00209         public function getVersion() {
00210                 return __CLASS__ . ': $Id$';
00211         }
00212 }