MediaWiki  REL1_24
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         );
00136     }
00137 
00138     public function getParamDescription() {
00139         return array(
00140             'filename' => 'Target filename without the File: prefix',
00141             'comment' => 'Upload comment',
00142             'archivename' => 'Archive name of the revision to revert to',
00143         );
00144     }
00145 
00146     public function getDescription() {
00147         return array(
00148             'Revert a file to an old version.'
00149         );
00150     }
00151 
00152     public function needsToken() {
00153         return 'csrf';
00154     }
00155 
00156     public function getExamples() {
00157         return array(
00158             'api.php?action=filerevert&filename=Wiki.png&comment=Revert&' .
00159                 'archivename=20110305152740!Wiki.png&token=123ABC'
00160                 => 'Revert Wiki.png to the version of 20110305152740',
00161         );
00162     }
00163 }