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 namespace MediaWiki\Api
;
24 use MediaWiki\FileBackend\FSFile\TempFSFileFactory
;
25 use MediaWiki\Status\Status
;
26 use MediaWiki\Title\TitleFactory
;
28 use Wikimedia\ParamValidator\ParamValidator
;
33 class ApiImageRotate
extends ApiBase
{
34 /** @var ApiPageSet|null */
35 private $mPageSet = null;
37 private RepoGroup
$repoGroup;
38 private TempFSFileFactory
$tempFSFileFactory;
39 private TitleFactory
$titleFactory;
41 public function __construct(
45 TempFSFileFactory
$tempFSFileFactory,
46 TitleFactory
$titleFactory
48 parent
::__construct( $mainModule, $moduleName );
49 $this->repoGroup
= $repoGroup;
50 $this->tempFSFileFactory
= $tempFSFileFactory;
51 $this->titleFactory
= $titleFactory;
54 public function execute() {
55 $this->useTransactionalTimeLimit();
57 $params = $this->extractRequestParams();
58 $rotation = $params['rotation'];
60 $continuationManager = new ApiContinuationManager( $this, [], [] );
61 $this->setContinuationManager( $continuationManager );
63 $pageSet = $this->getPageSet();
66 $result = $pageSet->getInvalidTitlesAndRevisions( [
67 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
70 // Check if user can add tags
71 if ( $params['tags'] ) {
72 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
73 if ( !$ableToTag->isOK() ) {
74 $this->dieStatus( $ableToTag );
78 foreach ( $pageSet->getPages() as $page ) {
79 $title = $this->titleFactory
->newFromPageIdentity( $page );
81 $r['id'] = $title->getArticleID();
82 ApiQueryBase
::addTitleInfo( $r, $title );
83 if ( !$title->exists() ) {
85 if ( $title->isKnown() ) {
90 $file = $this->repoGroup
->findFile( $title, [ 'latest' => true ] );
92 $r['result'] = 'Failure';
93 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
94 Status
::newFatal( 'apierror-filedoesnotexist' )
99 $handler = $file->getHandler();
100 if ( !$handler ||
!$handler->canRotate() ) {
101 $r['result'] = 'Failure';
102 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
103 Status
::newFatal( 'apierror-filetypecannotberotated' )
109 // Check whether we're allowed to rotate this file
110 $this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] );
112 $srcPath = $file->getLocalRefPath();
113 if ( $srcPath === false ) {
114 $r['result'] = 'Failure';
115 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
116 Status
::newFatal( 'apierror-filenopath' )
121 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION
) );
122 $tmpFile = $this->tempFSFileFactory
->newTempFSFile( 'rotate_', $ext );
123 $dstPath = $tmpFile->getPath();
124 // @phan-suppress-next-line PhanUndeclaredMethod
125 $err = $handler->rotate( $file, [
126 'srcPath' => $srcPath,
127 'dstPath' => $dstPath,
128 'rotation' => $rotation
131 $comment = $this->msg(
133 )->numParams( $rotation )->inContentLanguage()->text();
134 // @phan-suppress-next-line PhanUndeclaredMethod
135 $status = $file->upload(
142 $this->getAuthority(),
143 $params['tags'] ?
: []
145 if ( $status->isGood() ) {
146 $r['result'] = 'Success';
148 $r['result'] = 'Failure';
149 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
152 $r['result'] = 'Failure';
153 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
154 Status
::newFatal( ApiMessage
::create( $err->getMsg() ) )
159 $apiResult = $this->getResult();
160 ApiResult
::setIndexedTagName( $result, 'page' );
161 $apiResult->addValue( null, $this->getModuleName(), $result );
163 $this->setContinuationManager( null );
164 $continuationManager->setContinuationIntoResult( $apiResult );
168 * Get a cached instance of an ApiPageSet object
171 private function getPageSet() {
172 $this->mPageSet ??
= new ApiPageSet( $this, 0, NS_FILE
);
174 return $this->mPageSet
;
177 public function mustBePosted() {
181 public function isWriteMode() {
185 public function getAllowedParams( $flags = 0 ) {
188 ParamValidator
::PARAM_TYPE
=> [ '90', '180', '270' ],
189 ParamValidator
::PARAM_REQUIRED
=> true
192 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
195 ParamValidator
::PARAM_TYPE
=> 'tags',
196 ParamValidator
::PARAM_ISMULTI
=> true,
200 $result +
= $this->getPageSet()->getFinalParams( $flags );
206 public function needsToken() {
210 protected function getExamplesMessages() {
212 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
213 => 'apihelp-imagerotate-example-simple',
214 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
215 'rotation=180&token=123ABC'
216 => 'apihelp-imagerotate-example-generator',
220 public function getHelpUrls() {
221 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Imagerotate';
225 /** @deprecated class alias since 1.43 */
226 class_alias( ApiImageRotate
::class, 'ApiImageRotate' );