Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 118 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
ApiImageRotate | |
0.00% |
0 / 117 |
|
0.00% |
0 / 9 |
506 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 84 |
|
0.00% |
0 / 1 |
182 | |||
getPageSet | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
mustBePosted | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isWriteMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getAllowedParams | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
6 | |||
needsToken | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
getHelpUrls | |
0.00% |
0 / 1 |
|
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 | |
21 | namespace MediaWiki\Api; |
22 | |
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; |
29 | |
30 | /** |
31 | * @ingroup API |
32 | */ |
33 | class ApiImageRotate extends ApiBase { |
34 | /** @var ApiPageSet|null */ |
35 | private $mPageSet = null; |
36 | |
37 | private RepoGroup $repoGroup; |
38 | private TempFSFileFactory $tempFSFileFactory; |
39 | private TitleFactory $titleFactory; |
40 | |
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; |
52 | } |
53 | |
54 | public function execute() { |
55 | $this->useTransactionalTimeLimit(); |
56 | |
57 | $params = $this->extractRequestParams(); |
58 | $rotation = $params['rotation']; |
59 | |
60 | $continuationManager = new ApiContinuationManager( $this, [], [] ); |
61 | $this->setContinuationManager( $continuationManager ); |
62 | |
63 | $pageSet = $this->getPageSet(); |
64 | $pageSet->execute(); |
65 | |
66 | $result = $pageSet->getInvalidTitlesAndRevisions( [ |
67 | 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles', |
68 | ] ); |
69 | |
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 ); |
75 | } |
76 | } |
77 | |
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; |
87 | } |
88 | } |
89 | |
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' ) |
95 | ); |
96 | $result[] = $r; |
97 | continue; |
98 | } |
99 | $handler = $file->getHandler(); |
100 | if ( !$handler || !$handler->canRotate() ) { |
101 | $r['result'] = 'Failure'; |
102 | $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( |
103 | Status::newFatal( 'apierror-filetypecannotberotated' ) |
104 | ); |
105 | $result[] = $r; |
106 | continue; |
107 | } |
108 | |
109 | // Check whether we're allowed to rotate this file |
110 | $this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] ); |
111 | |
112 | $srcPath = $file->getLocalRefPath(); |
113 | if ( $srcPath === false ) { |
114 | $r['result'] = 'Failure'; |
115 | $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( |
116 | Status::newFatal( 'apierror-filenopath' ) |
117 | ); |
118 | $result[] = $r; |
119 | continue; |
120 | } |
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, |
139 | 0, |
140 | false, |
141 | false, |
142 | $this->getAuthority(), |
143 | $params['tags'] ?: [] |
144 | ); |
145 | if ( $status->isGood() ) { |
146 | $r['result'] = 'Success'; |
147 | } else { |
148 | $r['result'] = 'Failure'; |
149 | $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status ); |
150 | } |
151 | } else { |
152 | $r['result'] = 'Failure'; |
153 | $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( |
154 | Status::newFatal( ApiMessage::create( $err->getMsg() ) ) |
155 | ); |
156 | } |
157 | $result[] = $r; |
158 | } |
159 | $apiResult = $this->getResult(); |
160 | ApiResult::setIndexedTagName( $result, 'page' ); |
161 | $apiResult->addValue( null, $this->getModuleName(), $result ); |
162 | |
163 | $this->setContinuationManager( null ); |
164 | $continuationManager->setContinuationIntoResult( $apiResult ); |
165 | } |
166 | |
167 | /** |
168 | * Get a cached instance of an ApiPageSet object |
169 | * @return ApiPageSet |
170 | */ |
171 | private function getPageSet() { |
172 | $this->mPageSet ??= new ApiPageSet( $this, 0, NS_FILE ); |
173 | |
174 | return $this->mPageSet; |
175 | } |
176 | |
177 | public function mustBePosted() { |
178 | return true; |
179 | } |
180 | |
181 | public function isWriteMode() { |
182 | return true; |
183 | } |
184 | |
185 | public function getAllowedParams( $flags = 0 ) { |
186 | $result = [ |
187 | 'rotation' => [ |
188 | ParamValidator::PARAM_TYPE => [ '90', '180', '270' ], |
189 | ParamValidator::PARAM_REQUIRED => true |
190 | ], |
191 | 'continue' => [ |
192 | ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
193 | ], |
194 | 'tags' => [ |
195 | ParamValidator::PARAM_TYPE => 'tags', |
196 | ParamValidator::PARAM_ISMULTI => true, |
197 | ], |
198 | ]; |
199 | if ( $flags ) { |
200 | $result += $this->getPageSet()->getFinalParams( $flags ); |
201 | } |
202 | |
203 | return $result; |
204 | } |
205 | |
206 | public function needsToken() { |
207 | return 'csrf'; |
208 | } |
209 | |
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', |
217 | ]; |
218 | } |
219 | |
220 | public function getHelpUrls() { |
221 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Imagerotate'; |
222 | } |
223 | } |
224 | |
225 | /** @deprecated class alias since 1.43 */ |
226 | class_alias( ApiImageRotate::class, 'ApiImageRotate' ); |