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