MediaWiki  REL1_24
ApiImageRotate.php
Go to the documentation of this file.
00001 <?php
00024 class ApiImageRotate extends ApiBase {
00025     private $mPageSet = null;
00026 
00034     private static function addValues( array &$result, $values, $flag = null, $name = null ) {
00035         foreach ( $values as $val ) {
00036             if ( $val instanceof Title ) {
00037                 $v = array();
00038                 ApiQueryBase::addTitleInfo( $v, $val );
00039             } elseif ( $name !== null ) {
00040                 $v = array( $name => $val );
00041             } else {
00042                 $v = $val;
00043             }
00044             if ( $flag !== null ) {
00045                 $v[$flag] = '';
00046             }
00047             $result[] = $v;
00048         }
00049     }
00050 
00051     public function execute() {
00052         $params = $this->extractRequestParams();
00053         $rotation = $params['rotation'];
00054 
00055         $this->getResult()->beginContinuation( $params['continue'], array(), array() );
00056 
00057         $pageSet = $this->getPageSet();
00058         $pageSet->execute();
00059 
00060         $result = array();
00061 
00062         self::addValues( $result, $pageSet->getInvalidTitles(), 'invalid', 'title' );
00063         self::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' );
00064         self::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' );
00065         self::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' );
00066         self::addValues( $result, $pageSet->getInterwikiTitlesAsResult() );
00067 
00068         foreach ( $pageSet->getTitles() as $title ) {
00069             $r = array();
00070             $r['id'] = $title->getArticleID();
00071             ApiQueryBase::addTitleInfo( $r, $title );
00072             if ( !$title->exists() ) {
00073                 $r['missing'] = '';
00074             }
00075 
00076             $file = wfFindFile( $title );
00077             if ( !$file ) {
00078                 $r['result'] = 'Failure';
00079                 $r['errormessage'] = 'File does not exist';
00080                 $result[] = $r;
00081                 continue;
00082             }
00083             $handler = $file->getHandler();
00084             if ( !$handler || !$handler->canRotate() ) {
00085                 $r['result'] = 'Failure';
00086                 $r['errormessage'] = 'File type cannot be rotated';
00087                 $result[] = $r;
00088                 continue;
00089             }
00090 
00091             // Check whether we're allowed to rotate this file
00092             $permError = $this->checkPermissions( $this->getUser(), $file->getTitle() );
00093             if ( $permError !== null ) {
00094                 $r['result'] = 'Failure';
00095                 $r['errormessage'] = $permError;
00096                 $result[] = $r;
00097                 continue;
00098             }
00099 
00100             $srcPath = $file->getLocalRefPath();
00101             if ( $srcPath === false ) {
00102                 $r['result'] = 'Failure';
00103                 $r['errormessage'] = 'Cannot get local file path';
00104                 $result[] = $r;
00105                 continue;
00106             }
00107             $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
00108             $tmpFile = TempFSFile::factory( 'rotate_', $ext );
00109             $dstPath = $tmpFile->getPath();
00110             $err = $handler->rotate( $file, array(
00111                 "srcPath" => $srcPath,
00112                 "dstPath" => $dstPath,
00113                 "rotation" => $rotation
00114             ) );
00115             if ( !$err ) {
00116                 $comment = wfMessage(
00117                     'rotate-comment'
00118                 )->numParams( $rotation )->inContentLanguage()->text();
00119                 $status = $file->upload( $dstPath,
00120                     $comment, $comment, 0, false, false, $this->getUser() );
00121                 if ( $status->isGood() ) {
00122                     $r['result'] = 'Success';
00123                 } else {
00124                     $r['result'] = 'Failure';
00125                     $r['errormessage'] = $this->getResult()->convertStatusToArray( $status );
00126                 }
00127             } else {
00128                 $r['result'] = 'Failure';
00129                 $r['errormessage'] = $err->toText();
00130             }
00131             $result[] = $r;
00132         }
00133         $apiResult = $this->getResult();
00134         $apiResult->setIndexedTagName( $result, 'page' );
00135         $apiResult->addValue( null, $this->getModuleName(), $result );
00136         $apiResult->endContinuation();
00137     }
00138 
00143     private function getPageSet() {
00144         if ( $this->mPageSet === null ) {
00145             $this->mPageSet = new ApiPageSet( $this, 0, NS_FILE );
00146         }
00147 
00148         return $this->mPageSet;
00149     }
00150 
00157     protected function checkPermissions( $user, $title ) {
00158         $permissionErrors = array_merge(
00159             $title->getUserPermissionsErrors( 'edit', $user ),
00160             $title->getUserPermissionsErrors( 'upload', $user )
00161         );
00162 
00163         if ( $permissionErrors ) {
00164             // Just return the first error
00165             $msg = $this->parseMsg( $permissionErrors[0] );
00166 
00167             return $msg['info'];
00168         }
00169 
00170         return null;
00171     }
00172 
00173     public function mustBePosted() {
00174         return true;
00175     }
00176 
00177     public function isWriteMode() {
00178         return true;
00179     }
00180 
00181     public function getAllowedParams( $flags = 0 ) {
00182         $result = array(
00183             'rotation' => array(
00184                 ApiBase::PARAM_TYPE => array( '90', '180', '270' ),
00185                 ApiBase::PARAM_REQUIRED => true
00186             ),
00187             'continue' => '',
00188         );
00189         if ( $flags ) {
00190             $result += $this->getPageSet()->getFinalParams( $flags );
00191         }
00192 
00193         return $result;
00194     }
00195 
00196     public function getParamDescription() {
00197         $pageSet = $this->getPageSet();
00198 
00199         return $pageSet->getFinalParamDescription() + array(
00200             'rotation' => 'Degrees to rotate image clockwise',
00201             'continue' => 'When more results are available, use this to continue',
00202         );
00203     }
00204 
00205     public function getDescription() {
00206         return 'Rotate one or more images.';
00207     }
00208 
00209     public function needsToken() {
00210         return 'csrf';
00211     }
00212 
00213     public function getExamples() {
00214         return array(
00215             'api.php?action=imagerotate&titles=Example.jpg&rotation=90&token=123ABC',
00216         );
00217     }
00218 }