3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 class ApiImageRotate
extends ApiBase
{
22 private $mPageSet = null;
24 public function execute() {
25 $this->useTransactionalTimeLimit();
27 $params = $this->extractRequestParams();
28 $rotation = $params['rotation'];
30 $continuationManager = new ApiContinuationManager( $this, [], [] );
31 $this->setContinuationManager( $continuationManager );
33 $pageSet = $this->getPageSet();
36 $result = $pageSet->getInvalidTitlesAndRevisions( [
37 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
40 // Check if user can add tags
41 if ( $params['tags'] ) {
42 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $this->getUser() );
43 if ( !$ableToTag->isOK() ) {
44 $this->dieStatus( $ableToTag );
48 foreach ( $pageSet->getTitles() as $title ) {
50 $r['id'] = $title->getArticleID();
51 ApiQueryBase
::addTitleInfo( $r, $title );
52 if ( !$title->exists() ) {
54 if ( $title->isKnown() ) {
59 $file = wfFindFile( $title, [ 'latest' => true ] );
61 $r['result'] = 'Failure';
62 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
63 Status
::newFatal( 'apierror-filedoesnotexist' )
68 $handler = $file->getHandler();
69 if ( !$handler ||
!$handler->canRotate() ) {
70 $r['result'] = 'Failure';
71 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
72 Status
::newFatal( 'apierror-filetypecannotberotated' )
78 // Check whether we're allowed to rotate this file
79 $permError = $this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] );
81 $r['result'] = 'Failure';
82 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
83 $this->errorArrayToStatus( $permError )
89 $srcPath = $file->getLocalRefPath();
90 if ( $srcPath === false ) {
91 $r['result'] = 'Failure';
92 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
93 Status
::newFatal( 'apierror-filenopath' )
98 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION
) );
99 $tmpFile = TempFSFile
::factory( 'rotate_', $ext, wfTempDir() );
100 $dstPath = $tmpFile->getPath();
101 $err = $handler->rotate( $file, [
102 'srcPath' => $srcPath,
103 'dstPath' => $dstPath,
104 'rotation' => $rotation
107 $comment = wfMessage(
109 )->numParams( $rotation )->inContentLanguage()->text();
110 $status = $file->upload(
118 $params['tags'] ?
: []
120 if ( $status->isGood() ) {
121 $r['result'] = 'Success';
123 $r['result'] = 'Failure';
124 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
127 $r['result'] = 'Failure';
128 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
129 Status
::newFatal( ApiMessage
::create( $err->getMsg() ) )
134 $apiResult = $this->getResult();
135 ApiResult
::setIndexedTagName( $result, 'page' );
136 $apiResult->addValue( null, $this->getModuleName(), $result );
138 $this->setContinuationManager( null );
139 $continuationManager->setContinuationIntoResult( $apiResult );
143 * Get a cached instance of an ApiPageSet object
146 private function getPageSet() {
147 if ( $this->mPageSet
=== null ) {
148 $this->mPageSet
= new ApiPageSet( $this, 0, NS_FILE
);
151 return $this->mPageSet
;
154 public function mustBePosted() {
158 public function isWriteMode() {
162 public function getAllowedParams( $flags = 0 ) {
165 ApiBase
::PARAM_TYPE
=> [ '90', '180', '270' ],
166 ApiBase
::PARAM_REQUIRED
=> true
169 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
172 ApiBase
::PARAM_TYPE
=> 'tags',
173 ApiBase
::PARAM_ISMULTI
=> true,
177 $result +
= $this->getPageSet()->getFinalParams( $flags );
183 public function needsToken() {
187 protected function getExamplesMessages() {
189 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
190 => 'apihelp-imagerotate-example-simple',
191 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
192 'rotation=180&token=123ABC'
193 => 'apihelp-imagerotate-example-generator',