Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 117
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiImageRotate
0.00% covered (danger)
0.00%
0 / 117
0.00% covered (danger)
0.00%
0 / 9
506
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 84
0.00% covered (danger)
0.00%
0 / 1
182
 getPageSet
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 mustBePosted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isWriteMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
6
 needsToken
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21use MediaWiki\FileBackend\FSFile\TempFSFileFactory;
22use MediaWiki\Status\Status;
23use MediaWiki\Title\TitleFactory;
24use Wikimedia\ParamValidator\ParamValidator;
25
26/**
27 * @ingroup API
28 */
29class ApiImageRotate extends ApiBase {
30    private $mPageSet = null;
31
32    private RepoGroup $repoGroup;
33    private TempFSFileFactory $tempFSFileFactory;
34    private TitleFactory $titleFactory;
35
36    /**
37     * @param ApiMain $mainModule
38     * @param string $moduleName
39     * @param RepoGroup $repoGroup
40     * @param TempFSFileFactory $tempFSFileFactory
41     * @param TitleFactory $titleFactory
42     */
43    public function __construct(
44        ApiMain $mainModule,
45        $moduleName,
46        RepoGroup $repoGroup,
47        TempFSFileFactory $tempFSFileFactory,
48        TitleFactory $titleFactory
49    ) {
50        parent::__construct( $mainModule, $moduleName );
51        $this->repoGroup = $repoGroup;
52        $this->tempFSFileFactory = $tempFSFileFactory;
53        $this->titleFactory = $titleFactory;
54    }
55
56    public function execute() {
57        $this->useTransactionalTimeLimit();
58
59        $params = $this->extractRequestParams();
60        $rotation = $params['rotation'];
61
62        $continuationManager = new ApiContinuationManager( $this, [], [] );
63        $this->setContinuationManager( $continuationManager );
64
65        $pageSet = $this->getPageSet();
66        $pageSet->execute();
67
68        $result = $pageSet->getInvalidTitlesAndRevisions( [
69            'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
70        ] );
71
72        // Check if user can add tags
73        if ( $params['tags'] ) {
74            $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
75            if ( !$ableToTag->isOK() ) {
76                $this->dieStatus( $ableToTag );
77            }
78        }
79
80        foreach ( $pageSet->getPages() as $page ) {
81            $title = $this->titleFactory->newFromPageIdentity( $page );
82            $r = [];
83            $r['id'] = $title->getArticleID();
84            ApiQueryBase::addTitleInfo( $r, $title );
85            if ( !$title->exists() ) {
86                $r['missing'] = true;
87                if ( $title->isKnown() ) {
88                    $r['known'] = true;
89                }
90            }
91
92            $file = $this->repoGroup->findFile( $title, [ 'latest' => true ] );
93            if ( !$file ) {
94                $r['result'] = 'Failure';
95                $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
96                    Status::newFatal( 'apierror-filedoesnotexist' )
97                );
98                $result[] = $r;
99                continue;
100            }
101            $handler = $file->getHandler();
102            if ( !$handler || !$handler->canRotate() ) {
103                $r['result'] = 'Failure';
104                $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
105                    Status::newFatal( 'apierror-filetypecannotberotated' )
106                );
107                $result[] = $r;
108                continue;
109            }
110
111            // Check whether we're allowed to rotate this file
112            $this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] );
113
114            $srcPath = $file->getLocalRefPath();
115            if ( $srcPath === false ) {
116                $r['result'] = 'Failure';
117                $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
118                    Status::newFatal( 'apierror-filenopath' )
119                );
120                $result[] = $r;
121                continue;
122            }
123            $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
124            $tmpFile = $this->tempFSFileFactory->newTempFSFile( 'rotate_', $ext );
125            $dstPath = $tmpFile->getPath();
126            // @phan-suppress-next-line PhanUndeclaredMethod
127            $err = $handler->rotate( $file, [
128                'srcPath' => $srcPath,
129                'dstPath' => $dstPath,
130                'rotation' => $rotation
131            ] );
132            if ( !$err ) {
133                $comment = $this->msg(
134                    'rotate-comment'
135                )->numParams( $rotation )->inContentLanguage()->text();
136                // @phan-suppress-next-line PhanUndeclaredMethod
137                $status = $file->upload(
138                    $dstPath,
139                    $comment,
140                    $comment,
141                    0,
142                    false,
143                    false,
144                    $this->getAuthority(),
145                    $params['tags'] ?: []
146                );
147                if ( $status->isGood() ) {
148                    $r['result'] = 'Success';
149                } else {
150                    $r['result'] = 'Failure';
151                    $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
152                }
153            } else {
154                $r['result'] = 'Failure';
155                $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
156                    Status::newFatal( ApiMessage::create( $err->getMsg() ) )
157                );
158            }
159            $result[] = $r;
160        }
161        $apiResult = $this->getResult();
162        ApiResult::setIndexedTagName( $result, 'page' );
163        $apiResult->addValue( null, $this->getModuleName(), $result );
164
165        $this->setContinuationManager( null );
166        $continuationManager->setContinuationIntoResult( $apiResult );
167    }
168
169    /**
170     * Get a cached instance of an ApiPageSet object
171     * @return ApiPageSet
172     */
173    private function getPageSet() {
174        $this->mPageSet ??= new ApiPageSet( $this, 0, NS_FILE );
175
176        return $this->mPageSet;
177    }
178
179    public function mustBePosted() {
180        return true;
181    }
182
183    public function isWriteMode() {
184        return true;
185    }
186
187    public function getAllowedParams( $flags = 0 ) {
188        $result = [
189            'rotation' => [
190                ParamValidator::PARAM_TYPE => [ '90', '180', '270' ],
191                ParamValidator::PARAM_REQUIRED => true
192            ],
193            'continue' => [
194                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
195            ],
196            'tags' => [
197                ParamValidator::PARAM_TYPE => 'tags',
198                ParamValidator::PARAM_ISMULTI => true,
199            ],
200        ];
201        if ( $flags ) {
202            $result += $this->getPageSet()->getFinalParams( $flags );
203        }
204
205        return $result;
206    }
207
208    public function needsToken() {
209        return 'csrf';
210    }
211
212    protected function getExamplesMessages() {
213        return [
214            'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
215                => 'apihelp-imagerotate-example-simple',
216            'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
217                'rotation=180&token=123ABC'
218                => 'apihelp-imagerotate-example-generator',
219        ];
220    }
221
222    public function getHelpUrls() {
223        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Imagerotate';
224    }
225}