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 // Check if user can add tags
46 if ( count( $params['tags'] ) ) {
47 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $this->getUser() );
48 if ( !$ableToTag->isOK() ) {
49 $this->dieStatus( $ableToTag );
53 foreach ( $pageSet->getTitles() as $title ) {
55 $r['id'] = $title->getArticleID();
56 ApiQueryBase
::addTitleInfo( $r, $title );
57 if ( !$title->exists() ) {
59 if ( $title->isKnown() ) {
64 $file = wfFindFile( $title, [ 'latest' => true ] );
66 $r['result'] = 'Failure';
67 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
68 Status
::newFatal( 'apierror-filedoesnotexist' )
73 $handler = $file->getHandler();
74 if ( !$handler ||
!$handler->canRotate() ) {
75 $r['result'] = 'Failure';
76 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
77 Status
::newFatal( 'apierror-filetypecannotberotated' )
83 // Check whether we're allowed to rotate this file
84 $permError = $this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] );
86 $r['result'] = 'Failure';
87 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
88 $this->errorArrayToStatus( $permError )
94 $srcPath = $file->getLocalRefPath();
95 if ( $srcPath === false ) {
96 $r['result'] = 'Failure';
97 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
98 Status
::newFatal( 'apierror-filenopath' )
103 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION
) );
104 $tmpFile = TempFSFile
::factory( 'rotate_', $ext, wfTempDir() );
105 $dstPath = $tmpFile->getPath();
106 $err = $handler->rotate( $file, [
107 'srcPath' => $srcPath,
108 'dstPath' => $dstPath,
109 'rotation' => $rotation
112 $comment = wfMessage(
114 )->numParams( $rotation )->inContentLanguage()->text();
115 $status = $file->upload(
123 $params['tags'] ?
: []
125 if ( $status->isGood() ) {
126 $r['result'] = 'Success';
128 $r['result'] = 'Failure';
129 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
132 $r['result'] = 'Failure';
133 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
134 Status
::newFatal( ApiMessage
::create( $err->getMsg() ) )
139 $apiResult = $this->getResult();
140 ApiResult
::setIndexedTagName( $result, 'page' );
141 $apiResult->addValue( null, $this->getModuleName(), $result );
143 $this->setContinuationManager( null );
144 $continuationManager->setContinuationIntoResult( $apiResult );
148 * Get a cached instance of an ApiPageSet object
151 private function getPageSet() {
152 if ( $this->mPageSet
=== null ) {
153 $this->mPageSet
= new ApiPageSet( $this, 0, NS_FILE
);
156 return $this->mPageSet
;
159 public function mustBePosted() {
163 public function isWriteMode() {
167 public function getAllowedParams( $flags = 0 ) {
170 ApiBase
::PARAM_TYPE
=> [ '90', '180', '270' ],
171 ApiBase
::PARAM_REQUIRED
=> true
174 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
177 ApiBase
::PARAM_TYPE
=> 'tags',
178 ApiBase
::PARAM_ISMULTI
=> true,
182 $result +
= $this->getPageSet()->getFinalParams( $flags );
188 public function needsToken() {
192 protected function getExamplesMessages() {
194 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
195 => 'apihelp-imagerotate-example-simple',
196 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
197 'rotation=180&token=123ABC'
198 => 'apihelp-imagerotate-example-generator',