MediaWiki  REL1_20
ApiFileRevert.php
Go to the documentation of this file.
00001 <?php
00030 class ApiFileRevert extends ApiBase {
00031 
00035         protected $file;
00036         protected $archiveName;
00037 
00038         protected $params;
00039 
00040         public function __construct( $main, $action ) {
00041                 parent::__construct( $main, $action );
00042         }
00043 
00044         public function execute() {
00045                 $this->params = $this->extractRequestParams();
00046                 // Extract the file and archiveName from the request parameters
00047                 $this->validateParameters();
00048 
00049                 // Check whether we're allowed to revert this file
00050                 $this->checkPermissions( $this->getUser() );
00051 
00052                 $sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName );
00053                 $status = $this->file->upload( $sourceUrl, $this->params['comment'], $this->params['comment'] );
00054 
00055                 if ( $status->isGood() ) {
00056                         $result = array( 'result' => 'Success' );
00057                 } else {
00058                         $result = array(
00059                                 'result' => 'Failure',
00060                                 'errors' => $this->getResult()->convertStatusToArray( $status ),
00061                         );
00062                 }
00063 
00064                 $this->getResult()->addValue( null, $this->getModuleName(), $result );
00065 
00066         }
00067 
00073         protected function checkPermissions( $user ) {
00074                 $title = $this->file->getTitle();
00075                 $permissionErrors = array_merge(
00076                         $title->getUserPermissionsErrors( 'edit' , $user ),
00077                         $title->getUserPermissionsErrors( 'upload' , $user )
00078                 );
00079 
00080                 if ( $permissionErrors ) {
00081                         $this->dieUsageMsg( $permissionErrors[0] );
00082                 }
00083         }
00084 
00089         protected function validateParameters() {
00090                 // Validate the input title
00091                 $title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] );
00092                 if ( is_null( $title ) ) {
00093                         $this->dieUsageMsg( array( 'invalidtitle', $this->params['filename'] ) );
00094                 }
00095                 $localRepo = RepoGroup::singleton()->getLocalRepo();
00096 
00097                 // Check if the file really exists
00098                 $this->file = $localRepo->newFile( $title );
00099                 if ( !$this->file->exists() ) {
00100                         $this->dieUsageMsg( 'notanarticle' );
00101                 }
00102 
00103                 // Check if the archivename is valid for this file
00104                 $this->archiveName = $this->params['archivename'];
00105                 $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName );
00106                 if ( !$oldFile->exists() ) {
00107                         $this->dieUsageMsg( 'filerevert-badversion' );
00108                 }
00109         }
00110 
00111         public function mustBePosted() {
00112                 return true;
00113         }
00114 
00115         public function isWriteMode() {
00116                 return true;
00117         }
00118 
00119         public function getAllowedParams() {
00120                 return array(
00121                         'filename' => array(
00122                                 ApiBase::PARAM_TYPE => 'string',
00123                                 ApiBase::PARAM_REQUIRED => true,
00124                         ),
00125                         'comment' => array(
00126                                 ApiBase::PARAM_DFLT => '',
00127                         ),
00128                         'archivename' => array(
00129                                 ApiBase::PARAM_TYPE => 'string',
00130                                 ApiBase::PARAM_REQUIRED => true,
00131                         ),
00132                         'token' => array(
00133                                 ApiBase::PARAM_TYPE => 'string',
00134                                 ApiBase::PARAM_REQUIRED => true
00135                         ),
00136                 );
00137 
00138         }
00139 
00140         public function getParamDescription() {
00141                 return array(
00142                         'filename' => 'Target filename without the File: prefix',
00143                         'token' => 'Edit token. You can get one of these through prop=info',
00144                         'comment' => 'Upload comment',
00145                         'archivename' => 'Archive name of the revision to revert to',
00146                 );
00147         }
00148 
00149         public function getResultProperties() {
00150                 return array(
00151                         '' => array(
00152                                 'result' => array(
00153                                         ApiBase::PROP_TYPE => array(
00154                                                 'Success',
00155                                                 'Failure'
00156                                         )
00157                                 ),
00158                                 'errors' => array(
00159                                         ApiBase::PROP_TYPE => 'string',
00160                                         ApiBase::PROP_NULLABLE => true
00161                                 )
00162                         )
00163                 );
00164         }
00165 
00166         public function getDescription() {
00167                 return array(
00168                         'Revert a file to an old version'
00169                 );
00170         }
00171 
00172         public function getPossibleErrors() {
00173                 return array_merge( parent::getPossibleErrors(),
00174                         array(
00175                                 array( 'mustbeloggedin', 'upload' ),
00176                                 array( 'badaccess-groups' ),
00177                                 array( 'invalidtitle', 'title' ),
00178                                 array( 'notanarticle' ),
00179                                 array( 'filerevert-badversion' ),
00180                         )
00181                 );
00182         }
00183 
00184         public function needsToken() {
00185                 return true;
00186         }
00187 
00188         public function getTokenSalt() {
00189                 return '';
00190         }
00191 
00192         public function getExamples() {
00193                 return array(
00194                         'api.php?action=filerevert&filename=Wiki.png&comment=Revert&archivename=20110305152740!Wiki.png&token=+\\'
00195                                 => 'Revert Wiki.png to the version of 20110305152740',
00196                 );
00197         }
00198 
00199         public function getVersion() {
00200                 return __CLASS__ . ': $Id$';
00201         }
00202 }