Merge "Http::getProxy() method to get proxy configuration"
[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 /**
28 * Add all items from $values into the result
29 * @param array $result Output
30 * @param array $values Values to add
31 * @param string $flag The name of the boolean flag to mark this element
32 * @param string $name If given, name of the value
33 */
34 private static function addValues( array &$result, $values, $flag = null, $name = null ) {
35 foreach ( $values as $val ) {
36 if ( $val instanceof Title ) {
37 $v = [];
38 ApiQueryBase::addTitleInfo( $v, $val );
39 } elseif ( $name !== null ) {
40 $v = [ $name => $val ];
41 } else {
42 $v = $val;
43 }
44 if ( $flag !== null ) {
45 $v[$flag] = true;
46 }
47 $result[] = $v;
48 }
49 }
50
51 public function execute() {
52 $this->useTransactionalTimeLimit();
53
54 $params = $this->extractRequestParams();
55 $rotation = $params['rotation'];
56
57 $continuationManager = new ApiContinuationManager( $this, [], [] );
58 $this->setContinuationManager( $continuationManager );
59
60 $pageSet = $this->getPageSet();
61 $pageSet->execute();
62
63 $result = [];
64
65 self::addValues( $result, $pageSet->getInvalidTitlesAndReasons(), 'invalid' );
66 self::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' );
67 self::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' );
68 self::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' );
69 self::addValues( $result, $pageSet->getInterwikiTitlesAsResult() );
70
71 foreach ( $pageSet->getTitles() as $title ) {
72 $r = [];
73 $r['id'] = $title->getArticleID();
74 ApiQueryBase::addTitleInfo( $r, $title );
75 if ( !$title->exists() ) {
76 $r['missing'] = true;
77 }
78
79 $file = wfFindFile( $title, [ 'latest' => true ] );
80 if ( !$file ) {
81 $r['result'] = 'Failure';
82 $r['errormessage'] = 'File does not exist';
83 $result[] = $r;
84 continue;
85 }
86 $handler = $file->getHandler();
87 if ( !$handler || !$handler->canRotate() ) {
88 $r['result'] = 'Failure';
89 $r['errormessage'] = 'File type cannot be rotated';
90 $result[] = $r;
91 continue;
92 }
93
94 // Check whether we're allowed to rotate this file
95 $permError = $this->checkPermissions( $this->getUser(), $file->getTitle() );
96 if ( $permError !== null ) {
97 $r['result'] = 'Failure';
98 $r['errormessage'] = $permError;
99 $result[] = $r;
100 continue;
101 }
102
103 $srcPath = $file->getLocalRefPath();
104 if ( $srcPath === false ) {
105 $r['result'] = 'Failure';
106 $r['errormessage'] = 'Cannot get local file path';
107 $result[] = $r;
108 continue;
109 }
110 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
111 $tmpFile = TempFSFile::factory( 'rotate_', $ext );
112 $dstPath = $tmpFile->getPath();
113 $err = $handler->rotate( $file, [
114 'srcPath' => $srcPath,
115 'dstPath' => $dstPath,
116 'rotation' => $rotation
117 ] );
118 if ( !$err ) {
119 $comment = wfMessage(
120 'rotate-comment'
121 )->numParams( $rotation )->inContentLanguage()->text();
122 $status = $file->upload( $dstPath,
123 $comment, $comment, 0, false, false, $this->getUser() );
124 if ( $status->isGood() ) {
125 $r['result'] = 'Success';
126 } else {
127 $r['result'] = 'Failure';
128 $r['errormessage'] = $this->getErrorFormatter()->arrayFromStatus( $status );
129 }
130 } else {
131 $r['result'] = 'Failure';
132 $r['errormessage'] = $err->toText();
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 /**
157 * Checks that the user has permissions to perform rotations.
158 * @param User $user The user to check
159 * @param Title $title
160 * @return string|null Permission error message, or null if there is no error
161 */
162 protected function checkPermissions( $user, $title ) {
163 $permissionErrors = array_merge(
164 $title->getUserPermissionsErrors( 'edit', $user ),
165 $title->getUserPermissionsErrors( 'upload', $user )
166 );
167
168 if ( $permissionErrors ) {
169 // Just return the first error
170 $msg = $this->parseMsg( $permissionErrors[0] );
171
172 return $msg['info'];
173 }
174
175 return null;
176 }
177
178 public function mustBePosted() {
179 return true;
180 }
181
182 public function isWriteMode() {
183 return true;
184 }
185
186 public function getAllowedParams( $flags = 0 ) {
187 $result = [
188 'rotation' => [
189 ApiBase::PARAM_TYPE => [ '90', '180', '270' ],
190 ApiBase::PARAM_REQUIRED => true
191 ],
192 'continue' => [
193 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
194 ],
195 ];
196 if ( $flags ) {
197 $result += $this->getPageSet()->getFinalParams( $flags );
198 }
199
200 return $result;
201 }
202
203 public function needsToken() {
204 return 'csrf';
205 }
206
207 protected function getExamplesMessages() {
208 return [
209 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
210 => 'apihelp-imagerotate-example-simple',
211 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
212 'rotation=180&token=123ABC'
213 => 'apihelp-imagerotate-example-generator',
214 ];
215 }
216 }