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 use MediaWiki\MediaWikiServices
;
26 class ApiImageRotate
extends ApiBase
{
27 private $mPageSet = null;
29 public function execute() {
30 $this->useTransactionalTimeLimit();
32 $params = $this->extractRequestParams();
33 $rotation = $params['rotation'];
35 $continuationManager = new ApiContinuationManager( $this, [], [] );
36 $this->setContinuationManager( $continuationManager );
38 $pageSet = $this->getPageSet();
41 $result = $pageSet->getInvalidTitlesAndRevisions( [
42 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
45 // Check if user can add tags
46 if ( $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 = MediaWikiServices
::getInstance()->getRepoGroup()->findFile(
65 $title, [ 'latest' => true ]
68 $r['result'] = 'Failure';
69 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
70 Status
::newFatal( 'apierror-filedoesnotexist' )
75 $handler = $file->getHandler();
76 if ( !$handler ||
!$handler->canRotate() ) {
77 $r['result'] = 'Failure';
78 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
79 Status
::newFatal( 'apierror-filetypecannotberotated' )
85 // Check whether we're allowed to rotate this file
86 $permError = $this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] );
88 $r['result'] = 'Failure';
89 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
90 $this->errorArrayToStatus( $permError )
96 $srcPath = $file->getLocalRefPath();
97 if ( $srcPath === false ) {
98 $r['result'] = 'Failure';
99 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
100 Status
::newFatal( 'apierror-filenopath' )
105 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION
) );
106 $tmpFile = MediaWikiServices
::getInstance()->getTempFSFileFactory()
107 ->newTempFSFile( 'rotate_', $ext );
108 $dstPath = $tmpFile->getPath();
109 // @phan-suppress-next-line PhanUndeclaredMethod
110 $err = $handler->rotate( $file, [
111 'srcPath' => $srcPath,
112 'dstPath' => $dstPath,
113 'rotation' => $rotation
116 $comment = wfMessage(
118 )->numParams( $rotation )->inContentLanguage()->text();
119 // @phan-suppress-next-line PhanUndeclaredMethod
120 $status = $file->upload(
128 $params['tags'] ?
: []
130 if ( $status->isGood() ) {
131 $r['result'] = 'Success';
133 $r['result'] = 'Failure';
134 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
137 $r['result'] = 'Failure';
138 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
139 Status
::newFatal( ApiMessage
::create( $err->getMsg() ) )
144 $apiResult = $this->getResult();
145 ApiResult
::setIndexedTagName( $result, 'page' );
146 $apiResult->addValue( null, $this->getModuleName(), $result );
148 $this->setContinuationManager( null );
149 $continuationManager->setContinuationIntoResult( $apiResult );
153 * Get a cached instance of an ApiPageSet object
156 private function getPageSet() {
157 if ( $this->mPageSet
=== null ) {
158 $this->mPageSet
= new ApiPageSet( $this, 0, NS_FILE
);
161 return $this->mPageSet
;
164 public function mustBePosted() {
168 public function isWriteMode() {
172 public function getAllowedParams( $flags = 0 ) {
175 ApiBase
::PARAM_TYPE
=> [ '90', '180', '270' ],
176 ApiBase
::PARAM_REQUIRED
=> true
179 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
182 ApiBase
::PARAM_TYPE
=> 'tags',
183 ApiBase
::PARAM_ISMULTI
=> true,
187 $result +
= $this->getPageSet()->getFinalParams( $flags );
193 public function needsToken() {
197 protected function getExamplesMessages() {
199 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
200 => 'apihelp-imagerotate-example-simple',
201 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
202 'rotation=180&token=123ABC'
203 => 'apihelp-imagerotate-example-generator',