MediaWiki  REL1_19
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 = ( isset( $params['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' => null,
00094                         'summary' => null,
00095                         'markbot' => false,
00096                         'watchlist' => array(
00097                                 ApiBase::PARAM_DFLT => 'preferences',
00098                                 ApiBase::PARAM_TYPE => array(
00099                                         'watch',
00100                                         'unwatch',
00101                                         'preferences',
00102                                         'nochange'
00103                                 ),
00104                         ),
00105                 );
00106         }
00107 
00108         public function getParamDescription() {
00109                 return array(
00110                         'title' => 'Title of the page you want to rollback.',
00111                         'user' => 'Name of the user whose edits are to be rolled back. If set incorrectly, you\'ll get a badtoken error.',
00112                         'token' => "A rollback token previously retrieved through {$this->getModulePrefix()}prop=revisions",
00113                         'summary' => 'Custom edit summary. If not set, default summary will be used',
00114                         'markbot' => 'Mark the reverted edits and the revert as bot edits',
00115                         'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
00116                 );
00117         }
00118 
00119         public function getDescription() {
00120                 return array(
00121                         'Undo the last edit to the page. If the last user who edited the page made multiple edits in a row,',
00122                         'they will all be rolled back'
00123                 );
00124         }
00125 
00126         public function getPossibleErrors() {
00127                 return array_merge( parent::getPossibleErrors(), array(
00128                         array( 'invalidtitle', 'title' ),
00129                         array( 'notanarticle' ),
00130                         array( 'invaliduser', 'user' ),
00131                 ) );
00132         }
00133 
00134         public function needsToken() {
00135                 return true;
00136         }
00137 
00138         public function getTokenSalt() {
00139                 return array( $this->getRbTitle()->getPrefixedText(), $this->getRbUser() );
00140         }
00141 
00142         private function getRbUser() {
00143                 if ( $this->mUser !== null ) {
00144                         return $this->mUser;
00145                 }
00146 
00147                 $params = $this->extractRequestParams();
00148 
00149                 // We need to be able to revert IPs, but getCanonicalName rejects them
00150                 $this->mUser = User::isIP( $params['user'] )
00151                         ? $params['user']
00152                         : User::getCanonicalName( $params['user'] );
00153                 if ( !$this->mUser ) {
00154                         $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
00155                 }
00156 
00157                 return $this->mUser;
00158         }
00159 
00163         private function getRbTitle() {
00164                 if ( $this->mTitleObj !== null ) {
00165                         return $this->mTitleObj;
00166                 }
00167 
00168                 $params = $this->extractRequestParams();
00169 
00170                 $this->mTitleObj = Title::newFromText( $params['title'] );
00171 
00172                 if ( !$this->mTitleObj ) {
00173                         $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
00174                 }
00175                 if ( !$this->mTitleObj->exists() ) {
00176                         $this->dieUsageMsg( 'notanarticle' );
00177                 }
00178 
00179                 return $this->mTitleObj;
00180         }
00181 
00182         public function getExamples() {
00183                 return array(
00184                         'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
00185                         'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&token=123ABC&summary=Reverting%20vandalism&markbot=1'
00186                 );
00187         }
00188 
00189         public function getHelpUrls() {
00190                 return 'https://www.mediawiki.org/wiki/API:Rollback';
00191         }
00192 
00193         public function getVersion() {
00194                 return __CLASS__ . ': $Id$';
00195         }
00196 }