Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[lhc/web/wiklou.git] / includes / api / ApiImageRotate.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 /**
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 */
23
24 class ApiImageRotate extends ApiBase {
25 private $mPageSet = null;
26
27 public function execute() {
28 $this->useTransactionalTimeLimit();
29
30 $params = $this->extractRequestParams();
31 $rotation = $params['rotation'];
32
33 $continuationManager = new ApiContinuationManager( $this, [], [] );
34 $this->setContinuationManager( $continuationManager );
35
36 $pageSet = $this->getPageSet();
37 $pageSet->execute();
38
39 $result = $pageSet->getInvalidTitlesAndRevisions( [
40 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
41 ] );
42
43 // Check if user can add tags
44 if ( $params['tags'] ) {
45 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getUser() );
46 if ( !$ableToTag->isOK() ) {
47 $this->dieStatus( $ableToTag );
48 }
49 }
50
51 foreach ( $pageSet->getTitles() as $title ) {
52 $r = [];
53 $r['id'] = $title->getArticleID();
54 ApiQueryBase::addTitleInfo( $r, $title );
55 if ( !$title->exists() ) {
56 $r['missing'] = true;
57 if ( $title->isKnown() ) {
58 $r['known'] = true;
59 }
60 }
61
62 $file = MediaWikiServices::getInstance()->getRepoGroup()->findFile(
63 $title, [ 'latest' => true ]
64 );
65 if ( !$file ) {
66 $r['result'] = 'Failure';
67 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
68 Status::newFatal( 'apierror-filedoesnotexist' )
69 );
70 $result[] = $r;
71 continue;
72 }
73 $handler = $file->getHandler();
74 if ( !$handler || !$handler->canRotate() ) {
75 $r['result'] = 'Failure';
76 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
77 Status::newFatal( 'apierror-filetypecannotberotated' )
78 );
79 $result[] = $r;
80 continue;
81 }
82
83 // Check whether we're allowed to rotate this file
84 $permError = $this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] );
85 if ( $permError ) {
86 $r['result'] = 'Failure';
87 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
88 $this->errorArrayToStatus( $permError )
89 );
90 $result[] = $r;
91 continue;
92 }
93
94 $srcPath = $file->getLocalRefPath();
95 if ( $srcPath === false ) {
96 $r['result'] = 'Failure';
97 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
98 Status::newFatal( 'apierror-filenopath' )
99 );
100 $result[] = $r;
101 continue;
102 }
103 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
104 $tmpFile = MediaWikiServices::getInstance()->getTempFSFileFactory()
105 ->newTempFSFile( 'rotate_', $ext );
106 $dstPath = $tmpFile->getPath();
107 // @phan-suppress-next-line PhanUndeclaredMethod
108 $err = $handler->rotate( $file, [
109 'srcPath' => $srcPath,
110 'dstPath' => $dstPath,
111 'rotation' => $rotation
112 ] );
113 if ( !$err ) {
114 $comment = wfMessage(
115 'rotate-comment'
116 )->numParams( $rotation )->inContentLanguage()->text();
117 // @phan-suppress-next-line PhanUndeclaredMethod
118 $status = $file->upload(
119 $dstPath,
120 $comment,
121 $comment,
122 0,
123 false,
124 false,
125 $this->getUser(),
126 $params['tags'] ?: []
127 );
128 if ( $status->isGood() ) {
129 $r['result'] = 'Success';
130 } else {
131 $r['result'] = 'Failure';
132 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
133 }
134 } else {
135 $r['result'] = 'Failure';
136 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
137 Status::newFatal( ApiMessage::create( $err->getMsg() ) )
138 );
139 }
140 $result[] = $r;
141 }
142 $apiResult = $this->getResult();
143 ApiResult::setIndexedTagName( $result, 'page' );
144 $apiResult->addValue( null, $this->getModuleName(), $result );
145
146 $this->setContinuationManager( null );
147 $continuationManager->setContinuationIntoResult( $apiResult );
148 }
149
150 /**
151 * Get a cached instance of an ApiPageSet object
152 * @return ApiPageSet
153 */
154 private function getPageSet() {
155 if ( $this->mPageSet === null ) {
156 $this->mPageSet = new ApiPageSet( $this, 0, NS_FILE );
157 }
158
159 return $this->mPageSet;
160 }
161
162 public function mustBePosted() {
163 return true;
164 }
165
166 public function isWriteMode() {
167 return true;
168 }
169
170 public function getAllowedParams( $flags = 0 ) {
171 $result = [
172 'rotation' => [
173 ApiBase::PARAM_TYPE => [ '90', '180', '270' ],
174 ApiBase::PARAM_REQUIRED => true
175 ],
176 'continue' => [
177 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
178 ],
179 'tags' => [
180 ApiBase::PARAM_TYPE => 'tags',
181 ApiBase::PARAM_ISMULTI => true,
182 ],
183 ];
184 if ( $flags ) {
185 $result += $this->getPageSet()->getFinalParams( $flags );
186 }
187
188 return $result;
189 }
190
191 public function needsToken() {
192 return 'csrf';
193 }
194
195 protected function getExamplesMessages() {
196 return [
197 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
198 => 'apihelp-imagerotate-example-simple',
199 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
200 'rotation=180&token=123ABC'
201 => 'apihelp-imagerotate-example-generator',
202 ];
203 }
204 }