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