MediaWiki  REL1_24
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         $user = $this->getUser();
00044         $params = $this->extractRequestParams();
00045 
00046         // WikiPage::doRollback needs a Web UI token, so get one of those if we
00047         // validated based on an API rollback token.
00048         $token = $params['token'];
00049         if ( $user->matchEditToken( $token, 'rollback', $this->getRequest() ) ) {
00050             $token = $this->getUser()->getEditToken(
00051                 $this->getWebUITokenSalt( $params ),
00052                 $this->getRequest()
00053             );
00054         }
00055 
00056         $titleObj = $this->getRbTitle( $params );
00057         $pageObj = WikiPage::factory( $titleObj );
00058         $summary = $params['summary'];
00059         $details = array();
00060         $retval = $pageObj->doRollback(
00061             $this->getRbUser( $params ),
00062             $summary,
00063             $token,
00064             $params['markbot'],
00065             $details,
00066             $user
00067         );
00068 
00069         if ( $retval ) {
00070             // We don't care about multiple errors, just report one of them
00071             $this->dieUsageMsg( reset( $retval ) );
00072         }
00073 
00074         $watch = 'preferences';
00075         if ( isset( $params['watchlist'] ) ) {
00076             $watch = $params['watchlist'];
00077         }
00078 
00079         // Watch pages
00080         $this->setWatch( $watch, $titleObj, 'watchrollback' );
00081 
00082         $info = array(
00083             'title' => $titleObj->getPrefixedText(),
00084             'pageid' => intval( $details['current']->getPage() ),
00085             'summary' => $details['summary'],
00086             'revid' => intval( $details['newid'] ),
00087             'old_revid' => intval( $details['current']->getID() ),
00088             'last_revid' => intval( $details['target']->getID() )
00089         );
00090 
00091         $this->getResult()->addValue( null, $this->getModuleName(), $info );
00092     }
00093 
00094     public function mustBePosted() {
00095         return true;
00096     }
00097 
00098     public function isWriteMode() {
00099         return true;
00100     }
00101 
00102     public function getAllowedParams() {
00103         return array(
00104             'title' => null,
00105             'pageid' => array(
00106                 ApiBase::PARAM_TYPE => 'integer'
00107             ),
00108             'user' => array(
00109                 ApiBase::PARAM_TYPE => 'string',
00110                 ApiBase::PARAM_REQUIRED => true
00111             ),
00112             'summary' => '',
00113             'markbot' => false,
00114             'watchlist' => array(
00115                 ApiBase::PARAM_DFLT => 'preferences',
00116                 ApiBase::PARAM_TYPE => array(
00117                     'watch',
00118                     'unwatch',
00119                     'preferences',
00120                     'nochange'
00121                 ),
00122             ),
00123         );
00124     }
00125 
00126     public function getParamDescription() {
00127         $p = $this->getModulePrefix();
00128 
00129         return array(
00130             'title' => "Title of the page you want to roll back. Cannot be used together with {$p}pageid",
00131             'pageid' => "Page ID of the page you want to roll back. Cannot be used together with {$p}title",
00132             'user' => 'Name of the user whose edits are to be rolled back.',
00133             'token' => array(
00134                 /* Standard description automatically prepended */
00135                 'For compatibility, the token used in the web UI is also accepted.'
00136             ),
00137             'summary' => 'Custom edit summary. If empty, default summary will be used',
00138             'markbot' => 'Mark the reverted edits and the revert as bot edits',
00139             'watchlist' => 'Unconditionally add or remove the page from your watchlist, ' .
00140                 'use preferences or do not change watch',
00141         );
00142     }
00143 
00144     public function getDescription() {
00145         return array(
00146             'Undo the last edit to the page. If the last user who edited the page made',
00147             'multiple edits in a row, they will all be rolled back.'
00148         );
00149     }
00150 
00151     public function needsToken() {
00152         return 'rollback';
00153     }
00154 
00155     protected function getWebUITokenSalt( array $params ) {
00156         return array(
00157             $this->getRbTitle( $params )->getPrefixedText(),
00158             $this->getRbUser( $params )
00159         );
00160     }
00161 
00167     private function getRbUser( array $params ) {
00168         if ( $this->mUser !== null ) {
00169             return $this->mUser;
00170         }
00171 
00172         // We need to be able to revert IPs, but getCanonicalName rejects them
00173         $this->mUser = User::isIP( $params['user'] )
00174             ? $params['user']
00175             : User::getCanonicalName( $params['user'] );
00176         if ( !$this->mUser ) {
00177             $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
00178         }
00179 
00180         return $this->mUser;
00181     }
00182 
00188     private function getRbTitle( array $params ) {
00189         if ( $this->mTitleObj !== null ) {
00190             return $this->mTitleObj;
00191         }
00192 
00193         $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
00194 
00195         if ( isset( $params['title'] ) ) {
00196             $this->mTitleObj = Title::newFromText( $params['title'] );
00197             if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
00198                 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
00199             }
00200         } elseif ( isset( $params['pageid'] ) ) {
00201             $this->mTitleObj = Title::newFromID( $params['pageid'] );
00202             if ( !$this->mTitleObj ) {
00203                 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
00204             }
00205         }
00206 
00207         if ( !$this->mTitleObj->exists() ) {
00208             $this->dieUsageMsg( 'notanarticle' );
00209         }
00210 
00211         return $this->mTitleObj;
00212     }
00213 
00214     public function getExamples() {
00215         return array(
00216             'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
00217             'api.php?action=rollback&pageid=122&user=Catrope&token=123ABC',
00218             'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&' .
00219                 'token=123ABC&summary=Reverting%20vandalism&markbot=1'
00220         );
00221     }
00222 
00223     public function getHelpUrls() {
00224         return 'https://www.mediawiki.org/wiki/API:Rollback';
00225     }
00226 }