Merge "(bug 38152) jquery.tablesorter: Use .data() instead of .attr()"
[lhc/web/wiklou.git] / includes / api / ApiFileRevert.php
index 2cf3b90..7cac0eb 100644 (file)
  * @file
  */
 
-if ( !defined( 'MEDIAWIKI' ) ) {
-       // Eclipse helper - will be ignored in production
-       require_once( "ApiBase.php" );
-}
-
 /**
  * @ingroup API
  */
@@ -47,14 +42,12 @@ class ApiFileRevert extends ApiBase {
        }
 
        public function execute() {
-               global $wgUser;
-
-               // First check permission to upload/revert
-               $this->checkPermissions( $wgUser );
-
                $this->params = $this->extractRequestParams();
+               // Extract the file and archiveName from the request parameters
                $this->validateParameters();
 
+               // Check whether we're allowed to revert this file
+               $this->checkPermissions( $this->getUser() );
 
                $sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName );
                $status = $this->file->upload( $sourceUrl, $this->params['comment'], $this->params['comment'] );
@@ -62,13 +55,13 @@ class ApiFileRevert extends ApiBase {
                if ( $status->isGood() ) {
                        $result = array( 'result' => 'Success' );
                } else {
-                       $result = array( 
-                               'result' => 'Failure', 
+                       $result = array(
+                               'result' => 'Failure',
                                'errors' => $this->getResult()->convertStatusToArray( $status ),
                        );
                }
 
-               $this->getResult()->addValue( null, $this->getModuleName(), $result );  
+               $this->getResult()->addValue( null, $this->getModuleName(), $result );
 
        }
 
@@ -78,14 +71,13 @@ class ApiFileRevert extends ApiBase {
         * @param $user User The user to check.
         */
        protected function checkPermissions( $user ) {
-               $permission = $user->isAllowedAll( 'edit', 'upload' );
-
-               if ( $permission !== true ) {
-                       if ( !$user->isLoggedIn() ) {
-                               $this->dieUsageMsg( array( 'mustbeloggedin', 'upload' ) );
-                       } else {
-                               $this->dieUsageMsg( array( 'badaccess-groups' ) );
-                       }
+               $permissionErrors = array_merge(
+                       $this->file->getTitle()->getUserPermissionsErrors( 'edit' , $user ),
+                       $this->file->getTitle()->getUserPermissionsErrors( 'upload' , $user )
+               );
+
+               if ( $permissionErrors ) {
+                       $this->dieUsageMsg( $permissionErrors[0] );
                }
        }
 
@@ -102,14 +94,14 @@ class ApiFileRevert extends ApiBase {
                // Check if the file really exists
                $this->file = wfLocalFile( $title );
                if ( !$this->file->exists() ) {
-                       $this->dieUsageMsg( array( 'notanarticle' ) );
+                       $this->dieUsageMsg( 'notanarticle' );
                }
 
                // Check if the archivename is valid for this file
                $this->archiveName = $this->params['archivename'];
                $oldFile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $this->archiveName );
                if ( !$oldFile->exists() ) {
-                       $this->dieUsageMsg( array( 'filerevert-badversion' ) );
+                       $this->dieUsageMsg( 'filerevert-badversion' );
                }
        }
 
@@ -132,7 +124,7 @@ class ApiFileRevert extends ApiBase {
                        ),
                        'archivename' => array(
                                ApiBase::PARAM_TYPE => 'string',
-                               ApiBase::PARAM_REQUIRED => true,                        
+                               ApiBase::PARAM_REQUIRED => true,
                        ),
                        'token' => null,
                );
@@ -140,15 +132,29 @@ class ApiFileRevert extends ApiBase {
        }
 
        public function getParamDescription() {
-               $params = array(
-                       'filename' => 'Target filename',
+               return array(
+                       'filename' => 'Target filename without the File: prefix',
                        'token' => 'Edit token. You can get one of these through prop=info',
                        'comment' => 'Upload comment',
                        'archivename' => 'Archive name of the revision to revert to',
                );
+       }
 
-               return $params;
-
+       public function getResultProperties() {
+               return array(
+                       '' => array(
+                               'result' => array(
+                                       ApiBase::PROP_TYPE => array(
+                                               'Success',
+                                               'Failure'
+                                       )
+                               ),
+                               'errors' => array(
+                                       ApiBase::PROP_TYPE => 'string',
+                                       ApiBase::PROP_NULLABLE => true
+                               )
+                       )
+               );
        }
 
        public function getDescription() {
@@ -177,10 +183,10 @@ class ApiFileRevert extends ApiBase {
                return '';
        }
 
-       protected function getExamples() {
+       public function getExamples() {
                return array(
-                       'Revert Wiki.png to the version of 20110305152740:',
-                       '    api.php?action=filerevert&filename=Wiki.png&comment=Revert&archivename=20110305152740!Wiki.png&token=+\\',
+                       'api.php?action=filerevert&filename=Wiki.png&comment=Revert&archivename=20110305152740!Wiki.png&token=+\\'
+                               => 'Revert Wiki.png to the version of 20110305152740',
                );
        }