Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiImageRotate.php
blob9aaa918ffc3aac2cc4c95e0cdeb9666193f5c1d5
1 <?php
2 /**
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
18 * @file
21 namespace MediaWiki\Api;
23 use ChangeTags;
24 use MediaWiki\FileBackend\FSFile\TempFSFileFactory;
25 use MediaWiki\Status\Status;
26 use MediaWiki\Title\TitleFactory;
27 use RepoGroup;
28 use Wikimedia\ParamValidator\ParamValidator;
30 /**
31 * @ingroup API
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(
42 ApiMain $mainModule,
43 string $moduleName,
44 RepoGroup $repoGroup,
45 TempFSFileFactory $tempFSFileFactory,
46 TitleFactory $titleFactory
47 ) {
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();
64 $pageSet->execute();
66 $result = $pageSet->getInvalidTitlesAndRevisions( [
67 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
68 ] );
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 );
80 $r = [];
81 $r['id'] = $title->getArticleID();
82 ApiQueryBase::addTitleInfo( $r, $title );
83 if ( !$title->exists() ) {
84 $r['missing'] = true;
85 if ( $title->isKnown() ) {
86 $r['known'] = true;
90 $file = $this->repoGroup->findFile( $title, [ 'latest' => true ] );
91 if ( !$file ) {
92 $r['result'] = 'Failure';
93 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
94 Status::newFatal( 'apierror-filedoesnotexist' )
96 $result[] = $r;
97 continue;
99 $handler = $file->getHandler();
100 if ( !$handler || !$handler->canRotate() ) {
101 $r['result'] = 'Failure';
102 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
103 Status::newFatal( 'apierror-filetypecannotberotated' )
105 $result[] = $r;
106 continue;
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' )
118 $result[] = $r;
119 continue;
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
129 ] );
130 if ( !$err ) {
131 $comment = $this->msg(
132 'rotate-comment'
133 )->numParams( $rotation )->inContentLanguage()->text();
134 // @phan-suppress-next-line PhanUndeclaredMethod
135 $status = $file->upload(
136 $dstPath,
137 $comment,
138 $comment,
140 false,
141 false,
142 $this->getAuthority(),
143 $params['tags'] ?: []
145 if ( $status->isGood() ) {
146 $r['result'] = 'Success';
147 } else {
148 $r['result'] = 'Failure';
149 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
151 } else {
152 $r['result'] = 'Failure';
153 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
154 Status::newFatal( ApiMessage::create( $err->getMsg() ) )
157 $result[] = $r;
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
169 * @return ApiPageSet
171 private function getPageSet() {
172 $this->mPageSet ??= new ApiPageSet( $this, 0, NS_FILE );
174 return $this->mPageSet;
177 public function mustBePosted() {
178 return true;
181 public function isWriteMode() {
182 return true;
185 public function getAllowedParams( $flags = 0 ) {
186 $result = [
187 'rotation' => [
188 ParamValidator::PARAM_TYPE => [ '90', '180', '270' ],
189 ParamValidator::PARAM_REQUIRED => true
191 'continue' => [
192 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
194 'tags' => [
195 ParamValidator::PARAM_TYPE => 'tags',
196 ParamValidator::PARAM_ISMULTI => true,
199 if ( $flags ) {
200 $result += $this->getPageSet()->getFinalParams( $flags );
203 return $result;
206 public function needsToken() {
207 return 'csrf';
210 protected function getExamplesMessages() {
211 return [
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' );