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