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();
38 $result = $pageSet->getInvalidTitlesAndRevisions( [
39 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
42 // Check if user can add tags
43 if ( $params['tags'] ) {
44 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $this->getUser() );
45 if ( !$ableToTag->isOK() ) {
46 $this->dieStatus( $ableToTag );
50 foreach ( $pageSet->getTitles() as $title ) {
52 $r['id'] = $title->getArticleID();
53 ApiQueryBase
::addTitleInfo( $r, $title );
54 if ( !$title->exists() ) {
56 if ( $title->isKnown() ) {
61 $file = wfFindFile( $title, [ 'latest' => true ] );
63 $r['result'] = 'Failure';
64 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
65 Status
::newFatal( 'apierror-filedoesnotexist' )
70 $handler = $file->getHandler();
71 if ( !$handler ||
!$handler->canRotate() ) {
72 $r['result'] = 'Failure';
73 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
74 Status
::newFatal( 'apierror-filetypecannotberotated' )
80 // Check whether we're allowed to rotate this file
81 $permError = $this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] );
83 $r['result'] = 'Failure';
84 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
85 $this->errorArrayToStatus( $permError )
91 $srcPath = $file->getLocalRefPath();
92 if ( $srcPath === false ) {
93 $r['result'] = 'Failure';
94 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
95 Status
::newFatal( 'apierror-filenopath' )
100 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION
) );
101 $tmpFile = TempFSFile
::factory( 'rotate_', $ext, wfTempDir() );
102 $dstPath = $tmpFile->getPath();
103 $err = $handler->rotate( $file, [
104 'srcPath' => $srcPath,
105 'dstPath' => $dstPath,
106 'rotation' => $rotation
109 $comment = wfMessage(
111 )->numParams( $rotation )->inContentLanguage()->text();
112 $status = $file->upload(
120 $params['tags'] ?
: []
122 if ( $status->isGood() ) {
123 $r['result'] = 'Success';
125 $r['result'] = 'Failure';
126 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
129 $r['result'] = 'Failure';
130 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
131 Status
::newFatal( ApiMessage
::create( $err->getMsg() ) )
136 $apiResult = $this->getResult();
137 ApiResult
::setIndexedTagName( $result, 'page' );
138 $apiResult->addValue( null, $this->getModuleName(), $result );
140 $this->setContinuationManager( null );
141 $continuationManager->setContinuationIntoResult( $apiResult );
145 * Get a cached instance of an ApiPageSet object
148 private function getPageSet() {
149 if ( $this->mPageSet
=== null ) {
150 $this->mPageSet
= new ApiPageSet( $this, 0, NS_FILE
);
153 return $this->mPageSet
;
156 public function mustBePosted() {
160 public function isWriteMode() {
164 public function getAllowedParams( $flags = 0 ) {
167 ApiBase
::PARAM_TYPE
=> [ '90', '180', '270' ],
168 ApiBase
::PARAM_REQUIRED
=> true
171 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
174 ApiBase
::PARAM_TYPE
=> 'tags',
175 ApiBase
::PARAM_ISMULTI
=> true,
179 $result +
= $this->getPageSet()->getFinalParams( $flags );
185 public function needsToken() {
189 protected function getExamplesMessages() {
191 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
192 => 'apihelp-imagerotate-example-simple',
193 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
194 'rotation=180&token=123ABC'
195 => 'apihelp-imagerotate-example-generator',