Merge "Revert "Use display name in category page subheadings if provided""
[lhc/web/wiklou.git] / includes / api / ApiImageRotate.php
1 <?php
2 /**
3 *
4 * Created on January 3rd, 2013
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 = [];
40
41 $result = $pageSet->getInvalidTitlesAndRevisions( [
42 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
43 ] );
44
45 foreach ( $pageSet->getTitles() as $title ) {
46 $r = [];
47 $r['id'] = $title->getArticleID();
48 ApiQueryBase::addTitleInfo( $r, $title );
49 if ( !$title->exists() ) {
50 $r['missing'] = true;
51 if ( $title->isKnown() ) {
52 $r['known'] = true;
53 }
54 }
55
56 $file = wfFindFile( $title, [ 'latest' => true ] );
57 if ( !$file ) {
58 $r['result'] = 'Failure';
59 $r['errormessage'] = 'File does not exist';
60 $result[] = $r;
61 continue;
62 }
63 $handler = $file->getHandler();
64 if ( !$handler || !$handler->canRotate() ) {
65 $r['result'] = 'Failure';
66 $r['errormessage'] = 'File type cannot be rotated';
67 $result[] = $r;
68 continue;
69 }
70
71 // Check whether we're allowed to rotate this file
72 $permError = $this->checkPermissions( $this->getUser(), $file->getTitle() );
73 if ( $permError !== null ) {
74 $r['result'] = 'Failure';
75 $r['errormessage'] = $permError;
76 $result[] = $r;
77 continue;
78 }
79
80 $srcPath = $file->getLocalRefPath();
81 if ( $srcPath === false ) {
82 $r['result'] = 'Failure';
83 $r['errormessage'] = 'Cannot get local file path';
84 $result[] = $r;
85 continue;
86 }
87 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
88 $tmpFile = TempFSFile::factory( 'rotate_', $ext, wfTempDir() );
89 $dstPath = $tmpFile->getPath();
90 $err = $handler->rotate( $file, [
91 'srcPath' => $srcPath,
92 'dstPath' => $dstPath,
93 'rotation' => $rotation
94 ] );
95 if ( !$err ) {
96 $comment = wfMessage(
97 'rotate-comment'
98 )->numParams( $rotation )->inContentLanguage()->text();
99 $status = $file->upload( $dstPath,
100 $comment, $comment, 0, false, false, $this->getUser() );
101 if ( $status->isGood() ) {
102 $r['result'] = 'Success';
103 } else {
104 $r['result'] = 'Failure';
105 $r['errormessage'] = $this->getErrorFormatter()->arrayFromStatus( $status );
106 }
107 } else {
108 $r['result'] = 'Failure';
109 $r['errormessage'] = $err->toText();
110 }
111 $result[] = $r;
112 }
113 $apiResult = $this->getResult();
114 ApiResult::setIndexedTagName( $result, 'page' );
115 $apiResult->addValue( null, $this->getModuleName(), $result );
116
117 $this->setContinuationManager( null );
118 $continuationManager->setContinuationIntoResult( $apiResult );
119 }
120
121 /**
122 * Get a cached instance of an ApiPageSet object
123 * @return ApiPageSet
124 */
125 private function getPageSet() {
126 if ( $this->mPageSet === null ) {
127 $this->mPageSet = new ApiPageSet( $this, 0, NS_FILE );
128 }
129
130 return $this->mPageSet;
131 }
132
133 /**
134 * Checks that the user has permissions to perform rotations.
135 * @param User $user The user to check
136 * @param Title $title
137 * @return string|null Permission error message, or null if there is no error
138 */
139 protected function checkPermissions( $user, $title ) {
140 $permissionErrors = array_merge(
141 $title->getUserPermissionsErrors( 'edit', $user ),
142 $title->getUserPermissionsErrors( 'upload', $user )
143 );
144
145 if ( $permissionErrors ) {
146 // Just return the first error
147 $msg = $this->parseMsg( $permissionErrors[0] );
148
149 return $msg['info'];
150 }
151
152 return null;
153 }
154
155 public function mustBePosted() {
156 return true;
157 }
158
159 public function isWriteMode() {
160 return true;
161 }
162
163 public function getAllowedParams( $flags = 0 ) {
164 $result = [
165 'rotation' => [
166 ApiBase::PARAM_TYPE => [ '90', '180', '270' ],
167 ApiBase::PARAM_REQUIRED => true
168 ],
169 'continue' => [
170 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
171 ],
172 ];
173 if ( $flags ) {
174 $result += $this->getPageSet()->getFinalParams( $flags );
175 }
176
177 return $result;
178 }
179
180 public function needsToken() {
181 return 'csrf';
182 }
183
184 protected function getExamplesMessages() {
185 return [
186 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
187 => 'apihelp-imagerotate-example-simple',
188 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
189 'rotation=180&token=123ABC'
190 => 'apihelp-imagerotate-example-generator',
191 ];
192 }
193 }