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