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