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