Merge "StringUtils: Add a utility for checking if a string is a valid regex"
[lhc/web/wiklou.git] / includes / api / ApiImageRotate.php
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 use MediaWiki\MediaWikiServices;
22
23 /**
24 * @ingroup API
25 */
26 class ApiImageRotate extends ApiBase {
27 private $mPageSet = null;
28
29 public function execute() {
30 $this->useTransactionalTimeLimit();
31
32 $params = $this->extractRequestParams();
33 $rotation = $params['rotation'];
34
35 $continuationManager = new ApiContinuationManager( $this, [], [] );
36 $this->setContinuationManager( $continuationManager );
37
38 $pageSet = $this->getPageSet();
39 $pageSet->execute();
40
41 $result = $pageSet->getInvalidTitlesAndRevisions( [
42 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
43 ] );
44
45 // Check if user can add tags
46 if ( $params['tags'] ) {
47 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getUser() );
48 if ( !$ableToTag->isOK() ) {
49 $this->dieStatus( $ableToTag );
50 }
51 }
52
53 foreach ( $pageSet->getTitles() as $title ) {
54 $r = [];
55 $r['id'] = $title->getArticleID();
56 ApiQueryBase::addTitleInfo( $r, $title );
57 if ( !$title->exists() ) {
58 $r['missing'] = true;
59 if ( $title->isKnown() ) {
60 $r['known'] = true;
61 }
62 }
63
64 $file = MediaWikiServices::getInstance()->getRepoGroup()->findFile(
65 $title, [ 'latest' => true ]
66 );
67 if ( !$file ) {
68 $r['result'] = 'Failure';
69 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
70 Status::newFatal( 'apierror-filedoesnotexist' )
71 );
72 $result[] = $r;
73 continue;
74 }
75 $handler = $file->getHandler();
76 if ( !$handler || !$handler->canRotate() ) {
77 $r['result'] = 'Failure';
78 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
79 Status::newFatal( 'apierror-filetypecannotberotated' )
80 );
81 $result[] = $r;
82 continue;
83 }
84
85 // Check whether we're allowed to rotate this file
86 $permError = $this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] );
87 if ( $permError ) {
88 $r['result'] = 'Failure';
89 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
90 $this->errorArrayToStatus( $permError )
91 );
92 $result[] = $r;
93 continue;
94 }
95
96 $srcPath = $file->getLocalRefPath();
97 if ( $srcPath === false ) {
98 $r['result'] = 'Failure';
99 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
100 Status::newFatal( 'apierror-filenopath' )
101 );
102 $result[] = $r;
103 continue;
104 }
105 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
106 $tmpFile = MediaWikiServices::getInstance()->getTempFSFileFactory()
107 ->newTempFSFile( 'rotate_', $ext );
108 $dstPath = $tmpFile->getPath();
109 // @phan-suppress-next-line PhanUndeclaredMethod
110 $err = $handler->rotate( $file, [
111 'srcPath' => $srcPath,
112 'dstPath' => $dstPath,
113 'rotation' => $rotation
114 ] );
115 if ( !$err ) {
116 $comment = wfMessage(
117 'rotate-comment'
118 )->numParams( $rotation )->inContentLanguage()->text();
119 // @phan-suppress-next-line PhanUndeclaredMethod
120 $status = $file->upload(
121 $dstPath,
122 $comment,
123 $comment,
124 0,
125 false,
126 false,
127 $this->getUser(),
128 $params['tags'] ?: []
129 );
130 if ( $status->isGood() ) {
131 $r['result'] = 'Success';
132 } else {
133 $r['result'] = 'Failure';
134 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
135 }
136 } else {
137 $r['result'] = 'Failure';
138 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
139 Status::newFatal( ApiMessage::create( $err->getMsg() ) )
140 );
141 }
142 $result[] = $r;
143 }
144 $apiResult = $this->getResult();
145 ApiResult::setIndexedTagName( $result, 'page' );
146 $apiResult->addValue( null, $this->getModuleName(), $result );
147
148 $this->setContinuationManager( null );
149 $continuationManager->setContinuationIntoResult( $apiResult );
150 }
151
152 /**
153 * Get a cached instance of an ApiPageSet object
154 * @return ApiPageSet
155 */
156 private function getPageSet() {
157 if ( $this->mPageSet === null ) {
158 $this->mPageSet = new ApiPageSet( $this, 0, NS_FILE );
159 }
160
161 return $this->mPageSet;
162 }
163
164 public function mustBePosted() {
165 return true;
166 }
167
168 public function isWriteMode() {
169 return true;
170 }
171
172 public function getAllowedParams( $flags = 0 ) {
173 $result = [
174 'rotation' => [
175 ApiBase::PARAM_TYPE => [ '90', '180', '270' ],
176 ApiBase::PARAM_REQUIRED => true
177 ],
178 'continue' => [
179 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
180 ],
181 'tags' => [
182 ApiBase::PARAM_TYPE => 'tags',
183 ApiBase::PARAM_ISMULTI => true,
184 ],
185 ];
186 if ( $flags ) {
187 $result += $this->getPageSet()->getFinalParams( $flags );
188 }
189
190 return $result;
191 }
192
193 public function needsToken() {
194 return 'csrf';
195 }
196
197 protected function getExamplesMessages() {
198 return [
199 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
200 => 'apihelp-imagerotate-example-simple',
201 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
202 'rotation=180&token=123ABC'
203 => 'apihelp-imagerotate-example-generator',
204 ];
205 }
206 }