Merge "Use getFileContents() instead of incorrect file system call to fetch file...
[lhc/web/wiklou.git] / includes / api / ApiImageRotate.php
index f9c8058..7a60e83 100644 (file)
@@ -22,7 +22,6 @@
  */
 
 class ApiImageRotate extends ApiBase {
-
        private $mPageSet = null;
 
        public function __construct( $main, $action ) {
@@ -38,77 +37,87 @@ class ApiImageRotate extends ApiBase {
         */
        private static function addValues( array &$result, $values, $flag = null, $name = null ) {
                foreach ( $values as $val ) {
-                       if( $val instanceof Title ) {
+                       if ( $val instanceof Title ) {
                                $v = array();
                                ApiQueryBase::addTitleInfo( $v, $val );
-                       } elseif( $name !== null ) {
+                       } elseif ( $name !== null ) {
                                $v = array( $name => $val );
                        } else {
                                $v = $val;
                        }
-                       if( $flag !== null ) {
+                       if ( $flag !== null ) {
                                $v[$flag] = '';
                        }
                        $result[] = $v;
                }
        }
 
-
        public function execute() {
                $params = $this->extractRequestParams();
-               $rotation = $params[ 'rotation' ];
-               $user = $this->getUser();
-
-               if( is_null( $rotation ) || $rotation % 90 ) {
-                       $this->dieUsage( "Rotation: {$rotation}", 'rotation must be multiple of 90 degrees' );
-               }
+               $rotation = $params['rotation'];
 
                $pageSet = $this->getPageSet();
                $pageSet->execute();
 
-               $result = array();
                $result = array();
 
                self::addValues( $result, $pageSet->getInvalidTitles(), 'invalid', 'title' );
                self::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' );
                self::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' );
                self::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' );
-               self::addValues( $result, $pageSet->getMissingTitles(), 'missing' );
                self::addValues( $result, $pageSet->getInterwikiTitlesAsResult() );
 
                foreach ( $pageSet->getTitles() as $title ) {
-                       $file = wfFindFile( $title );
-
                        $r = array();
-                       $r[ 'title' ] = $title->getFullText();
-                       if ( !$file ) {
+                       $r['id'] = $title->getArticleID();
+                       ApiQueryBase::addTitleInfo( $r, $title );
+                       if ( !$title->exists() ) {
                                $r['missing'] = '';
+                       }
+
+                       $file = wfFindFile( $title );
+                       if ( !$file ) {
                                $r['result'] = 'Failure';
+                               $r['errormessage'] = 'File does not exist';
                                $result[] = $r;
                                continue;
                        }
                        $handler = $file->getHandler();
                        if ( !$handler || !$handler->canRotate() ) {
-                               $r['invalid'] = '';
                                $r['result'] = 'Failure';
+                               $r['errormessage'] = 'File type cannot be rotated';
                                $result[] = $r;
                                continue;
                        }
 
                        // Check whether we're allowed to rotate this file
-                       $this->checkPermissions( $this->getUser(), $file->getTitle() );
+                       $permError = $this->checkPermissions( $this->getUser(), $file->getTitle() );
+                       if ( $permError !== null ) {
+                               $r['result'] = 'Failure';
+                               $r['errormessage'] = $permError;
+                               $result[] = $r;
+                               continue;
+                       }
 
                        $srcPath = $file->getLocalRefPath();
+                       if ( $srcPath === false ) {
+                               $r['result'] = 'Failure';
+                               $r['errormessage'] = 'Cannot get local file path';
+                               $result[] = $r;
+                               continue;
+                       }
                        $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
-                       $tmpFile = TempFSFile::factory( 'rotate_', $ext);
+                       $tmpFile = TempFSFile::factory( 'rotate_', $ext );
                        $dstPath = $tmpFile->getPath();
                        $err = $handler->rotate( $file, array(
                                "srcPath" => $srcPath,
                                "dstPath" => $dstPath,
-                               "rotation"=> $rotation
+                               "rotation" => $rotation
                        ) );
                        if ( !$err ) {
-                               $comment = wfMessage( 'rotate-comment' )->numParams( $rotation )->text();
+                               $comment = wfMessage(
+                                       'rotate-comment'
+                               )->numParams( $rotation )->inContentLanguage()->text();
                                $status = $file->upload( $dstPath,
                                        $comment, $comment, 0, false, false, $this->getUser() );
                                if ( $status->isGood() ) {
@@ -141,18 +150,23 @@ class ApiImageRotate extends ApiBase {
 
        /**
         * Checks that the user has permissions to perform rotations.
-        * Dies with usage message on inadequate permissions.
-        * @param $user User The user to check.
+        * @param User $user The user to check
+        * @param Title $title
+        * @return string|null Permission error message, or null if there is no error
         */
        protected function checkPermissions( $user, $title ) {
                $permissionErrors = array_merge(
-                       $title->getUserPermissionsErrors( 'edit' , $user ),
-                       $title->getUserPermissionsErrors( 'upload' , $user )
+                       $title->getUserPermissionsErrors( 'edit', $user ),
+                       $title->getUserPermissionsErrors( 'upload', $user )
                );
 
                if ( $permissionErrors ) {
-                       $this->dieUsageMsg( $permissionErrors[0] );
+                       // Just return the first error
+                       $msg = $this->parseMsg( $permissionErrors[0] );
+                       return $msg['info'];
                }
+
+               return null;
        }
 
        public function mustBePosted() {
@@ -164,10 +178,10 @@ class ApiImageRotate extends ApiBase {
        }
 
        public function getAllowedParams( $flags = 0 ) {
-               $pageSet = $this->getPageSet();
                $result = array(
                        'rotation' => array(
-                               ApiBase::PARAM_DFLT => 0,
+                               ApiBase::PARAM_TYPE => array( '90', '180', '270' ),
+                               ApiBase::PARAM_REQUIRED => true
                        ),
                        'token' => array(
                                ApiBase::PARAM_TYPE => 'string',
@@ -182,9 +196,9 @@ class ApiImageRotate extends ApiBase {
 
        public function getParamDescription() {
                $pageSet = $this->getPageSet();
-               return $pageSet->getParamDescription() + array(
-                       'rotation' => 'Degrees to rotate image, values can be 0, 90, 180 or 270',
-                       'token' => 'Edit token. You can get one of these through prop=info',
+               return $pageSet->getFinalParamDescription() + array(
+                       'rotation' => 'Degrees to rotate image clockwise',
+                       'token' => 'Edit token. You can get one of these through action=tokens',
                );
        }
 
@@ -204,13 +218,13 @@ class ApiImageRotate extends ApiBase {
                $pageSet = $this->getPageSet();
                return array_merge(
                        parent::getPossibleErrors(),
-                       $pageSet->getPossibleErrors()
+                       $pageSet->getFinalPossibleErrors()
                );
        }
 
        public function getExamples() {
                return array(
-                       'api.php?action=imagerotate&titles=Example.jpg&rotation=90&token=+\\',
+                       'api.php?action=imagerotate&titles=Example.jpg&rotation=90&token=123ABC',
                );
        }
 }