MediaWiki
REL1_19
|
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 $permissionErrors = array_merge( 00075 $this->file->getTitle()->getUserPermissionsErrors( 'edit' , $user ), 00076 $this->file->getTitle()->getUserPermissionsErrors( 'upload' , $user ) 00077 ); 00078 00079 if ( $permissionErrors ) { 00080 $this->dieUsageMsg( $permissionErrors[0] ); 00081 } 00082 } 00083 00088 protected function validateParameters() { 00089 // Validate the input title 00090 $title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] ); 00091 if ( is_null( $title ) ) { 00092 $this->dieUsageMsg( array( 'invalidtitle', $this->params['filename'] ) ); 00093 } 00094 // Check if the file really exists 00095 $this->file = wfLocalFile( $title ); 00096 if ( !$this->file->exists() ) { 00097 $this->dieUsageMsg( 'notanarticle' ); 00098 } 00099 00100 // Check if the archivename is valid for this file 00101 $this->archiveName = $this->params['archivename']; 00102 $oldFile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $this->archiveName ); 00103 if ( !$oldFile->exists() ) { 00104 $this->dieUsageMsg( 'filerevert-badversion' ); 00105 } 00106 } 00107 00108 public function mustBePosted() { 00109 return true; 00110 } 00111 00112 public function isWriteMode() { 00113 return true; 00114 } 00115 00116 public function getAllowedParams() { 00117 return array( 00118 'filename' => array( 00119 ApiBase::PARAM_TYPE => 'string', 00120 ApiBase::PARAM_REQUIRED => true, 00121 ), 00122 'comment' => array( 00123 ApiBase::PARAM_DFLT => '', 00124 ), 00125 'archivename' => array( 00126 ApiBase::PARAM_TYPE => 'string', 00127 ApiBase::PARAM_REQUIRED => true, 00128 ), 00129 'token' => null, 00130 ); 00131 00132 } 00133 00134 public function getParamDescription() { 00135 $params = array( 00136 'filename' => 'Target filename', 00137 'token' => 'Edit token. You can get one of these through prop=info', 00138 'comment' => 'Upload comment', 00139 'archivename' => 'Archive name of the revision to revert to', 00140 ); 00141 00142 return $params; 00143 00144 } 00145 00146 public function getDescription() { 00147 return array( 00148 'Revert a file to an old version' 00149 ); 00150 } 00151 00152 public function getPossibleErrors() { 00153 return array_merge( parent::getPossibleErrors(), 00154 array( 00155 array( 'mustbeloggedin', 'upload' ), 00156 array( 'badaccess-groups' ), 00157 array( 'invalidtitle', 'title' ), 00158 array( 'notanarticle' ), 00159 array( 'filerevert-badversion' ), 00160 ) 00161 ); 00162 } 00163 00164 public function needsToken() { 00165 return true; 00166 } 00167 00168 public function getTokenSalt() { 00169 return ''; 00170 } 00171 00172 public function getExamples() { 00173 return array( 00174 'api.php?action=filerevert&filename=Wiki.png&comment=Revert&archivename=20110305152740!Wiki.png&token=+\\' 00175 => 'Revert Wiki.png to the version of 20110305152740', 00176 ); 00177 } 00178 00179 public function getVersion() { 00180 return __CLASS__ . ': $Id$'; 00181 } 00182 }