4 * Created on January 3rd, 2013
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
24 class ApiImageRotate
extends ApiBase
{
25 private $mPageSet = null;
27 public function execute() {
28 $this->useTransactionalTimeLimit();
30 $params = $this->extractRequestParams();
31 $rotation = $params['rotation'];
33 $continuationManager = new ApiContinuationManager( $this, [], [] );
34 $this->setContinuationManager( $continuationManager );
36 $pageSet = $this->getPageSet();
41 $result = $pageSet->getInvalidTitlesAndRevisions( [
42 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
45 foreach ( $pageSet->getTitles() as $title ) {
47 $r['id'] = $title->getArticleID();
48 ApiQueryBase
::addTitleInfo( $r, $title );
49 if ( !$title->exists() ) {
51 if ( $title->isKnown() ) {
56 $file = wfFindFile( $title, [ 'latest' => true ] );
58 $r['result'] = 'Failure';
59 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
60 Status
::newFatal( 'apierror-filedoesnotexist' )
65 $handler = $file->getHandler();
66 if ( !$handler ||
!$handler->canRotate() ) {
67 $r['result'] = 'Failure';
68 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
69 Status
::newFatal( 'apierror-filetypecannotberotated' )
75 // Check whether we're allowed to rotate this file
76 $permError = $this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] );
78 $r['result'] = 'Failure';
79 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
80 $this->errorArrayToStatus( $permError )
86 $srcPath = $file->getLocalRefPath();
87 if ( $srcPath === false ) {
88 $r['result'] = 'Failure';
89 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
90 Status
::newFatal( 'apierror-filenopath' )
95 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION
) );
96 $tmpFile = TempFSFile
::factory( 'rotate_', $ext, wfTempDir() );
97 $dstPath = $tmpFile->getPath();
98 $err = $handler->rotate( $file, [
99 'srcPath' => $srcPath,
100 'dstPath' => $dstPath,
101 'rotation' => $rotation
104 $comment = wfMessage(
106 )->numParams( $rotation )->inContentLanguage()->text();
107 $status = $file->upload( $dstPath,
108 $comment, $comment, 0, false, false, $this->getUser() );
109 if ( $status->isGood() ) {
110 $r['result'] = 'Success';
112 $r['result'] = 'Failure';
113 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
116 $r['result'] = 'Failure';
117 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
118 Status
::newFatal( ApiMessage
::create( $err->getMsg() ) )
123 $apiResult = $this->getResult();
124 ApiResult
::setIndexedTagName( $result, 'page' );
125 $apiResult->addValue( null, $this->getModuleName(), $result );
127 $this->setContinuationManager( null );
128 $continuationManager->setContinuationIntoResult( $apiResult );
132 * Get a cached instance of an ApiPageSet object
135 private function getPageSet() {
136 if ( $this->mPageSet
=== null ) {
137 $this->mPageSet
= new ApiPageSet( $this, 0, NS_FILE
);
140 return $this->mPageSet
;
143 public function mustBePosted() {
147 public function isWriteMode() {
151 public function getAllowedParams( $flags = 0 ) {
154 ApiBase
::PARAM_TYPE
=> [ '90', '180', '270' ],
155 ApiBase
::PARAM_REQUIRED
=> true
158 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
162 $result +
= $this->getPageSet()->getFinalParams( $flags );
168 public function needsToken() {
172 protected function getExamplesMessages() {
174 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
175 => 'apihelp-imagerotate-example-simple',
176 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
177 'rotation=180&token=123ABC'
178 => 'apihelp-imagerotate-example-generator',